|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00032 define('AJAX_SCRIPT', true); 00033 define('NO_MOODLE_COOKIES', true); 00034 require_once(dirname(dirname(__FILE__)) . '/config.php'); 00035 require_once($CFG->dirroot . '/webservice/lib.php'); 00036 $filepath = optional_param('filepath', '/', PARAM_PATH); 00037 00038 echo $OUTPUT->header(); 00039 00040 //authenticate the user 00041 $token = required_param('token', PARAM_ALPHANUM); 00042 $webservicelib = new webservice(); 00043 $authenticationinfo = $webservicelib->authenticate_user($token); 00044 00045 //check the user can manage his own files (can upload) 00046 $context = get_context_instance(CONTEXT_USER, $USER->id); 00047 require_capability('moodle/user:manageownfiles', $context); 00048 00049 $fs = get_file_storage(); 00050 00051 $totalsize = 0; 00052 $files = array(); 00053 foreach ($_FILES as $fieldname=>$uploaded_file) { 00054 // check upload errors 00055 if (!empty($_FILES[$fieldname]['error'])) { 00056 switch ($_FILES[$fieldname]['error']) { 00057 case UPLOAD_ERR_INI_SIZE: 00058 throw new moodle_exception('upload_error_ini_size', 'repository_upload'); 00059 break; 00060 case UPLOAD_ERR_FORM_SIZE: 00061 throw new moodle_exception('upload_error_form_size', 'repository_upload'); 00062 break; 00063 case UPLOAD_ERR_PARTIAL: 00064 throw new moodle_exception('upload_error_partial', 'repository_upload'); 00065 break; 00066 case UPLOAD_ERR_NO_FILE: 00067 throw new moodle_exception('upload_error_no_file', 'repository_upload'); 00068 break; 00069 case UPLOAD_ERR_NO_TMP_DIR: 00070 throw new moodle_exception('upload_error_no_tmp_dir', 'repository_upload'); 00071 break; 00072 case UPLOAD_ERR_CANT_WRITE: 00073 throw new moodle_exception('upload_error_cant_write', 'repository_upload'); 00074 break; 00075 case UPLOAD_ERR_EXTENSION: 00076 throw new moodle_exception('upload_error_extension', 'repository_upload'); 00077 break; 00078 default: 00079 throw new moodle_exception('nofile'); 00080 } 00081 } 00082 $file = new stdClass(); 00083 $file->filename = clean_param($_FILES[$fieldname]['name'], PARAM_FILE); 00084 // check system maxbytes setting 00085 if (($_FILES[$fieldname]['size'] > get_max_upload_file_size($CFG->maxbytes))) { 00086 // oversize file will be ignored, error added to array to notify 00087 // web service client 00088 $file->errortype = 'fileoversized'; 00089 $file->error = get_string('maxbytes', 'error'); 00090 } else { 00091 $file->filepath = $_FILES[$fieldname]['tmp_name']; 00092 // calculate total size of upload 00093 $totalsize += $_FILES[$fieldname]['size']; 00094 } 00095 $files[] = $file; 00096 } 00097 00098 $fs = get_file_storage(); 00099 00100 $usedspace = 0; 00101 $privatefiles = $fs->get_area_files($context->id, 'user', 'private', false, 'id', false); 00102 foreach ($privatefiles as $file) { 00103 $usedspace += $file->get_filesize(); 00104 } 00105 00106 if ($totalsize > ($CFG->userquota - $usedspace)) { 00107 throw new file_exception('userquotalimit'); 00108 } 00109 00110 $results = array(); 00111 foreach ($files as $file) { 00112 if (!empty($file->error)) { 00113 // including error and filename 00114 $results[] = $file; 00115 continue; 00116 } 00117 $file_record = new stdClass; 00118 $file_record->component = 'user'; 00119 $file_record->contextid = $context->id; 00120 $file_record->userid = $USER->id; 00121 $file_record->filearea = 'private'; 00122 $file_record->filename = $file->filename; 00123 $file_record->filepath = $filepath; 00124 $file_record->itemid = 0; 00125 $file_record->license = $CFG->sitedefaultlicense; 00126 $file_record->author = fullname($authenticationinfo['user']);; 00127 $file_record->source = ''; 00128 00129 //Check if the file already exist 00130 $existingfile = $fs->file_exists($file_record->contextid, $file_record->component, $file_record->filearea, 00131 $file_record->itemid, $file_record->filepath, $file_record->filename); 00132 if ($existingfile) { 00133 $file->errortype = 'filenameexist'; 00134 $file->error = get_string('filenameexist', 'webservice', $file->filename); 00135 $results[] = $file; 00136 } else { 00137 $stored_file = $fs->create_file_from_pathname($file_record, $file->filepath); 00138 $results[] = $file_record; 00139 } 00140 } 00141 echo json_encode($results);