chat_message.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Allows to type the messages that will be displayed on chat_chat.php
  5. *
  6. * @author Olivier Brouckaert
  7. * Modified by Alex Aragón (BeezNest)
  8. * @package chamilo.chat
  9. */
  10. /**
  11. * Code
  12. */
  13. /* INIT SECTION */
  14. define('FRAME', 'message');
  15. $language_file = array('chat');
  16. //require_once '../inc/global.inc.php';
  17. $course = api_get_course_id();
  18. $session_id = api_get_session_id();
  19. $group_id = api_get_group_id();
  20. // Juan Carlos Raña inserted smileys and self-closing window.
  21. ?>
  22. <script >
  23. function insert_smile(text) {
  24. if (text.createTextRange) {
  25. text.smile = document.selection.createRange().duplicate();
  26. }
  27. }
  28. function insert(text) {
  29. var chat = document.formMessage.message;
  30. if (chat.createTextRange && chat.smile) {
  31. var smile = chat.smile;
  32. smile.text = smile.text.charAt(smile.text.length - 1) == ' ' ? text + ' ' : text;
  33. }
  34. else chat.value += text;
  35. chat.focus(smile)
  36. }
  37. function close_chat_window() {
  38. var chat_window = top.window.self;
  39. chat_window.opener = top.window.self;
  40. chat_window.top.close();
  41. }
  42. </script>
  43. <?php
  44. // Mode open in a new window: close the window when there isn't an user login
  45. if (empty($_user['user_id'])) {
  46. echo '<script languaje="javascript" type="text/javascript"> close_chat_window(); </script>';
  47. } else {
  48. api_protect_course_script();
  49. }
  50. // if we have the session set up
  51. if (!empty($course) && !empty($_user['user_id'])) {
  52. require_once api_get_path(LIBRARY_PATH).'document.lib.php';
  53. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  54. /* Constants and variables */
  55. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  56. $sent = isset($_REQUEST['sent']) ? $_REQUEST['sent'] : null;
  57. /* MAIN CODE */
  58. $query = "SELECT user_id, lastname, firstname, username, picture_uri FROM $tbl_user WHERE user_id='".intval($_user['user_id'])."'";
  59. $result = Database::query($query);
  60. list($pseudo_user) = Database::fetch_row($result);
  61. $isAllowed = !(empty($pseudo_user) || !$_cid);
  62. $isMaster = (bool)api_is_course_admin();
  63. $user_id = Database::result($result, 0, 'user_id');
  64. $firstname = Database::result($result, 0, 'firstname');
  65. $lastname = Database::result($result, 0, 'lastname');
  66. $picture = Database::result($result, 0, 'picture_uri');
  67. $date_now = date('Y-m-d');
  68. $basepath_chat = '';
  69. $document_path = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  70. if (!empty($group_id)) {
  71. $group_info = GroupManager :: get_group_properties($group_id);
  72. $basepath_chat = $group_info['directory'].'/chat_files';
  73. } else {
  74. $basepath_chat = '/chat_files';
  75. }
  76. $chat_path = $document_path.$basepath_chat.'/';
  77. if (!is_dir($chat_path)) {
  78. if (is_file($chat_path)) {
  79. @unlink($chat_path);
  80. }
  81. }
  82. require 'header_frame.inc.php';
  83. $chat_size = 0;
  84. // Define emoticons
  85. $emoticon_text1 = ':-)';
  86. $emoticon_img1 = '<img src="'.api_get_path(WEB_IMG_PATH).'emoticons/icon_smile.png" alt="'.get_lang('Smile').'" title="'.get_lang('Smile').'" />';
  87. $emoticon_text2 = ':-D';
  88. $emoticon_img2 = '<img src="'.api_get_path(WEB_IMG_PATH).'emoticons/icon_biggrin.png" alt="'.get_lang('BigGrin').'" title="'.get_lang('BigGrin').'" />';
  89. $emoticon_text3 = ';-)';
  90. $emoticon_img3 = '<img src="'.api_get_path(WEB_IMG_PATH).'emoticons/icon_wink.png" alt="'.get_lang('Wink').'" title="'.get_lang('Wink').'" />';
  91. $emoticon_text4 = ':-P';
  92. $emoticon_img4 = '<img src="'.api_get_path(WEB_IMG_PATH).'emoticons/icon_razz.png" alt="'.get_lang('Avid').'" title="'.get_lang('Avid').'" />';
  93. $emoticon_text5 = '8-)';
  94. $emoticon_img5 = '<img src="'.api_get_path(WEB_IMG_PATH).'emoticons/icon_cool.png" alt="'.get_lang('Cool').'" title="'.get_lang('Cool').'" />';
  95. $emoticon_text6 = ':-o)';
  96. $emoticon_img6 = '<img src="'.api_get_path(WEB_IMG_PATH).'emoticons/icon_surprised.png" alt="'.get_lang('Surprised').'" title="'.get_lang('Surprised').'" />';
  97. $emoticon_text7 = '=;';
  98. $emoticon_img7 = '<img src="'.api_get_path(WEB_IMG_PATH).'emoticons/icon_hand.png" alt="'.get_lang('Hand').'" title="'.get_lang('Hand').'" />';
  99. $emoticon_text8 = '=8-o';
  100. $emoticon_img8 = '<img src="'.api_get_path(WEB_IMG_PATH).'emoticons/icon_eek.png" alt="'.get_lang('Amazing').'" title="'.get_lang('Amazing').'" />';
  101. $emoticon_text9 = ':-|)';
  102. $emoticon_img9 = '<img src="'.api_get_path(WEB_IMG_PATH).'emoticons/icon_neutral.png" alt="'.get_lang('Neutral').'" title="'.get_lang('Neutral').'" />';
  103. $emoticon_text10 = ':-k';
  104. $emoticon_img10 = '<img src="'.api_get_path(WEB_IMG_PATH).'emoticons/icon_think.png" alt="'.get_lang('Think').'" title="'.get_lang('Think').'" />';
  105. $emoticon_text11 = ':-?';
  106. $emoticon_img11 = '<img src="'.api_get_path(WEB_IMG_PATH).'emoticons/icon_confused.png" alt="'.get_lang('Confused').'" title="'.get_lang('Confused').'" />';
  107. $emoticon_text12 = ':-8';
  108. $emoticon_img12 = '<img src="'.api_get_path(WEB_IMG_PATH).'emoticons/icon_redface.png" alt="'.get_lang('Redface').'" title="'.get_lang('Redface').'" />';
  109. $emoticon_text13 = ':-L';
  110. $emoticon_img13 = '<img src="'.api_get_path(WEB_IMG_PATH).'emoticons/icon_shhh.png" alt="'.get_lang('Silence').'" title="'.get_lang('Silence').'" />';
  111. $emoticon_text14 = ':-#)';
  112. $emoticon_img14 = '<img src="'.api_get_path(WEB_IMG_PATH).'emoticons/icon_silenced.png" alt="'.get_lang('Silenced').'" title="'.get_lang('Silenced').'" />';
  113. $emoticon_text15 = ':-(';
  114. $emoticon_img15 = '<img src="'.api_get_path(WEB_IMG_PATH).'emoticons/icon_sad.png" alt="'.get_lang('Sad').'" title="'.get_lang('Sad').'" />';
  115. $emoticon_text16 = ';-(';
  116. $emoticon_img16 = '<img src="'.api_get_path(WEB_IMG_PATH).'emoticons/icon_mourn.png" alt="'.get_lang('Weep').'" title="'.get_lang('Weep').'" />';
  117. $emoticon_text17 = ':-[8';
  118. $emoticon_img17 = '<img src="'.api_get_path(WEB_IMG_PATH).'emoticons/icon_angry.png" alt="'.get_lang('Angry').'" title="'.get_lang('Angry').'" />';
  119. $emoticon_text18 = '--)';
  120. $emoticon_img18 = '<img src="'.api_get_path(WEB_IMG_PATH).'emoticons/icon_arrow.png" alt="'.get_lang('Arrow').'" title="'.get_lang('Arrow').'" />';
  121. $emoticon_text19 = ':!:';
  122. $emoticon_img19 = '<img src="'.api_get_path(WEB_IMG_PATH).'emoticons/icon_exclaim.png" alt="'.get_lang('Exclamation').'" title="'.get_lang('Exclamation').'" />';
  123. $emoticon_text20 = ':?:';
  124. $emoticon_img20 = '<img src="'.api_get_path(WEB_IMG_PATH).'emoticons/icon_question.png" alt="'.get_lang('Question').'" title="'.get_lang('Question').'" />';
  125. $emoticon_text21 = '0-';
  126. $emoticon_img21 = '<img src="'.api_get_path(WEB_IMG_PATH).'emoticons/icon_idea.png" alt="'.get_lang('Idea').'" title="'.get_lang('Idea').'" />';
  127. $emoticon_text201 = '*';
  128. $emoticon_img201 = '<img src="'.api_get_path(WEB_IMG_PATH).'emoticons/waiting.png" alt="'.get_lang('AskPermissionSpeak').'" title="'.get_lang('AskPermissionSpeak').'" />';
  129. $emoticon_text202 = ':speak:';
  130. $emoticon_img202 = '<img src="'.api_get_path(WEB_IMG_PATH).'emoticons/flag_green_small.png" alt="'.get_lang('GiveTheFloorTo').'" title="'.get_lang('GiveTheFloorTo').'" />';
  131. $emoticon_text203 = ':pause:';
  132. $emoticon_img203 = '<img src="'.api_get_path(WEB_IMG_PATH).'emoticons/flag_yellow_small.png" alt="'.get_lang('Pause').'" title="'.get_lang('Pause').'" />';
  133. $emoticon_text204 = ':stop:';
  134. $emoticon_img204 = '<img src="'.api_get_path(WEB_IMG_PATH).'emoticons/flag_red_small.png" alt="'.get_lang('Stop').'" title="'.get_lang('Stop').'" />';
  135. if ($sent) {
  136. $message = Security::remove_XSS(trim(htmlspecialchars(stripslashes($_POST['message']), ENT_QUOTES, $charset)));
  137. $message = str_replace($emoticon_text1, $emoticon_img1, $message);
  138. $message = str_replace($emoticon_text2, $emoticon_img2, $message);
  139. $message = str_replace($emoticon_text3, $emoticon_img3, $message);
  140. $message = str_replace($emoticon_text4, $emoticon_img4, $message);
  141. $message = str_replace($emoticon_text5, $emoticon_img5, $message);
  142. $message = str_replace($emoticon_text6, $emoticon_img6, $message);
  143. $message = str_replace($emoticon_text7, $emoticon_img7, $message);
  144. $message = str_replace($emoticon_text8, $emoticon_img8, $message);
  145. $message = str_replace($emoticon_text9, $emoticon_img9, $message);
  146. $message = str_replace($emoticon_text10, $emoticon_img10, $message);
  147. $message = str_replace($emoticon_text11, $emoticon_img11, $message);
  148. $message = str_replace($emoticon_text12, $emoticon_img12, $message);
  149. $message = str_replace($emoticon_text13, $emoticon_img13, $message);
  150. $message = str_replace($emoticon_text14, $emoticon_img14, $message);
  151. $message = str_replace($emoticon_text15, $emoticon_img15, $message);
  152. $message = str_replace($emoticon_text16, $emoticon_img16, $message);
  153. $message = str_replace($emoticon_text17, $emoticon_img17, $message);
  154. $message = str_replace($emoticon_text18, $emoticon_img18, $message);
  155. $message = str_replace($emoticon_text19, $emoticon_img19, $message);
  156. $message = str_replace($emoticon_text20, $emoticon_img20, $message);
  157. $message = str_replace($emoticon_text21, $emoticon_img21, $message);
  158. //
  159. $message = str_replace($emoticon_text201, $emoticon_img201, $message);
  160. $message = str_replace($emoticon_text202, $emoticon_img202, $message);
  161. $message = str_replace($emoticon_text203, $emoticon_img203, $message);
  162. $message = str_replace($emoticon_text204, $emoticon_img204, $message);
  163. $timeNow = date('d/m/y H:i:s');
  164. $basename_chat = '';
  165. if (!empty($group_id)) {
  166. $basename_chat = 'messages-'.$date_now.'_gid-'.$group_id;
  167. } elseif (!empty($session_id)) {
  168. $basename_chat = 'messages-'.$date_now.'_sid-'.$session_id;
  169. } else {
  170. $basename_chat = 'messages-'.$date_now;
  171. }
  172. if (!api_is_anonymous()) {
  173. if (!empty($message)) {
  174. //$message = make_clickable($message);
  175. if (!file_exists($chat_path.$basename_chat.'.log.html')) {
  176. $doc_id = add_document($_course, $basepath_chat.'/'.$basename_chat.'.log.html', 'file', 0, $basename_chat.'.log.html');
  177. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'DocumentAdded', $_user['user_id'], $group_id, null, null, null, $session_id);
  178. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'invisible', $_user['user_id'], $group_id, null, null, null, $session_id);
  179. item_property_update_on_folder($_course, $basepath_chat, $_user['user_id']);
  180. } else {
  181. $doc_id = DocumentManager::get_document_id($_course, $basepath_chat.'/'.$basename_chat.'.log.html');
  182. }
  183. $fp = fopen($chat_path.$basename_chat.'.log.html', 'a');
  184. // view user picture
  185. $userImage = UserManager::get_user_picture_path_by_id($user_id, 'web', false, true);
  186. if (substr($userImage['file'],0,7) != 'unknown') {
  187. $userPhoto = $userImage['dir'].'medium_'.$userImage['file'];
  188. } else {
  189. $userPhoto = $userImage['dir'].$userImage['file'];
  190. }
  191. $filePhoto = '<img class="chat-image" src="'.$userPhoto.'"/>';
  192. if ($isMaster) {
  193. fputs($fp, '<div class="message-teacher"><div class="content-message"><div>'.$message.'</div><div class="message-date">'.$timeNow.'</div></div><div class="icon-message"></div>'.$filePhoto.'</div>'."\n");
  194. } else {
  195. fputs($fp, '<div class="message-student">'.$filePhoto.'<div class="icon-message"></div><div class="content-message"><div>'.$message.'</div><div class="message-date">'.$timeNow.'</div></div></div>'."\n");
  196. }
  197. fclose($fp);
  198. $chat_size = filesize($chat_path.$basename_chat.'.log.html');
  199. update_existing_document($_course, $doc_id, $chat_size);
  200. item_property_update_on_folder($_course, $basepath_chat, $_user['user_id']);
  201. }
  202. }
  203. }
  204. ?>
  205. <form name="formMessage" method="post" action="<?php echo api_get_self().'?'.api_get_cidreq(); ?>" onsubmit="javascript: if(document.formMessage.message.value == '') { alert('<?php echo addslashes(api_htmlentities(get_lang('TypeMessage'), ENT_QUOTES)); ?>'); document.formMessage.message.focus(); return false; }" autocomplete="off">
  206. <input type="hidden" name="sent" value="1">
  207. <div class="message-form-chat">
  208. <table border="0" cellpadding="5" cellspacing="0" width="100%">
  209. <tr>
  210. <td width="320" valign="middle">
  211. <?php $talkboxsize=(api_get_course_setting('allow_open_chat_window')) ? 'width: 350px; height: 80px' : 'width: 450px; height: 35px'; ?>
  212. <textarea id="message" class="message-text" name="message" style=" <?php echo $talkboxsize; ?>" onkeydown="send_message(event);" onclick="javascript: insert_smile(this);"></textarea>
  213. </td>
  214. <td>
  215. <button id="send" type="submit" value="<?php echo get_lang('Send'); ?>" class="btn-send"><?php echo get_lang('Send'); ?></button>
  216. </td>
  217. </tr>
  218. </table>
  219. <?php
  220. echo '<div class="emoticons-chat">';
  221. echo "<a href=\"javascript:insert('".$emoticon_text1."')\">".$emoticon_img1."</a>";
  222. echo "<a href=\"javascript:insert('".$emoticon_text2."')\">".$emoticon_img2."</a>";
  223. echo "<a href=\"javascript:insert('".$emoticon_text3."')\">".$emoticon_img3."</a>";
  224. echo "<a href=\"javascript:insert('".$emoticon_text4."')\">".$emoticon_img4."</a>";
  225. echo "<a href=\"javascript:insert('".$emoticon_text5."')\">".$emoticon_img5."</a>";
  226. echo "<a href=\"javascript:insert('".$emoticon_text6."')\">".$emoticon_img6."</a>";
  227. echo "<a href=\"javascript:insert('".$emoticon_text7."')\">".$emoticon_img7."</a>";
  228. echo "<a href=\"javascript:insert('".$emoticon_text8."')\">".$emoticon_img8."</a>";
  229. echo "<a href=\"javascript:insert('".$emoticon_text9."')\">".$emoticon_img9."</a>";
  230. echo "<a href=\"javascript:insert('".$emoticon_text10."')\">".$emoticon_img10."</a>";
  231. echo "<a href=\"javascript:insert('".$emoticon_text11."')\">".$emoticon_img11."</a>";
  232. echo "<a href=\"javascript:insert('".$emoticon_text12."')\">".$emoticon_img12."</a>";
  233. echo "<a href=\"javascript:insert('".$emoticon_text13."')\">".$emoticon_img13."</a>";
  234. echo "<a href=\"javascript:insert('".$emoticon_text14."')\">".$emoticon_img14."</a>";
  235. echo "<a href=\"javascript:insert('".$emoticon_text15."')\">".$emoticon_img15."</a>";
  236. echo "<a href=\"javascript:insert('".$emoticon_text16."')\">".$emoticon_img16."</a>";
  237. echo "<a href=\"javascript:insert('".$emoticon_text17."')\">".$emoticon_img17."</a>";
  238. echo "<a href=\"javascript:insert('".$emoticon_text18."')\">".$emoticon_img18."</a>";
  239. echo "<a href=\"javascript:insert('".$emoticon_text19."')\">".$emoticon_img19."</a>";
  240. echo "<a href=\"javascript:insert('".$emoticon_text20."')\">".$emoticon_img20."</a>";
  241. echo "<a href=\"javascript:insert('".$emoticon_text21."')\">".$emoticon_img21."</a>";
  242. ?>
  243. <?php
  244. echo "<a href=\"javascript:insert('".$emoticon_text201."')\">".$emoticon_img201."</a>";
  245. echo "<a href=\"javascript:insert('".$emoticon_text202."')\">".$emoticon_img202."</a>";
  246. echo "<a href=\"javascript:insert('".$emoticon_text203."')\">".$emoticon_img203."</a>";
  247. echo "<a href=\"javascript:insert('".$emoticon_text204."')\">".$emoticon_img204."</a>";
  248. echo '</div>';
  249. ?>
  250. </div>
  251. </form>
  252. <?php
  253. }
  254. require 'footer_frame.inc.php';