Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   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  define('NO_MOODLE_COOKIES', true); // Session not used here.
  18  
  19  require('../../../config.php');
  20  require_once ('../lib.php');
  21  
  22  $chatsid      = required_param('chat_sid', PARAM_ALPHANUM);
  23  $chatlasttime = optional_param('chat_lasttime', 0, PARAM_INT);
  24  $chatlastrow  = optional_param('chat_lastrow', 1, PARAM_INT);
  25  
  26  $url = new moodle_url('/mod/chat/gui_header_js/jsupdate.php', array('chat_sid' => $chatsid));
  27  if ($chatlasttime !== 0) {
  28      $url->param('chat_lasttime', $chatlasttime);
  29  }
  30  if ($chatlastrow !== 1) {
  31      $url->param('chat_lastrow', $chatlastrow);
  32  }
  33  $PAGE->set_url($url);
  34  
  35  
  36  if (!$chatuser = $DB->get_record('chat_users', array('sid' => $chatsid))) {
  37      print_error('notlogged', 'chat');
  38  }
  39  
  40  // Get the minimal course.
  41  if (!$course = $DB->get_record('course', array('id' => $chatuser->course))) {
  42      print_error('invalidcourseid');
  43  }
  44  
  45  // Get the user theme and enough info to be used in chat_format_message() which passes it along to.
  46  // No optimisation here, it would break again in future!
  47  if (!$user = $DB->get_record('user', array('id' => $chatuser->userid, 'deleted' => 0, 'suspended' => 0))) {
  48      print_error('invaliduser');
  49  }
  50  \core\session\manager::set_user($user);
  51  
  52  // Setup course, lang and theme.
  53  $PAGE->set_course($course);
  54  
  55  // Force deleting of timed out users if there is a silence in room or just entering.
  56  if ((time() - $chatlasttime) > $CFG->chat_old_ping) {
  57      // Must be done before chat_get_latest_message!
  58      chat_delete_old_users();
  59  }
  60  
  61  if ($message = chat_get_latest_message($chatuser->chatid, $chatuser->groupid)) {
  62      $chatnewlasttime = $message->timestamp;
  63  } else {
  64      $chatnewlasttime = 0;
  65  }
  66  
  67  if ($chatlasttime == 0) { // Display some previous messages.
  68      $chatlasttime = time() - $CFG->chat_old_ping; // TO DO - any better value?
  69  }
  70  
  71  $timenow    = time();
  72  
  73  $params = array('groupid' => $chatuser->groupid, 'chatid' => $chatuser->chatid, 'lasttime' => $chatlasttime);
  74  
  75  $groupselect = $chatuser->groupid ? " AND (groupid=:groupid OR groupid=0) " : "";
  76  
  77  $messages = $DB->get_records_select("chat_messages_current",
  78                      "chatid = :chatid AND timestamp > :lasttime $groupselect", $params,
  79                      "timestamp ASC");
  80  
  81  if ($messages) {
  82      $num = count($messages);
  83  } else {
  84      $num = 0;
  85  }
  86  
  87  $chatnewrow = ($chatlastrow + $num) % 2;
  88  
  89  // No &amp; in url, does not work in header!
  90  $baseurl = "{$CFG->wwwroot}/mod/chat/gui_header_js/jsupdate.php?";
  91  $refreshurl = $baseurl . "chat_sid=$chatsid&chat_lasttime=$chatnewlasttime&chat_lastrow=$chatnewrow";
  92  $refreshurlamp = $baseurl . "chat_sid=$chatsid&amp;chat_lasttime=$chatnewlasttime&amp;chat_lastrow=$chatnewrow";
  93  
  94  header('Expires: Sun, 28 Dec 1997 09:32:45 GMT');
  95  header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  96  header('Cache-Control: no-cache, must-revalidate');
  97  header('Pragma: no-cache');
  98  header('Content-Type: text/html; charset=utf-8');
  99  header("Refresh: $CFG->chat_refresh_room; url=$refreshurl");
 100  
 101  // Use ob to be able to send Content-Length headers.
 102  // Needed for Keep-Alive to work.
 103  ob_start();
 104  
 105  ?>
 106  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 107  <html>
 108      <head>
 109          <meta http-equiv="content-type" content="text/html; charset=utf-8" />
 110          <script type="text/javascript">
 111          //<![CDATA[
 112          if (parent.msg && parent.msg.document.getElementById("msgStarted") == null) {
 113              parent.msg.document.close();
 114              parent.msg.document.open("text/html","replace");
 115              parent.msg.document.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
 116              parent.msg.document.write("<html><head>");
 117              parent.msg.document.write("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />");
 118              parent.msg.document.write("<base target=\"_blank\" />");
 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 (($chatlasttime != $chatnewlasttime) and $messages) {
 126  
 127      foreach ($messages as $message) {
 128          $chatlastrow = ($chatlastrow + 1) % 2;
 129          $formatmessage = chat_format_message($message, $chatuser->course, $USER, $chatlastrow);
 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  }
 141  
 142  $chatuser->lastping = time();
 143  $DB->set_field('chat_users', 'lastping', $chatuser->lastping, array('id' => $chatuser->id));
 144  
 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 '<script> (function() {';
 177      echo 'var audioElement = document.createElement("audio");';
 178      echo 'audioElement.setAttribute("src", "../beep.mp3");';
 179      echo 'audioElement.play(); })();';
 180      echo '</script>';
 181  }
 182  ?>
 183         <a href="<?php echo $refreshurlamp ?>" name="refreshLink">Refresh link</a>
 184      </body>
 185  </html>
 186  <?php
 187  
 188  // Support HTTP Keep-Alive.
 189  header("Content-Length: " . ob_get_length() );
 190  ob_end_flush();
 191  exit;
 192