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 $keypair = array();
24 function mnet_peer() {
28 function bootstrap($wwwroot, $pubkey = null) {
30 if (substr($wwwroot, 0, -1) == '/') {
31 $wwwroot = substr($wwwroot, 0, -1);
34 if ( ! $this->set_wwwroot($wwwroot) ) {
35 $hostname = mnet_get_hostname_from_uri($wwwroot);
37 // Get the IP address for that host - if this fails, it will
38 // return the hostname string
39 $ip_address = gethostbyname($hostname);
41 // Couldn't find the IP address?
42 if ($ip_address === $hostname && !preg_match('/^\d+\.\d+\.\d+.\d+$/',$hostname)) {
43 $this->error
[] = array('code' => 2, 'text' => get_string("noaddressforhost", 'mnet'));
47 $this->name
= $wwwroot;
49 // TODO: In reality, this will be prohibitively slow... need another
50 // default - maybe blank string
51 $homepage = file_get_contents($wwwroot);
52 if (!empty($homepage)) {
53 $count = preg_match("@<title>(.*)</title>@siU", $homepage, $matches);
55 $this->name
= $matches[1];
59 $this->wwwroot
= $wwwroot;
60 $this->ip_address
= $ip_address;
63 $this->public_key
= clean_param(mnet_get_public_key($this->wwwroot
), PARAM_PEM
);
65 $this->public_key
= clean_param($pubkey, PARAM_PEM
);
67 $this->public_key_expires
= $this->check_common_name($this->public_key
);
68 $this->last_connect_time
= 0;
69 $this->last_log_id
= 0;
70 if ($this->public_key_expires
== false) {
71 $this->public_key
== '';
80 if ($this->deleted
) return true;
82 $users = count_records('user','mnethostid', $this->id
);
87 $actions = count_records('mnet_log','hostid', $this->id
);
92 $obj = delete_records('mnet_rpc2host', 'host_id', $this->id
);
94 $this->delete_all_sessions();
96 // If we don't have any activity records for which the mnet_host table
97 // provides a foreign key, then we can delete the record. Otherwise, we
98 // just mark it as deleted.
99 if (0 == $this->deleted
) {
100 delete_records('mnet_host', "id", $this->id
);
106 function count_live_sessions() {
107 $obj = $this->delete_expired_sessions();
108 return count_records('mnet_session','mnethostid', $this->id
);
111 function delete_expired_sessions() {
113 return delete_records_select('mnet_session', " mnethostid = '{$this->id}' AND expires < '$now' ");
116 function delete_all_sessions() {
118 // TODO: Expires each PHP session individually
119 // $sessions = get_records('mnet_session', 'mnethostid', $this->id);
120 $sessions = get_records('mnet_session', 'mnethostid', $this->id
);
122 if (count($sessions) > 0 && file_exists($CFG->dirroot
.'/auth/mnet/auth.php')) {
123 require_once($CFG->dirroot
.'/auth/mnet/auth.php');
124 $auth = new auth_plugin_mnet();
125 $auth->end_local_sessions($sessions);
128 $deletereturn = delete_records_select('mnet_session', " mnethostid = '{$this->id}'");
132 function check_common_name($key) {
133 $credentials = openssl_x509_parse($key);
134 if ($credentials == false) {
135 $this->error
[] = array('code' => 3, 'text' => get_string("nonmatchingcert", 'mnet', array('','')));
137 } elseif ($credentials['subject']['CN'] != $this->wwwroot
) {
138 $a[] = $credentials['subject']['CN'];
139 $a[] = $this->wwwroot
;
140 $this->error
[] = array('code' => 4, 'text' => get_string("nonmatchingcert", 'mnet', $a));
143 return $credentials['validTo_time_t'];
148 $obj = new stdClass();
150 $obj->wwwroot
= $this->wwwroot
;
151 $obj->ip_address
= $this->ip_address
;
152 $obj->name
= $this->name
;
153 $obj->public_key
= $this->public_key
;
154 $obj->public_key_expires
= $this->public_key_expires
;
155 $obj->deleted
= $this->deleted
;
156 $obj->last_connect_time
= $this->last_connect_time
;
157 $obj->last_log_id
= $this->last_log_id
;
159 if (isset($this->id
) && $this->id
> 0) {
160 $obj->id
= $this->id
;
161 return update_record('mnet_host', $obj);
163 $this->id
= insert_record('mnet_host', $obj);
164 return $this->id
> 0;
169 $this->last_connect_time
= time();
173 function set_name($newname) {
174 if (is_string($newname) && strlen($newname <= 80)) {
175 $this->name
= $newname;
181 function set_wwwroot($wwwroot) {
184 $hostinfo = get_record('mnet_host', 'wwwroot', $wwwroot);
186 if ($hostinfo != false) {
187 $this->populate($hostinfo);
193 function set_id($id) {
196 if (clean_param($id, PARAM_INT
) != $id) {
198 $this->errmsg
[] = 'Your id ('.$id.') is not legal';
206 {$CFG->prefix}mnet_host h
210 if ($hostinfo = get_record_sql($sql)) {
211 $this->populate($hostinfo);
218 * Several methods can be used to get an 'mnet_host' record. They all then
219 * send it to this private method to populate this object's attributes.
221 * @param object $hostinfo A database record from the mnet_host table
224 function populate($hostinfo) {
225 $this->id
= $hostinfo->id
;
226 $this->wwwroot
= $hostinfo->wwwroot
;
227 $this->ip_address
= $hostinfo->ip_address
;
228 $this->name
= $hostinfo->name
;
229 $this->deleted
= $hostinfo->deleted
;
230 $this->public_key
= $hostinfo->public_key
;
231 $this->public_key_expires
= $hostinfo->public_key_expires
;
232 $this->last_connect_time
= $hostinfo->last_connect_time
;
233 $this->last_log_id
= $hostinfo->last_log_id
;
236 function get_public_key() {
237 if (isset($this->public_key_ref
)) return $this->public_key_ref
;
238 $this->public_key_ref
= openssl_pkey_get_public($this->public_key
);
239 return $this->public_key_ref
;