MDL-14492 Provide feedback to user about redirect delay in loadsco.php. Change to...
[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') {
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?>