chat.lib.php 8.5 KB

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