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
) {
114 $this->delete_all_sessions();
117 $this->updateparams
->deleted
= 1;
121 function count_live_sessions() {
122 $obj = $this->delete_expired_sessions();
123 return count_records('mnet_session','mnethostid', $this->id
);
126 function delete_expired_sessions() {
128 return delete_records_select('mnet_session', " mnethostid = '{$this->id}' AND expires < '$now' ");
131 function delete_all_sessions() {
133 // TODO: Expires each PHP session individually
134 // $sessions = get_records('mnet_session', 'mnethostid', $this->id);
135 $sessions = get_records('mnet_session', 'mnethostid', $this->id
);
137 if (count($sessions) > 0 && file_exists($CFG->dirroot
.'/auth/mnet/auth.php')) {
138 require_once($CFG->dirroot
.'/auth/mnet/auth.php');
139 $auth = new auth_plugin_mnet();
140 $auth->end_local_sessions($sessions);
143 $deletereturn = delete_records_select('mnet_session', " mnethostid = '{$this->id}'");
147 function check_common_name($key) {
148 $credentials = openssl_x509_parse($key);
149 if ($credentials == false) {
150 $this->error
[] = array('code' => 3, 'text' => get_string("nonmatchingcert", 'mnet', array('','')));
152 } elseif ($credentials['subject']['CN'] != $this->wwwroot
) {
153 $a[] = $credentials['subject']['CN'];
154 $a[] = $this->wwwroot
;
155 $this->error
[] = array('code' => 4, 'text' => get_string("nonmatchingcert", 'mnet', $a));
158 return $credentials['validTo_time_t'];
163 $obj = $this->updateparams
;
165 if (isset($this->id
) && $this->id
> 0) {
166 $obj->id
= $this->id
;
167 $dbresult = update_record('mnet_host', $obj);
169 $this->id
= insert_record('mnet_host', $obj);
170 $dbresult = ($this->id
> 0);
172 //If the insert/update was successful, clear the parameters that need updating
174 $this->updateparams
= new stdClass();
180 $this->last_connect_time
= time();
181 $this->updateparams
->last_connect_time
= time();
185 function set_name($newname) {
186 if (is_string($newname) && strlen($newname <= 80)) {
187 $this->name
= stripslashes($newname);
188 $this->updateparams
->name
= $newname;
194 function set_applicationid($applicationid) {
195 if (is_numeric($applicationid) && $applicationid == intval($applicationid)) {
196 $this->applicationid
= $applicationid;
197 $this->updateparams
->applicationid
= $applicationid;
203 function set_wwwroot($wwwroot) {
206 $hostinfo = get_record('mnet_host', 'wwwroot', $wwwroot);
208 if ($hostinfo != false) {
209 $this->populate($hostinfo);
215 function set_id($id) {
218 if (clean_param($id, PARAM_INT
) != $id) {
220 $this->errmsg
[] = 'Your id ('.$id.') is not legal';
228 {$CFG->prefix}mnet_host h
232 if ($hostinfo = get_record_sql($sql)) {
233 $this->populate($hostinfo);
240 * Several methods can be used to get an 'mnet_host' record. They all then
241 * send it to this private method to populate this object's attributes.
243 * @param object $hostinfo A database record from the mnet_host table
246 function populate($hostinfo) {
247 $this->id
= $hostinfo->id
;
248 $this->wwwroot
= $hostinfo->wwwroot
;
249 $this->ip_address
= $hostinfo->ip_address
;
250 $this->name
= $hostinfo->name
;
251 $this->deleted
= $hostinfo->deleted
;
252 $this->public_key
= $hostinfo->public_key
;
253 $this->public_key_expires
= $hostinfo->public_key_expires
;
254 $this->last_connect_time
= $hostinfo->last_connect_time
;
255 $this->last_log_id
= $hostinfo->last_log_id
;
256 $this->force_theme
= $hostinfo->force_theme
;
257 $this->theme
= $hostinfo->theme
;
258 $this->applicationid
= $hostinfo->applicationid
;
259 $this->application
= get_record('mnet_application', 'id', $this->applicationid
);
262 function get_public_key() {
263 if (isset($this->public_key_ref
)) return $this->public_key_ref
;
264 $this->public_key_ref
= openssl_pkey_get_public($this->public_key
);
265 return $this->public_key_ref
;