2008-07-07 06:34:39 +00:00
|
|
|
<?php
|
2008-07-17 03:54:20 +00:00
|
|
|
set_time_limit(0);
|
2008-08-21 02:05:02 +00:00
|
|
|
header("Cache-Control: no-cache, must-revalidate");
|
|
|
|
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
|
2008-07-07 06:34:39 +00:00
|
|
|
require_once('../config.php');
|
2008-08-01 03:40:37 +00:00
|
|
|
require_once('../lib/filelib.php');
|
2008-07-07 06:34:39 +00:00
|
|
|
require_once('lib.php');
|
2008-08-27 06:23:01 +00:00
|
|
|
// page or path
|
2008-08-07 03:33:47 +00:00
|
|
|
$p = optional_param('p', '', PARAM_INT);
|
2008-08-01 04:10:31 +00:00
|
|
|
// opened in editor or moodleform
|
2008-08-07 03:33:47 +00:00
|
|
|
$env = optional_param('env', 'form', PARAM_ALPHA);
|
2008-08-01 04:10:31 +00:00
|
|
|
// file to download
|
2008-08-11 03:30:09 +00:00
|
|
|
$file = optional_param('file', '', PARAM_RAW);
|
2008-08-01 04:10:31 +00:00
|
|
|
// rename the file name
|
2008-08-07 03:33:47 +00:00
|
|
|
$title = optional_param('title', '', PARAM_FILE);
|
|
|
|
$action = optional_param('action', '', PARAM_ALPHA);
|
|
|
|
$search = optional_param('s', '', PARAM_CLEANHTML);
|
2008-09-11 03:18:54 +00:00
|
|
|
$callback = optional_param('callback', '', PARAM_CLEANHTML);
|
2008-09-09 03:25:03 +00:00
|
|
|
// repository ID
|
2008-08-02 15:52:14 +00:00
|
|
|
$repo_id = optional_param('repo_id', 1, PARAM_INT);
|
2008-08-06 08:09:01 +00:00
|
|
|
$ctx_id = optional_param('ctx_id', SITEID, PARAM_INT);
|
|
|
|
$userid = $USER->id;
|
2008-07-09 07:48:33 +00:00
|
|
|
|
2008-09-09 03:25:03 +00:00
|
|
|
// check context id
|
2008-09-08 05:38:13 +00:00
|
|
|
if (!repository_check_context($ctx_id)) {
|
|
|
|
$err = new stdclass;
|
|
|
|
$err->e = get_string('nopermissiontoaccess', 'repository');
|
|
|
|
die(json_encode($err));
|
|
|
|
}
|
|
|
|
|
2008-09-09 03:25:03 +00:00
|
|
|
/**
|
|
|
|
* walk an array to attach repository ID
|
|
|
|
*/
|
2008-09-08 06:20:24 +00:00
|
|
|
function attach_repository_id(&$value, $key, $id){
|
|
|
|
$value['repo_id'] = $id;
|
|
|
|
}
|
2008-09-09 03:25:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* these actions are requested without repository ID
|
|
|
|
*/
|
|
|
|
switch ($action) {
|
|
|
|
case 'gsearch':
|
|
|
|
// global search
|
2008-09-08 05:38:13 +00:00
|
|
|
$repos = repository_get_instances(array(get_context_instance_by_id($ctx_id), get_system_context()));
|
|
|
|
$list = array();
|
|
|
|
foreach($repos as $repo){
|
|
|
|
if ($repo->global_search()) {
|
|
|
|
try {
|
|
|
|
$ret = $repo->get_listing(null, $search);
|
2008-09-08 06:20:24 +00:00
|
|
|
array_walk($ret['list'], 'attach_repository_id', $repo->id);
|
|
|
|
$tmp = array_merge($list, $ret['list']);
|
2008-09-08 05:38:13 +00:00
|
|
|
$list = $tmp;
|
|
|
|
} catch (repository_exception $e) {
|
|
|
|
$err = new stdclass;
|
|
|
|
$err->e = $e->getMessage();
|
|
|
|
die(json_encode($err));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
die(json_encode(array('list'=>$list)));
|
2008-09-09 03:25:03 +00:00
|
|
|
break;
|
|
|
|
case 'ccache':
|
|
|
|
//clean cache
|
2008-09-08 08:48:07 +00:00
|
|
|
$cache = new curl_cache;
|
|
|
|
$cache->refresh();
|
2008-09-09 09:04:32 +00:00
|
|
|
$action = 'list';
|
2008-09-09 03:25:03 +00:00
|
|
|
break;
|
2008-09-08 08:48:07 +00:00
|
|
|
}
|
2008-09-08 05:38:13 +00:00
|
|
|
|
2008-09-09 03:25:03 +00:00
|
|
|
// Get repository instance information
|
2008-09-02 04:08:49 +00:00
|
|
|
$sql = 'SELECT i.name, i.typeid, r.type FROM {repository} r, {repository_instances} i WHERE i.id='.$repo_id.' AND i.typeid=r.id';
|
2008-09-01 08:19:28 +00:00
|
|
|
if(!$repository = $DB->get_record_sql($sql)) {
|
2008-07-23 04:43:53 +00:00
|
|
|
$err = new stdclass;
|
|
|
|
$err->e = get_string('invalidrepositoryid', 'repository');
|
|
|
|
die(json_encode($err));
|
2008-09-01 08:19:28 +00:00
|
|
|
} else {
|
|
|
|
$type = $repository->type;
|
2008-07-07 06:34:39 +00:00
|
|
|
}
|
|
|
|
|
2008-07-23 04:43:53 +00:00
|
|
|
if(file_exists($CFG->dirroot.'/repository/'.
|
2008-09-01 08:19:28 +00:00
|
|
|
$type.'/repository.class.php'))
|
2008-07-23 04:43:53 +00:00
|
|
|
{
|
|
|
|
require_once($CFG->dirroot.'/repository/'.
|
2008-09-01 08:19:28 +00:00
|
|
|
$type.'/repository.class.php');
|
|
|
|
$classname = 'repository_' . $type;
|
2008-07-23 04:43:53 +00:00
|
|
|
try{
|
2008-09-02 04:08:49 +00:00
|
|
|
$repo = new $classname($repo_id, $ctx_id, array('ajax'=>true, 'name'=>$repository->name));
|
2008-07-23 04:43:53 +00:00
|
|
|
} catch (repository_exception $e){
|
|
|
|
$err = new stdclass;
|
|
|
|
$err->e = $e->getMessage();
|
2008-08-02 15:52:14 +00:00
|
|
|
die(json_encode($err));
|
2008-07-23 04:43:53 +00:00
|
|
|
}
|
2008-07-07 06:34:39 +00:00
|
|
|
} else {
|
2008-07-23 04:43:53 +00:00
|
|
|
$err = new stdclass;
|
|
|
|
$err->e = get_string('invalidplugin', 'repository');
|
|
|
|
die(json_encode($err));
|
2008-07-07 06:34:39 +00:00
|
|
|
}
|
|
|
|
|
2008-09-11 03:18:54 +00:00
|
|
|
if (!empty($callback)) {
|
|
|
|
// call opener window to refresh repository
|
|
|
|
// the callback url should be something like this:
|
|
|
|
// http://xx.moodle.com/repository/ws.php?callback=yes&repo_id=1&sid=xxx
|
|
|
|
// sid is the attached auth token from external source
|
|
|
|
$js =<<<EOD
|
|
|
|
<html><head><script type="text/javascript">
|
|
|
|
window.opener.repository_callback($repo_id);
|
|
|
|
window.close();
|
|
|
|
</script><body></body></html>
|
|
|
|
EOD;
|
|
|
|
echo $js;
|
|
|
|
die;
|
|
|
|
}
|
|
|
|
|
2008-09-09 03:25:03 +00:00
|
|
|
switch ($action) {
|
|
|
|
case 'login':
|
|
|
|
try {
|
|
|
|
echo json_encode($repo->print_login());
|
|
|
|
} catch (repository_exception $e){
|
|
|
|
$err = new stdclass;
|
|
|
|
$err->e = $e->getMessage();
|
|
|
|
die(json_encode($err));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'list':
|
|
|
|
case 'search':
|
2008-07-23 04:43:53 +00:00
|
|
|
try {
|
|
|
|
if(!empty($p)) {
|
|
|
|
echo json_encode($repo->get_listing($p));
|
|
|
|
} else if(!empty($search)) {
|
|
|
|
echo json_encode($repo->get_listing('', $search));
|
|
|
|
} else {
|
|
|
|
echo json_encode($repo->get_listing());
|
|
|
|
}
|
|
|
|
} catch (repository_exception $e) {
|
|
|
|
$err = new stdclass;
|
|
|
|
$err->e = $e->getMessage();
|
|
|
|
die(json_encode($err));
|
2008-07-09 07:48:33 +00:00
|
|
|
}
|
2008-09-09 03:25:03 +00:00
|
|
|
break;
|
|
|
|
case 'download':
|
2008-08-04 05:53:53 +00:00
|
|
|
$path = $repo->get_file($file, $title);
|
2008-08-11 03:30:09 +00:00
|
|
|
$itemid = (int)substr(hexdec(uniqid()), 0, 9)+rand(1,100);
|
2008-07-23 04:43:53 +00:00
|
|
|
try {
|
2008-08-28 05:13:13 +00:00
|
|
|
$info = repository_move_to_filepool($path, $title, $itemid);
|
2008-08-04 05:53:53 +00:00
|
|
|
if($env == 'form'){
|
2008-08-29 06:31:19 +00:00
|
|
|
echo json_encode($info);
|
2008-08-04 05:53:53 +00:00
|
|
|
} elseif($env == 'editor') {
|
2008-08-29 06:31:19 +00:00
|
|
|
echo json_encode($info);
|
2008-08-04 05:53:53 +00:00
|
|
|
} else {
|
2008-08-01 03:40:37 +00:00
|
|
|
}
|
2008-07-23 04:43:53 +00:00
|
|
|
} catch (repository_exception $e){
|
|
|
|
$err = new stdclass;
|
|
|
|
$err->e = $e->getMessage();
|
|
|
|
die(json_encode($err));
|
2008-08-05 05:12:30 +00:00
|
|
|
} catch (Exception $e) {
|
|
|
|
$err = new stdclass;
|
|
|
|
$err->e = $e->getMessage();
|
|
|
|
die(json_encode($err));
|
2008-07-23 04:43:53 +00:00
|
|
|
}
|
2008-09-09 03:25:03 +00:00
|
|
|
break;
|
|
|
|
case 'upload':
|
2008-09-01 08:19:28 +00:00
|
|
|
try {
|
|
|
|
echo json_encode($repo->get_listing());
|
|
|
|
} catch (repository_exception $e){
|
|
|
|
$err = new stdclass;
|
|
|
|
$err->e = $e->getMessage();
|
|
|
|
die(json_encode($err));
|
|
|
|
}
|
2008-09-09 03:25:03 +00:00
|
|
|
break;
|
2008-07-07 06:34:39 +00:00
|
|
|
}
|