PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/repository/draftfiles_ajax.php

http://github.com/moodle/moodle
PHP | 288 lines | 213 code | 39 blank | 36 comment | 22 complexity | 843f60ac1c24f4c00e1421a126bed5e3 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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. * Draft file ajax file manager
  18. *
  19. * @package core
  20. * @subpackage repository
  21. * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. define('AJAX_SCRIPT', true);
  25. require('../config.php');
  26. require_once($CFG->libdir.'/filelib.php');
  27. require_once($CFG->libdir.'/adminlib.php');
  28. require_once($CFG->dirroot.'/repository/lib.php');
  29. $PAGE->set_context(context_system::instance());
  30. require_login();
  31. if (isguestuser()) {
  32. print_error('noguest');
  33. }
  34. require_sesskey();
  35. $action = required_param('action', PARAM_ALPHA);
  36. $draftid = required_param('itemid', PARAM_INT);
  37. $filepath = optional_param('filepath', '/', PARAM_PATH);
  38. $usercontext = context_user::instance($USER->id);
  39. echo $OUTPUT->header(); // send headers
  40. //
  41. //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!!
  42. //
  43. switch ($action) {
  44. case 'dir':
  45. $data = new stdClass();
  46. file_get_drafarea_folders($draftid, $filepath, $data);
  47. echo json_encode($data);
  48. die;
  49. case 'list':
  50. $filepath = optional_param('filepath', '/', PARAM_PATH);
  51. $data = repository::prepare_listing(file_get_drafarea_files($draftid, $filepath));
  52. $info = file_get_draft_area_info($draftid);
  53. $data->filecount = $info['filecount'];
  54. $data->filesize = $info['filesize'];
  55. $data->tree = new stdClass();
  56. file_get_drafarea_folders($draftid, '/', $data->tree);
  57. echo json_encode($data);
  58. die;
  59. case 'mkdir':
  60. $filepath = required_param('filepath', PARAM_PATH);
  61. $newdirname = required_param('newdirname', PARAM_FILE);
  62. $fs = get_file_storage();
  63. $fs->create_directory($usercontext->id, 'user', 'draft', $draftid, file_correct_filepath(file_correct_filepath($filepath).$newdirname));
  64. $return = new stdClass();
  65. $return->filepath = $filepath;
  66. echo json_encode($return);
  67. die;
  68. case 'delete':
  69. $filename = required_param('filename', PARAM_FILE);
  70. $filepath = required_param('filepath', PARAM_PATH);
  71. $selectedfile = (object)[
  72. 'filename' => $filename,
  73. 'filepath' => $filepath
  74. ];
  75. $return = repository_delete_selected_files($usercontext, 'user', 'draft', $draftid, [$selectedfile]);
  76. if ($return) {
  77. $response = new stdClass();
  78. $response->filepath = array_keys($return)[0];
  79. echo json_encode($response);
  80. die;
  81. }
  82. echo json_encode(false);
  83. die;
  84. case 'deleteselected':
  85. $selected = required_param('selected', PARAM_RAW);
  86. $return = [];
  87. $selectedfiles = json_decode($selected);
  88. $return = repository_delete_selected_files($usercontext, 'user', 'draft', $draftid, $selectedfiles);
  89. echo (json_encode($return ? array_keys($return) : false));
  90. die;
  91. case 'setmainfile':
  92. $filename = required_param('filename', PARAM_FILE);
  93. $filepath = required_param('filepath', PARAM_PATH);
  94. $filepath = file_correct_filepath($filepath);
  95. // reset sort order
  96. file_reset_sortorder($usercontext->id, 'user', 'draft', $draftid);
  97. // set main file
  98. $return = file_set_sortorder($usercontext->id, 'user', 'draft', $draftid, $filepath, $filename, 1);
  99. echo json_encode($return);
  100. die;
  101. case 'updatefile':
  102. // Allows to Rename file, move it to another directory, change it's license and author information in one request
  103. $filename = required_param('filename', PARAM_FILE);
  104. $filepath = required_param('filepath', PARAM_PATH);
  105. $updatedata = array();
  106. $updatedata['filename'] = optional_param('newfilename', $filename, PARAM_FILE);
  107. $updatedata['filepath'] = $newfilepath = optional_param('newfilepath', $filepath, PARAM_PATH);
  108. if (($v = optional_param('newlicense', false, PARAM_TEXT)) !== false) {
  109. $updatedata['license'] = $v;
  110. }
  111. if (($v = optional_param('newauthor', false, PARAM_TEXT)) !== false) {
  112. $updatedata['author'] = $v;
  113. }
  114. try {
  115. repository::update_draftfile($draftid, $filepath, $filename, $updatedata);
  116. } catch (moodle_exception $e) {
  117. die(json_encode((object)array('error' => $e->getMessage())));
  118. }
  119. die(json_encode((object)array('filepath' => $newfilepath)));
  120. case 'updatedir':
  121. $filepath = required_param('filepath', PARAM_PATH);
  122. $newdirname = required_param('newdirname', PARAM_FILE);
  123. $parent = required_param('newfilepath', PARAM_PATH);
  124. $newfilepath = clean_param($parent . '/' . $newdirname . '/', PARAM_PATH);
  125. try {
  126. repository::update_draftfile($draftid, $filepath, '.', array('filepath' => $newfilepath));
  127. } catch (moodle_exception $e) {
  128. die(json_encode((object)array('error' => $e->getMessage())));
  129. }
  130. die(json_encode((object)array('filepath' => $parent)));
  131. case 'zip':
  132. $filepath = required_param('filepath', PARAM_PATH);
  133. $zipper = get_file_packer('application/zip');
  134. $fs = get_file_storage();
  135. $file = $fs->get_file($usercontext->id, 'user', 'draft', $draftid, $filepath, '.');
  136. $parent_path = $file->get_parent_directory()->get_filepath();
  137. $filepath = explode('/', trim($file->get_filepath(), '/'));
  138. $filepath = array_pop($filepath);
  139. $zipfile = repository::get_unused_filename($draftid, $parent_path, $filepath . '.zip');
  140. if ($newfile = $zipper->archive_to_storage([$filepath => $file], $usercontext->id, 'user', 'draft', $draftid, $parent_path, $zipfile, $USER->id)) {
  141. $return = new stdClass();
  142. $return->filepath = $parent_path;
  143. echo json_encode($return);
  144. } else {
  145. echo json_encode(false);
  146. }
  147. die;
  148. case 'downloadselected':
  149. $selected = required_param('selected', PARAM_RAW);
  150. $selectedfiles = json_decode($selected);
  151. $return = repository_download_selected_files($usercontext, 'user', 'draft', $draftid, $selectedfiles);
  152. echo (json_encode($return));
  153. die;
  154. case 'downloaddir':
  155. $filepath = required_param('filepath', PARAM_PATH);
  156. $selectedfile = (object)[
  157. 'filename' => '',
  158. 'filepath' => $filepath
  159. ];
  160. $return = repository_download_selected_files($usercontext, 'user', 'draft', $draftid, [$selectedfile]);
  161. echo json_encode($return);
  162. die;
  163. case 'unzip':
  164. $filename = required_param('filename', PARAM_FILE);
  165. $filepath = required_param('filepath', PARAM_PATH);
  166. $zipper = get_file_packer('application/zip');
  167. $fs = get_file_storage();
  168. $file = $fs->get_file($usercontext->id, 'user', 'draft', $draftid, $filepath, $filename);
  169. // Find unused name for directory to extract the archive.
  170. $temppath = $fs->get_unused_dirname($usercontext->id, 'user', 'draft', $draftid, $filepath. pathinfo($filename, PATHINFO_FILENAME). '/');
  171. $donotremovedirs = array();
  172. $doremovedirs = array($temppath);
  173. // Extract archive and move all files from $temppath to $filepath
  174. if ($file->extract_to_storage($zipper, $usercontext->id, 'user', 'draft', $draftid, $temppath, $USER->id) !== false) {
  175. $extractedfiles = $fs->get_directory_files($usercontext->id, 'user', 'draft', $draftid, $temppath, true);
  176. $xtemppath = preg_quote($temppath, '|');
  177. foreach ($extractedfiles as $file) {
  178. $realpath = preg_replace('|^'.$xtemppath.'|', $filepath, $file->get_filepath());
  179. if (!$file->is_directory()) {
  180. // Set the source to the extracted file to indicate that it came from archive.
  181. $file->set_source(serialize((object)array('source' => $filepath)));
  182. }
  183. if (!$fs->file_exists($usercontext->id, 'user', 'draft', $draftid, $realpath, $file->get_filename())) {
  184. // File or directory did not exist, just move it.
  185. $file->rename($realpath, $file->get_filename());
  186. } else if (!$file->is_directory()) {
  187. // File already existed, overwrite it
  188. repository::overwrite_existing_draftfile($draftid, $realpath, $file->get_filename(), $file->get_filepath(), $file->get_filename());
  189. } else {
  190. // Directory already existed, remove temporary dir but make sure we don't remove the existing dir
  191. $doremovedirs[] = $file->get_filepath();
  192. $donotremovedirs[] = $realpath;
  193. }
  194. }
  195. $return = new stdClass();
  196. $return->filepath = $filepath;
  197. } else {
  198. $return = false;
  199. }
  200. // Remove remaining temporary directories.
  201. foreach (array_diff($doremovedirs, $donotremovedirs) as $filepath) {
  202. if ($file = $fs->get_file($usercontext->id, 'user', 'draft', $draftid, $filepath, '.')) {
  203. $file->delete();
  204. }
  205. }
  206. die(json_encode($return));
  207. case 'getoriginal':
  208. $filename = required_param('filename', PARAM_FILE);
  209. $filepath = required_param('filepath', PARAM_PATH);
  210. $fs = get_file_storage();
  211. $file = $fs->get_file($usercontext->id, 'user', 'draft', $draftid, $filepath, $filename);
  212. if (!$file) {
  213. echo json_encode(false);
  214. } else {
  215. $return = array('filename' => $filename, 'filepath' => $filepath, 'original' => $file->get_reference_details());
  216. echo json_encode((object)$return);
  217. }
  218. die;
  219. case 'getreferences':
  220. $filename = required_param('filename', PARAM_FILE);
  221. $filepath = required_param('filepath', PARAM_PATH);
  222. $fs = get_file_storage();
  223. $file = $fs->get_file($usercontext->id, 'user', 'draft', $draftid, $filepath, $filename);
  224. if (!$file) {
  225. echo json_encode(false);
  226. } else {
  227. $source = unserialize($file->get_source());
  228. $return = array('filename' => $filename, 'filepath' => $filepath, 'references' => array());
  229. $browser = get_file_browser();
  230. if (isset($source->original)) {
  231. $reffiles = $fs->search_references($source->original);
  232. foreach ($reffiles as $reffile) {
  233. $refcontext = context::instance_by_id($reffile->get_contextid());
  234. $fileinfo = $browser->get_file_info($refcontext, $reffile->get_component(), $reffile->get_filearea(), $reffile->get_itemid(), $reffile->get_filepath(), $reffile->get_filename());
  235. if (empty($fileinfo)) {
  236. $return['references'][] = get_string('undisclosedreference', 'repository');
  237. } else {
  238. $return['references'][] = $fileinfo->get_readable_fullname();
  239. }
  240. }
  241. }
  242. echo json_encode((object)$return);
  243. }
  244. die;
  245. default:
  246. // no/unknown action?
  247. echo json_encode(false);
  248. die;
  249. }