forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws.php
263 lines (249 loc) · 10.3 KB
/
ws.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
/// The Web service script that is called from the filepicker front end
require_once('../config.php');
require_once('../lib/filelib.php');
require_once('lib.php');
require_login();
/// Parameters
$action = optional_param('action', '', PARAM_ALPHA);
$callback = optional_param('callback', '', PARAM_CLEANHTML);
$client_id = optional_param('client_id', SITEID, PARAM_RAW); // client ID
$contextid = optional_param('ctx_id', SITEID, PARAM_INT); // context ID
$env = optional_param('env', 'filepicker', PARAM_ALPHA); // opened in editor or moodleform
$file = optional_param('file', '', PARAM_RAW); // file to download
$title = optional_param('title', '', PARAM_FILE); // new file name
$itemid = optional_param('itemid', '', PARAM_INT);
$page = optional_param('page', '', PARAM_RAW); // page
$repo_id = optional_param('repo_id', 1, PARAM_INT); // repository ID
$req_path = optional_param('p', '', PARAM_RAW); // path
$save_path = optional_param('savepath', '/', PARAM_PATH);
$search_text = optional_param('s', '', PARAM_CLEANHTML);
/// Headers to make it not cacheable
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
$err = new stdclass;
$err->client_id = $client_id;
/// Check permissions
if (! (isloggedin() && repository::check_context($contextid)) ) {
$err->e = get_string('nopermissiontoaccess', 'repository');
die(json_encode($err));
}
/// Wait as long as it takes for this script to finish
set_time_limit(0);
/// Check for actions that do not need repository ID
switch ($action) {
// delete a file from filemanger
case 'delete':
try {
if (!$context = get_context_instance(CONTEXT_USER, $USER->id)) {
}
$contextid = $context->id;
$fs = get_file_storage();
if ($file = $fs->get_file($contextid, 'user_draft', $itemid, '/', $title)) {
if($result = $file->delete()) {
echo $client_id;
} else {
echo '';
}
} else {
echo '';
}
exit;
} catch (repository_exception $e) {
$err->e = $e->getMessage();
die(json_encode($err));
}
break;
case 'gsearch': // Global Search
$repos = repository::get_instances(array(get_context_instance_by_id($contextid), get_system_context()));
$list = array();
foreach($repos as $repo){
if ($repo->global_search()) {
try {
$ret = $repo->search($search_text);
array_walk($ret['list'], 'repository_attach_id', $repo->id); // See function below
$tmp = array_merge($list, $ret['list']);
$list = $tmp;
} catch (repository_exception $e) {
$err->e = $e->getMessage();
die(json_encode($err));
}
}
}
$listing = array('list'=>$list);
$listing['gsearch'] = true;
$listing['client_id'] = $client_id;
die(json_encode($listing));
break;
case 'ccache': // Clean cache
$cache = new curl_cache;
$cache->refresh();
$action = 'list';
break;
}
/// Get repository instance information
$sql = 'SELECT i.name, i.typeid, r.type FROM {repository} r, {repository_instances} i '.
'WHERE i.id=? AND i.typeid=r.id';
if (!$repository = $DB->get_record_sql($sql, array($repo_id))) {
$err->e = get_string('invalidrepositoryid', 'repository');
die(json_encode($err));
} else {
$type = $repository->type;
}
if (file_exists($CFG->dirroot.'/repository/'.$type.'/repository.class.php')) {
require_once($CFG->dirroot.'/repository/'.$type.'/repository.class.php');
$classname = 'repository_' . $type;
try {
$repo = new $classname($repo_id, $contextid, array('ajax'=>true, 'name'=>$repository->name, 'client_id'=>$client_id));
} catch (repository_exception $e){
$err->e = $e->getMessage();
die(json_encode($err));
}
} else {
$err->e = get_string('invalidplugin', 'repository', $type);
die(json_encode($err));
}
if (!empty($callback)) {
// call opener window to refresh repository
// the callback url should be something like this:
// http://xx.moodle.com/repository/ws.php?callback=yes&repo_id=1&sid=xxx
// sid is the attached auth token from external source
// If Moodle is working on HTTPS mode, then we are not allowed to access
// parent window, in this case, we need to alert user to refresh the repository
// manually.
$strhttpsbug = get_string('cannotaccessparentwin', 'repository');
$strrefreshnonjs = get_string('refreshnonjsfilepicker', 'repository');
$js =<<<EOD
<html><head>
<script type="text/javascript">
if(window.opener){
window.opener.repository_callback($repo_id);
window.close();
} else {
alert("{$strhttpsbug }");
}
</script>
<body>
<noscript>
{$strrefreshnonjs}
</noscript>
</body>
</html>
EOD;
echo $js;
die;
}
/// These actions all occur on the currently active repository instance
switch ($action) {
case 'sign':
case 'list':
if ($repo->check_login()) {
try {
$listing = $repo->get_listing($req_path, $page);
$listing['client_id'] = $client_id;
$listing['repo_id'] = $repo_id;
echo json_encode($listing);
} catch (repository_exception $e) {
$err->e = $e->getMessage();
die(json_encode($err));
}
break;
} else {
$action = 'login';
}
case 'login':
try {
$listing = $repo->print_login();
$listing['client_id'] = $client_id;
$listing['repo_id'] = $repo_id;
echo json_encode($listing);
} catch (repository_exception $e){
$err->e = $e->getMessage();
die(json_encode($err));
}
break;
case 'logout':
$logout = $repo->logout();
$logout['client_id'] = $client_id;
$logout['repo_id'] = $repo_id;
echo json_encode($logout);
break;
case 'searchform':
$search_form['form'] = $repo->print_search($client_id);
$search_form['client_id'] = $client_id;
echo json_encode($search_form);
break;
case 'search':
try {
$search_result = $repo->search($search_text);
$search_result['search_result'] = true;
$search_result['client_id'] = $client_id;
$search_result['repo_id'] = $repo_id;
echo json_encode($search_result);
} catch (repository_exception $e) {
$err->e = $e->getMessage();
die(json_encode($err));
}
break;
case 'download':
try {
// $file is the specific information of file, such as url, or meta information
// $title is the file name in file pool
// $itemid and $save_path will be used by local plugin only
if ($env == 'texturl') {
$CFG->repositoryuseexternallink = true;
}
$filepath = $repo->get_file($file, $title, $itemid, $save_path);
if ($filepath === false) {
$err->e = get_string('cannotdownload', 'repository');
die(json_encode($err));
}
if (empty($itemid)) {
$itemid = (int)substr(hexdec(uniqid()), 0, 9)+rand(1,100);
}
if (is_array($filepath)) {
// file api don't have real file path, so we need more file api specific info for "local" plugin
// only used by local plugin
$fileinfo = $filepath;
$info = array();
$info['client_id'] = $client_id;
$info['file'] = $fileinfo['title'];
$info['id'] = $itemid;
$info['url'] = $CFG->httpswwwroot.'/draftfile.php/'.$fileinfo['contextid'].'/user_draft/'.$itemid.'/'.$fileinfo['title'];
echo json_encode($info);
} else if (preg_match('#(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)#', $filepath)) {
// process external link
$url = $filepath;
echo json_encode(array('type'=>'link', 'client_id'=>$client_id, 'url'=>$url, 'id'=>$url, 'file'=>$url));
} else {
// used by most repository plugins
// move downloaded file to file pool
$info = repository::move_to_filepool($filepath, $title, $itemid, $save_path);
$info['client_id'] = $client_id;
echo json_encode($info);
}
} catch (repository_exception $e){
$err->e = $e->getMessage();
die(json_encode($err));
} catch (Exception $e) {
$err->e = $e->getMessage();
die(json_encode($err));
}
break;
case 'upload':
try {
$upload = $repo->get_listing();
$upload['client_id'] = $client_id;
echo json_encode($upload);
} catch (repository_exception $e){
$err->e = $e->getMessage();
die(json_encode($err));
}
break;
}
/**
* Small function to walk an array to attach repository ID
*/
function repository_attach_id(&$value, $key, $id){
$value['repo_id'] = $id;
}