Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mnet/peer.php
Go to the documentation of this file.
00001 <?php
00011 require_once($CFG->libdir . '/filelib.php'); // download_file_content() used here
00012 
00013 class mnet_peer {
00014 
00015     var $id                 = 0;
00016     var $wwwroot            = '';
00017     var $ip_address         = '';
00018     var $name               = '';
00019     var $public_key         = '';
00020     var $public_key_expires = 0;
00021     var $last_connect_time  = 0;
00022     var $last_log_id        = 0;
00023     var $force_theme        = 0;
00024     var $theme              = '';
00025     var $applicationid      = 1; // Default of 1 == Moodle
00026     var $keypair            = array();
00027     var $error              = array();
00028     var $bootstrapped       = false; // set when the object is populated
00029 
00030     function mnet_peer() {
00031         return true;
00032     }
00033 
00034     /*
00035      * Fetch information about a peer identified by wwwroot
00036      * If information does not preexist in db, collect it together based on
00037      * supplied information
00038      *
00039      * @param string $wwwroot - address of peer whose details we want
00040      * @param string $pubkey - to use if we add a record to db for new peer
00041      * @param int $application - table id - what kind of peer are we talking to
00042      * @return bool - indication of success or failure
00043      */
00044     function bootstrap($wwwroot, $pubkey = null, $application) {
00045         global $DB;
00046 
00047         if (substr($wwwroot, -1, 1) == '/') {
00048             $wwwroot = substr($wwwroot, 0, -1);
00049         }
00050 
00051         // If a peer record already exists for this address,
00052         // load that info and return
00053         if ($this->set_wwwroot($wwwroot)) {
00054             return true;
00055         }
00056 
00057         $hostname = mnet_get_hostname_from_uri($wwwroot);
00058         // Get the IP address for that host - if this fails, it will return the hostname string
00059         $ip_address = gethostbyname($hostname);
00060 
00061         // Couldn't find the IP address?
00062         if ($ip_address === $hostname && !preg_match('/^\d+\.\d+\.\d+.\d+$/',$hostname)) {
00063             throw new moodle_exception('noaddressforhost', 'mnet', '', $hostname);
00064         }
00065 
00066         $this->name = $wwwroot;
00067 
00068         // TODO: In reality, this will be prohibitively slow... need another
00069         // default - maybe blank string
00070         $homepage = download_file_content($wwwroot);
00071         if (!empty($homepage)) {
00072             $count = preg_match("@<title>(.*)</title>@siU", $homepage, $matches);
00073             if ($count > 0) {
00074                 $this->name = $matches[1];
00075             }
00076         }
00077 
00078         $this->wwwroot              = $wwwroot;
00079         $this->ip_address           = $ip_address;
00080         $this->deleted              = 0;
00081 
00082         $this->application = $DB->get_record('mnet_application', array('name'=>$application));
00083         if (empty($this->application)) {
00084             $this->application = $DB->get_record('mnet_application', array('name'=>'moodle'));
00085         }
00086 
00087         $this->applicationid = $this->application->id;
00088 
00089         if(empty($pubkey)) {
00090             $this->public_key           = clean_param(mnet_get_public_key($this->wwwroot, $this->application), PARAM_PEM);
00091         } else {
00092             $this->public_key           = clean_param($pubkey, PARAM_PEM);
00093         }
00094         $this->public_key_expires   = $this->check_common_name($this->public_key);
00095         $this->last_connect_time    = 0;
00096         $this->last_log_id          = 0;
00097         if ($this->public_key_expires == false) {
00098             $this->public_key == '';
00099             return false;
00100         }
00101         $this->bootstrapped = true;
00102     }
00103 
00104     /*
00105      * Delete mnet peer
00106      * the peer is marked as deleted in the database
00107      * we delete current sessions.
00108      * @return bool - success
00109      */
00110     function delete() {
00111         global $DB;
00112 
00113         if ($this->deleted) {
00114             return true;
00115         }
00116 
00117         $this->delete_all_sessions();
00118 
00119         $this->deleted = 1;
00120         return $this->commit();
00121     }
00122 
00123     function count_live_sessions() {
00124         global $DB;
00125         $obj = $this->delete_expired_sessions();
00126         return $DB->count_records('mnet_session', array('mnethostid'=>$this->id));
00127     }
00128 
00129     function delete_expired_sessions() {
00130         global $DB;
00131         $now = time();
00132         return $DB->delete_records_select('mnet_session', " mnethostid = ? AND expires < ? ", array($this->id, $now));
00133     }
00134 
00135     function delete_all_sessions() {
00136         global $CFG, $DB;
00137         // TODO: Expires each PHP session individually
00138         $sessions = $DB->get_records('mnet_session', array('mnethostid'=>$this->id));
00139 
00140         if (count($sessions) > 0 && file_exists($CFG->dirroot.'/auth/mnet/auth.php')) {
00141             require_once($CFG->dirroot.'/auth/mnet/auth.php');
00142             $auth = new auth_plugin_mnet();
00143             $auth->end_local_sessions($sessions);
00144         }
00145 
00146         $deletereturn = $DB->delete_records('mnet_session', array('mnethostid'=>$this->id));
00147         return true;
00148     }
00149 
00150     function check_common_name($key) {
00151         $credentials = $this->check_credentials($key);
00152         return $credentials['validTo_time_t'];
00153     }
00154 
00155     function check_credentials($key) {
00156         $credentials = openssl_x509_parse($key);
00157         if ($credentials == false) {
00158             $this->error[] = array('code' => 3, 'text' => get_string("nonmatchingcert", 'mnet', array('subject' => '','host' => '')));
00159             return false;
00160         } elseif (array_key_exists('subjectAltName', $credentials['subject']) && $credentials['subject']['subjectAltName'] != $this->wwwroot) {
00161             $a['subject'] = $credentials['subject']['subjectAltName'];
00162             $a['host'] = $this->wwwroot;
00163             $this->error[] = array('code' => 5, 'text' => get_string("nonmatchingcert", 'mnet', $a));
00164             return false;
00165         } elseif ($credentials['subject']['CN'] != $this->wwwroot) {
00166             $a['subject'] = $credentials['subject']['CN'];
00167             $a['host'] = $this->wwwroot;
00168             $this->error[] = array('code' => 4, 'text' => get_string("nonmatchingcert", 'mnet', $a));
00169             return false;
00170         } else {
00171             if (array_key_exists('subjectAltName', $credentials['subject'])) {
00172                 $credentials['wwwroot'] = $credentials['subject']['subjectAltName'];
00173             } else {
00174                 $credentials['wwwroot'] = $credentials['subject']['CN'];
00175             }
00176             return $credentials;
00177         }
00178     }
00179 
00180     function commit() {
00181         global $DB;
00182         $obj = new stdClass();
00183 
00184         $obj->wwwroot               = $this->wwwroot;
00185         $obj->ip_address            = $this->ip_address;
00186         $obj->name                  = $this->name;
00187         $obj->public_key            = $this->public_key;
00188         $obj->public_key_expires    = $this->public_key_expires;
00189         $obj->deleted               = $this->deleted;
00190         $obj->last_connect_time     = $this->last_connect_time;
00191         $obj->last_log_id           = $this->last_log_id;
00192         $obj->force_theme           = $this->force_theme;
00193         $obj->theme                 = $this->theme;
00194         $obj->applicationid         = $this->applicationid;
00195 
00196         if (isset($this->id) && $this->id > 0) {
00197             $obj->id = $this->id;
00198             return $DB->update_record('mnet_host', $obj);
00199         } else {
00200             $this->id = $DB->insert_record('mnet_host', $obj);
00201             return $this->id > 0;
00202         }
00203     }
00204 
00205     function touch() {
00206         $this->last_connect_time = time();
00207         $this->commit();
00208     }
00209 
00210     function set_name($newname) {
00211         if (is_string($newname) && strlen($newname <= 80)) {
00212             $this->name = $newname;
00213             return true;
00214         }
00215         return false;
00216     }
00217 
00218     function set_applicationid($applicationid) {
00219         if (is_numeric($applicationid) && $applicationid == intval($applicationid)) {
00220             $this->applicationid = $applicationid;
00221             return true;
00222         }
00223         return false;
00224     }
00225 
00232     function set_wwwroot($wwwroot) {
00233         global $CFG, $DB;
00234 
00235         $hostinfo = $DB->get_record('mnet_host', array('wwwroot'=>$wwwroot));
00236 
00237         if ($hostinfo != false) {
00238             $this->populate($hostinfo);
00239             return true;
00240         }
00241         return false;
00242     }
00243 
00244     function set_id($id) {
00245         global $CFG, $DB;
00246 
00247         if (clean_param($id, PARAM_INT) != $id) {
00248             $this->errno[]  = 1;
00249             $this->errmsg[] = 'Your id ('.$id.') is not legal';
00250             return false;
00251         }
00252 
00253         $sql = "
00254                 SELECT
00255                     h.*
00256                 FROM
00257                     {mnet_host} h
00258                 WHERE
00259                     h.id = ?";
00260 
00261         if ($hostinfo = $DB->get_record_sql($sql, array($id))) {
00262             $this->populate($hostinfo);
00263             return true;
00264         }
00265         return false;
00266     }
00267 
00275     function populate($hostinfo) {
00276         global $DB;
00277         $this->id                   = $hostinfo->id;
00278         $this->wwwroot              = $hostinfo->wwwroot;
00279         $this->ip_address           = $hostinfo->ip_address;
00280         $this->name                 = $hostinfo->name;
00281         $this->deleted              = $hostinfo->deleted;
00282         $this->public_key           = $hostinfo->public_key;
00283         $this->public_key_expires   = $hostinfo->public_key_expires;
00284         $this->last_connect_time    = $hostinfo->last_connect_time;
00285         $this->last_log_id          = $hostinfo->last_log_id;
00286         $this->force_theme          = $hostinfo->force_theme;
00287         $this->theme                = $hostinfo->theme;
00288         $this->applicationid        = $hostinfo->applicationid;
00289         $this->application = $DB->get_record('mnet_application', array('id'=>$this->applicationid));
00290         $this->bootstrapped = true;
00291     }
00292 
00293     function get_public_key() {
00294         if (isset($this->public_key_ref)) return $this->public_key_ref;
00295         $this->public_key_ref = openssl_pkey_get_public($this->public_key);
00296         return $this->public_key_ref;
00297     }
00298 }
 All Data Structures Namespaces Files Functions Variables Enumerations