mnet/lib: Bugfix: mnet_generate_keypair: Declare USER as global
[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);
40 foreach(get_object_vars($temparr) as $key => $value) {
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() {
75 if (!empty($this->keypair)) return true;
76 if ($result = get_record_select('config', " name = 'openssl'")) {
77 $this->keypair = unserialize($result->value);
78 $this->keypair['privatekey'] = openssl_pkey_get_private($this->keypair['keypair_PEM']);
79 $this->keypair['publickey'] = openssl_pkey_get_public($this->keypair['certificate']);
80 } else {
81 $this->keypair = mnet_generate_keypair();
f32689ad 82 $this->public_key = $this->keypair['certificate'];
83 $details = openssl_x509_parse($this->public_key);
84 $this->public_key_expires = $details['validTo_time_t'];
85
86 update_record('mnet_host', $this);
71558f85 87 }
88 return true;
89 }
90
91 function get_private_key() {
92 if (empty($this->keypair)) $this->get_keypair();
93 if (isset($this->keypair['privatekey'])) return $this->keypair['privatekey'];
94 $this->keypair['privatekey'] = openssl_pkey_get_private($this->keypair['keypair_PEM']);
95 return $this->keypair['privatekey'];
96 }
97
98 function get_public_key() {
99 if (!isset($this->keypair)) $this->get_keypair();
100 if (isset($this->keypair['publickey'])) return $this->keypair['publickey'];
101 $this->keypair['publickey'] = openssl_pkey_get_public($this->keypair['certificate']);
102 return $this->keypair['publickey'];
103 }
104
105 /**
106 * Note that the openssl_sign function computes the sha1 hash, and then
107 * signs the hash.
108 */
109 function sign_message($message) {
110 $bool = openssl_sign($message, $signature, $this->get_private_key());
111 return $signature;
112 }
113}
114
115?>