v2.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once __DIR__.'/../../inc/global.inc.php';
  4. $hash = isset($_REQUEST['hash']) ? $_REQUEST['hash'] : null;
  5. if ($hash) {
  6. $hashParams = Rest::decodeParams($hash);
  7. foreach ($hashParams as $key => $value) {
  8. $_REQUEST[$key] = $value;
  9. }
  10. }
  11. $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : null;
  12. $username = isset($_REQUEST['username']) ? Security::remove_XSS($_REQUEST['username']) : null;
  13. $apiKey = isset($_REQUEST['api_key']) ? Security::remove_XSS($_REQUEST['api_key']) : null;
  14. $course = !empty($_REQUEST['course']) ? intval($_REQUEST['course']) : null;
  15. $session = !empty($_REQUEST['session']) ? intval($_REQUEST['session']) : null;
  16. $restResponse = new RestResponse();
  17. try {
  18. /** @var Rest $restApi */
  19. $restApi = $apiKey ? Rest::validate($username, $apiKey) : null;
  20. if ($restApi) {
  21. $restApi->setCourse($course);
  22. $restApi->setSession($session);
  23. }
  24. switch ($action) {
  25. case Rest::GET_AUTH:
  26. Rest::init();
  27. $password = isset($_POST['password']) ? $_POST['password'] : null;
  28. $isValid = Rest::isValidUser($username, $password);
  29. if (!$isValid) {
  30. throw new Exception(get_lang('InvalideUserDetected'));
  31. }
  32. $restResponse->setData([
  33. 'url' => api_get_path(WEB_PATH),
  34. 'apiKey' => Rest::findUserApiKey($username, Rest::SERVIVE_NAME),
  35. 'gcmSenderId' => api_get_setting('messaging_gdc_project_number')
  36. ]);
  37. break;
  38. case Rest::SAVE_GCM_ID:
  39. $gcmId = isset($_POST['registration_id']) ? Security::remove_XSS($_POST['registration_id']) : null;
  40. $restApi->setGcmId($gcmId);
  41. $restResponse->setData(['status' => true]);
  42. break;
  43. case Rest::GET_USER_MESSAGES:
  44. $lastMessageId = isset($_POST['last']) ? intval($_POST['last']) : 0;
  45. $messages = $restApi->getUserMessages($lastMessageId);
  46. $restResponse->setData($messages);
  47. break;
  48. case Rest::GET_USER_COURSES:
  49. $courses = $restApi->getUserCourses();
  50. $restResponse->setData($courses);
  51. break;
  52. case Rest::GET_COURSE_INFO:
  53. $courseInfo = $restApi->getCourseInfo();
  54. $restResponse->setData($courseInfo);
  55. break;
  56. case Rest::GET_COURSE_DESCRIPTIONS:
  57. $descriptions = $restApi->getCourseDescriptions();
  58. $restResponse->setData($descriptions);
  59. break;
  60. case Rest::GET_COURSE_DOCUMENTS:
  61. $directoryId = isset($_POST['dir_id']) ? Security::remove_XSS($_POST['dir_id']) : null;
  62. $documents = $restApi->getCourseDocuments($directoryId);
  63. $restResponse->setData($documents);
  64. break;
  65. case Rest::GET_COURSE_ANNOUNCEMENTS:
  66. $announcements = $restApi->getCourseAnnouncements();
  67. $restResponse->setData($announcements);
  68. break;
  69. case Rest::GET_COURSE_ANNOUNCEMENT:
  70. $announcementId = isset($_POST['announcement']) ? Security::remove_XSS($_POST['announcement']) : 0;
  71. $announcement = $restApi->getCourseAnnouncement($announcementId);
  72. $restResponse->setData($announcement);
  73. break;
  74. case Rest::GET_COURSE_AGENDA:
  75. $agenda = $restApi->getCourseAgenda();
  76. $restResponse->setData($agenda);
  77. break;
  78. case Rest::GET_COURSE_NOTEBOOKS:
  79. $notebooks = $restApi->getCourseNotebooks();
  80. $restResponse->setData($notebooks);
  81. break;
  82. case Rest::GET_COURSE_FORUM_CATEGORIES:
  83. $forums = $restApi->getCourseForumCategories();
  84. $restResponse->setData($forums);
  85. break;
  86. case Rest::GET_COURSE_FORUM:
  87. $forumId = isset($_POST['forum']) ? Security::remove_XSS($_POST['forum']) : 0;
  88. $forum = $restApi->getCourseForum($forumId);
  89. $restResponse->setData($forum);
  90. break;
  91. case Rest::GET_COURSE_FORUM_THREAD:
  92. $forumId = isset($_POST['forum']) ? intval($_POST['forum']) : 0;
  93. $threadId = isset($_POST['thread']) ? intval($_POST['thread']) : 0;
  94. $thread = $restApi->getCourseForumThread($forumId, $threadId);
  95. $restResponse->setData($thread);
  96. break;
  97. case Rest::GET_PROFILE:
  98. $userInfo = $restApi->getUserProfile();
  99. $restResponse->setData($userInfo);
  100. break;
  101. case Rest::GET_COURSE_LEARNPATHS:
  102. $data = $restApi->getCourseLearnPaths();
  103. $restResponse->setData($data);
  104. break;
  105. case Rest::GET_COURSE_LEARNPATH:
  106. $lpId = isset($_REQUEST['lp_id']) ? intval($_REQUEST['lp_id']) : 0;
  107. $restApi->showLearningPath($lpId);
  108. break;
  109. case Rest::SAVE_FORUM_POST:
  110. if (
  111. empty($_POST['title']) || empty($_POST['text']) || empty($_POST['thread']) || empty($_POST['forum'])
  112. ) {
  113. throw new Exception(get_lang('NoData'));
  114. }
  115. $forumId = isset($_POST['forum']) ? intval($_POST['forum']) : 0;
  116. $notify = !empty($_POST['notify']);
  117. $parentId = !empty($_POST['parent']) ? intval($_POST['parent']) : null;
  118. $postValues = [
  119. 'post_title' => $_POST['title'],
  120. 'post_text' => nl2br($_POST['text']),
  121. 'thread_id' => $_POST['thread'],
  122. 'forum_id' => $_POST['forum'],
  123. 'post_notification' => $notify,
  124. 'post_parent_id' => $parentId
  125. ];
  126. $data = $restApi->saveForumPost($postValues, $forumId);
  127. $restResponse->setData($data);
  128. break;
  129. case Rest::GET_USER_SESSIONS:
  130. $courses = $restApi->getUserSessions();
  131. $restResponse->setData($courses);
  132. break;
  133. case Rest::SAVE_USER_MESSAGE:
  134. $receivers = isset($_POST['receivers']) ? $_POST['receivers'] : [];
  135. $subject = !empty($_POST['subject']) ? $_POST['subject'] : null;
  136. $text = !empty($_POST['text']) ? $_POST['text'] : null;
  137. $data = $restApi->saveUserMessage($subject, $text, $receivers);
  138. $restResponse->setData($data);
  139. break;
  140. case Rest::GET_MESSAGE_USERS:
  141. $search = !empty($_REQUEST['q']) ? $_REQUEST['q'] : null;
  142. if (!$search || strlen($search) < 2) {
  143. throw new Exception(get_lang('TooShort'));
  144. }
  145. $data = $restApi->getMessageUsers($search);
  146. $restResponse->setData($data);
  147. break;
  148. case Rest::SAVE_COURSE_NOTEBOOK:
  149. $title = !empty($_POST['title']) ? $_POST['title'] : null;
  150. $text = !empty($_POST['text']) ? $_POST['text'] : null;
  151. $data = $restApi->saveCourseNotebook($title, $text);
  152. $restResponse->setData($data);
  153. break;
  154. case Rest::SAVE_FORUM_THREAD:
  155. if (
  156. empty($_POST['title']) || empty($_POST['text']) || empty($_POST['forum'])
  157. ) {
  158. throw new Exception(get_lang('NoData'));
  159. }
  160. $forumId = isset($_POST['forum']) ? intval($_POST['forum']) : 0;
  161. $notify = !empty($_POST['notify']);
  162. $threadInfo = [
  163. 'post_title' => $_POST['title'],
  164. 'forum_id' => $_POST['forum'],
  165. 'post_text' => nl2br($_POST['text']),
  166. 'post_notification' => $notify
  167. ];
  168. $data = $restApi->saveForumThread($threadInfo, $forumId);
  169. $restResponse->setData($data);
  170. break;
  171. default:
  172. throw new Exception(get_lang('InvalidAction'));
  173. }
  174. } catch (Exception $exeption) {
  175. $restResponse->setErrorMessage(
  176. $exeption->getMessage()
  177. );
  178. }
  179. header('Content-Type: application/json');
  180. header('Access-Control-Allow-Origin: *');
  181. echo $restResponse->format();