chat.ajax.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : null;
  9. if (api_is_anonymous()) {
  10. exit;
  11. }
  12. // Course Chat
  13. if ($action == 'preview') {
  14. echo CourseChatUtils::prepareMessage($_REQUEST['message']);
  15. exit;
  16. }
  17. if (api_get_setting('allow_global_chat') == 'false') {
  18. exit;
  19. }
  20. $to_user_id = 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(), $to_user_id, $message);
  46. break;
  47. case 'startchatsession':
  48. $chat->startSession();
  49. break;
  50. case 'set_status':
  51. $status = isset($_REQUEST['status']) ? intval($_REQUEST['status']) : 0;
  52. $chat->setUserStatus($status);
  53. break;
  54. case 'create_room':
  55. $room = VideoChat::getChatRoomByUsers(api_get_user_id(), $to_user_id);
  56. if ($room === false) {
  57. $createdRoom = VideoChat::createRoom(api_get_user_id(), $to_user_id);
  58. if ($createdRoom === false) {
  59. echo Display::return_message(get_lang('ChatRoomNotCreated'), 'error');
  60. break;
  61. }
  62. $room = VideoChat::getChatRoomByUsers(api_get_user_id(), $to_user_id);
  63. }
  64. $videoChatUrl = api_get_path(WEB_LIBRARY_JS_PATH)."chat/video.php?room={$room['id']}";
  65. $videoChatLink = Display::url(
  66. Display::returnFontAwesomeIcon('video-camera').get_lang('StartVideoChat'),
  67. $videoChatUrl
  68. );
  69. $chat->send(
  70. api_get_user_id(),
  71. $to_user_id,
  72. $videoChatLink,
  73. false,
  74. false
  75. );
  76. echo Display::tag('p', $videoChatLink, ['class' => 'lead']);
  77. break;
  78. case 'notify_not_support':
  79. $chat->send(api_get_user_id(), $to_user_id, get_lang('TheXUserBrowserDoesNotSupportWebRTC'));
  80. break;
  81. default:
  82. echo '';
  83. }
  84. exit;