MDL-17457 migrated all modules to new db/install.php; added upgrade.txt file for...
[moodle.git] / repository / ws.php
1 <?php  // $Id$
3 /// The Web service script that is called from the filepicker front end
5     require_once('../config.php');
6     require_once('../lib/filelib.php');
7     require_once('lib.php');
9 /// Parameters
10     $p     = optional_param('p', '', PARAM_RAW);              // page or path
11     $env   = optional_param('env', 'filepicker', PARAM_ALPHA);// opened in editor or moodleform
12     $file  = optional_param('file', '', PARAM_RAW);           // file to download
13     $title = optional_param('title', '', PARAM_FILE);         // new file name
14     $itemid = optional_param('itemid', '', PARAM_INT);
15     $action = optional_param('action', '', PARAM_ALPHA);
16     $ctx_id  = optional_param('ctx_id', SITEID, PARAM_INT);   // context ID
17     $repo_id = optional_param('repo_id', 1, PARAM_INT);       // repository ID
18     $callback = optional_param('callback', '', PARAM_CLEANHTML);
19     $search_text = optional_param('s', '', PARAM_CLEANHTML);
21 /// Headers to make it not cacheable
22     header("Cache-Control: no-cache, must-revalidate");
23     header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
25 /// Check permissions
26     if (! (isloggedin() && repository::check_context($ctx_id)) ) {
27         $err = new stdclass;
28         $err->e = get_string('nopermissiontoaccess', 'repository');
29         die(json_encode($err));
30     }
32 /// Wait as long as it takes for this script to finish
33     set_time_limit(0);
35 /// Check for actions that do not need repository ID
36     switch ($action) {
37         // delete a file from filemanger
38         case 'delete':
39             try {
40                 if (!$context = get_context_instance(CONTEXT_USER, $USER->id)) {
41                 }
42                 $contextid = $context->id;
43                 $fs = get_file_storage();
44                 if ($file = $fs->get_file($contextid, 'user_draft', $itemid, '/', $title)) {
45                     $file->delete();
46                     echo 200;
47                 } else {
48                     echo '';
49                 }
50                 exit;
51             } catch (repository_exception $e) {
52                 $err = new stdclass;
53                 $err->e = $e->getMessage();
54                 die(json_encode($err));
55             }
56             break;
57         case 'gsearch':     //  Global Search
58             $repos = repository::get_instances(array(get_context_instance_by_id($ctx_id), get_system_context()));
59             $list = array();
60             foreach($repos as $repo){
61                 if ($repo->global_search()) {
62                     try {
63                         $ret = $repo->search($search_text);
64                         array_walk($ret['list'], 'repository_attach_id', $repo->id);  // See function below
65                         $tmp = array_merge($list, $ret['list']);
66                         $list = $tmp;
67                     } catch (repository_exception $e) {
68                         $err = new stdclass;
69                         $err->e = $e->getMessage();
70                         die(json_encode($err));
71                     }
72                 }
73             }
74             die(json_encode(array('list'=>$list)));
75             break;
77         case 'ccache':      // Clean cache
78             $cache = new curl_cache;
79             $cache->refresh();
80             $action = 'list';
81             break;
82     }
84 /// Get repository instance information
85     $sql = 'SELECT i.name, i.typeid, r.type FROM {repository} r, {repository_instances} i '.
86            'WHERE i.id='.$repo_id.' AND i.typeid=r.id';
87     if (!$repository = $DB->get_record_sql($sql)) {
88         $err = new stdclass;
89         $err->e = get_string('invalidrepositoryid', 'repository');
90         die(json_encode($err));
91     } else {
92         $type = $repository->type;
93     }
95     if (file_exists($CFG->dirroot.'/repository/'.$type.'/repository.class.php')) {
96         require_once($CFG->dirroot.'/repository/'.$type.'/repository.class.php');
97         $classname = 'repository_' . $type;
98         try {
99             $repo = new $classname($repo_id, $ctx_id, array('ajax'=>true, 'name'=>$repository->name));
100         } catch (repository_exception $e){
101             $err = new stdclass;
102             $err->e = $e->getMessage();
103             die(json_encode($err));
104         }
105     } else {
106         $err = new stdclass;
107         $err->e = get_string('invalidplugin', 'repository');
108         die(json_encode($err));
109     }
111     if (!empty($callback)) {
112         // call opener window to refresh repository
113         // the callback url should be something like this:
114         // http://xx.moodle.com/repository/ws.php?callback=yes&repo_id=1&sid=xxx
115         // sid is the attached auth token from external source
116         $js  =<<<EOD
117 <html><head><script type="text/javascript">
118     window.opener.repository_callback($repo_id);
119     window.close();
120 </script><body></body></html>
121 EOD;
122         echo $js;
123         die;
124     }
127 /// These actions all occur on the currently active repository instance
128     switch ($action) {
129         case 'sign':
130         case 'list':
131             if ($repo->check_login()) {
132                 try {
133                     if (!empty($p)) {
134                         echo json_encode($repo->get_listing($p));
135                     } else {
136                         echo json_encode($repo->get_listing());
137                     }
138                 } catch (repository_exception $e) {
139                     $err = new stdclass;
140                     $err->e = $e->getMessage();
141                     die(json_encode($err));
142                 }
143                 break;
144             } else {
145                 $action = 'login';
146             }
147         case 'login':
148             try {
149                 echo json_encode($repo->print_login());
150             } catch (repository_exception $e){
151                 $err = new stdclass;
152                 $err->e = $e->getMessage();
153                 die(json_encode($err));
154             }
155             break;
156         case 'logout':
157             echo json_encode($repo->logout());
158             break;
159         case 'searchform':
160             $repo->print_search();
161             break;
162         case 'search':
163             try {
164                 $search_result = $repo->search($search_text);
165                 $search_result['search_result'] = true;
166                 echo json_encode($search_result);
167             } catch (repository_exception $e) {
168                 $err = new stdclass;
169                 $err->e = $e->getMessage();
170                 die(json_encode($err));
171             }
172             break;
173         case 'download':
174             $path = $repo->get_file($file, $title);
175             if (empty($itemid)) {
176                 $itemid = (int)substr(hexdec(uniqid()), 0, 9)+rand(1,100);
177             }
178             try {
179                 if (preg_match('#(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)#', $path)) {
180                     echo json_encode(array('url'=>$path, 'id'=>$path, 'file'=>$path));
181                 } else {
182                     $info = repository::move_to_filepool($path, $title, $itemid);
183                     if ($env == 'filepicker' || $env == 'filemanager'){
184                         echo json_encode($info);
185                     } else if ($env == 'editor') {
186                         echo json_encode($info);
187                     }
188                 }
189             } catch (repository_exception $e){
190                 $err = new stdclass;
191                 $err->e = $e->getMessage();
192                 die(json_encode($err));
193             } catch (Exception $e) {
194                 $err = new stdclass;
195                 $err->e = $e->getMessage();
196                 die(json_encode($err));
197             }
198             break;
199         case 'upload':
200             try {
201                 echo json_encode($repo->get_listing());
202             } catch (repository_exception $e){
203                 $err = new stdclass;
204                 $err->e = $e->getMessage();
205                 die(json_encode($err));
206             }
207             break;
208     }
210 /**
211  * Small function to walk an array to attach repository ID
212  */
213 function repository_attach_id(&$value, $key, $id){
214     $value['repo_id'] = $id;