. // // // NOTE TO ALL DEVELOPERS: this script must deal only with draft area of current user, it has to use only file_storage and no file_browser!! // /** * This file is used to manage draft files in non-javascript browsers * * @since 2.0 * @package core * @subpackage repository * @copyright 2010 Dongsheng Cai * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ require_once('../config.php'); require_once($CFG->libdir.'/filelib.php'); require_once('lib.php'); require_sesskey(); require_login(); // disable blocks in this page $PAGE->set_pagelayout('embedded'); // general parameters $action = optional_param('action', '', PARAM_ALPHA); $itemid = optional_param('itemid', '', PARAM_INT); // parameters for repository $contextid = optional_param('ctx_id', SYSCONTEXTID, PARAM_INT); // context ID $courseid = optional_param('course', SITEID, PARAM_INT); // course ID $env = optional_param('env', 'filepicker', PARAM_ALPHA); // opened in file picker, file manager or html editor $filename = optional_param('filename', '', PARAM_FILE); $targetpath = optional_param('targetpath', '', PARAM_PATH); $maxfiles = optional_param('maxfiles', -1, PARAM_INT); // maxfiles $maxbytes = optional_param('maxbytes', 0, PARAM_INT); // maxbytes $subdirs = optional_param('subdirs', 0, PARAM_INT); // maxbytes // draft area $newdirname = optional_param('newdirname', '', PARAM_FILE); $newfilename = optional_param('newfilename', '', PARAM_FILE); // path in draft area $draftpath = optional_param('draftpath', '/', PARAM_PATH); // user context $user_context = get_context_instance(CONTEXT_USER, $USER->id); $PAGE->set_context($user_context); $fs = get_file_storage(); $params = array('ctx_id' => $contextid, 'itemid' => $itemid, 'env' => $env, 'course'=>$courseid, 'maxbytes'=>$maxbytes, 'maxfiles'=>$maxfiles, 'subdirs'=>$subdirs, 'sesskey'=>sesskey()); $PAGE->set_url('/repository/draftfiles_manager.php', $params); $filepicker_url = new moodle_url($CFG->httpswwwroot."/repository/filepicker.php", $params); $params['action'] = 'browse'; $home_url = new moodle_url('/repository/draftfiles_manager.php', $params); switch ($action) { // delete draft files case 'deletedraft': if ($file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename)) { if ($file->is_directory()) { $pathname = $draftpath; if ($file->get_parent_directory()) { $draftpath = $file->get_parent_directory()->get_filepath(); } else { $draftpath = '/'; } // delete files in folder $files = $fs->get_directory_files($user_context->id, 'user', 'draft', $itemid, $pathname, true); foreach ($files as $storedfile) { $storedfile->delete(); } $file->delete(); } else { $file->delete(); } $home_url->param('draftpath', $draftpath); $home_url->param('action', 'browse'); redirect($home_url); } break; case 'renameform': echo $OUTPUT->header(); echo '
'.get_string('back', 'repository')."
"; $home_url->param('draftpath', $draftpath); $home_url->param('action', 'rename'); echo '
'; echo ' '; echo ' '; echo ' '; echo ' '; echo '
'; echo $OUTPUT->footer(); break; case 'rename': if ($fs->file_exists($user_context->id, 'user', 'draft', $itemid, $draftpath, $newfilename)) { print_error('fileexists'); } else if ($file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename)) { $newfile = $fs->create_file_from_storedfile(array('filename'=>$newfilename), $file); $file->delete(); } $home_url->param('action', 'browse'); $home_url->param('draftpath', $draftpath); redirect($home_url); break; case 'downloaddir': $zipper = new zip_packer(); $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, '.'); if ($file->get_parent_directory()) { $parent_path = $file->get_parent_directory()->get_filepath(); $filename = trim($draftpath, '/').'.zip'; } else { $parent_path = '/'; $filename = get_string('files').'.zip'; } if ($newfile = $zipper->archive_to_storage(array($file), $user_context->id, 'user', 'draft', $itemid, $parent_path, $filename, $USER->id)) { $fileurl = moodle_url::make_draftfile_url($itemid, '/', $filename)->out(); header('Location: ' . $fileurl ); } else { print_error('cannotdownloaddir', 'repository'); } break; case 'zip': $zipper = new zip_packer(); $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, '.'); if (!$file->get_parent_directory()) { $parent_path = '/'; $filename = get_string('files').'.zip'; } else { $parent_path = $file->get_parent_directory()->get_filepath(); $filepath = explode('/', trim($file->get_filepath(), '/')); $filename = array_pop($filepath).'.zip'; } $newfile = $zipper->archive_to_storage(array($file), $user_context->id, 'user', 'draft', $itemid, $parent_path, $filename, $USER->id); $home_url->param('action', 'browse'); $home_url->param('draftpath', $parent_path); redirect($home_url, get_string('ziped', 'repository')); break; case 'unzip': $zipper = new zip_packer(); $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename); if ($newfile = $file->extract_to_storage($zipper, $user_context->id, 'user', 'draft', $itemid, $draftpath, $USER->id)) { $str = get_string('unzipped', 'repository'); } else { $str = get_string('cannotunzip', 'error'); } $home_url->param('action', 'browse'); $home_url->param('draftpath', $draftpath); redirect($home_url, $str); break; case 'movefile': if (!empty($targetpath)) { if ($fs->file_exists($user_context->id, 'user', 'draft', $itemid, $targetpath, $filename)) { print_error('cannotmovefile'); } else if ($file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename)) { $newfile = $fs->create_file_from_storedfile(array('filepath'=>$targetpath), $file); $file->delete(); } else { var_dump('cannot find file'); die; } $home_url->param('action', 'browse'); $home_url->param('draftpath', $targetpath); redirect($home_url); } echo $OUTPUT->header(); echo $OUTPUT->container_start(); echo html_writer::link($home_url, get_string('back', 'repository')); echo $OUTPUT->container_end(); $data = new stdClass(); $home_url->param('action', 'movefile'); $home_url->param('draftpath', $draftpath); $home_url->param('filename', $filename); file_get_drafarea_folders($itemid, '/', $data); print_draft_area_tree($data, true, $home_url); echo $OUTPUT->footer(); break; case 'mkdirform': echo $OUTPUT->header(); echo $OUTPUT->container_start(); echo html_writer::link($home_url, get_string('back', 'repository')); echo $OUTPUT->container_end(); $home_url->param('draftpath', $draftpath); $home_url->param('action', 'mkdir'); echo '
'; echo ' '; echo ' '; echo ' '; echo '
'; echo $OUTPUT->footer(); break; case 'mkdir': $newfolderpath = $draftpath . trim($newdirname, '/') . '/'; $fs->create_directory($user_context->id, 'user', 'draft', $itemid, $newfolderpath); $home_url->param('action', 'browse'); if (!empty($newdirname)) { $home_url->param('draftpath', $newfolderpath); $str = get_string('createfoldersuccess', 'repository'); } else { $home_url->param('draftpath', $draftpath); $str = get_string('createfolderfail', 'repository'); } redirect($home_url, $str); break; case 'browse': default: $files = file_get_drafarea_files($itemid, $draftpath); $info = file_get_draft_area_info($itemid); $filecount = $info['filecount']; echo $OUTPUT->header(); if ((!empty($files) or $draftpath != '/') and $env == 'filemanager') { echo '
'; $home_url->param('action', 'browse'); $home_url->param('draftpath', '/'); echo '' . get_string('files') . ' ▶'; $trail = ''; if ($draftpath !== '/') { $path = '/' . trim($draftpath, '/') . '/'; $parts = explode('/', $path); foreach ($parts as $part) { if (!empty($part)) { $trail .= ('/'.$part.'/'); $data->path[] = array('name'=>$part, 'path'=>$trail); $home_url->param('draftpath', $trail); echo ' '.$part.' ▶ '; } } } echo '
'; } $filepicker_url->param('draftpath', $draftpath); $filepicker_url->param('savepath', $draftpath); $filepicker_url->param('action', 'plugins'); echo '
'; if ($env == 'filepicker') { $maxfiles = 1; } if ($filecount < $maxfiles || $maxfiles == -1) { echo ' '.get_string('addfile', 'repository').''; } if ($env == 'filemanager') { if (!empty($subdirs)) { $home_url->param('action', 'mkdirform'); echo ' '.get_string('makeafolder', 'moodle').''; } $home_url->param('action', 'downloaddir'); echo html_writer::link($home_url, get_string('downloadfolder', 'repository'), array('target'=>'_blank')); } echo '
'; if (!empty($files->list)) { echo ''; } else { echo get_string('nofilesavailable', 'repository'); } echo $OUTPUT->footer(); break; } function print_draft_area_tree($tree, $root, $url) { echo ''; }