draftfile.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. * This script serves draft files of current user
  18. *
  19. * @package core
  20. * @subpackage file
  21. * @copyright 2008 Petr Skoda (http://skodak.org)
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. // disable moodle specific debug messages and any errors in output
  25. define('NO_DEBUG_DISPLAY', true);
  26. require_once('config.php');
  27. require_once('lib/filelib.php');
  28. require_login();
  29. if (isguestuser()) {
  30. print_error('noguest');
  31. }
  32. $relativepath = get_file_argument();
  33. $preview = optional_param('preview', null, PARAM_ALPHANUM);
  34. // relative path must start with '/'
  35. if (!$relativepath) {
  36. print_error('invalidargorconf');
  37. } else if ($relativepath{0} != '/') {
  38. print_error('pathdoesnotstartslash');
  39. }
  40. // extract relative path components
  41. $args = explode('/', ltrim($relativepath, '/'));
  42. if (count($args) == 0) { // always at least user id
  43. print_error('invalidarguments');
  44. }
  45. $contextid = (int)array_shift($args);
  46. $component = array_shift($args);
  47. $filearea = array_shift($args);
  48. $draftid = (int)array_shift($args);
  49. if ($component !== 'user' or $filearea !== 'draft') {
  50. send_file_not_found();
  51. }
  52. $context = context::instance_by_id($contextid);
  53. if ($context->contextlevel != CONTEXT_USER) {
  54. send_file_not_found();
  55. }
  56. $userid = $context->instanceid;
  57. if ($USER->id != $userid) {
  58. print_error('invaliduserid');
  59. }
  60. $fs = get_file_storage();
  61. $relativepath = implode('/', $args);
  62. $fullpath = "/$context->id/user/draft/$draftid/$relativepath";
  63. if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->get_filename() == '.') {
  64. send_file_not_found();
  65. }
  66. // ========================================
  67. // finally send the file
  68. // ========================================
  69. \core\session\manager::write_close(); // Unlock session during file serving.
  70. send_stored_file($file, 0, false, true, array('preview' => $preview)); // force download - security first!