Merge branch 'wip-MDL-40700-m24' of git://github.com/samhemelryk/moodle into MOODLE_2...
[moodle.git] / mnet / peer.php
blob5d8936b04cb1cdb83b9b2a070d5a84d2adfbb6e6
1 <?php
2 /**
3 * An object to represent lots of information about an RPC-peer machine
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 */
11 require_once($CFG->libdir . '/filelib.php'); // download_file_content() used here
13 class mnet_peer {
15 var $id = 0;
16 var $wwwroot = '';
17 var $ip_address = '';
18 var $name = '';
19 var $public_key = '';
20 var $public_key_expires = 0;
21 var $last_connect_time = 0;
22 var $last_log_id = 0;
23 var $force_theme = 0;
24 var $theme = '';
25 var $applicationid = 1; // Default of 1 == Moodle
26 var $keypair = array();
27 var $error = array();
28 var $bootstrapped = false; // set when the object is populated
30 function mnet_peer() {
31 return true;
35 * Fetch information about a peer identified by wwwroot
36 * If information does not preexist in db, collect it together based on
37 * supplied information
39 * @param string $wwwroot - address of peer whose details we want
40 * @param string $pubkey - to use if we add a record to db for new peer
41 * @param int $application - table id - what kind of peer are we talking to
42 * @return bool - indication of success or failure
44 function bootstrap($wwwroot, $pubkey = null, $application) {
45 global $DB;
47 if (substr($wwwroot, -1, 1) == '/') {
48 $wwwroot = substr($wwwroot, 0, -1);
51 // If a peer record already exists for this address,
52 // load that info and return
53 if ($this->set_wwwroot($wwwroot)) {
54 return true;
57 $hostname = mnet_get_hostname_from_uri($wwwroot);
58 // Get the IP address for that host - if this fails, it will return the hostname string
59 $ip_address = gethostbyname($hostname);
61 // Couldn't find the IP address?
62 if ($ip_address === $hostname && !preg_match('/^\d+\.\d+\.\d+.\d+$/',$hostname)) {
63 throw new moodle_exception('noaddressforhost', 'mnet', '', $hostname);
66 $this->name = $wwwroot;
68 // TODO: In reality, this will be prohibitively slow... need another
69 // default - maybe blank string
70 $homepage = download_file_content($wwwroot);
71 if (!empty($homepage)) {
72 $count = preg_match("@<title>(.*)</title>@siU", $homepage, $matches);
73 if ($count > 0) {
74 $this->name = $matches[1];
78 $this->wwwroot = $wwwroot;
79 $this->ip_address = $ip_address;
80 $this->deleted = 0;
82 $this->application = $DB->get_record('mnet_application', array('name'=>$application));
83 if (empty($this->application)) {
84 $this->application = $DB->get_record('mnet_application', array('name'=>'moodle'));
87 $this->applicationid = $this->application->id;
89 if(empty($pubkey)) {
90 $this->public_key = clean_param(mnet_get_public_key($this->wwwroot, $this->application), PARAM_PEM);
91 } else {
92 $this->public_key = clean_param($pubkey, PARAM_PEM);
94 $this->public_key_expires = $this->check_common_name($this->public_key);
95 $this->last_connect_time = 0;
96 $this->last_log_id = 0;
97 if ($this->public_key_expires == false) {
98 $this->public_key == '';
99 return false;
101 $this->bootstrapped = true;
105 * Delete mnet peer
106 * the peer is marked as deleted in the database
107 * we delete current sessions.
108 * @return bool - success
110 function delete() {
111 global $DB;
113 if ($this->deleted) {
114 return true;
117 $this->delete_all_sessions();
119 $this->deleted = 1;
120 return $this->commit();
123 function count_live_sessions() {
124 global $DB;
125 $obj = $this->delete_expired_sessions();
126 return $DB->count_records('mnet_session', array('mnethostid'=>$this->id));
129 function delete_expired_sessions() {
130 global $DB;
131 $now = time();
132 return $DB->delete_records_select('mnet_session', " mnethostid = ? AND expires < ? ", array($this->id, $now));
135 function delete_all_sessions() {
136 global $CFG, $DB;
137 // TODO: Expires each PHP session individually
138 $sessions = $DB->get_records('mnet_session', array('mnethostid'=>$this->id));
140 if (count($sessions) > 0 && file_exists($CFG->dirroot.'/auth/mnet/auth.php')) {
141 require_once($CFG->dirroot.'/auth/mnet/auth.php');
142 $auth = new auth_plugin_mnet();
143 $auth->end_local_sessions($sessions);
146 $deletereturn = $DB->delete_records('mnet_session', array('mnethostid'=>$this->id));
147 return true;
150 function check_common_name($key) {
151 $credentials = $this->check_credentials($key);
152 return $credentials['validTo_time_t'];
155 function check_credentials($key) {
156 $credentials = openssl_x509_parse($key);
157 if ($credentials == false) {
158 $this->error[] = array('code' => 3, 'text' => get_string("nonmatchingcert", 'mnet', array('subject' => '','host' => '')));
159 return false;
160 } elseif (array_key_exists('subjectAltName', $credentials['subject']) && $credentials['subject']['subjectAltName'] != $this->wwwroot) {
161 $a['subject'] = $credentials['subject']['subjectAltName'];
162 $a['host'] = $this->wwwroot;
163 $this->error[] = array('code' => 5, 'text' => get_string("nonmatchingcert", 'mnet', $a));
164 return false;
165 } elseif ($credentials['subject']['CN'] != $this->wwwroot) {
166 $a['subject'] = $credentials['subject']['CN'];
167 $a['host'] = $this->wwwroot;
168 $this->error[] = array('code' => 4, 'text' => get_string("nonmatchingcert", 'mnet', $a));
169 return false;
170 } else {
171 if (array_key_exists('subjectAltName', $credentials['subject'])) {
172 $credentials['wwwroot'] = $credentials['subject']['subjectAltName'];
173 } else {
174 $credentials['wwwroot'] = $credentials['subject']['CN'];
176 return $credentials;
180 function commit() {
181 global $DB;
182 $obj = new stdClass();
184 $obj->wwwroot = $this->wwwroot;
185 $obj->ip_address = $this->ip_address;
186 $obj->name = $this->name;
187 $obj->public_key = $this->public_key;
188 $obj->public_key_expires = $this->public_key_expires;
189 $obj->deleted = $this->deleted;
190 $obj->last_connect_time = $this->last_connect_time;
191 $obj->last_log_id = $this->last_log_id;
192 $obj->force_theme = $this->force_theme;
193 $obj->theme = $this->theme;
194 $obj->applicationid = $this->applicationid;
196 if (isset($this->id) && $this->id > 0) {
197 $obj->id = $this->id;
198 return $DB->update_record('mnet_host', $obj);
199 } else {
200 $this->id = $DB->insert_record('mnet_host', $obj);
201 return $this->id > 0;
205 function touch() {
206 $this->last_connect_time = time();
207 $this->commit();
210 function set_name($newname) {
211 if (is_string($newname) && strlen($newname <= 80)) {
212 $this->name = $newname;
213 return true;
215 return false;
218 function set_applicationid($applicationid) {
219 if (is_numeric($applicationid) && $applicationid == intval($applicationid)) {
220 $this->applicationid = $applicationid;
221 return true;
223 return false;
227 * Load information from db about an mnet peer into this object's properties
229 * @param string $wwwroot - address of peer whose details we want to load
230 * @return bool - indication of success or failure
232 function set_wwwroot($wwwroot) {
233 global $CFG, $DB;
235 $hostinfo = $DB->get_record('mnet_host', array('wwwroot'=>$wwwroot));
237 if ($hostinfo != false) {
238 $this->populate($hostinfo);
239 return true;
241 return false;
244 function set_id($id) {
245 global $CFG, $DB;
247 if (clean_param($id, PARAM_INT) != $id) {
248 $this->errno[] = 1;
249 $this->errmsg[] = 'Your id ('.$id.') is not legal';
250 return false;
253 $sql = "
254 SELECT
256 FROM
257 {mnet_host} h
258 WHERE
259 h.id = ?";
261 if ($hostinfo = $DB->get_record_sql($sql, array($id))) {
262 $this->populate($hostinfo);
263 return true;
265 return false;
269 * Several methods can be used to get an 'mnet_host' record. They all then
270 * send it to this private method to populate this object's attributes.
272 * @param object $hostinfo A database record from the mnet_host table
273 * @return void
275 function populate($hostinfo) {
276 global $DB;
277 $this->id = $hostinfo->id;
278 $this->wwwroot = $hostinfo->wwwroot;
279 $this->ip_address = $hostinfo->ip_address;
280 $this->name = $hostinfo->name;
281 $this->deleted = $hostinfo->deleted;
282 $this->public_key = $hostinfo->public_key;
283 $this->public_key_expires = $hostinfo->public_key_expires;
284 $this->last_connect_time = $hostinfo->last_connect_time;
285 $this->last_log_id = $hostinfo->last_log_id;
286 $this->force_theme = $hostinfo->force_theme;
287 $this->theme = $hostinfo->theme;
288 $this->applicationid = $hostinfo->applicationid;
289 $this->application = $DB->get_record('mnet_application', array('id'=>$this->applicationid));
290 $this->bootstrapped = true;
293 function get_public_key() {
294 if (isset($this->public_key_ref)) return $this->public_key_ref;
295 $this->public_key_ref = openssl_pkey_get_public($this->public_key);
296 return $this->public_key_ref;