3 * An object to represent lots of information about an RPC-peer machine
5 * @author Donal McMullan donal@catalyst.net.nz
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
11 require_once($CFG->libdir
. '/filelib.php'); // download_file_content() used here
20 var $public_key_expires = 0;
21 var $last_connect_time = 0;
25 var $applicationid = 1; // Default of 1 == Moodle
26 var $keypair = array();
28 var $bootstrapped = false; // set when the object is populated
30 function mnet_peer() {
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
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
44 function bootstrap($wwwroot, $pubkey = null, $application) {
47 if (substr($wwwroot, -1, 1) == '/') {
48 $wwwroot = substr($wwwroot, 0, -1);
51 // If a peer record already exists for this address,
52 // load that info and return
53 if ($this->set_wwwroot($wwwroot)) {
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);
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);
66 $this->name
= $wwwroot;
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);
74 $this->name
= $matches[1];
78 $this->wwwroot
= $wwwroot;
79 $this->ip_address
= $ip_address;
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'));
87 $this->applicationid
= $this->application
->id
;
90 $this->public_key
= clean_param(mnet_get_public_key($this->wwwroot
, $this->application
), PARAM_PEM
);
92 $this->public_key
= clean_param($pubkey, PARAM_PEM
);
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
== '';
101 $this->bootstrapped
= true;
106 * the peer is marked as deleted in the database
107 * we delete current sessions.
108 * @return bool - success
113 if ($this->deleted
) {
117 $this->delete_all_sessions();
120 return $this->commit();
123 function count_live_sessions() {
125 $obj = $this->delete_expired_sessions();
126 return $DB->count_records('mnet_session', array('mnethostid'=>$this->id
));
129 function delete_expired_sessions() {
132 return $DB->delete_records_select('mnet_session', " mnethostid = ? AND expires < ? ", array($this->id
, $now));
135 function delete_all_sessions() {
137 // TODO: Expires each PHP session individually
138 $sessions = $DB->get_records('mnet_session', array('mnethostid'=>$this->id
));
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);
146 $deletereturn = $DB->delete_records('mnet_session', array('mnethostid'=>$this->id
));
150 function check_common_name($key) {
151 $credentials = $this->check_credentials($key);
152 return $credentials['validTo_time_t'];
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' => '')));
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));
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));
171 if (array_key_exists('subjectAltName', $credentials['subject'])) {
172 $credentials['wwwroot'] = $credentials['subject']['subjectAltName'];
174 $credentials['wwwroot'] = $credentials['subject']['CN'];
182 $obj = new stdClass();
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
;
196 if (isset($this->id
) && $this->id
> 0) {
197 $obj->id
= $this->id
;
198 return $DB->update_record('mnet_host', $obj);
200 $this->id
= $DB->insert_record('mnet_host', $obj);
201 return $this->id
> 0;
206 $this->last_connect_time
= time();
210 function set_name($newname) {
211 if (is_string($newname) && strlen($newname <= 80)) {
212 $this->name
= $newname;
218 function set_applicationid($applicationid) {
219 if (is_numeric($applicationid) && $applicationid == intval($applicationid)) {
220 $this->applicationid
= $applicationid;
227 * Load information from db about an mnet peer into this object's properties
229 * @param string $wwwroot - address of peer whose details we want to load
230 * @return bool - indication of success or failure
232 function set_wwwroot($wwwroot) {
235 $hostinfo = $DB->get_record('mnet_host', array('wwwroot'=>$wwwroot));
237 if ($hostinfo != false) {
238 $this->populate($hostinfo);
244 function set_id($id) {
247 if (clean_param($id, PARAM_INT
) != $id) {
249 $this->errmsg
[] = 'Your id ('.$id.') is not legal';
261 if ($hostinfo = $DB->get_record_sql($sql, array($id))) {
262 $this->populate($hostinfo);
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.
272 * @param object $hostinfo A database record from the mnet_host table
275 function populate($hostinfo) {
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;
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
;