2010-07-03 13:37:13 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draft file ajax file manager
|
|
|
|
*
|
|
|
|
* @package core
|
|
|
|
* @subpackage repository
|
|
|
|
* @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
|
|
|
|
define('AJAX_SCRIPT', true);
|
|
|
|
|
|
|
|
require('../config.php');
|
|
|
|
require_once($CFG->libdir.'/filelib.php');
|
|
|
|
require_once($CFG->libdir.'/adminlib.php');
|
2012-05-02 14:59:09 +08:00
|
|
|
require_once($CFG->dirroot.'/repository/lib.php');
|
2013-07-03 14:39:10 +08:00
|
|
|
$PAGE->set_context(context_system::instance());
|
2010-07-03 13:37:13 +00:00
|
|
|
require_login();
|
|
|
|
if (isguestuser()) {
|
2022-04-12 09:38:41 +05:30
|
|
|
throw new \moodle_exception('noguest');
|
2010-07-03 13:37:13 +00:00
|
|
|
}
|
|
|
|
require_sesskey();
|
|
|
|
|
|
|
|
$action = required_param('action', PARAM_ALPHA);
|
|
|
|
$draftid = required_param('itemid', PARAM_INT);
|
2010-07-13 03:05:30 +00:00
|
|
|
$filepath = optional_param('filepath', '/', PARAM_PATH);
|
2010-07-03 13:37:13 +00:00
|
|
|
|
2018-12-06 09:20:53 +08:00
|
|
|
$usercontext = context_user::instance($USER->id);
|
2010-07-03 13:37:13 +00:00
|
|
|
|
2010-08-10 08:50:08 +00:00
|
|
|
echo $OUTPUT->header(); // send headers
|
|
|
|
|
2010-07-03 13:37:13 +00:00
|
|
|
//
|
|
|
|
//NOTE TO ALL DEVELOPERS: this script must deal only with draft area of current user, it has to use only file_storage and no file_browser!!
|
|
|
|
//
|
|
|
|
|
|
|
|
switch ($action) {
|
|
|
|
case 'dir':
|
2010-09-21 08:54:01 +00:00
|
|
|
$data = new stdClass();
|
2010-07-03 13:37:13 +00:00
|
|
|
file_get_drafarea_folders($draftid, $filepath, $data);
|
|
|
|
echo json_encode($data);
|
|
|
|
die;
|
|
|
|
|
|
|
|
case 'list':
|
|
|
|
$filepath = optional_param('filepath', '/', PARAM_PATH);
|
|
|
|
|
2012-05-02 14:59:09 +08:00
|
|
|
$data = repository::prepare_listing(file_get_drafarea_files($draftid, $filepath));
|
2012-01-14 16:41:19 +00:00
|
|
|
$info = file_get_draft_area_info($draftid);
|
|
|
|
$data->filecount = $info['filecount'];
|
|
|
|
$data->filesize = $info['filesize'];
|
2012-05-02 14:59:09 +08:00
|
|
|
$data->tree = new stdClass();
|
|
|
|
file_get_drafarea_folders($draftid, '/', $data->tree);
|
2010-07-03 13:37:13 +00:00
|
|
|
echo json_encode($data);
|
|
|
|
die;
|
|
|
|
|
|
|
|
case 'mkdir':
|
|
|
|
$filepath = required_param('filepath', PARAM_PATH);
|
|
|
|
$newdirname = required_param('newdirname', PARAM_FILE);
|
|
|
|
|
|
|
|
$fs = get_file_storage();
|
2018-12-06 09:20:53 +08:00
|
|
|
$fs->create_directory($usercontext->id, 'user', 'draft', $draftid, file_correct_filepath(file_correct_filepath($filepath).$newdirname));
|
2010-09-21 08:54:01 +00:00
|
|
|
$return = new stdClass();
|
2010-07-03 13:37:13 +00:00
|
|
|
$return->filepath = $filepath;
|
|
|
|
echo json_encode($return);
|
|
|
|
die;
|
|
|
|
|
|
|
|
case 'delete':
|
|
|
|
$filename = required_param('filename', PARAM_FILE);
|
|
|
|
$filepath = required_param('filepath', PARAM_PATH);
|
2018-12-06 09:20:53 +08:00
|
|
|
$selectedfile = (object)[
|
|
|
|
'filename' => $filename,
|
|
|
|
'filepath' => $filepath
|
|
|
|
];
|
|
|
|
$return = repository_delete_selected_files($usercontext, 'user', 'draft', $draftid, [$selectedfile]);
|
|
|
|
|
|
|
|
if ($return) {
|
|
|
|
$response = new stdClass();
|
|
|
|
$response->filepath = array_keys($return)[0];
|
|
|
|
echo json_encode($response);
|
|
|
|
die;
|
2010-07-03 13:37:13 +00:00
|
|
|
}
|
2018-12-06 09:20:53 +08:00
|
|
|
|
|
|
|
echo json_encode(false);
|
|
|
|
die;
|
|
|
|
|
|
|
|
case 'deleteselected':
|
|
|
|
$selected = required_param('selected', PARAM_RAW);
|
|
|
|
$return = [];
|
|
|
|
$selectedfiles = json_decode($selected);
|
|
|
|
$return = repository_delete_selected_files($usercontext, 'user', 'draft', $draftid, $selectedfiles);
|
|
|
|
echo (json_encode($return ? array_keys($return) : false));
|
2010-07-03 13:37:13 +00:00
|
|
|
die;
|
|
|
|
|
|
|
|
case 'setmainfile':
|
|
|
|
$filename = required_param('filename', PARAM_FILE);
|
|
|
|
$filepath = required_param('filepath', PARAM_PATH);
|
|
|
|
|
|
|
|
$filepath = file_correct_filepath($filepath);
|
|
|
|
// reset sort order
|
2018-12-06 09:20:53 +08:00
|
|
|
file_reset_sortorder($usercontext->id, 'user', 'draft', $draftid);
|
2010-07-03 13:37:13 +00:00
|
|
|
// set main file
|
2018-12-06 09:20:53 +08:00
|
|
|
$return = file_set_sortorder($usercontext->id, 'user', 'draft', $draftid, $filepath, $filename, 1);
|
2010-07-03 13:37:13 +00:00
|
|
|
echo json_encode($return);
|
|
|
|
die;
|
|
|
|
|
2012-05-04 13:26:10 +08:00
|
|
|
case 'updatefile':
|
|
|
|
// Allows to Rename file, move it to another directory, change it's license and author information in one request
|
|
|
|
$filename = required_param('filename', PARAM_FILE);
|
|
|
|
$filepath = required_param('filepath', PARAM_PATH);
|
|
|
|
$updatedata = array();
|
2013-05-08 22:22:53 +10:00
|
|
|
$updatedata['filename'] = optional_param('newfilename', $filename, PARAM_FILE);
|
|
|
|
$updatedata['filepath'] = $newfilepath = optional_param('newfilepath', $filepath, PARAM_PATH);
|
|
|
|
if (($v = optional_param('newlicense', false, PARAM_TEXT)) !== false) {
|
|
|
|
$updatedata['license'] = $v;
|
2012-05-04 13:26:10 +08:00
|
|
|
}
|
2013-05-08 22:22:53 +10:00
|
|
|
if (($v = optional_param('newauthor', false, PARAM_TEXT)) !== false) {
|
|
|
|
$updatedata['author'] = $v;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
repository::update_draftfile($draftid, $filepath, $filename, $updatedata);
|
|
|
|
} catch (moodle_exception $e) {
|
|
|
|
die(json_encode((object)array('error' => $e->getMessage())));
|
2012-05-04 13:26:10 +08:00
|
|
|
}
|
|
|
|
die(json_encode((object)array('filepath' => $newfilepath)));
|
|
|
|
|
|
|
|
case 'updatedir':
|
|
|
|
$filepath = required_param('filepath', PARAM_PATH);
|
|
|
|
$newdirname = required_param('newdirname', PARAM_FILE);
|
|
|
|
$parent = required_param('newfilepath', PARAM_PATH);
|
|
|
|
$newfilepath = clean_param($parent . '/' . $newdirname . '/', PARAM_PATH);
|
2013-05-08 22:22:53 +10:00
|
|
|
try {
|
|
|
|
repository::update_draftfile($draftid, $filepath, '.', array('filepath' => $newfilepath));
|
|
|
|
} catch (moodle_exception $e) {
|
|
|
|
die(json_encode((object)array('error' => $e->getMessage())));
|
2012-05-04 13:26:10 +08:00
|
|
|
}
|
2013-05-08 22:22:53 +10:00
|
|
|
die(json_encode((object)array('filepath' => $parent)));
|
2012-05-04 13:26:10 +08:00
|
|
|
|
2010-07-03 13:37:13 +00:00
|
|
|
case 'zip':
|
|
|
|
$filepath = required_param('filepath', PARAM_PATH);
|
|
|
|
|
|
|
|
$zipper = get_file_packer('application/zip');
|
|
|
|
$fs = get_file_storage();
|
|
|
|
|
2018-12-06 09:20:53 +08:00
|
|
|
$file = $fs->get_file($usercontext->id, 'user', 'draft', $draftid, $filepath, '.');
|
2010-07-03 13:37:13 +00:00
|
|
|
|
|
|
|
$parent_path = $file->get_parent_directory()->get_filepath();
|
2012-11-06 14:44:56 +08:00
|
|
|
|
2012-09-21 17:50:11 +07:00
|
|
|
$filepath = explode('/', trim($file->get_filepath(), '/'));
|
|
|
|
$filepath = array_pop($filepath);
|
2012-12-12 14:37:55 +08:00
|
|
|
$zipfile = repository::get_unused_filename($draftid, $parent_path, $filepath . '.zip');
|
2010-07-03 13:37:13 +00:00
|
|
|
|
2018-12-06 09:20:53 +08:00
|
|
|
if ($newfile = $zipper->archive_to_storage([$filepath => $file], $usercontext->id, 'user', 'draft', $draftid, $parent_path, $zipfile, $USER->id)) {
|
2010-09-21 08:54:01 +00:00
|
|
|
$return = new stdClass();
|
2010-07-03 13:37:13 +00:00
|
|
|
$return->filepath = $parent_path;
|
|
|
|
echo json_encode($return);
|
|
|
|
} else {
|
|
|
|
echo json_encode(false);
|
|
|
|
}
|
|
|
|
die;
|
2019-04-05 13:10:44 +08:00
|
|
|
case 'downloadselected':
|
|
|
|
$selected = required_param('selected', PARAM_RAW);
|
|
|
|
$selectedfiles = json_decode($selected);
|
2020-05-20 08:00:17 +08:00
|
|
|
if (!count($selectedfiles)) {
|
|
|
|
$filepath = required_param('filepath', PARAM_PATH);
|
|
|
|
$selectedfiles = [(object)[
|
|
|
|
'filename' => '',
|
|
|
|
'filepath' => $filepath
|
|
|
|
]];
|
|
|
|
}
|
2019-04-05 13:10:44 +08:00
|
|
|
$return = repository_download_selected_files($usercontext, 'user', 'draft', $draftid, $selectedfiles);
|
|
|
|
echo (json_encode($return));
|
|
|
|
die;
|
2010-07-03 13:37:13 +00:00
|
|
|
|
|
|
|
case 'downloaddir':
|
|
|
|
$filepath = required_param('filepath', PARAM_PATH);
|
|
|
|
|
2019-04-05 13:10:44 +08:00
|
|
|
$selectedfile = (object)[
|
|
|
|
'filename' => '',
|
|
|
|
'filepath' => $filepath
|
|
|
|
];
|
|
|
|
$return = repository_download_selected_files($usercontext, 'user', 'draft', $draftid, [$selectedfile]);
|
|
|
|
echo json_encode($return);
|
2010-07-03 13:37:13 +00:00
|
|
|
die;
|
|
|
|
|
|
|
|
case 'unzip':
|
|
|
|
$filename = required_param('filename', PARAM_FILE);
|
|
|
|
$filepath = required_param('filepath', PARAM_PATH);
|
2020-07-14 15:49:14 +08:00
|
|
|
$areamaxbytes = required_param('areamaxbytes', PARAM_INT);
|
2010-07-03 13:37:13 +00:00
|
|
|
|
2020-07-14 15:49:14 +08:00
|
|
|
$return = new stdClass();
|
2010-07-03 13:37:13 +00:00
|
|
|
$zipper = get_file_packer('application/zip');
|
|
|
|
$fs = get_file_storage();
|
2018-12-06 09:20:53 +08:00
|
|
|
$file = $fs->get_file($usercontext->id, 'user', 'draft', $draftid, $filepath, $filename);
|
2020-07-14 15:49:14 +08:00
|
|
|
// Get the total size of the content in the archive.
|
|
|
|
$filecontentsize = $file->get_total_content_size($zipper);
|
|
|
|
|
|
|
|
// Return an error if the returned size of the content is NULL.
|
|
|
|
// This means the utility class was unable to read the content of the archive.
|
|
|
|
if (is_null($filecontentsize)) {
|
|
|
|
$return->error = get_string('cannotunzipcontentunreadable', 'repository');
|
|
|
|
die(json_encode($return));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether the maximum size allowed in this draft area will be exceeded with unzipping the file.
|
|
|
|
// If the maximum size allowed is exceeded, return an error before attempting to unzip.
|
|
|
|
if (file_is_draft_area_limit_reached($draftid, $areamaxbytes, $filecontentsize)) {
|
|
|
|
$return->error = get_string('cannotunzipquotaexceeded', 'repository');
|
|
|
|
die(json_encode($return));
|
|
|
|
}
|
2010-07-03 13:37:13 +00:00
|
|
|
|
2013-05-13 20:15:32 +10:00
|
|
|
// Find unused name for directory to extract the archive.
|
2018-12-06 09:20:53 +08:00
|
|
|
$temppath = $fs->get_unused_dirname($usercontext->id, 'user', 'draft', $draftid, $filepath. pathinfo($filename, PATHINFO_FILENAME). '/');
|
2013-05-23 19:15:47 +10:00
|
|
|
$donotremovedirs = array();
|
|
|
|
$doremovedirs = array($temppath);
|
2020-09-14 23:08:22 +01:00
|
|
|
|
2013-05-13 20:15:32 +10:00
|
|
|
// Extract archive and move all files from $temppath to $filepath
|
2020-09-14 23:08:22 +01:00
|
|
|
if (($processed = $file->extract_to_storage($zipper, $usercontext->id, 'user', 'draft', $draftid, $temppath, $USER->id))
|
|
|
|
!== false) {
|
|
|
|
|
|
|
|
// Find all failures within the processed files, and return an error if any are found.
|
|
|
|
$failed = array_filter($processed, static function($result): bool {
|
|
|
|
return $result !== true;
|
|
|
|
});
|
|
|
|
if (count($failed) > 0) {
|
2024-04-04 11:44:32 +08:00
|
|
|
$return->error = get_string('cannotunzipextractfileerror', 'repository');
|
2020-09-14 23:08:22 +01:00
|
|
|
die(json_encode($return));
|
|
|
|
}
|
|
|
|
|
2018-12-06 09:20:53 +08:00
|
|
|
$extractedfiles = $fs->get_directory_files($usercontext->id, 'user', 'draft', $draftid, $temppath, true);
|
2013-05-13 20:15:32 +10:00
|
|
|
$xtemppath = preg_quote($temppath, '|');
|
|
|
|
foreach ($extractedfiles as $file) {
|
|
|
|
$realpath = preg_replace('|^'.$xtemppath.'|', $filepath, $file->get_filepath());
|
|
|
|
if (!$file->is_directory()) {
|
|
|
|
// Set the source to the extracted file to indicate that it came from archive.
|
|
|
|
$file->set_source(serialize((object)array('source' => $filepath)));
|
|
|
|
}
|
2018-12-06 09:20:53 +08:00
|
|
|
if (!$fs->file_exists($usercontext->id, 'user', 'draft', $draftid, $realpath, $file->get_filename())) {
|
2013-05-13 20:15:32 +10:00
|
|
|
// File or directory did not exist, just move it.
|
|
|
|
$file->rename($realpath, $file->get_filename());
|
|
|
|
} else if (!$file->is_directory()) {
|
2013-05-23 19:15:47 +10:00
|
|
|
// File already existed, overwrite it
|
2013-05-13 20:15:32 +10:00
|
|
|
repository::overwrite_existing_draftfile($draftid, $realpath, $file->get_filename(), $file->get_filepath(), $file->get_filename());
|
2013-05-23 19:15:47 +10:00
|
|
|
} else {
|
|
|
|
// Directory already existed, remove temporary dir but make sure we don't remove the existing dir
|
|
|
|
$doremovedirs[] = $file->get_filepath();
|
|
|
|
$donotremovedirs[] = $realpath;
|
2013-05-13 20:15:32 +10:00
|
|
|
}
|
|
|
|
}
|
2010-07-03 13:37:13 +00:00
|
|
|
$return->filepath = $filepath;
|
|
|
|
} else {
|
2013-05-13 20:15:32 +10:00
|
|
|
$return = false;
|
2010-07-03 13:37:13 +00:00
|
|
|
}
|
2013-05-13 20:15:32 +10:00
|
|
|
// Remove remaining temporary directories.
|
2013-05-23 19:15:47 +10:00
|
|
|
foreach (array_diff($doremovedirs, $donotremovedirs) as $filepath) {
|
2018-12-06 09:20:53 +08:00
|
|
|
if ($file = $fs->get_file($usercontext->id, 'user', 'draft', $draftid, $filepath, '.')) {
|
2013-05-23 19:15:47 +10:00
|
|
|
$file->delete();
|
|
|
|
}
|
2013-05-13 20:15:32 +10:00
|
|
|
}
|
|
|
|
die(json_encode($return));
|
2010-07-03 13:37:13 +00:00
|
|
|
|
2012-05-10 16:00:46 +08:00
|
|
|
case 'getoriginal':
|
|
|
|
$filename = required_param('filename', PARAM_FILE);
|
|
|
|
$filepath = required_param('filepath', PARAM_PATH);
|
|
|
|
|
|
|
|
$fs = get_file_storage();
|
2018-12-06 09:20:53 +08:00
|
|
|
$file = $fs->get_file($usercontext->id, 'user', 'draft', $draftid, $filepath, $filename);
|
2012-05-10 16:00:46 +08:00
|
|
|
if (!$file) {
|
|
|
|
echo json_encode(false);
|
|
|
|
} else {
|
|
|
|
$return = array('filename' => $filename, 'filepath' => $filepath, 'original' => $file->get_reference_details());
|
|
|
|
echo json_encode((object)$return);
|
|
|
|
}
|
|
|
|
die;
|
|
|
|
|
2012-05-15 10:51:00 +08:00
|
|
|
case 'getreferences':
|
|
|
|
$filename = required_param('filename', PARAM_FILE);
|
|
|
|
$filepath = required_param('filepath', PARAM_PATH);
|
|
|
|
|
|
|
|
$fs = get_file_storage();
|
2018-12-06 09:20:53 +08:00
|
|
|
$file = $fs->get_file($usercontext->id, 'user', 'draft', $draftid, $filepath, $filename);
|
2012-05-15 10:51:00 +08:00
|
|
|
if (!$file) {
|
|
|
|
echo json_encode(false);
|
|
|
|
} else {
|
|
|
|
$source = unserialize($file->get_source());
|
|
|
|
$return = array('filename' => $filename, 'filepath' => $filepath, 'references' => array());
|
|
|
|
$browser = get_file_browser();
|
|
|
|
if (isset($source->original)) {
|
|
|
|
$reffiles = $fs->search_references($source->original);
|
|
|
|
foreach ($reffiles as $reffile) {
|
2012-08-21 14:20:30 +08:00
|
|
|
$refcontext = context::instance_by_id($reffile->get_contextid());
|
2012-05-15 10:51:00 +08:00
|
|
|
$fileinfo = $browser->get_file_info($refcontext, $reffile->get_component(), $reffile->get_filearea(), $reffile->get_itemid(), $reffile->get_filepath(), $reffile->get_filename());
|
|
|
|
if (empty($fileinfo)) {
|
|
|
|
$return['references'][] = get_string('undisclosedreference', 'repository');
|
|
|
|
} else {
|
|
|
|
$return['references'][] = $fileinfo->get_readable_fullname();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
echo json_encode((object)$return);
|
|
|
|
}
|
|
|
|
die;
|
|
|
|
|
2010-07-03 13:37:13 +00:00
|
|
|
default:
|
|
|
|
// no/unknown action?
|
|
|
|
echo json_encode(false);
|
|
|
|
die;
|
|
|
|
}
|