chat_chat.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. define('FRAME', 'chat');
  10. $language_file = array('chat');
  11. require_once '../inc/global.inc.php';
  12. require_once api_get_path(LIBRARY_PATH).'document.lib.php';
  13. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  14. require_once api_get_path(LIBRARY_PATH).'groupmanager.lib.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)$is_courseAdmin;
  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. if (!is_dir($chat_path)) {
  39. if (is_file($chat_path)) {
  40. @unlink($chat_path);
  41. }
  42. if (!api_is_anonymous()) {
  43. @mkdir($chat_path, api_get_permissions_for_new_directories());
  44. // Save chat files document for group into item property
  45. if (!empty($group_id)) {
  46. $doc_id = add_document($_course, $basepath_chat, 'folder', 0, 'chat_files');
  47. $sql = "INSERT INTO $TABLEITEMPROPERTY (tool,insert_user_id,insert_date,lastedit_date,ref,lastedit_type,lastedit_user_id,to_group_id,to_user_id,visibility)
  48. VALUES ('document',1,NOW(),NOW(),$doc_id,'FolderCreated',1,$group_id,NULL,0)";
  49. Database::query($sql);
  50. }
  51. }
  52. }
  53. $filename_chat = '';
  54. if (!empty($group_id)) {
  55. $filename_chat = 'messages-'.$date_now.'_gid-'.$group_id.'.log.html';
  56. } else if (!empty($session_id)) {
  57. $filename_chat = 'messages-'.$date_now.'_sid-'.$session_id.'.log.html';
  58. } else {
  59. $filename_chat = 'messages-'.$date_now.'.log.html';
  60. }
  61. if (!file_exists($chat_path.$filename_chat)) {
  62. @fclose(fopen($chat_path.$filename_chat, 'w'));
  63. if (!api_is_anonymous()) {
  64. $doc_id = add_document($_course, $basepath_chat.'/'.$filename_chat, 'file', 0, $filename_chat);
  65. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'DocumentAdded', $_user['user_id'], $group_id, null, null, null, $session_id);
  66. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'invisible', $_user['user_id'], $group_id, null, null, null, $session_id);
  67. item_property_update_on_folder($_course, $basepath_chat, $_user['user_id']);
  68. }
  69. }
  70. $basename_chat = '';
  71. if (!empty($group_id)) {
  72. $basename_chat = 'messages-'.$date_now.'_gid-'.$group_id;
  73. } else if (!empty($session_id)) {
  74. $basename_chat = 'messages-'.$date_now.'_sid-'.$session_id;
  75. } else {
  76. $basename_chat = 'messages-'.$date_now;
  77. }
  78. if ($reset && $isMaster) {
  79. $i = 1;
  80. while (file_exists($chat_path.$basename_chat.'-'.$i.'.log.html')) {
  81. $i++;
  82. }
  83. @rename($chat_path.$basename_chat.'.log.html', $chat_path.$basename_chat.'-'.$i.'.log.html');
  84. @fclose(fopen($chat_path.$basename_chat.'.log.html', 'w'));
  85. $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');
  86. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'DocumentAdded', $_user['user_id'], $group_id, null, null, null, $session_id);
  87. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'invisible', $_user['user_id'], $group_id, null, null, null, $session_id);
  88. item_property_update_on_folder($_course,$basepath_chat, $_user['user_id']);
  89. $doc_id = DocumentManager::get_document_id($_course, $basepath_chat.'/'.$basename_chat.'.log.html');
  90. update_existing_document($_course, $doc_id, 0);
  91. }
  92. $remove = 0;
  93. $content = array();
  94. if (file_exists($chat_path.$basename_chat.'.log.html')) {
  95. $content = file($chat_path.$basename_chat.'.log.html');
  96. $nbr_lines = sizeof($content);
  97. $remove = $nbr_lines - 100;
  98. }
  99. if ($remove < 0) {
  100. $remove = 0;
  101. }
  102. array_splice($content, 0, $remove);
  103. require 'header_frame.inc.php';
  104. if ($_GET['origin'] == 'whoisonline') { //the caller
  105. $content[0] = get_lang('CallSent').'<br />'.$content[0];
  106. }
  107. if ($_GET['origin'] == 'whoisonlinejoin') { //the joiner (we have to delete the chat request to him when he joins the chat)
  108. $track_user_table = Database::get_main_table(TABLE_MAIN_USER);
  109. $sql = "UPDATE $track_user_table set chatcall_user_id = '', chatcall_date = '', chatcall_text='' where (user_id = ".$_user['user_id'].")";
  110. $result = Database::query($sql);
  111. }
  112. echo '<div style="margin-left: 5px;">';
  113. foreach ($content as & $this_line) {
  114. echo strip_tags(api_html_entity_decode($this_line), '<br> <span> <b> <i> <img> <font>');
  115. }
  116. echo '</div>';
  117. ?>
  118. <a name="bottom" style="text-decoration:none;">&nbsp;</a>
  119. <?php
  120. if ($isMaster || $is_courseCoach) {
  121. $rand = mt_rand(1, 1000);
  122. echo '<div style="margin-left: 5px;">';
  123. 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>';
  124. echo '</div>';
  125. }
  126. } else {
  127. require 'header_frame.inc.php';
  128. $message = get_lang('CloseOtherSession');
  129. Display :: display_error_message($message);
  130. }
  131. require 'footer_frame.inc.php';