.
//
require_once('../config.php');
require_once($CFG->libdir.'/filelib.php');
require_once('lib.php');
/// Wait as long as it takes for this script to finish
set_time_limit(0);
require_login();
$page = optional_param('page', '', PARAM_RAW); // page
$client_id = optional_param('client_id', SITEID, PARAM_RAW); // client ID
$env = optional_param('env', 'filepicker', PARAM_ALPHA); // opened in editor or moodleform
$file = optional_param('file', '', PARAM_RAW); // file to download
$title = optional_param('title', '', PARAM_FILE); // new file name
$itemid = optional_param('itemid', '', PARAM_INT);
$icon = optional_param('icon', '', PARAM_RAW);
$action = optional_param('action', '', PARAM_ALPHA);
$ctx_id = optional_param('ctx_id', SITEID, PARAM_INT); // context ID
$repo_id = optional_param('repo_id', 0, PARAM_INT); // repository ID
$req_path = optional_param('p', '', PARAM_RAW); // path
$page = optional_param('page', '', PARAM_RAW);
$callback = optional_param('callback', '', PARAM_CLEANHTML);
$search_text = optional_param('s', '', PARAM_CLEANHTML);
// init repository plugin
$sql = 'SELECT i.name, i.typeid, r.type FROM {repository} r, {repository_instances} i '.
'WHERE i.id=? AND i.typeid=r.id';
if ($repository = $DB->get_record_sql($sql, array($repo_id))) {
$type = $repository->type;
if (file_exists($CFG->dirroot.'/repository/'.$type.'/repository.class.php')) {
require_once($CFG->dirroot.'/repository/'.$type.'/repository.class.php');
$classname = 'repository_' . $type;
try {
$repo = new $classname($repo_id, $ctx_id, array('ajax'=>false, 'name'=>$repository->name, 'client_id'=>$client_id));
} catch (repository_exception $e){
print_error('pluginerror', 'repository');
}
} else {
print_error('invalidplugin', 'repository');
}
}
$url = $CFG->httpswwwroot."/repository/filepicker.php?ctx_id=$ctx_id&itemid=$itemid";
$home_url = $url.'&action=embedded';
switch ($action) {
case 'upload':
// The uploaded file has been processed in plugin construct function
redirect($url, get_string('uploadsucc','repository'));
break;
case 'deletedraft':
if (!$context = get_context_instance(CONTEXT_USER, $USER->id)) {
print_error('wrongcontextid', 'error');
}
$contextid = $context->id;
$fs = get_file_storage();
if ($file = $fs->get_file($contextid, 'user_draft', $itemid, '/', $title)) {
if($result = $file->delete()) {
header("Location: {$home_url}");
} else {
print_error('cannotdelete', 'repository');
}
}
exit;
break;
case 'search':
echo "
";
try {
$search_result = $repo->search($search_text);
$search_result['search_result'] = true;
$search_result['repo_id'] = $repo_id;
// TODO: need a better solution
print_paging_bar($search_result['total'], $search_result['page']-1,
$search_result['perpage'], "{$url}&search_paging=1&action=search&repo_id={$repo_id}&", 'p');
echo '';
foreach ($search_result['list'] as $item) {
echo '';
echo '';
echo ' | ';
if (!empty($item['url'])) {
echo ''.$item['title'].'';
} else {
echo $item['title'];
}
echo ' | ';
echo '';
echo '';
echo ' | ';
echo '
';
}
echo '
';
} catch (repository_exception $e) {
}
break;
case 'list':
case 'sign':
print_header();
echo "";
if ($repo->check_login()) {
$list = $repo->get_listing($req_path, $page);
$dynload = !empty($list['dynload'])?true:false;
if (!empty($list['upload'])) {
echo '';
} else {
if (!empty($list['path'])) {
foreach ($list['path'] as $p) {
echo '';
echo ' / ';
}
}
if (!empty($list['page'])) {
// TODO: need a better solution
print_paging_bar($list['total'], $list['page']-1,
$list['perpage'], $CFG->httpswwwroot
.'/repository/filepicker.php?action=list&itemid='
.$itemid.'&ctx_id='.$ctx_id.'&repo_id='.$repo_id.'&', 'page', false, false, 1);
}
echo '';
foreach ($list['list'] as $item) {
echo '';
echo '';
echo ' | ';
if (!empty($item['url'])) {
echo ''.$item['title'].'';
} else {
echo $item['title'];
}
echo ' | ';
echo '';
if (!isset($item['children'])) {
echo '';
} else {
echo '';
}
echo ' | ';
echo '
';
}
echo '
';
}
} else {
echo '';
}
print_footer('empty');
break;
case 'download':
$filepath = $repo->get_file($file, $title, $itemid);
if (!empty($filepath)) {
// normal file path name
$info = repository::move_to_filepool($filepath, $title, $itemid);
//echo json_encode($info);
redirect($url, get_string('downloadsucc','repository'));
} else {
print_error('cannotdownload', 'repository');
}
break;
case 'confirm':
print_header();
echo '';
echo '';
echo '';
print_footer('empty');
break;
case 'plugins':
$user_context = get_context_instance(CONTEXT_USER, $USER->id);
$repos = repository::get_instances(array($user_context, get_system_context()), null, true, null, '*', 'ref_id');
print_header();
echo '';
foreach($repos as $repo) {
$info = $repo->get_meta();
echo '- '.$info->name.'
';
}
echo '
';
break;
default:
$user_context = get_context_instance(CONTEXT_USER, $USER->id);
$repos = repository::get_instances(array($user_context, get_system_context()), null, true, null, '*', 'ref_id');
print_header();
$fs = get_file_storage();
$context = get_context_instance(CONTEXT_USER, $USER->id);
$files = $fs->get_area_files($context->id, 'user_draft', $itemid);
if (empty($files)) {
echo get_string('nofilesattached', 'repository');
} else {
echo '';
foreach ($files as $file) {
if ($file->get_filename()!='.') {
$drafturl = $CFG->httpswwwroot.'/draftfile.php/'.$context->id.'/user_draft/'.$itemid.'/'.$file->get_filename();
echo '- '.$file->get_filename().' ';
echo '
';
}
}
echo '
';
}
echo '';
print_footer('empty');
break;
}