Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/repository/repository_ajax.php
Go to the documentation of this file.
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 
00018 
00029 define('AJAX_SCRIPT', true);
00030 
00031 require_once(dirname(dirname(__FILE__)).'/config.php');
00032 require_once(dirname(dirname(__FILE__)).'/lib/filelib.php');
00033 require_once(dirname(__FILE__).'/lib.php');
00034 
00035 $err = new stdClass();
00036 
00038 $action    = optional_param('action', '', PARAM_ALPHA);
00039 $repo_id   = optional_param('repo_id', 0, PARAM_INT);           // Repository ID
00040 $contextid = optional_param('ctx_id', SYSCONTEXTID, PARAM_INT); // Context ID
00041 $env       = optional_param('env', 'filepicker', PARAM_ALPHA);  // Opened in editor or moodleform
00042 $license   = optional_param('license', $CFG->sitedefaultlicense, PARAM_TEXT);
00043 $author    = optional_param('author', '', PARAM_TEXT);          // File author
00044 $source    = optional_param('source', '', PARAM_RAW);           // File to download
00045 $itemid    = optional_param('itemid', 0, PARAM_INT);            // Itemid
00046 $page      = optional_param('page', '', PARAM_RAW);             // Page
00047 $maxbytes  = optional_param('maxbytes', 0, PARAM_INT);          // Maxbytes
00048 $req_path  = optional_param('p', '', PARAM_RAW);                // Path
00049 $accepted_types  = optional_param_array('accepted_types', '*', PARAM_RAW);
00050 $saveas_filename = optional_param('title', '', PARAM_FILE);     // save as file name
00051 $saveas_path   = optional_param('savepath', '/', PARAM_PATH);   // save as file path
00052 $search_text   = optional_param('s', '', PARAM_CLEANHTML);
00053 $linkexternal  = optional_param('linkexternal', '', PARAM_ALPHA);
00054 
00055 list($context, $course, $cm) = get_context_info_array($contextid);
00056 require_login($course, false, $cm);
00057 $PAGE->set_context($context);
00058 
00059 echo $OUTPUT->header(); // send headers
00060 @header('Content-type: text/html; charset=utf-8');
00061 
00062 // if uploaded file is larger than post_max_size (php.ini) setting, $_POST content will lost
00063 if (empty($_POST) && !empty($action)) {
00064     $err->error = get_string('errorpostmaxsize', 'repository');
00065     die(json_encode($err));
00066 }
00067 
00068 if (!confirm_sesskey()) {
00069     $err->error = get_string('invalidsesskey');
00070     die(json_encode($err));
00071 }
00072 
00074 $sql = 'SELECT i.name, i.typeid, r.type FROM {repository} r, {repository_instances} i WHERE i.id=? AND i.typeid=r.id';
00075 
00076 if (!$repository = $DB->get_record_sql($sql, array($repo_id))) {
00077     $err->error = get_string('invalidrepositoryid', 'repository');
00078     die(json_encode($err));
00079 } else {
00080     $type = $repository->type;
00081 }
00082 
00084 repository::check_capability($contextid, $repository);
00085 
00086 $moodle_maxbytes = get_max_upload_file_size();
00087 // to prevent maxbytes greater than moodle maxbytes setting
00088 if ($maxbytes == 0 || $maxbytes>=$moodle_maxbytes) {
00089     $maxbytes = $moodle_maxbytes;
00090 }
00091 
00093 set_time_limit(0);
00094 
00095 // Early actions which need to be done before repository instances initialised
00096 switch ($action) {
00097     // global search
00098     case 'gsearch':
00099         $params = array();
00100         $params['context'] = array(get_context_instance_by_id($contextid), get_system_context());
00101         $params['currentcontext'] = get_context_instance_by_id($contextid);
00102         $repos = repository::get_instances($params);
00103         $list = array();
00104         foreach($repos as $repo){
00105             if ($repo->global_search()) {
00106                 $ret = $repo->search($search_text);
00107                 array_walk($ret['list'], 'repository_attach_id', $repo->id);  // See function below
00108                 $tmp = array_merge($list, $ret['list']);
00109                 $list = $tmp;
00110             }
00111         }
00112         $listing = array('list'=>$list);
00113         $listing['gsearch'] = true;
00114         die(json_encode($listing));
00115         break;
00116 
00117     // remove the cache files & logout
00118     case 'ccache':
00119         $cache = new curl_cache;
00120         $cache->refresh();
00121         $action = 'list';
00122         break;
00123 }
00124 
00125 if (file_exists($CFG->dirroot.'/repository/'.$type.'/lib.php')) {
00126     require_once($CFG->dirroot.'/repository/'.$type.'/lib.php');
00127     $classname = 'repository_' . $type;
00128     $repo = new $classname($repo_id, $contextid, array('ajax'=>true, 'name'=>$repository->name, 'type'=>$type));
00129 } else {
00130     $err->error = get_string('invalidplugin', 'repository', $type);
00131     die(json_encode($err));
00132 }
00133 
00135 switch ($action) {
00136     case 'sign':
00137     case 'signin':
00138     case 'list':
00139         if ($repo->check_login()) {
00140             $listing = $repo->get_listing($req_path, $page);
00141             $listing['repo_id'] = $repo_id;
00142             echo json_encode($listing);
00143             break;
00144         } else {
00145             $action = 'login';
00146         }
00147     case 'login':
00148         $listing = $repo->print_login();
00149         $listing['repo_id'] = $repo_id;
00150         echo json_encode($listing);
00151         break;
00152     case 'logout':
00153         $logout = $repo->logout();
00154         $logout['repo_id'] = $repo_id;
00155         echo json_encode($logout);
00156         break;
00157     case 'searchform':
00158         $search_form['form'] = $repo->print_search();
00159         echo json_encode($search_form);
00160         break;
00161     case 'search':
00162         $search_result = $repo->search($search_text, (int)$page);
00163         $search_result['repo_id'] = $repo_id;
00164         $search_result['issearchresult'] = true;
00165         echo json_encode($search_result);
00166         break;
00167     case 'download':
00168         // validate mimetype
00169         $mimetypes = array();
00170         if ((is_array($accepted_types) and in_array('*', $accepted_types)) or $accepted_types == '*') {
00171             $mimetypes = '*';
00172         } else {
00173             foreach ($accepted_types as $type) {
00174                 $mimetypes[] = mimeinfo('type', $type);
00175             }
00176             if (!in_array(mimeinfo('type', $saveas_filename), $mimetypes)) {
00177                 throw new moodle_exception('invalidfiletype', 'repository', '', get_string(mimeinfo('type', $saveas_filename), 'mimetypes'));
00178             }
00179         }
00180 
00181         // We have two special repository type need to deal with
00182         // local and recent plugins don't added new files to moodle, just add new records to database
00183         // so we don't check user quota and maxbytes here
00184         $allowexternallink = (int)get_config(null, 'repositoryallowexternallinks');
00185         if (!empty($allowexternallink)) {
00186             $allowexternallink = true;
00187         } else {
00188             $allowexternallink = false;
00189         }
00190         // allow external links in url element all the time
00191         $allowexternallink = ($allowexternallink || ($env == 'url'));
00192 
00193         // Use link of the files
00194         if ($allowexternallink and $linkexternal === 'yes' and ($repo->supported_returntypes() & FILE_EXTERNAL)) {
00195             // use external link
00196             $link = $repo->get_link($source);
00197             $info = array();
00198             $info['file'] = $saveas_filename;
00199             $info['type'] = 'link';
00200             $info['url'] = $link;
00201             echo json_encode($info);
00202             die;
00203         } else {
00204             // some repository plugins deal with moodle internal files, so we cannot use get_file
00205             // method, so we use copy_to_area method
00206             // (local, user, coursefiles, recent)
00207             if ($repo->has_moodle_files()) {
00208                 // check filesize against max allowed size
00209                 $filesize = $repo->get_file_size($source);
00210                 if (empty($filesize)) {
00211                     $err->error = get_string('filesizenull', 'repository');
00212                     die(json_encode($err));
00213                 }
00214                 if (($maxbytes !== -1) && ($filesize > $maxbytes)) {
00215                     throw new file_exception('maxbytes');
00216                 }
00217                 $fileinfo = $repo->copy_to_area($source, $itemid, $saveas_path, $saveas_filename);
00218                 if (!isset($fileinfo['event'])) {
00219                     $fileinfo['file'] = $fileinfo['title'];
00220                 }
00221                 echo json_encode($fileinfo);
00222                 die;
00223             }
00224             // Download file to moodle
00225             $file = $repo->get_file($source, $saveas_filename);
00226             if ($file['path'] === false) {
00227                 $err->error = get_string('cannotdownload', 'repository');
00228                 die(json_encode($err));
00229             }
00230 
00231             // check if exceed maxbytes
00232             if (($maxbytes!==-1) && (filesize($file['path']) > $maxbytes)) {
00233                 throw new file_exception('maxbytes');
00234             }
00235 
00236             $record = new stdClass();
00237             $record->filepath = $saveas_path;
00238             $record->filename = $saveas_filename;
00239             $record->component = 'user';
00240             $record->filearea = 'draft';
00241             $record->itemid   = $itemid;
00242 
00243             if (!empty($file['license'])) {
00244                 $record->license  = $file['license'];
00245             } else {
00246                 $record->license  = $license;
00247             }
00248             if (!empty($file['author'])) {
00249                 $record->author   = $file['author'];
00250             } else {
00251                 $record->author   = $author;
00252             }
00253             $record->source = !empty($file['url']) ? $file['url'] : '';
00254 
00255             $info = repository::move_to_filepool($file['path'], $record);
00256             if (empty($info)) {
00257                 $info['e'] = get_string('error', 'moodle');
00258             }
00259             echo json_encode($info);
00260             die;
00261         }
00262         break;
00263     case 'upload':
00264         $result = $repo->upload($saveas_filename, $maxbytes);
00265         echo json_encode($result);
00266         break;
00267 
00268     case 'overwrite':
00269         // existing file
00270         $filepath    = required_param('existingfilepath', PARAM_PATH);
00271         $filename    = required_param('existingfilename', PARAM_FILE);
00272         // user added file which needs to replace the existing file
00273         $newfilepath = required_param('newfilepath', PARAM_PATH);
00274         $newfilename = required_param('newfilename', PARAM_FILE);
00275 
00276         $info = repository::overwrite_existing_draftfile($itemid, $filepath, $filename, $newfilepath, $newfilename);
00277         echo json_encode($info);
00278         break;
00279 
00280     case 'deletetmpfile':
00281         // delete tmp file
00282         $newfilepath = required_param('newfilepath', PARAM_PATH);
00283         $newfilename = required_param('newfilename', PARAM_FILE);
00284         echo json_encode(repository::delete_tempfile_from_draft($itemid, $newfilepath, $newfilename));
00285 
00286         break;
00287 }
 All Data Structures Namespaces Files Functions Variables Enumerations