[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
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 <[email protected]> 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 ('../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 print_error('notlogged', 'chat'); 62 } 63 64 // Get the minimal course. 65 if (!$course = $DB->get_record('course', array('id' => $chatuser->course))) { 66 print_error('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 print_error('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 // Required stylesheets. 94 $stylesheetshtml = ''; 95 /*foreach ($CFG->stylesheets as $stylesheet) { 96 //TODO: MDL-21120 97 $stylesheetshtml .= '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" />'; 98 }*/ 99 100 $refreshurl = "{$CFG->wwwroot}/mod/chat/gui_header_js/jsupdated.php?". 101 "chat_sid=$chatsid&chat_lasttime=$chatlasttime&chat_lastrow=$chatnewrow&chat_lastid=$chatlastid"; 102 ?> 103 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 104 <html> 105 <head> 106 <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 107 <script type="text/javascript"> 108 //<![CDATA[ 109 function safari_refresh() { 110 self.location.href= '<?php echo $refreshurl;?>'; 111 } 112 var issafari = false; 113 if(window.devicePixelRatio){ 114 issafari = true; 115 setTimeout('safari_refresh()', <?php echo $CFG->chat_refresh_room * 1000;?>); 116 } 117 if (parent.msg.document.getElementById("msgStarted") == null) { 118 parent.msg.document.close(); 119 parent.msg.document.open("text/html","replace"); 120 parent.msg.document.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"); 121 parent.msg.document.write("<html><head>"); 122 parent.msg.document.write("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />"); 123 parent.msg.document.write("<base target=\"_blank\" />"); 124 parent.msg.document.write("<?php echo addslashes_js($stylesheetshtml) ?>"); 125 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>"); 126 } 127 //]]> 128 </script> 129 </head> 130 <body> 131 132 <?php 133 134 // Ensure the HTML head makes it out there. 135 echo $CHAT_DUMMY_DATA; 136 137 for ($n = 0; $n <= CHAT_MAX_CLIENT_UPDATES; $n++) { 138 139 // Ping first so we can later shortcut as needed. 140 $chatuser->lastping = time(); 141 $DB->set_field('chat_users', 'lastping', $chatuser->lastping, array('id' => $chatuser->id)); 142 143 if ($message = chat_get_latest_message($chatuser->chatid, $chatuser->groupid)) { 144 $chatnewlasttime = $message->timestamp; 145 $chatnewlastid = $message->id; 146 } else { 147 $chatnewlasttime = 0; 148 $chatnewlastid = 0; 149 print " \n"; 150 print $CHAT_DUMMY_DATA; 151 sleep($CFG->chat_refresh_room); 152 continue; 153 } 154 155 $timenow = time(); 156 157 $params = array('groupid' => $chatuser->groupid, 158 'lastid' => $chatlastid, 159 'lasttime' => $chatlasttime, 160 'chatid' => $chatuser->chatid); 161 $groupselect = $chatuser->groupid ? " AND (groupid=:groupid OR groupid=0) " : ""; 162 163 $newcriteria = ''; 164 if ($chatlastid > 0) { 165 $newcriteria = "id > :lastid"; 166 } else { 167 if ($chatlasttime == 0) { // Display some previous messages. 168 $chatlasttime = $timenow - $CFG->chat_old_ping; // TO DO - any better value? 169 } 170 $newcriteria = "timestamp > :lasttime"; 171 } 172 173 $messages = $DB->get_records_select("chat_messages_current", 174 "chatid = :chatid AND $newcriteria $groupselect", $params, 175 "timestamp ASC"); 176 177 if ($messages) { 178 $num = count($messages); 179 } else { 180 print " \n"; 181 print $CHAT_DUMMY_DATA; 182 sleep($CFG->chat_refresh_room); 183 continue; 184 } 185 186 print '<script type="text/javascript">' . "\n"; 187 print "//<![CDATA[\n\n"; 188 189 $chatnewrow = ($chatlastrow + $num) % 2; 190 191 $refreshusers = false; 192 $us = array (); 193 if (($chatlasttime != $chatnewlasttime) and $messages) { 194 195 $beep = false; 196 $refreshusers = false; 197 foreach ($messages as $message) { 198 $chatlastrow = ($chatlastrow + 1) % 2; 199 $formatmessage = chat_format_message($message, $chatuser->course, $USER, $chatlastrow); 200 if ($formatmessage->beep) { 201 $beep = true; 202 } 203 if ($formatmessage->refreshusers) { 204 $refreshusers = true; 205 } 206 $us[$message->userid] = $timenow - $message->timestamp; 207 echo "parent.msg.document.write('".addslashes_js($formatmessage->html )."\\n');\n"; 208 209 } 210 // From the last message printed. 211 // A strange case where lack of closures is useful! 212 $chatlasttime = $message->timestamp; 213 $chatlastid = $message->id; 214 } 215 216 if ($refreshusers) { 217 echo "if (parent.users.document.anchors[0] != null) {" . 218 "parent.users.location.href = parent.users.document.anchors[0].href;}\n"; 219 } else { 220 foreach ($us as $uid => $lastping) { 221 $min = (int) ($lastping / 60); 222 $sec = $lastping - ($min * 60); 223 $min = $min < 10 ? '0'.$min : $min; 224 $sec = $sec < 10 ? '0'.$sec : $sec; 225 $idle = $min.':'.$sec; 226 echo "if (parent.users.document.getElementById('uidle{$uid}') != null) {". 227 "parent.users.document.getElementById('uidle{$uid}').innerHTML = '$idle';}\n"; 228 } 229 } 230 231 print <<<EOD 232 if(parent.input){ 233 var autoscroll = parent.input.document.getElementById('auto'); 234 if(parent.msg && autoscroll && autoscroll.checked){ 235 parent.msg.scroll(1,5000000); 236 } 237 } 238 EOD; 239 print "//]]>\n"; 240 print '</script>' . "\n\n"; 241 if ($beep) { 242 print '<embed src="../beep.wav" autostart="true" hidden="true" name="beep" />'; 243 } 244 print $CHAT_DUMMY_DATA; 245 sleep($CFG->chat_refresh_room); 246 } // Here ends the for() loop. 247 248 // Here & should be written & :-D. 249 $refreshurl = "{$CFG->wwwroot}/mod/chat/gui_header_js/jsupdated.php?"; 250 $refreshurl .= "chat_sid=$chatsid&chat_lasttime=$chatlasttime&chat_lastrow=$chatnewrow&chat_lastid=$chatlastid"; 251 252 print '<script type="text/javascript">' . "\n"; 253 print "//<![CDATA[ \n\n"; 254 print "location.href = '$refreshurl';\n"; 255 print "//]]>\n"; 256 print '</script>' . "\n\n"; 257 ?> 258 259 </body> 260 </html>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |