MDL-10984 more language strings
[moodle-pu.git] / mnet / peer.php
blob3e9d6ffe8245c68d96b57949f814a6f3d760f804
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 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 $applicationid = 1; // Default of 1 == Moodle
22 var $keypair = array();
23 var $error = array();
25 function mnet_peer() {
26 return true;
29 function bootstrap($wwwroot, $pubkey = null, $application) {
31 if (substr($wwwroot, -1, 1) == '/') {
32 $wwwroot = substr($wwwroot, 0, -1);
35 if ( ! $this->set_wwwroot($wwwroot) ) {
36 $hostname = mnet_get_hostname_from_uri($wwwroot);
38 // Get the IP address for that host - if this fails, it will
39 // return the hostname string
40 $ip_address = gethostbyname($hostname);
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;
48 $this->name = $wwwroot;
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];
60 $this->wwwroot = $wwwroot;
61 $this->ip_address = $ip_address;
62 $this->deleted = 0;
64 $this->application = get_record('mnet_application', 'name', $application);
65 if (empty($this->application)) {
66 $this->application = get_record('mnet_application', 'name', 'moodle');
69 $this->applicationid = $this->application->id;
71 if(empty($pubkey)) {
72 $this->public_key = clean_param(mnet_get_public_key($this->wwwroot, $this->application), PARAM_PEM);
73 } else {
74 $this->public_key = clean_param($pubkey, PARAM_PEM);
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;
85 return true;
88 function delete() {
89 if ($this->deleted) return true;
91 $users = count_records('user','mnethostid', $this->id);
92 if ($users > 0) {
93 $this->deleted = 1;
96 $actions = count_records('mnet_log','hostid', $this->id);
97 if ($actions > 0) {
98 $this->deleted = 1;
101 $obj = delete_records('mnet_rpc2host', 'host_id', $this->id);
103 $this->delete_all_sessions();
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();
115 function count_live_sessions() {
116 $obj = $this->delete_expired_sessions();
117 return count_records('mnet_session','mnethostid', $this->id);
120 function delete_expired_sessions() {
121 $now = time();
122 return delete_records_select('mnet_session', " mnethostid = '{$this->id}' AND expires < '$now' ");
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);
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);
137 $deletereturn = delete_records_select('mnet_session', " mnethostid = '{$this->id}'");
138 return true;
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'];
156 function commit() {
157 $obj = new stdClass();
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;
167 $obj->applicationid = $this->applicationid;
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;
178 function touch() {
179 $this->last_connect_time = time();
180 $this->commit();
183 function set_name($newname) {
184 if (is_string($newname) && strlen($newname <= 80)) {
185 $this->name = $newname;
186 return true;
188 return false;
191 function set_applicationid($applicationid) {
192 if (is_numeric($applicationid) && $applicationid == intval($applicationid)) {
193 $this->applicationid = $applicationid;
194 return true;
196 return false;
199 function set_wwwroot($wwwroot) {
200 global $CFG;
202 $hostinfo = get_record('mnet_host', 'wwwroot', $wwwroot);
204 if ($hostinfo != false) {
205 $this->populate($hostinfo);
206 return true;
208 return false;
211 function set_id($id) {
212 global $CFG;
214 if (clean_param($id, PARAM_INT) != $id) {
215 $this->errno[] = 1;
216 $this->errmsg[] = 'Your id ('.$id.') is not legal';
217 return false;
220 $sql = "
221 SELECT
223 FROM
224 {$CFG->prefix}mnet_host h
225 WHERE
226 h.id = '". $id ."'";
228 if ($hostinfo = get_record_sql($sql)) {
229 $this->populate($hostinfo);
230 return true;
232 return false;
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.
239 * @param object $hostinfo A database record from the mnet_host table
240 * @return void
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;
252 $this->applicationid = $hostinfo->applicationid;
253 $this->application = get_record('mnet_application', 'id', $this->applicationid);
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;