See Release Notes
Long Term Support Release
Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]
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 17 /** jsupdated.php - notes by Martin Langhoff <martin@catalyst.net.nz> 18 ** 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" 22 ** updates. 23 ** 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. 26 ** 27 ** This buys us all the benefits that chatd has, minus the setup, 28 ** as we are using apache to do the daemon handling. 29 ** 30 **/ 31 32 define('CHAT_MAX_CLIENT_UPDATES', 1000); 33 define('NO_MOODLE_COOKIES', true); // Session not used here. 34 define('NO_OUTPUT_BUFFERING', true); 35 36 require('../../../config.php'); 37 require_once ('../lib.php'); 38 39 // We are going to run for a long time. 40 // Avoid being terminated by php. 41 core_php_time_limit::raise(); 42 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); 47 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); 51 } 52 if ($chatlastrow !== 1) { 53 $url->param('chat_lastrow', $chatlastrow); 54 } 55 if ($chatlastid !== 1) { 56 $url->param('chat_lastid', $chatlastid); 57 } 58 $PAGE->set_url($url); 59 60 if (!$chatuser = $DB->get_record('chat_users', array('sid' => $chatsid))) { 61 throw new \moodle_exception('notlogged', 'chat'); 62 } 63 64 // Get the minimal course. 65 if (!$course = $DB->get_record('course', array('id' => $chatuser->course))) { 66 throw new \moodle_exception('invalidcourseid'); 67 } 68 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 throw new \moodle_exception('invaliduser'); 74 } 75 \core\session\manager::set_user($user); 76 77 // Setup course, lang and theme. 78 $PAGE->set_course($course); 79 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(); 84 } 85 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'); 92 93 $refreshurl = "{$CFG->wwwroot}/mod/chat/gui_header_js/jsupdated.php?". 94 "chat_sid=$chatsid&chat_lasttime=$chatlasttime&chat_lastrow=$chatnewrow&chat_lastid=$chatlastid"; 95 ?> 96 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 97 <html> 98 <head> 99 <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 100 <script type="text/javascript"> 101 //<![CDATA[ 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>"); 110 } 111 //]]> 112 </script> 113 </head> 114 <body> 115 116 <?php 117 118 // Ensure the HTML head makes it out there. 119 echo $CHAT_DUMMY_DATA; 120 121 for ($n = 0; $n <= CHAT_MAX_CLIENT_UPDATES; $n++) { 122 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)); 126 127 if ($message = chat_get_latest_message($chatuser->chatid, $chatuser->groupid)) { 128 $chatnewlasttime = $message->timestamp; 129 $chatnewlastid = $message->id; 130 } else { 131 $chatnewlasttime = 0; 132 $chatnewlastid = 0; 133 print " \n"; 134 print $CHAT_DUMMY_DATA; 135 sleep($CFG->chat_refresh_room); 136 continue; 137 } 138 139 $timenow = time(); 140 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) " : ""; 146 147 $newcriteria = ''; 148 if ($chatlastid > 0) { 149 $newcriteria = "id > :lastid"; 150 } else { 151 if ($chatlasttime == 0) { // Display some previous messages. 152 $chatlasttime = $timenow - $CFG->chat_old_ping; // TO DO - any better value? 153 } 154 $newcriteria = "timestamp > :lasttime"; 155 } 156 157 $messages = $DB->get_records_select("chat_messages_current", 158 "chatid = :chatid AND $newcriteria $groupselect", $params, 159 "timestamp ASC"); 160 161 if ($messages) { 162 $num = count($messages); 163 } else { 164 print " \n"; 165 print $CHAT_DUMMY_DATA; 166 sleep($CFG->chat_refresh_room); 167 continue; 168 } 169 170 print '<script type="text/javascript">' . "\n"; 171 print "//<![CDATA[\n\n"; 172 173 $chatnewrow = ($chatlastrow + $num) % 2; 174 175 $refreshusers = false; 176 $us = array (); 177 if (($chatlasttime != $chatnewlasttime) and $messages) { 178 179 $beep = false; 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) { 185 $beep = true; 186 } 187 if ($formatmessage->refreshusers) { 188 $refreshusers = true; 189 } 190 $us[$message->userid] = $timenow - $message->timestamp; 191 echo "parent.msg.document.write('".addslashes_js($formatmessage->html )."\\n');\n"; 192 193 } 194 // From the last message printed. 195 // A strange case where lack of closures is useful! 196 $chatlasttime = $message->timestamp; 197 $chatlastid = $message->id; 198 } 199 200 if ($refreshusers) { 201 echo "if (parent.users.document.anchors[0] != null) {" . 202 "parent.users.location.href = parent.users.document.anchors[0].href;}\n"; 203 } else { 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"; 212 } 213 } 214 215 print <<<EOD 216 if(parent.input){ 217 var autoscroll = parent.input.document.getElementById('auto'); 218 if(parent.msg && autoscroll && autoscroll.checked){ 219 parent.msg.scroll(1,5000000); 220 } 221 } 222 EOD; 223 print "//]]>\n"; 224 print '</script>' . "\n\n"; 225 if ($beep) { 226 print '<script> (function() {'; 227 print 'var audioElement = document.createElement("audio");'; 228 print 'audioElement.setAttribute("src", "../beep.mp3");'; 229 print 'audioElement.play(); })();'; 230 print '</script>'; 231 } 232 print $CHAT_DUMMY_DATA; 233 sleep($CFG->chat_refresh_room); 234 } // Here ends the for() loop. 235 236 // Here & should be written & :-D. 237 $refreshurl = "{$CFG->wwwroot}/mod/chat/gui_header_js/jsupdated.php?"; 238 $refreshurl .= "chat_sid=$chatsid&chat_lasttime=$chatlasttime&chat_lastrow=$chatnewrow&chat_lastid=$chatlastid"; 239 240 print '<script type="text/javascript">' . "\n"; 241 print "//<![CDATA[ \n\n"; 242 print "location.href = '$refreshurl';\n"; 243 print "//]]>\n"; 244 print '</script>' . "\n\n"; 245 ?> 246 247 </body> 248 </html>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body