chat_chat.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Chat frame that shows the message list
  5. *
  6. * @author Olivier Brouckaert
  7. * @package chamilo.chat
  8. */
  9. /**
  10. * Code
  11. */
  12. define('FRAME', 'chat');
  13. $language_file = array('chat');
  14. //require_once '../inc/global.inc.php';
  15. $course = $_GET['cidReq'];
  16. $session_id = api_get_session_id();
  17. $group_id = api_get_group_id();
  18. // if we have the session set up
  19. if (!empty($course)) {
  20. $reset = isset($_GET['reset']) ? (bool)$_GET['reset'] : null;
  21. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  22. $query = "SELECT username FROM $tbl_user WHERE user_id='".intval($_user['user_id'])."'";
  23. $result = Database::query($query);
  24. list($pseudo_user) = Database::fetch_row($result);
  25. $isAllowed = !(empty($pseudo_user) || !$_cid);
  26. $isMaster = (bool)api_is_course_admin();
  27. $date_now = date('Y-m-d');
  28. $basepath_chat = '';
  29. $document_path = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  30. if (!empty($group_id)) {
  31. $group_info = GroupManager :: get_group_properties($group_id);
  32. $basepath_chat = $group_info['directory'].'/chat_files';
  33. } else {
  34. $basepath_chat = '/chat_files';
  35. }
  36. $chat_path = $document_path.$basepath_chat.'/';
  37. $TABLEITEMPROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
  38. $course_id = api_get_course_int_id();
  39. if (!is_dir($chat_path)) {
  40. if (is_file($chat_path)) {
  41. @unlink($chat_path);
  42. }
  43. if (!api_is_anonymous()) {
  44. @mkdir($chat_path, api_get_permissions_for_new_directories());
  45. // Save chat files document for group into item property
  46. if (!empty($group_id)) {
  47. $doc_id = FileManager::add_document($_course, $basepath_chat, 'folder', 0, 'chat_files');
  48. $sql = "INSERT INTO $TABLEITEMPROPERTY (c_id, tool,insert_user_id,insert_date,lastedit_date,ref,lastedit_type,lastedit_user_id,to_group_id,to_user_id,visibility)
  49. VALUES ($course_id, 'document',1,NOW(),NOW(),$doc_id,'FolderCreated',1,$group_id,NULL,0)";
  50. Database::query($sql);
  51. }
  52. }
  53. }
  54. $filename_chat = '';
  55. if (!empty($group_id)) {
  56. $filename_chat = 'messages-'.$date_now.'_gid-'.$group_id.'.log.html';
  57. } else if (!empty($session_id)) {
  58. $filename_chat = 'messages-'.$date_now.'_sid-'.$session_id.'.log.html';
  59. } else {
  60. $filename_chat = 'messages-'.$date_now.'.log.html';
  61. }
  62. if (!file_exists($chat_path.$filename_chat)) {
  63. @fclose(fopen($chat_path.$filename_chat, 'w'));
  64. if (!api_is_anonymous()) {
  65. $doc_id = FileManager::add_document($_course, $basepath_chat.'/'.$filename_chat, 'file', 0, $filename_chat);
  66. api_item_property_update(
  67. $_course,
  68. TOOL_DOCUMENT,
  69. $doc_id,
  70. 'DocumentAdded',
  71. $_user['user_id'],
  72. $group_id,
  73. null,
  74. null,
  75. null,
  76. $session_id
  77. );
  78. api_item_property_update(
  79. $_course,
  80. TOOL_DOCUMENT,
  81. $doc_id,
  82. 'invisible',
  83. $_user['user_id'],
  84. $group_id,
  85. null,
  86. null,
  87. null,
  88. $session_id
  89. );
  90. FileManager::item_property_update_on_folder($_course, $basepath_chat, $_user['user_id']);
  91. }
  92. }
  93. $basename_chat = '';
  94. if (!empty($group_id)) {
  95. $basename_chat = 'messages-'.$date_now.'_gid-'.$group_id;
  96. } else {
  97. if (!empty($session_id)) {
  98. $basename_chat = 'messages-'.$date_now.'_sid-'.$session_id;
  99. } else {
  100. $basename_chat = 'messages-'.$date_now;
  101. }
  102. }
  103. if ($reset && $isMaster) {
  104. $i = 1;
  105. while (file_exists($chat_path.$basename_chat.'-'.$i.'.log.html')) {
  106. $i++;
  107. }
  108. @rename($chat_path.$basename_chat.'.log.html', $chat_path.$basename_chat.'-'.$i.'.log.html');
  109. @fclose(fopen($chat_path.$basename_chat.'.log.html', 'w'));
  110. $doc_id = FileManager::add_document(
  111. $_course,
  112. $basepath_chat.'/'.$basename_chat.'-'.$i.'.log.html',
  113. 'file',
  114. filesize($chat_path.$basename_chat.'-'.$i.'.log.html'),
  115. $basename_chat.'-'.$i.'.log.html'
  116. );
  117. api_item_property_update(
  118. $_course,
  119. TOOL_DOCUMENT,
  120. $doc_id,
  121. 'DocumentAdded',
  122. $_user['user_id'],
  123. $group_id,
  124. null,
  125. null,
  126. null,
  127. $session_id
  128. );
  129. api_item_property_update(
  130. $_course,
  131. TOOL_DOCUMENT,
  132. $doc_id,
  133. 'invisible',
  134. $_user['user_id'],
  135. $group_id,
  136. null,
  137. null,
  138. null,
  139. $session_id
  140. );
  141. FileManager::item_property_update_on_folder($_course, $basepath_chat, $_user['user_id']);
  142. $doc_id = DocumentManager::get_document_id($_course, $basepath_chat.'/'.$basename_chat.'.log.html');
  143. FileManager::update_existing_document($_course, $doc_id, 0);
  144. }
  145. $remove = 0;
  146. $content = array();
  147. if (file_exists($chat_path.$basename_chat.'.log.html')) {
  148. $content = file($chat_path.$basename_chat.'.log.html');
  149. $nbr_lines = sizeof($content);
  150. $remove = $nbr_lines - 100;
  151. }
  152. if ($remove < 0) {
  153. $remove = 0;
  154. }
  155. array_splice($content, 0, $remove);
  156. require 'header_frame.inc.php';
  157. if (isset($_GET['origin']) && $_GET['origin'] == 'whoisonline') { //the caller
  158. $content[0] = get_lang('CallSent').'<br />'.$content[0];
  159. }
  160. if (isset($_GET['origin']) && $_GET['origin'] == 'whoisonlinejoin') { //the joiner (we have to delete the chat request to him when he joins the chat)
  161. $track_user_table = Database::get_main_table(TABLE_MAIN_USER);
  162. $sql = "UPDATE $track_user_table set chatcall_user_id = '', chatcall_date = '', chatcall_text='' WHERE (user_id = ".$_user['user_id'].")";
  163. $result = Database::query($sql);
  164. }
  165. echo '<div id="content-chat">';
  166. foreach ($content as & $this_line) {
  167. echo strip_tags(api_html_entity_decode($this_line), '<div> <br> <span> <b> <i> <img> <font>');
  168. }
  169. echo '</div>';
  170. echo '<a name="bottom" style="text-decoration:none;">&nbsp;</a>';
  171. if ($isMaster || $is_courseCoach) {
  172. $rand = mt_rand(1, 1000);
  173. echo '<div id="clear-chat">';
  174. echo '<a href="'.api_get_self().'?rand='.$rand.'&reset=1&'.api_get_cidreq().'#bottom" onclick="javascript: if(!confirm(\''.addslashes(api_htmlentities(get_lang('ConfirmReset'), ENT_QUOTES)).'\')) return false;">'.Display::return_icon('delete.png', get_lang('ClearList')).' '.get_lang('ClearList').'</a>';
  175. echo '</div>';
  176. }
  177. } else {
  178. echo '</div>';
  179. require 'header_frame.inc.php';
  180. $message = get_lang('CloseOtherSession');
  181. Display :: display_error_message($message);
  182. }
  183. require 'footer_frame.inc.php';