You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

config.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Frank Karlitschek
  6. * @author Jakob Sack
  7. * @copyright 2012 Frank Karlitschek frank@owncloud.org
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  11. * License as published by the Free Software Foundation; either
  12. * version 3 of the License, or any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public
  20. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. /*
  24. *
  25. * An example of config.php
  26. *
  27. * <?php
  28. * $CONFIG = array(
  29. * "database" => "mysql",
  30. * "firstrun" => false,
  31. * "pi" => 3.14
  32. * );
  33. * ?>
  34. *
  35. */
  36. namespace OC;
  37. /**
  38. * This class is responsible for reading and writing config.php, the very basic
  39. * configuration file of ownCloud.
  40. */
  41. class Config {
  42. // associative array key => value
  43. protected $cache = array();
  44. protected $configDir;
  45. protected $configFilename;
  46. protected $debugMode;
  47. /**
  48. * @param $configDir path to the config dir, needs to end with '/'
  49. */
  50. public function __construct($configDir) {
  51. $this->configDir = $configDir;
  52. $this->configFilename = $this->configDir.'config.php';
  53. $this->readData();
  54. $this->setDebugMode(defined('DEBUG') && DEBUG);
  55. }
  56. public function setDebugMode($enable) {
  57. $this->debugMode = $enable;
  58. }
  59. /**
  60. * @brief Lists all available config keys
  61. * @return array with key names
  62. *
  63. * This function returns all keys saved in config.php. Please note that it
  64. * does not return the values.
  65. */
  66. public function getKeys() {
  67. return array_keys($this->cache);
  68. }
  69. /**
  70. * @brief Gets a value from config.php
  71. * @param string $key key
  72. * @param string $default = null default value
  73. * @return string the value or $default
  74. *
  75. * This function gets the value from config.php. If it does not exist,
  76. * $default will be returned.
  77. */
  78. public function getValue($key, $default = null) {
  79. if (isset($this->cache[$key])) {
  80. return $this->cache[$key];
  81. }
  82. return $default;
  83. }
  84. /**
  85. * @brief Sets a value
  86. * @param string $key key
  87. * @param string $value value
  88. *
  89. * This function sets the value and writes the config.php.
  90. *
  91. */
  92. public function setValue($key, $value) {
  93. // Add change
  94. $this->cache[$key] = $value;
  95. // Write changes
  96. $this->writeData();
  97. }
  98. /**
  99. * @brief Removes a key from the config
  100. * @param string $key key
  101. *
  102. * This function removes a key from the config.php.
  103. *
  104. */
  105. public function deleteKey($key) {
  106. if (isset($this->cache[$key])) {
  107. // Delete key from cache
  108. unset($this->cache[$key]);
  109. // Write changes
  110. $this->writeData();
  111. }
  112. }
  113. /**
  114. * @brief Loads the config file
  115. *
  116. * Reads the config file and saves it to the cache
  117. */
  118. private function readData() {
  119. // Default config
  120. $configFiles = array($this->configFilename);
  121. // Add all files in the config dir ending with config.php
  122. $extra = glob($this->configDir.'*.config.php');
  123. if (is_array($extra)) {
  124. natsort($extra);
  125. $configFiles = array_merge($configFiles, $extra);
  126. }
  127. // Include file and merge config
  128. foreach ($configFiles as $file) {
  129. if (!file_exists($file)) {
  130. continue;
  131. }
  132. unset($CONFIG);
  133. // ignore errors on include, this can happen when doing a fresh install
  134. @include $file;
  135. if (isset($CONFIG) && is_array($CONFIG)) {
  136. $this->cache = array_merge($this->cache, $CONFIG);
  137. }
  138. }
  139. }
  140. /**
  141. * @brief Writes the config file
  142. *
  143. * Saves the config to the config file.
  144. *
  145. */
  146. private function writeData() {
  147. // Create a php file ...
  148. $defaults = new \OC_Defaults;
  149. $content = "<?php\n";
  150. if ($this->debugMode) {
  151. $content .= "define('DEBUG',true);\n";
  152. }
  153. $content .= '$CONFIG = ';
  154. $content .= var_export($this->cache, true);
  155. $content .= ";\n";
  156. // Write the file
  157. $result = @file_put_contents($this->configFilename, $content);
  158. if (!$result) {
  159. $url = $defaults->getDocBaseUrl() . '/server/5.0/admin_manual/installation/installation_source.html#set-the-directory-permissions';
  160. throw new HintException(
  161. "Can't write into config directory!",
  162. 'This can usually be fixed by '
  163. .'<a href="' . $url . '" target="_blank">giving the webserver write access to the config directory</a>.');
  164. }
  165. // Prevent others not to read the config
  166. @chmod($this->configFilename, 0640);
  167. \OC_Util::clearOpcodeCache();
  168. }
  169. }