|
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 define('AJAX_SCRIPT', true); 00028 00029 require('../config.php'); 00030 require_once($CFG->libdir.'/filelib.php'); 00031 require_once($CFG->libdir.'/adminlib.php'); 00032 $PAGE->set_context(get_system_context()); 00033 require_login(); 00034 if (isguestuser()) { 00035 print_error('noguest'); 00036 } 00037 require_sesskey(); 00038 00039 $action = required_param('action', PARAM_ALPHA); 00040 $draftid = required_param('itemid', PARAM_INT); 00041 $filepath = optional_param('filepath', '/', PARAM_PATH); 00042 00043 $user_context = get_context_instance(CONTEXT_USER, $USER->id); 00044 00045 echo $OUTPUT->header(); // send headers 00046 00047 // 00048 //NOTE TO ALL DEVELOPERS: this script must deal only with draft area of current user, it has to use only file_storage and no file_browser!! 00049 // 00050 00051 switch ($action) { 00052 case 'dir': 00053 $data = new stdClass(); 00054 file_get_drafarea_folders($draftid, $filepath, $data); 00055 echo json_encode($data); 00056 die; 00057 00058 case 'list': 00059 $filepath = optional_param('filepath', '/', PARAM_PATH); 00060 00061 $data = file_get_drafarea_files($draftid, $filepath); 00062 $info = file_get_draft_area_info($draftid); 00063 $data->filecount = $info['filecount']; 00064 $data->filesize = $info['filesize']; 00065 echo json_encode($data); 00066 die; 00067 00068 case 'mkdir': 00069 $filepath = required_param('filepath', PARAM_PATH); 00070 $newdirname = required_param('newdirname', PARAM_FILE); 00071 00072 $fs = get_file_storage(); 00073 $fs->create_directory($user_context->id, 'user', 'draft', $draftid, file_correct_filepath(file_correct_filepath($filepath).$newdirname)); 00074 $return = new stdClass(); 00075 $return->filepath = $filepath; 00076 echo json_encode($return); 00077 die; 00078 00079 case 'delete': 00080 $filename = required_param('filename', PARAM_FILE); 00081 $filepath = required_param('filepath', PARAM_PATH); 00082 00083 $fs = get_file_storage(); 00084 $filepath = file_correct_filepath($filepath); 00085 $return = new stdClass(); 00086 if ($stored_file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename)) { 00087 $parent_path = $stored_file->get_parent_directory()->get_filepath(); 00088 if ($stored_file->is_directory()) { 00089 $files = $fs->get_directory_files($user_context->id, 'user', 'draft', $draftid, $filepath, true); 00090 foreach ($files as $file) { 00091 $file->delete(); 00092 } 00093 $stored_file->delete(); 00094 $return->filepath = $parent_path; 00095 echo json_encode($return); 00096 } else { 00097 if($result = $stored_file->delete()) { 00098 $return->filepath = $parent_path; 00099 echo json_encode($return); 00100 } else { 00101 echo json_encode(false); 00102 } 00103 } 00104 } else { 00105 echo json_encode(false); 00106 } 00107 die; 00108 00109 case 'setmainfile': 00110 $filename = required_param('filename', PARAM_FILE); 00111 $filepath = required_param('filepath', PARAM_PATH); 00112 00113 $filepath = file_correct_filepath($filepath); 00114 // reset sort order 00115 file_reset_sortorder($user_context->id, 'user', 'draft', $draftid); 00116 // set main file 00117 $return = file_set_sortorder($user_context->id, 'user', 'draft', $draftid, $filepath, $filename, 1); 00118 echo json_encode($return); 00119 die; 00120 00121 case 'rename': 00122 $filename = required_param('filename', PARAM_FILE); 00123 $filepath = required_param('filepath', PARAM_PATH); 00124 $newfilename = required_param('newfilename', PARAM_FILE); 00125 00126 $fs = get_file_storage(); 00127 if ($fs->file_exists($user_context->id, 'user', 'draft', $draftid, $filepath, $newfilename)) { 00128 //bad luck, we can not rename! 00129 echo json_encode(false); 00130 } else if ($file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename)) { 00131 $return = new stdClass(); 00132 $newfile = $fs->create_file_from_storedfile(array('filename'=>$newfilename), $file); 00133 $file->delete(); 00134 $return->filepath = $newfile->get_filepath(); 00135 echo json_encode($return); 00136 } else { 00137 echo json_encode(false); 00138 } 00139 die; 00140 00141 case 'renamedir': 00142 case 'movedir': 00143 00144 $filepath = required_param('filepath', PARAM_PATH); 00145 $fs = get_file_storage(); 00146 00147 if (!$dir = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.')) { 00148 echo json_encode(false); 00149 die; 00150 } 00151 if ($action === 'renamedir') { 00152 $newdirname = required_param('newdirname', PARAM_FILE); 00153 $parent = clean_param(dirname($filepath) . '/', PARAM_PATH); 00154 $newfilepath = $parent . $newdirname . '/'; 00155 } else { 00156 $newfilepath = required_param('newfilepath', PARAM_PATH); 00157 $parts = explode('/', trim($dir->get_filepath(), '/')); 00158 $dirname = end($parts); 00159 $newfilepath = clean_param($newfilepath . '/' . $dirname . '/', PARAM_PATH); 00160 } 00161 00162 //we must update directory and all children too 00163 if ($fs->get_directory_files($user_context->id, 'user', 'draft', $draftid, $newfilepath, true)) { 00164 //bad luck, we can not rename if something already exists there 00165 echo json_encode(false); 00166 die; 00167 } 00168 00169 $xfilepath = preg_quote($filepath, '|'); 00170 00171 $files = $fs->get_area_files($user_context->id, 'user', 'draft', $draftid); 00172 $moved = array(); 00173 foreach ($files as $file) { 00174 if (!preg_match("|^$xfilepath|", $file->get_filepath())) { 00175 continue; 00176 } 00177 // move one by one 00178 $path = preg_replace("|^$xfilepath|", $newfilepath, $file->get_filepath()); 00179 $fs->create_file_from_storedfile(array('filepath'=>$path), $file); 00180 $moved[] = $file; 00181 } 00182 foreach ($moved as $file) { 00183 // delete all old 00184 $file->delete(); 00185 } 00186 00187 $return = new stdClass(); 00188 if ($action === 'renamedir') { 00189 $return->filepath = $parent; 00190 } else { 00191 $return->filepath = $newfilepath; 00192 } 00193 echo json_encode($return); 00194 die; 00195 00196 case 'movefile': 00197 $filename = required_param('filename', PARAM_FILE); 00198 $filepath = required_param('filepath', PARAM_PATH); 00199 $newfilepath = required_param('newfilepath', PARAM_PATH); 00200 00201 $fs = get_file_storage(); 00202 if ($fs->file_exists($user_context->id, 'user', 'draft', $draftid, $newfilepath, $filename)) { 00203 //bad luck, we can not rename! 00204 echo json_encode(false); 00205 } else if ($file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename)) { 00206 $return = new stdClass(); 00207 $newfile = $fs->create_file_from_storedfile(array('filepath'=>$newfilepath), $file); 00208 $file->delete(); 00209 $return->filepath = $newfile->get_filepath(); 00210 echo json_encode($return); 00211 } else { 00212 echo json_encode(false); 00213 } 00214 die; 00215 00216 case 'zip': 00217 $filepath = required_param('filepath', PARAM_PATH); 00218 00219 $zipper = get_file_packer('application/zip'); 00220 $fs = get_file_storage(); 00221 00222 $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.'); 00223 00224 $parent_path = $file->get_parent_directory()->get_filepath(); 00225 00226 if ($newfile = $zipper->archive_to_storage(array($file), $user_context->id, 'user', 'draft', $draftid, $parent_path, $filepath.'.zip', $USER->id)) { 00227 $return = new stdClass(); 00228 $return->filepath = $parent_path; 00229 echo json_encode($return); 00230 } else { 00231 echo json_encode(false); 00232 } 00233 die; 00234 00235 case 'downloaddir': 00236 $filepath = required_param('filepath', PARAM_PATH); 00237 00238 $zipper = get_file_packer('application/zip'); 00239 $fs = get_file_storage(); 00240 $area = file_get_draft_area_info($draftid); 00241 if ($area['filecount'] == 0) { 00242 echo json_encode(false); 00243 die; 00244 } 00245 00246 $stored_file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.'); 00247 if ($filepath === '/') { 00248 $parent_path = '/'; 00249 $filename = get_string('files').'.zip'; 00250 } else { 00251 $parent_path = $stored_file->get_parent_directory()->get_filepath(); 00252 $filename = trim($filepath, '/').'.zip'; 00253 } 00254 00255 // archive compressed file to an unused draft area 00256 $newdraftitemid = file_get_unused_draft_itemid(); 00257 if ($newfile = $zipper->archive_to_storage(array($stored_file), $user_context->id, 'user', 'draft', $newdraftitemid, '/', $filename, $USER->id)) { 00258 $return = new stdClass(); 00259 $return->fileurl = moodle_url::make_draftfile_url($newdraftitemid, '/', $filename)->out(); 00260 $return->filepath = $parent_path; 00261 echo json_encode($return); 00262 } else { 00263 echo json_encode(false); 00264 } 00265 die; 00266 00267 case 'unzip': 00268 $filename = required_param('filename', PARAM_FILE); 00269 $filepath = required_param('filepath', PARAM_PATH); 00270 00271 $zipper = get_file_packer('application/zip'); 00272 00273 $fs = get_file_storage(); 00274 00275 $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename); 00276 00277 if ($newfile = $file->extract_to_storage($zipper, $user_context->id, 'user', 'draft', $draftid, $filepath, $USER->id)) { 00278 $return = new stdClass(); 00279 $return->filepath = $filepath; 00280 echo json_encode($return); 00281 } else { 00282 echo json_encode(false); 00283 } 00284 die; 00285 00286 default: 00287 // no/unknown action? 00288 echo json_encode(false); 00289 die; 00290 }