0eb58cf4 |
1 | <?php |
1e28c767 |
2 | set_time_limit(0); |
0e8ae38e |
3 | header("Cache-Control: no-cache, must-revalidate"); |
4 | header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); |
0eb58cf4 |
5 | require_once('../config.php'); |
6cbb4efb |
6 | require_once('../lib/filelib.php'); |
0eb58cf4 |
7 | require_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 |
29 | if (!repository_check_context($ctx_id)) { |
30 | $err = new stdclass; |
31 | $err->e = get_string('nopermissiontoaccess', 'repository'); |
32 | die(json_encode($err)); |
33 | } |
34 | |
d05ef4a3 |
35 | function attach_repository_id(&$value, $key, $id){ |
36 | $value['repo_id'] = $id; |
37 | } |
455860ce |
38 | // do global search |
39 | if($action=='gsearch'){ |
40 | $repos = repository_get_instances(array(get_context_instance_by_id($ctx_id), get_system_context())); |
41 | $list = array(); |
42 | foreach($repos as $repo){ |
43 | if ($repo->global_search()) { |
44 | try { |
45 | $ret = $repo->get_listing(null, $search); |
d05ef4a3 |
46 | array_walk($ret['list'], 'attach_repository_id', $repo->id); |
47 | $tmp = array_merge($list, $ret['list']); |
455860ce |
48 | $list = $tmp; |
49 | } catch (repository_exception $e) { |
50 | $err = new stdclass; |
51 | $err->e = $e->getMessage(); |
52 | die(json_encode($err)); |
53 | } |
54 | } |
55 | } |
56 | die(json_encode(array('list'=>$list))); |
57 | } |
aae85978 |
58 | if ($action=='ccache') { |
59 | $cache = new curl_cache; |
60 | $cache->refresh(); |
61 | die(get_string('cachecleared', 'repository')); |
62 | } |
455860ce |
63 | |
b3276c45 |
64 | $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 |
65 | if(!$repository = $DB->get_record_sql($sql)) { |
bf1fccf0 |
66 | $err = new stdclass; |
67 | $err->e = get_string('invalidrepositoryid', 'repository'); |
68 | die(json_encode($err)); |
5a3b9db9 |
69 | } else { |
70 | $type = $repository->type; |
0eb58cf4 |
71 | } |
72 | |
bf1fccf0 |
73 | if(file_exists($CFG->dirroot.'/repository/'. |
5a3b9db9 |
74 | $type.'/repository.class.php')) |
bf1fccf0 |
75 | { |
76 | require_once($CFG->dirroot.'/repository/'. |
5a3b9db9 |
77 | $type.'/repository.class.php'); |
78 | $classname = 'repository_' . $type; |
bf1fccf0 |
79 | try{ |
b3276c45 |
80 | $repo = new $classname($repo_id, $ctx_id, array('ajax'=>true, 'name'=>$repository->name)); |
bf1fccf0 |
81 | } catch (repository_exception $e){ |
82 | $err = new stdclass; |
83 | $err->e = $e->getMessage(); |
3570711a |
84 | die(json_encode($err)); |
bf1fccf0 |
85 | } |
0eb58cf4 |
86 | } else { |
bf1fccf0 |
87 | $err = new stdclass; |
88 | $err->e = get_string('invalidplugin', 'repository'); |
89 | die(json_encode($err)); |
0eb58cf4 |
90 | } |
91 | |
5fd3e8f7 |
92 | if ($action == 'list' || $action == 'search') { |
bf1fccf0 |
93 | try { |
94 | if(!empty($p)) { |
95 | echo json_encode($repo->get_listing($p)); |
96 | } else if(!empty($search)) { |
97 | echo json_encode($repo->get_listing('', $search)); |
98 | } else { |
99 | echo json_encode($repo->get_listing()); |
100 | } |
101 | } catch (repository_exception $e) { |
102 | $err = new stdclass; |
103 | $err->e = $e->getMessage(); |
104 | die(json_encode($err)); |
5bce5972 |
105 | } |
75e7a38c |
106 | |
1e28c767 |
107 | } elseif($action == 'download') { |
d8eb6e18 |
108 | $path = $repo->get_file($file, $title); |
64be2d6c |
109 | $itemid = (int)substr(hexdec(uniqid()), 0, 9)+rand(1,100); |
bf1fccf0 |
110 | try { |
49a1ce19 |
111 | $info = repository_move_to_filepool($path, $title, $itemid); |
d8eb6e18 |
112 | if($env == 'form'){ |
c0fa8cba |
113 | echo json_encode($info); |
d8eb6e18 |
114 | } elseif($env == 'editor') { |
c0fa8cba |
115 | echo json_encode($info); |
d8eb6e18 |
116 | } else { |
6cbb4efb |
117 | } |
bf1fccf0 |
118 | } catch (repository_exception $e){ |
119 | $err = new stdclass; |
120 | $err->e = $e->getMessage(); |
121 | die(json_encode($err)); |
c2762f06 |
122 | } catch (Exception $e) { |
123 | $err = new stdclass; |
124 | $err->e = $e->getMessage(); |
125 | die(json_encode($err)); |
bf1fccf0 |
126 | } |
5fd3e8f7 |
127 | } elseif ($action == 'login') { |
bf1fccf0 |
128 | try { |
129 | echo json_encode($repo->print_login()); |
130 | } catch (repository_exception $e){ |
131 | $err = new stdclass; |
132 | $err->e = $e->getMessage(); |
133 | die(json_encode($err)); |
134 | } |
5a3b9db9 |
135 | } elseif ($action == 'upload') { |
136 | try { |
137 | echo json_encode($repo->get_listing()); |
138 | } catch (repository_exception $e){ |
139 | $err = new stdclass; |
140 | $err->e = $e->getMessage(); |
141 | die(json_encode($err)); |
142 | } |
0eb58cf4 |
143 | } |