MDL-13766
[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);
17$search = optional_param('s', '', 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
cf493e89 98switch ($action) {
99case 'login':
100 try {
101 echo json_encode($repo->print_login());
102 } catch (repository_exception $e){
103 $err = new stdclass;
104 $err->e = $e->getMessage();
105 die(json_encode($err));
106 }
107 break;
108case 'callback':
109 // http://xx.moodle.com/repository/ws.php?action=callback&repo_id=1&sid=xxx
110 // sid is the attached auth token from external source
111 $js =<<<EOD
112<html><head><script type="text/javascript">
113window.opener.repository_callback($repo_id);
114</script><body></body></html>
115EOD;
116 break;
117case 'list':
118case 'search':
bf1fccf0 119 try {
120 if(!empty($p)) {
121 echo json_encode($repo->get_listing($p));
122 } else if(!empty($search)) {
123 echo json_encode($repo->get_listing('', $search));
124 } else {
125 echo json_encode($repo->get_listing());
126 }
127 } catch (repository_exception $e) {
128 $err = new stdclass;
129 $err->e = $e->getMessage();
130 die(json_encode($err));
5bce5972 131 }
cf493e89 132 break;
133case 'download':
d8eb6e18 134 $path = $repo->get_file($file, $title);
64be2d6c 135 $itemid = (int)substr(hexdec(uniqid()), 0, 9)+rand(1,100);
bf1fccf0 136 try {
49a1ce19 137 $info = repository_move_to_filepool($path, $title, $itemid);
d8eb6e18 138 if($env == 'form'){
c0fa8cba 139 echo json_encode($info);
d8eb6e18 140 } elseif($env == 'editor') {
c0fa8cba 141 echo json_encode($info);
d8eb6e18 142 } else {
6cbb4efb 143 }
bf1fccf0 144 } catch (repository_exception $e){
145 $err = new stdclass;
146 $err->e = $e->getMessage();
147 die(json_encode($err));
c2762f06 148 } catch (Exception $e) {
149 $err = new stdclass;
150 $err->e = $e->getMessage();
151 die(json_encode($err));
bf1fccf0 152 }
cf493e89 153 break;
154case 'upload':
5a3b9db9 155 try {
156 echo json_encode($repo->get_listing());
157 } catch (repository_exception $e){
158 $err = new stdclass;
159 $err->e = $e->getMessage();
160 die(json_encode($err));
161 }
cf493e89 162 break;
0eb58cf4 163}