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