MDL-16221
[moodle-linuxchix.git] / mnet / peer.php
bloba5a5d591f641ae69a89147bf0a1daf9e279fa4c3
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) return true;
112 $users = count_records('user','mnethostid', $this->id);
113 if ($users > 0) {
114 $this->deleted = 1;
115 $this->updateparams->deleted = 1;
118 $actions = count_records('mnet_log','hostid', $this->id);
119 if ($actions > 0) {
120 $this->deleted = 1;
121 $this->updateparams->deleted = 1;
124 $obj = delete_records('mnet_rpc2host', 'host_id', $this->id);
126 $this->delete_all_sessions();
128 // If we don't have any activity records for which the mnet_host table
129 // provides a foreign key, then we can delete the record. Otherwise, we
130 // just mark it as deleted.
131 if (0 == $this->deleted) {
132 delete_records('mnet_host', "id", $this->id);
133 } else {
134 $this->commit();
138 function count_live_sessions() {
139 $obj = $this->delete_expired_sessions();
140 return count_records('mnet_session','mnethostid', $this->id);
143 function delete_expired_sessions() {
144 $now = time();
145 return delete_records_select('mnet_session', " mnethostid = '{$this->id}' AND expires < '$now' ");
148 function delete_all_sessions() {
149 global $CFG;
150 // TODO: Expires each PHP session individually
151 // $sessions = get_records('mnet_session', 'mnethostid', $this->id);
152 $sessions = get_records('mnet_session', 'mnethostid', $this->id);
154 if (count($sessions) > 0 && file_exists($CFG->dirroot.'/auth/mnet/auth.php')) {
155 require_once($CFG->dirroot.'/auth/mnet/auth.php');
156 $auth = new auth_plugin_mnet();
157 $auth->end_local_sessions($sessions);
160 $deletereturn = delete_records_select('mnet_session', " mnethostid = '{$this->id}'");
161 return true;
164 function check_common_name($key) {
165 $credentials = openssl_x509_parse($key);
166 if ($credentials == false) {
167 $this->error[] = array('code' => 3, 'text' => get_string("nonmatchingcert", 'mnet', array('','')));
168 return false;
169 } elseif ($credentials['subject']['CN'] != $this->wwwroot) {
170 $a[] = $credentials['subject']['CN'];
171 $a[] = $this->wwwroot;
172 $this->error[] = array('code' => 4, 'text' => get_string("nonmatchingcert", 'mnet', $a));
173 return false;
174 } else {
175 return $credentials['validTo_time_t'];
179 function commit() {
180 $obj = $this->updateparams;
182 if (isset($this->id) && $this->id > 0) {
183 $obj->id = $this->id;
184 $dbresult = update_record('mnet_host', $obj);
185 } else {
186 $this->id = insert_record('mnet_host', $obj);
187 $dbresult = ($this->id > 0);
189 //If the insert/update was successful, clear the parameters that need updating
190 if ($dbresult) {
191 $this->updateparams = new stdClass();
193 return $dbresult;
196 function touch() {
197 $this->last_connect_time = time();
198 $this->updateparams->last_connect_time = time();
199 $this->commit();
202 function set_name($newname) {
203 if (is_string($newname) && strlen($newname <= 80)) {
204 $this->name = stripslashes($newname);
205 $this->updateparams->name = $newname;
206 return true;
208 return false;
211 function set_applicationid($applicationid) {
212 if (is_numeric($applicationid) && $applicationid == intval($applicationid)) {
213 $this->applicationid = $applicationid;
214 $this->updateparams->applicationid = $applicationid;
215 return true;
217 return false;
220 function set_wwwroot($wwwroot) {
221 global $CFG;
223 $hostinfo = get_record('mnet_host', 'wwwroot', $wwwroot);
225 if ($hostinfo != false) {
226 $this->populate($hostinfo);
227 return true;
229 return false;
232 function set_id($id) {
233 global $CFG;
235 if (clean_param($id, PARAM_INT) != $id) {
236 $this->errno[] = 1;
237 $this->errmsg[] = 'Your id ('.$id.') is not legal';
238 return false;
241 $sql = "
242 SELECT
244 FROM
245 {$CFG->prefix}mnet_host h
246 WHERE
247 h.id = '". $id ."'";
249 if ($hostinfo = get_record_sql($sql)) {
250 $this->populate($hostinfo);
251 return true;
253 return false;
257 * Several methods can be used to get an 'mnet_host' record. They all then
258 * send it to this private method to populate this object's attributes.
260 * @param object $hostinfo A database record from the mnet_host table
261 * @return void
263 function populate($hostinfo) {
264 $this->id = $hostinfo->id;
265 $this->wwwroot = $hostinfo->wwwroot;
266 $this->ip_address = $hostinfo->ip_address;
267 $this->name = $hostinfo->name;
268 $this->deleted = $hostinfo->deleted;
269 $this->public_key = $hostinfo->public_key;
270 $this->public_key_expires = $hostinfo->public_key_expires;
271 $this->last_connect_time = $hostinfo->last_connect_time;
272 $this->last_log_id = $hostinfo->last_log_id;
273 $this->force_theme = $hostinfo->force_theme;
274 $this->theme = $hostinfo->theme;
275 $this->applicationid = $hostinfo->applicationid;
276 $this->application = get_record('mnet_application', 'id', $this->applicationid);
279 function get_public_key() {
280 if (isset($this->public_key_ref)) return $this->public_key_ref;
281 $this->public_key_ref = openssl_pkey_get_public($this->public_key);
282 return $this->public_key_ref;