3 * An object to represent lots of information about an RPC-peer machine
5 * @author Donal McMullan donal@catalyst.net.nz
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
11 require_once($CFG->libdir
. '/filelib.php'); // download_file_content() used here
15 /** No SSL verification. */
18 /** SSL verification for host. */
21 /** SSL verification for host and peer. */
22 const SSL_HOST_AND_PEER
= 2;
29 var $public_key_expires = 0;
30 var $last_connect_time = 0;
34 var $applicationid = 1; // Default of 1 == Moodle
35 var $keypair = array();
37 var $bootstrapped = false; // set when the object is populated
39 /** @var int $sslverification The level of SSL verification to apply. */
40 public $sslverification = self
::SSL_HOST_AND_PEER
;
42 function mnet_peer() {
47 * Fetch information about a peer identified by wwwroot
48 * If information does not preexist in db, collect it together based on
49 * supplied information
51 * @param string $wwwroot - address of peer whose details we want
52 * @param string $pubkey - to use if we add a record to db for new peer
53 * @param int $application - table id - what kind of peer are we talking to
54 * @return bool - indication of success or failure
56 function bootstrap($wwwroot, $pubkey = null, $application) {
59 if (substr($wwwroot, -1, 1) == '/') {
60 $wwwroot = substr($wwwroot, 0, -1);
63 // If a peer record already exists for this address,
64 // load that info and return
65 if ($this->set_wwwroot($wwwroot)) {
69 $hostname = mnet_get_hostname_from_uri($wwwroot);
70 // Get the IP address for that host - if this fails, it will return the hostname string
71 $ip_address = gethostbyname($hostname);
73 // Couldn't find the IP address?
74 if ($ip_address === $hostname && !preg_match('/^\d+\.\d+\.\d+.\d+$/',$hostname)) {
75 throw new moodle_exception('noaddressforhost', 'mnet', '', $hostname);
78 $this->name
= $wwwroot;
80 // TODO: In reality, this will be prohibitively slow... need another
81 // default - maybe blank string
82 $homepage = download_file_content($wwwroot);
83 if (!empty($homepage)) {
84 $count = preg_match("@<title>(.*)</title>@siU", $homepage, $matches);
86 $this->name
= $matches[1];
90 $this->wwwroot
= $wwwroot;
91 $this->ip_address
= $ip_address;
94 $this->application
= $DB->get_record('mnet_application', array('name'=>$application));
95 if (empty($this->application
)) {
96 $this->application
= $DB->get_record('mnet_application', array('name'=>'moodle'));
99 $this->applicationid
= $this->application
->id
;
102 $this->public_key
= clean_param(mnet_get_public_key($this->wwwroot
, $this->application
), PARAM_PEM
);
104 $this->public_key
= clean_param($pubkey, PARAM_PEM
);
106 $this->public_key_expires
= $this->check_common_name($this->public_key
);
107 $this->last_connect_time
= 0;
108 $this->last_log_id
= 0;
109 if ($this->public_key_expires
== false) {
110 $this->public_key
== '';
113 $this->bootstrapped
= true;
118 * the peer is marked as deleted in the database
119 * we delete current sessions.
120 * @return bool - success
125 if ($this->deleted
) {
129 $this->delete_all_sessions();
132 return $this->commit();
135 function count_live_sessions() {
137 $obj = $this->delete_expired_sessions();
138 return $DB->count_records('mnet_session', array('mnethostid'=>$this->id
));
141 function delete_expired_sessions() {
144 return $DB->delete_records_select('mnet_session', " mnethostid = ? AND expires < ? ", array($this->id
, $now));
147 function delete_all_sessions() {
149 // TODO: Expires each PHP session individually
150 $sessions = $DB->get_records('mnet_session', array('mnethostid'=>$this->id
));
152 if (count($sessions) > 0 && file_exists($CFG->dirroot
.'/auth/mnet/auth.php')) {
153 require_once($CFG->dirroot
.'/auth/mnet/auth.php');
154 $auth = new auth_plugin_mnet();
155 $auth->end_local_sessions($sessions);
158 $deletereturn = $DB->delete_records('mnet_session', array('mnethostid'=>$this->id
));
162 function check_common_name($key) {
163 $credentials = $this->check_credentials($key);
164 return $credentials['validTo_time_t'];
167 function check_credentials($key) {
168 $credentials = openssl_x509_parse($key);
169 if ($credentials == false) {
170 $this->error
[] = array('code' => 3, 'text' => get_string("nonmatchingcert", 'mnet', array('subject' => '','host' => '')));
172 } elseif (array_key_exists('subjectAltName', $credentials['subject']) && $credentials['subject']['subjectAltName'] != $this->wwwroot
) {
173 $a['subject'] = $credentials['subject']['subjectAltName'];
174 $a['host'] = $this->wwwroot
;
175 $this->error
[] = array('code' => 5, 'text' => get_string("nonmatchingcert", 'mnet', $a));
177 } elseif ($credentials['subject']['CN'] != $this->wwwroot
) {
178 $a['subject'] = $credentials['subject']['CN'];
179 $a['host'] = $this->wwwroot
;
180 $this->error
[] = array('code' => 4, 'text' => get_string("nonmatchingcert", 'mnet', $a));
183 if (array_key_exists('subjectAltName', $credentials['subject'])) {
184 $credentials['wwwroot'] = $credentials['subject']['subjectAltName'];
186 $credentials['wwwroot'] = $credentials['subject']['CN'];
194 $obj = new stdClass();
196 $obj->wwwroot
= $this->wwwroot
;
197 $obj->ip_address
= $this->ip_address
;
198 $obj->name
= $this->name
;
199 $obj->public_key
= $this->public_key
;
200 $obj->public_key_expires
= $this->public_key_expires
;
201 $obj->deleted
= $this->deleted
;
202 $obj->last_connect_time
= $this->last_connect_time
;
203 $obj->last_log_id
= $this->last_log_id
;
204 $obj->force_theme
= $this->force_theme
;
205 $obj->theme
= $this->theme
;
206 $obj->applicationid
= $this->applicationid
;
207 $obj->sslverification
= $this->sslverification
;
209 if (isset($this->id
) && $this->id
> 0) {
210 $obj->id
= $this->id
;
211 return $DB->update_record('mnet_host', $obj);
213 $this->id
= $DB->insert_record('mnet_host', $obj);
214 return $this->id
> 0;
219 $this->last_connect_time
= time();
223 function set_name($newname) {
224 if (is_string($newname) && strlen($newname <= 80)) {
225 $this->name
= $newname;
231 function set_applicationid($applicationid) {
232 if (is_numeric($applicationid) && $applicationid == intval($applicationid)) {
233 $this->applicationid
= $applicationid;
240 * Load information from db about an mnet peer into this object's properties
242 * @param string $wwwroot - address of peer whose details we want to load
243 * @return bool - indication of success or failure
245 function set_wwwroot($wwwroot) {
248 $hostinfo = $DB->get_record('mnet_host', array('wwwroot'=>$wwwroot));
250 if ($hostinfo != false) {
251 $this->populate($hostinfo);
257 function set_id($id) {
260 if (clean_param($id, PARAM_INT
) != $id) {
262 $this->errmsg
[] = 'Your id ('.$id.') is not legal';
274 if ($hostinfo = $DB->get_record_sql($sql, array($id))) {
275 $this->populate($hostinfo);
282 * Several methods can be used to get an 'mnet_host' record. They all then
283 * send it to this private method to populate this object's attributes.
285 * @param object $hostinfo A database record from the mnet_host table
288 function populate($hostinfo) {
290 $this->id
= $hostinfo->id
;
291 $this->wwwroot
= $hostinfo->wwwroot
;
292 $this->ip_address
= $hostinfo->ip_address
;
293 $this->name
= $hostinfo->name
;
294 $this->deleted
= $hostinfo->deleted
;
295 $this->public_key
= $hostinfo->public_key
;
296 $this->public_key_expires
= $hostinfo->public_key_expires
;
297 $this->last_connect_time
= $hostinfo->last_connect_time
;
298 $this->last_log_id
= $hostinfo->last_log_id
;
299 $this->force_theme
= $hostinfo->force_theme
;
300 $this->theme
= $hostinfo->theme
;
301 $this->applicationid
= $hostinfo->applicationid
;
302 $this->sslverification
= $hostinfo->sslverification
;
303 $this->application
= $DB->get_record('mnet_application', array('id'=>$this->applicationid
));
304 $this->bootstrapped
= true;
307 function get_public_key() {
308 if (isset($this->public_key_ref
)) return $this->public_key_ref
;
309 $this->public_key_ref
= openssl_pkey_get_public($this->public_key
);
310 return $this->public_key_ref
;