weekly release 1.9.12+
[moodle.git] / mnet / peer.php
blob94017a1417ada52a454f41ab449fc45895fb86cf
1 <?php // $Id$
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 class mnet_peer {
13 //Unless stated otherwise, properties of this object are unescaped, and unsafe to
14 //insert into the db without further processing.
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 //Object whose properties need to be put into the database:
29 //(properties here are slashescaped)
30 var $updateparams;
32 function mnet_peer() {
33 $this->updateparams = new stdClass();
34 return true;
37 function bootstrap($wwwroot, $pubkey = null, $application) {
39 if (substr($wwwroot, -1, 1) == '/') {
40 $wwwroot = substr($wwwroot, 0, -1);
43 if ( ! $this->set_wwwroot($wwwroot) ) {
44 $hostname = mnet_get_hostname_from_uri($wwwroot);
46 // Get the IP address for that host - if this fails, it will
47 // return the hostname string
48 $ip_address = gethostbyname($hostname);
50 // Couldn't find the IP address?
51 if ($ip_address === $hostname && !preg_match('/^\d+\.\d+\.\d+.\d+$/',$hostname)) {
52 $this->error[] = array('code' => 2, 'text' => get_string("noaddressforhost", 'mnet'));
53 return false;
56 $this->name = stripslashes($wwwroot);
57 $this->updateparams->name = $wwwroot;
59 // TODO: In reality, this will be prohibitively slow... need another
60 // default - maybe blank string
61 $homepage = file_get_contents($wwwroot);
62 if (!empty($homepage)) {
63 $count = preg_match("@<title>(.*)</title>@siU", $homepage, $matches);
64 if ($count > 0) {
65 $this->name = $matches[1];
66 $this->updateparams->name = addslashes($matches[1]);
70 $this->wwwroot = stripslashes($wwwroot);
71 $this->updateparams->wwwroot = $wwwroot;
72 $this->ip_address = $ip_address;
73 $this->updateparams->ip_address = $ip_address;
74 $this->deleted = 0;
75 $this->updateparams->deleted = 0;
77 $this->application = get_record('mnet_application', 'name', $application);
78 if (empty($this->application)) {
79 $this->application = get_record('mnet_application', 'name', 'moodle');
82 $this->applicationid = $this->application->id;
83 $this->updateparams->applicationid = $this->application->id;
85 if(empty($pubkey)) {
86 $pubkeytemp = clean_param(mnet_get_public_key($this->wwwroot, $this->application), PARAM_PEM);
87 } else {
88 $pubkeytemp = clean_param($pubkey, PARAM_PEM);
90 $this->public_key_expires = $this->check_common_name($pubkeytemp);
92 if ($this->public_key_expires == false) {
93 return false;
95 $this->updateparams->public_key_expires = $this->public_key_expires;
97 $this->updateparams->public_key = $pubkeytemp;
98 $this->public_key = $pubkeytemp;
100 $this->last_connect_time = 0;
101 $this->updateparams->last_connect_time = 0;
102 $this->last_log_id = 0;
103 $this->updateparams->last_log_id = 0;
106 return true;
109 function delete() {
110 if ($this->deleted) {
111 return true;
114 $this->delete_all_sessions();
116 $this->deleted = 1;
117 $this->updateparams->deleted = 1;
118 $this->commit();
121 function count_live_sessions() {
122 $obj = $this->delete_expired_sessions();
123 return count_records('mnet_session','mnethostid', $this->id);
126 function delete_expired_sessions() {
127 $now = time();
128 return delete_records_select('mnet_session', " mnethostid = '{$this->id}' AND expires < '$now' ");
131 function delete_all_sessions() {
132 global $CFG;
133 // TODO: Expires each PHP session individually
134 // $sessions = get_records('mnet_session', 'mnethostid', $this->id);
135 $sessions = get_records('mnet_session', 'mnethostid', $this->id);
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);
143 $deletereturn = delete_records_select('mnet_session', " mnethostid = '{$this->id}'");
144 return true;
147 function check_common_name($key) {
148 $credentials = openssl_x509_parse($key);
149 if ($credentials == false) {
150 $this->error[] = array('code' => 3, 'text' => get_string("nonmatchingcert", 'mnet', array('','')));
151 return false;
152 } elseif ($credentials['subject']['CN'] != $this->wwwroot) {
153 $a[] = $credentials['subject']['CN'];
154 $a[] = $this->wwwroot;
155 $this->error[] = array('code' => 4, 'text' => get_string("nonmatchingcert", 'mnet', $a));
156 return false;
157 } else {
158 return $credentials['validTo_time_t'];
162 function commit() {
163 $obj = $this->updateparams;
165 if (isset($this->id) && $this->id > 0) {
166 $obj->id = $this->id;
167 $dbresult = update_record('mnet_host', $obj);
168 } else {
169 $this->id = insert_record('mnet_host', $obj);
170 $dbresult = ($this->id > 0);
172 //If the insert/update was successful, clear the parameters that need updating
173 if ($dbresult) {
174 $this->updateparams = new stdClass();
176 return $dbresult;
179 function touch() {
180 $this->last_connect_time = time();
181 $this->updateparams->last_connect_time = time();
182 $this->commit();
185 function set_name($newname) {
186 if (is_string($newname) && strlen($newname <= 80)) {
187 $this->name = stripslashes($newname);
188 $this->updateparams->name = $newname;
189 return true;
191 return false;
194 function set_applicationid($applicationid) {
195 if (is_numeric($applicationid) && $applicationid == intval($applicationid)) {
196 $this->applicationid = $applicationid;
197 $this->updateparams->applicationid = $applicationid;
198 return true;
200 return false;
203 function set_wwwroot($wwwroot) {
204 global $CFG;
206 $hostinfo = get_record('mnet_host', 'wwwroot', $wwwroot);
208 if ($hostinfo != false) {
209 $this->populate($hostinfo);
210 return true;
212 return false;
215 function set_id($id) {
216 global $CFG;
218 if (clean_param($id, PARAM_INT) != $id) {
219 $this->errno[] = 1;
220 $this->errmsg[] = 'Your id ('.$id.') is not legal';
221 return false;
224 $sql = "
225 SELECT
227 FROM
228 {$CFG->prefix}mnet_host h
229 WHERE
230 h.id = '". $id ."'";
232 if ($hostinfo = get_record_sql($sql)) {
233 $this->populate($hostinfo);
234 return true;
236 return false;
240 * Several methods can be used to get an 'mnet_host' record. They all then
241 * send it to this private method to populate this object's attributes.
243 * @param object $hostinfo A database record from the mnet_host table
244 * @return void
246 function populate($hostinfo) {
247 $this->id = $hostinfo->id;
248 $this->wwwroot = $hostinfo->wwwroot;
249 $this->ip_address = $hostinfo->ip_address;
250 $this->name = $hostinfo->name;
251 $this->deleted = $hostinfo->deleted;
252 $this->public_key = $hostinfo->public_key;
253 $this->public_key_expires = $hostinfo->public_key_expires;
254 $this->last_connect_time = $hostinfo->last_connect_time;
255 $this->last_log_id = $hostinfo->last_log_id;
256 $this->force_theme = $hostinfo->force_theme;
257 $this->theme = $hostinfo->theme;
258 $this->applicationid = $hostinfo->applicationid;
259 $this->application = get_record('mnet_application', 'id', $this->applicationid);
262 function get_public_key() {
263 if (isset($this->public_key_ref)) return $this->public_key_ref;
264 $this->public_key_ref = openssl_pkey_get_public($this->public_key);
265 return $this->public_key_ref;