/setup/includes/modinstallsettings.class.php

https://github.com/francisreboucas/revolution · PHP · 103 lines · 91 code · 9 blank · 3 comment · 10 complexity · 070339f0f7fe385e8abadb9c15f0c89c MD5 · raw file

  1. <?php
  2. /**
  3. * @package setup
  4. */
  5. class modInstallSettings {
  6. public $install = null;
  7. public $config = array();
  8. public $fileName = '';
  9. public $settings = array();
  10. function __construct(modInstall &$install,array $config = array()) {
  11. $this->install =& $install;
  12. $this->config = array_merge(array(),$config);
  13. $this->fileName = $this->getCachePath().'settings.cache.php';
  14. $this->load();
  15. }
  16. protected function getCachePath() {
  17. return MODX_CORE_PATH . 'cache/' . (MODX_CONFIG_KEY == 'config' ? '' : MODX_CONFIG_KEY . '/') . 'setup/';
  18. }
  19. protected function rebuildDSN() {
  20. if (array_key_exists('database_type', $this->settings)) {
  21. switch ($this->settings['database_type']) {
  22. case 'sqlsrv':
  23. $database_dsn = "{$this->settings['database_type']}:server={$this->settings['database_server']};database={$this->settings['dbase']}";
  24. $server_dsn = "{$this->settings['database_type']}:server={$this->settings['database_server']}";
  25. break;
  26. case 'mysql':
  27. $database_dsn = "{$this->settings['database_type']}:host={$this->settings['database_server']};dbname={$this->settings['dbase']};charset={$this->settings['database_connection_charset']}";
  28. $server_dsn = "{$this->settings['database_type']}:host={$this->settings['database_server']};charset={$this->settings['database_connection_charset']}";
  29. break;
  30. default:
  31. $database_dsn = '';
  32. $server_dsn = '';
  33. break;
  34. }
  35. $this->settings['database_dsn'] = $database_dsn;
  36. $this->settings['server_dsn'] = $server_dsn;
  37. }
  38. }
  39. public function set($k,$v) {
  40. $this->settings[$k] = $v;
  41. if (in_array($k, array('database_type', 'database_server', 'dbase', 'database_connection_charset'))) {
  42. $this->rebuildDSN();
  43. }
  44. }
  45. public function get($k,$default = null) {
  46. if (in_array($k, array('database_dsn', 'server_dsn'))) {
  47. $this->rebuildDSN();
  48. }
  49. return isset($this->settings[$k]) ? $this->settings[$k] : $default;
  50. }
  51. public function load() {
  52. if (file_exists($this->fileName)) {
  53. $this->settings = include $this->fileName;
  54. if (empty($this->settings)) {
  55. $this->restart();
  56. }
  57. }
  58. }
  59. public function check() {
  60. $this->load();
  61. if (empty($this->settings)) {
  62. $this->restart();
  63. }
  64. }
  65. public function restart() {
  66. $this->erase();
  67. header('Location: ' . MODX_SETUP_URL.'?restarted=1');
  68. exit();
  69. }
  70. public function delete($k) {
  71. unset($this->settings[$k]);
  72. }
  73. public function store(array $settings = array(),$expire = 900) {
  74. $this->settings = array_merge($this->settings,$settings);
  75. $this->rebuildDSN();
  76. $written = false;
  77. if ($file= @ fopen($this->fileName,'wb')) {
  78. $expirationTS= $expire ? time() + $expire : time();
  79. $expireContent= 'if(time() > ' . $expirationTS . '){return array();}';
  80. $content = '<?php ' . $expireContent . ' return ' . var_export($this->settings, true) . ';';
  81. $written= @ fwrite($file, $content);
  82. @ fclose($file);
  83. }
  84. return $written;
  85. }
  86. public function erase() {
  87. if (file_exists($this->fileName)) {
  88. @unlink($this->fileName);
  89. }
  90. }
  91. public function fetch() {
  92. return $this->settings;
  93. }
  94. }