chat.ajax.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Responses to AJAX calls
  5. */
  6. $_dont_save_user_course_access = true;
  7. require_once __DIR__.'/../global.inc.php';
  8. if (api_get_setting('allow_global_chat') == 'false') {
  9. exit;
  10. }
  11. $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : null;
  12. if (api_is_anonymous()) {
  13. exit;
  14. }
  15. // Course Chat
  16. if ($action == 'preview') {
  17. echo CourseChatUtils::prepareMessage($_REQUEST['message']);
  18. exit;
  19. }
  20. $toUserId = isset($_REQUEST['to']) ? $_REQUEST['to'] : null;
  21. $message = isset($_REQUEST['message']) ? $_REQUEST['message'] : null;
  22. if (!isset($_SESSION['chatHistory'])) {
  23. $_SESSION['chatHistory'] = array();
  24. }
  25. if (!isset($_SESSION['openChatBoxes'])) {
  26. $_SESSION['openChatBoxes'] = array();
  27. }
  28. $chat = new Chat();
  29. if (chat::disableChat()) {
  30. exit;
  31. }
  32. if ($chat->is_chat_blocked_by_exercises()) {
  33. // Disconnecting the user
  34. $chat->setUserStatus(0);
  35. exit;
  36. }
  37. switch ($action) {
  38. case 'chatheartbeat':
  39. $chat->heartbeat();
  40. break;
  41. case 'closechat':
  42. $chat->close();
  43. break;
  44. case 'sendchat':
  45. $chat->send(api_get_user_id(), $toUserId, $message);
  46. break;
  47. case 'startchatsession':
  48. $chat->startSession();
  49. break;
  50. case 'get_previous_messages':
  51. $userId = isset($_REQUEST['user_id']) ? $_REQUEST['user_id'] : null;
  52. $visibleMessages = isset($_REQUEST['visible_messages']) ? $_REQUEST['visible_messages'] : null;
  53. if (empty($userId)) {
  54. return '';
  55. }
  56. $items = $chat->getPreviousMessages(
  57. $userId,
  58. api_get_user_id(),
  59. $visibleMessages
  60. );
  61. echo json_encode($items);
  62. exit;
  63. break;
  64. case 'set_status':
  65. $status = isset($_REQUEST['status']) ? intval($_REQUEST['status']) : 0;
  66. $chat->setUserStatus($status);
  67. break;
  68. case 'create_room':
  69. $room = VideoChat::getChatRoomByUsers(api_get_user_id(), $toUserId);
  70. if ($room === false) {
  71. $createdRoom = VideoChat::createRoom(api_get_user_id(), $toUserId);
  72. if ($createdRoom === false) {
  73. echo Display::return_message(
  74. get_lang('ChatRoomNotCreated'),
  75. 'error'
  76. );
  77. break;
  78. }
  79. $room = VideoChat::getChatRoomByUsers(api_get_user_id(), $toUserId);
  80. }
  81. $videoChatUrl = api_get_path(WEB_CODE_PATH) . "chat/video.php?room={$room['id']}";
  82. $videoChatLink = Display::url(
  83. Display::returnFontAwesomeIcon('video-camera').get_lang('StartVideoChat'),
  84. $videoChatUrl
  85. );
  86. $chat->send(
  87. api_get_user_id(),
  88. $toUserId,
  89. $videoChatLink,
  90. false,
  91. false
  92. );
  93. echo Display::tag('p', $videoChatLink, ['class' => 'lead']);
  94. break;
  95. case 'notify_not_support':
  96. $chat->send(
  97. api_get_user_id(),
  98. $toUserId,
  99. get_lang('TheXUserBrowserDoesNotSupportWebRTC')
  100. );
  101. break;
  102. default:
  103. echo '';
  104. }
  105. exit;