chat_chat.php 7.0 KB

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