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 | |
b3276c45 |
29 | $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 |
30 | if(!$repository = $DB->get_record_sql($sql)) { |
bf1fccf0 |
31 | $err = new stdclass; |
32 | $err->e = get_string('invalidrepositoryid', 'repository'); |
33 | die(json_encode($err)); |
5a3b9db9 |
34 | } else { |
35 | $type = $repository->type; |
0eb58cf4 |
36 | } |
37 | |
2057487c |
38 | if (!repository_check_context($ctx_id)) { |
39 | $err = new stdclass; |
40 | $err->e = get_string('nopermissiontoaccess', 'repository'); |
41 | die(json_encode($err)); |
42 | } |
43 | |
bf1fccf0 |
44 | if(file_exists($CFG->dirroot.'/repository/'. |
5a3b9db9 |
45 | $type.'/repository.class.php')) |
bf1fccf0 |
46 | { |
47 | require_once($CFG->dirroot.'/repository/'. |
5a3b9db9 |
48 | $type.'/repository.class.php'); |
49 | $classname = 'repository_' . $type; |
bf1fccf0 |
50 | try{ |
b3276c45 |
51 | $repo = new $classname($repo_id, $ctx_id, array('ajax'=>true, 'name'=>$repository->name)); |
bf1fccf0 |
52 | } catch (repository_exception $e){ |
53 | $err = new stdclass; |
54 | $err->e = $e->getMessage(); |
3570711a |
55 | die(json_encode($err)); |
bf1fccf0 |
56 | } |
0eb58cf4 |
57 | } else { |
bf1fccf0 |
58 | $err = new stdclass; |
59 | $err->e = get_string('invalidplugin', 'repository'); |
60 | die(json_encode($err)); |
0eb58cf4 |
61 | } |
62 | |
5fd3e8f7 |
63 | if ($action == 'list' || $action == 'search') { |
bf1fccf0 |
64 | try { |
65 | if(!empty($p)) { |
66 | echo json_encode($repo->get_listing($p)); |
67 | } else if(!empty($search)) { |
68 | echo json_encode($repo->get_listing('', $search)); |
69 | } else { |
70 | echo json_encode($repo->get_listing()); |
71 | } |
72 | } catch (repository_exception $e) { |
73 | $err = new stdclass; |
74 | $err->e = $e->getMessage(); |
75 | die(json_encode($err)); |
5bce5972 |
76 | } |
75e7a38c |
77 | |
1e28c767 |
78 | } elseif($action == 'download') { |
d8eb6e18 |
79 | $path = $repo->get_file($file, $title); |
64be2d6c |
80 | $itemid = (int)substr(hexdec(uniqid()), 0, 9)+rand(1,100); |
bf1fccf0 |
81 | try { |
49a1ce19 |
82 | $info = repository_move_to_filepool($path, $title, $itemid); |
d8eb6e18 |
83 | if($env == 'form'){ |
c0fa8cba |
84 | echo json_encode($info); |
d8eb6e18 |
85 | } elseif($env == 'editor') { |
c0fa8cba |
86 | echo json_encode($info); |
d8eb6e18 |
87 | } else { |
6cbb4efb |
88 | } |
bf1fccf0 |
89 | } catch (repository_exception $e){ |
90 | $err = new stdclass; |
91 | $err->e = $e->getMessage(); |
92 | die(json_encode($err)); |
c2762f06 |
93 | } catch (Exception $e) { |
94 | $err = new stdclass; |
95 | $err->e = $e->getMessage(); |
96 | die(json_encode($err)); |
bf1fccf0 |
97 | } |
5fd3e8f7 |
98 | } elseif ($action == 'login') { |
bf1fccf0 |
99 | try { |
100 | echo json_encode($repo->print_login()); |
101 | } catch (repository_exception $e){ |
102 | $err = new stdclass; |
103 | $err->e = $e->getMessage(); |
104 | die(json_encode($err)); |
105 | } |
5a3b9db9 |
106 | } elseif ($action == 'upload') { |
107 | try { |
108 | echo json_encode($repo->get_listing()); |
109 | } catch (repository_exception $e){ |
110 | $err = new stdclass; |
111 | $err->e = $e->getMessage(); |
112 | die(json_encode($err)); |
113 | } |
0eb58cf4 |
114 | } |