| 1 | efrain | 1 | <?php
 | 
        
           |  |  | 2 | // This file is part of Moodle - http://moodle.org/
 | 
        
           |  |  | 3 | //
 | 
        
           |  |  | 4 | // Moodle is free software: you can redistribute it and/or modify
 | 
        
           |  |  | 5 | // it under the terms of the GNU General Public License as published by
 | 
        
           |  |  | 6 | // the Free Software Foundation, either version 3 of the License, or
 | 
        
           |  |  | 7 | // (at your option) any later version.
 | 
        
           |  |  | 8 | //
 | 
        
           |  |  | 9 | // Moodle is distributed in the hope that it will be useful,
 | 
        
           |  |  | 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
        
           |  |  | 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
        
           |  |  | 12 | // GNU General Public License for more details.
 | 
        
           |  |  | 13 | //
 | 
        
           |  |  | 14 | // You should have received a copy of the GNU General Public License
 | 
        
           |  |  | 15 | // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 | 
        
           |  |  | 16 |   | 
        
           |  |  | 17 | /**
 | 
        
           |  |  | 18 |  * This plugin is used to upload files
 | 
        
           |  |  | 19 |  *
 | 
        
           |  |  | 20 |  * @since Moodle 2.0
 | 
        
           |  |  | 21 |  * @package    repository_upload
 | 
        
           |  |  | 22 |  * @copyright  2010 Dongsheng Cai {@link http://dongsheng.org}
 | 
        
           |  |  | 23 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 24 |  */
 | 
        
           |  |  | 25 |   | 
        
           |  |  | 26 | defined('MOODLE_INTERNAL') || die();
 | 
        
           |  |  | 27 | require_once($CFG->dirroot . '/repository/lib.php');
 | 
        
           |  |  | 28 |   | 
        
           |  |  | 29 | /**
 | 
        
           |  |  | 30 |  * A repository plugin to allow user uploading files
 | 
        
           |  |  | 31 |  *
 | 
        
           |  |  | 32 |  * @since Moodle 2.0
 | 
        
           |  |  | 33 |  * @package    repository_upload
 | 
        
           |  |  | 34 |  * @copyright  2009 Dongsheng Cai {@link http://dongsheng.org}
 | 
        
           |  |  | 35 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 36 |  */
 | 
        
           |  |  | 37 |   | 
        
           |  |  | 38 | class repository_upload extends repository {
 | 
        
           |  |  | 39 |     private $mimetypes = array();
 | 
        
           |  |  | 40 |   | 
        
           |  |  | 41 |     /**
 | 
        
           |  |  | 42 |      * Print a upload form
 | 
        
           |  |  | 43 |      * @return array
 | 
        
           |  |  | 44 |      */
 | 
        
           |  |  | 45 |     public function print_login() {
 | 
        
           |  |  | 46 |         return $this->get_listing();
 | 
        
           |  |  | 47 |     }
 | 
        
           |  |  | 48 |   | 
        
           |  |  | 49 |     /**
 | 
        
           |  |  | 50 |      * Process uploaded file
 | 
        
           |  |  | 51 |      * @return array|bool
 | 
        
           |  |  | 52 |      */
 | 
        
           |  |  | 53 |     public function upload($saveasfilename, $maxbytes) {
 | 
        
           |  |  | 54 |         global $CFG;
 | 
        
           |  |  | 55 |   | 
        
           |  |  | 56 |         $types = optional_param_array('accepted_types', '*', PARAM_RAW);
 | 
        
           |  |  | 57 |         $savepath = optional_param('savepath', '/', PARAM_PATH);
 | 
        
           |  |  | 58 |         $itemid   = optional_param('itemid', 0, PARAM_INT);
 | 
        
           |  |  | 59 |         $license  = optional_param('license', $CFG->sitedefaultlicense, PARAM_TEXT);
 | 
        
           |  |  | 60 |         $author   = optional_param('author', '', PARAM_TEXT);
 | 
        
           |  |  | 61 |         $areamaxbytes = optional_param('areamaxbytes', FILE_AREA_MAX_BYTES_UNLIMITED, PARAM_INT);
 | 
        
           |  |  | 62 |         $overwriteexisting = optional_param('overwrite', false, PARAM_BOOL);
 | 
        
           |  |  | 63 |   | 
        
           |  |  | 64 |         return $this->process_upload($saveasfilename, $maxbytes, $types, $savepath, $itemid, $license, $author,
 | 
        
           |  |  | 65 |                          $overwriteexisting, $areamaxbytes);
 | 
        
           |  |  | 66 |     }
 | 
        
           |  |  | 67 |   | 
        
           |  |  | 68 |     /**
 | 
        
           |  |  | 69 |      * Do the actual processing of the uploaded file
 | 
        
           |  |  | 70 |      * @param string $saveasfilename name to give to the file
 | 
        
           |  |  | 71 |      * @param int $maxbytes maximum file size
 | 
        
           |  |  | 72 |      * @param mixed $types optional array of file extensions that are allowed or '*' for all
 | 
        
           |  |  | 73 |      * @param string $savepath optional path to save the file to
 | 
        
           |  |  | 74 |      * @param int $itemid optional the ID for this item within the file area
 | 
        
           |  |  | 75 |      * @param string $license optional the license to use for this file
 | 
        
           |  |  | 76 |      * @param string $author optional the name of the author of this file
 | 
        
           |  |  | 77 |      * @param bool $overwriteexisting optional user has asked to overwrite the existing file
 | 
        
           |  |  | 78 |      * @param int $areamaxbytes maximum size of the file area.
 | 
        
           | 1441 | ariadna | 79 |      * @return array containing details of the file uploaded
 | 
        
           | 1 | efrain | 80 |      */
 | 
        
           |  |  | 81 |     public function process_upload($saveasfilename, $maxbytes, $types = '*', $savepath = '/', $itemid = 0,
 | 
        
           |  |  | 82 |             $license = null, $author = '', $overwriteexisting = false, $areamaxbytes = FILE_AREA_MAX_BYTES_UNLIMITED) {
 | 
        
           |  |  | 83 |         global $USER, $CFG;
 | 
        
           |  |  | 84 |   | 
        
           |  |  | 85 |         \core\session\manager::write_close();
 | 
        
           |  |  | 86 |   | 
        
           |  |  | 87 |         if ((is_array($types) and in_array('*', $types)) or $types == '*') {
 | 
        
           |  |  | 88 |             $this->mimetypes = '*';
 | 
        
           |  |  | 89 |         } else {
 | 
        
           |  |  | 90 |             foreach ($types as $type) {
 | 
        
           |  |  | 91 |                 $this->mimetypes[] = mimeinfo('type', $type);
 | 
        
           |  |  | 92 |             }
 | 
        
           |  |  | 93 |         }
 | 
        
           |  |  | 94 |   | 
        
           |  |  | 95 |         if ($license == null) {
 | 
        
           |  |  | 96 |             $license = $CFG->sitedefaultlicense;
 | 
        
           |  |  | 97 |         }
 | 
        
           |  |  | 98 |   | 
        
           |  |  | 99 |         $record = new stdClass();
 | 
        
           |  |  | 100 |         $record->filearea = 'draft';
 | 
        
           |  |  | 101 |         $record->component = 'user';
 | 
        
           |  |  | 102 |         $record->filepath = $savepath;
 | 
        
           |  |  | 103 |         $record->itemid   = $itemid;
 | 
        
           |  |  | 104 |         $record->license  = $license;
 | 
        
           |  |  | 105 |         $record->author   = $author;
 | 
        
           |  |  | 106 |   | 
        
           |  |  | 107 |         $context = context_user::instance($USER->id);
 | 
        
           |  |  | 108 |         $elname = 'repo_upload_file';
 | 
        
           |  |  | 109 |   | 
        
           |  |  | 110 |         $fs = get_file_storage();
 | 
        
           |  |  | 111 |         $sm = get_string_manager();
 | 
        
           |  |  | 112 |   | 
        
           |  |  | 113 |         if ($record->filepath !== '/') {
 | 
        
           |  |  | 114 |             $record->filepath = file_correct_filepath($record->filepath);
 | 
        
           |  |  | 115 |         }
 | 
        
           |  |  | 116 |   | 
        
           |  |  | 117 |         if (!isset($_FILES[$elname])) {
 | 
        
           |  |  | 118 |             throw new moodle_exception('nofile');
 | 
        
           |  |  | 119 |         }
 | 
        
           |  |  | 120 |         if (!empty($_FILES[$elname]['error'])) {
 | 
        
           |  |  | 121 |             switch ($_FILES[$elname]['error']) {
 | 
        
           |  |  | 122 |                 case UPLOAD_ERR_INI_SIZE:
 | 
        
           |  |  | 123 |                     throw new moodle_exception('upload_error_ini_size', 'repository_upload');
 | 
        
           |  |  | 124 |                     break;
 | 
        
           |  |  | 125 |                 case UPLOAD_ERR_FORM_SIZE:
 | 
        
           |  |  | 126 |                     throw new moodle_exception('upload_error_form_size', 'repository_upload');
 | 
        
           |  |  | 127 |                     break;
 | 
        
           |  |  | 128 |                 case UPLOAD_ERR_PARTIAL:
 | 
        
           |  |  | 129 |                     throw new moodle_exception('upload_error_partial', 'repository_upload');
 | 
        
           |  |  | 130 |                     break;
 | 
        
           |  |  | 131 |                 case UPLOAD_ERR_NO_FILE:
 | 
        
           |  |  | 132 |                     throw new moodle_exception('upload_error_no_file', 'repository_upload');
 | 
        
           |  |  | 133 |                     break;
 | 
        
           |  |  | 134 |                 case UPLOAD_ERR_NO_TMP_DIR:
 | 
        
           |  |  | 135 |                     throw new moodle_exception('upload_error_no_tmp_dir', 'repository_upload');
 | 
        
           |  |  | 136 |                     break;
 | 
        
           |  |  | 137 |                 case UPLOAD_ERR_CANT_WRITE:
 | 
        
           |  |  | 138 |                     throw new moodle_exception('upload_error_cant_write', 'repository_upload');
 | 
        
           |  |  | 139 |                     break;
 | 
        
           |  |  | 140 |                 case UPLOAD_ERR_EXTENSION:
 | 
        
           |  |  | 141 |                     throw new moodle_exception('upload_error_extension', 'repository_upload');
 | 
        
           |  |  | 142 |                     break;
 | 
        
           |  |  | 143 |                 default:
 | 
        
           |  |  | 144 |                     throw new moodle_exception('nofile');
 | 
        
           |  |  | 145 |             }
 | 
        
           |  |  | 146 |         }
 | 
        
           |  |  | 147 |   | 
        
           | 1441 | ariadna | 148 |         $avscanstarttime = microtime(true);
 | 
        
           | 1 | efrain | 149 |         \core\antivirus\manager::scan_file($_FILES[$elname]['tmp_name'], $_FILES[$elname]['name'], true);
 | 
        
           | 1441 | ariadna | 150 |         $avscantime = microtime(true) - $avscanstarttime;
 | 
        
           | 1 | efrain | 151 |   | 
        
           |  |  | 152 |         // {@link repository::build_source_field()}
 | 
        
           |  |  | 153 |         $sourcefield = $this->get_file_source_info($_FILES[$elname]['name']);
 | 
        
           |  |  | 154 |         $record->source = self::build_source_field($sourcefield);
 | 
        
           |  |  | 155 |   | 
        
           |  |  | 156 |         if (empty($saveasfilename)) {
 | 
        
           |  |  | 157 |             $record->filename = clean_param($_FILES[$elname]['name'], PARAM_FILE);
 | 
        
           |  |  | 158 |         } else {
 | 
        
           |  |  | 159 |             $ext = '';
 | 
        
           |  |  | 160 |             $match = array();
 | 
        
           |  |  | 161 |             $filename = clean_param($_FILES[$elname]['name'], PARAM_FILE);
 | 
        
           |  |  | 162 |             if (strpos($filename, '.') === false) {
 | 
        
           |  |  | 163 |                 // File has no extension at all - do not add a dot.
 | 
        
           |  |  | 164 |                 $record->filename = $saveasfilename;
 | 
        
           |  |  | 165 |             } else {
 | 
        
           |  |  | 166 |                 if (preg_match('/\.([a-z0-9]+)$/i', $filename, $match)) {
 | 
        
           |  |  | 167 |                     if (isset($match[1])) {
 | 
        
           |  |  | 168 |                         $ext = $match[1];
 | 
        
           |  |  | 169 |                     }
 | 
        
           |  |  | 170 |                 }
 | 
        
           |  |  | 171 |                 $ext = !empty($ext) ? $ext : '';
 | 
        
           |  |  | 172 |                 if (preg_match('#\.(' . $ext . ')$#i', $saveasfilename)) {
 | 
        
           |  |  | 173 |                     // The saveas filename contains file extension already.
 | 
        
           |  |  | 174 |                     $record->filename = $saveasfilename;
 | 
        
           |  |  | 175 |                 } else {
 | 
        
           |  |  | 176 |                     $record->filename = $saveasfilename . '.' . $ext;
 | 
        
           |  |  | 177 |                 }
 | 
        
           |  |  | 178 |             }
 | 
        
           |  |  | 179 |         }
 | 
        
           |  |  | 180 |   | 
        
           |  |  | 181 |         // Check the file has some non-null contents - usually an indication that a user has
 | 
        
           |  |  | 182 |         // tried to upload a folder by mistake.
 | 
        
           |  |  | 183 |         if (!$this->check_valid_contents($_FILES[$elname]['tmp_name'])) {
 | 
        
           |  |  | 184 |             throw new moodle_exception('upload_error_invalid_file', 'repository_upload', '', $record->filename);
 | 
        
           |  |  | 185 |         }
 | 
        
           |  |  | 186 |   | 
        
           |  |  | 187 |         if ($this->mimetypes != '*') {
 | 
        
           |  |  | 188 |             // Check filetype.
 | 
        
           |  |  | 189 |             $filemimetype = file_storage::mimetype($_FILES[$elname]['tmp_name'], $record->filename);
 | 
        
           |  |  | 190 |             if (!in_array($filemimetype, $this->mimetypes)) {
 | 
        
           |  |  | 191 |                 throw new moodle_exception('invalidfiletype', 'repository', '',
 | 
        
           |  |  | 192 |                     get_mimetype_description(array('filename' => $_FILES[$elname]['name'])));
 | 
        
           |  |  | 193 |             }
 | 
        
           |  |  | 194 |         }
 | 
        
           |  |  | 195 |   | 
        
           |  |  | 196 |         if (empty($record->itemid)) {
 | 
        
           |  |  | 197 |             $record->itemid = 0;
 | 
        
           |  |  | 198 |         }
 | 
        
           |  |  | 199 |   | 
        
           |  |  | 200 |         $filesize = filesize($_FILES[$elname]['tmp_name']);
 | 
        
           |  |  | 201 |         if (($maxbytes !== -1) && ($filesize > $maxbytes)) {
 | 
        
           |  |  | 202 |             $maxbytesdisplay = display_size($maxbytes, 0);
 | 
        
           |  |  | 203 |             throw new file_exception('maxbytesfile', (object) array('file' => $record->filename,
 | 
        
           |  |  | 204 |                                                                     'size' => $maxbytesdisplay));
 | 
        
           |  |  | 205 |         }
 | 
        
           |  |  | 206 |   | 
        
           |  |  | 207 |         if (file_is_draft_area_limit_reached($record->itemid, $areamaxbytes, $filesize)) {
 | 
        
           |  |  | 208 |             throw new file_exception('maxareabytes');
 | 
        
           |  |  | 209 |         }
 | 
        
           |  |  | 210 |         // Ensure the user does not upload too many draft files in a short period.
 | 
        
           |  |  | 211 |         if (file_is_draft_areas_limit_reached($USER->id)) {
 | 
        
           |  |  | 212 |             throw new file_exception('maxdraftitemids');
 | 
        
           |  |  | 213 |         }
 | 
        
           |  |  | 214 |   | 
        
           |  |  | 215 |         $record->contextid = $context->id;
 | 
        
           |  |  | 216 |         $record->userid    = $USER->id;
 | 
        
           |  |  | 217 |   | 
        
           |  |  | 218 |         if (repository::draftfile_exists($record->itemid, $record->filepath, $record->filename)) {
 | 
        
           |  |  | 219 |             $existingfilename = $record->filename;
 | 
        
           |  |  | 220 |             $unusedfilename = repository::get_unused_filename($record->itemid, $record->filepath, $record->filename);
 | 
        
           |  |  | 221 |             $record->filename = $unusedfilename;
 | 
        
           |  |  | 222 |             $storedfile = $fs->create_file_from_pathname($record, $_FILES[$elname]['tmp_name']);
 | 
        
           |  |  | 223 |             if ($overwriteexisting) {
 | 
        
           |  |  | 224 |                 repository::overwrite_existing_draftfile($record->itemid, $record->filepath, $existingfilename,
 | 
        
           |  |  | 225 |                     $record->filepath, $record->filename);
 | 
        
           |  |  | 226 |                 $record->filename = $existingfilename;
 | 
        
           |  |  | 227 |             } else {
 | 
        
           |  |  | 228 |                 $event = array();
 | 
        
           |  |  | 229 |                 $event['event'] = 'fileexists';
 | 
        
           |  |  | 230 |                 $event['newfile'] = new stdClass;
 | 
        
           |  |  | 231 |                 $event['newfile']->filepath = $record->filepath;
 | 
        
           |  |  | 232 |                 $event['newfile']->filename = $unusedfilename;
 | 
        
           |  |  | 233 |                 $event['newfile']->url = moodle_url::make_draftfile_url($record->itemid, $record->filepath,
 | 
        
           |  |  | 234 |                     $unusedfilename)->out(false);
 | 
        
           |  |  | 235 |   | 
        
           |  |  | 236 |                 $event['existingfile'] = new stdClass;
 | 
        
           |  |  | 237 |                 $event['existingfile']->filepath = $record->filepath;
 | 
        
           |  |  | 238 |                 $event['existingfile']->filename = $existingfilename;
 | 
        
           |  |  | 239 |                 $event['existingfile']->url = moodle_url::make_draftfile_url($record->itemid, $record->filepath,
 | 
        
           |  |  | 240 |                     $existingfilename)->out(false);
 | 
        
           |  |  | 241 |                 return $event;
 | 
        
           |  |  | 242 |             }
 | 
        
           |  |  | 243 |         } else {
 | 
        
           |  |  | 244 |             $storedfile = $fs->create_file_from_pathname($record, $_FILES[$elname]['tmp_name']);
 | 
        
           |  |  | 245 |         }
 | 
        
           |  |  | 246 |   | 
        
           |  |  | 247 |         $logevent = \core\event\draft_file_added::create([
 | 
        
           |  |  | 248 |                 'objectid' => $storedfile->get_id(),
 | 
        
           |  |  | 249 |                 'context' => $context,
 | 
        
           |  |  | 250 |                 'other' => [
 | 
        
           |  |  | 251 |                         'itemid' => $record->itemid,
 | 
        
           |  |  | 252 |                         'filename' => $record->filename,
 | 
        
           |  |  | 253 |                         'filesize' => $filesize,
 | 
        
           |  |  | 254 |                         'filepath' => $record->filepath,
 | 
        
           |  |  | 255 |                         'contenthash' => $storedfile->get_contenthash(),
 | 
        
           | 1441 | ariadna | 256 |                         'avscantime' => $avscantime,
 | 
        
           | 1 | efrain | 257 |                 ],
 | 
        
           |  |  | 258 |         ]);
 | 
        
           |  |  | 259 |         $logevent->trigger();
 | 
        
           |  |  | 260 |   | 
        
           |  |  | 261 |         return array(
 | 
        
           |  |  | 262 |             'url' => moodle_url::make_draftfile_url($record->itemid, $record->filepath, $record->filename)->out(false),
 | 
        
           |  |  | 263 |             'id' => $record->itemid,
 | 
        
           |  |  | 264 |             'file' => $record->filename);
 | 
        
           |  |  | 265 |     }
 | 
        
           |  |  | 266 |   | 
        
           |  |  | 267 |   | 
        
           |  |  | 268 |     /**
 | 
        
           |  |  | 269 |      * Checks the contents of the given file is not completely NULL - this can happen if a
 | 
        
           |  |  | 270 |      * user drags & drops a folder onto a filemanager / filepicker element
 | 
        
           |  |  | 271 |      * @param string $filepath full path (including filename) to file to check
 | 
        
           |  |  | 272 |      * @return true if file has at least one non-null byte within it
 | 
        
           |  |  | 273 |      */
 | 
        
           |  |  | 274 |     protected function check_valid_contents($filepath) {
 | 
        
           |  |  | 275 |         $buffersize = 4096;
 | 
        
           |  |  | 276 |   | 
        
           |  |  | 277 |         $fp = fopen($filepath, 'r');
 | 
        
           |  |  | 278 |         if (!$fp) {
 | 
        
           |  |  | 279 |             return false; // Cannot read the file - something has gone wrong.
 | 
        
           |  |  | 280 |         }
 | 
        
           |  |  | 281 |         while (!feof($fp)) {
 | 
        
           |  |  | 282 |             // Read the file 4k at a time.
 | 
        
           |  |  | 283 |             $data = fread($fp, $buffersize);
 | 
        
           |  |  | 284 |             if (preg_match('/[^\0]+/', $data)) {
 | 
        
           |  |  | 285 |                 fclose($fp);
 | 
        
           |  |  | 286 |                 return true; // Return as soon as a non-null byte is found.
 | 
        
           |  |  | 287 |             }
 | 
        
           |  |  | 288 |         }
 | 
        
           |  |  | 289 |         // Entire file is NULL.
 | 
        
           |  |  | 290 |         fclose($fp);
 | 
        
           |  |  | 291 |         return false;
 | 
        
           |  |  | 292 |     }
 | 
        
           |  |  | 293 |   | 
        
           |  |  | 294 |     /**
 | 
        
           |  |  | 295 |      * Return a upload form
 | 
        
           |  |  | 296 |      * @return array
 | 
        
           |  |  | 297 |      */
 | 
        
           |  |  | 298 |     public function get_listing($path = '', $page = '') {
 | 
        
           |  |  | 299 |         global $CFG;
 | 
        
           |  |  | 300 |         $ret = array();
 | 
        
           |  |  | 301 |         $ret['nologin']  = true;
 | 
        
           |  |  | 302 |         $ret['nosearch'] = true;
 | 
        
           |  |  | 303 |         $ret['norefresh'] = true;
 | 
        
           |  |  | 304 |         $ret['list'] = array();
 | 
        
           |  |  | 305 |         $ret['dynload'] = false;
 | 
        
           |  |  | 306 |         $ret['upload'] = array('label' => get_string('attachment', 'repository'), 'id' => 'repo-form');
 | 
        
           |  |  | 307 |         $ret['allowcaching'] = true; // Indicates that result of get_listing() can be cached in filepicker.js.
 | 
        
           |  |  | 308 |         return $ret;
 | 
        
           |  |  | 309 |     }
 | 
        
           |  |  | 310 |   | 
        
           |  |  | 311 |     /**
 | 
        
           |  |  | 312 |      * supported return types
 | 
        
           |  |  | 313 |      * @return int
 | 
        
           |  |  | 314 |      */
 | 
        
           |  |  | 315 |     public function supported_returntypes() {
 | 
        
           |  |  | 316 |         return FILE_INTERNAL;
 | 
        
           |  |  | 317 |     }
 | 
        
           |  |  | 318 |   | 
        
           |  |  | 319 |     /**
 | 
        
           |  |  | 320 |      * Is this repository accessing private data?
 | 
        
           |  |  | 321 |      *
 | 
        
           |  |  | 322 |      * @return bool
 | 
        
           |  |  | 323 |      */
 | 
        
           |  |  | 324 |     public function contains_private_data() {
 | 
        
           |  |  | 325 |         return false;
 | 
        
           |  |  | 326 |     }
 | 
        
           |  |  | 327 | }
 |