download.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This file is responsible for passing requested documents to the browser.
  5. * Html files are parsed to fix a few problems with URLs,
  6. * but this code will hopefully be replaced soon by an Apache URL
  7. * rewrite mechanism.
  8. *
  9. * @package chamilo.work
  10. */
  11. session_cache_limiter('public');
  12. require_once '../inc/global.inc.php';
  13. require_once 'work.lib.php';
  14. $current_course_tool = TOOL_STUDENTPUBLICATION;
  15. $this_section = SECTION_COURSES;
  16. // IMPORTANT to avoid caching of documents
  17. header('Expires: Wed, 01 Jan 1990 00:00:00 GMT');
  18. header('Cache-Control: public');
  19. header('Pragma: no-cache');
  20. //protection
  21. api_protect_course_script(true);
  22. $id = intval($_GET['id']);
  23. $course_info = api_get_course_info();
  24. if (empty($course_info)) {
  25. api_not_allowed(true);
  26. }
  27. $tbl_student_publication = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  28. if (!empty($course_info['real_id'])) {
  29. $sql = 'SELECT * FROM '.$tbl_student_publication.' WHERE c_id = '.$course_info['real_id'].' AND id = "'.$id.'"';
  30. $result = Database::query($sql);
  31. if ($result && Database::num_rows($result)) {
  32. $row = Database::fetch_array($result, 'ASSOC');
  33. $full_file_name = api_get_path(SYS_COURSE_PATH).api_get_course_path().'/'.$row['url'];
  34. $item_info = api_get_item_property_info(api_get_course_int_id(), 'work', $row['id']);
  35. allowOnlySubscribedUser(api_get_user_id(), $row['parent_id'], $course_info['real_id']);
  36. if (empty($item_info)) {
  37. exit;
  38. }
  39. /*
  40. field show_score in table course : 0 => New documents are visible for all users
  41. 1 => New documents are only visible for the teacher(s)
  42. field visibility in table item_property : 0 => eye closed, invisible for all students
  43. 1 => eye open
  44. field accepted in table c_student_publication : 0 => eye closed, invisible for all students
  45. 1 => eye open
  46. (we should have visibility == accepted , otherwise there is an inconsistency in the Database)
  47. field value in table c_course_setting : 0 => Allow learners to delete their own publications = NO
  48. 1 => Allow learners to delete their own publications = YES
  49. +------------------+------------------------------+----------------------------+
  50. |Can download work?| doc visible for all = 0 | doc visible for all = 1|
  51. +------------------+------------------------------+----------------------------+
  52. | visibility = 0 | editor only | editor only |
  53. | | | |
  54. +------------------+------------------------------+----------------------------+
  55. | visibility = 1 | editor | editor |
  56. | | + owner of the work | + any student |
  57. +------------------+------------------------------+----------------------------+
  58. (editor = teacher + admin + anybody with right api_is_allowed_to_edit)
  59. */
  60. $work_is_visible = ($item_info['visibility'] == 1 && $row['accepted'] == 1);
  61. $doc_visible_for_all = ($course_info['show_score'] == 1);
  62. $is_editor = api_is_allowed_to_edit(true, true, true);
  63. $student_is_owner_of_work = user_is_author($row['id'], $row['user_id']);
  64. if ($is_editor
  65. //|| (!$doc_visible_for_all && $work_is_visible && $student_is_owner_of_work)
  66. || ($student_is_owner_of_work)
  67. || ($doc_visible_for_all && $work_is_visible)) {
  68. $title = str_replace(' ', '_', $row['title']);
  69. event_download($title);
  70. if (Security::check_abs_path($full_file_name, api_get_path(SYS_COURSE_PATH).api_get_course_path().'/')) {
  71. DocumentManager::file_send_for_download($full_file_name, true, $title);
  72. }
  73. } else {
  74. api_not_allowed();
  75. }
  76. }
  77. } else {
  78. api_not_allowed();
  79. }
  80. exit;