mirrored from git://git.moodle.org/moodle.git
/
lib.php
285 lines (252 loc) · 11.3 KB
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?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/>.
/**
* 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');
/**
* A repository plugin to allow user uploading files
*
* @since 2.0
* @package repository_upload
* @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class repository_upload extends repository {
private $mimetypes = array();
/**
* Print a upload form
* @return array
*/
public function print_login() {
return $this->get_listing();
}
/**
* Process uploaded file
* @return array|bool
*/
public function upload($saveas_filename, $maxbytes) {
global $CFG;
$types = optional_param_array('accepted_types', '*', PARAM_RAW);
$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);
$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);
}
/**
* 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
* @param bool $overwriteexisting optional user has asked to overwrite the existing file
* @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,
$license = null, $author = '', $overwriteexisting = false, $areamaxbytes = FILE_AREA_MAX_BYTES_UNLIMITED) {
global $USER, $CFG;
if ((is_array($types) and in_array('*', $types)) or $types == '*') {
$this->mimetypes = '*';
} else {
foreach ($types as $type) {
$this->mimetypes[] = mimeinfo('type', $type);
}
}
if ($license == null) {
$license = $CFG->sitedefaultlicense;
}
$record = new stdClass();
$record->filearea = 'draft';
$record->component = 'user';
$record->filepath = $savepath;
$record->itemid = $itemid;
$record->license = $license;
$record->author = $author;
$context = context_user::instance($USER->id);
$elname = 'repo_upload_file';
$fs = get_file_storage();
$sm = get_string_manager();
if ($record->filepath !== '/') {
$record->filepath = file_correct_filepath($record->filepath);
}
if (!isset($_FILES[$elname])) {
throw new moodle_exception('nofile');
}
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');
}
}
self::antivir_scan_file($_FILES[$elname]['tmp_name'], $_FILES[$elname]['name'], true);
// {@link repository::build_source_field()}
$sourcefield = $this->get_file_source_info($_FILES[$elname]['name']);
$record->source = self::build_source_field($sourcefield);
if (empty($saveas_filename)) {
$record->filename = clean_param($_FILES[$elname]['name'], PARAM_FILE);
} else {
$ext = '';
$match = array();
$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;
} else {
if (preg_match('/\.([a-z0-9]+)$/i', $filename, $match)) {
if (isset($match[1])) {
$ext = $match[1];
}
}
$ext = !empty($ext) ? $ext : '';
if (preg_match('#\.(' . $ext . ')$#i', $saveas_filename)) {
// saveas filename contains file extension already
$record->filename = $saveas_filename;
} else {
$record->filename = $saveas_filename . '.' . $ext;
}
}
}
// 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);
}
if ($this->mimetypes != '*') {
// 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'])));
}
}
if (empty($record->itemid)) {
$record->itemid = 0;
}
if (file_is_draft_area_limit_reached($record->itemid, $areamaxbytes, filesize($_FILES[$elname]['tmp_name']))) {
throw new file_exception('maxareabytes');
}
if (($maxbytes!==-1) && (filesize($_FILES[$elname]['tmp_name']) > $maxbytes)) {
throw new file_exception('maxbytes');
}
$record->contextid = $context->id;
$record->userid = $USER->id;
if (repository::draftfile_exists($record->itemid, $record->filepath, $record->filename)) {
if ($overwriteexisting) {
repository::delete_tempfile_from_draft($record->itemid, $record->filepath, $record->filename);
} else {
$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;
$event['newfile']->url = moodle_url::make_draftfile_url($record->itemid, $record->filepath, $unused_filename)->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);
return $event;
}
}
$stored_file = $fs->create_file_from_pathname($record, $_FILES[$elname]['tmp_name']);
return array(
'url'=>moodle_url::make_draftfile_url($record->itemid, $record->filepath, $record->filename)->out(false),
'id'=>$record->itemid,
'file'=>$record->filename);
}
/**
* 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
* @param string $filepath full path (including filename) to file to check
* @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;
}
/**
* Return a upload form
* @return array
*/
public function get_listing($path = '', $page = '') {
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');
$ret['allowcaching'] = true; // indicates that result of get_listing() can be cached in filepicker.js
return $ret;
}
/**
* supported return types
* @return int
*/
public function supported_returntypes() {
return FILE_INTERNAL;
}
}