Config.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * Functions related to configuration management.
  4. */
  5. /**
  6. * Re-write configuration file according to given array.
  7. * Requires mandatory fields listed in $MANDATORY_FIELDS.
  8. *
  9. * @param array $config contains all configuration fields.
  10. * @param bool $isLoggedIn true if user is logged in.
  11. *
  12. * @return void
  13. *
  14. * @throws MissingFieldConfigException: a mandatory field has not been provided in $config.
  15. * @throws UnauthorizedConfigException: user is not authorize to change configuration.
  16. * @throws Exception: an error occured while writing the new config file.
  17. */
  18. function writeConfig($config, $isLoggedIn)
  19. {
  20. // These fields are required in configuration.
  21. $MANDATORY_FIELDS = array(
  22. 'login', 'hash', 'salt', 'timezone', 'title', 'titleLink',
  23. 'redirector', 'disablesessionprotection', 'privateLinkByDefault'
  24. );
  25. if (!isset($config['config']['CONFIG_FILE'])) {
  26. throw new MissingFieldConfigException('CONFIG_FILE');
  27. }
  28. // Only logged in user can alter config.
  29. if (is_file($config['config']['CONFIG_FILE']) && !$isLoggedIn) {
  30. throw new UnauthorizedConfigException();
  31. }
  32. // Check that all mandatory fields are provided in $config.
  33. foreach ($MANDATORY_FIELDS as $field) {
  34. if (!isset($config[$field])) {
  35. throw new MissingFieldConfigException($field);
  36. }
  37. }
  38. $configStr = '<?php '. PHP_EOL;
  39. $configStr .= '$GLOBALS[\'login\'] = '.var_export($config['login'], true).';'. PHP_EOL;
  40. $configStr .= '$GLOBALS[\'hash\'] = '.var_export($config['hash'], true).';'. PHP_EOL;
  41. $configStr .= '$GLOBALS[\'salt\'] = '.var_export($config['salt'], true).'; '. PHP_EOL;
  42. $configStr .= '$GLOBALS[\'timezone\'] = '.var_export($config['timezone'], true).';'. PHP_EOL;
  43. $configStr .= 'date_default_timezone_set('.var_export($config['timezone'], true).');'. PHP_EOL;
  44. $configStr .= '$GLOBALS[\'title\'] = '.var_export($config['title'], true).';'. PHP_EOL;
  45. $configStr .= '$GLOBALS[\'titleLink\'] = '.var_export($config['titleLink'], true).'; '. PHP_EOL;
  46. $configStr .= '$GLOBALS[\'redirector\'] = '.var_export($config['redirector'], true).'; '. PHP_EOL;
  47. $configStr .= '$GLOBALS[\'disablesessionprotection\'] = '.var_export($config['disablesessionprotection'], true).'; '. PHP_EOL;
  48. $configStr .= '$GLOBALS[\'privateLinkByDefault\'] = '.var_export($config['privateLinkByDefault'], true).'; '. PHP_EOL;
  49. // Store all $config['config']
  50. foreach ($config['config'] as $key => $value) {
  51. $configStr .= '$GLOBALS[\'config\'][\''. $key .'\'] = '.var_export($config['config'][$key], true).';'. PHP_EOL;
  52. }
  53. if (isset($config['plugins'])) {
  54. foreach ($config['plugins'] as $key => $value) {
  55. $configStr .= '$GLOBALS[\'plugins\'][\''. $key .'\'] = '.var_export($config['plugins'][$key], true).';'. PHP_EOL;
  56. }
  57. }
  58. if (!file_put_contents($config['config']['CONFIG_FILE'], $configStr)
  59. || strcmp(file_get_contents($config['config']['CONFIG_FILE']), $configStr) != 0
  60. ) {
  61. throw new Exception(
  62. 'Shaarli could not create the config file.
  63. Please make sure Shaarli has the right to write in the folder is it installed in.'
  64. );
  65. }
  66. }
  67. /**
  68. * Milestone 0.9 - shaarli/Shaarli#41: options.php is not supported anymore.
  69. * ==> if user is loggedIn, merge its content with config.php, then delete options.php.
  70. *
  71. * @param array $config contains all configuration fields.
  72. * @param bool $isLoggedIn true if user is logged in.
  73. *
  74. * @return void
  75. */
  76. function mergeDeprecatedConfig($config, $isLoggedIn)
  77. {
  78. $config_file = $config['config']['CONFIG_FILE'];
  79. if (is_file($config['config']['DATADIR'].'/options.php') && $isLoggedIn) {
  80. include $config['config']['DATADIR'].'/options.php';
  81. // Load GLOBALS into config
  82. foreach ($GLOBALS as $key => $value) {
  83. $config[$key] = $value;
  84. }
  85. $config['config']['CONFIG_FILE'] = $config_file;
  86. writeConfig($config, $isLoggedIn);
  87. unlink($config['config']['DATADIR'].'/options.php');
  88. }
  89. }
  90. /**
  91. * Exception used if a mandatory field is missing in given configuration.
  92. */
  93. class MissingFieldConfigException extends Exception
  94. {
  95. public $field;
  96. /**
  97. * Construct exception.
  98. *
  99. * @param string $field field name missing.
  100. */
  101. public function __construct($field)
  102. {
  103. $this->field = $field;
  104. $this->message = 'Configuration value is required for '. $this->field;
  105. }
  106. }
  107. /**
  108. * Exception used if an unauthorized attempt to edit configuration has been made.
  109. */
  110. class UnauthorizedConfigException extends Exception
  111. {
  112. /**
  113. * Construct exception.
  114. */
  115. public function __construct()
  116. {
  117. $this->message = 'You are not authorized to alter config.';
  118. }
  119. }