PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/chat/gui_header_js/jsupdate.php

http://github.com/moodle/moodle
PHP | 192 lines | 139 code | 27 blank | 26 comment | 30 complexity | 952fe69c55ead784bf2ad86c3afd8497 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. define('NO_MOODLE_COOKIES', true); // Session not used here.
  17. require('../../../config.php');
  18. require('../lib.php');
  19. $chatsid = required_param('chat_sid', PARAM_ALPHANUM);
  20. $chatlasttime = optional_param('chat_lasttime', 0, PARAM_INT);
  21. $chatlastrow = optional_param('chat_lastrow', 1, PARAM_INT);
  22. $url = new moodle_url('/mod/chat/gui_header_js/jsupdate.php', array('chat_sid' => $chatsid));
  23. if ($chatlasttime !== 0) {
  24. $url->param('chat_lasttime', $chatlasttime);
  25. }
  26. if ($chatlastrow !== 1) {
  27. $url->param('chat_lastrow', $chatlastrow);
  28. }
  29. $PAGE->set_url($url);
  30. if (!$chatuser = $DB->get_record('chat_users', array('sid' => $chatsid))) {
  31. print_error('notlogged', 'chat');
  32. }
  33. // Get the minimal course.
  34. if (!$course = $DB->get_record('course', array('id' => $chatuser->course))) {
  35. print_error('invalidcourseid');
  36. }
  37. // Get the user theme and enough info to be used in chat_format_message() which passes it along to.
  38. // No optimisation here, it would break again in future!
  39. if (!$user = $DB->get_record('user', array('id' => $chatuser->userid, 'deleted' => 0, 'suspended' => 0))) {
  40. print_error('invaliduser');
  41. }
  42. \core\session\manager::set_user($user);
  43. // Setup course, lang and theme.
  44. $PAGE->set_course($course);
  45. // Force deleting of timed out users if there is a silence in room or just entering.
  46. if ((time() - $chatlasttime) > $CFG->chat_old_ping) {
  47. // Must be done before chat_get_latest_message!
  48. chat_delete_old_users();
  49. }
  50. if ($message = chat_get_latest_message($chatuser->chatid, $chatuser->groupid)) {
  51. $chatnewlasttime = $message->timestamp;
  52. } else {
  53. $chatnewlasttime = 0;
  54. }
  55. if ($chatlasttime == 0) { // Display some previous messages.
  56. $chatlasttime = time() - $CFG->chat_old_ping; // TO DO - any better value?
  57. }
  58. $timenow = time();
  59. $params = array('groupid' => $chatuser->groupid, 'chatid' => $chatuser->chatid, 'lasttime' => $chatlasttime);
  60. $groupselect = $chatuser->groupid ? " AND (groupid=:groupid OR groupid=0) " : "";
  61. $messages = $DB->get_records_select("chat_messages_current",
  62. "chatid = :chatid AND timestamp > :lasttime $groupselect", $params,
  63. "timestamp ASC");
  64. if ($messages) {
  65. $num = count($messages);
  66. } else {
  67. $num = 0;
  68. }
  69. $chatnewrow = ($chatlastrow + $num) % 2;
  70. // No &amp; in url, does not work in header!
  71. $baseurl = "{$CFG->wwwroot}/mod/chat/gui_header_js/jsupdate.php?";
  72. $refreshurl = $baseurl . "chat_sid=$chatsid&chat_lasttime=$chatnewlasttime&chat_lastrow=$chatnewrow";
  73. $refreshurlamp = $baseurl . "chat_sid=$chatsid&amp;chat_lasttime=$chatnewlasttime&amp;chat_lastrow=$chatnewrow";
  74. header('Expires: Sun, 28 Dec 1997 09:32:45 GMT');
  75. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  76. header('Cache-Control: no-cache, must-revalidate');
  77. header('Pragma: no-cache');
  78. header('Content-Type: text/html; charset=utf-8');
  79. header("Refresh: $CFG->chat_refresh_room; url=$refreshurl");
  80. // Use ob to be able to send Content-Length headers.
  81. // Needed for Keep-Alive to work.
  82. ob_start();
  83. ?>
  84. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  85. <html>
  86. <head>
  87. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  88. <script type="text/javascript">
  89. //<![CDATA[
  90. if (parent.msg && parent.msg.document.getElementById("msgStarted") == null) {
  91. parent.msg.document.close();
  92. parent.msg.document.open("text/html","replace");
  93. parent.msg.document.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
  94. parent.msg.document.write("<html><head>");
  95. parent.msg.document.write("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />");
  96. parent.msg.document.write("<base target=\"_blank\" />");
  97. parent.msg.document.write("<\/head><body class=\"mod-chat-gui_header_js course-<?php echo $chatuser->course ?>\" id=\"mod-chat-gui_header_js-jsupdate\"><div style=\"display: none\" id=\"msgStarted\">&nbsp;<\/div>");
  98. }
  99. <?php
  100. $beep = false;
  101. $refreshusers = false;
  102. $us = array ();
  103. if (($chatlasttime != $chatnewlasttime) and $messages) {
  104. foreach ($messages as $message) {
  105. $chatlastrow = ($chatlastrow + 1) % 2;
  106. $formatmessage = chat_format_message($message, $chatuser->course, $USER, $chatlastrow);
  107. if ($formatmessage->beep) {
  108. $beep = true;
  109. }
  110. if ($formatmessage->refreshusers) {
  111. $refreshusers = true;
  112. }
  113. $us[$message->userid] = $timenow - $message->timestamp;
  114. echo "if(parent.msg)";
  115. echo "parent.msg.document.write('".addslashes_js($formatmessage->html)."\\n');\n";
  116. }
  117. }
  118. $chatuser->lastping = time();
  119. $DB->set_field('chat_users', 'lastping', $chatuser->lastping, array('id' => $chatuser->id));
  120. if ($refreshusers) {
  121. ?>
  122. var link = parent.users.document.getElementById('refreshLink');
  123. if (link != null) {
  124. parent.users.location.href = link.href;
  125. }
  126. <?php
  127. } else {
  128. foreach ($us as $uid => $lastping) {
  129. $min = (int) ($lastping / 60);
  130. $sec = $lastping - ($min * 60);
  131. $min = $min < 10 ? '0'.$min : $min;
  132. $sec = $sec < 10 ? '0'.$sec : $sec;
  133. $idle = $min.':'.$sec;
  134. echo "if (parent.users && parent.users.document.getElementById('uidle{$uid}') != null) {".
  135. "parent.users.document.getElementById('uidle{$uid}').innerHTML = '$idle';}\n";
  136. }
  137. }
  138. ?>
  139. if (parent.input) {
  140. var autoscroll = parent.input.document.getElementById('auto');
  141. if (parent.msg && autoscroll && autoscroll.checked) {
  142. parent.msg.scroll(1,5000000);
  143. }
  144. }
  145. //]]>
  146. </script>
  147. </head>
  148. <body>
  149. <?php
  150. if ($beep) {
  151. echo '<script> (function() {';
  152. echo 'var audioElement = document.createElement("audio");';
  153. echo 'audioElement.setAttribute("src", "../beep.mp3");';
  154. echo 'audioElement.play(); })();';
  155. echo '</script>';
  156. }
  157. ?>
  158. <a href="<?php echo $refreshurlamp ?>" name="refreshLink">Refresh link</a>
  159. </body>
  160. </html>
  161. <?php
  162. // Support HTTP Keep-Alive.
  163. header("Content-Length: " . ob_get_length() );
  164. ob_end_flush();
  165. exit;