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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. /**
  37. * This class is responsible for reading and writing config.php, the very basic
  38. * configuration file of owncloud.
  39. */
  40. class OC_Config{
  41. // associative array key => value
  42. private static $cache = array();
  43. // Is the cache filled?
  44. private static $init = false;
  45. /**
  46. * @brief Lists all available config keys
  47. * @return array with key names
  48. *
  49. * This function returns all keys saved in config.php. Please note that it
  50. * does not return the values.
  51. */
  52. public static function getKeys() {
  53. self::readData();
  54. return array_keys( self::$cache );
  55. }
  56. /**
  57. * @brief Gets a value from config.php
  58. * @param string $key key
  59. * @param string $default = null default value
  60. * @return string the value or $default
  61. *
  62. * This function gets the value from config.php. If it does not exist,
  63. * $default will be returned.
  64. */
  65. public static function getValue( $key, $default = null ) {
  66. self::readData();
  67. if( array_key_exists( $key, self::$cache )) {
  68. return self::$cache[$key];
  69. }
  70. return $default;
  71. }
  72. /**
  73. * @brief Sets a value
  74. * @param string $key key
  75. * @param string $value value
  76. * @return bool
  77. *
  78. * This function sets the value and writes the config.php. If the file can
  79. * not be written, false will be returned.
  80. */
  81. public static function setValue( $key, $value ) {
  82. self::readData();
  83. // Add change
  84. self::$cache[$key] = $value;
  85. // Write changes
  86. self::writeData();
  87. return true;
  88. }
  89. /**
  90. * @brief Removes a key from the config
  91. * @param string $key key
  92. * @return bool
  93. *
  94. * This function removes a key from the config.php. If owncloud has no
  95. * write access to config.php, the function will return false.
  96. */
  97. public static function deleteKey( $key ) {
  98. self::readData();
  99. if( array_key_exists( $key, self::$cache )) {
  100. // Delete key from cache
  101. unset( self::$cache[$key] );
  102. // Write changes
  103. self::writeData();
  104. }
  105. return true;
  106. }
  107. /**
  108. * @brief Loads the config file
  109. * @return bool
  110. *
  111. * Reads the config file and saves it to the cache
  112. */
  113. private static function readData() {
  114. if( self::$init ) {
  115. return true;
  116. }
  117. // read all file in config dir ending by config.php
  118. $config_files = glob( OC::$SERVERROOT."/config/*.config.php");
  119. //Filter only regular files
  120. $config_files = array_filter($config_files, 'is_file');
  121. //Sort array naturally :
  122. natsort($config_files);
  123. // Add default config
  124. array_unshift($config_files,OC::$SERVERROOT."/config/config.php");
  125. //Include file and merge config
  126. foreach($config_files as $file){
  127. include $file;
  128. if( isset( $CONFIG ) && is_array( $CONFIG )) {
  129. self::$cache = array_merge(self::$cache, $CONFIG);
  130. }
  131. }
  132. // We cached everything
  133. self::$init = true;
  134. return true;
  135. }
  136. /**
  137. * @brief Writes the config file
  138. * @return bool
  139. *
  140. * Saves the config to the config file.
  141. *
  142. */
  143. public static function writeData() {
  144. // Create a php file ...
  145. $content = "<?php\n ";
  146. if (defined('DEBUG') && DEBUG) {
  147. $content .= "define('DEBUG',true);\n";
  148. }
  149. $content .= "\$CONFIG = ";
  150. $content .= var_export(self::$cache, true);
  151. $content .= ";\n";
  152. $filename = OC::$SERVERROOT."/config/config.php";
  153. // Write the file
  154. $result=@file_put_contents( $filename, $content );
  155. if(!$result) {
  156. $tmpl = new OC_Template( '', 'error', 'guest' );
  157. $tmpl->assign('errors', array(1=>array(
  158. 'error'=>"Can't write into config directory 'config'",
  159. 'hint'=>'You can usually fix this by giving the webserver user write access'
  160. .' to the config directory in owncloud')));
  161. $tmpl->printPage();
  162. exit;
  163. }
  164. // Prevent others not to read the config
  165. @chmod($filename, 0640);
  166. return true;
  167. }
  168. }