forum.ajax.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Responses to AJAX calls for forum attachments
  5. * @package chamilo/forum
  6. * @author Daniel Barreto Alva <daniel.barreto@beeznest.com>
  7. */
  8. //require_once '../global.inc.php';
  9. require_once api_get_path(SYS_CODE_PATH) . 'forum/forumfunction.inc.php';
  10. // First, protect this script
  11. api_protect_course_script(false);
  12. /**
  13. * Main code
  14. */
  15. // Create a default error response
  16. $json = array(
  17. 'error' => true,
  18. 'errorMessage' => 'ERROR',
  19. );
  20. $action = isset($_REQUEST['a']) ? $_REQUEST['a'] : null;
  21. $current_forum = get_forum_information($_REQUEST['forum']);
  22. $current_forum_category = get_forumcategory_information($current_forum['forum_category']);
  23. $current_thread = get_thread_information($_REQUEST['forum'], $_REQUEST['thread']);
  24. // Check if exist action
  25. if (!empty($action)) {
  26. switch ($action) {
  27. case 'upload_file':
  28. if (!empty($_FILES) && !empty($_REQUEST['forum'])) {
  29. // The user is not allowed here if
  30. // 1. the forum category, forum or thread is invisible (visibility==0)
  31. // 2. the forum category, forum or thread is locked (locked <>0)
  32. // 3. if anonymous posts are not allowed
  33. // The only exception is the course manager
  34. // They are several pieces for clarity.
  35. if (!api_is_allowed_to_edit(null, true) &&
  36. (
  37. ($current_forum_category && $current_forum_category['visibility'] == 0) ||
  38. $current_forum['visibility'] == 0
  39. )
  40. ) {
  41. $json['errorMessage'] = '1. the forum category, forum or thread is invisible (visibility==0)';
  42. break;
  43. }
  44. if (!api_is_allowed_to_edit(null, true) &&
  45. (
  46. ($current_forum_category && $current_forum_category['locked'] <> 0) ||
  47. $current_forum['locked'] <> 0 || $current_thread['locked'] <> 0
  48. )
  49. ) {
  50. $json['errorMessage'] = '2. the forum category, forum or thread is locked (locked <>0)';
  51. break;
  52. }
  53. if (api_is_anonymous() && $current_forum['allow_anonymous'] == 0) {
  54. $json['errorMessage'] = '3. if anonymous posts are not allowed';
  55. break;
  56. }
  57. // If pass all previous control, user can edit post
  58. $courseId = isset($_REQUEST['c_id'])? intval($_REQUEST['c_id']) : api_get_course_int_id();
  59. $json['courseId'] = $courseId;
  60. $forumId = isset($_REQUEST['forum'])? intval($_REQUEST['forum']) : null;
  61. $json['forum'] = $forumId;
  62. $threadId = isset($_REQUEST['thread'])? intval($_REQUEST['thread']) : null;
  63. $json['thread'] = $threadId;
  64. $postId = isset($_REQUEST['postId'])? intval($_REQUEST['postId']) : null;
  65. $json['postId'] = $postId;
  66. if (!empty($courseId) &&
  67. !is_null($forumId) &&
  68. !is_null($threadId) &&
  69. !is_null($postId)
  70. ) {
  71. // Save forum attachment
  72. $attachId = add_forum_attachment_file('', $postId);
  73. if ($attachId !== false) {
  74. // Get prepared array of attachment data
  75. $array = getAttachedFiles(
  76. $forumId,
  77. $threadId,
  78. $postId,
  79. $attachId,
  80. $courseId
  81. );
  82. // Check if array data is consistent
  83. if (isset($array['name'])) {
  84. $json['error'] = false;
  85. $json['errorMessage'] = 'Success';
  86. $json = array_merge($json, $array);
  87. }
  88. }
  89. }
  90. }
  91. break;
  92. case 'delete_file':
  93. // Check if set attachment ID and thread ID
  94. if (isset($_REQUEST['attachId']) && isset($_REQUEST['thread'])) {
  95. api_block_course_item_locked_by_gradebook($_REQUEST['thread'], LINK_FORUM_THREAD);
  96. // The user is not allowed here if
  97. // 1. the forum category, forum or thread is invisible (visibility==0)
  98. // 2. the forum category, forum or thread is locked (locked <>0)
  99. // 3. if anonymous posts are not allowed
  100. // 4. if editing of replies is not allowed
  101. // The only exception is the course manager
  102. // They are several pieces for clarity.
  103. if (!api_is_allowed_to_edit(null, true) AND
  104. (
  105. ($current_forum_category && $current_forum_category['visibility'] == 0) OR
  106. $current_forum['visibility'] == 0)
  107. ) {
  108. $json['errorMessage'] = '1. the forum category, forum or thread is invisible (visibility==0)';
  109. break;
  110. }
  111. if (!api_is_allowed_to_edit(null, true) AND
  112. (
  113. ($current_forum_category && $current_forum_category['locked'] <> 0) OR
  114. $current_forum['locked'] <> 0 OR $current_thread['locked'] <> 0
  115. )
  116. ) {
  117. $json['errorMessage'] = '2. the forum category, forum or thread is locked (locked <>0)';
  118. break;
  119. }
  120. if (api_is_anonymous() AND $current_forum['allow_anonymous'] == 0) {
  121. $json['errorMessage'] = '3. if anonymous posts are not allowed';
  122. break;
  123. }
  124. $group_id = api_get_group_id();
  125. $groupInfo = GroupManager::get_group_properties($group_id);
  126. if (!api_is_allowed_to_edit(null, true) &&
  127. $current_forum['allow_edit'] == 0 &&
  128. ($group_id && !GroupManager::is_tutor_of_group(api_get_user_id(), $groupInfo['iid']))
  129. ) {
  130. $json['errorMessage'] = '4. if editing of replies is not allowed';
  131. break;
  132. }
  133. // If pass all previous control, user can edit post
  134. $attachId = $_REQUEST['attachId'];
  135. $threadId = $_REQUEST['thread'];
  136. // Delete forum attachment from database and file system
  137. $affectedRows = delete_attachment(0, $attachId, false);
  138. if ($affectedRows > 0) {
  139. $json['error'] = false;
  140. $json['errorMessage'] = 'Success';
  141. }
  142. }
  143. break;
  144. }
  145. }
  146. echo json_encode($json);
  147. exit;