Commit | Line | Data |
---|---|---|
a62542e0 | 1 | <?php |
10d53fd3 DC |
2 | |
3 | // This file is part of Moodle - http://moodle.org/ | |
4 | // | |
5 | // Moodle is free software: you can redistribute it and/or modify | |
6 | // it under the terms of the GNU General Public License as published by | |
7 | // the Free Software Foundation, either version 3 of the License, or | |
8 | // (at your option) any later version. | |
9 | // | |
10 | // Moodle is distributed in the hope that it will be useful, | |
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | // GNU General Public License for more details. | |
14 | // | |
15 | // You should have received a copy of the GNU General Public License | |
16 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
17 | ||
a62542e0 | 18 | /** |
e35194be | 19 | * A repository plugin to allow user uploading files |
a62542e0 | 20 | * |
10d53fd3 DC |
21 | * @since 2.0 |
22 | * @package moodlecore | |
23 | * @subpackage repository | |
24 | * @copyright 2009 Dongsheng Cai | |
25 | * @author Dongsheng Cai <dongsheng@moodle.com> | |
e35194be | 26 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
a62542e0 | 27 | */ |
28 | ||
29 | class repository_upload extends repository { | |
e35194be | 30 | private $mimetypes = array(); |
a62542e0 | 31 | |
96297ca2 | 32 | /** |
f3b38030 | 33 | * Print a upload form |
f3b38030 | 34 | * @return array |
96297ca2 | 35 | */ |
f3b38030 | 36 | public function print_login() { |
a62542e0 | 37 | return $this->get_listing(); |
38 | } | |
39 | ||
f3b38030 DC |
40 | /** |
41 | * Process uploaded file | |
e35194be | 42 | * @return array|bool |
f3b38030 | 43 | */ |
7d9cb3b5 | 44 | public function upload() { |
e35194be | 45 | global $USER, $CFG; |
900d4606 | 46 | |
e35194be DC |
47 | $types = optional_param('accepted_types', '*', PARAM_RAW); |
48 | if ((is_array($types) and in_array('*', $types)) or $types == '*') { | |
49 | $this->mimetypes = '*'; | |
50 | } else { | |
51 | foreach ($types as $type) { | |
52 | $this->mimetypes[] = mimeinfo('type', $type); | |
53 | } | |
900d4606 | 54 | } |
7d9cb3b5 | 55 | |
e35194be DC |
56 | $record = new stdclass; |
57 | $record->filearea = 'draft'; | |
58 | $record->component = 'user'; | |
59 | $record->filepath = urldecode(optional_param('savepath', '/', PARAM_PATH)); | |
60 | $record->itemid = optional_param('itemid', 0, PARAM_INT); | |
61 | $record->license = optional_param('license', $CFG->sitedefaultlicense, PARAM_TEXT); | |
62 | $record->author = optional_param('author', '', PARAM_TEXT); | |
7d9cb3b5 | 63 | |
1dce6261 | 64 | $context = get_context_instance(CONTEXT_USER, $USER->id); |
e35194be | 65 | $elname = 'repo_upload_file'; |
64f93798 | 66 | |
1dce6261 | 67 | $fs = get_file_storage(); |
e35194be | 68 | $browser = get_file_browser(); |
7d9cb3b5 | 69 | |
1dce6261 | 70 | if ($record->filepath !== '/') { |
e35194be | 71 | $record->filepath = file_correct_filepath($record->filepath); |
7d9cb3b5 DC |
72 | } |
73 | ||
74 | if (!isset($_FILES[$elname])) { | |
75 | throw new moodle_exception('nofile'); | |
76 | } | |
77 | ||
78 | if (!empty($_FILES[$elname]['error'])) { | |
79 | throw new moodle_exception('maxbytes'); | |
80 | } | |
81 | ||
1dce6261 DC |
82 | if (empty($record->filename)) { |
83 | $record->filename = $_FILES[$elname]['name']; | |
7d9cb3b5 DC |
84 | } |
85 | ||
e35194be DC |
86 | if ($this->mimetypes != '*') { |
87 | // check filetype | |
88 | if (!in_array(mimeinfo('type', $_FILES[$elname]['name']), $this->mimetypes)) { | |
89 | throw new moodle_exception('invalidfiletype', 'repository', '', mimeinfo('type', $_FILES[$elname]['name'])); | |
90 | } | |
91 | } | |
92 | ||
93 | $userquota = file_get_user_used_space(); | |
94 | if (filesize($_FILES[$elname]['tmp_name'])+$userquota>=(int)$CFG->userquota) { | |
95 | throw new file_exception('userquotalimit'); | |
96 | } | |
97 | ||
1dce6261 DC |
98 | if (empty($record->itemid)) { |
99 | $record->itemid = 0; | |
7d9cb3b5 | 100 | } |
7d9cb3b5 | 101 | |
e35194be DC |
102 | if ($file = $browser->get_file_info($context, $record->filearea, $record->itemid, $record->filepath, $record->filename)) { |
103 | $file->delete(); | |
104 | //throw new moodle_exception('fileexist'); | |
7d9cb3b5 DC |
105 | } |
106 | ||
1dce6261 DC |
107 | $record->contextid = $context->id; |
108 | $record->userid = $USER->id; | |
109 | $record->source = ''; | |
7d9cb3b5 | 110 | |
e35194be | 111 | $stored_file = $fs->create_file_from_pathname($record, $_FILES[$elname]['tmp_name']); |
1dce6261 | 112 | |
7d9cb3b5 | 113 | return array( |
e35194be | 114 | 'url'=>file_draftfile_url($record->itemid, $record->filepath, $record->filename), |
1dce6261 | 115 | 'id'=>$record->itemid, |
e35194be DC |
116 | 'file'=>$record->filename); |
117 | } | |
118 | ||
119 | /** | |
120 | * Return a upload form | |
121 | * @return array | |
122 | */ | |
123 | public function get_listing() { | |
124 | global $CFG; | |
125 | $ret = array(); | |
126 | $ret['nologin'] = true; | |
127 | $ret['nosearch'] = true; | |
128 | $ret['norefresh'] = true; | |
129 | $ret['list'] = array(); | |
130 | $ret['dynload'] = false; | |
131 | $ret['upload'] = array('label'=>get_string('attachment', 'repository'), 'id'=>'repo-form'); | |
132 | return $ret; | |
133 | } | |
134 | ||
135 | /** | |
136 | * Define the readable name of this repository | |
137 | * @return string | |
138 | */ | |
139 | public function get_name(){ | |
140 | return get_string('pluginname', 'repository_upload'); | |
141 | } | |
142 | ||
143 | /** | |
144 | * supported return types | |
145 | * @return int | |
146 | */ | |
147 | public function supported_returntypes() { | |
148 | return FILE_INTERNAL; | |
149 | } | |
150 | ||
151 | /** | |
152 | * Upload file to local filesystem pool | |
153 | * @param string $elname name of element | |
154 | * @param string $filearea | |
155 | * @param string $filepath | |
156 | * @param string $filename - use specified filename, if not specified name of uploaded file used | |
157 | * @param bool $override override file if exists | |
158 | * @return mixed stored_file object or false if error; may throw exception if duplicate found | |
159 | */ | |
160 | public function upload_to_filepool($elname, $record, $override = true) { | |
7d9cb3b5 | 161 | } |
a62542e0 | 162 | } |