file.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. Use Model\StudentPublication;
  3. Use Model\Course;
  4. /**
  5. * Return either
  6. *
  7. * - one work item (file)
  8. * - several work items (files) zipped together
  9. *
  10. * Used to transfer files to another application through http.
  11. *
  12. * Script parameters:
  13. *
  14. * - id id(s) of the work item id=1 or id=1,2,4
  15. * - cidReq course code
  16. *
  17. * Note this script enables key authentication so access with a key token is possible.
  18. *
  19. * @package chamilo.document
  20. * @license see /license.txt
  21. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva
  22. */
  23. KeyAuth::enable();
  24. require_once __DIR__ . '/../inc/global.inc.php';
  25. $has_access = api_protect_course_script();
  26. if (!$has_access) {
  27. exit;
  28. }
  29. session_cache_limiter('none');
  30. $ids = Request::get('id', '');
  31. $ids = $ids ? explode(',', $ids) : array();
  32. $course = Course::current();
  33. /**
  34. * No files requested. We make sure we return 404 error to tell the client
  35. * that the call failed.
  36. */
  37. if (count($ids) == 0 || empty($course)) {
  38. Response::not_found();
  39. }
  40. /**
  41. * One file/folder requested.
  42. */
  43. if (count($ids) == 1) {
  44. $id = reset($ids);
  45. $pub = StudentPublication::get_by_id($course, $id);
  46. if (empty($pub)) {
  47. Response::not_found();
  48. }
  49. $has_access = $pub->is_accessible();
  50. if (!$has_access) {
  51. Response::not_found();
  52. }
  53. if ($pub->is_file()) {
  54. event_download(Uri::here());
  55. DocumentManager::file_send_for_download($pub->get_absolute_path(), false, $pub->get_title());
  56. exit;
  57. }
  58. /**
  59. * one folder requested
  60. */
  61. $items = array();
  62. $children = $pub->get_children();
  63. foreach ($children as $child) {
  64. if ($child->is_accessible()) {
  65. $items[] = $child;
  66. }
  67. }
  68. if (count($items) == 0) {
  69. Response::not_found();
  70. }
  71. $zip = Chamilo::temp_zip();
  72. foreach ($items as $item) {
  73. $path = $item->get_absolute_path();
  74. $title = $item->get_title();
  75. $zip->add($path, $title);
  76. }
  77. event_download(Uri::here());
  78. DocumentManager::file_send_for_download($zip->get_path(), false, $pub->get_title() . '.zip');
  79. }
  80. /**
  81. * Several files requested. In this case we zip them together.
  82. */
  83. $items = array();
  84. foreach ($ids as $id) {
  85. $pub = StudentPublication::get_by_id($course, $id);
  86. if (!$pub->is_accessible()) {
  87. break;
  88. }
  89. if ($pub->is_file()) {
  90. $items[] = $pub;
  91. }
  92. /**
  93. * We ignore folders
  94. */
  95. }
  96. /**
  97. * Requested files may not be accessible.
  98. */
  99. if (count($items) == 0) {
  100. Response::not_found();
  101. }
  102. /**
  103. * Zip files together.
  104. */
  105. $zip = Chamilo::temp_zip();
  106. foreach ($items as $item) {
  107. $path = $item->get_absolute_path();
  108. $title = $item->get_title();
  109. $zip->add($path, $title);
  110. }
  111. /**
  112. * Send file for download
  113. */
  114. event_download(Uri::here());
  115. DocumentManager::file_send_for_download($zip->get_path(), false, get_lang('StudentPublications') . '.zip');