rest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. switch ($action) {
  16. case 'loginNewMessages':
  17. $password = isset($_POST['password']) ? Security::remove_XSS($_POST['password']) : null;
  18. if (MessagesWebService::isValidUser($username, $password)) {
  19. $webService = new MessagesWebService();
  20. $apiKey = $webService->getApiKey($username);
  21. $json = array(
  22. 'status' => true,
  23. 'apiKey' => $apiKey
  24. );
  25. } else {
  26. $json = array(
  27. 'status' => false
  28. );
  29. }
  30. break;
  31. case 'countNewMessages':
  32. if (MessagesWebService::isValidApiKey($username, $apiKey)) {
  33. $webService = new MessagesWebService();
  34. $webService->setApiKey($apiKey);
  35. $lastId = isset($_POST['last']) ? $_POST['last'] : 0;
  36. $count = $webService->countNewMessages($username, $lastId);
  37. $json = array(
  38. 'status' => true,
  39. 'count' => $count
  40. );
  41. } else {
  42. $json = array(
  43. 'status' => false
  44. );
  45. }
  46. break;
  47. case 'getNewMessages':
  48. if (MessagesWebService::isValidApiKey($username, $apiKey)) {
  49. $webService = new MessagesWebService();
  50. $webService->setApiKey($apiKey);
  51. $lastId = isset($_POST['last']) ? $_POST['last'] : 0;
  52. $messages = $webService->getNewMessages($username, $lastId);
  53. $json = array(
  54. 'status' => true,
  55. 'messages' => $messages
  56. );
  57. } else {
  58. $json = array(
  59. 'status' => false
  60. );
  61. }
  62. break;
  63. default:
  64. }
  65. /* View */
  66. header('Content-Type: application/json');
  67. header('Access-Control-Allow-Origin: *');
  68. echo json_encode($json);