MDL-9137 Removed hidden field from grade_categories and added deleted field to grade_...
[moodle-pu.git] / mnet / environment.php
blob6fc97a2ecfbb328669ca9866214f871e1a0e94cc
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 if(empty($_SERVER['SERVER_ADDR'])) {
36 // SERVER_ADDR is only returned by Apache-like webservers
37 $my_hostname = mnet_get_hostname_from_uri($CFG->wwwroot);
38 $my_ip = gethostbyname($my_hostname); // Returns unmodified hostname on failure. DOH!
39 if($my_ip == $my_hostname) {
40 $this->ip_address = 'UNKNOWN';
41 } else {
42 $this->ip_address = $my_ip;
44 } else {
45 $this->ip_address = $_SERVER['SERVER_ADDR'];
47 $this->id = insert_record('mnet_host', $this, true);
49 set_config('mnet_localhost_id', $this->id);
50 $this->get_keypair();
51 } else {
52 $hostobject = get_record('mnet_host','id', $CFG->mnet_localhost_id);
53 if(is_object($hostobject)) {
54 $temparr = get_object_vars($hostobject);
55 foreach($temparr as $key => $value) {
56 $this->$key = $value;
58 unset($hostobject, $temparr);
59 } else {
60 return false;
63 // Unless this is an install/upgrade, generate the SSL keys.
64 if(empty($this->public_key)) {
65 $this->get_keypair();
69 // We need to set up a record that represents 'all hosts'. Any rights
70 // granted to this host will be conferred on all hosts.
71 if (empty($CFG->mnet_all_hosts_id) ) {
72 $hostobject = new stdClass();
73 $hostobject->wwwroot = '';
74 $hostobject->ip_address = '';
75 $hostobject->public_key = '';
76 $hostobject->public_key_expires = '';
77 $hostobject->last_connect_time = '0';
78 $hostobject->last_log_id = '0';
79 $hostobject->deleted = 0;
80 $hostobject->name = 'All Hosts';
82 $hostobject->id = insert_record('mnet_host',$hostobject, true);
83 set_config('mnet_all_hosts_id', $hostobject->id);
84 $CFG->mnet_all_hosts_id = $hostobject->id;
85 unset($hostobject);
89 function get_keypair() {
90 // We don't generate keys on install/upgrade because we want the USER
91 // record to have an email address, city and country already.
92 if (!empty($_SESSION['upgraderunning'])) return true;
93 if (!extension_loaded("openssl")) return true;
94 if (!empty($this->keypair)) return true;
96 $this->keypair = array();
97 $keypair = get_field('config_plugins', 'value', 'plugin', 'mnet', 'name', 'openssl');
99 if (!empty($keypair)) {
100 // Explode/Implode is faster than Unserialize/Serialize
101 list($this->keypair['certificate'], $this->keypair['keypair_PEM']) = explode('@@@@@@@@', $keypair);
104 if ($this->public_key_expires > time()) {
105 $this->keypair['privatekey'] = openssl_pkey_get_private($this->keypair['keypair_PEM']);
106 $this->keypair['publickey'] = openssl_pkey_get_public($this->keypair['certificate']);
107 } else {
108 // Key generation/rotation
110 // 1. Archive the current key (if there is one).
111 $result = get_field('config_plugins', 'value', 'plugin', 'mnet', 'name', 'openssl_history');
112 if(empty($result)) {
113 set_config('openssl_history', serialize(array()), 'mnet');
114 $openssl_history = array();
115 } else {
116 $openssl_history = unserialize($result);
119 if(count($this->keypair)) {
120 $this->keypair['expires'] = $this->public_key_expires;
121 array_unshift($openssl_history, $this->keypair);
124 // 2. How many old keys do we want to keep? Use array_slice to get
125 // rid of any we don't want
126 $openssl_generations = get_field('config_plugins', 'value', 'plugin', 'mnet', 'name', 'openssl_generations');
127 if(empty($openssl_generations)) {
128 set_config('openssl_generations', 3, 'mnet');
129 $openssl_generations = 3;
132 if(count($openssl_history) > $openssl_generations) {
133 $openssl_history = array_slice($openssl_history, 0, $openssl_generations);
136 set_config('openssl_history', serialize($openssl_history), 'mnet');
138 // 3. Generate fresh keys
139 $this->replace_keys();
141 return true;
144 function replace_keys() {
145 $this->keypair = array();
146 $this->keypair = mnet_generate_keypair();
147 $this->public_key = $this->keypair['certificate'];
148 $details = openssl_x509_parse($this->public_key);
149 $this->public_key_expires = $details['validTo_time_t'];
151 set_config('openssl', implode('@@@@@@@@', $this->keypair), 'mnet');
153 update_record('mnet_host', $this);
156 function get_private_key() {
157 if (empty($this->keypair)) $this->get_keypair();
158 if (isset($this->keypair['privatekey'])) return $this->keypair['privatekey'];
159 $this->keypair['privatekey'] = openssl_pkey_get_private($this->keypair['keypair_PEM']);
160 return $this->keypair['privatekey'];
163 function get_public_key() {
164 if (!isset($this->keypair)) $this->get_keypair();
165 if (isset($this->keypair['publickey'])) return $this->keypair['publickey'];
166 $this->keypair['publickey'] = openssl_pkey_get_public($this->keypair['certificate']);
167 return $this->keypair['publickey'];
171 * Note that the openssl_sign function computes the sha1 hash, and then
172 * signs the hash.
174 function sign_message($message) {
175 $bool = openssl_sign($message, $signature, $this->get_private_key());
176 return $signature;