config.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. * @returns 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 $key key
  59. * @param $default = null default value
  60. * @returns 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 $key key
  75. * @param $value value
  76. * @returns true/false
  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 $key key
  92. * @returns true/false
  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. * @returns true/false
  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. * @returns true/false
  132. *
  133. * Saves the config to the config file.
  134. *
  135. * Known flaws: Strings are not escaped properly
  136. */
  137. public static function writeData(){
  138. // Create a php file ...
  139. $content = "<?php\n\$CONFIG = array(\n";
  140. foreach( self::$cache as $key => $value ){
  141. if( is_bool( $value )){
  142. $value = $value ? 'true' : 'false';
  143. $content .= "\"$key\" => $value,\n";
  144. }
  145. else{
  146. $value = str_replace( "'", "\\'", $value );
  147. $content .= "\"$key\" => '$value',\n";
  148. }
  149. }
  150. $content .= ");\n?>\n";
  151. $filename = OC::$SERVERROOT."/config/config.php";
  152. // Write the file
  153. $result=@file_put_contents( $filename, $content );
  154. if(!$result) {
  155. $tmpl = new OC_Template( '', 'error', 'guest' );
  156. $tmpl->assign('errors',array(1=>array('error'=>"Can't write into config directory 'config'",'hint'=>"You can usually fix this by giving the webserver user write access to the config directory in owncloud")));
  157. $tmpl->printPage();
  158. exit;
  159. }
  160. // Prevent others not to read the config
  161. @chmod($filename, 0640);
  162. return true;
  163. }
  164. }