0eb58cf4 |
1 | <?php |
1e28c767 |
2 | set_time_limit(0); |
0eb58cf4 |
3 | require_once('../config.php'); |
6cbb4efb |
4 | require_once('../lib/filelib.php'); |
0eb58cf4 |
5 | require_once('lib.php'); |
bf1fccf0 |
6 | // set one hour here |
7 | $CFG->repository_cache_expire = 60*60; |
3e515a9f |
8 | // page |
9 | $p = optional_param('p', '', PARAM_RAW); |
10 | // id of repository |
bf1fccf0 |
11 | $id = optional_param('id', 1, PARAM_INT); |
3e515a9f |
12 | // opened in editor or moodleform |
13 | $env = optional_param('env', 'form', PARAM_RAW); |
14 | // file to download |
bb2c046d |
15 | $file = optional_param('file', '', PARAM_RAW); |
3e515a9f |
16 | // rename the file name |
bb2c046d |
17 | $title = optional_param('title', '', PARAM_RAW); |
3e515a9f |
18 | $action = optional_param('action', '', PARAM_RAW); |
19 | $search = optional_param('s', '', PARAM_RAW); |
5bce5972 |
20 | |
0eb58cf4 |
21 | if(!$repository = $DB->get_record('repository', array('id'=>$id))) { |
bf1fccf0 |
22 | $err = new stdclass; |
23 | $err->e = get_string('invalidrepositoryid', 'repository'); |
24 | die(json_encode($err)); |
0eb58cf4 |
25 | } |
26 | |
bf1fccf0 |
27 | if(file_exists($CFG->dirroot.'/repository/'. |
28 | $repository->repositorytype.'/repository.class.php')) |
29 | { |
30 | require_once($CFG->dirroot.'/repository/'. |
31 | $repository->repositorytype.'/repository.class.php'); |
0eb58cf4 |
32 | $classname = 'repository_' . $repository->repositorytype; |
bf1fccf0 |
33 | try{ |
34 | $repo = new $classname($id, SITEID, array('ajax'=>true)); |
35 | } catch (repository_exception $e){ |
36 | $err = new stdclass; |
37 | $err->e = $e->getMessage(); |
19add4c0 |
38 | die(json_encode($err.time())); |
bf1fccf0 |
39 | } |
0eb58cf4 |
40 | } else { |
bf1fccf0 |
41 | $err = new stdclass; |
42 | $err->e = get_string('invalidplugin', 'repository'); |
43 | die(json_encode($err)); |
0eb58cf4 |
44 | } |
45 | |
46 | if($action == 'list') { |
bf1fccf0 |
47 | try { |
48 | if(!empty($p)) { |
49 | echo json_encode($repo->get_listing($p)); |
50 | } else if(!empty($search)) { |
51 | echo json_encode($repo->get_listing('', $search)); |
52 | } else { |
53 | echo json_encode($repo->get_listing()); |
54 | } |
55 | } catch (repository_exception $e) { |
56 | $err = new stdclass; |
57 | $err->e = $e->getMessage(); |
58 | die(json_encode($err)); |
5bce5972 |
59 | } |
75e7a38c |
60 | |
1e28c767 |
61 | } elseif($action == 'download') { |
bb2c046d |
62 | $ret = $repo->get_file($file, $title); |
63 | // TODO |
6cbb4efb |
64 | // Ask Petr how to use FILE_API here |
bf1fccf0 |
65 | try { |
6cbb4efb |
66 | $pathname = $ret; |
67 | $entry = new object(); |
68 | $entry->contextid = SITEID; |
69 | $entry->filearea = 'repository'; |
70 | $entry->filepath = '/'; |
71 | $entry->filename = $title; |
72 | $entry->timecreated = time(); |
73 | $entry->timemodified = time(); |
74 | $entry->itemid = $USER->id; |
75 | $entry->mimetype = mimeinfo('type', $pathname); |
76 | $entry->userid = $USER->id; |
77 | $fs = get_file_storage(); |
78 | if ($file = $fs->create_file_from_pathname($entry, $pathname)) { |
3e515a9f |
79 | if($env == 'form'){ |
80 | // return reference id |
81 | echo json_encode($file->get_itemid()); |
82 | } elseif($env == 'editor') { |
83 | // return url |
84 | // echo json_encode($file->get_content_file_location()); |
85 | } else { |
86 | } |
6cbb4efb |
87 | } |
bf1fccf0 |
88 | } catch (repository_exception $e){ |
89 | $err = new stdclass; |
90 | $err->e = $e->getMessage(); |
91 | die(json_encode($err)); |
92 | } |
0eb58cf4 |
93 | } else { |
bf1fccf0 |
94 | try { |
95 | echo json_encode($repo->print_login()); |
96 | } catch (repository_exception $e){ |
97 | $err = new stdclass; |
98 | $err->e = $e->getMessage(); |
99 | die(json_encode($err)); |
100 | } |
0eb58cf4 |
101 | } |
102 | |
103 | ?> |