PageRenderTime 57ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/repository/draftfiles_manager.php

https://gitlab.com/unofficial-mirrors/moodle
PHP | 385 lines | 294 code | 54 blank | 37 comment | 49 complexity | f7394029f829f70bc1c208f57696b72d MD5 | raw file
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. //
  17. //
  18. // 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!!
  19. //
  20. /**
  21. * This file is used to manage draft files in non-javascript browsers
  22. *
  23. * @since Moodle 2.0
  24. * @package core
  25. * @subpackage repository
  26. * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
  27. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28. */
  29. require_once('../config.php');
  30. require_once($CFG->libdir.'/filelib.php');
  31. require_once('lib.php');
  32. require_sesskey();
  33. require_login();
  34. // disable blocks in this page
  35. $PAGE->set_pagelayout('embedded');
  36. // general parameters
  37. $action = optional_param('action', '', PARAM_ALPHA);
  38. $itemid = optional_param('itemid', '', PARAM_INT);
  39. // parameters for repository
  40. $contextid = optional_param('ctx_id', SYSCONTEXTID, PARAM_INT); // context ID
  41. $courseid = optional_param('course', SITEID, PARAM_INT); // course ID
  42. $env = optional_param('env', 'filepicker', PARAM_ALPHA); // opened in file picker, file manager or html editor
  43. $filename = optional_param('filename', '', PARAM_FILE);
  44. $targetpath = optional_param('targetpath', '', PARAM_PATH);
  45. $maxfiles = optional_param('maxfiles', -1, PARAM_INT); // maxfiles
  46. $maxbytes = optional_param('maxbytes', 0, PARAM_INT); // maxbytes
  47. $subdirs = optional_param('subdirs', 0, PARAM_INT); // maxbytes
  48. $areamaxbytes = optional_param('areamaxbytes', FILE_AREA_MAX_BYTES_UNLIMITED, PARAM_INT); // Area maxbytes.
  49. // draft area
  50. $newdirname = optional_param('newdirname', '', PARAM_FILE);
  51. $newfilename = optional_param('newfilename', '', PARAM_FILE);
  52. // path in draft area
  53. $draftpath = optional_param('draftpath', '/', PARAM_PATH);
  54. // user context
  55. $user_context = context_user::instance($USER->id);
  56. $PAGE->set_context($user_context);
  57. $fs = get_file_storage();
  58. $params = array('ctx_id' => $contextid, 'itemid' => $itemid, 'env' => $env, 'course'=>$courseid, 'maxbytes'=>$maxbytes, 'areamaxbytes'=>$areamaxbytes, 'maxfiles'=>$maxfiles, 'subdirs'=>$subdirs, 'sesskey'=>sesskey());
  59. $PAGE->set_url('/repository/draftfiles_manager.php', $params);
  60. $filepicker_url = new moodle_url("/repository/filepicker.php", $params);
  61. $params['action'] = 'browse';
  62. $home_url = new moodle_url('/repository/draftfiles_manager.php', $params);
  63. switch ($action) {
  64. // delete draft files
  65. case 'deletedraft':
  66. if ($file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename)) {
  67. if ($file->is_directory()) {
  68. $pathname = $draftpath;
  69. if ($file->get_parent_directory()) {
  70. $draftpath = $file->get_parent_directory()->get_filepath();
  71. } else {
  72. $draftpath = '/';
  73. }
  74. // delete files in folder
  75. $files = $fs->get_directory_files($user_context->id, 'user', 'draft', $itemid, $pathname, true);
  76. foreach ($files as $storedfile) {
  77. $storedfile->delete();
  78. }
  79. $file->delete();
  80. } else {
  81. $file->delete();
  82. }
  83. $home_url->param('draftpath', $draftpath);
  84. $home_url->param('action', 'browse');
  85. redirect($home_url);
  86. }
  87. break;
  88. case 'renameform':
  89. echo $OUTPUT->header();
  90. echo '<div><a href="' . $home_url->out() . '">'.get_string('back', 'repository')."</a></div>";
  91. $home_url->param('draftpath', $draftpath);
  92. $home_url->param('action', 'rename');
  93. echo ' <form method="post" action="'.$home_url->out().'">';
  94. echo html_writer::label(get_string('enternewname', 'repository'), 'newfilename', array('class' => 'accesshide'));
  95. echo ' <input id="newfilename" name="newfilename" type="text" value="'.s($filename).'" />';
  96. echo ' <input name="filename" type="hidden" value="'.s($filename).'" />';
  97. echo ' <input name="draftpath" type="hidden" value="'.s($draftpath).'" />';
  98. echo ' <input type="submit" value="'.s(get_string('rename', 'moodle')).'" />';
  99. echo ' </form>';
  100. echo $OUTPUT->footer();
  101. break;
  102. case 'rename':
  103. repository::update_draftfile($itemid, $draftpath, $filename, array('filename' => $newfilename));
  104. $home_url->param('action', 'browse');
  105. $home_url->param('draftpath', $draftpath);
  106. redirect($home_url);
  107. break;
  108. case 'downloaddir':
  109. $zipper = new zip_packer();
  110. $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, '.');
  111. if ($draftpath === '/') {
  112. $filename = get_string('files').'.zip';
  113. } else {
  114. $filename = explode('/', trim($draftpath, '/'));
  115. $filename = array_pop($filename) . '.zip';
  116. }
  117. $newdraftitemid = file_get_unused_draft_itemid();
  118. if ($newfile = $zipper->archive_to_storage(array('/' => $file), $user_context->id, 'user', 'draft', $newdraftitemid, '/', $filename, $USER->id)) {
  119. $fileurl = moodle_url::make_draftfile_url($newdraftitemid, '/', $filename)->out();
  120. header('Location: ' . $fileurl);
  121. } else {
  122. print_error('cannotdownloaddir', 'repository');
  123. }
  124. break;
  125. case 'zip':
  126. $zipper = new zip_packer();
  127. $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, '.');
  128. if (!$file->get_parent_directory()) {
  129. $parent_path = '/';
  130. $filepath = '/';
  131. $filename = get_string('files').'.zip';
  132. } else {
  133. $parent_path = $file->get_parent_directory()->get_filepath();
  134. $filepath = explode('/', trim($file->get_filepath(), '/'));
  135. $filepath = array_pop($filepath);
  136. $filename = $filepath.'.zip';
  137. }
  138. $filename = repository::get_unused_filename($itemid, $parent_path, $filename);
  139. $newfile = $zipper->archive_to_storage(array($filepath => $file), $user_context->id, 'user', 'draft', $itemid, $parent_path, $filename, $USER->id);
  140. $home_url->param('action', 'browse');
  141. $home_url->param('draftpath', $parent_path);
  142. redirect($home_url, get_string('ziped', 'repository'));
  143. break;
  144. case 'unzip':
  145. $zipper = new zip_packer();
  146. $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename);
  147. if ($newfile = $file->extract_to_storage($zipper, $user_context->id, 'user', 'draft', $itemid, $draftpath, $USER->id)) {
  148. $str = get_string('unzipped', 'repository');
  149. } else {
  150. $str = get_string('cannotunzip', 'error');
  151. }
  152. $home_url->param('action', 'browse');
  153. $home_url->param('draftpath', $draftpath);
  154. redirect($home_url, $str);
  155. break;
  156. case 'movefile':
  157. if (!empty($targetpath)) {
  158. repository::update_draftfile($itemid, $draftpath, $filename, array('filepath' => $targetpath));
  159. $home_url->param('action', 'browse');
  160. $home_url->param('draftpath', $targetpath);
  161. redirect($home_url);
  162. }
  163. echo $OUTPUT->header();
  164. echo $OUTPUT->container_start();
  165. echo html_writer::link($home_url, get_string('back', 'repository'));
  166. echo $OUTPUT->container_end();
  167. $data = new stdClass();
  168. $home_url->param('action', 'movefile');
  169. $home_url->param('draftpath', $draftpath);
  170. $home_url->param('filename', $filename);
  171. file_get_drafarea_folders($itemid, '/', $data);
  172. print_draft_area_tree($data, true, $home_url);
  173. echo $OUTPUT->footer();
  174. break;
  175. case 'mkdirform':
  176. echo $OUTPUT->header();
  177. echo $OUTPUT->container_start();
  178. echo html_writer::link($home_url, get_string('back', 'repository'));
  179. echo $OUTPUT->container_end();
  180. $home_url->param('draftpath', $draftpath);
  181. $home_url->param('action', 'mkdir');
  182. echo ' <form method="post" action="'.$home_url->out().'">';
  183. echo html_writer::label(get_string('entername', 'repository'), 'newdirname', array('class' => 'accesshide'));
  184. echo ' <input name="newdirname" id="newdirname" type="text" />';
  185. echo ' <input name="draftpath" type="hidden" value="'.s($draftpath).'" />';
  186. echo ' <input type="submit" value="'.s(get_string('makeafolder', 'moodle')).'" />';
  187. echo ' </form>';
  188. echo $OUTPUT->footer();
  189. break;
  190. case 'mkdir':
  191. $newfolderpath = $draftpath . trim($newdirname, '/') . '/';
  192. $fs->create_directory($user_context->id, 'user', 'draft', $itemid, $newfolderpath);
  193. $home_url->param('action', 'browse');
  194. if (!empty($newdirname)) {
  195. $home_url->param('draftpath', $newfolderpath);
  196. $str = get_string('createfoldersuccess', 'repository');
  197. } else {
  198. $home_url->param('draftpath', $draftpath);
  199. $str = get_string('createfolderfail', 'repository');
  200. }
  201. redirect($home_url, $str);
  202. break;
  203. case 'browse':
  204. default:
  205. $files = file_get_drafarea_files($itemid, $draftpath);
  206. $info = file_get_draft_area_info($itemid);
  207. $filecount = $info['filecount'];
  208. echo $OUTPUT->header();
  209. if ((!empty($files) or $draftpath != '/') and $env == 'filemanager') {
  210. echo '<div class="fm-breadcrumb">';
  211. $home_url->param('action', 'browse');
  212. $home_url->param('draftpath', '/');
  213. echo '<a href="'.$home_url->out().'">' . get_string('files') . '</a> ▶';
  214. $trail = '';
  215. if ($draftpath !== '/') {
  216. $path = '/' . trim($draftpath, '/') . '/';
  217. $parts = explode('/', $path);
  218. foreach ($parts as $part) {
  219. if ($part != '') {
  220. $trail .= ('/'.$part.'/');
  221. $data->path[] = array('name'=>$part, 'path'=>$trail);
  222. $home_url->param('draftpath', $trail);
  223. echo ' <a href="'.$home_url->out().'">'.$part.'</a> ▶ ';
  224. }
  225. }
  226. }
  227. echo '</div>';
  228. }
  229. $filepicker_url->param('draftpath', $draftpath);
  230. $filepicker_url->param('savepath', $draftpath);
  231. $filepicker_url->param('action', 'plugins');
  232. echo '<div class="filemanager-toolbar">';
  233. if ($env == 'filepicker') {
  234. $maxfiles = 1;
  235. }
  236. if ($filecount < $maxfiles || $maxfiles == -1) {
  237. echo ' <a href="'.$filepicker_url->out().'">'.get_string('addfile', 'repository').'</a>';
  238. }
  239. if ($env == 'filemanager') {
  240. if (!empty($subdirs)) {
  241. $home_url->param('action', 'mkdirform');
  242. echo ' <a href="'.$home_url->out().'">'.get_string('makeafolder', 'moodle').'</a>';
  243. }
  244. if (!empty($files->list)) {
  245. $home_url->param('action', 'downloaddir');
  246. echo ' ' . html_writer::link($home_url, get_string('downloadfolder', 'repository'), array('target'=>'_blank'));
  247. }
  248. }
  249. echo '</div>';
  250. if (!empty($files->list)) {
  251. echo '<ul>';
  252. foreach ($files->list as $file) {
  253. if ($file->type != 'folder') {
  254. $drafturl = $file->url;
  255. // a file
  256. echo '<li>';
  257. echo $OUTPUT->pix_icon(file_file_icon($file), '', 'moodle', array('class' => 'iconsmall'));
  258. echo html_writer::link($drafturl, $file->filename);
  259. $home_url->param('filename', $file->filename);
  260. $home_url->param('draftpath', $file->filepath);
  261. $home_url->param('action', 'deletedraft');
  262. echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('delete').'</a>]';
  263. $home_url->param('action', 'movefile');
  264. echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('move').'</a>]';
  265. $home_url->param('action', 'renameform');
  266. echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('rename').'</a>]';
  267. if (file_extension_in_typegroup($file->filename, 'archive', true)) {
  268. $home_url->param('action', 'unzip');
  269. $home_url->param('draftpath', $file->filepath);
  270. echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('unzip').'</a>]';
  271. }
  272. echo '</li>';
  273. } else {
  274. // a folder
  275. echo '<li>';
  276. echo $OUTPUT->pix_icon(file_folder_icon(), get_string('folder'));
  277. $home_url->param('action', 'browse');
  278. $home_url->param('draftpath', $file->filepath);
  279. $filepathchunks = explode('/', trim($file->filepath, '/'));
  280. $foldername = trim(array_pop($filepathchunks), '/');
  281. echo html_writer::link($home_url, $foldername);
  282. $home_url->param('draftpath', $file->filepath);
  283. $home_url->param('filename', $file->filename);
  284. $home_url->param('action', 'deletedraft');
  285. echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('delete').'</a>]';
  286. $home_url->param('action', 'zip');
  287. echo ' [<a href="'.$home_url->out().'" class="fm-operation">Zip</a>]';
  288. echo '</li>';
  289. }
  290. }
  291. echo '</ul>';
  292. } else {
  293. echo get_string('nofilesavailable', 'repository');
  294. }
  295. echo $OUTPUT->footer();
  296. break;
  297. }
  298. function print_draft_area_tree($tree, $root, $url) {
  299. echo '<ul>';
  300. if ($root) {
  301. $url->param('targetpath', '/');
  302. if ($url->param('draftpath') == '/') {
  303. echo '<li>'.get_string('files').'</li>';
  304. } else {
  305. echo '<li><a href="'.$url->out().'">'.get_string('files').'</a></li>';
  306. }
  307. echo '<ul>';
  308. if (isset($tree->children)) {
  309. $tree = $tree->children;
  310. }
  311. }
  312. if (!empty($tree)) {
  313. foreach ($tree as $node) {
  314. echo '<li>';
  315. $url->param('targetpath', $node->filepath);
  316. if ($url->param('draftpath') != $node->filepath) {
  317. echo '<a href="'.$url->out().'">'.$node->fullname.'</a>';
  318. } else {
  319. echo $node->fullname;
  320. }
  321. echo '</li>';
  322. if (!empty($node->children)) {
  323. print_draft_area_tree($node->children, false, $url);
  324. }
  325. }
  326. }
  327. if ($root) {
  328. echo '</ul>';
  329. }
  330. echo '</ul>';
  331. }