chat.lib.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. var $table;
  14. var $columns = array('id', 'from_user','to_user','message','sent','recd');
  15. var $window_list = array();
  16. /**
  17. * The contructor sets the chat table name and the window_list attribute
  18. * @return object Object reference
  19. */
  20. public function __construct() {
  21. $this->table = Database::get_main_table(TABLE_MAIN_CHAT);
  22. $this->window_list = $_SESSION['window_list'] = isset($_SESSION['window_list']) ? $_SESSION['window_list'] : array();
  23. }
  24. /**
  25. * Get user chat status
  26. * @return int 0 if disconnected, 1 if connected
  27. */
  28. function get_user_status() {
  29. $status = UserManager::get_extra_user_data_by_field(api_get_user_id(), 'user_chat_status', false, true);
  30. return $status['user_chat_status'];
  31. }
  32. /*
  33. * Set user chat status
  34. * @param int 0 if disconnected, 1 if connected
  35. * @return void
  36. */
  37. function set_user_status($status) {
  38. UserManager::update_extra_field_value(api_get_user_id(), 'user_chat_status', $status);
  39. }
  40. /*
  41. * Starts a chat session and returns JSON array of status and chat history
  42. * @return void (prints output in JSON format)
  43. */
  44. public function start_session() {
  45. $items = array();
  46. if (isset($_SESSION['chatHistory'])) {
  47. $items = $_SESSION['chatHistory'];
  48. }
  49. //print_r($items);
  50. $return = array('user_status' => $this->get_user_status(), 'me' => get_lang('Me'), 'items' => $items);
  51. echo json_encode($return);
  52. exit;
  53. }
  54. /**
  55. * Refreshes the chat windows (usually called every x seconds through AJAX)
  56. * @return void (prints JSON array of chat windows)
  57. */
  58. public function heartbeat() {
  59. $to_user_id = api_get_user_id();
  60. $minutes = 60;
  61. $now = time() - $minutes*60;
  62. $now = api_get_utc_datetime($now);
  63. //OR sent > '$now'
  64. $sql = "SELECT * FROM ".$this->table."
  65. WHERE to_user = '".intval($to_user_id)."' AND ( recd = 0 ) ORDER BY id ASC";
  66. $result = Database::query($sql);
  67. $chat_list = array();
  68. while ($chat = Database::fetch_array($result,'ASSOC')) {
  69. $chat_list[$chat['from_user']]['items'][] = $chat;
  70. }
  71. $items = array();
  72. foreach ($chat_list as $from_user_id => $rows) {
  73. $rows = $rows['items'];
  74. $user_info = api_get_user_info($from_user_id, true);
  75. //Cleaning tsChatBoxes
  76. unset($_SESSION['tsChatBoxes'][$from_user_id]);
  77. foreach ($rows as $chat) {
  78. $chat['message'] = Security::remove_XSS($chat['message']);
  79. $item = array( 's' => '0',
  80. 'f' => $from_user_id,
  81. 'm' => $chat['message'],
  82. 'username' => $user_info['complete_name'],
  83. 'id' => $chat['id']
  84. );
  85. $items[$from_user_id]['items'][] = $item;
  86. $items[$from_user_id]['user_info']['user_name'] = $user_info['complete_name'];
  87. $items[$from_user_id]['user_info']['online'] = $user_info['user_is_online'];
  88. $_SESSION['openChatBoxes'][$from_user_id] = api_strtotime($chat['sent'],'UTC');
  89. }
  90. $_SESSION['chatHistory'][$from_user_id]['items'][] = $item;
  91. $_SESSION['chatHistory'][$from_user_id]['user_info']['user_name'] = $user_info['complete_name'];
  92. $_SESSION['chatHistory'][$from_user_id]['user_info']['online'] = $user_info['user_is_online'];
  93. }
  94. if (!empty($_SESSION['openChatBoxes'])) {
  95. foreach ($_SESSION['openChatBoxes'] as $user_id => $time) {
  96. if (!isset($_SESSION['tsChatBoxes'][$user_id])) {
  97. $now = time() - $time;
  98. $time = api_convert_and_format_date($time, DATE_TIME_FORMAT_SHORT_TIME_FIRST);
  99. $message = sprintf(get_lang('SentAtX'), $time);
  100. if ($now > 180) {
  101. $item = array('s' => '2', 'f' => $user_id, 'm' => $message);
  102. if (isset($_SESSION['chatHistory'][$user_id])) {
  103. $_SESSION['chatHistory'][$user_id]['items'][] = $item;
  104. }
  105. $_SESSION['tsChatBoxes'][$user_id] = 1;
  106. }
  107. }
  108. }
  109. }
  110. //print_r($_SESSION['chatHistory']);
  111. /*
  112. var_dump($_SESSION['openChatBoxes']);
  113. var_dump($_SESSION['tsChatBoxes']);
  114. var_dump($_SESSION['chatHistory']);
  115. var_dump($items);
  116. */
  117. //print_r($_SESSION['chatHistory']);
  118. $sql = "UPDATE ".$this->table." SET recd = 1 WHERE to_user = '".$to_user_id."' AND recd = 0";
  119. Database::query($sql);
  120. if ($items != '') {
  121. //$items = substr($items, 0, -1);
  122. }
  123. echo json_encode(array('items' => $items));
  124. }
  125. /*
  126. * Returns an array of messages inside a chat session with a specific user
  127. * @param int The ID of the user with whom the current user is chatting
  128. * @return array Messages list
  129. */
  130. function box_session($user_id) {
  131. $items = array();
  132. if (isset($_SESSION['chatHistory'][$user_id])) {
  133. $items = $_SESSION['chatHistory'][$user_id];
  134. }
  135. return $items;
  136. }
  137. /**
  138. * Saves into session the fact that a chat window exists with the given user
  139. * @param int The ID of the user with whom the current user is chatting
  140. * @return void
  141. */
  142. function save_window($user_id){
  143. $this->window_list[$user_id] = true;
  144. $_SESSION['window_list'] = $this->window_list;
  145. }
  146. /**
  147. * Sends a message from one user to another user
  148. * @param int The ID of the user sending the message
  149. * @param int The ID of the user receiving the message
  150. * @param string Message
  151. * @return void Prints "1"
  152. */
  153. function send($from_user_id, $to_user_id, $message) {
  154. $user_friend_relation = SocialManager::get_relation_between_contacts($from_user_id, $to_user_id);
  155. if ($user_friend_relation == USER_RELATION_TYPE_FRIEND) {
  156. $user_info = api_get_user_info($to_user_id, true);
  157. $this->save_window($to_user_id);
  158. $_SESSION['openChatBoxes'][$to_user_id] = api_get_utc_datetime();
  159. $messagesan = self::sanitize($message);
  160. if (!isset($_SESSION['chatHistory'][$to_user_id])) {
  161. $_SESSION['chatHistory'][$to_user_id] = array();
  162. }
  163. $item = array ( "s" => "1",
  164. "f" => $from_user_id,
  165. "m" => $messagesan,
  166. "username" => get_lang('Me')
  167. );
  168. $_SESSION['chatHistory'][$to_user_id]['items'][] = $item;
  169. $_SESSION['chatHistory'][$to_user_id]['user_info']['user_name'] = $user_info['complete_name'];
  170. $_SESSION['chatHistory'][$to_user_id]['user_info']['online'] = $user_info['user_is_online'];
  171. unset($_SESSION['tsChatBoxes'][$to_user_id]);
  172. $params = array();
  173. $params['from_user'] = intval($from_user_id);
  174. $params['to_user'] = intval($to_user_id);
  175. $params['message'] = $message;
  176. $params['sent'] = api_get_utc_datetime();
  177. if (!empty($from_user_id) && !empty($to_user_id)) {
  178. $this->save($params);
  179. }
  180. //print_r($_SESSION['chatHistory']);
  181. echo "1";
  182. exit;
  183. } else {
  184. echo "0";
  185. exit;
  186. }
  187. }
  188. /**
  189. * Close a specific chat box (user ID taken from $_POST['chatbox'])
  190. * @return void Prints "1"
  191. */
  192. function close() {
  193. unset($_SESSION['openChatBoxes'][$_POST['chatbox']]);
  194. unset($_SESSION['chatHistory'][$_POST['chatbox']]);
  195. echo "1";
  196. exit;
  197. }
  198. /**
  199. * Filter chat messages to avoid XSS or other JS
  200. * @param string Unfiltered message
  201. * @return string Filterd mssage
  202. */
  203. function sanitize($text) {
  204. $text = htmlspecialchars($text, ENT_QUOTES);
  205. $text = str_replace("\n\r","\n",$text);
  206. $text = str_replace("\r\n","\n",$text);
  207. $text = str_replace("\n","<br>",$text);
  208. return $text;
  209. }
  210. function is_chat_blocked_by_exercises() {
  211. if (isset($_SESSION['current_exercises'])) {
  212. foreach ($_SESSION['current_exercises'] as $attempt_status) {
  213. if ($attempt_status == true) {
  214. return true;
  215. }
  216. }
  217. }
  218. return false;
  219. }
  220. }