chat.lib.php 9.9 KB

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