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-07-23 04:43:53 +00:00
|
|
|
// set one hour here
|
|
|
|
$CFG->repository_cache_expire = 60*60;
|
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
|
|
|
// TODO: which type should be?
|
|
|
|
$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-08-02 15:52:14 +00:00
|
|
|
// id of repository
|
|
|
|
$repo_id = optional_param('repo_id', 1, PARAM_INT);
|
2008-08-07 03:33:47 +00:00
|
|
|
// TODO
|
|
|
|
// what will happen if user use a fake ctx_id?
|
|
|
|
// Think about using $SESSION save it
|
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-01 08:19:28 +00:00
|
|
|
$sql = 'SELECT i.typeid, r.type FROM {repository} r, {repository_instances} i WHERE i.id='.$repo_id.' AND i.typeid=r.id';
|
|
|
|
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-08-07 03:33:47 +00:00
|
|
|
$repo = new $classname($repo_id, $ctx_id, array('ajax'=>true));
|
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-08-20 04:24:38 +00:00
|
|
|
if ($action == 'list' || $action == '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-07-14 04:34:26 +00:00
|
|
|
|
2008-07-17 03:54:20 +00:00
|
|
|
} elseif($action == '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-08-20 04:24:38 +00:00
|
|
|
} elseif ($action == 'login') {
|
2008-07-23 04:43:53 +00:00
|
|
|
try {
|
|
|
|
echo json_encode($repo->print_login());
|
|
|
|
} catch (repository_exception $e){
|
|
|
|
$err = new stdclass;
|
|
|
|
$err->e = $e->getMessage();
|
|
|
|
die(json_encode($err));
|
|
|
|
}
|
2008-09-01 08:19:28 +00:00
|
|
|
} elseif ($action == 'upload') {
|
|
|
|
try {
|
|
|
|
echo json_encode($repo->get_listing());
|
|
|
|
} catch (repository_exception $e){
|
|
|
|
$err = new stdclass;
|
|
|
|
$err->e = $e->getMessage();
|
|
|
|
die(json_encode($err));
|
|
|
|
}
|
2008-07-07 06:34:39 +00:00
|
|
|
}
|