chat.lib.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * Class Chat
  6. *
  7. * @package chamilo.library.chat
  8. */
  9. class Chat extends Model
  10. {
  11. public $table;
  12. public $columns = array('id', 'from_user', 'to_user', 'message', 'sent', 'recd');
  13. public $window_list = array();
  14. /**
  15. * The contructor sets the chat table name and the window_list attribute
  16. * @return object Object reference
  17. */
  18. public function __construct()
  19. {
  20. $this->table = Database::get_main_table(TABLE_MAIN_CHAT);
  21. $this->window_list = Session::write(
  22. 'window_list',
  23. Session::read('window_list', [])
  24. );
  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(
  33. api_get_user_id(),
  34. 'user_chat_status',
  35. false,
  36. true
  37. );
  38. return $status['user_chat_status'];
  39. }
  40. /**
  41. * Set user chat status
  42. * @param int 0 if disconnected, 1 if connected
  43. *
  44. * @return void
  45. */
  46. public function setUserStatus($status)
  47. {
  48. UserManager::update_extra_field_value(api_get_user_id(), 'user_chat_status', $status);
  49. }
  50. /**
  51. * Starts a chat session and returns JSON array of status and chat history
  52. * @return void (prints output in JSON format)
  53. */
  54. public function startSession()
  55. {
  56. $items = Session::read('chatHistory', []);
  57. $return = array(
  58. 'user_status' => $this->get_user_status(),
  59. 'me' => get_lang('Me'),
  60. 'items' => $items
  61. );
  62. echo json_encode($return);
  63. exit;
  64. }
  65. /**
  66. * Refreshes the chat windows (usually called every x seconds through AJAX)
  67. * @return void (prints JSON array of chat windows)
  68. */
  69. public function heartbeat()
  70. {
  71. $to_user_id = api_get_user_id();
  72. $sql = "SELECT * FROM ".$this->table."
  73. WHERE to_user = '".intval($to_user_id)."' AND ( recd = 0 )
  74. 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. $items[$from_user_id]['user_info']['avatar'] = $user_info['avatar_small'];
  98. $_SESSION['openChatBoxes'][$from_user_id] = api_strtotime($chat['sent'], 'UTC');
  99. }
  100. $_SESSION['chatHistory'][$from_user_id]['items'][] = $item;
  101. $_SESSION['chatHistory'][$from_user_id]['user_info']['user_name'] = $user_info['complete_name'];
  102. $_SESSION['chatHistory'][$from_user_id]['user_info']['online'] = $user_info['user_is_online'];
  103. $_SESSION['chatHistory'][$from_user_id]['user_info']['avatar'] = $user_info['avatar_small'];
  104. }
  105. if (!empty($_SESSION['openChatBoxes'])) {
  106. foreach ($_SESSION['openChatBoxes'] as $user_id => $time) {
  107. if (!isset($_SESSION['tsChatBoxes'][$user_id])) {
  108. $now = time() - $time;
  109. $time = api_convert_and_format_date($time, DATE_TIME_FORMAT_SHORT_TIME_FIRST);
  110. $message = sprintf(get_lang('SentAtX'), $time);
  111. if ($now > 180) {
  112. $item = array('s' => '2', 'f' => $user_id, 'm' => $message);
  113. if (isset($_SESSION['chatHistory'][$user_id])) {
  114. $_SESSION['chatHistory'][$user_id]['items'][] = $item;
  115. }
  116. $_SESSION['tsChatBoxes'][$user_id] = 1;
  117. }
  118. }
  119. }
  120. }
  121. $sql = "UPDATE ".$this->table." SET recd = 1
  122. WHERE to_user = '".$to_user_id."' AND recd = 0";
  123. Database::query($sql);
  124. if ($items != '') {
  125. //$items = substr($items, 0, -1);
  126. }
  127. echo json_encode(array('items' => $items));
  128. }
  129. /**
  130. * Returns an array of messages inside a chat session with a specific user
  131. * @param int The ID of the user with whom the current user is chatting
  132. * @return array Messages list
  133. */
  134. public function box_session($user_id)
  135. {
  136. $items = array();
  137. if (isset($_SESSION['chatHistory'][$user_id])) {
  138. $items = $_SESSION['chatHistory'][$user_id];
  139. }
  140. return $items;
  141. }
  142. /**
  143. * Saves into session the fact that a chat window exists with the given user
  144. * @param int The ID of the user with whom the current user is chatting
  145. * @return void
  146. */
  147. public function save_window($user_id)
  148. {
  149. $this->window_list[$user_id] = true;
  150. $_SESSION['window_list'] = $this->window_list;
  151. }
  152. /**
  153. * Sends a message from one user to another user
  154. * @param int $from_user_id The ID of the user sending the message
  155. * @param int $to_user_id The ID of the user receiving the message
  156. * @param string $message Message
  157. * @param boolean $printResult Optional. Whether print the result
  158. * @param boolean $sanitize Optional. Whether sanitize the message
  159. * @return void Prints "1"
  160. */
  161. public function send(
  162. $from_user_id,
  163. $to_user_id,
  164. $message,
  165. $printResult = true,
  166. $sanitize = true
  167. )
  168. {
  169. $user_friend_relation = SocialManager::get_relation_between_contacts(
  170. $from_user_id,
  171. $to_user_id
  172. );
  173. if ($user_friend_relation == USER_RELATION_TYPE_FRIEND) {
  174. $user_info = api_get_user_info($to_user_id, true);
  175. $this->save_window($to_user_id);
  176. $_SESSION['openChatBoxes'][$to_user_id] = api_get_utc_datetime();
  177. if ($sanitize) {
  178. $messagesan = self::sanitize($message);
  179. } else {
  180. $messagesan = $message;
  181. }
  182. error_log(print_r($sanitize) . '----' . $messagesan);
  183. if (!isset($_SESSION['chatHistory'][$to_user_id])) {
  184. $_SESSION['chatHistory'][$to_user_id] = array();
  185. }
  186. $item = array("s" => "1",
  187. "f" => $from_user_id,
  188. "m" => $messagesan,
  189. "username" => get_lang('Me')
  190. );
  191. $_SESSION['chatHistory'][$to_user_id]['items'][] = $item;
  192. $_SESSION['chatHistory'][$to_user_id]['user_info']['user_name'] = $user_info['complete_name'];
  193. $_SESSION['chatHistory'][$to_user_id]['user_info']['online'] = $user_info['user_is_online'];
  194. $_SESSION['chatHistory'][$to_user_id]['user_info']['avatar'] = $user_info['avatar_small'];
  195. unset($_SESSION['tsChatBoxes'][$to_user_id]);
  196. $params = array();
  197. $params['from_user'] = intval($from_user_id);
  198. $params['to_user'] = intval($to_user_id);
  199. $params['message'] = $message;
  200. $params['sent'] = api_get_utc_datetime();
  201. if (!empty($from_user_id) && !empty($to_user_id)) {
  202. $this->save($params);
  203. }
  204. if ($printResult) {
  205. echo "1";
  206. exit;
  207. }
  208. } else {
  209. if ($printResult) {
  210. echo "0";
  211. exit;
  212. }
  213. }
  214. }
  215. /**
  216. * Close a specific chat box (user ID taken from $_POST['chatbox'])
  217. * @return void Prints "1"
  218. */
  219. public function close()
  220. {
  221. unset($_SESSION['openChatBoxes'][$_POST['chatbox']]);
  222. unset($_SESSION['chatHistory'][$_POST['chatbox']]);
  223. echo "1";
  224. exit;
  225. }
  226. /**
  227. * Filter chat messages to avoid XSS or other JS
  228. * @param string $text Unfiltered message
  229. *
  230. * @return string Filterd mssage
  231. */
  232. public function sanitize($text)
  233. {
  234. $text = htmlspecialchars($text, ENT_QUOTES);
  235. $text = str_replace("\n\r", "\n", $text);
  236. $text = str_replace("\r\n", "\n", $text);
  237. $text = str_replace("\n", "<br>", $text);
  238. return $text;
  239. }
  240. /**
  241. * SET Disable Chat
  242. * @param boolean status to disable chat
  243. * @return void
  244. */
  245. public static function setDisableChat($status = true)
  246. {
  247. Session::write('disable_chat', $status);
  248. }
  249. /**
  250. * Disable Chat - disable the chat
  251. * @return boolean - return true if setDisableChat status is true
  252. */
  253. public static function disableChat()
  254. {
  255. if (!empty($_SESSION['disable_chat'])){
  256. $status = $_SESSION['disable_chat'];
  257. if ($status == true){
  258. $_SESSION['disable_chat'] = null;
  259. return true;
  260. }
  261. }
  262. return false;
  263. }
  264. public function is_chat_blocked_by_exercises()
  265. {
  266. if (isset($_SESSION['current_exercises'])) {
  267. foreach ($_SESSION['current_exercises'] as $attempt_status) {
  268. if ($attempt_status == true) {
  269. return true;
  270. }
  271. }
  272. }
  273. return false;
  274. }
  275. }