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
18 var $public_key_expires = 0;
19 var $last_connect_time = 0;
21 var $applicationid = 1; // Default of 1 == Moodle
22 var $keypair = array();
25 function mnet_peer() {
29 function bootstrap($wwwroot, $pubkey = null, $application) {
31 if (substr($wwwroot, -1, 1) == '/') {
32 $wwwroot = substr($wwwroot, 0, -1);
35 if ( ! $this->set_wwwroot($wwwroot) ) {
36 $hostname = mnet_get_hostname_from_uri($wwwroot);
38 // Get the IP address for that host - if this fails, it will
39 // return the hostname string
40 $ip_address = gethostbyname($hostname);
42 // Couldn't find the IP address?
43 if ($ip_address === $hostname && !preg_match('/^\d+\.\d+\.\d+.\d+$/',$hostname)) {
44 $this->error
[] = array('code' => 2, 'text' => get_string("noaddressforhost", 'mnet'));
48 $this->name
= $wwwroot;
50 // TODO: In reality, this will be prohibitively slow... need another
51 // default - maybe blank string
52 $homepage = file_get_contents($wwwroot);
53 if (!empty($homepage)) {
54 $count = preg_match("@<title>(.*)</title>@siU", $homepage, $matches);
56 $this->name
= $matches[1];
60 $this->wwwroot
= $wwwroot;
61 $this->ip_address
= $ip_address;
64 $this->application
= get_record('mnet_application', 'name', $application);
65 if (empty($this->application
)) {
66 $this->application
= get_record('mnet_application', 'name', 'moodle');
69 $this->applicationid
= $this->application
->id
;
72 $this->public_key
= clean_param(mnet_get_public_key($this->wwwroot
, $this->application
), PARAM_PEM
);
74 $this->public_key
= clean_param($pubkey, PARAM_PEM
);
76 $this->public_key_expires
= $this->check_common_name($this->public_key
);
77 $this->last_connect_time
= 0;
78 $this->last_log_id
= 0;
79 if ($this->public_key_expires
== false) {
80 $this->public_key
== '';
89 if ($this->deleted
) return true;
91 $users = count_records('user','mnethostid', $this->id
);
96 $actions = count_records('mnet_log','hostid', $this->id
);
101 $obj = delete_records('mnet_rpc2host', 'host_id', $this->id
);
103 $this->delete_all_sessions();
105 // If we don't have any activity records for which the mnet_host table
106 // provides a foreign key, then we can delete the record. Otherwise, we
107 // just mark it as deleted.
108 if (0 == $this->deleted
) {
109 delete_records('mnet_host', "id", $this->id
);
115 function count_live_sessions() {
116 $obj = $this->delete_expired_sessions();
117 return count_records('mnet_session','mnethostid', $this->id
);
120 function delete_expired_sessions() {
122 return delete_records_select('mnet_session', " mnethostid = '{$this->id}' AND expires < '$now' ");
125 function delete_all_sessions() {
127 // TODO: Expires each PHP session individually
128 // $sessions = get_records('mnet_session', 'mnethostid', $this->id);
129 $sessions = get_records('mnet_session', 'mnethostid', $this->id
);
131 if (count($sessions) > 0 && file_exists($CFG->dirroot
.'/auth/mnet/auth.php')) {
132 require_once($CFG->dirroot
.'/auth/mnet/auth.php');
133 $auth = new auth_plugin_mnet();
134 $auth->end_local_sessions($sessions);
137 $deletereturn = delete_records_select('mnet_session', " mnethostid = '{$this->id}'");
141 function check_common_name($key) {
142 $credentials = openssl_x509_parse($key);
143 if ($credentials == false) {
144 $this->error
[] = array('code' => 3, 'text' => get_string("nonmatchingcert", 'mnet', array('','')));
146 } elseif ($credentials['subject']['CN'] != $this->wwwroot
) {
147 $a[] = $credentials['subject']['CN'];
148 $a[] = $this->wwwroot
;
149 $this->error
[] = array('code' => 4, 'text' => get_string("nonmatchingcert", 'mnet', $a));
152 return $credentials['validTo_time_t'];
157 $obj = new stdClass();
159 $obj->wwwroot
= $this->wwwroot
;
160 $obj->ip_address
= $this->ip_address
;
161 $obj->name
= $this->name
;
162 $obj->public_key
= $this->public_key
;
163 $obj->public_key_expires
= $this->public_key_expires
;
164 $obj->deleted
= $this->deleted
;
165 $obj->last_connect_time
= $this->last_connect_time
;
166 $obj->last_log_id
= $this->last_log_id
;
167 $obj->applicationid
= $this->applicationid
;
169 if (isset($this->id
) && $this->id
> 0) {
170 $obj->id
= $this->id
;
171 return update_record('mnet_host', $obj);
173 $this->id
= insert_record('mnet_host', $obj);
174 return $this->id
> 0;
179 $this->last_connect_time
= time();
183 function set_name($newname) {
184 if (is_string($newname) && strlen($newname <= 80)) {
185 $this->name
= $newname;
191 function set_applicationid($applicationid) {
192 if (is_numeric($applicationid) && $applicationid == intval($applicationid)) {
193 $this->applicationid
= $applicationid;
199 function set_wwwroot($wwwroot) {
202 $hostinfo = get_record('mnet_host', 'wwwroot', $wwwroot);
204 if ($hostinfo != false) {
205 $this->populate($hostinfo);
211 function set_id($id) {
214 if (clean_param($id, PARAM_INT
) != $id) {
216 $this->errmsg
[] = 'Your id ('.$id.') is not legal';
224 {$CFG->prefix}mnet_host h
228 if ($hostinfo = get_record_sql($sql)) {
229 $this->populate($hostinfo);
236 * Several methods can be used to get an 'mnet_host' record. They all then
237 * send it to this private method to populate this object's attributes.
239 * @param object $hostinfo A database record from the mnet_host table
242 function populate($hostinfo) {
243 $this->id
= $hostinfo->id
;
244 $this->wwwroot
= $hostinfo->wwwroot
;
245 $this->ip_address
= $hostinfo->ip_address
;
246 $this->name
= $hostinfo->name
;
247 $this->deleted
= $hostinfo->deleted
;
248 $this->public_key
= $hostinfo->public_key
;
249 $this->public_key_expires
= $hostinfo->public_key_expires
;
250 $this->last_connect_time
= $hostinfo->last_connect_time
;
251 $this->last_log_id
= $hostinfo->last_log_id
;
252 $this->applicationid
= $hostinfo->applicationid
;
253 $this->application
= get_record('mnet_application', 'id', $this->applicationid
);
256 function get_public_key() {
257 if (isset($this->public_key_ref
)) return $this->public_key_ref
;
258 $this->public_key_ref
= openssl_pkey_get_public($this->public_key
);
259 return $this->public_key_ref
;