New files for config of MNet roaming themes
[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;
25202581 21 var $applicationid = 1; // Default of 1 == Moodle
71558f85 22 var $keypair = array();
23 var $error = array();
24
25 function mnet_peer() {
26 return true;
27 }
28
25202581 29 function bootstrap($wwwroot, $pubkey = null, $application) {
71558f85 30
b2d38907 31 if (substr($wwwroot, -1, 1) == '/') {
e931fedd 32 $wwwroot = substr($wwwroot, 0, -1);
33 }
34
71558f85 35 if ( ! $this->set_wwwroot($wwwroot) ) {
36 $hostname = mnet_get_hostname_from_uri($wwwroot);
37
38 // Get the IP address for that host - if this fails, it will
39 // return the hostname string
40 $ip_address = gethostbyname($hostname);
41
42 // Couldn't find the IP address?
43 if ($ip_address === $hostname && !preg_match('/^\d+\.\d+\.\d+.\d+$/',$hostname)) {
44 $this->error[] = array('code' => 2, 'text' => get_string("noaddressforhost", 'mnet'));
45 return false;
46 }
47
48 $this->name = $wwwroot;
49
50 // TODO: In reality, this will be prohibitively slow... need another
51 // default - maybe blank string
52 $homepage = file_get_contents($wwwroot);
53 if (!empty($homepage)) {
54 $count = preg_match("@<title>(.*)</title>@siU", $homepage, $matches);
55 if ($count > 0) {
56 $this->name = $matches[1];
57 }
58 }
59
71558f85 60 $this->wwwroot = $wwwroot;
61 $this->ip_address = $ip_address;
62 $this->deleted = 0;
25202581 63
64 $this->application = get_record('mnet_application', 'name', $application);
65 if (empty($this->application)) {
66 $this->application = get_record('mnet_application', 'name', 'moodle');
67 }
68
69 $this->applicationid = $this->application->id;
70
85d2d959 71 if(empty($pubkey)) {
25202581 72 $this->public_key = clean_param(mnet_get_public_key($this->wwwroot, $this->application), PARAM_PEM);
85d2d959 73 } else {
74 $this->public_key = clean_param($pubkey, PARAM_PEM);
75 }
71558f85 76 $this->public_key_expires = $this->check_common_name($this->public_key);
77 $this->last_connect_time = 0;
78 $this->last_log_id = 0;
79 if ($this->public_key_expires == false) {
80 $this->public_key == '';
81 return false;
82 }
83 }
84
85 return true;
86 }
87
88 function delete() {
89 if ($this->deleted) return true;
90
91 $users = count_records('user','mnethostid', $this->id);
92 if ($users > 0) {
93 $this->deleted = 1;
94 }
95
96 $actions = count_records('mnet_log','hostid', $this->id);
97 if ($actions > 0) {
98 $this->deleted = 1;
99 }
100
101 $obj = delete_records('mnet_rpc2host', 'host_id', $this->id);
102
103 $this->delete_all_sessions();
104
105 // If we don't have any activity records for which the mnet_host table
106 // provides a foreign key, then we can delete the record. Otherwise, we
107 // just mark it as deleted.
108 if (0 == $this->deleted) {
109 delete_records('mnet_host', "id", $this->id);
110 } else {
111 $this->commit();
112 }
113 }
114
115 function count_live_sessions() {
116 $obj = $this->delete_expired_sessions();
117 return count_records('mnet_session','mnethostid', $this->id);
118 }
119
120 function delete_expired_sessions() {
121 $now = time();
122 return delete_records_select('mnet_session', " mnethostid = '{$this->id}' AND expires < '$now' ");
123 }
124
125 function delete_all_sessions() {
126 global $CFG;
127 // TODO: Expires each PHP session individually
128 // $sessions = get_records('mnet_session', 'mnethostid', $this->id);
129 $sessions = get_records('mnet_session', 'mnethostid', $this->id);
130
131 if (count($sessions) > 0 && file_exists($CFG->dirroot.'/auth/mnet/auth.php')) {
132 require_once($CFG->dirroot.'/auth/mnet/auth.php');
133 $auth = new auth_plugin_mnet();
134 $auth->end_local_sessions($sessions);
135 }
136
137 $deletereturn = delete_records_select('mnet_session', " mnethostid = '{$this->id}'");
138 return true;
139 }
140
141 function check_common_name($key) {
142 $credentials = openssl_x509_parse($key);
143 if ($credentials == false) {
144 $this->error[] = array('code' => 3, 'text' => get_string("nonmatchingcert", 'mnet', array('','')));
145 return false;
146 } elseif ($credentials['subject']['CN'] != $this->wwwroot) {
147 $a[] = $credentials['subject']['CN'];
148 $a[] = $this->wwwroot;
149 $this->error[] = array('code' => 4, 'text' => get_string("nonmatchingcert", 'mnet', $a));
150 return false;
151 } else {
152 return $credentials['validTo_time_t'];
153 }
154 }
155
156 function commit() {
157 $obj = new stdClass();
158
159 $obj->wwwroot = $this->wwwroot;
160 $obj->ip_address = $this->ip_address;
161 $obj->name = $this->name;
162 $obj->public_key = $this->public_key;
163 $obj->public_key_expires = $this->public_key_expires;
164 $obj->deleted = $this->deleted;
165 $obj->last_connect_time = $this->last_connect_time;
166 $obj->last_log_id = $this->last_log_id;
25202581 167 $obj->applicationid = $this->applicationid;
71558f85 168
169 if (isset($this->id) && $this->id > 0) {
170 $obj->id = $this->id;
171 return update_record('mnet_host', $obj);
172 } else {
173 $this->id = insert_record('mnet_host', $obj);
174 return $this->id > 0;
175 }
176 }
177
3e008de8 178 function touch() {
179 $this->last_connect_time = time();
180 $this->commit();
181 }
182
71558f85 183 function set_name($newname) {
184 if (is_string($newname) && strlen($newname <= 80)) {
185 $this->name = $newname;
186 return true;
187 }
188 return false;
189 }
190
25202581 191 function set_applicationid($applicationid) {
192 if (is_numeric($applicationid) && $applicationid == intval($applicationid)) {
193 $this->applicationid = $applicationid;
194 return true;
195 }
196 return false;
197 }
198
71558f85 199 function set_wwwroot($wwwroot) {
200 global $CFG;
201
202 $hostinfo = get_record('mnet_host', 'wwwroot', $wwwroot);
203
204 if ($hostinfo != false) {
205 $this->populate($hostinfo);
206 return true;
207 }
208 return false;
209 }
210
211 function set_id($id) {
212 global $CFG;
213
214 if (clean_param($id, PARAM_INT) != $id) {
215 $this->errno[] = 1;
216 $this->errmsg[] = 'Your id ('.$id.') is not legal';
217 return false;
218 }
219
220 $sql = "
221 SELECT
222 h.*
223 FROM
224 {$CFG->prefix}mnet_host h
225 WHERE
226 h.id = '". $id ."'";
227
228 if ($hostinfo = get_record_sql($sql)) {
229 $this->populate($hostinfo);
230 return true;
231 }
232 return false;
233 }
234
735c7beb 235 /**
236 * Several methods can be used to get an 'mnet_host' record. They all then
237 * send it to this private method to populate this object's attributes.
238 *
239 * @param object $hostinfo A database record from the mnet_host table
240 * @return void
241 */
71558f85 242 function populate($hostinfo) {
243 $this->id = $hostinfo->id;
244 $this->wwwroot = $hostinfo->wwwroot;
245 $this->ip_address = $hostinfo->ip_address;
246 $this->name = $hostinfo->name;
247 $this->deleted = $hostinfo->deleted;
248 $this->public_key = $hostinfo->public_key;
249 $this->public_key_expires = $hostinfo->public_key_expires;
250 $this->last_connect_time = $hostinfo->last_connect_time;
251 $this->last_log_id = $hostinfo->last_log_id;
25202581 252 $this->applicationid = $hostinfo->applicationid;
253 $this->application = get_record('mnet_application', 'id', $this->applicationid);
71558f85 254 }
255
256 function get_public_key() {
257 if (isset($this->public_key_ref)) return $this->public_key_ref;
258 $this->public_key_ref = openssl_pkey_get_public($this->public_key);
259 return $this->public_key_ref;
260 }
261}
262
263?>