[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/phpbb/config/ -> db.php (source)

   1  <?php
   2  /**
   3  *
   4  * This file is part of the phpBB Forum Software package.
   5  *
   6  * @copyright (c) phpBB Limited <https://www.phpbb.com>
   7  * @license GNU General Public License, version 2 (GPL-2.0)
   8  *
   9  * For full copyright and license information, please see
  10  * the docs/CREDITS.txt file.
  11  *
  12  */
  13  
  14  namespace phpbb\config;
  15  
  16  /**
  17  * Configuration container class
  18  */
  19  class db extends \phpbb\config\config
  20  {
  21      /**
  22      * Cache instance
  23      * @var \phpbb\cache\driver\driver_interface
  24      */
  25      protected $cache;
  26  
  27      /**
  28      * Database connection
  29      * @var \phpbb\db\driver\driver_interface
  30      */
  31      protected $db;
  32  
  33      /**
  34      * Name of the database table used for configuration.
  35      * @var string
  36      */
  37      protected $table;
  38  
  39      /**
  40      * Creates a configuration container with a default set of values
  41      *
  42      * @param \phpbb\db\driver\driver_interface    $db    Database connection
  43      * @param \phpbb\cache\driver\driver_interface $cache Cache instance
  44      * @param string                       $table Configuration table name
  45      */
  46  	public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\cache\driver\driver_interface $cache, $table)
  47      {
  48          $this->db = $db;
  49          $this->cache = $cache;
  50          $this->table = $table;
  51  
  52          if (($config = $cache->get('config')) !== false)
  53          {
  54              $sql = 'SELECT config_name, config_value
  55                  FROM ' . $this->table . '
  56                  WHERE is_dynamic = 1';
  57              $result = $this->db->sql_query($sql);
  58  
  59              while ($row = $this->db->sql_fetchrow($result))
  60              {
  61                  $config[$row['config_name']] = $row['config_value'];
  62              }
  63              $this->db->sql_freeresult($result);
  64          }
  65          else
  66          {
  67              $config = $cached_config = array();
  68  
  69              $sql = 'SELECT config_name, config_value, is_dynamic
  70                  FROM ' . $this->table;
  71              $result = $this->db->sql_query($sql);
  72  
  73              while ($row = $this->db->sql_fetchrow($result))
  74              {
  75                  if (!$row['is_dynamic'])
  76                  {
  77                      $cached_config[$row['config_name']] = $row['config_value'];
  78                  }
  79  
  80                  $config[$row['config_name']] = $row['config_value'];
  81              }
  82              $this->db->sql_freeresult($result);
  83  
  84              $cache->put('config', $cached_config);
  85          }
  86  
  87          parent::__construct($config);
  88      }
  89  
  90      /**
  91      * Removes a configuration option
  92      *
  93      * @param  String $key       The configuration option's name
  94      * @param  bool   $use_cache Whether this variable should be cached or if it
  95      *                           changes too frequently to be efficiently cached
  96      * @return null
  97      */
  98  	public function delete($key, $use_cache = true)
  99      {
 100          $sql = 'DELETE FROM ' . $this->table . "
 101              WHERE config_name = '" . $this->db->sql_escape($key) . "'";
 102          $this->db->sql_query($sql);
 103  
 104          unset($this->config[$key]);
 105  
 106          if ($use_cache)
 107          {
 108              $this->cache->destroy('config');
 109          }
 110      }
 111  
 112      /**
 113      * Sets a configuration option's value
 114      *
 115      * @param string $key       The configuration option's name
 116      * @param string $value     New configuration value
 117      * @param bool   $use_cache Whether this variable should be cached or if it
 118      *                          changes too frequently to be efficiently cached.
 119      */
 120  	public function set($key, $value, $use_cache = true)
 121      {
 122          $this->set_atomic($key, false, $value, $use_cache);
 123      }
 124  
 125      /**
 126      * Sets a configuration option's value only if the old_value matches the
 127      * current configuration value or the configuration value does not exist yet.
 128      *
 129      * @param  string $key       The configuration option's name
 130      * @param  mixed  $old_value Current configuration value or false to ignore
 131      *                           the old value
 132      * @param  string $new_value New configuration value
 133      * @param  bool   $use_cache Whether this variable should be cached or if it
 134      *                           changes too frequently to be efficiently cached
 135      * @return bool              True if the value was changed, false otherwise
 136      */
 137  	public function set_atomic($key, $old_value, $new_value, $use_cache = true)
 138      {
 139          $sql = 'UPDATE ' . $this->table . "
 140              SET config_value = '" . $this->db->sql_escape($new_value) . "'
 141              WHERE config_name = '" . $this->db->sql_escape($key) . "'";
 142  
 143          if ($old_value !== false)
 144          {
 145              $sql .= " AND config_value = '" . $this->db->sql_escape($old_value) . "'";
 146          }
 147  
 148          $this->db->sql_query($sql);
 149  
 150          if (!$this->db->sql_affectedrows() && isset($this->config[$key]))
 151          {
 152              return false;
 153          }
 154  
 155          if (!isset($this->config[$key]))
 156          {
 157              $sql = 'INSERT INTO ' . $this->table . ' ' . $this->db->sql_build_array('INSERT', array(
 158                  'config_name'    => $key,
 159                  'config_value'    => $new_value,
 160                  'is_dynamic'    => ($use_cache) ? 0 : 1));
 161              $this->db->sql_query($sql);
 162          }
 163  
 164          if ($use_cache)
 165          {
 166              $this->cache->destroy('config');
 167          }
 168  
 169          $this->config[$key] = $new_value;
 170          return true;
 171      }
 172  
 173      /**
 174      * Increments an integer config value directly in the database.
 175      *
 176      * Using this method instead of setting the new value directly avoids race
 177      * conditions and unlike set_atomic it cannot fail.
 178      *
 179      * @param string $key       The configuration option's name
 180      * @param int    $increment Amount to increment by
 181      * @param bool   $use_cache Whether this variable should be cached or if it
 182      *                          changes too frequently to be efficiently cached.
 183      */
 184  	function increment($key, $increment, $use_cache = true)
 185      {
 186          if (!isset($this->config[$key]))
 187          {
 188              $this->set($key, '0', $use_cache);
 189          }
 190  
 191          $sql_update = $this->db->cast_expr_to_string($this->db->cast_expr_to_bigint('config_value') . ' + ' . (int) $increment);
 192  
 193          $this->db->sql_query('UPDATE ' . $this->table . '
 194              SET config_value = ' . $sql_update . "
 195              WHERE config_name = '" . $this->db->sql_escape($key) . "'");
 196  
 197          if ($use_cache)
 198          {
 199              $this->cache->destroy('config');
 200          }
 201  
 202          $this->config[$key] += $increment;
 203      }
 204  }


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1