PageRenderTime 56ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/moodle/repository/draftfiles_manager.php

#
PHP | 393 lines | 300 code | 56 blank | 37 comment | 55 complexity | 27934dd8c38ded84de13b4655b795bfd MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, BSD-3-Clause, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, Apache-2.0
  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 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. // draft area
  49. $newdirname = optional_param('newdirname', '', PARAM_FILE);
  50. $newfilename = optional_param('newfilename', '', PARAM_FILE);
  51. // path in draft area
  52. $draftpath = optional_param('draftpath', '/', PARAM_PATH);
  53. // user context
  54. $user_context = get_context_instance(CONTEXT_USER, $USER->id);
  55. $PAGE->set_context($user_context);
  56. $fs = get_file_storage();
  57. $params = array('ctx_id' => $contextid, 'itemid' => $itemid, 'env' => $env, 'course'=>$courseid, 'maxbytes'=>$maxbytes, 'maxfiles'=>$maxfiles, 'subdirs'=>$subdirs, 'sesskey'=>sesskey());
  58. $PAGE->set_url('/repository/draftfiles_manager.php', $params);
  59. $filepicker_url = new moodle_url($CFG->httpswwwroot."/repository/filepicker.php", $params);
  60. $params['action'] = 'browse';
  61. $home_url = new moodle_url('/repository/draftfiles_manager.php', $params);
  62. switch ($action) {
  63. // delete draft files
  64. case 'deletedraft':
  65. if ($file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename)) {
  66. if ($file->is_directory()) {
  67. $pathname = $draftpath;
  68. if ($file->get_parent_directory()) {
  69. $draftpath = $file->get_parent_directory()->get_filepath();
  70. } else {
  71. $draftpath = '/';
  72. }
  73. // delete files in folder
  74. $files = $fs->get_directory_files($user_context->id, 'user', 'draft', $itemid, $pathname, true);
  75. foreach ($files as $storedfile) {
  76. $storedfile->delete();
  77. }
  78. $file->delete();
  79. } else {
  80. $file->delete();
  81. }
  82. $home_url->param('draftpath', $draftpath);
  83. $home_url->param('action', 'browse');
  84. redirect($home_url);
  85. }
  86. break;
  87. case 'renameform':
  88. echo $OUTPUT->header();
  89. echo '<div><a href="' . $home_url->out() . '">'.get_string('back', 'repository')."</a></div>";
  90. $home_url->param('draftpath', $draftpath);
  91. $home_url->param('action', 'rename');
  92. echo ' <form method="post" action="'.$home_url->out().'">';
  93. echo ' <input name="newfilename" type="text" value="'.s($filename).'" />';
  94. echo ' <input name="filename" type="hidden" value="'.s($filename).'" />';
  95. echo ' <input name="draftpath" type="hidden" value="'.s($draftpath).'" />';
  96. echo ' <input type="submit" value="'.s(get_string('rename', 'moodle')).'" />';
  97. echo ' </form>';
  98. echo $OUTPUT->footer();
  99. break;
  100. case 'rename':
  101. if ($fs->file_exists($user_context->id, 'user', 'draft', $itemid, $draftpath, $newfilename)) {
  102. print_error('fileexists');
  103. } else if ($file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename)) {
  104. $newfile = $fs->create_file_from_storedfile(array('filename'=>$newfilename), $file);
  105. $file->delete();
  106. }
  107. $home_url->param('action', 'browse');
  108. $home_url->param('draftpath', $draftpath);
  109. redirect($home_url);
  110. break;
  111. case 'downloaddir':
  112. $zipper = new zip_packer();
  113. $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, '.');
  114. if ($file->get_parent_directory()) {
  115. $parent_path = $file->get_parent_directory()->get_filepath();
  116. $filename = trim($draftpath, '/').'.zip';
  117. } else {
  118. $parent_path = '/';
  119. $filename = get_string('files').'.zip';
  120. }
  121. if ($newfile = $zipper->archive_to_storage(array($file), $user_context->id, 'user', 'draft', $itemid, $parent_path, $filename, $USER->id)) {
  122. $fileurl = moodle_url::make_draftfile_url($itemid, '/', $filename)->out();
  123. header('Location: ' . $fileurl );
  124. } else {
  125. print_error('cannotdownloaddir', 'repository');
  126. }
  127. break;
  128. case 'zip':
  129. $zipper = new zip_packer();
  130. $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, '.');
  131. if (!$file->get_parent_directory()) {
  132. $parent_path = '/';
  133. $filename = get_string('files').'.zip';
  134. } else {
  135. $parent_path = $file->get_parent_directory()->get_filepath();
  136. $filepath = explode('/', trim($file->get_filepath(), '/'));
  137. $filename = array_pop($filepath).'.zip';
  138. }
  139. $newfile = $zipper->archive_to_storage(array($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. if ($fs->file_exists($user_context->id, 'user', 'draft', $itemid, $targetpath, $filename)) {
  159. print_error('cannotmovefile');
  160. } else if ($file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename)) {
  161. $newfile = $fs->create_file_from_storedfile(array('filepath'=>$targetpath), $file);
  162. $file->delete();
  163. } else {
  164. var_dump('cannot find file');
  165. die;
  166. }
  167. $home_url->param('action', 'browse');
  168. $home_url->param('draftpath', $targetpath);
  169. redirect($home_url);
  170. }
  171. echo $OUTPUT->header();
  172. echo $OUTPUT->container_start();
  173. echo html_writer::link($home_url, get_string('back', 'repository'));
  174. echo $OUTPUT->container_end();
  175. $data = new stdClass();
  176. $home_url->param('action', 'movefile');
  177. $home_url->param('draftpath', $draftpath);
  178. $home_url->param('filename', $filename);
  179. file_get_drafarea_folders($itemid, '/', $data);
  180. print_draft_area_tree($data, true, $home_url);
  181. echo $OUTPUT->footer();
  182. break;
  183. case 'mkdirform':
  184. echo $OUTPUT->header();
  185. echo $OUTPUT->container_start();
  186. echo html_writer::link($home_url, get_string('back', 'repository'));
  187. echo $OUTPUT->container_end();
  188. $home_url->param('draftpath', $draftpath);
  189. $home_url->param('action', 'mkdir');
  190. echo ' <form method="post" action="'.$home_url->out().'">';
  191. echo ' <input name="newdirname" type="text" />';
  192. echo ' <input name="draftpath" type="hidden" value="'.s($draftpath).'" />';
  193. echo ' <input type="submit" value="'.s(get_string('makeafolder', 'moodle')).'" />';
  194. echo ' </form>';
  195. echo $OUTPUT->footer();
  196. break;
  197. case 'mkdir':
  198. $newfolderpath = $draftpath . trim($newdirname, '/') . '/';
  199. $fs->create_directory($user_context->id, 'user', 'draft', $itemid, $newfolderpath);
  200. $home_url->param('action', 'browse');
  201. if (!empty($newdirname)) {
  202. $home_url->param('draftpath', $newfolderpath);
  203. $str = get_string('createfoldersuccess', 'repository');
  204. } else {
  205. $home_url->param('draftpath', $draftpath);
  206. $str = get_string('createfolderfail', 'repository');
  207. }
  208. redirect($home_url, $str);
  209. break;
  210. case 'browse':
  211. default:
  212. $files = file_get_drafarea_files($itemid, $draftpath);
  213. $info = file_get_draft_area_info($itemid);
  214. $filecount = $info['filecount'];
  215. echo $OUTPUT->header();
  216. if ((!empty($files) or $draftpath != '/') and $env == 'filemanager') {
  217. echo '<div class="fm-breadcrumb">';
  218. $home_url->param('action', 'browse');
  219. $home_url->param('draftpath', '/');
  220. echo '<a href="'.$home_url->out().'">' . get_string('files') . '</a> â&#x2013;?';
  221. $trail = '';
  222. if ($draftpath !== '/') {
  223. $path = '/' . trim($draftpath, '/') . '/';
  224. $parts = explode('/', $path);
  225. foreach ($parts as $part) {
  226. if (!empty($part)) {
  227. $trail .= ('/'.$part.'/');
  228. $data->path[] = array('name'=>$part, 'path'=>$trail);
  229. $home_url->param('draftpath', $trail);
  230. echo ' <a href="'.$home_url->out().'">'.$part.'</a> â&#x2013;? ';
  231. }
  232. }
  233. }
  234. echo '</div>';
  235. }
  236. $filepicker_url->param('draftpath', $draftpath);
  237. $filepicker_url->param('savepath', $draftpath);
  238. $filepicker_url->param('action', 'plugins');
  239. echo '<div class="filemanager-toolbar">';
  240. if ($env == 'filepicker') {
  241. $maxfiles = 1;
  242. }
  243. if ($filecount < $maxfiles || $maxfiles == -1) {
  244. echo ' <a href="'.$filepicker_url->out().'">'.get_string('addfile', 'repository').'</a>';
  245. }
  246. if ($env == 'filemanager') {
  247. if (!empty($subdirs)) {
  248. $home_url->param('action', 'mkdirform');
  249. echo ' <a href="'.$home_url->out().'">'.get_string('makeafolder', 'moodle').'</a>';
  250. }
  251. $home_url->param('action', 'downloaddir');
  252. echo html_writer::link($home_url, get_string('downloadfolder', 'repository'), array('target'=>'_blank'));
  253. }
  254. echo '</div>';
  255. if (!empty($files->list)) {
  256. echo '<ul>';
  257. foreach ($files->list as $file) {
  258. if ($file->type != 'folder') {
  259. $drafturl = $file->url;
  260. // a file
  261. $fileicon = $OUTPUT->pix_url(file_extension_icon($file->filename))->out(false);
  262. $type = mimeinfo('icon', $file->filename);
  263. echo '<li>';
  264. echo '<img src="'.$fileicon. '" class="iconsmall" />';
  265. echo html_writer::link($drafturl, $file->filename);
  266. $home_url->param('filename', $file->filename);
  267. $home_url->param('draftpath', $file->filepath);
  268. $home_url->param('action', 'deletedraft');
  269. echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('delete').'</a>]';
  270. $home_url->param('action', 'movefile');
  271. echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('move').'</a>]';
  272. $home_url->param('action', 'renameform');
  273. echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('rename').'</a>]';
  274. if ($type == 'zip') {
  275. $home_url->param('action', 'unzip');
  276. $home_url->param('draftpath', $file->filepath);
  277. echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('unzip').'</a>]';
  278. }
  279. echo '</li>';
  280. } else {
  281. // a folder
  282. echo '<li>';
  283. echo '<img src="'.$OUTPUT->pix_url('f/folder') . '" class="iconsmall" />';
  284. $home_url->param('action', 'browse');
  285. $home_url->param('draftpath', $file->filepath);
  286. $foldername = trim(array_pop(explode('/', trim($file->filepath, '/'))), '/');
  287. echo html_writer::link($home_url, $foldername);
  288. $home_url->param('draftpath', $file->filepath);
  289. $home_url->param('filename', $file->filename);
  290. $home_url->param('action', 'deletedraft');
  291. echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('delete').'</a>]';
  292. $home_url->param('action', 'zip');
  293. echo ' [<a href="'.$home_url->out().'" class="fm-operation">Zip</a>]';
  294. echo '</li>';
  295. }
  296. }
  297. echo '</ul>';
  298. } else {
  299. echo get_string('nofilesavailable', 'repository');
  300. }
  301. echo $OUTPUT->footer();
  302. break;
  303. }
  304. function print_draft_area_tree($tree, $root, $url) {
  305. echo '<ul>';
  306. if ($root) {
  307. $url->param('targetpath', '/');
  308. if ($url->param('draftpath') == '/') {
  309. echo '<li>'.get_string('files').'</li>';
  310. } else {
  311. echo '<li><a href="'.$url->out().'">'.get_string('files').'</a></li>';
  312. }
  313. echo '<ul>';
  314. if (isset($tree->children)) {
  315. $tree = $tree->children;
  316. }
  317. }
  318. if (!empty($tree)) {
  319. foreach ($tree as $node) {
  320. echo '<li>';
  321. $url->param('targetpath', $node->filepath);
  322. if ($url->param('draftpath') != $node->filepath) {
  323. echo '<a href="'.$url->out().'">'.$node->fullname.'</a>';
  324. } else {
  325. echo $node->fullname;
  326. }
  327. echo '</li>';
  328. if (!empty($node->children)) {
  329. print_draft_area_tree($node->children, false, $url);
  330. }
  331. }
  332. }
  333. if ($root) {
  334. echo '</ul>';
  335. }
  336. echo '</ul>';
  337. }