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