v2.php 8.4 KB

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