chat.lib.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class Chat
  5. *
  6. * @package chamilo.library.chat
  7. */
  8. class Chat extends Model
  9. {
  10. public $table;
  11. public $columns = array('id', 'from_user', 'to_user', 'message', 'sent', 'recd');
  12. public $window_list = array();
  13. /**
  14. * The contructor sets the chat table name and the window_list attribute
  15. * @return object Object reference
  16. */
  17. public function __construct()
  18. {
  19. $this->table = Database::get_main_table(TABLE_MAIN_CHAT);
  20. $this->window_list = $_SESSION['window_list'] = isset($_SESSION['window_list']) ? $_SESSION['window_list'] : array();
  21. }
  22. /**
  23. * Get user chat status
  24. * @return int 0 if disconnected, 1 if connected
  25. */
  26. function get_user_status()
  27. {
  28. $status = UserManager::get_extra_user_data_by_field(api_get_user_id(), 'user_chat_status', false, true);
  29. return $status['user_chat_status'];
  30. }
  31. /**
  32. * Set user chat status
  33. * @param int 0 if disconnected, 1 if connected
  34. * @return void
  35. */
  36. public function setUserStatus($status)
  37. {
  38. UserManager::update_extra_field_value(api_get_user_id(), 'user_chat_status', $status);
  39. }
  40. /**
  41. * Starts a chat session and returns JSON array of status and chat history
  42. * @return void (prints output in JSON format)
  43. */
  44. public function startSession()
  45. {
  46. $items = array();
  47. if (isset($_SESSION['chatHistory'])) {
  48. $items = $_SESSION['chatHistory'];
  49. }
  50. $return = array(
  51. 'user_status' => $this->get_user_status(),
  52. 'me' => get_lang('Me'),
  53. 'items' => $items
  54. );
  55. echo json_encode($return);
  56. exit;
  57. }
  58. /**
  59. * Refreshes the chat windows (usually called every x seconds through AJAX)
  60. * @return void (prints JSON array of chat windows)
  61. */
  62. public function heartbeat()
  63. {
  64. $to_user_id = api_get_user_id();
  65. $minutes = 60;
  66. $now = time() - $minutes * 60;
  67. $now = api_get_utc_datetime($now);
  68. //OR sent > '$now'
  69. $sql = "SELECT * FROM ".$this->table."
  70. WHERE to_user = '".intval($to_user_id)."' AND ( recd = 0 ) ORDER BY id ASC";
  71. $result = Database::query($sql);
  72. $chat_list = array();
  73. while ($chat = Database::fetch_array($result, 'ASSOC')) {
  74. $chat_list[$chat['from_user']]['items'][] = $chat;
  75. }
  76. $items = array();
  77. foreach ($chat_list as $from_user_id => $rows) {
  78. $rows = $rows['items'];
  79. $user_info = api_get_user_info($from_user_id, true);
  80. //Cleaning tsChatBoxes
  81. unset($_SESSION['tsChatBoxes'][$from_user_id]);
  82. foreach ($rows as $chat) {
  83. $chat['message'] = Security::remove_XSS($chat['message']);
  84. $item = array('s' => '0',
  85. 'f' => $from_user_id,
  86. 'm' => $chat['message'],
  87. 'username' => $user_info['complete_name'],
  88. 'id' => $chat['id']
  89. );
  90. $items[$from_user_id]['items'][] = $item;
  91. $items[$from_user_id]['user_info']['user_name'] = $user_info['complete_name'];
  92. $items[$from_user_id]['user_info']['online'] = $user_info['user_is_online'];
  93. $_SESSION['openChatBoxes'][$from_user_id] = api_strtotime($chat['sent'], 'UTC');
  94. }
  95. $_SESSION['chatHistory'][$from_user_id]['items'][] = $item;
  96. $_SESSION['chatHistory'][$from_user_id]['user_info']['user_name'] = $user_info['complete_name'];
  97. $_SESSION['chatHistory'][$from_user_id]['user_info']['online'] = $user_info['user_is_online'];
  98. }
  99. if (!empty($_SESSION['openChatBoxes'])) {
  100. foreach ($_SESSION['openChatBoxes'] as $user_id => $time) {
  101. if (!isset($_SESSION['tsChatBoxes'][$user_id])) {
  102. $now = time() - $time;
  103. $time = api_convert_and_format_date($time, DATE_TIME_FORMAT_SHORT_TIME_FIRST);
  104. $message = sprintf(get_lang('SentAtX'), $time);
  105. if ($now > 180) {
  106. $item = array('s' => '2', 'f' => $user_id, 'm' => $message);
  107. if (isset($_SESSION['chatHistory'][$user_id])) {
  108. $_SESSION['chatHistory'][$user_id]['items'][] = $item;
  109. }
  110. $_SESSION['tsChatBoxes'][$user_id] = 1;
  111. }
  112. }
  113. }
  114. }
  115. $sql = "UPDATE ".$this->table." SET recd = 1 WHERE to_user = '".$to_user_id."' AND recd = 0";
  116. Database::query($sql);
  117. if ($items != '') {
  118. //$items = substr($items, 0, -1);
  119. }
  120. echo json_encode(array('items' => $items));
  121. }
  122. /*
  123. * Returns an array of messages inside a chat session with a specific user
  124. * @param int The ID of the user with whom the current user is chatting
  125. * @return array Messages list
  126. */
  127. public function box_session($user_id)
  128. {
  129. $items = array();
  130. if (isset($_SESSION['chatHistory'][$user_id])) {
  131. $items = $_SESSION['chatHistory'][$user_id];
  132. }
  133. return $items;
  134. }
  135. /**
  136. * Saves into session the fact that a chat window exists with the given user
  137. * @param int The ID of the user with whom the current user is chatting
  138. * @return void
  139. */
  140. public function save_window($user_id)
  141. {
  142. $this->window_list[$user_id] = true;
  143. $_SESSION['window_list'] = $this->window_list;
  144. }
  145. /**
  146. * Sends a message from one user to another user
  147. * @param int The ID of the user sending the message
  148. * @param int The ID of the user receiving the message
  149. * @param string Message
  150. * @return void Prints "1"
  151. */
  152. public function send($from_user_id, $to_user_id, $message, $printResult = true)
  153. {
  154. $user_friend_relation = SocialManager::get_relation_between_contacts($from_user_id, $to_user_id);
  155. if ($user_friend_relation == USER_RELATION_TYPE_FRIEND) {
  156. $user_info = api_get_user_info($to_user_id, true);
  157. $this->save_window($to_user_id);
  158. $_SESSION['openChatBoxes'][$to_user_id] = api_get_utc_datetime();
  159. $messagesan = self::sanitize($message);
  160. if (!isset($_SESSION['chatHistory'][$to_user_id])) {
  161. $_SESSION['chatHistory'][$to_user_id] = array();
  162. }
  163. $item = array("s" => "1",
  164. "f" => $from_user_id,
  165. "m" => $messagesan,
  166. "username" => get_lang('Me')
  167. );
  168. $_SESSION['chatHistory'][$to_user_id]['items'][] = $item;
  169. $_SESSION['chatHistory'][$to_user_id]['user_info']['user_name'] = $user_info['complete_name'];
  170. $_SESSION['chatHistory'][$to_user_id]['user_info']['online'] = $user_info['user_is_online'];
  171. unset($_SESSION['tsChatBoxes'][$to_user_id]);
  172. $params = array();
  173. $params['from_user'] = intval($from_user_id);
  174. $params['to_user'] = intval($to_user_id);
  175. $params['message'] = $message;
  176. $params['sent'] = api_get_utc_datetime();
  177. if (!empty($from_user_id) && !empty($to_user_id)) {
  178. $this->save($params);
  179. }
  180. if ($printResult) {
  181. echo "1";
  182. exit;
  183. }
  184. } else {
  185. if ($printResult) {
  186. echo "0";
  187. exit;
  188. }
  189. }
  190. }
  191. /**
  192. * Close a specific chat box (user ID taken from $_POST['chatbox'])
  193. * @return void Prints "1"
  194. */
  195. public function close()
  196. {
  197. unset($_SESSION['openChatBoxes'][$_POST['chatbox']]);
  198. unset($_SESSION['chatHistory'][$_POST['chatbox']]);
  199. echo "1";
  200. exit;
  201. }
  202. /**
  203. * Filter chat messages to avoid XSS or other JS
  204. * @param string Unfiltered message
  205. * @return string Filterd mssage
  206. */
  207. public function sanitize($text)
  208. {
  209. $text = htmlspecialchars($text, ENT_QUOTES);
  210. $text = str_replace("\n\r", "\n", $text);
  211. $text = str_replace("\r\n", "\n", $text);
  212. $text = str_replace("\n", "<br>", $text);
  213. return $text;
  214. }
  215. public function is_chat_blocked_by_exercises()
  216. {
  217. if (isset($_SESSION['current_exercises'])) {
  218. foreach ($_SESSION['current_exercises'] as $attempt_status) {
  219. if ($attempt_status == true) {
  220. return true;
  221. }
  222. }
  223. }
  224. return false;
  225. }
  226. }