|
Moodle
2.2.1
http://www.collinsharper.com
|
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 00027 require_once("$CFG->libdir/externallib.php"); 00028 require_once("$CFG->libdir/filelib.php"); 00029 00033 class core_files_external extends external_api { 00034 00039 public static function get_files_parameters() { 00040 return new external_function_parameters( 00041 array( 00042 'contextid' => new external_value(PARAM_INT, 'context id'), 00043 'component' => new external_value(PARAM_TEXT, 'component'), 00044 'filearea' => new external_value(PARAM_TEXT, 'file area'), 00045 'itemid' => new external_value(PARAM_INT, 'associated id'), 00046 'filepath' => new external_value(PARAM_PATH, 'file path'), 00047 'filename' => new external_value(PARAM_FILE, 'file name') 00048 ) 00049 ); 00050 } 00051 00062 public static function get_files($contextid, $component, $filearea, $itemid, $filepath, $filename) { 00063 global $CFG, $USER, $OUTPUT; 00064 $fileinfo = self::validate_parameters(self::get_files_parameters(), array('contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea, 'itemid'=>$itemid, 'filepath'=>$filepath, 'filename'=>$filename)); 00065 00066 $browser = get_file_browser(); 00067 00068 if (empty($fileinfo['contextid'])) { 00069 $context = get_system_context(); 00070 } else { 00071 $context = get_context_instance_by_id($fileinfo['contextid']); 00072 } 00073 if (empty($fileinfo['component'])) { 00074 $fileinfo['component'] = null; 00075 } 00076 if (empty($fileinfo['filearea'])) { 00077 $fileinfo['filearea'] = null; 00078 } 00079 if (empty($fileinfo['itemid'])) { 00080 $fileinfo['itemid'] = null; 00081 } 00082 if (empty($fileinfo['filename'])) { 00083 $fileinfo['filename'] = null; 00084 } 00085 if (empty($fileinfo['filepath'])) { 00086 $fileinfo['filepath'] = null; 00087 } 00088 00089 $return = array(); 00090 $return['parents'] = array(); 00091 $return['files'] = array(); 00092 if ($file = $browser->get_file_info($context, $fileinfo['component'], $fileinfo['filearea'], $fileinfo['itemid'], $fileinfo['filepath'], $fileinfo['filename'])) { 00093 $level = $file->get_parent(); 00094 while ($level) { 00095 $params = $level->get_params(); 00096 $params['filename'] = $level->get_visible_name(); 00097 array_unshift($return['parents'], $params); 00098 $level = $level->get_parent(); 00099 } 00100 $list = array(); 00101 $children = $file->get_children(); 00102 foreach ($children as $child) { 00103 00104 $params = $child->get_params(); 00105 00106 if ($child->is_directory()) { 00107 $node = array( 00108 'contextid' => $params['contextid'], 00109 'component' => $params['component'], 00110 'filearea' => $params['filearea'], 00111 'itemid' => $params['itemid'], 00112 'filepath' => $params['filepath'], 00113 'filename' => $child->get_visible_name(), 00114 'url' => null, 00115 'isdir' => true 00116 ); 00117 $list[] = $node; 00118 } else { 00119 $node = array( 00120 'contextid' => $params['contextid'], 00121 'component' => $params['component'], 00122 'filearea' => $params['filearea'], 00123 'itemid' => $params['itemid'], 00124 'filepath' => $params['filepath'], 00125 'filename' => $child->get_visible_name(), 00126 'url' => $child->get_url(), 00127 'isdir' => false 00128 ); 00129 $list[] = $node; 00130 } 00131 } 00132 } 00133 $return['files'] = $list; 00134 return $return; 00135 } 00136 00141 public static function get_files_returns() { 00142 return new external_single_structure( 00143 array( 00144 'parents' => new external_multiple_structure( 00145 new external_single_structure( 00146 array( 00147 'contextid' => new external_value(PARAM_INT, ''), 00148 'component' => new external_value(PARAM_COMPONENT, ''), 00149 'filearea' => new external_value(PARAM_AREA, ''), 00150 'itemid' => new external_value(PARAM_INT, ''), 00151 'filepath' => new external_value(PARAM_TEXT, ''), 00152 'filename' => new external_value(PARAM_TEXT, ''), 00153 ) 00154 ) 00155 ), 00156 'files' => new external_multiple_structure( 00157 new external_single_structure( 00158 array( 00159 'contextid' => new external_value(PARAM_INT, ''), 00160 'component' => new external_value(PARAM_COMPONENT, ''), 00161 'filearea' => new external_value(PARAM_AREA, ''), 00162 'itemid' => new external_value(PARAM_INT, ''), 00163 'filepath' => new external_value(PARAM_TEXT, ''), 00164 'filename' => new external_value(PARAM_FILE, ''), 00165 'isdir' => new external_value(PARAM_BOOL, ''), 00166 'url' => new external_value(PARAM_TEXT, ''), 00167 ) 00168 ) 00169 ) 00170 ) 00171 ); 00172 } 00173 00178 public static function upload_parameters() { 00179 return new external_function_parameters( 00180 array( 00181 'contextid' => new external_value(PARAM_INT, 'context id'), 00182 'component' => new external_value(PARAM_COMPONENT, 'component'), 00183 'filearea' => new external_value(PARAM_AREA, 'file area'), 00184 'itemid' => new external_value(PARAM_INT, 'associated id'), 00185 'filepath' => new external_value(PARAM_PATH, 'file path'), 00186 'filename' => new external_value(PARAM_FILE, 'file name'), 00187 'filecontent' => new external_value(PARAM_TEXT, 'file content') 00188 ) 00189 ); 00190 } 00191 00204 public static function upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent) { 00205 global $USER, $CFG; 00206 00207 $fileinfo = self::validate_parameters(self::upload_parameters(), array('contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea, 'itemid'=>$itemid, 'filepath'=>$filepath, 'filename'=>$filename, 'filecontent'=>$filecontent)); 00208 00209 if (!isset($fileinfo['filecontent'])) { 00210 throw new moodle_exception('nofile'); 00211 } 00212 // saving file 00213 $dir = make_temp_directory('wsupload'); 00214 00215 if (empty($fileinfo['filename'])) { 00216 $filename = uniqid('wsupload', true).'_'.time().'.tmp'; 00217 } else { 00218 $filename = $fileinfo['filename']; 00219 } 00220 00221 if (file_exists($dir.$filename)) { 00222 $savedfilepath = $dir.uniqid('m').$filename; 00223 } else { 00224 $savedfilepath = $dir.$filename; 00225 } 00226 00227 00228 file_put_contents($savedfilepath, base64_decode($fileinfo['filecontent'])); 00229 unset($fileinfo['filecontent']); 00230 00231 if (!empty($fileinfo['filepath'])) { 00232 $filepath = $fileinfo['filepath']; 00233 } else { 00234 $filepath = '/'; 00235 } 00236 00237 if (isset($fileinfo['itemid'])) { 00238 // TODO: in user private area, itemid is always 0 00239 $itemid = 0; 00240 } else { 00241 throw new coding_exception('itemid cannot be empty'); 00242 } 00243 00244 if (!empty($fileinfo['contextid'])) { 00245 $context = get_context_instance_by_id($fileinfo['contextid']); 00246 } else { 00247 $context = get_system_context(); 00248 } 00249 00250 if (!($fileinfo['component'] == 'user' and $fileinfo['filearea'] == 'private')) { 00251 throw new coding_exception('File can be uploaded to user private area only'); 00252 } else { 00253 // TODO: hard-coded to use user_private area 00254 $component = 'user'; 00255 $filearea = 'private'; 00256 } 00257 00258 $browser = get_file_browser(); 00259 00260 // check existing file 00261 if ($file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) { 00262 throw new moodle_exception('fileexist'); 00263 } 00264 00265 // move file to filepool 00266 if ($dir = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, '.')) { 00267 $info = $dir->create_file_from_pathname($filename, $savedfilepath); 00268 $params = $info->get_params(); 00269 unlink($savedfilepath); 00270 return array( 00271 'contextid'=>$params['contextid'], 00272 'component'=>$params['component'], 00273 'filearea'=>$params['filearea'], 00274 'itemid'=>$params['itemid'], 00275 'filepath'=>$params['filepath'], 00276 'filename'=>$params['filename'], 00277 'url'=>$info->get_url() 00278 ); 00279 } else { 00280 throw new moodle_exception('nofile'); 00281 } 00282 } 00283 00288 public static function upload_returns() { 00289 return new external_single_structure( 00290 array( 00291 'contextid' => new external_value(PARAM_INT, ''), 00292 'component' => new external_value(PARAM_COMPONENT, ''), 00293 'filearea' => new external_value(PARAM_AREA, ''), 00294 'itemid' => new external_value(PARAM_INT, ''), 00295 'filepath' => new external_value(PARAM_TEXT, ''), 00296 'filename' => new external_value(PARAM_FILE, ''), 00297 'url' => new external_value(PARAM_TEXT, ''), 00298 ) 00299 ); 00300 } 00301 } 00302 00307 class moodle_file_external extends external_api { 00308 00314 public static function get_files_parameters() { 00315 return core_files_external::get_files_parameters(); 00316 } 00317 00329 public static function get_files($contextid, $component, $filearea, $itemid, $filepath, $filename) { 00330 return core_files_external::get_files($contextid, $component, $filearea, $itemid, $filepath, $filename); 00331 } 00332 00338 public static function get_files_returns() { 00339 return core_files_external::get_files_returns(); 00340 } 00341 00347 public static function upload_parameters() { 00348 return core_files_external::upload_parameters(); 00349 } 00350 00363 public static function upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent) { 00364 return core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent); 00365 } 00366 00372 public static function upload_returns() { 00373 return core_files_external::upload_returns(); 00374 } 00375 }