aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/includes/config
diff options
context:
space:
mode:
Diffstat (limited to 'phpBB/includes/config')
-rw-r--r--phpBB/includes/config/config.php170
-rw-r--r--phpBB/includes/config/db.php207
2 files changed, 377 insertions, 0 deletions
diff --git a/phpBB/includes/config/config.php b/phpBB/includes/config/config.php
new file mode 100644
index 0000000000..354dc85c03
--- /dev/null
+++ b/phpBB/includes/config/config.php
@@ -0,0 +1,170 @@
+<?php
+/**
+*
+* @package phpBB3
+* @copyright (c) 2010 phpBB Group
+* @license http://opensource.org/licenses/gpl-license.php GNU Public License
+*
+*/
+
+/**
+* @ignore
+*/
+if (!defined('IN_PHPBB'))
+{
+ exit;
+}
+
+/**
+* Configuration container class
+* @package phpBB3
+*/
+class phpbb_config implements ArrayAccess, IteratorAggregate, Countable
+{
+ /**
+ * The configuration data
+ * @var array(string => string)
+ */
+ protected $config;
+
+ /**
+ * Creates a configuration container with a default set of values
+ *
+ * @param array(string => string) $config The configuration data.
+ */
+ public function __construct(array $config)
+ {
+ $this->config = $config;
+ }
+
+ /**
+ * Retrieves an ArrayIterator over the configuration values.
+ *
+ * @return ArrayIterator An iterator over all config data
+ */
+ public function getIterator()
+ {
+ return new ArrayIterator($this->config);
+ }
+
+ /**
+ * Checks if the specified config value exists.
+ *
+ * @param string $key The configuration option's name.
+ * @return bool Whether the configuration option exists.
+ */
+ public function offsetExists($key)
+ {
+ return isset($this->config[$key]);
+ }
+
+ /**
+ * Retrieves a configuration value.
+ *
+ * @param string $key The configuration option's name.
+ * @return string The configuration value
+ */
+ public function offsetGet($key)
+ {
+ return (isset($this->config[$key])) ? $this->config[$key] : '';
+ }
+
+ /**
+ * Temporarily overwrites the value of a configuration variable.
+ *
+ * The configuration change will not persist. It will be lost
+ * after the request.
+ *
+ * @param string $key The configuration option's name.
+ * @param string $value The temporary value.
+ */
+ public function offsetSet($key, $value)
+ {
+ $this->config[$key] = $value;
+ }
+
+ /**
+ * Called when deleting a configuration value directly, triggers an error.
+ *
+ * @param string $key The configuration option's name.
+ */
+ public function offsetUnset($key)
+ {
+ trigger_error('Config values have to be deleted explicitly with the phpbb_config::delete($key) method.', E_USER_ERROR);
+ }
+
+ /**
+ * Retrieves the number of configuration options currently set.
+ *
+ * @return int Number of config options
+ */
+ public function count()
+ {
+ return count($this->config);
+ }
+
+ /**
+ * Removes a configuration option
+ *
+ * @param String $key The configuration option's name
+ * @param bool $use_cache Whether this variable should be cached or if it
+ * changes too frequently to be efficiently cached
+ * @return void
+ */
+ public function delete($key, $use_cache = true)
+ {
+ unset($this->config[$key]);
+ }
+
+ /**
+ * Sets a configuration option's value
+ *
+ * @param string $key The configuration option's name
+ * @param string $value New configuration value
+ * @param bool $use_cache Whether this variable should be cached or if it
+ * changes too frequently to be efficiently cached.
+ */
+ public function set($key, $value, $use_cache = true)
+ {
+ $this->config[$key] = $value;
+ }
+
+ /**
+ * Sets a configuration option's value only if the old_value matches the
+ * current configuration value or the configuration value does not exist yet.
+ *
+ * @param string $key The configuration option's name
+ * @param string $old_value Current configuration value
+ * @param string $new_value New configuration value
+ * @param bool $use_cache Whether this variable should be cached or if it
+ * changes too frequently to be efficiently cached.
+ * @return bool True if the value was changed, false otherwise.
+ */
+ public function set_atomic($key, $old_value, $new_value, $use_cache = true)
+ {
+ if (!isset($this->config[$key]) || $this->config[$key] == $old_value)
+ {
+ $this->config[$key] = $new_value;
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Increments an integer configuration value.
+ *
+ * @param string $key The configuration option's name
+ * @param int $increment Amount to increment by
+ * @param bool $use_cache Whether this variable should be cached or if it
+ * changes too frequently to be efficiently cached.
+ */
+ function increment($key, $increment, $use_cache = true)
+ {
+ if (!isset($this->config[$key]))
+ {
+ $this->config[$key] = 0;
+ }
+
+ $this->config[$key] += $increment;
+ }
+}
diff --git a/phpBB/includes/config/db.php b/phpBB/includes/config/db.php
new file mode 100644
index 0000000000..a2db4056df
--- /dev/null
+++ b/phpBB/includes/config/db.php
@@ -0,0 +1,207 @@
+<?php
+/**
+*
+* @package phpBB3
+* @copyright (c) 2010 phpBB Group
+* @license http://opensource.org/licenses/gpl-license.php GNU Public License
+*
+*/
+
+/**
+* @ignore
+*/
+if (!defined('IN_PHPBB'))
+{
+ exit;
+}
+
+/**
+* Configuration container class
+* @package phpBB3
+*/
+class phpbb_config_db extends phpbb_config
+{
+ /**
+ * Cache instance
+ * @var phpbb_cache_driver_interface
+ */
+ protected $cache;
+
+ /**
+ * Database connection
+ * @var dbal
+ */
+ protected $db;
+
+ /**
+ * Name of the database table used for configuration.
+ * @var string
+ */
+ protected $table;
+
+ /**
+ * Creates a configuration container with a default set of values
+ *
+ * @param dbal $db Database connection
+ * @param phpbb_cache_driver_interface $cache Cache instance
+ * @param string $table Configuration table name
+ */
+ public function __construct(dbal $db, phpbb_cache_driver_interface $cache, $table)
+ {
+ $this->db = $db;
+ $this->cache = $cache;
+ $this->table = $table;
+
+ if (($config = $cache->get('config')) !== false)
+ {
+ $sql = 'SELECT config_name, config_value
+ FROM ' . $this->table . '
+ WHERE is_dynamic = 1';
+ $result = $this->db->sql_query($sql);
+
+ while ($row = $this->db->sql_fetchrow($result))
+ {
+ $config[$row['config_name']] = $row['config_value'];
+ }
+ $this->db->sql_freeresult($result);
+ }
+ else
+ {
+ $config = $cached_config = array();
+
+ $sql = 'SELECT config_name, config_value, is_dynamic
+ FROM ' . $this->table;
+ $result = $this->db->sql_query($sql);
+
+ while ($row = $this->db->sql_fetchrow($result))
+ {
+ if (!$row['is_dynamic'])
+ {
+ $cached_config[$row['config_name']] = $row['config_value'];
+ }
+
+ $config[$row['config_name']] = $row['config_value'];
+ }
+ $this->db->sql_freeresult($result);
+
+ $cache->put('config', $cached_config);
+ }
+
+ parent::__construct($config);
+ }
+
+ /**
+ * Removes a configuration option
+ *
+ * @param String $key The configuration option's name
+ * @param bool $use_cache Whether this variable should be cached or if it
+ * changes too frequently to be efficiently cached
+ * @return void
+ */
+ public function delete($key, $use_cache = true)
+ {
+ $sql = 'DELETE FROM ' . $this->table . "
+ WHERE config_name = '" . $this->db->sql_escape($key) . "'";
+ $this->db->sql_query($sql);
+
+ unset($this->config[$key]);
+
+ if ($use_cache)
+ {
+ $this->cache->destroy('config');
+ }
+ }
+
+ /**
+ * Sets a configuration option's value
+ *
+ * @param string $key The configuration option's name
+ * @param string $value New configuration value
+ * @param bool $use_cache Whether this variable should be cached or if it
+ * changes too frequently to be efficiently cached.
+ */
+ public function set($key, $value, $use_cache = true)
+ {
+ $this->set_atomic($key, false, $value, $use_cache);
+ }
+
+ /**
+ * Sets a configuration option's value only if the old_value matches the
+ * current configuration value or the configuration value does not exist yet.
+ *
+ * @param string $key The configuration option's name
+ * @param mixed $old_value Current configuration value or false to ignore
+ * the old value
+ * @param string $new_value New configuration value
+ * @param bool $use_cache Whether this variable should be cached or if it
+ * changes too frequently to be efficiently cached
+ * @return bool True if the value was changed, false otherwise
+ */
+ public function set_atomic($key, $old_value, $new_value, $use_cache = true)
+ {
+ $sql = 'UPDATE ' . $this->table . "
+ SET config_value = '" . $this->db->sql_escape($new_value) . "'
+ WHERE config_name = '" . $this->db->sql_escape($key) . "'";
+
+ if ($old_value !== false)
+ {
+ $sql .= " AND config_value = '" . $this->db->sql_escape($old_value) . "'";
+ }
+
+ $result = $this->db->sql_query($sql);
+
+ if (!$this->db->sql_affectedrows($result) && isset($this->config[$key]))
+ {
+ return false;
+ }
+
+ if (!isset($this->config[$key]))
+ {
+ $sql = 'INSERT INTO ' . $this->table . ' ' . $this->db->sql_build_array('INSERT', array(
+ 'config_name' => $key,
+ 'config_value' => $new_value,
+ 'is_dynamic' => ($use_cache) ? 0 : 1));
+ $this->db->sql_query($sql);
+ }
+
+ if ($use_cache)
+ {
+ $this->cache->destroy('config');
+ }
+
+ $this->config[$key] = $new_value;
+ return true;
+ }
+
+ /**
+ * Increments an integer config value directly in the database.
+ *
+ * Using this method instead of setting the new value directly avoids race
+ * conditions and unlike set_atomic it cannot fail.
+ *
+ * @param string $key The configuration option's name
+ * @param int $increment Amount to increment by
+ * @param bool $use_cache Whether this variable should be cached or if it
+ * changes too frequently to be efficiently cached.
+ */
+ function increment($key, $increment, $use_cache = true)
+ {
+ if (!isset($this->config[$key]))
+ {
+ $this->set($key, '0', $use_cache);
+ }
+
+ $sql_update = $this->db->cast_expr_to_string($this->db->cast_expr_to_bigint('config_value') . ' + ' . (int) $increment);
+
+ $this->db->sql_query('UPDATE ' . $this->table . '
+ SET config_value = ' . $sql_update . "
+ WHERE config_name = '" . $this->db->sql_escape($key) . "'");
+
+ if ($use_cache)
+ {
+ $this->cache->destroy('config');
+ }
+
+ $this->config[$key] += $increment;
+ }
+}