mirror of
https://github.com/moodle/moodle.git
synced 2025-01-17 21:49:15 +01:00
Merge branch 'MDL-77406' of https://github.com/AnupamaSarjoshi/moodle
This commit is contained in:
commit
5b8c1ffd84
@ -25,6 +25,7 @@
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
$string['eventfileaddedtodraftarea'] = 'File added to draft area';
|
||||
$string['privacy:metadata:file_conversions'] = 'A record of the file conversions performed by a user.';
|
||||
$string['privacy:metadata:file_conversion:usermodified'] = 'The user who started the file conversion.';
|
||||
$string['privacy:metadata:files'] = 'A record of the files uploaded or shared by users';
|
||||
|
81
lib/classes/event/draft_file_added.php
Normal file
81
lib/classes/event/draft_file_added.php
Normal file
@ -0,0 +1,81 @@
|
||||
<?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/>.
|
||||
|
||||
namespace core\event;
|
||||
|
||||
/**
|
||||
* draft_file_added
|
||||
*
|
||||
* Event fired when a file is added to the draft area.
|
||||
*
|
||||
* @property-read array $other {
|
||||
* Extra information about the event.
|
||||
*
|
||||
* - string itemid: itemid of the file
|
||||
* - string filename: Name of the file added to the draft area.
|
||||
* - string filesize: The file size.
|
||||
* - string filepath: The filepath.
|
||||
* - string contenthash: The file contenthash.
|
||||
* }
|
||||
*
|
||||
* @package core
|
||||
* @since Moodle 4.2
|
||||
* @copyright 2023 The Open University.
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class draft_file_added extends base {
|
||||
|
||||
protected function init() {
|
||||
$this->data['objecttable'] = 'files';
|
||||
$this->data['crud'] = 'c';
|
||||
$this->data['edulevel'] = self::LEVEL_OTHER;
|
||||
}
|
||||
|
||||
public static function get_name() {
|
||||
return get_string('eventfileaddedtodraftarea', 'files');
|
||||
}
|
||||
|
||||
public function get_description() {
|
||||
$humansize = display_size($this->other['filesize']);
|
||||
return "The user with id '{$this->userid}' has uploaded file '{$this->other['filepath']}{$this->other['filename']}' " .
|
||||
"to the draft file area with item id {$this->other['itemid']}. Size: {$humansize}. ".
|
||||
"Content hash: {$this->other['contenthash']}.";
|
||||
}
|
||||
|
||||
protected function validate_data() {
|
||||
parent::validate_data();
|
||||
|
||||
if (!isset($this->other['itemid'])) {
|
||||
throw new \coding_exception('The \'itemid\' must be set in other.');
|
||||
}
|
||||
|
||||
if (!isset($this->other['filename'])) {
|
||||
throw new \coding_exception('The \'filename\' value must be set in other.');
|
||||
}
|
||||
|
||||
if (!isset($this->other['filesize'])) {
|
||||
throw new \coding_exception('The \'filesize\' value must be set in other.');
|
||||
}
|
||||
|
||||
if (!isset($this->other['filepath'])) {
|
||||
throw new \coding_exception('The \'filepath\' value must be set in other.');
|
||||
}
|
||||
|
||||
if (!isset($this->other['contenthash'])) {
|
||||
throw new \coding_exception('The \'contenthash\' value must be set in other.');
|
||||
}
|
||||
}
|
||||
}
|
@ -725,6 +725,12 @@ class behat_navigation extends behat_base {
|
||||
case 'Admin notifications':
|
||||
return new moodle_url('/admin/');
|
||||
|
||||
case 'My private files':
|
||||
return new moodle_url('/user/files.php');
|
||||
|
||||
case 'System logs report':
|
||||
return new moodle_url('/report/log/index.php');
|
||||
|
||||
default:
|
||||
throw new Exception('Unrecognised core page type "' . $name . '."');
|
||||
}
|
||||
|
86
lib/tests/event/draft_file_added_test.php
Normal file
86
lib/tests/event/draft_file_added_test.php
Normal file
@ -0,0 +1,86 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* File added to draft area test events.
|
||||
*
|
||||
* @package core
|
||||
* @category test
|
||||
* @copyright 2023 The Open University.
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace core\event;
|
||||
|
||||
/**
|
||||
* Test for draft file added event.
|
||||
*
|
||||
* @package core
|
||||
* @category test
|
||||
* @copyright 2023 The Open University.
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @covers \core\event\draft_file_added
|
||||
*/
|
||||
class draft_file_added_test extends \advanced_testcase {
|
||||
/**
|
||||
* Test draft file added event.
|
||||
*/
|
||||
public function test_event() {
|
||||
$this->resetAfterTest();
|
||||
$user = $this->getDataGenerator()->create_user();
|
||||
$this->setUser($user);
|
||||
$usercontext = \context_user::instance($user->id);
|
||||
|
||||
$sink = $this->redirectEvents();
|
||||
$fs = get_file_storage();
|
||||
|
||||
$filerecord = [
|
||||
'contextid' => $usercontext->id,
|
||||
'component' => 'core',
|
||||
'filearea' => 'unittest',
|
||||
'itemid' => 0,
|
||||
'filepath' => '/',
|
||||
'filename' => 'test.txt',
|
||||
'source' => 'Copyright stuff',
|
||||
];
|
||||
$originalfile = $fs->create_file_from_string($filerecord, 'Test content');
|
||||
$nbsp = "\xc2\xa0";
|
||||
|
||||
// Event data for logging.
|
||||
$eventdata = [
|
||||
'objectid' => $originalfile->get_id(),
|
||||
'context' => $usercontext,
|
||||
'other' => [
|
||||
'itemid' => $originalfile->get_itemid(),
|
||||
'filename' => $originalfile->get_filename(),
|
||||
'filesize' => $originalfile->get_filesize(),
|
||||
'filepath' => $originalfile->get_filepath(),
|
||||
'contenthash' => $originalfile->get_contenthash(),
|
||||
]
|
||||
];
|
||||
$event = \core\event\draft_file_added::create($eventdata);
|
||||
$event->trigger();
|
||||
|
||||
$events = $sink->get_events();
|
||||
$this->assertCount(1, $events);
|
||||
$event = reset($events);
|
||||
|
||||
$this->assertEquals($usercontext, $event->get_context());
|
||||
$expected = "The user with id '{$user->id}' has uploaded file '/test.txt' to the draft file area with item id 0. ".
|
||||
"Size: 12{$nbsp}bytes. Content hash: {$originalfile->get_contenthash()}.";
|
||||
$this->assertSame($expected, $event->get_description());
|
||||
}
|
||||
}
|
@ -1,5 +1,4 @@
|
||||
<?php
|
||||
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
@ -23,6 +22,8 @@
|
||||
* @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
require_once($CFG->dirroot . '/repository/lib.php');
|
||||
|
||||
/**
|
||||
@ -49,7 +50,7 @@ class repository_upload extends repository {
|
||||
* Process uploaded file
|
||||
* @return array|bool
|
||||
*/
|
||||
public function upload($saveas_filename, $maxbytes) {
|
||||
public function upload($saveasfilename, $maxbytes) {
|
||||
global $CFG;
|
||||
|
||||
$types = optional_param_array('accepted_types', '*', PARAM_RAW);
|
||||
@ -60,12 +61,13 @@ class repository_upload extends repository {
|
||||
$areamaxbytes = optional_param('areamaxbytes', FILE_AREA_MAX_BYTES_UNLIMITED, PARAM_INT);
|
||||
$overwriteexisting = optional_param('overwrite', false, PARAM_BOOL);
|
||||
|
||||
return $this->process_upload($saveas_filename, $maxbytes, $types, $savepath, $itemid, $license, $author, $overwriteexisting, $areamaxbytes);
|
||||
return $this->process_upload($saveasfilename, $maxbytes, $types, $savepath, $itemid, $license, $author,
|
||||
$overwriteexisting, $areamaxbytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Do the actual processing of the uploaded file
|
||||
* @param string $saveas_filename name to give to the file
|
||||
* @param string $saveasfilename name to give to the file
|
||||
* @param int $maxbytes maximum file size
|
||||
* @param mixed $types optional array of file extensions that are allowed or '*' for all
|
||||
* @param string $savepath optional path to save the file to
|
||||
@ -76,7 +78,7 @@ class repository_upload extends repository {
|
||||
* @param int $areamaxbytes maximum size of the file area.
|
||||
* @return object containing details of the file uploaded
|
||||
*/
|
||||
public function process_upload($saveas_filename, $maxbytes, $types = '*', $savepath = '/', $itemid = 0,
|
||||
public function process_upload($saveasfilename, $maxbytes, $types = '*', $savepath = '/', $itemid = 0,
|
||||
$license = null, $author = '', $overwriteexisting = false, $areamaxbytes = FILE_AREA_MAX_BYTES_UNLIMITED) {
|
||||
global $USER, $CFG;
|
||||
|
||||
@ -115,29 +117,29 @@ class repository_upload extends repository {
|
||||
}
|
||||
if (!empty($_FILES[$elname]['error'])) {
|
||||
switch ($_FILES[$elname]['error']) {
|
||||
case UPLOAD_ERR_INI_SIZE:
|
||||
throw new moodle_exception('upload_error_ini_size', 'repository_upload');
|
||||
break;
|
||||
case UPLOAD_ERR_FORM_SIZE:
|
||||
throw new moodle_exception('upload_error_form_size', 'repository_upload');
|
||||
break;
|
||||
case UPLOAD_ERR_PARTIAL:
|
||||
throw new moodle_exception('upload_error_partial', 'repository_upload');
|
||||
break;
|
||||
case UPLOAD_ERR_NO_FILE:
|
||||
throw new moodle_exception('upload_error_no_file', 'repository_upload');
|
||||
break;
|
||||
case UPLOAD_ERR_NO_TMP_DIR:
|
||||
throw new moodle_exception('upload_error_no_tmp_dir', 'repository_upload');
|
||||
break;
|
||||
case UPLOAD_ERR_CANT_WRITE:
|
||||
throw new moodle_exception('upload_error_cant_write', 'repository_upload');
|
||||
break;
|
||||
case UPLOAD_ERR_EXTENSION:
|
||||
throw new moodle_exception('upload_error_extension', 'repository_upload');
|
||||
break;
|
||||
default:
|
||||
throw new moodle_exception('nofile');
|
||||
case UPLOAD_ERR_INI_SIZE:
|
||||
throw new moodle_exception('upload_error_ini_size', 'repository_upload');
|
||||
break;
|
||||
case UPLOAD_ERR_FORM_SIZE:
|
||||
throw new moodle_exception('upload_error_form_size', 'repository_upload');
|
||||
break;
|
||||
case UPLOAD_ERR_PARTIAL:
|
||||
throw new moodle_exception('upload_error_partial', 'repository_upload');
|
||||
break;
|
||||
case UPLOAD_ERR_NO_FILE:
|
||||
throw new moodle_exception('upload_error_no_file', 'repository_upload');
|
||||
break;
|
||||
case UPLOAD_ERR_NO_TMP_DIR:
|
||||
throw new moodle_exception('upload_error_no_tmp_dir', 'repository_upload');
|
||||
break;
|
||||
case UPLOAD_ERR_CANT_WRITE:
|
||||
throw new moodle_exception('upload_error_cant_write', 'repository_upload');
|
||||
break;
|
||||
case UPLOAD_ERR_EXTENSION:
|
||||
throw new moodle_exception('upload_error_extension', 'repository_upload');
|
||||
break;
|
||||
default:
|
||||
throw new moodle_exception('nofile');
|
||||
}
|
||||
}
|
||||
|
||||
@ -147,7 +149,7 @@ class repository_upload extends repository {
|
||||
$sourcefield = $this->get_file_source_info($_FILES[$elname]['name']);
|
||||
$record->source = self::build_source_field($sourcefield);
|
||||
|
||||
if (empty($saveas_filename)) {
|
||||
if (empty($saveasfilename)) {
|
||||
$record->filename = clean_param($_FILES[$elname]['name'], PARAM_FILE);
|
||||
} else {
|
||||
$ext = '';
|
||||
@ -155,7 +157,7 @@ class repository_upload extends repository {
|
||||
$filename = clean_param($_FILES[$elname]['name'], PARAM_FILE);
|
||||
if (strpos($filename, '.') === false) {
|
||||
// File has no extension at all - do not add a dot.
|
||||
$record->filename = $saveas_filename;
|
||||
$record->filename = $saveasfilename;
|
||||
} else {
|
||||
if (preg_match('/\.([a-z0-9]+)$/i', $filename, $match)) {
|
||||
if (isset($match[1])) {
|
||||
@ -163,26 +165,27 @@ class repository_upload extends repository {
|
||||
}
|
||||
}
|
||||
$ext = !empty($ext) ? $ext : '';
|
||||
if (preg_match('#\.(' . $ext . ')$#i', $saveas_filename)) {
|
||||
// saveas filename contains file extension already
|
||||
$record->filename = $saveas_filename;
|
||||
if (preg_match('#\.(' . $ext . ')$#i', $saveasfilename)) {
|
||||
// The saveas filename contains file extension already.
|
||||
$record->filename = $saveasfilename;
|
||||
} else {
|
||||
$record->filename = $saveas_filename . '.' . $ext;
|
||||
$record->filename = $saveasfilename . '.' . $ext;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check the file has some non-null contents - usually an indication that a user has
|
||||
// tried to upload a folder by mistake
|
||||
// tried to upload a folder by mistake.
|
||||
if (!$this->check_valid_contents($_FILES[$elname]['tmp_name'])) {
|
||||
throw new moodle_exception('upload_error_invalid_file', 'repository_upload', '', $record->filename);
|
||||
}
|
||||
|
||||
if ($this->mimetypes != '*') {
|
||||
// check filetype
|
||||
// Check filetype.
|
||||
$filemimetype = file_storage::mimetype($_FILES[$elname]['tmp_name'], $record->filename);
|
||||
if (!in_array($filemimetype, $this->mimetypes)) {
|
||||
throw new moodle_exception('invalidfiletype', 'repository', '', get_mimetype_description(array('filename' => $_FILES[$elname]['name'])));
|
||||
throw new moodle_exception('invalidfiletype', 'repository', '',
|
||||
get_mimetype_description(array('filename' => $_FILES[$elname]['name'])));
|
||||
}
|
||||
}
|
||||
|
||||
@ -190,13 +193,14 @@ class repository_upload extends repository {
|
||||
$record->itemid = 0;
|
||||
}
|
||||
|
||||
if (($maxbytes!==-1) && (filesize($_FILES[$elname]['tmp_name']) > $maxbytes)) {
|
||||
$filesize = filesize($_FILES[$elname]['tmp_name']);
|
||||
if (($maxbytes !== -1) && ($filesize > $maxbytes)) {
|
||||
$maxbytesdisplay = display_size($maxbytes, 0);
|
||||
throw new file_exception('maxbytesfile', (object) array('file' => $record->filename,
|
||||
'size' => $maxbytesdisplay));
|
||||
}
|
||||
|
||||
if (file_is_draft_area_limit_reached($record->itemid, $areamaxbytes, filesize($_FILES[$elname]['tmp_name']))) {
|
||||
if (file_is_draft_area_limit_reached($record->itemid, $areamaxbytes, $filesize)) {
|
||||
throw new file_exception('maxareabytes');
|
||||
}
|
||||
// Ensure the user does not upload too many draft files in a short period.
|
||||
@ -209,34 +213,50 @@ class repository_upload extends repository {
|
||||
|
||||
if (repository::draftfile_exists($record->itemid, $record->filepath, $record->filename)) {
|
||||
$existingfilename = $record->filename;
|
||||
$unused_filename = repository::get_unused_filename($record->itemid, $record->filepath, $record->filename);
|
||||
$record->filename = $unused_filename;
|
||||
$stored_file = $fs->create_file_from_pathname($record, $_FILES[$elname]['tmp_name']);
|
||||
$unusedfilename = repository::get_unused_filename($record->itemid, $record->filepath, $record->filename);
|
||||
$record->filename = $unusedfilename;
|
||||
$storedfile = $fs->create_file_from_pathname($record, $_FILES[$elname]['tmp_name']);
|
||||
if ($overwriteexisting) {
|
||||
repository::overwrite_existing_draftfile($record->itemid, $record->filepath, $existingfilename, $record->filepath, $record->filename);
|
||||
repository::overwrite_existing_draftfile($record->itemid, $record->filepath, $existingfilename,
|
||||
$record->filepath, $record->filename);
|
||||
$record->filename = $existingfilename;
|
||||
} else {
|
||||
$event = array();
|
||||
$event['event'] = 'fileexists';
|
||||
$event['newfile'] = new stdClass;
|
||||
$event['newfile']->filepath = $record->filepath;
|
||||
$event['newfile']->filename = $unused_filename;
|
||||
$event['newfile']->url = moodle_url::make_draftfile_url($record->itemid, $record->filepath, $unused_filename)->out(false);
|
||||
$event['newfile']->filename = $unusedfilename;
|
||||
$event['newfile']->url = moodle_url::make_draftfile_url($record->itemid, $record->filepath,
|
||||
$unusedfilename)->out(false);
|
||||
|
||||
$event['existingfile'] = new stdClass;
|
||||
$event['existingfile']->filepath = $record->filepath;
|
||||
$event['existingfile']->filename = $existingfilename;
|
||||
$event['existingfile']->url = moodle_url::make_draftfile_url($record->itemid, $record->filepath, $existingfilename)->out(false);
|
||||
$event['existingfile']->url = moodle_url::make_draftfile_url($record->itemid, $record->filepath,
|
||||
$existingfilename)->out(false);
|
||||
return $event;
|
||||
}
|
||||
} else {
|
||||
$stored_file = $fs->create_file_from_pathname($record, $_FILES[$elname]['tmp_name']);
|
||||
$storedfile = $fs->create_file_from_pathname($record, $_FILES[$elname]['tmp_name']);
|
||||
}
|
||||
|
||||
$logevent = \core\event\draft_file_added::create([
|
||||
'objectid' => $storedfile->get_id(),
|
||||
'context' => $context,
|
||||
'other' => [
|
||||
'itemid' => $record->itemid,
|
||||
'filename' => $record->filename,
|
||||
'filesize' => $filesize,
|
||||
'filepath' => $record->filepath,
|
||||
'contenthash' => $storedfile->get_contenthash(),
|
||||
],
|
||||
]);
|
||||
$logevent->trigger();
|
||||
|
||||
return array(
|
||||
'url'=>moodle_url::make_draftfile_url($record->itemid, $record->filepath, $record->filename)->out(false),
|
||||
'id'=>$record->itemid,
|
||||
'file'=>$record->filename);
|
||||
'url' => moodle_url::make_draftfile_url($record->itemid, $record->filepath, $record->filename)->out(false),
|
||||
'id' => $record->itemid,
|
||||
'file' => $record->filename);
|
||||
}
|
||||
|
||||
|
||||
@ -251,17 +271,17 @@ class repository_upload extends repository {
|
||||
|
||||
$fp = fopen($filepath, 'r');
|
||||
if (!$fp) {
|
||||
return false; // Cannot read the file - something has gone wrong
|
||||
return false; // Cannot read the file - something has gone wrong.
|
||||
}
|
||||
while (!feof($fp)) {
|
||||
// Read the file 4k at a time
|
||||
// Read the file 4k at a time.
|
||||
$data = fread($fp, $buffersize);
|
||||
if (preg_match('/[^\0]+/', $data)) {
|
||||
fclose($fp);
|
||||
return true; // Return as soon as a non-null byte is found
|
||||
return true; // Return as soon as a non-null byte is found.
|
||||
}
|
||||
}
|
||||
// Entire file is NULL
|
||||
// Entire file is NULL.
|
||||
fclose($fp);
|
||||
return false;
|
||||
}
|
||||
@ -278,8 +298,8 @@ class repository_upload extends repository {
|
||||
$ret['norefresh'] = true;
|
||||
$ret['list'] = array();
|
||||
$ret['dynload'] = false;
|
||||
$ret['upload'] = array('label'=>get_string('attachment', 'repository'), 'id'=>'repo-form');
|
||||
$ret['allowcaching'] = true; // indicates that result of get_listing() can be cached in filepicker.js
|
||||
$ret['upload'] = array('label' => get_string('attachment', 'repository'), 'id' => 'repo-form');
|
||||
$ret['allowcaching'] = true; // Indicates that result of get_listing() can be cached in filepicker.js.
|
||||
return $ret;
|
||||
}
|
||||
|
||||
|
@ -21,3 +21,13 @@ Feature: Upload files
|
||||
Then I should see "2" elements in "Files" filemanager
|
||||
And I should see "empty.txt"
|
||||
And I should see "empty_copy.txt"
|
||||
|
||||
@javascript
|
||||
Scenario: Verify logs for file upload
|
||||
Given I am on the "My private files" page logged in as "admin"
|
||||
And I upload "lib/tests/fixtures/empty.txt" file to "Files" filemanager
|
||||
And I click on "Save changes" "button"
|
||||
And I am on the "System logs report" page
|
||||
And I click on "Get these logs" "button"
|
||||
Then I should see "The user with id '2' has uploaded file '/empty.txt' to the draft file area with item id" in the "File added to draft area" "table_row"
|
||||
And I should see "Size: 32 bytes. Content hash: " in the "File added to draft area" "table_row"
|
||||
|
@ -52,7 +52,7 @@ $itemid = optional_param('itemid', 0, PARAM_INT);
|
||||
|
||||
echo $OUTPUT->header();
|
||||
|
||||
// authenticate the user
|
||||
// Authenticate the user.
|
||||
$token = required_param('token', PARAM_ALPHANUM);
|
||||
$webservicelib = new webservice();
|
||||
$authenticationinfo = $webservicelib->authenticate_user($token);
|
||||
@ -67,33 +67,33 @@ $fs = get_file_storage();
|
||||
|
||||
$totalsize = 0;
|
||||
$files = array();
|
||||
foreach ($_FILES as $fieldname=>$uploaded_file) {
|
||||
// check upload errors
|
||||
foreach ($_FILES as $fieldname => $uploadedfile) {
|
||||
// Check upload errors.
|
||||
if (!empty($_FILES[$fieldname]['error'])) {
|
||||
switch ($_FILES[$fieldname]['error']) {
|
||||
case UPLOAD_ERR_INI_SIZE:
|
||||
throw new moodle_exception('upload_error_ini_size', 'repository_upload');
|
||||
break;
|
||||
case UPLOAD_ERR_FORM_SIZE:
|
||||
throw new moodle_exception('upload_error_form_size', 'repository_upload');
|
||||
break;
|
||||
case UPLOAD_ERR_PARTIAL:
|
||||
throw new moodle_exception('upload_error_partial', 'repository_upload');
|
||||
break;
|
||||
case UPLOAD_ERR_NO_FILE:
|
||||
throw new moodle_exception('upload_error_no_file', 'repository_upload');
|
||||
break;
|
||||
case UPLOAD_ERR_NO_TMP_DIR:
|
||||
throw new moodle_exception('upload_error_no_tmp_dir', 'repository_upload');
|
||||
break;
|
||||
case UPLOAD_ERR_CANT_WRITE:
|
||||
throw new moodle_exception('upload_error_cant_write', 'repository_upload');
|
||||
break;
|
||||
case UPLOAD_ERR_EXTENSION:
|
||||
throw new moodle_exception('upload_error_extension', 'repository_upload');
|
||||
break;
|
||||
default:
|
||||
throw new moodle_exception('nofile');
|
||||
case UPLOAD_ERR_INI_SIZE:
|
||||
throw new moodle_exception('upload_error_ini_size', 'repository_upload');
|
||||
break;
|
||||
case UPLOAD_ERR_FORM_SIZE:
|
||||
throw new moodle_exception('upload_error_form_size', 'repository_upload');
|
||||
break;
|
||||
case UPLOAD_ERR_PARTIAL:
|
||||
throw new moodle_exception('upload_error_partial', 'repository_upload');
|
||||
break;
|
||||
case UPLOAD_ERR_NO_FILE:
|
||||
throw new moodle_exception('upload_error_no_file', 'repository_upload');
|
||||
break;
|
||||
case UPLOAD_ERR_NO_TMP_DIR:
|
||||
throw new moodle_exception('upload_error_no_tmp_dir', 'repository_upload');
|
||||
break;
|
||||
case UPLOAD_ERR_CANT_WRITE:
|
||||
throw new moodle_exception('upload_error_cant_write', 'repository_upload');
|
||||
break;
|
||||
case UPLOAD_ERR_EXTENSION:
|
||||
throw new moodle_exception('upload_error_extension', 'repository_upload');
|
||||
break;
|
||||
default:
|
||||
throw new moodle_exception('nofile');
|
||||
}
|
||||
}
|
||||
|
||||
@ -102,16 +102,18 @@ foreach ($_FILES as $fieldname=>$uploaded_file) {
|
||||
|
||||
$file = new stdClass();
|
||||
$file->filename = clean_param($_FILES[$fieldname]['name'], PARAM_FILE);
|
||||
// check system maxbytes setting
|
||||
// Check system maxbytes setting.
|
||||
if (($_FILES[$fieldname]['size'] > get_max_upload_file_size($CFG->maxbytes))) {
|
||||
// oversize file will be ignored, error added to array to notify
|
||||
// web service client
|
||||
// Oversize file will be ignored, error added to array to notify
|
||||
// web service client.
|
||||
$file->errortype = 'fileoversized';
|
||||
$file->error = get_string('maxbytes', 'error');
|
||||
} else {
|
||||
$file->filepath = $_FILES[$fieldname]['tmp_name'];
|
||||
// calculate total size of upload
|
||||
// Calculate total size of upload.
|
||||
$totalsize += $_FILES[$fieldname]['size'];
|
||||
// Size of individual file.
|
||||
$file->size = $_FILES[$fieldname]['size'];
|
||||
}
|
||||
$files[] = $file;
|
||||
}
|
||||
@ -133,32 +135,47 @@ if ($maxupload !== USER_CAN_IGNORE_FILE_SIZE_LIMITS && $totalsize > $maxupload)
|
||||
$results = array();
|
||||
foreach ($files as $file) {
|
||||
if (!empty($file->error)) {
|
||||
// including error and filename
|
||||
// Including error and filename.
|
||||
$results[] = $file;
|
||||
continue;
|
||||
}
|
||||
$file_record = new stdClass;
|
||||
$file_record->component = 'user';
|
||||
$file_record->contextid = $context->id;
|
||||
$file_record->userid = $USER->id;
|
||||
$file_record->filearea = 'draft';
|
||||
$file_record->filename = $file->filename;
|
||||
$file_record->filepath = $filepath;
|
||||
$file_record->itemid = $itemid;
|
||||
$file_record->license = $CFG->sitedefaultlicense;
|
||||
$file_record->author = fullname($authenticationinfo['user']);
|
||||
$file_record->source = serialize((object)array('source' => $file->filename));
|
||||
$filerecord = new stdClass;
|
||||
$filerecord->component = 'user';
|
||||
$filerecord->contextid = $context->id;
|
||||
$filerecord->userid = $USER->id;
|
||||
$filerecord->filearea = 'draft';
|
||||
$filerecord->filename = $file->filename;
|
||||
$filerecord->filepath = $filepath;
|
||||
$filerecord->itemid = $itemid;
|
||||
$filerecord->license = $CFG->sitedefaultlicense;
|
||||
$filerecord->author = fullname($authenticationinfo['user']);
|
||||
$filerecord->source = serialize((object)array('source' => $file->filename));
|
||||
$filerecord->filesize = $file->size;
|
||||
|
||||
//Check if the file already exist
|
||||
$existingfile = $fs->file_exists($file_record->contextid, $file_record->component, $file_record->filearea,
|
||||
$file_record->itemid, $file_record->filepath, $file_record->filename);
|
||||
// Check if the file already exist.
|
||||
$existingfile = $fs->file_exists($filerecord->contextid, $filerecord->component, $filerecord->filearea,
|
||||
$filerecord->itemid, $filerecord->filepath, $filerecord->filename);
|
||||
if ($existingfile) {
|
||||
$file->errortype = 'filenameexist';
|
||||
$file->error = get_string('filenameexist', 'webservice', $file->filename);
|
||||
$results[] = $file;
|
||||
} else {
|
||||
$stored_file = $fs->create_file_from_pathname($file_record, $file->filepath);
|
||||
$results[] = $file_record;
|
||||
$storedfile = $fs->create_file_from_pathname($filerecord, $file->filepath);
|
||||
$results[] = $filerecord;
|
||||
|
||||
// Log the event when a file is uploaded to the draft area.
|
||||
$logevent = \core\event\draft_file_added::create([
|
||||
'objectid' => $storedfile->get_id(),
|
||||
'context' => $context,
|
||||
'other' => [
|
||||
'itemid' => $filerecord->itemid,
|
||||
'filename' => $filerecord->filename,
|
||||
'filesize' => $filerecord->filesize,
|
||||
'filepath' => $filerecord->filepath,
|
||||
'contenthash' => $storedfile->get_contenthash(),
|
||||
],
|
||||
]);
|
||||
$logevent->trigger();
|
||||
}
|
||||
}
|
||||
echo json_encode($results);
|
||||
|
Loading…
x
Reference in New Issue
Block a user