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;
23 var $applicationid = 1; // Default of 1 == Moodle
24 var $keypair = array();
27 function mnet_peer() {
31 function bootstrap($wwwroot, $pubkey = null, $application) {
33 if (substr($wwwroot, -1, 1) == '/') {
34 $wwwroot = substr($wwwroot, 0, -1);
37 if ( ! $this->set_wwwroot($wwwroot) ) {
38 $hostname = mnet_get_hostname_from_uri($wwwroot);
40 // Get the IP address for that host - if this fails, it will
41 // return the hostname string
42 $ip_address = gethostbyname($hostname);
44 // Couldn't find the IP address?
45 if ($ip_address === $hostname && !preg_match('/^\d+\.\d+\.\d+.\d+$/',$hostname)) {
46 $this->error
[] = array('code' => 2, 'text' => get_string("noaddressforhost", 'mnet'));
50 $this->name
= $wwwroot;
52 // TODO: In reality, this will be prohibitively slow... need another
53 // default - maybe blank string
54 $homepage = file_get_contents($wwwroot);
55 if (!empty($homepage)) {
56 $count = preg_match("@<title>(.*)</title>@siU", $homepage, $matches);
58 $this->name
= $matches[1];
62 $this->wwwroot
= $wwwroot;
63 $this->ip_address
= $ip_address;
66 $this->application
= get_record('mnet_application', 'name', $application);
67 if (empty($this->application
)) {
68 $this->application
= get_record('mnet_application', 'name', 'moodle');
71 $this->applicationid
= $this->application
->id
;
74 $this->public_key
= clean_param(mnet_get_public_key($this->wwwroot
, $this->application
), PARAM_PEM
);
76 $this->public_key
= clean_param($pubkey, PARAM_PEM
);
78 $this->public_key_expires
= $this->check_common_name($this->public_key
);
79 $this->last_connect_time
= 0;
80 $this->last_log_id
= 0;
81 if ($this->public_key_expires
== false) {
82 $this->public_key
== '';
91 if ($this->deleted
) return true;
93 $users = count_records('user','mnethostid', $this->id
);
98 $actions = count_records('mnet_log','hostid', $this->id
);
103 $obj = delete_records('mnet_rpc2host', 'host_id', $this->id
);
105 $this->delete_all_sessions();
107 // If we don't have any activity records for which the mnet_host table
108 // provides a foreign key, then we can delete the record. Otherwise, we
109 // just mark it as deleted.
110 if (0 == $this->deleted
) {
111 delete_records('mnet_host', "id", $this->id
);
117 function count_live_sessions() {
118 $obj = $this->delete_expired_sessions();
119 return count_records('mnet_session','mnethostid', $this->id
);
122 function delete_expired_sessions() {
124 return delete_records_select('mnet_session', " mnethostid = '{$this->id}' AND expires < '$now' ");
127 function delete_all_sessions() {
129 // TODO: Expires each PHP session individually
130 // $sessions = get_records('mnet_session', 'mnethostid', $this->id);
131 $sessions = get_records('mnet_session', 'mnethostid', $this->id
);
133 if (count($sessions) > 0 && file_exists($CFG->dirroot
.'/auth/mnet/auth.php')) {
134 require_once($CFG->dirroot
.'/auth/mnet/auth.php');
135 $auth = new auth_plugin_mnet();
136 $auth->end_local_sessions($sessions);
139 $deletereturn = delete_records_select('mnet_session', " mnethostid = '{$this->id}'");
143 function check_common_name($key) {
144 $credentials = openssl_x509_parse($key);
145 if ($credentials == false) {
146 $this->error
[] = array('code' => 3, 'text' => get_string("nonmatchingcert", 'mnet', array('','')));
148 } elseif ($credentials['subject']['CN'] != $this->wwwroot
) {
149 $a[] = $credentials['subject']['CN'];
150 $a[] = $this->wwwroot
;
151 $this->error
[] = array('code' => 4, 'text' => get_string("nonmatchingcert", 'mnet', $a));
154 return $credentials['validTo_time_t'];
159 $obj = new stdClass();
161 $obj->wwwroot
= $this->wwwroot
;
162 $obj->ip_address
= $this->ip_address
;
163 $obj->name
= $this->name
;
164 $obj->public_key
= $this->public_key
;
165 $obj->public_key_expires
= $this->public_key_expires
;
166 $obj->deleted
= $this->deleted
;
167 $obj->last_connect_time
= $this->last_connect_time
;
168 $obj->last_log_id
= $this->last_log_id
;
169 $obj->force_theme
= $this->force_theme
;
170 $obj->theme
= $this->theme
;
171 $obj->applicationid
= $this->applicationid
;
173 if (isset($this->id
) && $this->id
> 0) {
174 $obj->id
= $this->id
;
175 return update_record('mnet_host', $obj);
177 $this->id
= insert_record('mnet_host', $obj);
178 return $this->id
> 0;
183 $this->last_connect_time
= time();
187 function set_name($newname) {
188 if (is_string($newname) && strlen($newname <= 80)) {
189 $this->name
= $newname;
195 function set_applicationid($applicationid) {
196 if (is_numeric($applicationid) && $applicationid == intval($applicationid)) {
197 $this->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
;