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
;
43 * Fetch information about a peer identified by wwwroot
44 * If information does not preexist in db, collect it together based on
45 * supplied information
47 * @param string $wwwroot - address of peer whose details we want
48 * @param string $pubkey - to use if we add a record to db for new peer
49 * @param int $application - table id - what kind of peer are we talking to
50 * @return bool - indication of success or failure
52 function bootstrap($wwwroot, $pubkey, $application) {
55 if (substr($wwwroot, -1, 1) == '/') {
56 $wwwroot = substr($wwwroot, 0, -1);
59 // If a peer record already exists for this address,
60 // load that info and return
61 if ($this->set_wwwroot($wwwroot)) {
65 $hostname = mnet_get_hostname_from_uri($wwwroot);
66 // Get the IP address for that host - if this fails, it will return the hostname string
67 $ip_address = gethostbyname($hostname);
69 // Couldn't find the IP address?
70 if ($ip_address === $hostname && !preg_match('/^\d+\.\d+\.\d+.\d+$/',$hostname)) {
71 throw new moodle_exception('noaddressforhost', 'mnet', '', $hostname);
74 $this->name
= $wwwroot;
76 // TODO: In reality, this will be prohibitively slow... need another
77 // default - maybe blank string
78 $homepage = download_file_content($wwwroot);
79 if (!empty($homepage)) {
80 $count = preg_match("@<title>(.*)</title>@siU", $homepage, $matches);
82 $this->name
= $matches[1];
86 $this->wwwroot
= $wwwroot;
87 $this->ip_address
= $ip_address;
90 $this->application
= $DB->get_record('mnet_application', array('name'=>$application));
91 if (empty($this->application
)) {
92 $this->application
= $DB->get_record('mnet_application', array('name'=>'moodle'));
95 $this->applicationid
= $this->application
->id
;
98 $this->public_key
= clean_param(mnet_get_public_key($this->wwwroot
, $this->application
), PARAM_PEM
);
100 $this->public_key
= clean_param($pubkey, PARAM_PEM
);
102 $this->public_key_expires
= $this->check_common_name($this->public_key
);
103 $this->last_connect_time
= 0;
104 $this->last_log_id
= 0;
105 if ($this->public_key_expires
== false) {
106 $this->public_key
== '';
109 $this->bootstrapped
= true;
115 * the peer is marked as deleted in the database
116 * we delete current sessions.
117 * @return bool - success
122 if ($this->deleted
) {
126 $this->delete_all_sessions();
129 return $this->commit();
132 function count_live_sessions() {
134 $obj = $this->delete_expired_sessions();
135 return $DB->count_records('mnet_session', array('mnethostid'=>$this->id
));
138 function delete_expired_sessions() {
141 return $DB->delete_records_select('mnet_session', " mnethostid = ? AND expires < ? ", array($this->id
, $now));
144 function delete_all_sessions() {
146 // TODO: Expires each PHP session individually
147 $sessions = $DB->get_records('mnet_session', array('mnethostid'=>$this->id
));
149 if (count($sessions) > 0 && file_exists($CFG->dirroot
.'/auth/mnet/auth.php')) {
150 require_once($CFG->dirroot
.'/auth/mnet/auth.php');
151 $auth = new auth_plugin_mnet();
152 $auth->end_local_sessions($sessions);
155 $deletereturn = $DB->delete_records('mnet_session', array('mnethostid'=>$this->id
));
159 function check_common_name($key) {
160 $credentials = $this->check_credentials($key);
161 return $credentials['validTo_time_t'];
164 function check_credentials($key) {
165 $credentials = openssl_x509_parse($key);
166 if ($credentials == false) {
167 $this->error
[] = array('code' => 3, 'text' => get_string("nonmatchingcert", 'mnet', array('subject' => '','host' => '')));
169 } elseif (array_key_exists('subjectAltName', $credentials['subject']) && $credentials['subject']['subjectAltName'] != $this->wwwroot
) {
170 $a['subject'] = $credentials['subject']['subjectAltName'];
171 $a['host'] = $this->wwwroot
;
172 $this->error
[] = array('code' => 5, 'text' => get_string("nonmatchingcert", 'mnet', $a));
174 } else if ($credentials['subject']['CN'] !== substr($this->wwwroot
, 0, 64)) {
175 $a['subject'] = $credentials['subject']['CN'];
176 $a['host'] = $this->wwwroot
;
177 $this->error
[] = array('code' => 4, 'text' => get_string("nonmatchingcert", 'mnet', $a));
180 if (array_key_exists('subjectAltName', $credentials['subject'])) {
181 $credentials['wwwroot'] = $credentials['subject']['subjectAltName'];
183 $credentials['wwwroot'] = $credentials['subject']['CN'];
191 $obj = new stdClass();
193 $obj->wwwroot
= $this->wwwroot
;
194 $obj->ip_address
= $this->ip_address
;
195 $obj->name
= $this->name
;
196 $obj->public_key
= $this->public_key
;
197 $obj->public_key_expires
= $this->public_key_expires
;
198 $obj->deleted
= $this->deleted
;
199 $obj->last_connect_time
= $this->last_connect_time
;
200 $obj->last_log_id
= $this->last_log_id
;
201 $obj->force_theme
= $this->force_theme
;
202 $obj->theme
= $this->theme
;
203 $obj->applicationid
= $this->applicationid
;
204 $obj->sslverification
= $this->sslverification
;
206 if (isset($this->id
) && $this->id
> 0) {
207 $obj->id
= $this->id
;
208 return $DB->update_record('mnet_host', $obj);
210 $this->id
= $DB->insert_record('mnet_host', $obj);
211 return $this->id
> 0;
216 $this->last_connect_time
= time();
220 function set_name($newname) {
221 if (is_string($newname) && strlen($newname <= 80)) {
222 $this->name
= $newname;
228 function set_applicationid($applicationid) {
229 if (is_numeric($applicationid) && $applicationid == intval($applicationid)) {
230 $this->applicationid
= $applicationid;
237 * Load information from db about an mnet peer into this object's properties
239 * @param string $wwwroot - address of peer whose details we want to load
240 * @return bool - indication of success or failure
242 function set_wwwroot($wwwroot) {
245 $hostinfo = $DB->get_record('mnet_host', array('wwwroot'=>$wwwroot));
247 if ($hostinfo != false) {
248 $this->populate($hostinfo);
254 function set_id($id) {
257 if (clean_param($id, PARAM_INT
) != $id) {
259 $this->errmsg
[] = 'Your id ('.$id.') is not legal';
271 if ($hostinfo = $DB->get_record_sql($sql, array($id))) {
272 $this->populate($hostinfo);
279 * Several methods can be used to get an 'mnet_host' record. They all then
280 * send it to this private method to populate this object's attributes.
282 * @param object $hostinfo A database record from the mnet_host table
285 function populate($hostinfo) {
287 $this->id
= $hostinfo->id
;
288 $this->wwwroot
= $hostinfo->wwwroot
;
289 $this->ip_address
= $hostinfo->ip_address
;
290 $this->name
= $hostinfo->name
;
291 $this->deleted
= $hostinfo->deleted
;
292 $this->public_key
= $hostinfo->public_key
;
293 $this->public_key_expires
= $hostinfo->public_key_expires
;
294 $this->last_connect_time
= $hostinfo->last_connect_time
;
295 $this->last_log_id
= $hostinfo->last_log_id
;
296 $this->force_theme
= $hostinfo->force_theme
;
297 $this->theme
= $hostinfo->theme
;
298 $this->applicationid
= $hostinfo->applicationid
;
299 $this->sslverification
= $hostinfo->sslverification
;
300 $this->application
= $DB->get_record('mnet_application', array('id'=>$this->applicationid
));
301 $this->bootstrapped
= true;
304 function get_public_key() {
305 if (isset($this->public_key_ref
)) return $this->public_key_ref
;
306 $this->public_key_ref
= openssl_pkey_get_public($this->public_key
);
307 return $this->public_key_ref
;