Updated the HEAD build version to 20080807
[moodle.git] / repository / ws.php
CommitLineData
0eb58cf4 1<?php
1e28c767 2set_time_limit(0);
0eb58cf4 3require_once('../config.php');
6cbb4efb 4require_once('../lib/filelib.php');
0eb58cf4 5require_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);
3e515a9f 10// opened in editor or moodleform
11$env = optional_param('env', 'form', PARAM_RAW);
12// file to download
bb2c046d 13$file = optional_param('file', '', PARAM_RAW);
3e515a9f 14// rename the file name
bb2c046d 15$title = optional_param('title', '', PARAM_RAW);
3e515a9f 16$action = optional_param('action', '', PARAM_RAW);
17$search = optional_param('s', '', PARAM_RAW);
3570711a 18// id of repository
19$repo_id = optional_param('repo_id', 1, PARAM_INT);
f3a6f85b 20// context id
21$ctx_id = optional_param('ctx_id', SITEID, PARAM_INT);
22$itemid = optional_param('itemid', 0, PARAM_INT);
23$userid = $USER->id;
5bce5972 24
f3a6f85b 25if(!$repository = $DB->get_record('repository', array('id'=>$repo_id)))
26{
bf1fccf0 27 $err = new stdclass;
28 $err->e = get_string('invalidrepositoryid', 'repository');
29 die(json_encode($err));
0eb58cf4 30}
31
bf1fccf0 32if(file_exists($CFG->dirroot.'/repository/'.
33 $repository->repositorytype.'/repository.class.php'))
34{
35 require_once($CFG->dirroot.'/repository/'.
36 $repository->repositorytype.'/repository.class.php');
0eb58cf4 37 $classname = 'repository_' . $repository->repositorytype;
bf1fccf0 38 try{
3570711a 39 $repo = new $classname($repo_id, SITEID, array('ajax'=>true));
bf1fccf0 40 } catch (repository_exception $e){
41 $err = new stdclass;
42 $err->e = $e->getMessage();
3570711a 43 die(json_encode($err));
bf1fccf0 44 }
0eb58cf4 45} else {
bf1fccf0 46 $err = new stdclass;
47 $err->e = get_string('invalidplugin', 'repository');
48 die(json_encode($err));
0eb58cf4 49}
50
51if($action == 'list') {
bf1fccf0 52 try {
53 if(!empty($p)) {
54 echo json_encode($repo->get_listing($p));
55 } else if(!empty($search)) {
56 echo json_encode($repo->get_listing('', $search));
57 } else {
58 echo json_encode($repo->get_listing());
59 }
60 } catch (repository_exception $e) {
61 $err = new stdclass;
62 $err->e = $e->getMessage();
63 die(json_encode($err));
5bce5972 64 }
75e7a38c 65
1e28c767 66} elseif($action == 'download') {
d8eb6e18 67 $path = $repo->get_file($file, $title);
bf1fccf0 68 try {
c2762f06 69 $info = move_to_filepool($path, $title, $itemid);
d8eb6e18 70 if($env == 'form'){
71 echo json_encode($info['id']);
72 } elseif($env == 'editor') {
73 echo json_encode($info['url']);
74 } else {
6cbb4efb 75 }
bf1fccf0 76 } catch (repository_exception $e){
77 $err = new stdclass;
78 $err->e = $e->getMessage();
79 die(json_encode($err));
c2762f06 80 } catch (Exception $e) {
81 $err = new stdclass;
82 $err->e = $e->getMessage();
83 die(json_encode($err));
bf1fccf0 84 }
0eb58cf4 85} else {
bf1fccf0 86 try {
87 echo json_encode($repo->print_login());
88 } catch (repository_exception $e){
89 $err = new stdclass;
90 $err->e = $e->getMessage();
91 die(json_encode($err));
92 }
0eb58cf4 93}