Forgot the README
[moodle.git] / repository / ws.php
CommitLineData
0eb58cf4 1<?php
1e28c767 2set_time_limit(0);
0e8ae38e 3header("Cache-Control: no-cache, must-revalidate");
4header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
0eb58cf4 5require_once('../config.php');
6cbb4efb 6require_once('../lib/filelib.php');
0eb58cf4 7require_once('lib.php');
55b4bb1d 8// page or path
b6558c3b 9$p = optional_param('p', '', PARAM_INT);
3e515a9f 10// opened in editor or moodleform
b6558c3b 11$env = optional_param('env', 'form', PARAM_ALPHA);
3e515a9f 12// file to download
64be2d6c 13$file = optional_param('file', '', PARAM_RAW);
3e515a9f 14// rename the file name
b6558c3b 15$title = optional_param('title', '', PARAM_FILE);
16$action = optional_param('action', '', PARAM_ALPHA);
8988c36b 17$callback = optional_param('callback', '', PARAM_CLEANHTML);
cf493e89 18// repository ID
3570711a 19$repo_id = optional_param('repo_id', 1, PARAM_INT);
f3a6f85b 20$ctx_id = optional_param('ctx_id', SITEID, PARAM_INT);
f3a6f85b 21$userid = $USER->id;
5bce5972 22
cf493e89 23// check context id
455860ce 24if (!repository_check_context($ctx_id)) {
25 $err = new stdclass;
26 $err->e = get_string('nopermissiontoaccess', 'repository');
27 die(json_encode($err));
28}
29
cf493e89 30/**
31 * walk an array to attach repository ID
32 */
d05ef4a3 33function attach_repository_id(&$value, $key, $id){
34 $value['repo_id'] = $id;
35}
cf493e89 36
37/**
38 * these actions are requested without repository ID
39 */
40switch ($action) {
41case 'gsearch':
42 // global search
455860ce 43 $repos = repository_get_instances(array(get_context_instance_by_id($ctx_id), get_system_context()));
44 $list = array();
45 foreach($repos as $repo){
46 if ($repo->global_search()) {
47 try {
48 $ret = $repo->get_listing(null, $search);
d05ef4a3 49 array_walk($ret['list'], 'attach_repository_id', $repo->id);
50 $tmp = array_merge($list, $ret['list']);
455860ce 51 $list = $tmp;
52 } catch (repository_exception $e) {
53 $err = new stdclass;
54 $err->e = $e->getMessage();
55 die(json_encode($err));
56 }
57 }
58 }
59 die(json_encode(array('list'=>$list)));
cf493e89 60 break;
61case 'ccache':
62 //clean cache
aae85978 63 $cache = new curl_cache;
64 $cache->refresh();
7dfb3c1c 65 $action = 'list';
cf493e89 66 break;
aae85978 67}
455860ce 68
cf493e89 69// Get repository instance information
b3276c45 70$sql = 'SELECT i.name, i.typeid, r.type FROM {repository} r, {repository_instances} i WHERE i.id='.$repo_id.' AND i.typeid=r.id';
5a3b9db9 71if(!$repository = $DB->get_record_sql($sql)) {
bf1fccf0 72 $err = new stdclass;
73 $err->e = get_string('invalidrepositoryid', 'repository');
74 die(json_encode($err));
5a3b9db9 75} else {
76 $type = $repository->type;
0eb58cf4 77}
78
bf1fccf0 79if(file_exists($CFG->dirroot.'/repository/'.
5a3b9db9 80 $type.'/repository.class.php'))
bf1fccf0 81{
82 require_once($CFG->dirroot.'/repository/'.
5a3b9db9 83 $type.'/repository.class.php');
84 $classname = 'repository_' . $type;
bf1fccf0 85 try{
b3276c45 86 $repo = new $classname($repo_id, $ctx_id, array('ajax'=>true, 'name'=>$repository->name));
bf1fccf0 87 } catch (repository_exception $e){
88 $err = new stdclass;
89 $err->e = $e->getMessage();
3570711a 90 die(json_encode($err));
bf1fccf0 91 }
0eb58cf4 92} else {
bf1fccf0 93 $err = new stdclass;
94 $err->e = get_string('invalidplugin', 'repository');
95 die(json_encode($err));
0eb58cf4 96}
97
8988c36b 98if (!empty($callback)) {
99 // call opener window to refresh repository
100 // the callback url should be something like this:
101 // http://xx.moodle.com/repository/ws.php?callback=yes&repo_id=1&sid=xxx
102 // sid is the attached auth token from external source
103 $js =<<<EOD
104<html><head><script type="text/javascript">
105window.opener.repository_callback($repo_id);
106window.close();
107</script><body></body></html>
108EOD;
109 echo $js;
110 die;
111}
112
cf493e89 113switch ($action) {
2b9feb5f 114case 'searchform':
115 $repo->print_search();
116 break;
cf493e89 117case 'login':
118 try {
119 echo json_encode($repo->print_login());
120 } catch (repository_exception $e){
121 $err = new stdclass;
122 $err->e = $e->getMessage();
123 die(json_encode($err));
124 }
125 break;
cf493e89 126case 'search':
fbd508b4 127 try {
128 echo json_encode($repo->search());
129 } catch (repository_exception $e) {
130 $err = new stdclass;
131 $err->e = $e->getMessage();
132 die(json_encode($err));
133 }
134 break;
135case 'list':
bf1fccf0 136 try {
137 if(!empty($p)) {
138 echo json_encode($repo->get_listing($p));
bf1fccf0 139 } else {
140 echo json_encode($repo->get_listing());
141 }
142 } catch (repository_exception $e) {
143 $err = new stdclass;
144 $err->e = $e->getMessage();
145 die(json_encode($err));
5bce5972 146 }
cf493e89 147 break;
148case 'download':
d8eb6e18 149 $path = $repo->get_file($file, $title);
64be2d6c 150 $itemid = (int)substr(hexdec(uniqid()), 0, 9)+rand(1,100);
bf1fccf0 151 try {
49a1ce19 152 $info = repository_move_to_filepool($path, $title, $itemid);
d8eb6e18 153 if($env == 'form'){
c0fa8cba 154 echo json_encode($info);
d8eb6e18 155 } elseif($env == 'editor') {
c0fa8cba 156 echo json_encode($info);
d8eb6e18 157 } else {
6cbb4efb 158 }
bf1fccf0 159 } catch (repository_exception $e){
160 $err = new stdclass;
161 $err->e = $e->getMessage();
162 die(json_encode($err));
c2762f06 163 } catch (Exception $e) {
164 $err = new stdclass;
165 $err->e = $e->getMessage();
166 die(json_encode($err));
bf1fccf0 167 }
cf493e89 168 break;
169case 'upload':
5a3b9db9 170 try {
171 echo json_encode($repo->get_listing());
172 } catch (repository_exception $e){
173 $err = new stdclass;
174 $err->e = $e->getMessage();
175 die(json_encode($err));
176 }
cf493e89 177 break;
0eb58cf4 178}