[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mnet/ -> peer.php (source)

   1  <?php
   2  /**
   3   * An object to represent lots of information about an RPC-peer machine
   4   *
   5   * @author  Donal McMullan  [email protected]
   6   * @version 0.0.1
   7   * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
   8   * @package mnet
   9   */
  10  
  11  require_once($CFG->libdir . '/filelib.php'); // download_file_content() used here
  12  
  13  class mnet_peer {
  14  
  15      var $id                 = 0;
  16      var $wwwroot            = '';
  17      var $ip_address         = '';
  18      var $name               = '';
  19      var $public_key         = '';
  20      var $public_key_expires = 0;
  21      var $last_connect_time  = 0;
  22      var $last_log_id        = 0;
  23      var $force_theme        = 0;
  24      var $theme              = '';
  25      var $applicationid      = 1; // Default of 1 == Moodle
  26      var $keypair            = array();
  27      var $error              = array();
  28      var $bootstrapped       = false; // set when the object is populated
  29  
  30      function mnet_peer() {
  31          return true;
  32      }
  33  
  34      /*
  35       * Fetch information about a peer identified by wwwroot
  36       * If information does not preexist in db, collect it together based on
  37       * supplied information
  38       *
  39       * @param string $wwwroot - address of peer whose details we want
  40       * @param string $pubkey - to use if we add a record to db for new peer
  41       * @param int $application - table id - what kind of peer are we talking to
  42       * @return bool - indication of success or failure
  43       */
  44      function bootstrap($wwwroot, $pubkey = null, $application) {
  45          global $DB;
  46  
  47          if (substr($wwwroot, -1, 1) == '/') {
  48              $wwwroot = substr($wwwroot, 0, -1);
  49          }
  50  
  51          // If a peer record already exists for this address,
  52          // load that info and return
  53          if ($this->set_wwwroot($wwwroot)) {
  54              return true;
  55          }
  56  
  57          $hostname = mnet_get_hostname_from_uri($wwwroot);
  58          // Get the IP address for that host - if this fails, it will return the hostname string
  59          $ip_address = gethostbyname($hostname);
  60  
  61          // Couldn't find the IP address?
  62          if ($ip_address === $hostname && !preg_match('/^\d+\.\d+\.\d+.\d+$/',$hostname)) {
  63              throw new moodle_exception('noaddressforhost', 'mnet', '', $hostname);
  64          }
  65  
  66          $this->name = $wwwroot;
  67  
  68          // TODO: In reality, this will be prohibitively slow... need another
  69          // default - maybe blank string
  70          $homepage = download_file_content($wwwroot);
  71          if (!empty($homepage)) {
  72              $count = preg_match("@<title>(.*)</title>@siU", $homepage, $matches);
  73              if ($count > 0) {
  74                  $this->name = $matches[1];
  75              }
  76          }
  77  
  78          $this->wwwroot              = $wwwroot;
  79          $this->ip_address           = $ip_address;
  80          $this->deleted              = 0;
  81  
  82          $this->application = $DB->get_record('mnet_application', array('name'=>$application));
  83          if (empty($this->application)) {
  84              $this->application = $DB->get_record('mnet_application', array('name'=>'moodle'));
  85          }
  86  
  87          $this->applicationid = $this->application->id;
  88  
  89          if(empty($pubkey)) {
  90              $this->public_key           = clean_param(mnet_get_public_key($this->wwwroot, $this->application), PARAM_PEM);
  91          } else {
  92              $this->public_key           = clean_param($pubkey, PARAM_PEM);
  93          }
  94          $this->public_key_expires   = $this->check_common_name($this->public_key);
  95          $this->last_connect_time    = 0;
  96          $this->last_log_id          = 0;
  97          if ($this->public_key_expires == false) {
  98              $this->public_key == '';
  99              return false;
 100          }
 101          $this->bootstrapped = true;
 102      }
 103  
 104      /*
 105       * Delete mnet peer
 106       * the peer is marked as deleted in the database
 107       * we delete current sessions.
 108       * @return bool - success
 109       */
 110      function delete() {
 111          global $DB;
 112  
 113          if ($this->deleted) {
 114              return true;
 115          }
 116  
 117          $this->delete_all_sessions();
 118  
 119          $this->deleted = 1;
 120          return $this->commit();
 121      }
 122  
 123      function count_live_sessions() {
 124          global $DB;
 125          $obj = $this->delete_expired_sessions();
 126          return $DB->count_records('mnet_session', array('mnethostid'=>$this->id));
 127      }
 128  
 129      function delete_expired_sessions() {
 130          global $DB;
 131          $now = time();
 132          return $DB->delete_records_select('mnet_session', " mnethostid = ? AND expires < ? ", array($this->id, $now));
 133      }
 134  
 135      function delete_all_sessions() {
 136          global $CFG, $DB;
 137          // TODO: Expires each PHP session individually
 138          $sessions = $DB->get_records('mnet_session', array('mnethostid'=>$this->id));
 139  
 140          if (count($sessions) > 0 && file_exists($CFG->dirroot.'/auth/mnet/auth.php')) {
 141              require_once($CFG->dirroot.'/auth/mnet/auth.php');
 142              $auth = new auth_plugin_mnet();
 143              $auth->end_local_sessions($sessions);
 144          }
 145  
 146          $deletereturn = $DB->delete_records('mnet_session', array('mnethostid'=>$this->id));
 147          return true;
 148      }
 149  
 150      function check_common_name($key) {
 151          $credentials = $this->check_credentials($key);
 152          return $credentials['validTo_time_t'];
 153      }
 154  
 155      function check_credentials($key) {
 156          $credentials = openssl_x509_parse($key);
 157          if ($credentials == false) {
 158              $this->error[] = array('code' => 3, 'text' => get_string("nonmatchingcert", 'mnet', array('subject' => '','host' => '')));
 159              return false;
 160          } elseif (array_key_exists('subjectAltName', $credentials['subject']) && $credentials['subject']['subjectAltName'] != $this->wwwroot) {
 161              $a['subject'] = $credentials['subject']['subjectAltName'];
 162              $a['host'] = $this->wwwroot;
 163              $this->error[] = array('code' => 5, 'text' => get_string("nonmatchingcert", 'mnet', $a));
 164              return false;
 165          } elseif ($credentials['subject']['CN'] != $this->wwwroot) {
 166              $a['subject'] = $credentials['subject']['CN'];
 167              $a['host'] = $this->wwwroot;
 168              $this->error[] = array('code' => 4, 'text' => get_string("nonmatchingcert", 'mnet', $a));
 169              return false;
 170          } else {
 171              if (array_key_exists('subjectAltName', $credentials['subject'])) {
 172                  $credentials['wwwroot'] = $credentials['subject']['subjectAltName'];
 173              } else {
 174                  $credentials['wwwroot'] = $credentials['subject']['CN'];
 175              }
 176              return $credentials;
 177          }
 178      }
 179  
 180      function commit() {
 181          global $DB;
 182          $obj = new stdClass();
 183  
 184          $obj->wwwroot               = $this->wwwroot;
 185          $obj->ip_address            = $this->ip_address;
 186          $obj->name                  = $this->name;
 187          $obj->public_key            = $this->public_key;
 188          $obj->public_key_expires    = $this->public_key_expires;
 189          $obj->deleted               = $this->deleted;
 190          $obj->last_connect_time     = $this->last_connect_time;
 191          $obj->last_log_id           = $this->last_log_id;
 192          $obj->force_theme           = $this->force_theme;
 193          $obj->theme                 = $this->theme;
 194          $obj->applicationid         = $this->applicationid;
 195  
 196          if (isset($this->id) && $this->id > 0) {
 197              $obj->id = $this->id;
 198              return $DB->update_record('mnet_host', $obj);
 199          } else {
 200              $this->id = $DB->insert_record('mnet_host', $obj);
 201              return $this->id > 0;
 202          }
 203      }
 204  
 205      function touch() {
 206          $this->last_connect_time = time();
 207          $this->commit();
 208      }
 209  
 210      function set_name($newname) {
 211          if (is_string($newname) && strlen($newname <= 80)) {
 212              $this->name = $newname;
 213              return true;
 214          }
 215          return false;
 216      }
 217  
 218      function set_applicationid($applicationid) {
 219          if (is_numeric($applicationid) && $applicationid == intval($applicationid)) {
 220              $this->applicationid = $applicationid;
 221              return true;
 222          }
 223          return false;
 224      }
 225  
 226      /**
 227       * Load information from db about an mnet peer into this object's properties
 228       *
 229       * @param string $wwwroot - address of peer whose details we want to load
 230       * @return bool - indication of success or failure
 231       */
 232      function set_wwwroot($wwwroot) {
 233          global $CFG, $DB;
 234  
 235          $hostinfo = $DB->get_record('mnet_host', array('wwwroot'=>$wwwroot));
 236  
 237          if ($hostinfo != false) {
 238              $this->populate($hostinfo);
 239              return true;
 240          }
 241          return false;
 242      }
 243  
 244      function set_id($id) {
 245          global $CFG, $DB;
 246  
 247          if (clean_param($id, PARAM_INT) != $id) {
 248              $this->errno[]  = 1;
 249              $this->errmsg[] = 'Your id ('.$id.') is not legal';
 250              return false;
 251          }
 252  
 253          $sql = "
 254                  SELECT
 255                      h.*
 256                  FROM
 257                      {mnet_host} h
 258                  WHERE
 259                      h.id = ?";
 260  
 261          if ($hostinfo = $DB->get_record_sql($sql, array($id))) {
 262              $this->populate($hostinfo);
 263              return true;
 264          }
 265          return false;
 266      }
 267  
 268      /**
 269       * Several methods can be used to get an 'mnet_host' record. They all then
 270       * send it to this private method to populate this object's attributes.
 271       *
 272       * @param   object  $hostinfo   A database record from the mnet_host table
 273       * @return  void
 274       */
 275      function populate($hostinfo) {
 276          global $DB;
 277          $this->id                   = $hostinfo->id;
 278          $this->wwwroot              = $hostinfo->wwwroot;
 279          $this->ip_address           = $hostinfo->ip_address;
 280          $this->name                 = $hostinfo->name;
 281          $this->deleted              = $hostinfo->deleted;
 282          $this->public_key           = $hostinfo->public_key;
 283          $this->public_key_expires   = $hostinfo->public_key_expires;
 284          $this->last_connect_time    = $hostinfo->last_connect_time;
 285          $this->last_log_id          = $hostinfo->last_log_id;
 286          $this->force_theme          = $hostinfo->force_theme;
 287          $this->theme                = $hostinfo->theme;
 288          $this->applicationid        = $hostinfo->applicationid;
 289          $this->application = $DB->get_record('mnet_application', array('id'=>$this->applicationid));
 290          $this->bootstrapped = true;
 291      }
 292  
 293      function get_public_key() {
 294          if (isset($this->public_key_ref)) return $this->public_key_ref;
 295          $this->public_key_ref = openssl_pkey_get_public($this->public_key);
 296          return $this->public_key_ref;
 297      }
 298  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1