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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. if( !file_exists( OC::$SERVERROOT."/config/config.php" )) {
  118. return false;
  119. }
  120. // Include the file, save the data from $CONFIG
  121. include OC::$SERVERROOT."/config/config.php";
  122. if( isset( $CONFIG ) && is_array( $CONFIG )) {
  123. self::$cache = $CONFIG;
  124. }
  125. // We cached everything
  126. self::$init = true;
  127. return true;
  128. }
  129. /**
  130. * @brief Writes the config file
  131. * @return bool
  132. *
  133. * Saves the config to the config file.
  134. *
  135. */
  136. public static function writeData() {
  137. // Create a php file ...
  138. $content = "<?php\n\$CONFIG = ";
  139. $content .= var_export(self::$cache, true);
  140. $content .= ";\n";
  141. $filename = OC::$SERVERROOT."/config/config.php";
  142. // Write the file
  143. $result=@file_put_contents( $filename, $content );
  144. if(!$result) {
  145. $tmpl = new OC_Template( '', 'error', 'guest' );
  146. $tmpl->assign('errors', array(1=>array(
  147. 'error'=>"Can't write into config directory 'config'",
  148. 'hint'=>"You can usually fix this by giving the webserver user write access to the config directory in owncloud")));
  149. $tmpl->printPage();
  150. exit;
  151. }
  152. // Prevent others not to read the config
  153. @chmod($filename, 0640);
  154. return true;
  155. }
  156. }