[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/mnet/ -> peer.php (source)

   1  <?php // $Id: peer.php,v 1.9.2.3 2008/08/27 03:33:14 peterbulmer Exp $
   2  /**
   3   * An object to represent lots of information about an RPC-peer machine
   4   *
   5   * @author  Donal McMullan  donal@catalyst.net.nz
   6   * @version 0.0.1
   7   * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
   8   * @package mnet
   9   */
  10  
  11  class mnet_peer {
  12  
  13      //Unless stated otherwise, properties of this object are unescaped, and unsafe to
  14      //insert into the db without further processing.
  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      //Object whose properties need to be put into the database:
  29      //(properties here are slashescaped)
  30      var $updateparams;
  31  
  32      function mnet_peer() {
  33          $this->updateparams = new stdClass();
  34          return true;
  35      }
  36  
  37      function bootstrap($wwwroot, $pubkey = null, $application) {
  38  
  39          if (substr($wwwroot, -1, 1) == '/') {
  40              $wwwroot = substr($wwwroot, 0, -1);
  41          }
  42  
  43          if ( ! $this->set_wwwroot($wwwroot) ) {
  44              $hostname = mnet_get_hostname_from_uri($wwwroot);
  45  
  46              // Get the IP address for that host - if this fails, it will
  47              // return the hostname string
  48              $ip_address = gethostbyname($hostname);
  49  
  50              // Couldn't find the IP address?
  51              if ($ip_address === $hostname && !preg_match('/^\d+\.\d+\.\d+.\d+$/',$hostname)) {
  52                  $this->error[] = array('code' => 2, 'text' => get_string("noaddressforhost", 'mnet'));
  53                  return false;
  54              }
  55  
  56              $this->name = stripslashes($wwwroot);
  57              $this->updateparams->name = $wwwroot;
  58  
  59              // TODO: In reality, this will be prohibitively slow... need another
  60              // default - maybe blank string
  61              $homepage = file_get_contents($wwwroot);
  62              if (!empty($homepage)) {
  63                  $count = preg_match("@<title>(.*)</title>@siU", $homepage, $matches);
  64                  if ($count > 0) {
  65                      $this->name = $matches[1];
  66                      $this->updateparams->name = addslashes($matches[1]);
  67                  }
  68              }
  69  
  70              $this->wwwroot = stripslashes($wwwroot);
  71              $this->updateparams->wwwroot = $wwwroot;
  72              $this->ip_address = $ip_address;
  73              $this->updateparams->ip_address = $ip_address;
  74              $this->deleted = 0;
  75              $this->updateparams->deleted = 0;
  76  
  77              $this->application = get_record('mnet_application', 'name', $application);
  78              if (empty($this->application)) {
  79                  $this->application = get_record('mnet_application', 'name', 'moodle');
  80              }
  81  
  82              $this->applicationid = $this->application->id;
  83              $this->updateparams->applicationid = $this->application->id;
  84  
  85              if(empty($pubkey)) {
  86                  $pubkeytemp = clean_param(mnet_get_public_key($this->wwwroot, $this->application), PARAM_PEM);
  87              } else {
  88                  $pubkeytemp = clean_param($pubkey, PARAM_PEM);
  89              }
  90              $this->public_key_expires = $this->check_common_name($pubkeytemp);
  91  
  92              if ($this->public_key_expires == false) {
  93                  return false;
  94              }
  95              $this->updateparams->public_key_expires = $this->public_key_expires;
  96  
  97              $this->updateparams->public_key = $pubkeytemp;
  98              $this->public_key = $pubkeytemp;
  99  
 100              $this->last_connect_time = 0;
 101              $this->updateparams->last_connect_time = 0;
 102              $this->last_log_id = 0;
 103              $this->updateparams->last_log_id = 0;
 104          }
 105  
 106          return true;
 107      }
 108  
 109      function delete() {
 110          if ($this->deleted) return true;
 111  
 112          $users = count_records('user','mnethostid', $this->id);
 113          if ($users > 0) {
 114              $this->deleted = 1;
 115              $this->updateparams->deleted = 1;
 116          }
 117  
 118          $actions = count_records('mnet_log','hostid', $this->id);
 119          if ($actions > 0) {
 120              $this->deleted = 1;
 121              $this->updateparams->deleted = 1;
 122          }
 123  
 124          $obj = delete_records('mnet_rpc2host', 'host_id', $this->id);
 125  
 126          $this->delete_all_sessions();
 127  
 128          // If we don't have any activity records for which the mnet_host table
 129          // provides a foreign key, then we can delete the record. Otherwise, we
 130          // just mark it as deleted.
 131          if (0 == $this->deleted) {
 132              delete_records('mnet_host', "id", $this->id);
 133          } else {
 134              $this->commit();
 135          }
 136      }
 137  
 138      function count_live_sessions() {
 139          $obj = $this->delete_expired_sessions();
 140          return count_records('mnet_session','mnethostid', $this->id);
 141      }
 142  
 143      function delete_expired_sessions() {
 144          $now = time();
 145          return delete_records_select('mnet_session', " mnethostid = '{$this->id}' AND expires < '$now' ");
 146      }
 147  
 148      function delete_all_sessions() {
 149          global $CFG;
 150          // TODO: Expires each PHP session individually
 151          // $sessions = get_records('mnet_session', 'mnethostid', $this->id);
 152          $sessions = get_records('mnet_session', 'mnethostid', $this->id);
 153  
 154          if (count($sessions) > 0 && file_exists($CFG->dirroot.'/auth/mnet/auth.php')) {
 155              require_once($CFG->dirroot.'/auth/mnet/auth.php');
 156              $auth = new auth_plugin_mnet();
 157              $auth->end_local_sessions($sessions);
 158          }
 159  
 160          $deletereturn = delete_records_select('mnet_session', " mnethostid = '{$this->id}'");
 161          return true;
 162      }
 163  
 164      function check_common_name($key) {
 165          $credentials = openssl_x509_parse($key);
 166          if ($credentials == false) {
 167              $this->error[] = array('code' => 3, 'text' => get_string("nonmatchingcert", 'mnet', array('','')));
 168              return false;
 169          } elseif ($credentials['subject']['CN'] != $this->wwwroot) {
 170              $a[] = $credentials['subject']['CN'];
 171              $a[] = $this->wwwroot;
 172              $this->error[] = array('code' => 4, 'text' => get_string("nonmatchingcert", 'mnet', $a));
 173              return false;
 174          } else {
 175              return $credentials['validTo_time_t'];
 176          }
 177      }
 178  
 179      function commit() {
 180          $obj = $this->updateparams;
 181  
 182          if (isset($this->id) && $this->id > 0) {
 183              $obj->id = $this->id;
 184              $dbresult = update_record('mnet_host', $obj);
 185          } else {
 186              $this->id = insert_record('mnet_host', $obj);
 187              $dbresult = ($this->id > 0);
 188          }
 189          //If the insert/update was successful, clear the parameters that need updating
 190          if ($dbresult) {
 191              $this->updateparams = new stdClass();
 192          }
 193          return $dbresult;
 194      }
 195  
 196      function touch() {
 197          $this->last_connect_time = time();
 198          $this->updateparams->last_connect_time = time();
 199          $this->commit();
 200      }
 201  
 202      function set_name($newname) {
 203          if (is_string($newname) && strlen($newname <= 80)) {
 204              $this->name = stripslashes($newname);
 205              $this->updateparams->name = $newname;
 206              return true;
 207          }
 208          return false;
 209      }
 210  
 211      function set_applicationid($applicationid) {
 212          if (is_numeric($applicationid) && $applicationid == intval($applicationid)) {
 213              $this->applicationid = $applicationid;
 214              $this->updateparams->applicationid = $applicationid;
 215              return true;
 216          }
 217          return false;
 218      }
 219  
 220      function set_wwwroot($wwwroot) {
 221          global $CFG;
 222  
 223          $hostinfo = get_record('mnet_host', 'wwwroot', $wwwroot);
 224  
 225          if ($hostinfo != false) {
 226              $this->populate($hostinfo);
 227              return true;
 228          }
 229          return false;
 230      }
 231  
 232      function set_id($id) {
 233          global $CFG;
 234  
 235          if (clean_param($id, PARAM_INT) != $id) {
 236              $this->errno[]  = 1;
 237              $this->errmsg[] = 'Your id ('.$id.') is not legal';
 238              return false;
 239          }
 240  
 241          $sql = "
 242                  SELECT
 243                      h.*
 244                  FROM
 245                      {$CFG->prefix}mnet_host h
 246                  WHERE
 247                      h.id = '". $id ."'";
 248  
 249          if ($hostinfo = get_record_sql($sql)) {
 250              $this->populate($hostinfo);
 251              return true;
 252          }
 253          return false;
 254      }
 255  
 256      /**
 257       * Several methods can be used to get an 'mnet_host' record. They all then
 258       * send it to this private method to populate this object's attributes.
 259       * 
 260       * @param   object  $hostinfo   A database record from the mnet_host table
 261       * @return  void
 262       */
 263      function populate($hostinfo) {
 264          $this->id                   = $hostinfo->id;
 265          $this->wwwroot              = $hostinfo->wwwroot;
 266          $this->ip_address           = $hostinfo->ip_address;
 267          $this->name                 = $hostinfo->name;
 268          $this->deleted              = $hostinfo->deleted;
 269          $this->public_key           = $hostinfo->public_key;
 270          $this->public_key_expires   = $hostinfo->public_key_expires;
 271          $this->last_connect_time    = $hostinfo->last_connect_time;
 272          $this->last_log_id          = $hostinfo->last_log_id;
 273          $this->force_theme          = $hostinfo->force_theme;
 274          $this->theme                = $hostinfo->theme;
 275          $this->applicationid        = $hostinfo->applicationid;
 276          $this->application = get_record('mnet_application', 'id', $this->applicationid);
 277      }
 278  
 279      function get_public_key() {
 280          if (isset($this->public_key_ref)) return $this->public_key_ref;
 281          $this->public_key_ref = openssl_pkey_get_public($this->public_key);
 282          return $this->public_key_ref;
 283      }
 284  }
 285  
 286  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7