mnet: Some very minor style (etc) fixes
[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();
18
19 function mnet_environment() {
20 return true;
21 }
22
23 function init() {
24 global $CFG;
25
26 // Bootstrap the object data on first load.
27 if (empty($CFG->mnet_localhost_id) ) {
28 $this->get_keypair();
29
30 $hostobject = new stdClass();
31 $hostobject->wwwroot = $CFG->wwwroot;
32 $hostobject->ip_address = $_SERVER['SERVER_ADDR'];
33 $hostobject->public_key = $this->keypair['certificate'];
34 $hostobject->public_key_expires = '';
35 $hostobject->last_connect_time = '0';
36 $hostobject->last_log_id = '0';
37 $hostobject->deleted = 0;
38
39 $this->id = insert_record('mnet_host',$hostobject, true);
40
41 $temparr = (array)get_object_vars($hostobject);
42
43 foreach($temparr as $key => $value) {
44 $this->$key = $value;
45 }
46
47 unset($temparr, $hostobject);
48
49 set_config('mnet_localhost_id', $this->id);
50 $CFG->mnet_localhost_id = $this->id;
51 } else {
52 $hostobject = get_record('mnet_host','id', $CFG->mnet_localhost_id);
53 $temparr = (array)get_object_vars($hostobject);
54
55 foreach($temparr as $key => $value) {
56 $this->$key = $value;
57 }
58
59 unset($temparr, $hostobject);
60 }
61
62 // We need to set up a record that represents 'all hosts'. Any rights
63 // granted to this host will be conferred on all hosts.
64 if (empty($CFG->mnet_all_hosts_id) ) {
65 $hostobject = new stdClass();
66 $hostobject->wwwroot = '';
67 $hostobject->ip_address = '';
68 $hostobject->public_key = '';
69 $hostobject->public_key_expires = '';
70 $hostobject->last_connect_time = '0';
71 $hostobject->last_log_id = '0';
72 $hostobject->deleted = 0;
73 $hostobject->name = 'All Hosts';
74
75 $hostobject->id = insert_record('mnet_host',$hostobject, true);
76 set_config('mnet_all_hosts_id', $hostobject->id);
77 $CFG->mnet_all_hosts_id = $hostobject->id;
78 unset($hostobject);
79 }
80 }
81
82 function get_keypair() {
83 if (!empty($this->keypair)) return true;
84 if ($result = get_record_select('config', " name = 'openssl'")) {
85 $this->keypair = unserialize($result->value);
86 $this->keypair['privatekey'] = openssl_pkey_get_private($this->keypair['keypair_PEM']);
87 $this->keypair['publickey'] = openssl_pkey_get_public($this->keypair['certificate']);
88 } else {
89 $this->keypair = mnet_generate_keypair();
90 }
91 return true;
92 }
93
94 function get_private_key() {
95 if (empty($this->keypair)) $this->get_keypair();
96 if (isset($this->keypair['privatekey'])) return $this->keypair['privatekey'];
97 $this->keypair['privatekey'] = openssl_pkey_get_private($this->keypair['keypair_PEM']);
98 return $this->keypair['privatekey'];
99 }
100
101 function get_public_key() {
102 if (!isset($this->keypair)) $this->get_keypair();
103 if (isset($this->keypair['publickey'])) return $this->keypair['publickey'];
104 $this->keypair['publickey'] = openssl_pkey_get_public($this->keypair['certificate']);
105 return $this->keypair['publickey'];
106 }
107
108 /**
109 * Note that the openssl_sign function computes the sha1 hash, and then
110 * signs the hash.
111 */
112 function sign_message($message) {
113 $bool = openssl_sign($message, $signature, $this->get_private_key());
114 return $signature;
115 }
116}
117
118?>