MDL-15267 PHP5 strict object warning, whitespace fixes
[moodle.git] / mnet / peer.php
CommitLineData
b16393cb 1<?php // $Id$
71558f85 2/**
3 * An object to represent lots of information about an RPC-peer machine
4 *
5 * @author Donal McMullan donal@catalyst.net.nz
6 * @version 0.0.1
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8 * @package mnet
9 */
10
11class mnet_peer {
12
13 var $id = 0;
14 var $wwwroot = '';
15 var $ip_address = '';
16 var $name = '';
17 var $public_key = '';
18 var $public_key_expires = 0;
19 var $last_connect_time = 0;
20 var $last_log_id = 0;
22882b66 21 var $force_theme = 0;
22 var $theme = '';
25202581 23 var $applicationid = 1; // Default of 1 == Moodle
71558f85 24 var $keypair = array();
25 var $error = array();
26
27 function mnet_peer() {
28 return true;
29 }
30
25202581 31 function bootstrap($wwwroot, $pubkey = null, $application) {
cc38ff5d 32 global $DB;
71558f85 33
b2d38907 34 if (substr($wwwroot, -1, 1) == '/') {
e931fedd 35 $wwwroot = substr($wwwroot, 0, -1);
36 }
37
71558f85 38 if ( ! $this->set_wwwroot($wwwroot) ) {
39 $hostname = mnet_get_hostname_from_uri($wwwroot);
40
41 // Get the IP address for that host - if this fails, it will
42 // return the hostname string
43 $ip_address = gethostbyname($hostname);
44
45 // Couldn't find the IP address?
46 if ($ip_address === $hostname && !preg_match('/^\d+\.\d+\.\d+.\d+$/',$hostname)) {
47 $this->error[] = array('code' => 2, 'text' => get_string("noaddressforhost", 'mnet'));
48 return false;
49 }
50
51 $this->name = $wwwroot;
52
53 // TODO: In reality, this will be prohibitively slow... need another
54 // default - maybe blank string
2e34d3f9 55 $homepage = download_file_content($wwwroot);
71558f85 56 if (!empty($homepage)) {
57 $count = preg_match("@<title>(.*)</title>@siU", $homepage, $matches);
58 if ($count > 0) {
59 $this->name = $matches[1];
60 }
61 }
62
71558f85 63 $this->wwwroot = $wwwroot;
64 $this->ip_address = $ip_address;
65 $this->deleted = 0;
25202581 66
cc38ff5d 67 $this->application = $DB->get_record('mnet_application', array('name'=>$application));
25202581 68 if (empty($this->application)) {
cc38ff5d 69 $this->application = $DB->get_record('mnet_application', array('name'=>'moodle'));
25202581 70 }
71
72 $this->applicationid = $this->application->id;
73
85d2d959 74 if(empty($pubkey)) {
25202581 75 $this->public_key = clean_param(mnet_get_public_key($this->wwwroot, $this->application), PARAM_PEM);
85d2d959 76 } else {
77 $this->public_key = clean_param($pubkey, PARAM_PEM);
78 }
71558f85 79 $this->public_key_expires = $this->check_common_name($this->public_key);
80 $this->last_connect_time = 0;
81 $this->last_log_id = 0;
82 if ($this->public_key_expires == false) {
83 $this->public_key == '';
84 return false;
85 }
86 }
87
88 return true;
89 }
90
91 function delete() {
c7da4357 92 global $DB;
93
71558f85 94 if ($this->deleted) return true;
95
c7da4357 96 $users = $DB->count_records('user', array('mnethostid'=>$this->id));
71558f85 97 if ($users > 0) {
98 $this->deleted = 1;
99 }
100
c7da4357 101 $actions = $DB->count_records('mnet_log', array('hostid'=>$this->id));
71558f85 102 if ($actions > 0) {
103 $this->deleted = 1;
104 }
105
c7da4357 106 $obj = $DB->delete_records('mnet_rpc2host', array('host_id'=>$this->id));
71558f85 107
108 $this->delete_all_sessions();
109
110 // If we don't have any activity records for which the mnet_host table
111 // provides a foreign key, then we can delete the record. Otherwise, we
112 // just mark it as deleted.
113 if (0 == $this->deleted) {
c7da4357 114 $DB->delete_records('mnet_host', array("id"=>$this->id));
71558f85 115 } else {
116 $this->commit();
117 }
118 }
119
120 function count_live_sessions() {
c7da4357 121 global $DB;
71558f85 122 $obj = $this->delete_expired_sessions();
c7da4357 123 return $DB->count_records('mnet_session', array('mnethostid'=>$this->id));
71558f85 124 }
125
126 function delete_expired_sessions() {
c7da4357 127 global $DB;
71558f85 128 $now = time();
c7da4357 129 return $DB->delete_records_select('mnet_session', " mnethostid = ? AND expires < ? ", array($this->id, $now));
71558f85 130 }
131
132 function delete_all_sessions() {
c7da4357 133 global $CFG, $DB;
71558f85 134 // TODO: Expires each PHP session individually
c7da4357 135 $sessions = $DB->get_records('mnet_session', array('mnethostid'=>$this->id));
71558f85 136
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);
141 }
142
c7da4357 143 $deletereturn = $DB->delete_records('mnet_session', array('mnethostid'=>$this->id));
71558f85 144 return true;
145 }
146
147 function check_common_name($key) {
00d3c66b 148 $credentials = $this->check_credentials($key);
149 return $credentials['validTo_time_t'];
150 }
151
152 function check_credentials($key) {
71558f85 153 $credentials = openssl_x509_parse($key);
154 if ($credentials == false) {
155 $this->error[] = array('code' => 3, 'text' => get_string("nonmatchingcert", 'mnet', array('','')));
156 return false;
00d3c66b 157 } elseif (array_key_exists('subjectAltName', $credentials['subject']) && $credentials['subject']['subjectAltName'] != $this->wwwroot) {
158 $a[] = $credentials['subject']['subjectAltName'];
159 $a[] = $this->wwwroot;
160 $this->error[] = array('code' => 5, 'text' => get_string("nonmatchingcert", 'mnet', $a));
161 return false;
71558f85 162 } elseif ($credentials['subject']['CN'] != $this->wwwroot) {
163 $a[] = $credentials['subject']['CN'];
164 $a[] = $this->wwwroot;
165 $this->error[] = array('code' => 4, 'text' => get_string("nonmatchingcert", 'mnet', $a));
166 return false;
167 } else {
00d3c66b 168 if (array_key_exists('subjectAltName', $credentials['subject'])) {
169 $credentials['wwwroot'] = $credentials['subject']['subjectAltName'];
170 } else {
171 $credentials['wwwroot'] = $credentials['subject']['CN'];
172 }
173 return $credentials;
71558f85 174 }
175 }
176
177 function commit() {
c7da4357 178 global $DB;
71558f85 179 $obj = new stdClass();
180
181 $obj->wwwroot = $this->wwwroot;
182 $obj->ip_address = $this->ip_address;
183 $obj->name = $this->name;
184 $obj->public_key = $this->public_key;
185 $obj->public_key_expires = $this->public_key_expires;
186 $obj->deleted = $this->deleted;
187 $obj->last_connect_time = $this->last_connect_time;
188 $obj->last_log_id = $this->last_log_id;
22882b66 189 $obj->force_theme = $this->force_theme;
190 $obj->theme = $this->theme;
25202581 191 $obj->applicationid = $this->applicationid;
71558f85 192
193 if (isset($this->id) && $this->id > 0) {
194 $obj->id = $this->id;
c7da4357 195 return $DB->update_record('mnet_host', $obj);
71558f85 196 } else {
c7da4357 197 $this->id = $DB->insert_record('mnet_host', $obj);
71558f85 198 return $this->id > 0;
199 }
200 }
201
3e008de8 202 function touch() {
203 $this->last_connect_time = time();
204 $this->commit();
205 }
206
71558f85 207 function set_name($newname) {
208 if (is_string($newname) && strlen($newname <= 80)) {
209 $this->name = $newname;
210 return true;
211 }
212 return false;
213 }
214
25202581 215 function set_applicationid($applicationid) {
216 if (is_numeric($applicationid) && $applicationid == intval($applicationid)) {
217 $this->applicationid = $applicationid;
218 return true;
219 }
220 return false;
221 }
222
71558f85 223 function set_wwwroot($wwwroot) {
c7da4357 224 global $CFG, $DB;
71558f85 225
c7da4357 226 $hostinfo = $DB->get_record('mnet_host', array('wwwroot'=>$wwwroot));
71558f85 227
228 if ($hostinfo != false) {
229 $this->populate($hostinfo);
230 return true;
231 }
232 return false;
233 }
234
235 function set_id($id) {
c7da4357 236 global $CFG, $DB;
71558f85 237
238 if (clean_param($id, PARAM_INT) != $id) {
239 $this->errno[] = 1;
240 $this->errmsg[] = 'Your id ('.$id.') is not legal';
241 return false;
242 }
243
244 $sql = "
245 SELECT
246 h.*
247 FROM
c7da4357 248 {mnet_host} h
71558f85 249 WHERE
c7da4357 250 h.id = ?";
71558f85 251
c7da4357 252 if ($hostinfo = $DB->get_record_sql($sql, array($id))) {
71558f85 253 $this->populate($hostinfo);
254 return true;
255 }
256 return false;
257 }
258
735c7beb 259 /**
260 * Several methods can be used to get an 'mnet_host' record. They all then
261 * send it to this private method to populate this object's attributes.
262 *
263 * @param object $hostinfo A database record from the mnet_host table
264 * @return void
265 */
71558f85 266 function populate($hostinfo) {
c7da4357 267 global $DB;
71558f85 268 $this->id = $hostinfo->id;
269 $this->wwwroot = $hostinfo->wwwroot;
270 $this->ip_address = $hostinfo->ip_address;
271 $this->name = $hostinfo->name;
272 $this->deleted = $hostinfo->deleted;
273 $this->public_key = $hostinfo->public_key;
274 $this->public_key_expires = $hostinfo->public_key_expires;
275 $this->last_connect_time = $hostinfo->last_connect_time;
276 $this->last_log_id = $hostinfo->last_log_id;
22882b66 277 $this->force_theme = $hostinfo->force_theme;
278 $this->theme = $hostinfo->theme;
25202581 279 $this->applicationid = $hostinfo->applicationid;
c7da4357 280 $this->application = $DB->get_record('mnet_application', array('id'=>$this->applicationid));
71558f85 281 }
282
283 function get_public_key() {
284 if (isset($this->public_key_ref)) return $this->public_key_ref;
285 $this->public_key_ref = openssl_pkey_get_public($this->public_key);
286 return $this->public_key_ref;
287 }
288}
289
290?>