MDL-41999 fix setting of USER in chat
[moodle.git] / mod / chat / gui_header_js / jsupdate.php
1 <?php
3 define('NO_MOODLE_COOKIES', true); // session not used here
5 require('../../../config.php');
6 require('../lib.php');
8 $chat_sid      = required_param('chat_sid', PARAM_ALPHANUM);
9 $chat_lasttime = optional_param('chat_lasttime', 0, PARAM_INT);
10 $chat_lastrow  = optional_param('chat_lastrow', 1, PARAM_INT);
12 $url = new moodle_url('/mod/chat/gui_header_js/jsupdate.php', array('chat_sid'=>$chat_sid));
13 if ($chat_lasttime !== 0) {
14     $url->param('chat_lasttime', $chat_lasttime);
15 }
16 if ($chat_lastrow !== 1) {
17     $url->param('chat_lastrow', $chat_lastrow);
18 }
19 $PAGE->set_url($url);
22 if (!$chatuser = $DB->get_record('chat_users', array('sid'=>$chat_sid))) {
23     print_error('notlogged', 'chat');
24 }
26 //Get the minimal course
27 if (!$course = $DB->get_record('course', array('id'=>$chatuser->course))) {
28     print_error('invalidcourseid');
29 }
31 //Get the user theme and enough info to be used in chat_format_message() which passes it along to
32 if (!$user = $DB->get_record('user', array('id'=>$chatuser->userid, 'deleted'=>0, 'suspended'=>0))) { // no optimisation here, it would break again in future!
33     print_error('invaliduser');
34 }
35 \core\session\manager::set_user($user);
37 //Setup course, lang and theme
38 $PAGE->set_course($course);
40 // force deleting of timed out users if there is a silence in room or just entering
41 if ((time() - $chat_lasttime) > $CFG->chat_old_ping) {
42     // must be done before chat_get_latest_message!!!
43     chat_delete_old_users();
44 }
46 if ($message = chat_get_latest_message($chatuser->chatid, $chatuser->groupid)) {
47     $chat_newlasttime = $message->timestamp;
48 } else {
49     $chat_newlasttime = 0;
50 }
52 if ($chat_lasttime == 0) { //display some previous messages
53     $chat_lasttime = time() - $CFG->chat_old_ping; //TO DO - any better value??
54 }
56 $timenow    = time();
58 $params = array('groupid'=>$chatuser->groupid, 'chatid'=>$chatuser->chatid, 'lasttime'=>$chat_lasttime);
60 $groupselect = $chatuser->groupid ? " AND (groupid=:groupid OR groupid=0) " : "";
62 $messages = $DB->get_records_select("chat_messages_current",
63                     "chatid = :chatid AND timestamp > :lasttime $groupselect", $params,
64                     "timestamp ASC");
66 if ($messages) {
67     $num = count($messages);
68 } else {
69     $num = 0;
70 }
72 $chat_newrow = ($chat_lastrow + $num) % 2;
74 // no &amp; in url, does not work in header!
75 $refreshurl = "{$CFG->wwwroot}/mod/chat/gui_header_js/jsupdate.php?chat_sid=$chat_sid&chat_lasttime=$chat_newlasttime&chat_lastrow=$chat_newrow";
76 $refreshurlamp = "{$CFG->wwwroot}/mod/chat/gui_header_js/jsupdate.php?chat_sid=$chat_sid&amp;chat_lasttime=$chat_newlasttime&amp;chat_lastrow=$chat_newrow";
78 header('Expires: Sun, 28 Dec 1997 09:32:45 GMT');
79 header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
80 header('Cache-Control: no-cache, must-revalidate');
81 header('Pragma: no-cache');
82 header('Content-Type: text/html; charset=utf-8');
83 header("Refresh: $CFG->chat_refresh_room; url=$refreshurl");
85 /// required stylesheets
86 $stylesheetshtml = '';
87 /*foreach ($CFG->stylesheets as $stylesheet) {
88     //TODO: MDL-21120
89     $stylesheetshtml .= '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" />';
90 }*/
92 // use ob to be able to send Content-Length headers
93 // needed for Keep-Alive to work
94 ob_start();
96 ?>
97 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
98 <html>
99     <head>
100         <meta http-equiv="content-type" content="text/html; charset=utf-8" />
101         <script type="text/javascript">
102         //<![CDATA[
103         function safari_refresh() {
104             self.location.href= '<?php echo $refreshurl;?>';
105         }
106         var issafari = false;
107         if(window.devicePixelRatio){
108             issafari = true;
109             setTimeout('safari_refresh()', <?php echo $CFG->chat_refresh_room*1000;?>);
110         }
111         if (parent.msg && parent.msg.document.getElementById("msgStarted") == null) {
112             parent.msg.document.close();
113             parent.msg.document.open("text/html","replace");
114             parent.msg.document.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
115             parent.msg.document.write("<html><head>");
116             parent.msg.document.write("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />");
117             parent.msg.document.write("<base target=\"_blank\" />");
118             parent.msg.document.write("<?php echo addslashes_js($stylesheetshtml) ?>");
119             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>");
120         }
121         <?php
122         $beep = false;
123         $refreshusers = false;
124         $us = array ();
125         if (($chat_lasttime != $chat_newlasttime) and $messages) {
127             foreach ($messages as $message) {
128                 $chat_lastrow = ($chat_lastrow + 1) % 2;
129                 $formatmessage = chat_format_message($message, $chatuser->course, $USER, $chat_lastrow);
130                 if ($formatmessage->beep) {
131                      $beep = true;
132                 }
133                 if ($formatmessage->refreshusers) {
134                      $refreshusers = true;
135                 }
136                 $us[$message->userid] = $timenow - $message->timestamp;
137                 echo "if(parent.msg)";
138                 echo "parent.msg.document.write('".addslashes_js($formatmessage->html)."\\n');\n";
139              }
140         }
142         $chatuser->lastping = time();
143         $DB->set_field('chat_users', 'lastping', $chatuser->lastping, array('id'=>$chatuser->id));
145         if ($refreshusers) {
146         ?>
147         var link = parent.users.document.getElementById('refreshLink');
148         if (link != null) {
149             parent.users.location.href = link.href;
150         }
151         <?php
152         } else {
153             foreach($us as $uid=>$lastping) {
154                 $min = (int) ($lastping/60);
155                 $sec = $lastping - ($min*60);
156                 $min = $min < 10 ? '0'.$min : $min;
157                 $sec = $sec < 10 ? '0'.$sec : $sec;
158                 $idle = $min.':'.$sec;
159                 echo "if (parent.users && parent.users.document.getElementById('uidle{$uid}') != null) {".
160                         "parent.users.document.getElementById('uidle{$uid}').innerHTML = '$idle';}\n";
161             }
162         }
163         ?>
164         if(parent.input){
165             var autoscroll = parent.input.document.getElementById('auto');
166             if(parent.msg && autoscroll && autoscroll.checked){
167                 parent.msg.scroll(1,5000000);
168             }
169         }
170         //]]>
171         </script>
172     </head>
173     <body>
174        <?php
175             if ($beep) {
176                 echo '<embed src="../beep.wav" autostart="true" hidden="true" name="beep" />';
177             }
178         ?>
179        <a href="<?php echo $refreshurlamp ?>" name="refreshLink">Refresh link</a>
180     </body>
181 </html>
182 <?php
184 // support HTTP Keep-Alive
185 header("Content-Length: " . ob_get_length() );
186 ob_end_flush();
187 exit;
190 ?>