Verzeichnisstruktur phpBB-3.1.0


Veröffentlicht
27.10.2014

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

db.php

Zuletzt modifiziert: 09.10.2024, 12:51 - Dateigröße: 5.42 KiB


001  <?php
002  /**
003  *
004  * This file is part of the phpBB Forum Software package.
005  *
006  * @copyright (c) phpBB Limited <https://www.phpbb.com>
007  * @license GNU General Public License, version 2 (GPL-2.0)
008  *
009  * For full copyright and license information, please see
010  * the docs/CREDITS.txt file.
011  *
012  */
013   
014  namespace phpbb\config;
015   
016  /**
017  * Configuration container class
018  */
019  class db extends \phpbb\config\config
020  {
021      /**
022      * Cache instance
023      * @var \phpbb\cache\driver\driver_interface
024      */
025      protected $cache;
026   
027      /**
028      * Database connection
029      * @var \phpbb\db\driver\driver_interface
030      */
031      protected $db;
032   
033      /**
034      * Name of the database table used for configuration.
035      * @var string
036      */
037      protected $table;
038   
039      /**
040      * Creates a configuration container with a default set of values
041      *
042      * @param \phpbb\db\driver\driver_interface    $db    Database connection
043      * @param \phpbb\cache\driver\driver_interface $cache Cache instance
044      * @param string                       $table Configuration table name
045      */
046      public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\cache\driver\driver_interface $cache, $table)
047      {
048          $this->db = $db;
049          $this->cache = $cache;
050          $this->table = $table;
051   
052          if (($config = $cache->get('config')) !== false)
053          {
054              $sql = 'SELECT config_name, config_value
055                  FROM ' . $this->table . '
056                  WHERE is_dynamic = 1';
057              $result = $this->db->sql_query($sql);
058   
059              while ($row = $this->db->sql_fetchrow($result))
060              {
061                  $config[$row['config_name']] = $row['config_value'];
062              }
063              $this->db->sql_freeresult($result);
064          }
065          else
066          {
067              $config = $cached_config = array();
068   
069              $sql = 'SELECT config_name, config_value, is_dynamic
070                  FROM ' . $this->table;
071              $result = $this->db->sql_query($sql);
072   
073              while ($row = $this->db->sql_fetchrow($result))
074              {
075                  if (!$row['is_dynamic'])
076                  {
077                      $cached_config[$row['config_name']] = $row['config_value'];
078                  }
079   
080                  $config[$row['config_name']] = $row['config_value'];
081              }
082              $this->db->sql_freeresult($result);
083   
084              $cache->put('config', $cached_config);
085          }
086   
087          parent::__construct($config);
088      }
089   
090      /**
091      * Removes a configuration option
092      *
093      * @param  String $key       The configuration option's name
094      * @param  bool   $use_cache Whether this variable should be cached or if it
095      *                           changes too frequently to be efficiently cached
096      * @return null
097      */
098      public function delete($key, $use_cache = true)
099      {
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          $result = $this->db->sql_query($sql);
149   
150          if (!$this->db->sql_affectedrows($result) && 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  }
205