v2.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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::CREATE_CAMPUS:
  124. $data = $restApi->createCampusURL($_POST);
  125. $restResponse->setData($data);
  126. break;
  127. case Rest::EDIT_CAMPUS:
  128. $data = $restApi->editCampusURL($_POST);
  129. $restResponse->setData($data);
  130. break;
  131. case Rest::DELETE_CAMPUS:
  132. $data = $restApi->deleteCampusURL($_POST);
  133. $restResponse->setData($data);
  134. break;
  135. case Rest::SAVE_SESSION:
  136. $data = $restApi->addSession($_POST);
  137. $restResponse->setData($data);
  138. break;
  139. case Rest::GET_USERS:
  140. $data = $restApi->getUsersCampus($_POST);
  141. $restResponse->setData($data);
  142. break;
  143. case Rest::GET_COURSE:
  144. $data = $restApi->getCoursesCampus($_POST);
  145. $restResponse->setData($data);
  146. break;
  147. case Rest::ADD_COURSES_SESSION:
  148. $data = $restApi->addCoursesSession($_POST);
  149. $restResponse->setData($data);
  150. break;
  151. case Rest::ADD_USER_SESSION:
  152. $data = $restApi->addUsersSession($_POST);
  153. $restResponse->setData($data);
  154. break;
  155. case Rest::SAVE_FORUM_POST:
  156. if (
  157. empty($_POST['title']) || empty($_POST['text']) || empty($_POST['thread']) || empty($_POST['forum'])
  158. ) {
  159. throw new Exception(get_lang('NoData'));
  160. }
  161. $forumId = isset($_POST['forum']) ? intval($_POST['forum']) : 0;
  162. $notify = !empty($_POST['notify']);
  163. $parentId = !empty($_POST['parent']) ? intval($_POST['parent']) : null;
  164. $postValues = [
  165. 'post_title' => $_POST['title'],
  166. 'post_text' => nl2br($_POST['text']),
  167. 'thread_id' => $_POST['thread'],
  168. 'forum_id' => $_POST['forum'],
  169. 'post_notification' => $notify,
  170. 'post_parent_id' => $parentId,
  171. ];
  172. $data = $restApi->saveForumPost($postValues, $forumId);
  173. $restResponse->setData($data);
  174. break;
  175. case Rest::GET_USER_SESSIONS:
  176. $courses = $restApi->getUserSessions();
  177. $restResponse->setData($courses);
  178. break;
  179. case Rest::SAVE_USER_MESSAGE:
  180. $receivers = isset($_POST['receivers']) ? $_POST['receivers'] : [];
  181. $subject = !empty($_POST['subject']) ? $_POST['subject'] : null;
  182. $text = !empty($_POST['text']) ? $_POST['text'] : null;
  183. $data = $restApi->saveUserMessage($subject, $text, $receivers);
  184. $restResponse->setData($data);
  185. break;
  186. case Rest::GET_MESSAGE_USERS:
  187. $search = !empty($_REQUEST['q']) ? $_REQUEST['q'] : null;
  188. if (!$search || strlen($search) < 2) {
  189. throw new Exception(get_lang('TooShort'));
  190. }
  191. $data = $restApi->getMessageUsers($search);
  192. $restResponse->setData($data);
  193. break;
  194. case Rest::SAVE_COURSE_NOTEBOOK:
  195. $title = !empty($_POST['title']) ? $_POST['title'] : null;
  196. $text = !empty($_POST['text']) ? $_POST['text'] : null;
  197. $data = $restApi->saveCourseNotebook($title, $text);
  198. $restResponse->setData($data);
  199. break;
  200. case Rest::SAVE_FORUM_THREAD:
  201. if (
  202. empty($_POST['title']) || empty($_POST['text']) || empty($_POST['forum'])
  203. ) {
  204. throw new Exception(get_lang('NoData'));
  205. }
  206. $forumId = isset($_POST['forum']) ? intval($_POST['forum']) : 0;
  207. $notify = !empty($_POST['notify']);
  208. $threadInfo = [
  209. 'post_title' => $_POST['title'],
  210. 'forum_id' => $_POST['forum'],
  211. 'post_text' => nl2br($_POST['text']),
  212. 'post_notification' => $notify,
  213. ];
  214. $data = $restApi->saveForumThread($threadInfo, $forumId);
  215. $restResponse->setData($data);
  216. break;
  217. default:
  218. throw new Exception(get_lang('InvalidAction'));
  219. }
  220. } catch (Exception $exeption) {
  221. $restResponse->setErrorMessage(
  222. $exeption->getMessage()
  223. );
  224. }
  225. header('Content-Type: application/json');
  226. header('Access-Control-Allow-Origin: *');
  227. echo $restResponse->format();