chat.ajax.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. api_block_anonymous_users();
  9. if (api_get_setting('allow_global_chat') == 'false') {
  10. exit;
  11. }
  12. $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
  13. // Course Chat
  14. if ($action === 'preview') {
  15. echo CourseChatUtils::prepareMessage($_REQUEST['message']);
  16. exit;
  17. }
  18. $toUserId = isset($_REQUEST['to']) ? $_REQUEST['to'] : null;
  19. $message = isset($_REQUEST['message']) ? $_REQUEST['message'] : null;
  20. $currentUserId = api_get_user_id();
  21. $chat = new Chat();
  22. if (Chat::disableChat()) {
  23. exit;
  24. }
  25. if ($chat->isChatBlockedByExercises()) {
  26. // Disconnecting the user
  27. $chat->setUserStatus(0);
  28. exit;
  29. }
  30. switch ($action) {
  31. case 'get_message_status':
  32. $messageId = isset($_REQUEST['message_id']) ? $_REQUEST['message_id'] : 0;
  33. $messageInfo = $chat->get($messageId);
  34. if ($messageInfo && $messageInfo['from_user'] == $currentUserId) {
  35. echo json_encode($messageInfo);
  36. }
  37. break;
  38. case 'chatheartbeat':
  39. $chat->heartbeat();
  40. break;
  41. case 'close_window':
  42. // Closes friend window
  43. $chatId = isset($_POST['chatbox']) ? $_POST['chatbox'] : '';
  44. $chat->closeWindow($chatId);
  45. echo '1';
  46. exit;
  47. break;
  48. case 'close':
  49. // Disconnects user from all chat
  50. $chat->close();
  51. echo '1';
  52. exit;
  53. break;
  54. case 'create_room':
  55. if (api_get_configuration_value('hide_chat_video')) {
  56. api_not_allowed();
  57. }
  58. /*$room = VideoChat::getChatRoomByUsers(api_get_user_id(), $toUserId);
  59. if ($room === false) {
  60. $createdRoom = VideoChat::createRoom(api_get_user_id(), $toUserId);
  61. if ($createdRoom === false) {
  62. echo Display::return_message(
  63. get_lang('Chatroom could not be created'),
  64. 'error'
  65. );
  66. break;
  67. }
  68. $room = VideoChat::getChatRoomByUsers(api_get_user_id(), $toUserId);
  69. }
  70. $videoChatUrl = api_get_path(WEB_LIBRARY_JS_PATH)."chat/video.php?room={$room['id']}";
  71. $videoChatLink = Display::url(
  72. Display::returnFontAwesomeIcon('video-camera').get_lang('Start video call'),
  73. $videoChatUrl
  74. );
  75. $chat->send(
  76. api_get_user_id(),
  77. $toUserId,
  78. $videoChatLink,
  79. false,
  80. false
  81. );
  82. echo Display::tag('p', $videoChatLink, ['class' => 'lead']);*/
  83. break;
  84. case 'get_contacts':
  85. echo $chat->getContacts();
  86. break;
  87. case 'get_previous_messages':
  88. $userId = isset($_REQUEST['user_id']) ? $_REQUEST['user_id'] : 0;
  89. $visibleMessages = isset($_REQUEST['visible_messages']) ? $_REQUEST['visible_messages'] : 0;
  90. if (empty($userId)) {
  91. return '';
  92. }
  93. $items = $chat->getPreviousMessages(
  94. $userId,
  95. $currentUserId,
  96. $visibleMessages
  97. );
  98. if (!empty($items)) {
  99. sort($items);
  100. echo json_encode($items);
  101. exit;
  102. }
  103. echo json_encode([]);
  104. exit;
  105. break;
  106. case 'notify_not_support':
  107. $chat->send(
  108. $currentUserId,
  109. $toUserId,
  110. get_lang('The browser of %s does not support native video transmission. Sorry.')
  111. );
  112. break;
  113. case 'sendchat':
  114. $chat->send($currentUserId, $toUserId, $message);
  115. break;
  116. case 'startchatsession':
  117. $chat->startSession();
  118. break;
  119. case 'set_status':
  120. $status = isset($_REQUEST['status']) ? (int) $_REQUEST['status'] : 0;
  121. $chat->setUserStatus($status);
  122. break;
  123. default:
  124. echo '';
  125. }
  126. exit;