Tracker #MDL-9353 - Paypal cost of course not visible in course settings.
[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
85d2d959 28 function bootstrap($wwwroot, $pubkey = null) {
71558f85 29
b2d38907 30 if (substr($wwwroot, -1, 1) == '/') {
e931fedd 31 $wwwroot = substr($wwwroot, 0, -1);
32 }
33
71558f85 34 if ( ! $this->set_wwwroot($wwwroot) ) {
35 $hostname = mnet_get_hostname_from_uri($wwwroot);
36
37 // Get the IP address for that host - if this fails, it will
38 // return the hostname string
39 $ip_address = gethostbyname($hostname);
40
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;
45 }
46
47 $this->name = $wwwroot;
48
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];
56 }
57 }
58
71558f85 59 $this->wwwroot = $wwwroot;
60 $this->ip_address = $ip_address;
61 $this->deleted = 0;
85d2d959 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);
66 }
71558f85 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;
73 }
74 }
75
76 return true;
77 }
78
79 function delete() {
80 if ($this->deleted) return true;
81
82 $users = count_records('user','mnethostid', $this->id);
83 if ($users > 0) {
84 $this->deleted = 1;
85 }
86
87 $actions = count_records('mnet_log','hostid', $this->id);
88 if ($actions > 0) {
89 $this->deleted = 1;
90 }
91
92 $obj = delete_records('mnet_rpc2host', 'host_id', $this->id);
93
94 $this->delete_all_sessions();
95
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();
103 }
104 }
105
106 function count_live_sessions() {
107 $obj = $this->delete_expired_sessions();
108 return count_records('mnet_session','mnethostid', $this->id);
109 }
110
111 function delete_expired_sessions() {
112 $now = time();
113 return delete_records_select('mnet_session', " mnethostid = '{$this->id}' AND expires < '$now' ");
114 }
115
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);
121
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);
126 }
127
128 $deletereturn = delete_records_select('mnet_session', " mnethostid = '{$this->id}'");
129 return true;
130 }
131
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'];
144 }
145 }
146
147 function commit() {
148 $obj = new stdClass();
149
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;
158
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;
165 }
166 }
167
3e008de8 168 function touch() {
169 $this->last_connect_time = time();
170 $this->commit();
171 }
172
71558f85 173 function set_name($newname) {
174 if (is_string($newname) && strlen($newname <= 80)) {
175 $this->name = $newname;
176 return true;
177 }
178 return false;
179 }
180
181 function set_wwwroot($wwwroot) {
182 global $CFG;
183
184 $hostinfo = get_record('mnet_host', 'wwwroot', $wwwroot);
185
186 if ($hostinfo != false) {
187 $this->populate($hostinfo);
188 return true;
189 }
190 return false;
191 }
192
193 function set_id($id) {
194 global $CFG;
195
196 if (clean_param($id, PARAM_INT) != $id) {
197 $this->errno[] = 1;
198 $this->errmsg[] = 'Your id ('.$id.') is not legal';
199 return false;
200 }
201
202 $sql = "
203 SELECT
204 h.*
205 FROM
206 {$CFG->prefix}mnet_host h
207 WHERE
208 h.id = '". $id ."'";
209
210 if ($hostinfo = get_record_sql($sql)) {
211 $this->populate($hostinfo);
212 return true;
213 }
214 return false;
215 }
216
735c7beb 217 /**
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.
220 *
221 * @param object $hostinfo A database record from the mnet_host table
222 * @return void
223 */
71558f85 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;
234 }
235
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;
240 }
241}
242
243?>