rest.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Controller for REST request
  5. * @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
  6. * @package chamilo.webservices
  7. */
  8. /* Require libs and classes */
  9. require_once '../inc/global.inc.php';
  10. /* Manage actions */
  11. $json = array();
  12. $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'nothing';
  13. $username = isset($_POST['username']) ? Security::remove_XSS($_POST['username']) : null;
  14. $apiKey = isset($_POST['api_key']) ? Security::remove_XSS($_POST['api_key']) : null;
  15. $em = Database::getManager();
  16. switch ($action) {
  17. case 'loginNewMessages':
  18. $password = isset($_POST['password']) ? Security::remove_XSS($_POST['password']) : null;
  19. if (MessagesWebService::isValidUser($username, $password)) {
  20. MessagesWebService::init();
  21. $webService = new MessagesWebService();
  22. $apiKey = $webService->getApiKey($username);
  23. $json = array(
  24. 'status' => true,
  25. 'apiKey' => $apiKey,
  26. 'gcmSenderId' => api_get_configuration_value('messaging_gdc_project_number'),
  27. );
  28. } else {
  29. $json = array(
  30. 'status' => false
  31. );
  32. }
  33. break;
  34. case 'countNewMessages':
  35. if (MessagesWebService::isValidApiKey($username, $apiKey)) {
  36. $webService = new MessagesWebService();
  37. $webService->setApiKey($apiKey);
  38. $lastId = isset($_POST['last']) ? $_POST['last'] : 0;
  39. $count = $webService->countNewMessages($username, $lastId);
  40. $json = array(
  41. 'status' => true,
  42. 'count' => $count
  43. );
  44. } else {
  45. $json = array(
  46. 'status' => false
  47. );
  48. }
  49. break;
  50. case 'getNewMessages':
  51. if (MessagesWebService::isValidApiKey($username, $apiKey)) {
  52. $webService = new MessagesWebService();
  53. $webService->setApiKey($apiKey);
  54. $lastId = isset($_POST['last']) ? $_POST['last'] : 0;
  55. $messages = $webService->getNewMessages($username, $lastId);
  56. $json = array(
  57. 'status' => true,
  58. 'messages' => $messages
  59. );
  60. } else {
  61. $json = array(
  62. 'status' => false
  63. );
  64. }
  65. break;
  66. case 'setGcmRegistrationId':
  67. if (!MessagesWebService::isValidApiKey($username, $apiKey)) {
  68. $json = ['status' => false];
  69. break;
  70. }
  71. $user = $em->getRepository('ChamiloUserBundle:User')->findOneBy(['username' => $username]);
  72. MessagesWebService::setGcmRegistrationId($user, $_POST['registration_id']);
  73. $json = ['status' => true];
  74. break;
  75. default:
  76. }
  77. /* View */
  78. header('Content-Type: application/json');
  79. header('Access-Control-Allow-Origin: *');
  80. echo json_encode($json);