chat_chat.php 5.8 KB

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