2008-09-01 08:21:39 +00:00
|
|
|
<?php
|
2009-12-17 03:40:38 +00:00
|
|
|
|
|
|
|
// 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/>.
|
|
|
|
|
2012-05-12 04:14:53 +08:00
|
|
|
/**
|
|
|
|
* This plugin is used to upload files
|
|
|
|
*
|
|
|
|
* @since 2.0
|
|
|
|
* @package repository_upload
|
|
|
|
* @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
require_once($CFG->dirroot . '/repository/lib.php');
|
|
|
|
|
2008-09-01 08:21:39 +00:00
|
|
|
/**
|
2010-07-05 07:27:49 +00:00
|
|
|
* A repository plugin to allow user uploading files
|
2008-09-01 08:21:39 +00:00
|
|
|
*
|
2009-12-17 03:40:38 +00:00
|
|
|
* @since 2.0
|
2012-05-12 04:14:53 +08:00
|
|
|
* @package repository_upload
|
|
|
|
* @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
|
2010-09-06 11:29:21 +00:00
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2008-09-01 08:21:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
class repository_upload extends repository {
|
2010-07-05 07:27:49 +00:00
|
|
|
private $mimetypes = array();
|
2008-09-01 08:21:39 +00:00
|
|
|
|
2008-09-18 05:33:44 +00:00
|
|
|
/**
|
2009-11-16 02:20:13 +00:00
|
|
|
* Print a upload form
|
|
|
|
* @return array
|
2008-09-18 05:33:44 +00:00
|
|
|
*/
|
2009-11-16 02:20:13 +00:00
|
|
|
public function print_login() {
|
2008-09-01 08:21:39 +00:00
|
|
|
return $this->get_listing();
|
|
|
|
}
|
|
|
|
|
2009-11-16 02:20:13 +00:00
|
|
|
/**
|
|
|
|
* Process uploaded file
|
2010-07-05 07:27:49 +00:00
|
|
|
* @return array|bool
|
2009-11-16 02:20:13 +00:00
|
|
|
*/
|
2010-09-29 10:13:35 +00:00
|
|
|
public function upload($saveas_filename, $maxbytes) {
|
2012-02-17 21:23:28 +00:00
|
|
|
global $CFG;
|
2010-05-07 03:46:30 +00:00
|
|
|
|
2011-08-17 16:27:15 +02:00
|
|
|
$types = optional_param_array('accepted_types', '*', PARAM_RAW);
|
2012-02-17 21:23:28 +00:00
|
|
|
$savepath = optional_param('savepath', '/', PARAM_PATH);
|
|
|
|
$itemid = optional_param('itemid', 0, PARAM_INT);
|
|
|
|
$license = optional_param('license', $CFG->sitedefaultlicense, PARAM_TEXT);
|
|
|
|
$author = optional_param('author', '', PARAM_TEXT);
|
|
|
|
|
|
|
|
return $this->process_upload($saveas_filename, $maxbytes, $types, $savepath, $itemid, $license, $author);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do the actual processing of the uploaded file
|
|
|
|
* @param string $saveas_filename 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
|
|
|
|
* @param int $itemid optional the ID for this item within the file area
|
|
|
|
* @param string $license optional the license to use for this file
|
|
|
|
* @param string $author optional the name of the author of this file
|
|
|
|
* @return object containing details of the file uploaded
|
|
|
|
*/
|
|
|
|
public function process_upload($saveas_filename, $maxbytes, $types = '*', $savepath = '/', $itemid = 0, $license = null, $author = '') {
|
|
|
|
global $USER, $CFG;
|
|
|
|
|
2010-07-05 07:27:49 +00:00
|
|
|
if ((is_array($types) and in_array('*', $types)) or $types == '*') {
|
|
|
|
$this->mimetypes = '*';
|
|
|
|
} else {
|
|
|
|
foreach ($types as $type) {
|
|
|
|
$this->mimetypes[] = mimeinfo('type', $type);
|
|
|
|
}
|
2010-05-07 03:46:30 +00:00
|
|
|
}
|
2009-11-13 05:31:02 +00:00
|
|
|
|
2012-02-17 21:23:28 +00:00
|
|
|
if ($license == null) {
|
|
|
|
$license = $CFG->sitedefaultlicense;
|
|
|
|
}
|
|
|
|
|
2010-09-21 08:54:01 +00:00
|
|
|
$record = new stdClass();
|
2010-07-05 07:27:49 +00:00
|
|
|
$record->filearea = 'draft';
|
|
|
|
$record->component = 'user';
|
2012-02-17 21:23:28 +00:00
|
|
|
$record->filepath = $savepath;
|
|
|
|
$record->itemid = $itemid;
|
|
|
|
$record->license = $license;
|
|
|
|
$record->author = $author;
|
2009-11-13 05:31:02 +00:00
|
|
|
|
2010-03-29 03:39:08 +00:00
|
|
|
$context = get_context_instance(CONTEXT_USER, $USER->id);
|
2010-07-05 07:27:49 +00:00
|
|
|
$elname = 'repo_upload_file';
|
2010-07-03 13:37:13 +00:00
|
|
|
|
2010-03-29 03:39:08 +00:00
|
|
|
$fs = get_file_storage();
|
2010-07-22 05:32:05 +00:00
|
|
|
$sm = get_string_manager();
|
2009-11-13 05:31:02 +00:00
|
|
|
|
2010-03-29 03:39:08 +00:00
|
|
|
if ($record->filepath !== '/') {
|
2010-07-05 07:27:49 +00:00
|
|
|
$record->filepath = file_correct_filepath($record->filepath);
|
2009-11-13 05:31:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($_FILES[$elname])) {
|
|
|
|
throw new moodle_exception('nofile');
|
|
|
|
}
|
|
|
|
if (!empty($_FILES[$elname]['error'])) {
|
2010-12-10 03:16:59 +00:00
|
|
|
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');
|
|
|
|
}
|
2009-11-13 05:31:02 +00:00
|
|
|
}
|
|
|
|
|
2011-08-07 11:26:03 +02:00
|
|
|
// scan the files, throws exception and deletes if virus found
|
|
|
|
// this is tricky because clamdscan daemon might not be able to access the files
|
|
|
|
$permissions = fileperms($_FILES[$elname]['tmp_name']);
|
|
|
|
@chmod($_FILES[$elname]['tmp_name'], $CFG->filepermissions);
|
|
|
|
self::antivir_scan_file($_FILES[$elname]['tmp_name'], $_FILES[$elname]['name'], true);
|
|
|
|
@chmod($_FILES[$elname]['tmp_name'], $permissions);
|
|
|
|
|
2010-09-29 07:05:51 +00:00
|
|
|
if (empty($saveas_filename)) {
|
2010-10-04 09:35:14 +00:00
|
|
|
$record->filename = clean_param($_FILES[$elname]['name'], PARAM_FILE);
|
2010-09-29 07:05:51 +00:00
|
|
|
} else {
|
2011-02-09 16:45:14 +08:00
|
|
|
$ext = '';
|
|
|
|
$match = array();
|
|
|
|
$filename = clean_param($_FILES[$elname]['name'], PARAM_FILE);
|
|
|
|
if (preg_match('/\.([a-z0-9]+)$/i', $filename, $match)) {
|
|
|
|
if (isset($match[1])) {
|
|
|
|
$ext = $match[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$ext = !empty($ext) ? $ext : '';
|
2011-02-14 09:50:07 +01:00
|
|
|
if (preg_match('#\.(' . $ext . ')$#i', $saveas_filename)) {
|
2011-02-09 16:45:14 +08:00
|
|
|
// saveas filename contains file extension already
|
|
|
|
$record->filename = $saveas_filename;
|
|
|
|
} else {
|
|
|
|
$record->filename = $saveas_filename . '.' . $ext;
|
|
|
|
}
|
2009-11-13 05:31:02 +00:00
|
|
|
}
|
|
|
|
|
2012-02-07 07:26:54 +00:00
|
|
|
// Check the file has some non-null contents - usually an indication that a user has
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2010-07-05 07:27:49 +00:00
|
|
|
if ($this->mimetypes != '*') {
|
|
|
|
// check filetype
|
2012-05-23 14:51:55 +08:00
|
|
|
$filemimetype = file_storage::mimetype($_FILES[$elname]['tmp_name']);
|
2010-07-22 05:32:05 +00:00
|
|
|
if (!in_array($filemimetype, $this->mimetypes)) {
|
2012-05-22 14:24:11 +08:00
|
|
|
throw new moodle_exception('invalidfiletype', 'repository', '', get_mimetype_description(array('filename' => $_FILES[$elname]['name'])));
|
2010-07-05 07:27:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-29 03:39:08 +00:00
|
|
|
if (empty($record->itemid)) {
|
|
|
|
$record->itemid = 0;
|
2009-11-13 05:31:02 +00:00
|
|
|
}
|
|
|
|
|
2010-09-29 10:13:35 +00:00
|
|
|
if (($maxbytes!==-1) && (filesize($_FILES[$elname]['tmp_name']) > $maxbytes)) {
|
|
|
|
throw new file_exception('maxbytes');
|
|
|
|
}
|
2010-03-29 03:39:08 +00:00
|
|
|
$record->contextid = $context->id;
|
|
|
|
$record->userid = $USER->id;
|
|
|
|
$record->source = '';
|
2009-11-13 05:31:02 +00:00
|
|
|
|
2011-05-02 10:11:19 +08:00
|
|
|
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']);
|
|
|
|
$event = array();
|
|
|
|
$event['event'] = 'fileexists';
|
|
|
|
$event['newfile'] = new stdClass;
|
|
|
|
$event['newfile']->filepath = $record->filepath;
|
|
|
|
$event['newfile']->filename = $unused_filename;
|
2012-05-21 15:17:53 +08:00
|
|
|
$event['newfile']->url = moodle_url::make_draftfile_url($record->itemid, $record->filepath, $unused_filename)->out(false);
|
2011-05-02 10:11:19 +08:00
|
|
|
|
|
|
|
$event['existingfile'] = new stdClass;
|
|
|
|
$event['existingfile']->filepath = $record->filepath;
|
|
|
|
$event['existingfile']->filename = $existingfilename;
|
2012-05-21 15:17:53 +08:00
|
|
|
$event['existingfile']->url = moodle_url::make_draftfile_url($record->itemid, $record->filepath, $existingfilename)->out(false);
|
2011-05-02 10:11:19 +08:00
|
|
|
return $event;
|
|
|
|
} else {
|
|
|
|
$stored_file = $fs->create_file_from_pathname($record, $_FILES[$elname]['tmp_name']);
|
2010-03-29 03:39:08 +00:00
|
|
|
|
2011-05-02 10:11:19 +08:00
|
|
|
return array(
|
2012-05-21 15:17:53 +08:00
|
|
|
'url'=>moodle_url::make_draftfile_url($record->itemid, $record->filepath, $record->filename)->out(false),
|
2011-05-02 10:11:19 +08:00
|
|
|
'id'=>$record->itemid,
|
|
|
|
'file'=>$record->filename);
|
|
|
|
}
|
2010-07-05 07:27:49 +00:00
|
|
|
}
|
|
|
|
|
2012-02-07 07:26:54 +00:00
|
|
|
/**
|
|
|
|
* Checks the contents of the given file is not completely NULL - this can happen if a
|
|
|
|
* user drags & drops a folder onto a filemanager / filepicker element
|
2012-02-15 12:44:32 +13:00
|
|
|
* @param string $filepath full path (including filename) to file to check
|
2012-02-07 07:26:54 +00:00
|
|
|
* @return true if file has at least one non-null byte within it
|
|
|
|
*/
|
|
|
|
protected function check_valid_contents($filepath) {
|
|
|
|
$buffersize = 4096;
|
|
|
|
|
|
|
|
$fp = fopen($filepath, 'r');
|
|
|
|
if (!$fp) {
|
|
|
|
return false; // Cannot read the file - something has gone wrong
|
|
|
|
}
|
|
|
|
while (!feof($fp)) {
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Entire file is NULL
|
|
|
|
fclose($fp);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-07-05 07:27:49 +00:00
|
|
|
/**
|
|
|
|
* Return a upload form
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-03-18 18:31:32 +01:00
|
|
|
public function get_listing($path = '', $page = '') {
|
2010-07-05 07:27:49 +00:00
|
|
|
global $CFG;
|
|
|
|
$ret = array();
|
|
|
|
$ret['nologin'] = true;
|
|
|
|
$ret['nosearch'] = true;
|
|
|
|
$ret['norefresh'] = true;
|
|
|
|
$ret['list'] = array();
|
|
|
|
$ret['dynload'] = false;
|
|
|
|
$ret['upload'] = array('label'=>get_string('attachment', 'repository'), 'id'=>'repo-form');
|
2012-03-07 11:25:18 +08:00
|
|
|
$ret['allowcaching'] = true; // indicates that result of get_listing() can be cached in filepicker.js
|
2010-07-05 07:27:49 +00:00
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* supported return types
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function supported_returntypes() {
|
|
|
|
return FILE_INTERNAL;
|
|
|
|
}
|
2008-09-01 08:21:39 +00:00
|
|
|
}
|