chat.lib.php 10.0 KB

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