file.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Document download/view script
  5. * @package chamilo.document
  6. */
  7. /**
  8. * Init
  9. */
  10. Use Model\Document;
  11. Use Model\Course;
  12. /**
  13. * Return either
  14. *
  15. * - one document
  16. * - several documents (file and/or folders) zipped together
  17. *
  18. * Used to transfer files to another application through http.
  19. *
  20. * Script parameters:
  21. *
  22. * - id id(s) of the document id=1 or id=1,2,4
  23. * - cidReq course code
  24. *
  25. * Note this script enables key authentication so access with a key token is possible.
  26. *
  27. * @package chamilo.document
  28. * @license see /license.txt
  29. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva
  30. */
  31. KeyAuth::enable();
  32. require_once __DIR__ . '/../inc/global.inc.php';
  33. $has_access = api_protect_course_script();
  34. if (!$has_access) {
  35. exit;
  36. }
  37. session_cache_limiter('none');
  38. $ids = Request::get('id', '');
  39. $ids = $ids ? explode(',', $ids) : array();
  40. $course = Course::current();
  41. /**
  42. * No files requested. We make sure we return 404 error to tell the client
  43. * that the call failed.
  44. */
  45. if (count($ids) == 0 || empty($course)) {
  46. Response::not_found();
  47. }
  48. /**
  49. * One file requested. In this case we return the file itself.
  50. */
  51. if (count($ids) == 1) {
  52. $id = reset($ids);
  53. $doc = Document::get_by_id($course, $id);
  54. if (empty($doc)) {
  55. Response::not_found();
  56. }
  57. if ($doc->is_file()) {
  58. $has_access = $doc->is_accessible();
  59. if (!$has_access) {
  60. Response::not_found();
  61. }
  62. event_download(Uri::here());
  63. DocumentManager::file_send_for_download($doc);
  64. exit;
  65. }
  66. }
  67. /**
  68. * Several files requested. In this case we zip them together.
  69. */
  70. $files = array();
  71. $folders = array();
  72. foreach ($ids as $id) {
  73. $doc = Document::get_by_id($course, $id);
  74. if (!$doc->is_accessible()) {
  75. break;
  76. }
  77. if ($doc->is_file()) {
  78. $files[] = $doc;
  79. }
  80. if ($doc->is_folder()) {
  81. $folders[] = $doc;
  82. }
  83. }
  84. $requested_folders = $folders;
  85. /**
  86. * Note that if a parent folder is hidden children should not be accesible
  87. * even if they are visible. It is therefore not sufficient to check document
  88. * visibility.
  89. */
  90. while ($folders) {
  91. $items = $folders;
  92. $folders = array();
  93. foreach ($items as $item) {
  94. $children = $item->get_children();
  95. foreach ($children as $child) {
  96. if (!$child->is_accessible()) {
  97. break;
  98. }
  99. if ($child->is_file()) {
  100. $files[] = $child;
  101. }
  102. if ($child->is_folder()) {
  103. $folders[] = $child;
  104. }
  105. }
  106. }
  107. }
  108. $folders = $requested_folders;
  109. /**
  110. * Requested files may not be accessible.
  111. */
  112. if (count($files) == 0) {
  113. Response::not_found();
  114. }
  115. $root_dir = '';
  116. $items = array_merge($folders, $files);
  117. foreach ($items as $item) {
  118. $path = $item->get_absolute_path();
  119. $path = realpath($path);
  120. $dir = dirname($path);
  121. if (empty($root_dir) || strlen($root_dir) > strlen($dir)) {
  122. $root_dir = $dir;
  123. }
  124. }
  125. /**
  126. * Zip files together.
  127. */
  128. $temp_zip_path = Chamilo::temp_file('zip');
  129. $zip_folder = new PclZip($temp_zip_path);
  130. foreach ($files as $file) {
  131. if (empty($root_dir)) {
  132. $root_dir = dirname($file);
  133. }
  134. $file = (string) $file;
  135. $zip_folder->add($file, PCLZIP_OPT_REMOVE_PATH, $root_dir);
  136. }
  137. /**
  138. * Send file for download
  139. */
  140. event_download(Uri::here());
  141. DocumentManager::file_send_for_download($temp_zip_path, false, get_lang('Documents') . '.zip');