"MDL-8642, setting of auto scrolling"
[moodle-linuxchix.git] / mnet / peer.php
blob46cf2808cd166739582cb61cd3f1b02b5cd56039
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 $force_theme = 0;
22 var $theme = '';
23 var $applicationid = 1; // Default of 1 == Moodle
24 var $keypair = array();
25 var $error = array();
27 function mnet_peer() {
28 return true;
31 function bootstrap($wwwroot, $pubkey = null, $application) {
33 if (substr($wwwroot, -1, 1) == '/') {
34 $wwwroot = substr($wwwroot, 0, -1);
37 if ( ! $this->set_wwwroot($wwwroot) ) {
38 $hostname = mnet_get_hostname_from_uri($wwwroot);
40 // Get the IP address for that host - if this fails, it will
41 // return the hostname string
42 $ip_address = gethostbyname($hostname);
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;
50 $this->name = $wwwroot;
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];
62 $this->wwwroot = $wwwroot;
63 $this->ip_address = $ip_address;
64 $this->deleted = 0;
66 $this->application = get_record('mnet_application', 'name', $application);
67 if (empty($this->application)) {
68 $this->application = get_record('mnet_application', 'name', 'moodle');
71 $this->applicationid = $this->application->id;
73 if(empty($pubkey)) {
74 $this->public_key = clean_param(mnet_get_public_key($this->wwwroot, $this->application), PARAM_PEM);
75 } else {
76 $this->public_key = clean_param($pubkey, PARAM_PEM);
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;
87 return true;
90 function delete() {
91 if ($this->deleted) return true;
93 $users = count_records('user','mnethostid', $this->id);
94 if ($users > 0) {
95 $this->deleted = 1;
98 $actions = count_records('mnet_log','hostid', $this->id);
99 if ($actions > 0) {
100 $this->deleted = 1;
103 $obj = delete_records('mnet_rpc2host', 'host_id', $this->id);
105 $this->delete_all_sessions();
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();
117 function count_live_sessions() {
118 $obj = $this->delete_expired_sessions();
119 return count_records('mnet_session','mnethostid', $this->id);
122 function delete_expired_sessions() {
123 $now = time();
124 return delete_records_select('mnet_session', " mnethostid = '{$this->id}' AND expires < '$now' ");
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);
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);
139 $deletereturn = delete_records_select('mnet_session', " mnethostid = '{$this->id}'");
140 return true;
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'];
158 function commit() {
159 $obj = new stdClass();
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;
169 $obj->force_theme = $this->force_theme;
170 $obj->theme = $this->theme;
171 $obj->applicationid = $this->applicationid;
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;
182 function touch() {
183 $this->last_connect_time = time();
184 $this->commit();
187 function set_name($newname) {
188 if (is_string($newname) && strlen($newname <= 80)) {
189 $this->name = $newname;
190 return true;
192 return false;
195 function set_applicationid($applicationid) {
196 if (is_numeric($applicationid) && $applicationid == intval($applicationid)) {
197 $this->applicationid = $applicationid;
198 return true;
200 return false;
203 function set_wwwroot($wwwroot) {
204 global $CFG;
206 $hostinfo = get_record('mnet_host', 'wwwroot', $wwwroot);
208 if ($hostinfo != false) {
209 $this->populate($hostinfo);
210 return true;
212 return false;
215 function set_id($id) {
216 global $CFG;
218 if (clean_param($id, PARAM_INT) != $id) {
219 $this->errno[] = 1;
220 $this->errmsg[] = 'Your id ('.$id.') is not legal';
221 return false;
224 $sql = "
225 SELECT
227 FROM
228 {$CFG->prefix}mnet_host h
229 WHERE
230 h.id = '". $id ."'";
232 if ($hostinfo = get_record_sql($sql)) {
233 $this->populate($hostinfo);
234 return true;
236 return false;
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.
243 * @param object $hostinfo A database record from the mnet_host table
244 * @return void
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;
256 $this->force_theme = $hostinfo->force_theme;
257 $this->theme = $hostinfo->theme;
258 $this->applicationid = $hostinfo->applicationid;
259 $this->application = get_record('mnet_application', 'id', $this->applicationid);
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;