chat_chat.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 = intval($_SESSION['id_session']);
  17. $group_id = intval($_SESSION['_gid']);
  18. // if we have the session set up
  19. if (!empty($course)) {
  20. $reset = (bool)$_GET['reset'];
  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 {
  58. if (!empty($session_id)) {
  59. $filename_chat = 'messages-'.$date_now.'_sid-'.$session_id.'.log.html';
  60. } else {
  61. $filename_chat = 'messages-'.$date_now.'.log.html';
  62. }
  63. }
  64. if (!file_exists($chat_path.$filename_chat)) {
  65. @fclose(fopen($chat_path.$filename_chat, 'w'));
  66. if (!api_is_anonymous()) {
  67. $doc_id = FileManager::add_document($_course, $basepath_chat.'/'.$filename_chat, 'file', 0, $filename_chat);
  68. api_item_property_update(
  69. $_course,
  70. TOOL_DOCUMENT,
  71. $doc_id,
  72. 'DocumentAdded',
  73. $_user['user_id'],
  74. $group_id,
  75. null,
  76. null,
  77. null,
  78. $session_id
  79. );
  80. api_item_property_update(
  81. $_course,
  82. TOOL_DOCUMENT,
  83. $doc_id,
  84. 'invisible',
  85. $_user['user_id'],
  86. $group_id,
  87. null,
  88. null,
  89. null,
  90. $session_id
  91. );
  92. FileManager::item_property_update_on_folder($_course, $basepath_chat, $_user['user_id']);
  93. }
  94. }
  95. $basename_chat = '';
  96. if (!empty($group_id)) {
  97. $basename_chat = 'messages-'.$date_now.'_gid-'.$group_id;
  98. } else {
  99. if (!empty($session_id)) {
  100. $basename_chat = 'messages-'.$date_now.'_sid-'.$session_id;
  101. } else {
  102. $basename_chat = 'messages-'.$date_now;
  103. }
  104. }
  105. if ($reset && $isMaster) {
  106. $i = 1;
  107. while (file_exists($chat_path.$basename_chat.'-'.$i.'.log.html')) {
  108. $i++;
  109. }
  110. @rename($chat_path.$basename_chat.'.log.html', $chat_path.$basename_chat.'-'.$i.'.log.html');
  111. @fclose(fopen($chat_path.$basename_chat.'.log.html', 'w'));
  112. $doc_id = FileManager::add_document(
  113. $_course,
  114. $basepath_chat.'/'.$basename_chat.'-'.$i.'.log.html',
  115. 'file',
  116. filesize($chat_path.$basename_chat.'-'.$i.'.log.html'),
  117. $basename_chat.'-'.$i.'.log.html'
  118. );
  119. api_item_property_update(
  120. $_course,
  121. TOOL_DOCUMENT,
  122. $doc_id,
  123. 'DocumentAdded',
  124. $_user['user_id'],
  125. $group_id,
  126. null,
  127. null,
  128. null,
  129. $session_id
  130. );
  131. api_item_property_update(
  132. $_course,
  133. TOOL_DOCUMENT,
  134. $doc_id,
  135. 'invisible',
  136. $_user['user_id'],
  137. $group_id,
  138. null,
  139. null,
  140. null,
  141. $session_id
  142. );
  143. FileManager::item_property_update_on_folder($_course, $basepath_chat, $_user['user_id']);
  144. $doc_id = DocumentManager::get_document_id($_course, $basepath_chat.'/'.$basename_chat.'.log.html');
  145. FileManager::update_existing_document($_course, $doc_id, 0);
  146. }
  147. $remove = 0;
  148. $content = array();
  149. if (file_exists($chat_path.$basename_chat.'.log.html')) {
  150. $content = file($chat_path.$basename_chat.'.log.html');
  151. $nbr_lines = sizeof($content);
  152. $remove = $nbr_lines - 100;
  153. }
  154. if ($remove < 0) {
  155. $remove = 0;
  156. }
  157. array_splice($content, 0, $remove);
  158. require 'header_frame.inc.php';
  159. if ($_GET['origin'] == 'whoisonline') { //the caller
  160. $content[0] = get_lang('CallSent').'<br />'.$content[0];
  161. }
  162. if ($_GET['origin'] == 'whoisonlinejoin') { //the joiner (we have to delete the chat request to him when he joins the chat)
  163. $track_user_table = Database::get_main_table(TABLE_MAIN_USER);
  164. $sql = "UPDATE $track_user_table set chatcall_user_id = '', chatcall_date = '', chatcall_text='' WHERE (user_id = ".$_user['user_id'].")";
  165. $result = Database::query($sql);
  166. }
  167. echo '<div style="margin-left: 5px;">';
  168. foreach ($content as & $this_line) {
  169. echo strip_tags(api_html_entity_decode($this_line), '<br> <span> <b> <i> <img> <font>');
  170. }
  171. echo '</div>';
  172. ?>
  173. <a name="bottom" style="text-decoration:none;">&nbsp;</a>
  174. <?php
  175. if ($isMaster || api_is_course_coach()) {
  176. $rand = mt_rand(1, 1000);
  177. echo '<div style="margin-left: 5px;">';
  178. echo '<a href="'.api_get_self(
  179. ).'?rand='.$rand.'&reset=1&cidReq='.$_GET['cidReq'].'#bottom" onclick="javascript: if(!confirm(\''.addslashes(
  180. api_htmlentities(get_lang('ConfirmReset'), ENT_QUOTES)
  181. ).'\')) return false;">'.Display::return_icon('delete.gif', get_lang('ClearList')).' '.get_lang(
  182. 'ClearList'
  183. ).'</a>';
  184. echo '</div>';
  185. }
  186. } else {
  187. require 'header_frame.inc.php';
  188. $message = get_lang('CloseOtherSession');
  189. Display :: display_error_message($message);
  190. }
  191. require 'footer_frame.inc.php';