MDL-17457 migrated all modules to new db/install.php; added upgrade.txt file for...
[moodle.git] / repository / ws.php
CommitLineData
d0f8585d 1<?php // $Id$
5bce5972 2
d0f8585d 3/// The Web service script that is called from the filepicker front end
455860ce 4
d0f8585d 5 require_once('../config.php');
6 require_once('../lib/filelib.php');
7 require_once('lib.php');
cf493e89 8
d0f8585d 9/// Parameters
00045af9 10 $p = optional_param('p', '', PARAM_RAW); // page or path
b423b4af 11 $env = optional_param('env', 'filepicker', PARAM_ALPHA);// opened in editor or moodleform
d0f8585d 12 $file = optional_param('file', '', PARAM_RAW); // file to download
13 $title = optional_param('title', '', PARAM_FILE); // new file name
b423b4af 14 $itemid = optional_param('itemid', '', PARAM_INT);
d0f8585d 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);
353d5cf3 19 $search_text = optional_param('s', '', PARAM_CLEANHTML);
455860ce 20
577aab9b 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");
0eb58cf4 24
d0f8585d 25/// Check permissions
dbc01944 26 if (! (isloggedin() && repository::check_context($ctx_id)) ) {
bf1fccf0 27 $err = new stdclass;
d0f8585d 28 $err->e = get_string('nopermissiontoaccess', 'repository');
3570711a 29 die(json_encode($err));
bf1fccf0 30 }
0eb58cf4 31
577aab9b 32/// Wait as long as it takes for this script to finish
33 set_time_limit(0);
34
d0f8585d 35/// Check for actions that do not need repository ID
36 switch ($action) {
b423b4af 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;
d0f8585d 57 case 'gsearch': // Global Search
dbc01944 58 $repos = repository::get_instances(array(get_context_instance_by_id($ctx_id), get_system_context()));
d0f8585d 59 $list = array();
60 foreach($repos as $repo){
61 if ($repo->global_search()) {
62 try {
353d5cf3 63 $ret = $repo->search($search_text);
d0f8585d 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;
76
77 case 'ccache': // Clean cache
78 $cache = new curl_cache;
79 $cache->refresh();
80 $action = 'list';
81 break;
cf493e89 82 }
d0f8585d 83
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)) {
fbd508b4 88 $err = new stdclass;
d0f8585d 89 $err->e = get_string('invalidrepositoryid', 'repository');
fbd508b4 90 die(json_encode($err));
d0f8585d 91 } else {
92 $type = $repository->type;
fbd508b4 93 }
d0f8585d 94
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));
bf1fccf0 104 }
d0f8585d 105 } else {
bf1fccf0 106 $err = new stdclass;
d0f8585d 107 $err->e = get_string('invalidplugin', 'repository');
bf1fccf0 108 die(json_encode($err));
5bce5972 109 }
d0f8585d 110
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>
121EOD;
122 echo $js;
123 die;
bf1fccf0 124 }
d0f8585d 125
126
127/// These actions all occur on the currently active repository instance
128 switch ($action) {
d68c527f 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 }
d0f8585d 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;
d68c527f 156 case 'logout':
157 echo json_encode($repo->logout());
158 break;
159 case 'searchform':
160 $repo->print_search();
161 break;
d0f8585d 162 case 'search':
163 try {
6ecf0dfa 164 $search_result = $repo->search($search_text);
165 $search_result['search_result'] = true;
166 echo json_encode($search_result);
d0f8585d 167 } catch (repository_exception $e) {
168 $err = new stdclass;
169 $err->e = $e->getMessage();
170 die(json_encode($err));
171 }
172 break;
d0f8585d 173 case 'download':
174 $path = $repo->get_file($file, $title);
b423b4af 175 if (empty($itemid)) {
176 $itemid = (int)substr(hexdec(uniqid()), 0, 9)+rand(1,100);
177 }
d0f8585d 178 try {
122d195b 179 if (preg_match('#(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)#', $path)) {
53c8ae53 180 echo json_encode(array('url'=>$path, 'id'=>$path, 'file'=>$path));
d0f8585d 181 } else {
dbc01944 182 $info = repository::move_to_filepool($path, $title, $itemid);
b423b4af 183 if ($env == 'filepicker' || $env == 'filemanager'){
122d195b 184 echo json_encode($info);
185 } else if ($env == 'editor') {
186 echo json_encode($info);
187 }
d0f8585d 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;
5a3b9db9 208 }
d0f8585d 209
210/**
211 * Small function to walk an array to attach repository ID
212 */
213function repository_attach_id(&$value, $key, $id){
214 $value['repo_id'] = $id;
0eb58cf4 215}