Updated the HEAD build version to 20080805
[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);
5bce5972 20
3570711a 21if(!$repository = $DB->get_record('repository', array('id'=>$repo_id))) {
bf1fccf0 22 $err = new stdclass;
23 $err->e = get_string('invalidrepositoryid', 'repository');
24 die(json_encode($err));
0eb58cf4 25}
26
bf1fccf0 27if(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{
3570711a 34 $repo = new $classname($repo_id, SITEID, array('ajax'=>true));
bf1fccf0 35 } catch (repository_exception $e){
36 $err = new stdclass;
37 $err->e = $e->getMessage();
3570711a 38 die(json_encode($err));
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
46if($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') {
d8eb6e18 62 $path = $repo->get_file($file, $title);
bf1fccf0 63 try {
d8eb6e18 64 $info = move_to_filepool($path, $title);
65 if($env == 'form'){
66 echo json_encode($info['id']);
67 } elseif($env == 'editor') {
68 echo json_encode($info['url']);
69 } else {
6cbb4efb 70 }
bf1fccf0 71 } catch (repository_exception $e){
72 $err = new stdclass;
73 $err->e = $e->getMessage();
74 die(json_encode($err));
75 }
0eb58cf4 76} else {
bf1fccf0 77 try {
78 echo json_encode($repo->print_login());
79 } catch (repository_exception $e){
80 $err = new stdclass;
81 $err->e = $e->getMessage();
82 die(json_encode($err));
83 }
0eb58cf4 84}
85
86?>