chat.lib.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This is the array library for Chamilo.
  5. * Include/require it in your code to use its functionality.
  6. *
  7. * @package chamilo.library
  8. */
  9. class Chat extends Model {
  10. var $table;
  11. var $columns = array('id', 'from_user','to_user','message','sent','recd');
  12. var $window_list = array();
  13. public function __construct() {
  14. $this->table = Database::get_main_table(TABLE_MAIN_CHAT);
  15. $this->window_list = $_SESSION['window_list'] = isset($_SESSION['window_list']) ? $_SESSION['window_list'] : array();
  16. }
  17. /**
  18. * Get user chat status
  19. * @return type
  20. */
  21. function get_user_status() {
  22. $status = UserManager::get_extra_user_data_by_field(api_get_user_id(), 'user_chat_status', false, true);
  23. return $status['user_chat_status'];
  24. }
  25. /*
  26. * Set user chat status
  27. */
  28. function set_user_status($status) {
  29. UserManager::update_extra_field_value(api_get_user_id(), 'user_chat_status', $status);
  30. }
  31. /*
  32. * Starts a chat session
  33. */
  34. public function start_session() {
  35. $items = array();
  36. if (isset($_SESSION['chatHistory'])) {
  37. $items = $_SESSION['chatHistory'];
  38. }
  39. //print_r($items);
  40. $return = array('user_status' => $this->get_user_status(), 'me' => get_lang('Me'), 'items' => $items);
  41. echo json_encode($return);
  42. exit;
  43. }
  44. public function heartbeat() {
  45. $to_user_id = api_get_user_id();
  46. $minutes = 60;
  47. $now = time() - $minutes*60;
  48. $now = api_get_utc_datetime($now);
  49. //OR sent > '$now'
  50. $sql = "SELECT * FROM ".$this->table."
  51. WHERE to_user = '".intval($to_user_id)."' AND ( recd = 0 ) ORDER BY id ASC";
  52. $result = Database::query($sql);
  53. $chat_list = array();
  54. while ($chat = Database::fetch_array($result,'ASSOC')) {
  55. $chat_list[$chat['from_user']]['items'][] = $chat;
  56. }
  57. $items = array();
  58. foreach ($chat_list as $from_user_id => $rows) {
  59. $rows = $rows['items'];
  60. $user_info = api_get_user_info($from_user_id, true);
  61. //Cleaning tsChatBoxes
  62. unset($_SESSION['tsChatBoxes'][$from_user_id]);
  63. foreach ($rows as $chat) {
  64. $chat['message'] = Security::remove_XSS($chat['message']);
  65. $item = array( 's' => '0',
  66. 'f' => $from_user_id,
  67. 'm' => $chat['message'],
  68. 'username' => $user_info['complete_name'],
  69. 'id' => $chat['id']
  70. );
  71. $items[$from_user_id]['items'][] = $item;
  72. $items[$from_user_id]['user_info']['user_name'] = $user_info['complete_name'];
  73. $items[$from_user_id]['user_info']['online'] = $user_info['user_is_online'];
  74. $_SESSION['openChatBoxes'][$from_user_id] = api_strtotime($chat['sent'],'UTC');
  75. }
  76. $_SESSION['chatHistory'][$from_user_id]['items'][] = $item;
  77. $_SESSION['chatHistory'][$from_user_id]['user_info']['user_name'] = $user_info['complete_name'];
  78. $_SESSION['chatHistory'][$from_user_id]['user_info']['online'] = $user_info['user_is_online'];
  79. }
  80. if (!empty($_SESSION['openChatBoxes'])) {
  81. foreach ($_SESSION['openChatBoxes'] as $user_id => $time) {
  82. if (!isset($_SESSION['tsChatBoxes'][$user_id])) {
  83. $now = time() - $time;
  84. $time = api_convert_and_format_date($time, DATE_TIME_FORMAT_SHORT_TIME_FIRST);
  85. $message = sprintf(get_lang('SentAtX'), $time);
  86. if ($now > 180) {
  87. $item = array('s' => '2', 'f' => $user_id, 'm' => $message);
  88. if (isset($_SESSION['chatHistory'][$user_id])) {
  89. $_SESSION['chatHistory'][$user_id]['items'][] = $item;
  90. }
  91. $_SESSION['tsChatBoxes'][$user_id] = 1;
  92. }
  93. }
  94. }
  95. }
  96. //print_r($_SESSION['chatHistory']);
  97. /*
  98. var_dump($_SESSION['openChatBoxes']);
  99. var_dump($_SESSION['tsChatBoxes']);
  100. var_dump($_SESSION['chatHistory']);
  101. var_dump($items);
  102. */
  103. //print_r($_SESSION['chatHistory']);
  104. $sql = "UPDATE ".$this->table." SET recd = 1 WHERE to_user = '".$to_user_id."' AND recd = 0";
  105. Database::query($sql);
  106. if ($items != '') {
  107. //$items = substr($items, 0, -1);
  108. }
  109. echo json_encode(array('items' => $items));
  110. }
  111. /*
  112. * chatBoxSession
  113. */
  114. function box_session($user_id) {
  115. $items = array();
  116. if (isset($_SESSION['chatHistory'][$user_id])) {
  117. $items = $_SESSION['chatHistory'][$user_id];
  118. }
  119. return $items;
  120. }
  121. function save_window($user_id){
  122. $this->window_list[$user_id] = true;
  123. $_SESSION['window_list'] = $this->window_list;
  124. }
  125. function send($from_user_id, $to_user_id, $message) {
  126. $user_info = api_get_user_info($to_user_id, true);
  127. $this->save_window($to_user_id);
  128. $_SESSION['openChatBoxes'][$to_user_id] = api_get_utc_datetime();
  129. $messagesan = self::sanitize($message);
  130. if (!isset($_SESSION['chatHistory'][$to_user_id])) {
  131. $_SESSION['chatHistory'][$to_user_id] = array();
  132. }
  133. $item = array ( "s" => "1",
  134. "f" => $from_user_id,
  135. "m" => $messagesan,
  136. "username" => get_lang('Me')
  137. );
  138. $_SESSION['chatHistory'][$to_user_id]['items'][] = $item;
  139. $_SESSION['chatHistory'][$to_user_id]['user_info']['user_name'] = $user_info['complete_name'];
  140. $_SESSION['chatHistory'][$to_user_id]['user_info']['online'] = $user_info['user_is_online'];
  141. unset($_SESSION['tsChatBoxes'][$to_user_id]);
  142. $params = array();
  143. $params['from_user'] = intval($from_user_id);
  144. $params['to_user'] = intval($to_user_id);
  145. $params['message'] = $message;
  146. $params['sent'] = api_get_utc_datetime();
  147. if (!empty($from_user_id) && !empty($to_user_id)) {
  148. $this->save($params);
  149. }
  150. //print_r($_SESSION['chatHistory']);
  151. echo "1";
  152. exit;
  153. }
  154. function close() {
  155. unset($_SESSION['openChatBoxes'][$_POST['chatbox']]);
  156. unset($_SESSION['chatHistory'][$_POST['chatbox']]);
  157. echo "1";
  158. exit;
  159. }
  160. function sanitize($text) {
  161. $text = htmlspecialchars($text, ENT_QUOTES);
  162. $text = str_replace("\n\r","\n",$text);
  163. $text = str_replace("\r\n","\n",$text);
  164. $text = str_replace("\n","<br>",$text);
  165. return $text;
  166. }
  167. }