grade items in xml export should not be checkable if they have no idnumbers
[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
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) ) {
71558f85 33
f32689ad 34 $this->wwwroot = $CFG->wwwroot;
dd9f1b7d 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 }
44 } else {
45 $this->ip_address = $_SERVER['SERVER_ADDR'];
46 }
f32689ad 47 $this->id = insert_record('mnet_host', $this, true);
71558f85 48
49 set_config('mnet_localhost_id', $this->id);
f32689ad 50 $this->get_keypair();
71558f85 51 } else {
52 $hostobject = get_record('mnet_host','id', $CFG->mnet_localhost_id);
f32689ad 53 if(is_object($hostobject)) {
54 $temparr = get_object_vars($hostobject);
8c99567e 55 foreach($temparr as $key => $value) {
f32689ad 56 $this->$key = $value;
57 }
58 unset($hostobject, $temparr);
59 } else {
60 return false;
71558f85 61 }
62
f32689ad 63 // Unless this is an install/upgrade, generate the SSL keys.
64 if(empty($this->public_key)) {
65 $this->get_keypair();
66 }
71558f85 67 }
68
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';
81
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);
86 }
87 }
88
89 function get_keypair() {
735c7beb 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;
722f3f05 93 if (!extension_loaded("openssl")) return true;
71558f85 94 if (!empty($this->keypair)) return true;
735c7beb 95
96 $this->keypair = array();
97 $keypair = get_field('config_plugins', 'value', 'plugin', 'mnet', 'name', 'openssl');
98
99 if (!empty($keypair)) {
100 // Explode/Implode is faster than Unserialize/Serialize
8c99567e 101 list($this->keypair['certificate'], $this->keypair['keypair_PEM']) = explode('@@@@@@@@', $keypair);
735c7beb 102 }
103
104 if ($this->public_key_expires > time()) {
08cb427a 105 $this->keypair['privatekey'] = openssl_pkey_get_private($this->keypair['keypair_PEM']);
106 $this->keypair['publickey'] = openssl_pkey_get_public($this->keypair['certificate']);
71558f85 107 } else {
735c7beb 108 // Key generation/rotation
109
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);
117 }
118
119 if(count($this->keypair)) {
120 $this->keypair['expires'] = $this->public_key_expires;
121 array_unshift($openssl_history, $this->keypair);
122 }
123
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;
130 }
131
132 if(count($openssl_history) > $openssl_generations) {
133 $openssl_history = array_slice($openssl_history, 0, $openssl_generations);
134 }
135
136 set_config('openssl_history', serialize($openssl_history), 'mnet');
137
138 // 3. Generate fresh keys
e2d4f75c 139 $this->replace_keys();
71558f85 140 }
141 return true;
142 }
143
e2d4f75c 144 function replace_keys() {
145 $this->keypair = array();
146 $this->keypair = mnet_generate_keypair();
147 $this->public_key = $this->keypair['certificate'];
08cb427a 148 $details = openssl_x509_parse($this->public_key);
e2d4f75c 149 $this->public_key_expires = $details['validTo_time_t'];
150
151 set_config('openssl', implode('@@@@@@@@', $this->keypair), 'mnet');
152
153 update_record('mnet_host', $this);
154 }
155
71558f85 156 function get_private_key() {
157 if (empty($this->keypair)) $this->get_keypair();
158 if (isset($this->keypair['privatekey'])) return $this->keypair['privatekey'];
08cb427a 159 $this->keypair['privatekey'] = openssl_pkey_get_private($this->keypair['keypair_PEM']);
71558f85 160 return $this->keypair['privatekey'];
161 }
162
163 function get_public_key() {
164 if (!isset($this->keypair)) $this->get_keypair();
165 if (isset($this->keypair['publickey'])) return $this->keypair['publickey'];
08cb427a 166 $this->keypair['publickey'] = openssl_pkey_get_public($this->keypair['certificate']);
71558f85 167 return $this->keypair['publickey'];
168 }
169
170 /**
171 * Note that the openssl_sign function computes the sha1 hash, and then
172 * signs the hash.
173 */
174 function sign_message($message) {
08cb427a 175 $bool = openssl_sign($message, $signature, $this->get_private_key());
71558f85 176 return $signature;
177 }
178}
179
180?>