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
13 //Unless stated otherwise, properties of this object are unescaped, and unsafe to
14 //insert into the db without further processing.
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 //Object whose properties need to be put into the database:
29 //(properties here are slashescaped)
32 function mnet_peer() {
33 $this->updateparams
= new stdClass();
37 function bootstrap($wwwroot, $pubkey = null, $application) {
39 if (substr($wwwroot, -1, 1) == '/') {
40 $wwwroot = substr($wwwroot, 0, -1);
43 if ( ! $this->set_wwwroot($wwwroot) ) {
44 $hostname = mnet_get_hostname_from_uri($wwwroot);
46 // Get the IP address for that host - if this fails, it will
47 // return the hostname string
48 $ip_address = gethostbyname($hostname);
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'));
56 $this->name
= stripslashes($wwwroot);
57 $this->updateparams
->name
= $wwwroot;
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);
65 $this->name
= $matches[1];
66 $this->updateparams
->name
= addslashes($matches[1]);
70 $this->wwwroot
= stripslashes($wwwroot);
71 $this->updateparams
->wwwroot
= $wwwroot;
72 $this->ip_address
= $ip_address;
73 $this->updateparams
->ip_address
= $ip_address;
75 $this->updateparams
->deleted
= 0;
77 $this->application
= get_record('mnet_application', 'name', $application);
78 if (empty($this->application
)) {
79 $this->application
= get_record('mnet_application', 'name', 'moodle');
82 $this->applicationid
= $this->application
->id
;
83 $this->updateparams
->applicationid
= $this->application
->id
;
86 $pubkeytemp = clean_param(mnet_get_public_key($this->wwwroot
, $this->application
), PARAM_PEM
);
88 $pubkeytemp = clean_param($pubkey, PARAM_PEM
);
90 $this->public_key_expires
= $this->check_common_name($pubkeytemp);
92 if ($this->public_key_expires
== false) {
95 $this->updateparams
->public_key_expires
= $this->public_key_expires
;
97 $this->updateparams
->public_key
= $pubkeytemp;
98 $this->public_key
= $pubkeytemp;
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;
110 if ($this->deleted
) return true;
112 $users = count_records('user','mnethostid', $this->id
);
115 $this->updateparams
->deleted
= 1;
118 $actions = count_records('mnet_log','hostid', $this->id
);
121 $this->updateparams
->deleted
= 1;
124 $obj = delete_records('mnet_rpc2host', 'host_id', $this->id
);
126 $this->delete_all_sessions();
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
);
138 function count_live_sessions() {
139 $obj = $this->delete_expired_sessions();
140 return count_records('mnet_session','mnethostid', $this->id
);
143 function delete_expired_sessions() {
145 return delete_records_select('mnet_session', " mnethostid = '{$this->id}' AND expires < '$now' ");
148 function delete_all_sessions() {
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
);
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);
160 $deletereturn = delete_records_select('mnet_session', " mnethostid = '{$this->id}'");
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('','')));
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));
175 return $credentials['validTo_time_t'];
180 $obj = $this->updateparams
;
182 if (isset($this->id
) && $this->id
> 0) {
183 $obj->id
= $this->id
;
184 $dbresult = update_record('mnet_host', $obj);
186 $this->id
= insert_record('mnet_host', $obj);
187 $dbresult = ($this->id
> 0);
189 //If the insert/update was successful, clear the parameters that need updating
191 $this->updateparams
= new stdClass();
197 $this->last_connect_time
= time();
198 $this->updateparams
->last_connect_time
= time();
202 function set_name($newname) {
203 if (is_string($newname) && strlen($newname <= 80)) {
204 $this->name
= stripslashes($newname);
205 $this->updateparams
->name
= $newname;
211 function set_applicationid($applicationid) {
212 if (is_numeric($applicationid) && $applicationid == intval($applicationid)) {
213 $this->applicationid
= $applicationid;
214 $this->updateparams
->applicationid
= $applicationid;
220 function set_wwwroot($wwwroot) {
223 $hostinfo = get_record('mnet_host', 'wwwroot', $wwwroot);
225 if ($hostinfo != false) {
226 $this->populate($hostinfo);
232 function set_id($id) {
235 if (clean_param($id, PARAM_INT
) != $id) {
237 $this->errmsg
[] = 'Your id ('.$id.') is not legal';
245 {$CFG->prefix}mnet_host h
249 if ($hostinfo = get_record_sql($sql)) {
250 $this->populate($hostinfo);
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.
260 * @param object $hostinfo A database record from the mnet_host table
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
);
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
;