Automatic installer.php lang files by installer_builder (20070105)
[moodle.git] / mnet / environment.php
CommitLineData
71558f85 1<?php
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() {
25 global $CFG;
26
27 // Bootstrap the object data on first load.
28 if (empty($CFG->mnet_localhost_id) ) {
71558f85 29
f32689ad 30 $this->wwwroot = $CFG->wwwroot;
31 $this->ip_address = $_SERVER['SERVER_ADDR'];
32 $this->id = insert_record('mnet_host', $this, true);
71558f85 33
34 set_config('mnet_localhost_id', $this->id);
f32689ad 35 $this->get_keypair();
71558f85 36 } else {
37 $hostobject = get_record('mnet_host','id', $CFG->mnet_localhost_id);
f32689ad 38 if(is_object($hostobject)) {
39 $temparr = get_object_vars($hostobject);
8c99567e 40 foreach($temparr as $key => $value) {
f32689ad 41 $this->$key = $value;
42 }
43 unset($hostobject, $temparr);
44 } else {
45 return false;
71558f85 46 }
47
f32689ad 48 // Unless this is an install/upgrade, generate the SSL keys.
49 if(empty($this->public_key)) {
50 $this->get_keypair();
51 }
71558f85 52 }
53
54 // We need to set up a record that represents 'all hosts'. Any rights
55 // granted to this host will be conferred on all hosts.
56 if (empty($CFG->mnet_all_hosts_id) ) {
57 $hostobject = new stdClass();
58 $hostobject->wwwroot = '';
59 $hostobject->ip_address = '';
60 $hostobject->public_key = '';
61 $hostobject->public_key_expires = '';
62 $hostobject->last_connect_time = '0';
63 $hostobject->last_log_id = '0';
64 $hostobject->deleted = 0;
65 $hostobject->name = 'All Hosts';
66
67 $hostobject->id = insert_record('mnet_host',$hostobject, true);
68 set_config('mnet_all_hosts_id', $hostobject->id);
69 $CFG->mnet_all_hosts_id = $hostobject->id;
70 unset($hostobject);
71 }
72 }
73
74 function get_keypair() {
735c7beb 75 // We don't generate keys on install/upgrade because we want the USER
76 // record to have an email address, city and country already.
77 if (!empty($_SESSION['upgraderunning'])) return true;
722f3f05 78 if (!extension_loaded("openssl")) return true;
71558f85 79 if (!empty($this->keypair)) return true;
735c7beb 80
81 $this->keypair = array();
82 $keypair = get_field('config_plugins', 'value', 'plugin', 'mnet', 'name', 'openssl');
83
84 if (!empty($keypair)) {
85 // Explode/Implode is faster than Unserialize/Serialize
8c99567e 86 list($this->keypair['certificate'], $this->keypair['keypair_PEM']) = explode('@@@@@@@@', $keypair);
735c7beb 87 }
88
89 if ($this->public_key_expires > time()) {
722f3f05 90 $this->keypair['privatekey'] = XYZopenssl_pkey_get_private($this->keypair['keypair_PEM']);
91 $this->keypair['publickey'] = XYZopenssl_pkey_get_public($this->keypair['certificate']);
71558f85 92 } else {
735c7beb 93 // Key generation/rotation
94
95 // 1. Archive the current key (if there is one).
96 $result = get_field('config_plugins', 'value', 'plugin', 'mnet', 'name', 'openssl_history');
97 if(empty($result)) {
98 set_config('openssl_history', serialize(array()), 'mnet');
99 $openssl_history = array();
100 } else {
101 $openssl_history = unserialize($result);
102 }
103
104 if(count($this->keypair)) {
105 $this->keypair['expires'] = $this->public_key_expires;
106 array_unshift($openssl_history, $this->keypair);
107 }
108
109 // 2. How many old keys do we want to keep? Use array_slice to get
110 // rid of any we don't want
111 $openssl_generations = get_field('config_plugins', 'value', 'plugin', 'mnet', 'name', 'openssl_generations');
112 if(empty($openssl_generations)) {
113 set_config('openssl_generations', 3, 'mnet');
114 $openssl_generations = 3;
115 }
116
117 if(count($openssl_history) > $openssl_generations) {
118 $openssl_history = array_slice($openssl_history, 0, $openssl_generations);
119 }
120
121 set_config('openssl_history', serialize($openssl_history), 'mnet');
122
123 // 3. Generate fresh keys
e2d4f75c 124 $this->replace_keys();
71558f85 125 }
126 return true;
127 }
128
e2d4f75c 129 function replace_keys() {
130 $this->keypair = array();
131 $this->keypair = mnet_generate_keypair();
132 $this->public_key = $this->keypair['certificate'];
722f3f05 133 $details = XYZopenssl_x509_parse($this->public_key);
e2d4f75c 134 $this->public_key_expires = $details['validTo_time_t'];
135
136 set_config('openssl', implode('@@@@@@@@', $this->keypair), 'mnet');
137
138 update_record('mnet_host', $this);
139 }
140
71558f85 141 function get_private_key() {
142 if (empty($this->keypair)) $this->get_keypair();
143 if (isset($this->keypair['privatekey'])) return $this->keypair['privatekey'];
722f3f05 144 $this->keypair['privatekey'] = XYZopenssl_pkey_get_private($this->keypair['keypair_PEM']);
71558f85 145 return $this->keypair['privatekey'];
146 }
147
148 function get_public_key() {
149 if (!isset($this->keypair)) $this->get_keypair();
150 if (isset($this->keypair['publickey'])) return $this->keypair['publickey'];
722f3f05 151 $this->keypair['publickey'] = XYZopenssl_pkey_get_public($this->keypair['certificate']);
71558f85 152 return $this->keypair['publickey'];
153 }
154
155 /**
156 * Note that the openssl_sign function computes the sha1 hash, and then
157 * signs the hash.
158 */
159 function sign_message($message) {
722f3f05 160 $bool = XYZopenssl_sign($message, $signature, $this->get_private_key());
71558f85 161 return $signature;
162 }
163}
164
165?>