MDL-51461 core: Fix issue with client side mustache templates
[moodle.git] / mnet / peer.php
blob9f72e179853c4e3ea1da17170c763646ab5a3d4d
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 /** No SSL verification. */
16 const SSL_NONE = 0;
18 /** SSL verification for host. */
19 const SSL_HOST = 1;
21 /** SSL verification for host and peer. */
22 const SSL_HOST_AND_PEER = 2;
24 var $id = 0;
25 var $wwwroot = '';
26 var $ip_address = '';
27 var $name = '';
28 var $public_key = '';
29 var $public_key_expires = 0;
30 var $last_connect_time = 0;
31 var $last_log_id = 0;
32 var $force_theme = 0;
33 var $theme = '';
34 var $applicationid = 1; // Default of 1 == Moodle
35 var $keypair = array();
36 var $error = array();
37 var $bootstrapped = false; // set when the object is populated
39 /** @var int $sslverification The level of SSL verification to apply. */
40 public $sslverification = self::SSL_HOST_AND_PEER;
42 function mnet_peer() {
43 return true;
47 * Fetch information about a peer identified by wwwroot
48 * If information does not preexist in db, collect it together based on
49 * supplied information
51 * @param string $wwwroot - address of peer whose details we want
52 * @param string $pubkey - to use if we add a record to db for new peer
53 * @param int $application - table id - what kind of peer are we talking to
54 * @return bool - indication of success or failure
56 function bootstrap($wwwroot, $pubkey = null, $application) {
57 global $DB;
59 if (substr($wwwroot, -1, 1) == '/') {
60 $wwwroot = substr($wwwroot, 0, -1);
63 // If a peer record already exists for this address,
64 // load that info and return
65 if ($this->set_wwwroot($wwwroot)) {
66 return true;
69 $hostname = mnet_get_hostname_from_uri($wwwroot);
70 // Get the IP address for that host - if this fails, it will return the hostname string
71 $ip_address = gethostbyname($hostname);
73 // Couldn't find the IP address?
74 if ($ip_address === $hostname && !preg_match('/^\d+\.\d+\.\d+.\d+$/',$hostname)) {
75 throw new moodle_exception('noaddressforhost', 'mnet', '', $hostname);
78 $this->name = $wwwroot;
80 // TODO: In reality, this will be prohibitively slow... need another
81 // default - maybe blank string
82 $homepage = download_file_content($wwwroot);
83 if (!empty($homepage)) {
84 $count = preg_match("@<title>(.*)</title>@siU", $homepage, $matches);
85 if ($count > 0) {
86 $this->name = $matches[1];
90 $this->wwwroot = $wwwroot;
91 $this->ip_address = $ip_address;
92 $this->deleted = 0;
94 $this->application = $DB->get_record('mnet_application', array('name'=>$application));
95 if (empty($this->application)) {
96 $this->application = $DB->get_record('mnet_application', array('name'=>'moodle'));
99 $this->applicationid = $this->application->id;
101 if(empty($pubkey)) {
102 $this->public_key = clean_param(mnet_get_public_key($this->wwwroot, $this->application), PARAM_PEM);
103 } else {
104 $this->public_key = clean_param($pubkey, PARAM_PEM);
106 $this->public_key_expires = $this->check_common_name($this->public_key);
107 $this->last_connect_time = 0;
108 $this->last_log_id = 0;
109 if ($this->public_key_expires == false) {
110 $this->public_key == '';
111 return false;
113 $this->bootstrapped = true;
117 * Delete mnet peer
118 * the peer is marked as deleted in the database
119 * we delete current sessions.
120 * @return bool - success
122 function delete() {
123 global $DB;
125 if ($this->deleted) {
126 return true;
129 $this->delete_all_sessions();
131 $this->deleted = 1;
132 return $this->commit();
135 function count_live_sessions() {
136 global $DB;
137 $obj = $this->delete_expired_sessions();
138 return $DB->count_records('mnet_session', array('mnethostid'=>$this->id));
141 function delete_expired_sessions() {
142 global $DB;
143 $now = time();
144 return $DB->delete_records_select('mnet_session', " mnethostid = ? AND expires < ? ", array($this->id, $now));
147 function delete_all_sessions() {
148 global $CFG, $DB;
149 // TODO: Expires each PHP session individually
150 $sessions = $DB->get_records('mnet_session', array('mnethostid'=>$this->id));
152 if (count($sessions) > 0 && file_exists($CFG->dirroot.'/auth/mnet/auth.php')) {
153 require_once($CFG->dirroot.'/auth/mnet/auth.php');
154 $auth = new auth_plugin_mnet();
155 $auth->end_local_sessions($sessions);
158 $deletereturn = $DB->delete_records('mnet_session', array('mnethostid'=>$this->id));
159 return true;
162 function check_common_name($key) {
163 $credentials = $this->check_credentials($key);
164 return $credentials['validTo_time_t'];
167 function check_credentials($key) {
168 $credentials = openssl_x509_parse($key);
169 if ($credentials == false) {
170 $this->error[] = array('code' => 3, 'text' => get_string("nonmatchingcert", 'mnet', array('subject' => '','host' => '')));
171 return false;
172 } elseif (array_key_exists('subjectAltName', $credentials['subject']) && $credentials['subject']['subjectAltName'] != $this->wwwroot) {
173 $a['subject'] = $credentials['subject']['subjectAltName'];
174 $a['host'] = $this->wwwroot;
175 $this->error[] = array('code' => 5, 'text' => get_string("nonmatchingcert", 'mnet', $a));
176 return false;
177 } elseif ($credentials['subject']['CN'] != $this->wwwroot) {
178 $a['subject'] = $credentials['subject']['CN'];
179 $a['host'] = $this->wwwroot;
180 $this->error[] = array('code' => 4, 'text' => get_string("nonmatchingcert", 'mnet', $a));
181 return false;
182 } else {
183 if (array_key_exists('subjectAltName', $credentials['subject'])) {
184 $credentials['wwwroot'] = $credentials['subject']['subjectAltName'];
185 } else {
186 $credentials['wwwroot'] = $credentials['subject']['CN'];
188 return $credentials;
192 function commit() {
193 global $DB;
194 $obj = new stdClass();
196 $obj->wwwroot = $this->wwwroot;
197 $obj->ip_address = $this->ip_address;
198 $obj->name = $this->name;
199 $obj->public_key = $this->public_key;
200 $obj->public_key_expires = $this->public_key_expires;
201 $obj->deleted = $this->deleted;
202 $obj->last_connect_time = $this->last_connect_time;
203 $obj->last_log_id = $this->last_log_id;
204 $obj->force_theme = $this->force_theme;
205 $obj->theme = $this->theme;
206 $obj->applicationid = $this->applicationid;
207 $obj->sslverification = $this->sslverification;
209 if (isset($this->id) && $this->id > 0) {
210 $obj->id = $this->id;
211 return $DB->update_record('mnet_host', $obj);
212 } else {
213 $this->id = $DB->insert_record('mnet_host', $obj);
214 return $this->id > 0;
218 function touch() {
219 $this->last_connect_time = time();
220 $this->commit();
223 function set_name($newname) {
224 if (is_string($newname) && strlen($newname <= 80)) {
225 $this->name = $newname;
226 return true;
228 return false;
231 function set_applicationid($applicationid) {
232 if (is_numeric($applicationid) && $applicationid == intval($applicationid)) {
233 $this->applicationid = $applicationid;
234 return true;
236 return false;
240 * Load information from db about an mnet peer into this object's properties
242 * @param string $wwwroot - address of peer whose details we want to load
243 * @return bool - indication of success or failure
245 function set_wwwroot($wwwroot) {
246 global $CFG, $DB;
248 $hostinfo = $DB->get_record('mnet_host', array('wwwroot'=>$wwwroot));
250 if ($hostinfo != false) {
251 $this->populate($hostinfo);
252 return true;
254 return false;
257 function set_id($id) {
258 global $CFG, $DB;
260 if (clean_param($id, PARAM_INT) != $id) {
261 $this->errno[] = 1;
262 $this->errmsg[] = 'Your id ('.$id.') is not legal';
263 return false;
266 $sql = "
267 SELECT
269 FROM
270 {mnet_host} h
271 WHERE
272 h.id = ?";
274 if ($hostinfo = $DB->get_record_sql($sql, array($id))) {
275 $this->populate($hostinfo);
276 return true;
278 return false;
282 * Several methods can be used to get an 'mnet_host' record. They all then
283 * send it to this private method to populate this object's attributes.
285 * @param object $hostinfo A database record from the mnet_host table
286 * @return void
288 function populate($hostinfo) {
289 global $DB;
290 $this->id = $hostinfo->id;
291 $this->wwwroot = $hostinfo->wwwroot;
292 $this->ip_address = $hostinfo->ip_address;
293 $this->name = $hostinfo->name;
294 $this->deleted = $hostinfo->deleted;
295 $this->public_key = $hostinfo->public_key;
296 $this->public_key_expires = $hostinfo->public_key_expires;
297 $this->last_connect_time = $hostinfo->last_connect_time;
298 $this->last_log_id = $hostinfo->last_log_id;
299 $this->force_theme = $hostinfo->force_theme;
300 $this->theme = $hostinfo->theme;
301 $this->applicationid = $hostinfo->applicationid;
302 $this->sslverification = $hostinfo->sslverification;
303 $this->application = $DB->get_record('mnet_application', array('id'=>$this->applicationid));
304 $this->bootstrapped = true;
307 function get_public_key() {
308 if (isset($this->public_key_ref)) return $this->public_key_ref;
309 $this->public_key_ref = openssl_pkey_get_public($this->public_key);
310 return $this->public_key_ref;