chat.lib.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class Chat
  5. * @todo ChamiloSession instead of $_SESSION
  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. parent::__construct();
  20. $this->table = Database::get_main_table(TABLE_MAIN_CHAT);
  21. $this->window_list = $_SESSION['window_list'] = isset($_SESSION['window_list']) ? $_SESSION['window_list'] : array();
  22. }
  23. /**
  24. * Get user chat status
  25. * @return int 0 if disconnected, 1 if connected
  26. */
  27. public function get_user_status()
  28. {
  29. $status = UserManager::get_extra_user_data_by_field(
  30. api_get_user_id(),
  31. 'user_chat_status',
  32. false,
  33. true
  34. );
  35. return $status['user_chat_status'];
  36. }
  37. /**
  38. * Set user chat status
  39. * @param int 0 if disconnected, 1 if connected
  40. * @param integer $status
  41. *
  42. * @return void
  43. */
  44. public function setUserStatus($status)
  45. {
  46. UserManager::update_extra_field_value(api_get_user_id(), 'user_chat_status', $status);
  47. }
  48. /**
  49. * Starts a chat session and returns JSON array of status and chat history
  50. * @return void (prints output in JSON format)
  51. */
  52. public function startSession()
  53. {
  54. $items = array();
  55. if (isset($_SESSION['chatHistory'])) {
  56. $items = $_SESSION['chatHistory'];
  57. }
  58. $return = array(
  59. 'user_status' => $this->get_user_status(),
  60. 'me' => get_lang('Me'),
  61. 'items' => $items
  62. );
  63. echo json_encode($return);
  64. exit;
  65. }
  66. /**
  67. * Refreshes the chat windows (usually called every x seconds through AJAX)
  68. * @return void (prints JSON array of chat windows)
  69. */
  70. public function heartbeat()
  71. {
  72. $to_user_id = api_get_user_id();
  73. $sql = "SELECT * FROM ".$this->table."
  74. WHERE to_user = '".intval($to_user_id)."' AND ( recd = 0 )
  75. ORDER BY id ASC";
  76. $result = Database::query($sql);
  77. $chat_list = array();
  78. while ($chat = Database::fetch_array($result, 'ASSOC')) {
  79. $chat_list[$chat['from_user']]['items'][] = $chat;
  80. }
  81. $items = array();
  82. foreach ($chat_list as $from_user_id => $rows) {
  83. $rows = $rows['items'];
  84. $user_info = api_get_user_info($from_user_id, true);
  85. //Cleaning tsChatBoxes
  86. unset($_SESSION['tsChatBoxes'][$from_user_id]);
  87. foreach ($rows as $chat) {
  88. $chat['message'] = Security::remove_XSS($chat['message']);
  89. $item = array(
  90. 's' => '0',
  91. 'f' => $from_user_id,
  92. 'm' => $chat['message'],
  93. 'username' => $user_info['complete_name'],
  94. 'id' => $chat['id']
  95. );
  96. $items[$from_user_id]['items'][] = $item;
  97. $items[$from_user_id]['user_info']['user_name'] = $user_info['complete_name'];
  98. $items[$from_user_id]['user_info']['online'] = $user_info['user_is_online'];
  99. $items[$from_user_id]['user_info']['avatar'] = $user_info['avatar_small'];
  100. $_SESSION['openChatBoxes'][$from_user_id] = api_strtotime($chat['sent'], 'UTC');
  101. }
  102. $_SESSION['chatHistory'][$from_user_id]['items'][] = $item;
  103. $_SESSION['chatHistory'][$from_user_id]['user_info']['user_name'] = $user_info['complete_name'];
  104. $_SESSION['chatHistory'][$from_user_id]['user_info']['online'] = $user_info['user_is_online'];
  105. $_SESSION['chatHistory'][$from_user_id]['user_info']['avatar'] = $user_info['avatar_small'];
  106. }
  107. if (!empty($_SESSION['openChatBoxes'])) {
  108. foreach ($_SESSION['openChatBoxes'] as $user_id => $time) {
  109. if (!isset($_SESSION['tsChatBoxes'][$user_id])) {
  110. $now = time() - $time;
  111. $time = api_convert_and_format_date($time, DATE_TIME_FORMAT_SHORT_TIME_FIRST);
  112. $message = sprintf(get_lang('SentAtX'), $time);
  113. if ($now > 180) {
  114. $item = array('s' => '2', 'f' => $user_id, 'm' => $message);
  115. if (isset($_SESSION['chatHistory'][$user_id])) {
  116. $_SESSION['chatHistory'][$user_id]['items'][] = $item;
  117. }
  118. $_SESSION['tsChatBoxes'][$user_id] = 1;
  119. }
  120. }
  121. }
  122. }
  123. $sql = "UPDATE ".$this->table." SET recd = 1
  124. WHERE to_user = '".$to_user_id."' AND recd = 0";
  125. Database::query($sql);
  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. * @param integer $user_id
  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. *
  160. * @return void Prints "1"
  161. */
  162. public function send(
  163. $from_user_id,
  164. $to_user_id,
  165. $message,
  166. $printResult = true,
  167. $sanitize = true
  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. if (!isset($_SESSION['chatHistory'][$to_user_id])) {
  183. $_SESSION['chatHistory'][$to_user_id] = array();
  184. }
  185. $item = array(
  186. "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['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. }