Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/repository/upload/lib.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00029 class repository_upload extends repository {
00030     private $mimetypes = array();
00031 
00036     public function print_login() {
00037         return $this->get_listing();
00038     }
00039 
00044     public function upload($saveas_filename, $maxbytes) {
00045         global $USER, $CFG;
00046 
00047         $types = optional_param_array('accepted_types', '*', PARAM_RAW);
00048         if ((is_array($types) and in_array('*', $types)) or $types == '*') {
00049             $this->mimetypes = '*';
00050         } else {
00051             foreach ($types as $type) {
00052                 $this->mimetypes[] = mimeinfo('type', $type);
00053             }
00054         }
00055 
00056         $record = new stdClass();
00057         $record->filearea = 'draft';
00058         $record->component = 'user';
00059         $record->filepath = optional_param('savepath', '/', PARAM_PATH);
00060         $record->itemid   = optional_param('itemid', 0, PARAM_INT);
00061         $record->license  = optional_param('license', $CFG->sitedefaultlicense, PARAM_TEXT);
00062         $record->author   = optional_param('author', '', PARAM_TEXT);
00063 
00064         $context = get_context_instance(CONTEXT_USER, $USER->id);
00065         $elname = 'repo_upload_file';
00066 
00067         $fs = get_file_storage();
00068         $sm = get_string_manager();
00069 
00070         if ($record->filepath !== '/') {
00071             $record->filepath = file_correct_filepath($record->filepath);
00072         }
00073 
00074         if (!isset($_FILES[$elname])) {
00075             throw new moodle_exception('nofile');
00076         }
00077         if (!empty($_FILES[$elname]['error'])) {
00078             switch ($_FILES[$elname]['error']) {
00079             case UPLOAD_ERR_INI_SIZE:
00080                 throw new moodle_exception('upload_error_ini_size', 'repository_upload');
00081                 break;
00082             case UPLOAD_ERR_FORM_SIZE:
00083                 throw new moodle_exception('upload_error_form_size', 'repository_upload');
00084                 break;
00085             case UPLOAD_ERR_PARTIAL:
00086                 throw new moodle_exception('upload_error_partial', 'repository_upload');
00087                 break;
00088             case UPLOAD_ERR_NO_FILE:
00089                 throw new moodle_exception('upload_error_no_file', 'repository_upload');
00090                 break;
00091             case UPLOAD_ERR_NO_TMP_DIR:
00092                 throw new moodle_exception('upload_error_no_tmp_dir', 'repository_upload');
00093                 break;
00094             case UPLOAD_ERR_CANT_WRITE:
00095                 throw new moodle_exception('upload_error_cant_write', 'repository_upload');
00096                 break;
00097             case UPLOAD_ERR_EXTENSION:
00098                 throw new moodle_exception('upload_error_extension', 'repository_upload');
00099                 break;
00100             default:
00101                 throw new moodle_exception('nofile');
00102             }
00103         }
00104 
00105         // scan the files, throws exception and deletes if virus found
00106         // this is tricky because clamdscan daemon might not be able to access the files
00107         $permissions = fileperms($_FILES[$elname]['tmp_name']);
00108         @chmod($_FILES[$elname]['tmp_name'], $CFG->filepermissions);
00109         self::antivir_scan_file($_FILES[$elname]['tmp_name'], $_FILES[$elname]['name'], true);
00110         @chmod($_FILES[$elname]['tmp_name'], $permissions);
00111 
00112         if (empty($saveas_filename)) {
00113             $record->filename = clean_param($_FILES[$elname]['name'], PARAM_FILE);
00114         } else {
00115             $ext = '';
00116             $match = array();
00117             $filename = clean_param($_FILES[$elname]['name'], PARAM_FILE);
00118             if (preg_match('/\.([a-z0-9]+)$/i', $filename, $match)) {
00119                 if (isset($match[1])) {
00120                     $ext = $match[1];
00121                 }
00122             }
00123             $ext = !empty($ext) ? $ext : '';
00124             if (preg_match('#\.(' . $ext . ')$#i', $saveas_filename)) {
00125                 // saveas filename contains file extension already
00126                 $record->filename = $saveas_filename;
00127             } else {
00128                 $record->filename = $saveas_filename . '.' . $ext;
00129             }
00130         }
00131 
00132         if ($this->mimetypes != '*') {
00133             // check filetype
00134             $filemimetype = mimeinfo('type', $_FILES[$elname]['name']);
00135             if (!in_array($filemimetype, $this->mimetypes)) {
00136                 if ($sm->string_exists($filemimetype, 'mimetypes')) {
00137                     $filemimetype = get_string($filemimetype, 'mimetypes');
00138                 }
00139                 throw new moodle_exception('invalidfiletype', 'repository', '', $filemimetype);
00140             }
00141         }
00142 
00143         if (empty($record->itemid)) {
00144             $record->itemid = 0;
00145         }
00146 
00147         if (($maxbytes!==-1) && (filesize($_FILES[$elname]['tmp_name']) > $maxbytes)) {
00148             throw new file_exception('maxbytes');
00149         }
00150         $record->contextid = $context->id;
00151         $record->userid    = $USER->id;
00152         $record->source    = '';
00153 
00154         if (repository::draftfile_exists($record->itemid, $record->filepath, $record->filename)) {
00155             $existingfilename = $record->filename;
00156             $unused_filename = repository::get_unused_filename($record->itemid, $record->filepath, $record->filename);
00157             $record->filename = $unused_filename;
00158             $stored_file = $fs->create_file_from_pathname($record, $_FILES[$elname]['tmp_name']);
00159             $event = array();
00160             $event['event'] = 'fileexists';
00161             $event['newfile'] = new stdClass;
00162             $event['newfile']->filepath = $record->filepath;
00163             $event['newfile']->filename = $unused_filename;
00164             $event['newfile']->url = moodle_url::make_draftfile_url($record->itemid, $record->filepath, $unused_filename)->out();
00165 
00166             $event['existingfile'] = new stdClass;
00167             $event['existingfile']->filepath = $record->filepath;
00168             $event['existingfile']->filename = $existingfilename;
00169             $event['existingfile']->url      = moodle_url::make_draftfile_url($record->itemid, $record->filepath, $existingfilename)->out();;
00170             return $event;
00171         } else {
00172             $stored_file = $fs->create_file_from_pathname($record, $_FILES[$elname]['tmp_name']);
00173 
00174             return array(
00175                 'url'=>moodle_url::make_draftfile_url($record->itemid, $record->filepath, $record->filename)->out(),
00176                 'id'=>$record->itemid,
00177                 'file'=>$record->filename);
00178         }
00179     }
00180 
00185     public function get_listing() {
00186         global $CFG;
00187         $ret = array();
00188         $ret['nologin']  = true;
00189         $ret['nosearch'] = true;
00190         $ret['norefresh'] = true;
00191         $ret['list'] = array();
00192         $ret['dynload'] = false;
00193         $ret['upload'] = array('label'=>get_string('attachment', 'repository'), 'id'=>'repo-form');
00194         return $ret;
00195     }
00196 
00201     public function supported_returntypes() {
00202         return FILE_INTERNAL;
00203     }
00204 }
 All Data Structures Namespaces Files Functions Variables Enumerations