chat.lib.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. //print_r($_SESSION['chatHistory']);
  120. $sql = "UPDATE ".$this->table." SET recd = 1 WHERE to_user = '".$to_user_id."' AND recd = 0";
  121. Database::query($sql);
  122. if ($items != '') {
  123. //$items = substr($items, 0, -1);
  124. }
  125. echo json_encode(array('items' => $items));
  126. }
  127. /*
  128. * Returns an array of messages inside a chat session with a specific user
  129. * @param int The ID of the user with whom the current user is chatting
  130. * @return array Messages list
  131. */
  132. function box_session($user_id)
  133. {
  134. $items = array();
  135. if (isset($_SESSION['chatHistory'][$user_id])) {
  136. $items = $_SESSION['chatHistory'][$user_id];
  137. }
  138. return $items;
  139. }
  140. /**
  141. * Saves into session the fact that a chat window exists with the given user
  142. * @param int The ID of the user with whom the current user is chatting
  143. * @return void
  144. */
  145. function save_window($user_id)
  146. {
  147. $this->window_list[$user_id] = true;
  148. $_SESSION['window_list'] = $this->window_list;
  149. }
  150. /**
  151. * Sends a message from one user to another user
  152. * @param int The ID of the user sending the message
  153. * @param int The ID of the user receiving the message
  154. * @param string Message
  155. * @return void Prints "1"
  156. */
  157. function send($from_user_id, $to_user_id, $message)
  158. {
  159. $user_friend_relation = SocialManager::get_relation_between_contacts($from_user_id, $to_user_id);
  160. if ($user_friend_relation == USER_RELATION_TYPE_FRIEND) {
  161. $user_info = api_get_user_info($to_user_id, true);
  162. $this->save_window($to_user_id);
  163. $_SESSION['openChatBoxes'][$to_user_id] = api_get_utc_datetime();
  164. $messagesan = self::sanitize($message);
  165. if (!isset($_SESSION['chatHistory'][$to_user_id])) {
  166. $_SESSION['chatHistory'][$to_user_id] = array();
  167. }
  168. $item = array("s" => "1",
  169. "f" => $from_user_id,
  170. "m" => $messagesan,
  171. "username" => get_lang('Me')
  172. );
  173. $_SESSION['chatHistory'][$to_user_id]['items'][] = $item;
  174. $_SESSION['chatHistory'][$to_user_id]['user_info']['user_name'] = $user_info['complete_name'];
  175. $_SESSION['chatHistory'][$to_user_id]['user_info']['online'] = $user_info['user_is_online'];
  176. unset($_SESSION['tsChatBoxes'][$to_user_id]);
  177. $params = array();
  178. $params['from_user'] = intval($from_user_id);
  179. $params['to_user'] = intval($to_user_id);
  180. $params['message'] = $message;
  181. $params['sent'] = api_get_utc_datetime();
  182. if (!empty($from_user_id) && !empty($to_user_id)) {
  183. $this->save($params);
  184. }
  185. //print_r($_SESSION['chatHistory']);
  186. echo "1";
  187. exit;
  188. } else {
  189. echo "0";
  190. exit;
  191. }
  192. }
  193. /**
  194. * Close a specific chat box (user ID taken from $_POST['chatbox'])
  195. * @return void Prints "1"
  196. */
  197. function close()
  198. {
  199. unset($_SESSION['openChatBoxes'][$_POST['chatbox']]);
  200. unset($_SESSION['chatHistory'][$_POST['chatbox']]);
  201. echo "1";
  202. exit;
  203. }
  204. /**
  205. * Filter chat messages to avoid XSS or other JS
  206. * @param string Unfiltered message
  207. * @return string Filterd mssage
  208. */
  209. function sanitize($text)
  210. {
  211. $text = htmlspecialchars($text, ENT_QUOTES);
  212. $text = str_replace("\n\r", "\n", $text);
  213. $text = str_replace("\r\n", "\n", $text);
  214. $text = str_replace("\n", "<br>", $text);
  215. return $text;
  216. }
  217. function is_chat_blocked_by_exercises()
  218. {
  219. if (isset($_SESSION['current_exercises'])) {
  220. foreach ($_SESSION['current_exercises'] as $attempt_status) {
  221. if ($attempt_status == true) {
  222. return true;
  223. }
  224. }
  225. }
  226. return false;
  227. }
  228. }