MDL-15405: draftfile url rewriting function now working as it should for a nice bunch...
[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');
bf1fccf0 8// set one hour here
9$CFG->repository_cache_expire = 60*60;
55b4bb1d 10// page or path
b6558c3b 11$p = optional_param('p', '', PARAM_INT);
3e515a9f 12// opened in editor or moodleform
b6558c3b 13$env = optional_param('env', 'form', PARAM_ALPHA);
3e515a9f 14// file to download
64be2d6c 15// TODO: which type should be?
16$file = optional_param('file', '', PARAM_RAW);
3e515a9f 17// rename the file name
b6558c3b 18$title = optional_param('title', '', PARAM_FILE);
19$action = optional_param('action', '', PARAM_ALPHA);
20$search = optional_param('s', '', PARAM_CLEANHTML);
3570711a 21// id of repository
22$repo_id = optional_param('repo_id', 1, PARAM_INT);
b6558c3b 23// TODO
24// what will happen if user use a fake ctx_id?
25// Think about using $SESSION save it
f3a6f85b 26$ctx_id = optional_param('ctx_id', SITEID, PARAM_INT);
f3a6f85b 27$userid = $USER->id;
5bce5972 28
455860ce 29if (!repository_check_context($ctx_id)) {
30 $err = new stdclass;
31 $err->e = get_string('nopermissiontoaccess', 'repository');
32 die(json_encode($err));
33}
34
35// do global search
36if($action=='gsearch'){
37 $repos = repository_get_instances(array(get_context_instance_by_id($ctx_id), get_system_context()));
38 $list = array();
39 foreach($repos as $repo){
40 if ($repo->global_search()) {
41 try {
42 $ret = $repo->get_listing(null, $search);
43 $tmp = array_merge($list, $ret->list);
44 $list = $tmp;
45 } catch (repository_exception $e) {
46 $err = new stdclass;
47 $err->e = $e->getMessage();
48 die(json_encode($err));
49 }
50 }
51 }
52 die(json_encode(array('list'=>$list)));
53}
54
b3276c45 55$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 56if(!$repository = $DB->get_record_sql($sql)) {
bf1fccf0 57 $err = new stdclass;
58 $err->e = get_string('invalidrepositoryid', 'repository');
59 die(json_encode($err));
5a3b9db9 60} else {
61 $type = $repository->type;
0eb58cf4 62}
63
bf1fccf0 64if(file_exists($CFG->dirroot.'/repository/'.
5a3b9db9 65 $type.'/repository.class.php'))
bf1fccf0 66{
67 require_once($CFG->dirroot.'/repository/'.
5a3b9db9 68 $type.'/repository.class.php');
69 $classname = 'repository_' . $type;
bf1fccf0 70 try{
b3276c45 71 $repo = new $classname($repo_id, $ctx_id, array('ajax'=>true, 'name'=>$repository->name));
bf1fccf0 72 } catch (repository_exception $e){
73 $err = new stdclass;
74 $err->e = $e->getMessage();
3570711a 75 die(json_encode($err));
bf1fccf0 76 }
0eb58cf4 77} else {
bf1fccf0 78 $err = new stdclass;
79 $err->e = get_string('invalidplugin', 'repository');
80 die(json_encode($err));
0eb58cf4 81}
82
5fd3e8f7 83if ($action == 'list' || $action == 'search') {
bf1fccf0 84 try {
85 if(!empty($p)) {
86 echo json_encode($repo->get_listing($p));
87 } else if(!empty($search)) {
88 echo json_encode($repo->get_listing('', $search));
89 } else {
90 echo json_encode($repo->get_listing());
91 }
92 } catch (repository_exception $e) {
93 $err = new stdclass;
94 $err->e = $e->getMessage();
95 die(json_encode($err));
5bce5972 96 }
75e7a38c 97
1e28c767 98} elseif($action == 'download') {
d8eb6e18 99 $path = $repo->get_file($file, $title);
64be2d6c 100 $itemid = (int)substr(hexdec(uniqid()), 0, 9)+rand(1,100);
bf1fccf0 101 try {
49a1ce19 102 $info = repository_move_to_filepool($path, $title, $itemid);
d8eb6e18 103 if($env == 'form'){
c0fa8cba 104 echo json_encode($info);
d8eb6e18 105 } elseif($env == 'editor') {
c0fa8cba 106 echo json_encode($info);
d8eb6e18 107 } else {
6cbb4efb 108 }
bf1fccf0 109 } catch (repository_exception $e){
110 $err = new stdclass;
111 $err->e = $e->getMessage();
112 die(json_encode($err));
c2762f06 113 } catch (Exception $e) {
114 $err = new stdclass;
115 $err->e = $e->getMessage();
116 die(json_encode($err));
bf1fccf0 117 }
5fd3e8f7 118} elseif ($action == 'login') {
bf1fccf0 119 try {
120 echo json_encode($repo->print_login());
121 } catch (repository_exception $e){
122 $err = new stdclass;
123 $err->e = $e->getMessage();
124 die(json_encode($err));
125 }
5a3b9db9 126} elseif ($action == 'upload') {
127 try {
128 echo json_encode($repo->get_listing());
129 } catch (repository_exception $e){
130 $err = new stdclass;
131 $err->e = $e->getMessage();
132 die(json_encode($err));
133 }
0eb58cf4 134}