Changing one generic class-name to a glossary specific one.
[pfb-moodle.git] / mnet / environment.php
blob1fd7dda4c322ee102c7e3afe544df63b7968f9e8
1 <?php
2 /**
3 * Info about the local environment, wrt RPC
5 * This should really be a singleton. A PHP5 Todo I guess.
6 */
8 class mnet_environment {
10 var $id = 0;
11 var $wwwroot = '';
12 var $ip_address = '';
13 var $public_key = '';
14 var $public_key_expires = 0;
15 var $last_connect_time = 0;
16 var $last_log_id = 0;
17 var $keypair = array();
18 var $deleted = 0;
20 function mnet_environment() {
21 return true;
24 function init() {
25 global $CFG;
27 if (empty($CFG->mnet_dispatcher_mode)) {
28 set_config('mnet_dispatcher_mode', 'off');
31 // Bootstrap the object data on first load.
32 if (empty($CFG->mnet_localhost_id) ) {
34 $this->wwwroot = $CFG->wwwroot;
35 $this->ip_address = $_SERVER['SERVER_ADDR'];
36 $this->id = insert_record('mnet_host', $this, true);
38 set_config('mnet_localhost_id', $this->id);
39 $this->get_keypair();
40 } else {
41 $hostobject = get_record('mnet_host','id', $CFG->mnet_localhost_id);
42 if(is_object($hostobject)) {
43 $temparr = get_object_vars($hostobject);
44 foreach($temparr as $key => $value) {
45 $this->$key = $value;
47 unset($hostobject, $temparr);
48 } else {
49 return false;
52 // Unless this is an install/upgrade, generate the SSL keys.
53 if(empty($this->public_key)) {
54 $this->get_keypair();
58 // We need to set up a record that represents 'all hosts'. Any rights
59 // granted to this host will be conferred on all hosts.
60 if (empty($CFG->mnet_all_hosts_id) ) {
61 $hostobject = new stdClass();
62 $hostobject->wwwroot = '';
63 $hostobject->ip_address = '';
64 $hostobject->public_key = '';
65 $hostobject->public_key_expires = '';
66 $hostobject->last_connect_time = '0';
67 $hostobject->last_log_id = '0';
68 $hostobject->deleted = 0;
69 $hostobject->name = 'All Hosts';
71 $hostobject->id = insert_record('mnet_host',$hostobject, true);
72 set_config('mnet_all_hosts_id', $hostobject->id);
73 $CFG->mnet_all_hosts_id = $hostobject->id;
74 unset($hostobject);
78 function get_keypair() {
79 // We don't generate keys on install/upgrade because we want the USER
80 // record to have an email address, city and country already.
81 if (!empty($_SESSION['upgraderunning'])) return true;
82 if (!extension_loaded("openssl")) return true;
83 if (!empty($this->keypair)) return true;
85 $this->keypair = array();
86 $keypair = get_field('config_plugins', 'value', 'plugin', 'mnet', 'name', 'openssl');
88 if (!empty($keypair)) {
89 // Explode/Implode is faster than Unserialize/Serialize
90 list($this->keypair['certificate'], $this->keypair['keypair_PEM']) = explode('@@@@@@@@', $keypair);
93 if ($this->public_key_expires > time()) {
94 $this->keypair['privatekey'] = openssl_pkey_get_private($this->keypair['keypair_PEM']);
95 $this->keypair['publickey'] = openssl_pkey_get_public($this->keypair['certificate']);
96 } else {
97 // Key generation/rotation
99 // 1. Archive the current key (if there is one).
100 $result = get_field('config_plugins', 'value', 'plugin', 'mnet', 'name', 'openssl_history');
101 if(empty($result)) {
102 set_config('openssl_history', serialize(array()), 'mnet');
103 $openssl_history = array();
104 } else {
105 $openssl_history = unserialize($result);
108 if(count($this->keypair)) {
109 $this->keypair['expires'] = $this->public_key_expires;
110 array_unshift($openssl_history, $this->keypair);
113 // 2. How many old keys do we want to keep? Use array_slice to get
114 // rid of any we don't want
115 $openssl_generations = get_field('config_plugins', 'value', 'plugin', 'mnet', 'name', 'openssl_generations');
116 if(empty($openssl_generations)) {
117 set_config('openssl_generations', 3, 'mnet');
118 $openssl_generations = 3;
121 if(count($openssl_history) > $openssl_generations) {
122 $openssl_history = array_slice($openssl_history, 0, $openssl_generations);
125 set_config('openssl_history', serialize($openssl_history), 'mnet');
127 // 3. Generate fresh keys
128 $this->replace_keys();
130 return true;
133 function replace_keys() {
134 $this->keypair = array();
135 $this->keypair = mnet_generate_keypair();
136 $this->public_key = $this->keypair['certificate'];
137 $details = openssl_x509_parse($this->public_key);
138 $this->public_key_expires = $details['validTo_time_t'];
140 set_config('openssl', implode('@@@@@@@@', $this->keypair), 'mnet');
142 update_record('mnet_host', $this);
145 function get_private_key() {
146 if (empty($this->keypair)) $this->get_keypair();
147 if (isset($this->keypair['privatekey'])) return $this->keypair['privatekey'];
148 $this->keypair['privatekey'] = openssl_pkey_get_private($this->keypair['keypair_PEM']);
149 return $this->keypair['privatekey'];
152 function get_public_key() {
153 if (!isset($this->keypair)) $this->get_keypair();
154 if (isset($this->keypair['publickey'])) return $this->keypair['publickey'];
155 $this->keypair['publickey'] = openssl_pkey_get_public($this->keypair['certificate']);
156 return $this->keypair['publickey'];
160 * Note that the openssl_sign function computes the sha1 hash, and then
161 * signs the hash.
163 function sign_message($message) {
164 $bool = openssl_sign($message, $signature, $this->get_private_key());
165 return $signature;