2010-03-16 22:25:05 +03:00
|
|
|
<?php
|
2011-03-12 20:19:11 +03:00
|
|
|
/**
|
|
|
|
* ownCloud
|
|
|
|
*
|
|
|
|
* @author Frank Karlitschek
|
|
|
|
* @author Jakob Sack
|
2012-05-26 21:14:24 +04:00
|
|
|
* @copyright 2012 Frank Karlitschek frank@owncloud.org
|
2011-03-12 20:19:11 +03:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2011-03-13 19:25:34 +03:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* An example of config.php
|
|
|
|
*
|
|
|
|
* <?php
|
|
|
|
* $CONFIG = array(
|
|
|
|
* "database" => "mysql",
|
|
|
|
* "firstrun" => false,
|
|
|
|
* "pi" => 3.14
|
|
|
|
* );
|
|
|
|
* ?>
|
|
|
|
*
|
|
|
|
*/
|
2011-03-12 20:19:11 +03:00
|
|
|
|
2013-03-03 15:06:00 +04:00
|
|
|
namespace OC;
|
|
|
|
|
2011-03-12 20:19:11 +03:00
|
|
|
/**
|
|
|
|
* This class is responsible for reading and writing config.php, the very basic
|
|
|
|
* configuration file of owncloud.
|
|
|
|
*/
|
2013-03-03 15:06:00 +04:00
|
|
|
class Config {
|
2011-03-12 20:19:11 +03:00
|
|
|
// associative array key => value
|
2013-03-03 15:06:00 +04:00
|
|
|
protected $cache = array();
|
|
|
|
|
|
|
|
protected $config_dir;
|
|
|
|
protected $config_filename;
|
2011-03-12 20:19:11 +03:00
|
|
|
|
2013-03-03 15:06:00 +04:00
|
|
|
protected $debug_mode;
|
2010-03-16 22:25:05 +03:00
|
|
|
|
2013-03-03 15:06:00 +04:00
|
|
|
public function __construct($config_dir, $debug_mode) {
|
|
|
|
$this->config_dir = $config_dir;
|
|
|
|
$this->debug_mode = $debug_mode;
|
|
|
|
$this->config_filename = $this->config_dir.'config.php';
|
|
|
|
$this->readData();
|
|
|
|
}
|
2010-04-23 02:05:04 +04:00
|
|
|
/**
|
2011-03-12 20:19:11 +03:00
|
|
|
* @brief Lists all available config keys
|
2012-09-23 04:39:11 +04:00
|
|
|
* @return array with key names
|
2011-03-12 20:19:11 +03:00
|
|
|
*
|
|
|
|
* This function returns all keys saved in config.php. Please note that it
|
|
|
|
* does not return the values.
|
|
|
|
*/
|
2013-03-03 15:06:00 +04:00
|
|
|
public function getKeys() {
|
|
|
|
return array_keys( $this->cache );
|
2010-04-23 02:05:04 +04:00
|
|
|
}
|
2010-07-12 00:44:48 +04:00
|
|
|
|
|
|
|
/**
|
2011-03-12 20:19:11 +03:00
|
|
|
* @brief Gets a value from config.php
|
2012-09-23 04:39:11 +04:00
|
|
|
* @param string $key key
|
|
|
|
* @param string $default = null default value
|
|
|
|
* @return string the value or $default
|
2010-07-12 00:44:48 +04:00
|
|
|
*
|
2011-03-12 20:19:11 +03:00
|
|
|
* This function gets the value from config.php. If it does not exist,
|
|
|
|
* $default will be returned.
|
2010-07-12 00:44:48 +04:00
|
|
|
*/
|
2013-03-03 15:06:00 +04:00
|
|
|
public function getValue( $key, $default = null ) {
|
|
|
|
if( array_key_exists( $key, $this->cache )) {
|
|
|
|
return $this->cache[$key];
|
2011-04-08 18:54:12 +04:00
|
|
|
}
|
|
|
|
|
2011-03-12 20:19:11 +03:00
|
|
|
return $default;
|
2010-07-12 00:44:48 +04:00
|
|
|
}
|
2010-04-23 02:05:04 +04:00
|
|
|
|
2010-06-18 22:08:24 +04:00
|
|
|
/**
|
2011-03-12 20:19:11 +03:00
|
|
|
* @brief Sets a value
|
2012-09-23 04:39:11 +04:00
|
|
|
* @param string $key key
|
|
|
|
* @param string $value value
|
2011-03-12 20:19:11 +03:00
|
|
|
*
|
|
|
|
* This function sets the value and writes the config.php. If the file can
|
|
|
|
* not be written, false will be returned.
|
|
|
|
*/
|
2013-03-03 15:06:00 +04:00
|
|
|
public function setValue( $key, $value ) {
|
2011-04-08 18:54:12 +04:00
|
|
|
// Add change
|
2013-03-03 15:06:00 +04:00
|
|
|
$this->cache[$key] = $value;
|
2011-04-08 18:54:12 +04:00
|
|
|
|
|
|
|
// Write changes
|
2013-03-03 15:06:00 +04:00
|
|
|
$this->writeData();
|
2010-06-18 22:08:24 +04:00
|
|
|
}
|
2011-03-12 20:19:11 +03:00
|
|
|
|
2010-06-18 22:08:24 +04:00
|
|
|
/**
|
2011-03-12 20:19:11 +03:00
|
|
|
* @brief Removes a key from the config
|
2012-09-23 04:39:11 +04:00
|
|
|
* @param string $key key
|
2011-03-12 20:19:11 +03:00
|
|
|
*
|
|
|
|
* This function removes a key from the config.php. If owncloud has no
|
|
|
|
* write access to config.php, the function will return false.
|
|
|
|
*/
|
2013-03-03 15:06:00 +04:00
|
|
|
public function deleteKey( $key ) {
|
|
|
|
if( array_key_exists( $key, $this->cache )) {
|
2011-04-08 18:54:12 +04:00
|
|
|
// Delete key from cache
|
2013-03-03 15:06:00 +04:00
|
|
|
unset( $this->cache[$key] );
|
2011-04-08 18:54:12 +04:00
|
|
|
|
|
|
|
// Write changes
|
2013-03-03 15:06:00 +04:00
|
|
|
$this->writeData();
|
2011-04-08 18:54:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Loads the config file
|
|
|
|
*
|
|
|
|
* Reads the config file and saves it to the cache
|
|
|
|
*/
|
2013-03-03 15:06:00 +04:00
|
|
|
private function readData() {
|
2013-03-07 02:30:16 +04:00
|
|
|
// read all file in config dir ending by config.php
|
2013-03-03 15:06:00 +04:00
|
|
|
$config_files = glob( $this->config_dir.'*.config.php');
|
2013-03-07 02:30:16 +04:00
|
|
|
|
|
|
|
//Filter only regular files
|
|
|
|
$config_files = array_filter($config_files, 'is_file');
|
|
|
|
|
2013-03-10 00:41:59 +04:00
|
|
|
//Sort array naturally :
|
|
|
|
natsort($config_files);
|
|
|
|
|
2013-03-07 02:30:16 +04:00
|
|
|
// Add default config
|
2013-03-03 15:06:00 +04:00
|
|
|
array_unshift($config_files, $this->config_filename);
|
2011-04-08 18:54:12 +04:00
|
|
|
|
2013-03-07 02:30:16 +04:00
|
|
|
//Include file and merge config
|
2013-03-03 15:06:00 +04:00
|
|
|
foreach($config_files as $file) {
|
|
|
|
if( !file_exists( $file) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
unset($CONFIG);
|
2013-03-07 02:30:16 +04:00
|
|
|
include $file;
|
|
|
|
if( isset( $CONFIG ) && is_array( $CONFIG )) {
|
2013-03-03 15:06:00 +04:00
|
|
|
$this->cache = array_merge($this->cache, $CONFIG);
|
2013-03-07 02:30:16 +04:00
|
|
|
}
|
2011-05-15 17:03:12 +04:00
|
|
|
}
|
2011-04-08 18:54:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Writes the config file
|
|
|
|
*
|
|
|
|
* Saves the config to the config file.
|
|
|
|
*
|
|
|
|
*/
|
2013-03-03 15:06:00 +04:00
|
|
|
private function writeData() {
|
2011-04-08 18:54:12 +04:00
|
|
|
// Create a php file ...
|
2013-03-03 15:06:00 +04:00
|
|
|
$content = "<?php\n";
|
|
|
|
if ($this->debug_mode) {
|
2013-03-07 00:05:13 +04:00
|
|
|
$content .= "define('DEBUG',true);\n";
|
|
|
|
}
|
2013-03-03 15:06:00 +04:00
|
|
|
$content .= '$CONFIG = ';
|
|
|
|
$content .= var_export($this->cache, true);
|
2012-06-23 01:01:12 +04:00
|
|
|
$content .= ";\n";
|
2013-03-03 15:06:00 +04:00
|
|
|
//var_dump($content, $this);
|
2011-04-08 18:54:12 +04:00
|
|
|
|
|
|
|
// Write the file
|
2013-03-03 15:06:00 +04:00
|
|
|
$result=@file_put_contents( $this->config_filename, $content );
|
2011-08-12 21:22:29 +04:00
|
|
|
if(!$result) {
|
2013-03-03 15:06:00 +04:00
|
|
|
throw new HintException(
|
|
|
|
"Can't write into config directory 'config'",
|
|
|
|
'You can usually fix this by giving the webserver user write access'
|
|
|
|
.' to the config directory in owncloud');
|
2011-08-12 21:22:29 +04:00
|
|
|
}
|
2012-06-13 01:15:44 +04:00
|
|
|
// Prevent others not to read the config
|
2013-03-03 15:06:00 +04:00
|
|
|
@chmod($this->config_filename, 0640);
|
2010-04-08 16:49:48 +04:00
|
|
|
}
|
2010-03-16 22:25:05 +03:00
|
|
|
}
|