PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/mnet/environment.php

https://github.com/mackensen/moodle
PHP | 167 lines | 122 code | 26 blank | 19 comment | 27 complexity | a0ea90dc6b89df0df29ca16b2e2c3cd8 MD5 | raw file
  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. 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 init() {
  18. global $CFG, $DB;
  19. // Bootstrap the object data on first load.
  20. if (!$hostobject = $DB->get_record('mnet_host', array('id'=>$CFG->mnet_localhost_id))) {
  21. return false;
  22. }
  23. $temparr = get_object_vars($hostobject);
  24. foreach($temparr as $key => $value) {
  25. $this->$key = $value;
  26. }
  27. unset($hostobject, $temparr);
  28. // Unless this is an install/upgrade, generate the SSL keys.
  29. if (empty($this->public_key)) {
  30. $this->get_keypair();
  31. }
  32. // We need to set up a record that represents 'all hosts'. Any rights
  33. // granted to this host will be conferred on all hosts.
  34. if (empty($CFG->mnet_all_hosts_id) ) {
  35. $hostobject = new stdClass();
  36. $hostobject->wwwroot = '';
  37. $hostobject->ip_address = '';
  38. $hostobject->public_key = '';
  39. $hostobject->public_key_expires = 0;
  40. $hostobject->last_connect_time = 0;
  41. $hostobject->last_log_id = 0;
  42. $hostobject->deleted = 0;
  43. $hostobject->name = 'All Hosts';
  44. $hostobject->id = $DB->insert_record('mnet_host',$hostobject);
  45. set_config('mnet_all_hosts_id', $hostobject->id);
  46. $CFG->mnet_all_hosts_id = $hostobject->id;
  47. unset($hostobject);
  48. }
  49. }
  50. function get_keypair() {
  51. global $DB, $CFG;
  52. // We don't generate keys on install/upgrade because we want the USER
  53. // record to have an email address, city and country already.
  54. if (during_initial_install()) return true;
  55. if ($CFG->mnet_dispatcher_mode == 'off') return true;
  56. if (!extension_loaded("openssl")) return true;
  57. if (!empty($this->keypair)) return true;
  58. $this->keypair = array();
  59. $keypair = get_config('mnet', 'openssl');
  60. if (!empty($keypair)) {
  61. // Explode/Implode is faster than Unserialize/Serialize
  62. list($this->keypair['certificate'], $this->keypair['keypair_PEM']) = explode('@@@@@@@@', $keypair);
  63. }
  64. if ($this->public_key_expires > time()) {
  65. $this->keypair['privatekey'] = openssl_pkey_get_private($this->keypair['keypair_PEM']);
  66. $this->keypair['publickey'] = openssl_pkey_get_public($this->keypair['certificate']);
  67. } else {
  68. // Key generation/rotation
  69. // 1. Archive the current key (if there is one).
  70. $result = get_config('mnet', 'openssl_history');
  71. if(empty($result)) {
  72. set_config('openssl_history', serialize(array()), 'mnet');
  73. $openssl_history = array();
  74. } else {
  75. $openssl_history = unserialize($result);
  76. }
  77. if(count($this->keypair)) {
  78. $this->keypair['expires'] = $this->public_key_expires;
  79. array_unshift($openssl_history, $this->keypair);
  80. }
  81. // 2. How many old keys do we want to keep? Use array_slice to get
  82. // rid of any we don't want
  83. $openssl_generations = get_config('mnet', 'openssl_generations');
  84. if(empty($openssl_generations)) {
  85. set_config('openssl_generations', 3, 'mnet');
  86. $openssl_generations = 3;
  87. }
  88. if(count($openssl_history) > $openssl_generations) {
  89. $openssl_history = array_slice($openssl_history, 0, $openssl_generations);
  90. }
  91. set_config('openssl_history', serialize($openssl_history), 'mnet');
  92. // 3. Generate fresh keys
  93. $this->replace_keys();
  94. }
  95. return true;
  96. }
  97. function replace_keys() {
  98. global $DB, $CFG;
  99. $keypair = mnet_generate_keypair();
  100. if (empty($keypair)) {
  101. error_log('Can not generate keypair, sorry');
  102. return;
  103. }
  104. $this->keypair = array();
  105. $this->keypair = $keypair;
  106. $this->public_key = $this->keypair['certificate'];
  107. $details = openssl_x509_parse($this->public_key);
  108. $this->public_key_expires = $details['validTo_time_t'];
  109. $this->wwwroot = $CFG->wwwroot;
  110. if (empty($_SERVER['SERVER_ADDR'])) {
  111. // SERVER_ADDR is only returned by Apache-like webservers
  112. $my_hostname = mnet_get_hostname_from_uri($CFG->wwwroot);
  113. $my_ip = gethostbyname($my_hostname); // Returns unmodified hostname on failure. DOH!
  114. if ($my_ip == $my_hostname) {
  115. $this->ip_address = 'UNKNOWN';
  116. } else {
  117. $this->ip_address = $my_ip;
  118. }
  119. } else {
  120. $this->ip_address = $_SERVER['SERVER_ADDR'];
  121. }
  122. set_config('openssl', implode('@@@@@@@@', $this->keypair), 'mnet');
  123. $DB->update_record('mnet_host', $this);
  124. if (!PHPUNIT_TEST) {
  125. // We don't want to output this log for PHPUnit since it will make the test to fail as risky.
  126. error_log('New public key has been generated. It expires ' . date('Y/m/d h:i:s', $this->public_key_expires));
  127. }
  128. }
  129. function get_private_key() {
  130. if (empty($this->keypair)) $this->get_keypair();
  131. if (isset($this->keypair['privatekey'])) return $this->keypair['privatekey'];
  132. $this->keypair['privatekey'] = openssl_pkey_get_private($this->keypair['keypair_PEM']);
  133. return $this->keypair['privatekey'];
  134. }
  135. function get_public_key() {
  136. if (!isset($this->keypair)) $this->get_keypair();
  137. if (isset($this->keypair['publickey'])) return $this->keypair['publickey'];
  138. $this->keypair['publickey'] = openssl_pkey_get_public($this->keypair['certificate']);
  139. return $this->keypair['publickey'];
  140. }
  141. }