Label for wwwroot 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
54 $homepage = file_get_contents($wwwroot);
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) {
144 $credentials = openssl_x509_parse($key);
145 if ($credentials == false) {
146 $this->error[] = array('code' => 3, 'text' => get_string("nonmatchingcert", 'mnet', array('','')));
147 return false;
148 } elseif ($credentials['subject']['CN'] != $this->wwwroot) {
149 $a[] = $credentials['subject']['CN'];
150 $a[] = $this->wwwroot;
151 $this->error[] = array('code' => 4, 'text' => get_string("nonmatchingcert", 'mnet', $a));
152 return false;
153 } else {
154 return $credentials['validTo_time_t'];
155 }
156 }
157
158 function commit() {
159 $obj = new stdClass();
160
161 $obj->wwwroot = $this->wwwroot;
162 $obj->ip_address = $this->ip_address;
163 $obj->name = $this->name;
164 $obj->public_key = $this->public_key;
165 $obj->public_key_expires = $this->public_key_expires;
166 $obj->deleted = $this->deleted;
167 $obj->last_connect_time = $this->last_connect_time;
168 $obj->last_log_id = $this->last_log_id;
22882b66 169 $obj->force_theme = $this->force_theme;
170 $obj->theme = $this->theme;
25202581 171 $obj->applicationid = $this->applicationid;
71558f85 172
173 if (isset($this->id) && $this->id > 0) {
174 $obj->id = $this->id;
175 return update_record('mnet_host', $obj);
176 } else {
177 $this->id = insert_record('mnet_host', $obj);
178 return $this->id > 0;
179 }
180 }
181
3e008de8 182 function touch() {
183 $this->last_connect_time = time();
184 $this->commit();
185 }
186
71558f85 187 function set_name($newname) {
188 if (is_string($newname) && strlen($newname <= 80)) {
189 $this->name = $newname;
190 return true;
191 }
192 return false;
193 }
194
25202581 195 function set_applicationid($applicationid) {
196 if (is_numeric($applicationid) && $applicationid == intval($applicationid)) {
197 $this->applicationid = $applicationid;
198 return true;
199 }
200 return false;
201 }
202
71558f85 203 function set_wwwroot($wwwroot) {
204 global $CFG;
205
206 $hostinfo = get_record('mnet_host', 'wwwroot', $wwwroot);
207
208 if ($hostinfo != false) {
209 $this->populate($hostinfo);
210 return true;
211 }
212 return false;
213 }
214
215 function set_id($id) {
216 global $CFG;
217
218 if (clean_param($id, PARAM_INT) != $id) {
219 $this->errno[] = 1;
220 $this->errmsg[] = 'Your id ('.$id.') is not legal';
221 return false;
222 }
223
224 $sql = "
225 SELECT
226 h.*
227 FROM
228 {$CFG->prefix}mnet_host h
229 WHERE
230 h.id = '". $id ."'";
231
232 if ($hostinfo = get_record_sql($sql)) {
233 $this->populate($hostinfo);
234 return true;
235 }
236 return false;
237 }
238
735c7beb 239 /**
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.
242 *
243 * @param object $hostinfo A database record from the mnet_host table
244 * @return void
245 */
71558f85 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;
22882b66 256 $this->force_theme = $hostinfo->force_theme;
257 $this->theme = $hostinfo->theme;
25202581 258 $this->applicationid = $hostinfo->applicationid;
259 $this->application = get_record('mnet_application', 'id', $this->applicationid);
71558f85 260 }
261
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;
266 }
267}
268
269?>