MDL-14679 converted some get/set_field()
[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) {
71558f85 32
b2d38907 33 if (substr($wwwroot, -1, 1) == '/') {
e931fedd 34 $wwwroot = substr($wwwroot, 0, -1);
35 }
36
71558f85 37 if ( ! $this->set_wwwroot($wwwroot) ) {
38 $hostname = mnet_get_hostname_from_uri($wwwroot);
39
40 // Get the IP address for that host - if this fails, it will
41 // return the hostname string
42 $ip_address = gethostbyname($hostname);
43
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'));
47 return false;
48 }
49
50 $this->name = $wwwroot;
51
52 // TODO: In reality, this will be prohibitively slow... need another
53 // default - maybe blank string
2e34d3f9 54 $homepage = download_file_content($wwwroot);
71558f85 55 if (!empty($homepage)) {
56 $count = preg_match("@<title>(.*)</title>@siU", $homepage, $matches);
57 if ($count > 0) {
58 $this->name = $matches[1];
59 }
60 }
61
71558f85 62 $this->wwwroot = $wwwroot;
63 $this->ip_address = $ip_address;
64 $this->deleted = 0;
25202581 65
66 $this->application = get_record('mnet_application', 'name', $application);
67 if (empty($this->application)) {
68 $this->application = get_record('mnet_application', 'name', 'moodle');
69 }
70
71 $this->applicationid = $this->application->id;
72
85d2d959 73 if(empty($pubkey)) {
25202581 74 $this->public_key = clean_param(mnet_get_public_key($this->wwwroot, $this->application), PARAM_PEM);
85d2d959 75 } else {
76 $this->public_key = clean_param($pubkey, PARAM_PEM);
77 }
71558f85 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 == '';
83 return false;
84 }
85 }
86
87 return true;
88 }
89
90 function delete() {
91 if ($this->deleted) return true;
92
93 $users = count_records('user','mnethostid', $this->id);
94 if ($users > 0) {
95 $this->deleted = 1;
96 }
97
98 $actions = count_records('mnet_log','hostid', $this->id);
99 if ($actions > 0) {
100 $this->deleted = 1;
101 }
102
103 $obj = delete_records('mnet_rpc2host', 'host_id', $this->id);
104
105 $this->delete_all_sessions();
106
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);
112 } else {
113 $this->commit();
114 }
115 }
116
117 function count_live_sessions() {
118 $obj = $this->delete_expired_sessions();
119 return count_records('mnet_session','mnethostid', $this->id);
120 }
121
122 function delete_expired_sessions() {
123 $now = time();
124 return delete_records_select('mnet_session', " mnethostid = '{$this->id}' AND expires < '$now' ");
125 }
126
127 function delete_all_sessions() {
128 global $CFG;
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);
132
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);
137 }
138
139 $deletereturn = delete_records_select('mnet_session', " mnethostid = '{$this->id}'");
140 return true;
141 }
142
143 function check_common_name($key) {
00d3c66b 144 $credentials = $this->check_credentials($key);
145 return $credentials['validTo_time_t'];
146 }
147
148 function check_credentials($key) {
71558f85 149 $credentials = openssl_x509_parse($key);
150 if ($credentials == false) {
151 $this->error[] = array('code' => 3, 'text' => get_string("nonmatchingcert", 'mnet', array('','')));
152 return false;
00d3c66b 153 } elseif (array_key_exists('subjectAltName', $credentials['subject']) && $credentials['subject']['subjectAltName'] != $this->wwwroot) {
154 $a[] = $credentials['subject']['subjectAltName'];
155 $a[] = $this->wwwroot;
156 $this->error[] = array('code' => 5, 'text' => get_string("nonmatchingcert", 'mnet', $a));
157 return false;
71558f85 158 } elseif ($credentials['subject']['CN'] != $this->wwwroot) {
159 $a[] = $credentials['subject']['CN'];
160 $a[] = $this->wwwroot;
161 $this->error[] = array('code' => 4, 'text' => get_string("nonmatchingcert", 'mnet', $a));
162 return false;
163 } else {
00d3c66b 164 if (array_key_exists('subjectAltName', $credentials['subject'])) {
165 $credentials['wwwroot'] = $credentials['subject']['subjectAltName'];
166 } else {
167 $credentials['wwwroot'] = $credentials['subject']['CN'];
168 }
169 return $credentials;
71558f85 170 }
171 }
172
173 function commit() {
174 $obj = new stdClass();
175
176 $obj->wwwroot = $this->wwwroot;
177 $obj->ip_address = $this->ip_address;
178 $obj->name = $this->name;
179 $obj->public_key = $this->public_key;
180 $obj->public_key_expires = $this->public_key_expires;
181 $obj->deleted = $this->deleted;
182 $obj->last_connect_time = $this->last_connect_time;
183 $obj->last_log_id = $this->last_log_id;
22882b66 184 $obj->force_theme = $this->force_theme;
185 $obj->theme = $this->theme;
25202581 186 $obj->applicationid = $this->applicationid;
71558f85 187
188 if (isset($this->id) && $this->id > 0) {
189 $obj->id = $this->id;
190 return update_record('mnet_host', $obj);
191 } else {
192 $this->id = insert_record('mnet_host', $obj);
193 return $this->id > 0;
194 }
195 }
196
3e008de8 197 function touch() {
198 $this->last_connect_time = time();
199 $this->commit();
200 }
201
71558f85 202 function set_name($newname) {
203 if (is_string($newname) && strlen($newname <= 80)) {
204 $this->name = $newname;
205 return true;
206 }
207 return false;
208 }
209
25202581 210 function set_applicationid($applicationid) {
211 if (is_numeric($applicationid) && $applicationid == intval($applicationid)) {
212 $this->applicationid = $applicationid;
213 return true;
214 }
215 return false;
216 }
217
71558f85 218 function set_wwwroot($wwwroot) {
219 global $CFG;
220
221 $hostinfo = get_record('mnet_host', 'wwwroot', $wwwroot);
222
223 if ($hostinfo != false) {
224 $this->populate($hostinfo);
225 return true;
226 }
227 return false;
228 }
229
230 function set_id($id) {
231 global $CFG;
232
233 if (clean_param($id, PARAM_INT) != $id) {
234 $this->errno[] = 1;
235 $this->errmsg[] = 'Your id ('.$id.') is not legal';
236 return false;
237 }
238
239 $sql = "
240 SELECT
241 h.*
242 FROM
243 {$CFG->prefix}mnet_host h
244 WHERE
245 h.id = '". $id ."'";
246
247 if ($hostinfo = get_record_sql($sql)) {
248 $this->populate($hostinfo);
249 return true;
250 }
251 return false;
252 }
253
735c7beb 254 /**
255 * Several methods can be used to get an 'mnet_host' record. They all then
256 * send it to this private method to populate this object's attributes.
257 *
258 * @param object $hostinfo A database record from the mnet_host table
259 * @return void
260 */
71558f85 261 function populate($hostinfo) {
262 $this->id = $hostinfo->id;
263 $this->wwwroot = $hostinfo->wwwroot;
264 $this->ip_address = $hostinfo->ip_address;
265 $this->name = $hostinfo->name;
266 $this->deleted = $hostinfo->deleted;
267 $this->public_key = $hostinfo->public_key;
268 $this->public_key_expires = $hostinfo->public_key_expires;
269 $this->last_connect_time = $hostinfo->last_connect_time;
270 $this->last_log_id = $hostinfo->last_log_id;
22882b66 271 $this->force_theme = $hostinfo->force_theme;
272 $this->theme = $hostinfo->theme;
25202581 273 $this->applicationid = $hostinfo->applicationid;
274 $this->application = get_record('mnet_application', 'id', $this->applicationid);
71558f85 275 }
276
277 function get_public_key() {
278 if (isset($this->public_key_ref)) return $this->public_key_ref;
279 $this->public_key_ref = openssl_pkey_get_public($this->public_key);
280 return $this->public_key_ref;
281 }
282}
283
284?>