2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 /** jsupdated.php - notes by Martin Langhoff <martin@catalyst.net.nz>
19 ** This is an alternative version of jsupdate.php that acts
20 ** as a long-running daemon. It will feed/stall/feed JS updates
21 ** to the client. From the module configuration select "Stream"
24 ** The client connection is not forever though. Once we reach
25 ** CHAT_MAX_CLIENT_UPDATES, it will force the client to re-fetch it.
27 ** This buys us all the benefits that chatd has, minus the setup,
28 ** as we are using apache to do the daemon handling.
32 define('CHAT_MAX_CLIENT_UPDATES', 1000);
33 define('NO_MOODLE_COOKIES', true); // Session not used here.
34 define('NO_OUTPUT_BUFFERING', true);
36 require('../../../config.php');
37 require('../lib.php');
39 // We are going to run for a long time.
40 // Avoid being terminated by php.
41 core_php_time_limit::raise();
43 $chatsid = required_param('chat_sid', PARAM_ALPHANUM);
44 $chatlasttime = optional_param('chat_lasttime', 0, PARAM_INT);
45 $chatlastrow = optional_param('chat_lastrow', 1, PARAM_INT);
46 $chatlastid = optional_param('chat_lastid', 0, PARAM_INT);
48 $url = new moodle_url('/mod/chat/gui_header_js/jsupdated.php', array('chat_sid' => $chatsid));
49 if ($chatlasttime !== 0) {
50 $url->param('chat_lasttime', $chatlasttime);
52 if ($chatlastrow !== 1) {
53 $url->param('chat_lastrow', $chatlastrow);
55 if ($chatlastid !== 1) {
56 $url->param('chat_lastid', $chatlastid);
60 if (!$chatuser = $DB->get_record('chat_users', array('sid' => $chatsid))) {
61 print_error('notlogged', 'chat');
64 // Get the minimal course.
65 if (!$course = $DB->get_record('course', array('id' => $chatuser->course))) {
66 print_error('invalidcourseid');
69 // Get the user theme and enough info to be used in chat_format_message() which passes it along to
70 // chat_format_message_manually() -- and only id and timezone are used.
71 // No optimisation here, it would break again in future!
72 if (!$user = $DB->get_record('user', array('id' => $chatuser->userid, 'deleted' => 0, 'suspended' => 0))) {
73 print_error('invaliduser');
75 \core\session\manager::set_user($user);
77 // Setup course, lang and theme.
78 $PAGE->set_course($course);
80 // Force deleting of timed out users if there is a silence in room or just entering.
81 if ((time() - $chatlasttime) > $CFG->chat_old_ping) {
82 // Must be done before chat_get_latest_message!
83 chat_delete_old_users();
86 // Time to send headers, and lay out the basic JS updater page.
87 header('Expires: Sun, 28 Dec 1997 09:32:45 GMT');
88 header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
89 header('Cache-Control: no-cache, must-revalidate');
90 header('Pragma: no-cache');
91 header('Content-Type: text/html; charset=utf-8');
93 $refreshurl = "{$CFG->wwwroot}/mod/chat/gui_header_js/jsupdated.php?".
94 "chat_sid=$chatsid&chat_lasttime=$chatlasttime&chat_lastrow=$chatnewrow&chat_lastid=$chatlastid";
96 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
99 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
100 <script type="text/javascript">
102 if (parent.msg.document.getElementById("msgStarted") == null) {
103 parent.msg.document.close();
104 parent.msg.document.open("text/html","replace");
105 parent.msg.document.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
106 parent.msg.document.write("<html><head>");
107 parent.msg.document.write("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />");
108 parent.msg.document.write("<base target=\"_blank\" />");
109 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\"> </div>");
118 // Ensure the HTML head makes it out there.
119 echo $CHAT_DUMMY_DATA;
121 for ($n = 0; $n <= CHAT_MAX_CLIENT_UPDATES; $n++) {
123 // Ping first so we can later shortcut as needed.
124 $chatuser->lastping = time();
125 $DB->set_field('chat_users', 'lastping', $chatuser->lastping, array('id' => $chatuser->id));
127 if ($message = chat_get_latest_message($chatuser->chatid, $chatuser->groupid)) {
128 $chatnewlasttime = $message->timestamp;
129 $chatnewlastid = $message->id;
131 $chatnewlasttime = 0;
134 print $CHAT_DUMMY_DATA;
135 sleep($CFG->chat_refresh_room);
141 $params = array('groupid' => $chatuser->groupid,
142 'lastid' => $chatlastid,
143 'lasttime' => $chatlasttime,
144 'chatid' => $chatuser->chatid);
145 $groupselect = $chatuser->groupid ? " AND (groupid=:groupid OR groupid=0) " : "";
148 if ($chatlastid > 0) {
149 $newcriteria = "id > :lastid";
151 if ($chatlasttime == 0) { // Display some previous messages.
152 $chatlasttime = $timenow - $CFG->chat_old_ping; // TO DO - any better value?
154 $newcriteria = "timestamp > :lasttime";
157 $messages = $DB->get_records_select("chat_messages_current",
158 "chatid = :chatid AND $newcriteria $groupselect", $params,
162 $num = count($messages);
165 print $CHAT_DUMMY_DATA;
166 sleep($CFG->chat_refresh_room);
170 print '<script type="text/javascript">' . "\n";
171 print "//<![CDATA[\n\n";
173 $chatnewrow = ($chatlastrow + $num) % 2;
175 $refreshusers = false;
177 if (($chatlasttime != $chatnewlasttime) and $messages) {
180 $refreshusers = false;
181 foreach ($messages as $message) {
182 $chatlastrow = ($chatlastrow + 1) % 2;
183 $formatmessage = chat_format_message($message, $chatuser->course, $USER, $chatlastrow);
184 if ($formatmessage->beep) {
187 if ($formatmessage->refreshusers) {
188 $refreshusers = true;
190 $us[$message->userid] = $timenow - $message->timestamp;
191 echo "parent.msg.document.write('".addslashes_js($formatmessage->html )."\\n');\n";
194 // From the last message printed.
195 // A strange case where lack of closures is useful!
196 $chatlasttime = $message->timestamp;
197 $chatlastid = $message->id;
201 echo "if (parent.users.document.anchors[0] != null) {" .
202 "parent.users.location.href = parent.users.document.anchors[0].href;}\n";
204 foreach ($us as $uid => $lastping) {
205 $min = (int) ($lastping / 60);
206 $sec = $lastping - ($min * 60);
207 $min = $min < 10 ? '0'.$min : $min;
208 $sec = $sec < 10 ? '0'.$sec : $sec;
209 $idle = $min.':'.$sec;
210 echo "if (parent.users.document.getElementById('uidle{$uid}') != null) {".
211 "parent.users.document.getElementById('uidle{$uid}').innerHTML = '$idle';}\n";
217 var autoscroll = parent.input.document.getElementById('auto');
218 if(parent.msg && autoscroll && autoscroll.checked){
219 parent.msg.scroll(1,5000000);
224 print '</script>' . "\n\n";
226 print '<embed src="../beep.wav" autostart="true" hidden="true" name="beep" />';
228 print $CHAT_DUMMY_DATA;
229 sleep($CFG->chat_refresh_room);
230 } // Here ends the for() loop.
232 // Here & should be written & :-D.
233 $refreshurl = "{$CFG->wwwroot}/mod/chat/gui_header_js/jsupdated.php?";
234 $refreshurl .= "chat_sid=$chatsid&chat_lasttime=$chatlasttime&chat_lastrow=$chatnewrow&chat_lastid=$chatlastid";
236 print '<script type="text/javascript">' . "\n";
237 print "//<![CDATA[ \n\n";
238 print "location.href = '$refreshurl';\n";
240 print '</script>' . "\n\n";