require('../config.php');
require_once($CFG->libdir.'/filelib.php');
- $contextid = required_param('contextid', PARAM_INT);
- $filearea = required_param('filearea', PARAM_ALPHAEXT);
$itemid = required_param('itemid', PARAM_INT);
$filepath = optional_param('filepath', '/', PARAM_PATH);
$newdirname = optional_param('newdirname', '', PARAM_FILE);
$delete = optional_param('delete', 0, PARAM_BOOL);
- if (!$context = get_context_instance_by_id($contextid)) {
- print_error('invalidcontext');
- }
-
require_login();
if (isguestuser()) {
print_error('noguest');
}
+ if (!$context = get_context_instance(CONTEXT_USER, $USER->id)) {
+ print_error('invalidcontext');
+ }
+
+ $contextid = $context->id;
+ $filearea = 'user_draft';
+
$browser = get_file_browser();
if (!$area_info = $browser->get_file_info($context, $filearea, $itemid, '/', null)) {
if ($newdir_info = $file_info->create_directory($newdirname, $USER->id)) {
$params = $newdir_info->get_params_rawencoded();
$params = implode('&', $params);
- redirect("areafiles.php?$params");
+ redirect("draftfiles.php?$params");
} else {
$error = "Could not create new dir"; // TODO: localise
}
if ($newfile = $file_info->create_file_from_pathname($newfilename, $_FILES['newfile']['tmp_name'], $USER->id)) {
$params = $file_info->get_params_rawencoded();
$params = implode('&', $params);
- redirect("areafiles.php?$params");
+ redirect("draftfiles.php?$params");
} else {
$error = "Could not create upload file"; // TODO: localise
$optionsyes['delete'] = 1;
$optionsyes['sesskey'] = sesskey();
- notice_yesno (get_string('deletecheckfiles'), 'areafiles.php', 'areafiles.php', $optionsyes, $optionsno, 'post', 'get');
+ notice_yesno (get_string('deletecheckfiles'), 'draftfiles.php', 'draftfiles.php', $optionsyes, $optionsno, 'post', 'get');
print_footer('empty');
die;
}
}
$params = $parent_info->get_params_rawencoded();
$params = implode('&', $params);
- redirect("areafiles.php?$params", $error);
+ redirect("draftfiles.php?$params", $error);
}
}
if ($file_info and $file_info->is_directory() and $file_info->is_writable()) {
- echo '<form enctype="multipart/form-data" method="post" action="areafiles.php"><div>';
+ echo '<form enctype="multipart/form-data" method="post" action="draftfiles.php"><div>';
echo '<input type="hidden" name="contextid" value="'.$contextid.'" />';
echo '<input type="hidden" name="filearea" value="'.$filearea.'" />';
echo '<input type="hidden" name="itemid" value="'.$itemid.'" />';
echo '<input type="submit" value="'.get_string('uploadafile').'" />';
echo '</div></form>';
- echo '<form action="areafiles.php" method="post"><div>';
+ echo '<form action="draftfiles.php" method="post"><div>';
echo '<input type="hidden" name="contextid" value="'.$contextid.'" />';
echo '<input type="hidden" name="filearea" value="'.$filearea.'" />';
echo '<input type="hidden" name="itemid" value="'.$itemid.'" />';
$params = implode('&', $params);
echo '<div class="folder">';
- echo '<a href="areafiles.php?'.$params.'"><img src="'.$CFG->pixpath.'/f/parent.gif" class="icon" alt="" /> '.get_string('parentfolder').'</a>';
+ echo '<a href="draftfiles.php?'.$params.'"><img src="'.$CFG->pixpath.'/f/parent.gif" class="icon" alt="" /> '.get_string('parentfolder').'</a>';
echo '</div>';
}
if ($child_info->is_directory()) {
echo '<div class="folder">';
- echo "<a href=\"areafiles.php?$params\"><img src=\"$CFG->pixpath/f/folder.gif\" class=\"icon\" alt=\"$strfolder\" /> ".s($filename)."</a>";
+ echo "<a href=\"draftfiles.php?$params\"><img src=\"$CFG->pixpath/f/folder.gif\" class=\"icon\" alt=\"$strfolder\" /> ".s($filename)."</a>";
if ($parentwritable) {
- echo "<a href=\"areafiles.php?$params&sesskey=".sesskey()."&delete=1\"><img src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strdelete\" /></a>";
+ echo "<a href=\"draftfiles.php?$params&sesskey=".sesskey()."&delete=1\"><img src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strdelete\" /></a>";
}
echo '</div>';
480, 640, get_string('viewfileinpopup'), null, true);
}
if ($parentwritable) {
- echo "<a href=\"areafiles.php?$params&sesskey=".sesskey()."&delete=1\"><img src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strdelete\" /></a>";;
+ echo "<a href=\"draftfiles.php?$params&sesskey=".sesskey()."&delete=1\"><img src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strdelete\" /></a>";;
}
echo '</div>';
}
/**
* Returns empty user upload draft area information
- * @return array with area info
+ * @return int draftareaid
*/
-function get_new_draftarea() {
+function file_get_new_draftitemid() {
global $DB, $USER;
if (isguestuser() or !isloggedin()) {
+ // guests and not-logged-in users can not be allowed to upload anything!
print_error('noguest');
}
$draftitemid = rand(1, 999999999);
}
- return array('contextid'=>$contextid, 'filearea'=>$filearea, 'itemid'=>$draftitemid);
+ return $draftitemid;
}
/**
- * Converts absolute links in text and moves draft files.
- * @param int $draftitemid
+ * Creates new draft area if not exists yet and copies files there
+ * @param int &$draftitemid
* @param int $contextid
* @param string $filearea
* @param int $itemid
* @param string $text usually html text with embedded links to draft area
- * @param boolean $https force https
+ * @param boolean $forcehttps force https
* @return string text with relative links starting with @@PLUGINFILE@@
*/
-function file_convert_draftarea($draftitemid, $contextid, $filearea, $itemid, $text=null, $https=false) {
+function file_prepare_draftarea(&$draftitemid, $contextid, $filearea, $itemid, $text=null, $forcehttps=false) {
global $CFG, $USER;
- /// move draft files first
$usercontext = get_context_instance(CONTEXT_USER, $USER->id);
+ $fs = get_file_storage();
+
+ if (empty($draftitemid)) {
+ // create a new area and copy existing files into
+ $draftitemid = file_get_new_draftitemid();
+ $file_record = array('contextid'=>$usercontext->id, 'filearea'=>'user_draft', 'itemid'=>$draftitemid);
+ if ($files = $fs->get_area_files($contextid, $filearea, $itemid)) {
+ foreach ($files as $file) {
+ $fs->create_file_from_storedfile($file_record, $file);
+ }
+ }
+ } else {
+ // nothing to do
+ }
+
+ if (is_null($text)) {
+ return null;
+ }
+
+ /// relink embedded files - editor can not handle @@PLUGINFILE@@ !
+
+ if ($CFG->slasharguments) {
+ $draftbase = "$CFG->wwwroot/draftfile.php/user_draft/$draftitemid/";
+ } else {
+ $draftbase = "$CFG->wwwroot/draftfile.php?file=/user_draft/$draftitemid/";
+ }
+ if ($forcehttps) {
+ $draftbase = str_replace('http://', 'https://', $draftbase);
+ }
+
+ $text = str_replace('@@PLUGINFILE@@/', $draftbase);
+
+ return $text;
+}
+
+/**
+ * Converts absolute links in text and merges draft files to target area.
+ * @param int $draftitemid
+ * @param int $contextid
+ * @param string $filearea
+ * @param int $itemid
+ * @param string $text usually html text with embedded links to draft area
+ * @param boolean $forcehttps force https
+ * @return string text with relative links starting with @@PLUGINFILE@@
+ */
+function file_convert_draftarea($draftitemid, $contextid, $filearea, $itemid, $text=null, $forcehttps=false) {
+ global $CFG, $USER;
+
+ $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
$fs = get_file_storage();
- if ($files = $fs->get_area_files($usercontext->id, 'user_draft', $draftitemid, 'id', 'false')) {
+
+ $draftfiles = $fs->get_area_files($usercontext->id, 'user_draft', $draftitemid, 'id');
+ $oldfiles = $fs->get_area_files($contextid, $filearea, $itemid, 'id');
+
+ if (count($draftfiles) < 2) {
+ // means there are no files - one file means root dir only ;-)
+ $fs->delete_area_files($contextid, $filearea, $itemid);
+
+ } else if (count($oldfiles) < 2) {
+ // there were no files before - one file means root dir only ;-)
+ $fs->delete_area_files($contextid, $filearea, $itemid);
$file_record = array('contextid'=>$contextid, 'filearea'=>$filearea, 'itemid'=>$itemid);
- foreach ($files as $file) {
- $fs->create_file_from_stored($file_record, $file);
+ foreach ($draftfiles as $file) {
+ $fs->create_file_from_storedfile($file_record, $file);
+ }
+
+ } else {
+ // we have to merge old and new files - we want to keep file ids for files that were not changed
+ $file_record = array('contextid'=>$contextid, 'filearea'=>$filearea, 'itemid'=>$itemid);
+ foreach ($draftfiles as $file) {
+ $newhash = sha1($contextid.$filearea.$itemid.$file->get_filepath().$file->get_filename());
+ if (isset($oldfiles[$newhash])) {
+ $oldfile = $oldfiles[$newhash];
+ unset($oldfiles[$newhash]); // do not delete afterwards
+
+ if (!$file->is_directory()) {
+ if ($file->get_contenthash() === $oldfile->get_contenthash()) {
+ // file was not changed at all
+ continue;
+ } else {
+ // file changed, delete the original
+ $oldfile->delete();
+ }
+ }
+ }
+ $fs->create_file_from_storedfile($file_record, $file);
+ }
+ // cleanup deleted files and dirs
+ foreach ($oldfiles as $file) {
$file->delete();
}
}
+ // purge the draft area
+ $fs->delete_area_files($usercontext->id, 'user_draft', $draftitemid);
+
if (is_null($text)) {
return null;
}
- /// relink embedded files if text submitted - no absolute links allowed!
+ /// relink embedded files if text submitted - no absolute links allowed in database!
if ($CFG->slasharguments) {
$draftbase = "$CFG->wwwroot/draftfile.php/user_draft/$draftitemid/";
$draftbase = "$CFG->wwwroot/draftfile.php?file=/user_draft/$draftitemid/";
}
- if ($https) {
+ if ($forcehttps) {
$draftbase = str_replace('http://', 'https://', $draftbase);
}
}
/**
- * Finds occurences of a link to "draftfile.php" in text and replaces the
- * address based on passed information. Matching is performed using the given
- * current itemid, contextid and filearea and $CFG->wwwroot. This function
- * replaces all the urls for one file. If more than one files were sent, it
+ * Finds occurences of a link to "draftfile.php" in text and replaces the
+ * address based on passed information. Matching is performed using the given
+ * current itemid, contextid and filearea and $CFG->wwwroot. This function
+ * replaces all the urls for one file. If more than one files were sent, it
* must be called once for each file.
*
* @uses $CFG
$text = str_replace('"'. $currenturl .'"', '"'. $newurl .'"', $text);
}
} // else file not found, wrong file, or string is just not a file so we leave it alone.
-
+
}
return $text;
}
}
if (empty($CFG->repository_cache_expire)) {
$CFG->repository_cache_expire = 120;
- }
+ }
}
public function get($param){
global $CFG, $USER;
require_once('HTML/QuickForm/element.php');
class MoodleQuickForm_areafiles extends HTML_QuickForm_element {
- var $_helpbutton = '';
- var $_areainfo = array();
+ protected $_helpbutton = '';
+ protected $_options = null;
- function MoodleQuickForm_files($elementName=null, $elementLabel=null, $attributes=null) {
- parent::HTML_QuickForm_element($elementName, $elementLabel, $attributes);
+ function MoodleQuickForm_files($elementName=null, $elementLabel=null, $options=null) {
+ $this->_options = $options;
+ parent::HTML_QuickForm_element($elementName, $elementLabel);
}
function setName($name) {
}
function setValue($value) {
- if (!is_array($value)) {
- $this->_areainfo = array();
- } else {
- $this->_areainfo = $value;
- }
+ $this->updateAttributes(array('value'=>$value));
}
function getValue() {
- return $this->_areainfo;
+ return $this->getAttribute('value');
}
- function setHelpButton($helpbuttonargs, $function='helpbutton') {
- if (!is_array($helpbuttonargs)) {
- $helpbuttonargs = array($helpbuttonargs);
+ function setHelpButton($_helpbuttonargs, $function='_helpbutton') {
+ if (!is_array($_helpbuttonargs)) {
+ $_helpbuttonargs = array($_helpbuttonargs);
} else {
- $helpbuttonargs = $helpbuttonargs;
+ $_helpbuttonargs = $_helpbuttonargs;
}
//we do this to to return html instead of printing it
//without having to specify it in every call to make a button.
- if ('helpbutton' == $function){
+ if ('_helpbutton' == $function){
$defaultargs = array('', '', 'moodle', true, false, '', true);
- $helpbuttonargs = $helpbuttonargs + $defaultargs ;
+ $_helpbuttonargs = $_helpbuttonargs + $defaultargs ;
}
- $this->_helpbutton=call_user_func_array($function, $helpbuttonargs);
+ $this->_helpbutton=call_user_func_array($function, $_helpbuttonargs);
}
function getHelpButton() {
}
function toHtml() {
- global $CFG;
+ global $CFG, $USER;
+
+ // security - never ever allow guest/not logged in user to upload anything or use this element!
+ if (isguestuser() or !isloggedin()) {
+ print_error('noguest');
+ }
if ($this->_flagFrozen) {
return $this->getFrozenHtml();
$id = $this->_attributes['id'];
$elname = $this->_attributes['name'];
- $value = $this->getValue();
+ $draftitemid = $this->getValue();
- if (empty($value['contextid'])) {
+ if (empty($draftitemid)) {
// no existing area info provided - let's use fresh new draft area
require_once("$CFG->libdir/filelib.php");
- $this->setValue(get_new_draftarea());
- $value = $this->getValue();
+ $this->setValue(file_get_new_draftitemid());
+ $draftitemid = $this->getValue();
}
- $contextid = $value['contextid'];
- $filearea = $value['filearea'];
- $itemid = $value['itemid'];
+ $editorurl = "$CFG->wwwroot/files/draftfiles.php?itemid=$draftitemid";
- $str = '<input type="hidden" name="'.$elname.'[contextid]" value="'.$contextid.'" />';
- $str .= '<input type="hidden" name="'.$elname.'[filearea]" value="'.$filearea.'" />';
- $str .= '<input type="hidden" name="'.$elname.'[itemid]" value="'.$itemid.'" />';
-
- $url = "$CFG->wwwroot/files/areafiles.php?contextid=$contextid&filearea=$filearea&itemid=$itemid";
-
- $str .= '<object type="text/html" data="'.$url.'" height="160" width="600" style="border:1px solid #000">Error</object>'; // TODO: localise, fix styles, etc.
+ $str = $this->_getTabs();
+ $str .= '<input type="hidden" name="'.$elname.'" value="'.$draftitemid.'" />';
+ $str .= '<object type="text/html" id="'.$id.'" data="'.$editorurl.'" height="160" width="600" style="border:1px solid #000">Error</object>'; // TODO: localise, fix styles, etc.
return $str;
}
- function exportValue(&$submitValues, $assoc = false) {
- return array(
- $this->_attributes['name']['contexid'] => $submitValues[$this->_attributes['name']]['contextid'],
- $this->_attributes['name']['filearea'] => $submitValues[$this->_attributes['name']]['filearea'],
- $this->_attributes['name']['itemid'] => $submitValues[$this->_attributes['name']]['itemid'],
- );
- }
}