PageRenderTime 27ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/mnet/environment.php

https://github.com/nadavkav/MoodleTAO
PHP | 191 lines | 146 code | 26 blank | 19 comment | 35 complexity | ac22dc13fe0989c5555578c47eceef11 MD5 | raw file
  1. <?php // $Id$
  2. /**
  3. * Info about the local environment, wrt RPC
  4. *
  5. * This should really be a singleton. A PHP5 Todo I guess.
  6. */
  7. class mnet_environment {
  8. var $id = 0;
  9. var $wwwroot = '';
  10. var $ip_address = '';
  11. var $public_key = '';
  12. var $public_key_expires = 0;
  13. var $last_connect_time = 0;
  14. var $last_log_id = 0;
  15. var $keypair = array();
  16. var $deleted = 0;
  17. function mnet_environment() {
  18. return true;
  19. }
  20. function init() {
  21. global $CFG;
  22. if (empty($CFG->mnet_dispatcher_mode)) {
  23. set_config('mnet_dispatcher_mode', 'off');
  24. }
  25. // Bootstrap the object data on first load.
  26. if (empty($CFG->mnet_localhost_id) ) {
  27. if (!$CFG->mnet_localhost_id = get_config(NULL, 'mnet_localhost_id')) { // Double-check db
  28. $this->wwwroot = $CFG->wwwroot;
  29. if (empty($_SERVER['SERVER_ADDR'])) {
  30. // SERVER_ADDR is only returned by Apache-like webservers
  31. $my_hostname = mnet_get_hostname_from_uri($CFG->wwwroot);
  32. $my_ip = gethostbyname($my_hostname); // Returns unmodified hostname on failure. DOH!
  33. if ($my_ip == $my_hostname) {
  34. $this->ip_address = 'UNKNOWN';
  35. } else {
  36. $this->ip_address = $my_ip;
  37. }
  38. } else {
  39. $this->ip_address = $_SERVER['SERVER_ADDR'];
  40. }
  41. if ($existingrecord = get_record('mnet_host', 'ip_address', $this->ip_address)) {
  42. $this->id = $existingrecord->id;
  43. } else { // make a new one
  44. $this->id = insert_record('mnet_host', $this, true);
  45. }
  46. set_config('mnet_localhost_id', $this->id);
  47. $this->get_keypair();
  48. }
  49. } else {
  50. $hostobject = get_record('mnet_host','id', $CFG->mnet_localhost_id);
  51. if(is_object($hostobject)) {
  52. $temparr = get_object_vars($hostobject);
  53. foreach($temparr as $key => $value) {
  54. $this->$key = $value;
  55. }
  56. unset($hostobject, $temparr);
  57. } else {
  58. return false;
  59. }
  60. // Unless this is an install/upgrade, generate the SSL keys.
  61. if(empty($this->public_key)) {
  62. $this->get_keypair();
  63. }
  64. }
  65. // We need to set up a record that represents 'all hosts'. Any rights
  66. // granted to this host will be conferred on all hosts.
  67. if (empty($CFG->mnet_all_hosts_id) ) {
  68. $hostobject = new stdClass();
  69. $hostobject->wwwroot = '';
  70. $hostobject->ip_address = '';
  71. $hostobject->public_key = '';
  72. $hostobject->public_key_expires = 0;
  73. $hostobject->last_connect_time = 0;
  74. $hostobject->last_log_id = 0;
  75. $hostobject->deleted = 0;
  76. $hostobject->name = 'All Hosts';
  77. $hostobject->id = insert_record('mnet_host',$hostobject, true);
  78. set_config('mnet_all_hosts_id', $hostobject->id);
  79. $CFG->mnet_all_hosts_id = $hostobject->id;
  80. unset($hostobject);
  81. }
  82. }
  83. function get_keypair() {
  84. // We don't generate keys on install/upgrade because we want the USER
  85. // record to have an email address, city and country already.
  86. if (!empty($_SESSION['upgraderunning'])) return true;
  87. if (!extension_loaded("openssl")) return true;
  88. if (!empty($this->keypair)) return true;
  89. $this->keypair = array();
  90. $keypair = get_field('config_plugins', 'value', 'plugin', 'mnet', 'name', 'openssl');
  91. if (!empty($keypair)) {
  92. // Explode/Implode is faster than Unserialize/Serialize
  93. list($this->keypair['certificate'], $this->keypair['keypair_PEM']) = explode('@@@@@@@@', $keypair);
  94. }
  95. if ($this->public_key_expires > time()) {
  96. $this->keypair['privatekey'] = openssl_pkey_get_private($this->keypair['keypair_PEM']);
  97. $this->keypair['publickey'] = openssl_pkey_get_public($this->keypair['certificate']);
  98. } else {
  99. // Key generation/rotation
  100. // 1. Archive the current key (if there is one).
  101. $result = get_field('config_plugins', 'value', 'plugin', 'mnet', 'name', 'openssl_history');
  102. if(empty($result)) {
  103. set_config('openssl_history', serialize(array()), 'mnet');
  104. $openssl_history = array();
  105. } else {
  106. $openssl_history = unserialize($result);
  107. }
  108. if(count($this->keypair)) {
  109. $this->keypair['expires'] = $this->public_key_expires;
  110. array_unshift($openssl_history, $this->keypair);
  111. }
  112. // 2. How many old keys do we want to keep? Use array_slice to get
  113. // rid of any we don't want
  114. $openssl_generations = get_field('config_plugins', 'value', 'plugin', 'mnet', 'name', 'openssl_generations');
  115. if(empty($openssl_generations)) {
  116. set_config('openssl_generations', 3, 'mnet');
  117. $openssl_generations = 3;
  118. }
  119. if(count($openssl_history) > $openssl_generations) {
  120. $openssl_history = array_slice($openssl_history, 0, $openssl_generations);
  121. }
  122. set_config('openssl_history', serialize($openssl_history), 'mnet');
  123. // 3. Generate fresh keys
  124. $this->replace_keys();
  125. }
  126. return true;
  127. }
  128. function replace_keys() {
  129. global $CFG;
  130. $this->keypair = array();
  131. $this->keypair = mnet_generate_keypair();
  132. $this->public_key = $this->keypair['certificate'];
  133. $this->wwwroot = $CFG->wwwroot;
  134. $details = openssl_x509_parse($this->public_key);
  135. $this->public_key_expires = $details['validTo_time_t'];
  136. if (empty($_SERVER['SERVER_ADDR'])) {
  137. // SERVER_ADDR is only returned by Apache-like webservers
  138. $my_hostname = mnet_get_hostname_from_uri($CFG->wwwroot);
  139. $my_ip = gethostbyname($my_hostname); // Returns unmodified hostname on failure. DOH!
  140. if ($my_ip == $my_hostname) {
  141. $this->ip_address = 'UNKNOWN';
  142. } else {
  143. $this->ip_address = $my_ip;
  144. }
  145. } else {
  146. $this->ip_address = $_SERVER['SERVER_ADDR'];
  147. }
  148. set_config('openssl', implode('@@@@@@@@', $this->keypair), 'mnet');
  149. update_record('mnet_host', $this);
  150. error_log('New public key has been generated. It expires ' . date('Y/m/d h:i:s', $this->public_key_expires));
  151. }
  152. function get_private_key() {
  153. if (empty($this->keypair)) $this->get_keypair();
  154. if (isset($this->keypair['privatekey'])) return $this->keypair['privatekey'];
  155. $this->keypair['privatekey'] = openssl_pkey_get_private($this->keypair['keypair_PEM']);
  156. return $this->keypair['privatekey'];
  157. }
  158. function get_public_key() {
  159. if (!isset($this->keypair)) $this->get_keypair();
  160. if (isset($this->keypair['publickey'])) return $this->keypair['publickey'];
  161. $this->keypair['publickey'] = openssl_pkey_get_public($this->keypair['certificate']);
  162. return $this->keypair['publickey'];
  163. }
  164. }
  165. ?>