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/>.
|
|
|
|
|
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
|
2010-09-06 11:29:21 +00:00
|
|
|
* @package repository
|
|
|
|
* @subpackage upload
|
|
|
|
* @copyright 2009 Dongsheng Cai
|
|
|
|
* @author Dongsheng Cai <dongsheng@moodle.com>
|
|
|
|
* @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
|
|
|
*/
|
2009-11-13 05:31:02 +00:00
|
|
|
public function upload() {
|
2010-07-05 07:27:49 +00:00
|
|
|
global $USER, $CFG;
|
2010-05-07 03:46:30 +00:00
|
|
|
|
2010-07-05 07:27:49 +00:00
|
|
|
$types = optional_param('accepted_types', '*', PARAM_RAW);
|
|
|
|
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
|
|
|
|
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';
|
2010-07-26 08:37:48 +00:00
|
|
|
$record->filepath = optional_param('savepath', '/', PARAM_PATH);
|
2010-07-05 07:27:49 +00:00
|
|
|
$record->itemid = optional_param('itemid', 0, PARAM_INT);
|
|
|
|
$record->license = optional_param('license', $CFG->sitedefaultlicense, PARAM_TEXT);
|
|
|
|
$record->author = optional_param('author', '', PARAM_TEXT);
|
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'])) {
|
|
|
|
throw new moodle_exception('maxbytes');
|
|
|
|
}
|
|
|
|
|
2010-03-29 03:39:08 +00:00
|
|
|
if (empty($record->filename)) {
|
2010-07-26 08:37:48 +00:00
|
|
|
$record->filename = clean_param($_FILES[$elname]['name'], PARAM_FILE);
|
2009-11-13 05:31:02 +00:00
|
|
|
}
|
|
|
|
|
2010-07-05 07:27:49 +00:00
|
|
|
if ($this->mimetypes != '*') {
|
|
|
|
// check filetype
|
2010-07-22 05:32:05 +00:00
|
|
|
$filemimetype = mimeinfo('type', $_FILES[$elname]['name']);
|
|
|
|
if (!in_array($filemimetype, $this->mimetypes)) {
|
|
|
|
if ($sm->string_exists($filemimetype, 'mimetypes')) {
|
|
|
|
$filemimetype = get_string($filemimetype, 'mimetypes');
|
|
|
|
}
|
|
|
|
throw new moodle_exception('invalidfiletype', 'repository', '', $filemimetype);
|
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-07-26 08:37:48 +00:00
|
|
|
if ($file = $fs->get_file($context->id, $record->component, $record->filearea, $record->itemid, $record->filepath, $record->filename)) {
|
2010-08-24 06:29:24 +00:00
|
|
|
return array(
|
|
|
|
'url'=>moodle_url::make_draftfile_url($file->get_itemid(), $file->get_filepath(), $file->get_filename())->out(),
|
|
|
|
'id'=>$file->get_itemid(),
|
|
|
|
'file'=>$file->get_filename()
|
|
|
|
);
|
2009-11-13 05:31:02 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2010-07-05 07:27:49 +00:00
|
|
|
$stored_file = $fs->create_file_from_pathname($record, $_FILES[$elname]['tmp_name']);
|
2010-03-29 03:39:08 +00:00
|
|
|
|
2009-11-13 05:31:02 +00:00
|
|
|
return array(
|
2010-07-11 13:30:33 +00:00
|
|
|
'url'=>moodle_url::make_draftfile_url($record->itemid, $record->filepath, $record->filename)->out(),
|
2010-03-29 03:39:08 +00:00
|
|
|
'id'=>$record->itemid,
|
2010-07-05 07:27:49 +00:00
|
|
|
'file'=>$record->filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a upload form
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_listing() {
|
|
|
|
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');
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* supported return types
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function supported_returntypes() {
|
|
|
|
return FILE_INTERNAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Upload file to local filesystem pool
|
|
|
|
* @param string $elname name of element
|
|
|
|
* @param string $filearea
|
|
|
|
* @param string $filepath
|
|
|
|
* @param string $filename - use specified filename, if not specified name of uploaded file used
|
|
|
|
* @param bool $override override file if exists
|
|
|
|
* @return mixed stored_file object or false if error; may throw exception if duplicate found
|
|
|
|
*/
|
|
|
|
public function upload_to_filepool($elname, $record, $override = true) {
|
2009-11-13 05:31:02 +00:00
|
|
|
}
|
2008-09-01 08:21:39 +00:00
|
|
|
}
|