file.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. require_once __DIR__ . '/../inc/autoload.inc.php';
  24. KeyAuth::enable();
  25. require_once __DIR__ . '/../inc/global.inc.php';
  26. $has_access = api_protect_course_script();
  27. if (!$has_access) {
  28. exit;
  29. }
  30. session_cache_limiter('none');
  31. $ids = Request::get('id', '');
  32. $ids = $ids ? explode(',', $ids) : array();
  33. $course = Course::current();
  34. /**
  35. * No files requested. We make sure we return 404 error to tell the client
  36. * that the call failed.
  37. */
  38. if (count($ids) == 0 || empty($course)) {
  39. Response::not_found();
  40. }
  41. /**
  42. * One file/folder requested.
  43. */
  44. if (count($ids) == 1) {
  45. $id = reset($ids);
  46. $pub = StudentPublication::get_by_id($course, $id);
  47. if (empty($pub)) {
  48. Response::not_found();
  49. }
  50. $has_access = $pub->is_accessible();
  51. if (!$has_access) {
  52. Response::not_found();
  53. }
  54. if ($pub->is_file()) {
  55. event_download(Uri::here());
  56. DocumentManager::file_send_for_download($pub->get_absolute_path(), false, $pub->get_title());
  57. exit;
  58. }
  59. /**
  60. * one folder requested
  61. */
  62. $items = array();
  63. $children = $pub->get_children();
  64. foreach ($children as $child) {
  65. if ($child->is_accessible()) {
  66. $items[] = $child;
  67. }
  68. }
  69. if (count($items) == 0) {
  70. Response::not_found();
  71. }
  72. $zip = Chamilo::temp_zip();
  73. foreach ($items as $item) {
  74. $path = $item->get_absolute_path();
  75. $title = $item->get_title();
  76. $zip->add($path, $title);
  77. }
  78. event_download(Uri::here());
  79. DocumentManager::file_send_for_download($zip->get_path(), false, $pub->get_title() . '.zip');
  80. }
  81. /**
  82. * Several files requested. In this case we zip them together.
  83. */
  84. $items = array();
  85. foreach ($ids as $id) {
  86. $pub = StudentPublication::get_by_id($course, $id);
  87. if (!$pub->is_accessible()) {
  88. break;
  89. }
  90. if ($pub->is_file()) {
  91. $items[] = $pub;
  92. }
  93. /**
  94. * We ignore folders
  95. */
  96. }
  97. /**
  98. * Requested files may not be accessible.
  99. */
  100. if (count($items) == 0) {
  101. Response::not_found();
  102. }
  103. /**
  104. * Zip files together.
  105. */
  106. $zip = Chamilo::temp_zip();
  107. foreach ($items as $item) {
  108. $path = $item->get_absolute_path();
  109. $title = $item->get_title();
  110. $zip->add($path, $title);
  111. }
  112. /**
  113. * Send file for download
  114. */
  115. event_download(Uri::here());
  116. DocumentManager::file_send_for_download($zip->get_path(), false, get_lang('StudentPublications') . '.zip');