Follow-up fix for Bug MDL-8617 "Implement groupings & course modules..." internal...
[moodle-pu.git] / mnet / peer.php
blob2e1c64cc5058c11ab6a42187e14bf18230f32eb1
1 <?php
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 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();
24 function mnet_peer() {
25 return true;
28 function bootstrap($wwwroot, $pubkey = null) {
30 if (substr($wwwroot, 0, -1) == '/') {
31 $wwwroot = substr($wwwroot, 0, -1);
34 if ( ! $this->set_wwwroot($wwwroot) ) {
35 $hostname = mnet_get_hostname_from_uri($wwwroot);
37 // Get the IP address for that host - if this fails, it will
38 // return the hostname string
39 $ip_address = gethostbyname($hostname);
41 // Couldn't find the IP address?
42 if ($ip_address === $hostname && !preg_match('/^\d+\.\d+\.\d+.\d+$/',$hostname)) {
43 $this->error[] = array('code' => 2, 'text' => get_string("noaddressforhost", 'mnet'));
44 return false;
47 $this->name = $wwwroot;
49 // TODO: In reality, this will be prohibitively slow... need another
50 // default - maybe blank string
51 $homepage = file_get_contents($wwwroot);
52 if (!empty($homepage)) {
53 $count = preg_match("@<title>(.*)</title>@siU", $homepage, $matches);
54 if ($count > 0) {
55 $this->name = $matches[1];
59 $this->wwwroot = $wwwroot;
60 $this->ip_address = $ip_address;
61 $this->deleted = 0;
62 if(empty($pubkey)) {
63 $this->public_key = clean_param(mnet_get_public_key($this->wwwroot), PARAM_PEM);
64 } else {
65 $this->public_key = clean_param($pubkey, PARAM_PEM);
67 $this->public_key_expires = $this->check_common_name($this->public_key);
68 $this->last_connect_time = 0;
69 $this->last_log_id = 0;
70 if ($this->public_key_expires == false) {
71 $this->public_key == '';
72 return false;
76 return true;
79 function delete() {
80 if ($this->deleted) return true;
82 $users = count_records('user','mnethostid', $this->id);
83 if ($users > 0) {
84 $this->deleted = 1;
87 $actions = count_records('mnet_log','hostid', $this->id);
88 if ($actions > 0) {
89 $this->deleted = 1;
92 $obj = delete_records('mnet_rpc2host', 'host_id', $this->id);
94 $this->delete_all_sessions();
96 // If we don't have any activity records for which the mnet_host table
97 // provides a foreign key, then we can delete the record. Otherwise, we
98 // just mark it as deleted.
99 if (0 == $this->deleted) {
100 delete_records('mnet_host', "id", $this->id);
101 } else {
102 $this->commit();
106 function count_live_sessions() {
107 $obj = $this->delete_expired_sessions();
108 return count_records('mnet_session','mnethostid', $this->id);
111 function delete_expired_sessions() {
112 $now = time();
113 return delete_records_select('mnet_session', " mnethostid = '{$this->id}' AND expires < '$now' ");
116 function delete_all_sessions() {
117 global $CFG;
118 // TODO: Expires each PHP session individually
119 // $sessions = get_records('mnet_session', 'mnethostid', $this->id);
120 $sessions = get_records('mnet_session', 'mnethostid', $this->id);
122 if (count($sessions) > 0 && file_exists($CFG->dirroot.'/auth/mnet/auth.php')) {
123 require_once($CFG->dirroot.'/auth/mnet/auth.php');
124 $auth = new auth_plugin_mnet();
125 $auth->end_local_sessions($sessions);
128 $deletereturn = delete_records_select('mnet_session', " mnethostid = '{$this->id}'");
129 return true;
132 function check_common_name($key) {
133 $credentials = openssl_x509_parse($key);
134 if ($credentials == false) {
135 $this->error[] = array('code' => 3, 'text' => get_string("nonmatchingcert", 'mnet', array('','')));
136 return false;
137 } elseif ($credentials['subject']['CN'] != $this->wwwroot) {
138 $a[] = $credentials['subject']['CN'];
139 $a[] = $this->wwwroot;
140 $this->error[] = array('code' => 4, 'text' => get_string("nonmatchingcert", 'mnet', $a));
141 return false;
142 } else {
143 return $credentials['validTo_time_t'];
147 function commit() {
148 $obj = new stdClass();
150 $obj->wwwroot = $this->wwwroot;
151 $obj->ip_address = $this->ip_address;
152 $obj->name = $this->name;
153 $obj->public_key = $this->public_key;
154 $obj->public_key_expires = $this->public_key_expires;
155 $obj->deleted = $this->deleted;
156 $obj->last_connect_time = $this->last_connect_time;
157 $obj->last_log_id = $this->last_log_id;
159 if (isset($this->id) && $this->id > 0) {
160 $obj->id = $this->id;
161 return update_record('mnet_host', $obj);
162 } else {
163 $this->id = insert_record('mnet_host', $obj);
164 return $this->id > 0;
168 function touch() {
169 $this->last_connect_time = time();
170 $this->commit();
173 function set_name($newname) {
174 if (is_string($newname) && strlen($newname <= 80)) {
175 $this->name = $newname;
176 return true;
178 return false;
181 function set_wwwroot($wwwroot) {
182 global $CFG;
184 $hostinfo = get_record('mnet_host', 'wwwroot', $wwwroot);
186 if ($hostinfo != false) {
187 $this->populate($hostinfo);
188 return true;
190 return false;
193 function set_id($id) {
194 global $CFG;
196 if (clean_param($id, PARAM_INT) != $id) {
197 $this->errno[] = 1;
198 $this->errmsg[] = 'Your id ('.$id.') is not legal';
199 return false;
202 $sql = "
203 SELECT
205 FROM
206 {$CFG->prefix}mnet_host h
207 WHERE
208 h.id = '". $id ."'";
210 if ($hostinfo = get_record_sql($sql)) {
211 $this->populate($hostinfo);
212 return true;
214 return false;
218 * Several methods can be used to get an 'mnet_host' record. They all then
219 * send it to this private method to populate this object's attributes.
221 * @param object $hostinfo A database record from the mnet_host table
222 * @return void
224 function populate($hostinfo) {
225 $this->id = $hostinfo->id;
226 $this->wwwroot = $hostinfo->wwwroot;
227 $this->ip_address = $hostinfo->ip_address;
228 $this->name = $hostinfo->name;
229 $this->deleted = $hostinfo->deleted;
230 $this->public_key = $hostinfo->public_key;
231 $this->public_key_expires = $hostinfo->public_key_expires;
232 $this->last_connect_time = $hostinfo->last_connect_time;
233 $this->last_log_id = $hostinfo->last_log_id;
236 function get_public_key() {
237 if (isset($this->public_key_ref)) return $this->public_key_ref;
238 $this->public_key_ref = openssl_pkey_get_public($this->public_key);
239 return $this->public_key_ref;