MDL-14905 towards functional DB tests
[moodle.git] / mnet / environment.php
CommitLineData
b16393cb 1<?php // $Id$
71558f85 2/**
3 * Info about the local environment, wrt RPC
4 *
5 * This should really be a singleton. A PHP5 Todo I guess.
6 */
7
8class mnet_environment {
9
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();
f32689ad 18 var $deleted = 0;
71558f85 19
20 function mnet_environment() {
21 return true;
22 }
23
24 function init() {
cc38ff5d 25 global $CFG, $DB;
71558f85 26
36e6379e 27 if (empty($CFG->mnet_dispatcher_mode)) {
28 set_config('mnet_dispatcher_mode', 'off');
29 }
30
71558f85 31 // Bootstrap the object data on first load.
32 if (empty($CFG->mnet_localhost_id) ) {
94783496 33 if (!$CFG->mnet_localhost_id = get_config(NULL, 'mnet_localhost_id')) { // Double-check db
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;
43 }
dd9f1b7d 44 } else {
94783496 45 $this->ip_address = $_SERVER['SERVER_ADDR'];
dd9f1b7d 46 }
71558f85 47
cc38ff5d 48 if ($existingrecord = $DB->get_record('mnet_host', array('ip_address'=>$this->ip_address))) {
94783496 49 $this->id = $existingrecord->id;
50 } else { // make a new one
cc38ff5d 51 $this->id = $DB->insert_record('mnet_host', $this);
94783496 52 }
53
54 set_config('mnet_localhost_id', $this->id);
55 $this->get_keypair();
56 }
71558f85 57 } else {
cc38ff5d 58 $hostobject = $DB->get_record('mnet_host', array('id'=>$CFG->mnet_localhost_id));
f32689ad 59 if(is_object($hostobject)) {
60 $temparr = get_object_vars($hostobject);
8c99567e 61 foreach($temparr as $key => $value) {
f32689ad 62 $this->$key = $value;
63 }
64 unset($hostobject, $temparr);
65 } else {
66 return false;
71558f85 67 }
68
f32689ad 69 // Unless this is an install/upgrade, generate the SSL keys.
70 if(empty($this->public_key)) {
71 $this->get_keypair();
72 }
71558f85 73 }
74
75 // We need to set up a record that represents 'all hosts'. Any rights
76 // granted to this host will be conferred on all hosts.
77 if (empty($CFG->mnet_all_hosts_id) ) {
78 $hostobject = new stdClass();
79 $hostobject->wwwroot = '';
80 $hostobject->ip_address = '';
81 $hostobject->public_key = '';
82 $hostobject->public_key_expires = '';
83 $hostobject->last_connect_time = '0';
84 $hostobject->last_log_id = '0';
85 $hostobject->deleted = 0;
86 $hostobject->name = 'All Hosts';
87
cc38ff5d 88 $hostobject->id = $DB->insert_record('mnet_host',$hostobject);
71558f85 89 set_config('mnet_all_hosts_id', $hostobject->id);
90 $CFG->mnet_all_hosts_id = $hostobject->id;
91 unset($hostobject);
92 }
93 }
94
95 function get_keypair() {
a5d424df 96 global $DB;
97
735c7beb 98 // We don't generate keys on install/upgrade because we want the USER
99 // record to have an email address, city and country already.
100 if (!empty($_SESSION['upgraderunning'])) return true;
722f3f05 101 if (!extension_loaded("openssl")) return true;
71558f85 102 if (!empty($this->keypair)) return true;
735c7beb 103
104 $this->keypair = array();
a5d424df 105 $keypair = $DB->get_field('config_plugins', 'value', array('plugin'=>'mnet', 'name'=>'openssl'));
735c7beb 106
107 if (!empty($keypair)) {
108 // Explode/Implode is faster than Unserialize/Serialize
8c99567e 109 list($this->keypair['certificate'], $this->keypair['keypair_PEM']) = explode('@@@@@@@@', $keypair);
735c7beb 110 }
111
112 if ($this->public_key_expires > time()) {
08cb427a 113 $this->keypair['privatekey'] = openssl_pkey_get_private($this->keypair['keypair_PEM']);
114 $this->keypair['publickey'] = openssl_pkey_get_public($this->keypair['certificate']);
71558f85 115 } else {
735c7beb 116 // Key generation/rotation
117
118 // 1. Archive the current key (if there is one).
a5d424df 119 $result = $DB->get_field('config_plugins', 'value', array('plugin'=>'mnet', 'name'=>'openssl_history'));
735c7beb 120 if(empty($result)) {
121 set_config('openssl_history', serialize(array()), 'mnet');
122 $openssl_history = array();
123 } else {
124 $openssl_history = unserialize($result);
125 }
126
127 if(count($this->keypair)) {
128 $this->keypair['expires'] = $this->public_key_expires;
129 array_unshift($openssl_history, $this->keypair);
130 }
131
132 // 2. How many old keys do we want to keep? Use array_slice to get
133 // rid of any we don't want
a5d424df 134 $openssl_generations = $DB->get_field('config_plugins', 'value', array('plugin'=>'mnet', 'name'=>'openssl_generations'));
735c7beb 135 if(empty($openssl_generations)) {
136 set_config('openssl_generations', 3, 'mnet');
137 $openssl_generations = 3;
138 }
139
140 if(count($openssl_history) > $openssl_generations) {
141 $openssl_history = array_slice($openssl_history, 0, $openssl_generations);
142 }
143
144 set_config('openssl_history', serialize($openssl_history), 'mnet');
145
146 // 3. Generate fresh keys
e2d4f75c 147 $this->replace_keys();
71558f85 148 }
149 return true;
150 }
151
e2d4f75c 152 function replace_keys() {
cc38ff5d 153 global $DB;
154
e2d4f75c 155 $this->keypair = array();
156 $this->keypair = mnet_generate_keypair();
157 $this->public_key = $this->keypair['certificate'];
08cb427a 158 $details = openssl_x509_parse($this->public_key);
e2d4f75c 159 $this->public_key_expires = $details['validTo_time_t'];
160
161 set_config('openssl', implode('@@@@@@@@', $this->keypair), 'mnet');
162
cc38ff5d 163 $DB->update_record('mnet_host', $this);
309eb040 164 error_log('New public key has been generated. It expires ' . date('Y/m/d h:i:s', $this->public_key_expires));
e2d4f75c 165 }
166
71558f85 167 function get_private_key() {
168 if (empty($this->keypair)) $this->get_keypair();
169 if (isset($this->keypair['privatekey'])) return $this->keypair['privatekey'];
08cb427a 170 $this->keypair['privatekey'] = openssl_pkey_get_private($this->keypair['keypair_PEM']);
71558f85 171 return $this->keypair['privatekey'];
172 }
173
174 function get_public_key() {
175 if (!isset($this->keypair)) $this->get_keypair();
176 if (isset($this->keypair['publickey'])) return $this->keypair['publickey'];
08cb427a 177 $this->keypair['publickey'] = openssl_pkey_get_public($this->keypair['certificate']);
71558f85 178 return $this->keypair['publickey'];
179 }
71558f85 180}
181
182?>