Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
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. |
|
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
db.php
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 use phpbb\cache\driver\driver_interface as cache_interface;
017 use phpbb\db\driver\driver_interface as db_interface;
018
019 /**
020 * Configuration container class
021 */
022 class db extends config
023 {
024 /**
025 * Cache instance
026 * @var cache_interface
027 */
028 protected $cache;
029
030 /**
031 * Database connection
032 * @var db_interface
033 */
034 protected $db;
035
036 /**
037 * Name of the database table used for configuration.
038 * @var string
039 */
040 protected $table;
041
042 /**
043 * Creates a configuration container with a default set of values
044 *
045 * @param db_interface $db Database connection
046 * @param cache_interface $cache Cache instance
047 * @param string $table Configuration table name
048 */
049 public function __construct(db_interface $db, cache_interface $cache, $table)
050 {
051 $this->db = $db;
052 $this->cache = $cache;
053 $this->table = $table;
054
055 $this->initialise($cache);
056
057 parent::__construct($this->config);
058 }
059
060 /**
061 * Initialise config with database and/or cached entries
062 *
063 * @param cache_interface $cache
064 */
065 public function initialise(cache_interface $cache)
066 {
067 if (($config = $cache->get('config')) !== false)
068 {
069 $sql = 'SELECT config_name, config_value
070 FROM ' . $this->table . '
071 WHERE is_dynamic = 1';
072 $result = $this->db->sql_query($sql);
073
074 while ($row = $this->db->sql_fetchrow($result))
075 {
076 $config[$row['config_name']] = $row['config_value'];
077 }
078 $this->db->sql_freeresult($result);
079 }
080 else
081 {
082 $config = $cached_config = array();
083
084 $sql = 'SELECT config_name, config_value, is_dynamic
085 FROM ' . $this->table;
086 $result = $this->db->sql_query($sql);
087
088 while ($row = $this->db->sql_fetchrow($result))
089 {
090 if (!$row['is_dynamic'])
091 {
092 $cached_config[$row['config_name']] = $row['config_value'];
093 }
094
095 $config[$row['config_name']] = $row['config_value'];
096 }
097 $this->db->sql_freeresult($result);
098
099 $cache->put('config', $cached_config);
100 }
101
102 $this->config = $config;
103 }
104
105 /**
106 * Removes a configuration option
107 *
108 * @param String $key The configuration option's name
109 * @param bool $use_cache Whether this variable should be cached or if it
110 * changes too frequently to be efficiently cached
111 * @return void
112 */
113 public function delete($key, $use_cache = true)
114 {
115 $sql = 'DELETE FROM ' . $this->table . "
116 WHERE config_name = '" . $this->db->sql_escape($key) . "'";
117 $this->db->sql_query($sql);
118
119 unset($this->config[$key]);
120
121 if ($use_cache)
122 {
123 $this->cache->destroy('config');
124 }
125 }
126
127 /**
128 * Sets a configuration option's value
129 *
130 * @param string $key The configuration option's name
131 * @param string $value New configuration value
132 * @param bool $use_cache Whether this variable should be cached or if it
133 * changes too frequently to be efficiently cached.
134 */
135 public function set($key, $value, $use_cache = true)
136 {
137 $this->set_atomic($key, false, $value, $use_cache);
138 }
139
140 /**
141 * Sets a configuration option's value only if the old_value matches the
142 * current configuration value or the configuration value does not exist yet.
143 *
144 * @param string $key The configuration option's name
145 * @param mixed $old_value Current configuration value or false to ignore
146 * the old value
147 * @param string $new_value New configuration value
148 * @param bool $use_cache Whether this variable should be cached or if it
149 * changes too frequently to be efficiently cached
150 * @return bool True if the value was changed, false otherwise
151 */
152 public function set_atomic($key, $old_value, $new_value, $use_cache = true)
153 {
154 $sql = 'UPDATE ' . $this->table . "
155 SET config_value = '" . $this->db->sql_escape($new_value) . "'
156 WHERE config_name = '" . $this->db->sql_escape($key) . "'";
157
158 if ($old_value !== false)
159 {
160 $sql .= " AND config_value = '" . $this->db->sql_escape($old_value) . "'";
161 }
162
163 $this->db->sql_query($sql);
164
165 if (!$this->db->sql_affectedrows() && isset($this->config[$key]))
166 {
167 return false;
168 }
169
170 if (!isset($this->config[$key]))
171 {
172 $sql = 'INSERT INTO ' . $this->table . ' ' . $this->db->sql_build_array('INSERT', array(
173 'config_name' => $key,
174 'config_value' => $new_value,
175 'is_dynamic' => $use_cache ? 0 : 1));
176 $this->db->sql_query($sql);
177 }
178
179 if ($use_cache)
180 {
181 $this->cache->destroy('config');
182 }
183
184 $this->config[$key] = $new_value;
185 return true;
186 }
187
188 /**
189 * Increments an integer config value directly in the database.
190 *
191 * Using this method instead of setting the new value directly avoids race
192 * conditions and unlike set_atomic it cannot fail.
193 *
194 * @param string $key The configuration option's name
195 * @param int $increment Amount to increment by
196 * @param bool $use_cache Whether this variable should be cached or if it
197 * changes too frequently to be efficiently cached.
198 */
199 function increment($key, $increment, $use_cache = true)
200 {
201 if (!isset($this->config[$key]))
202 {
203 $this->set($key, '0', $use_cache);
204 }
205
206 $sql_update = $this->db->cast_expr_to_string($this->db->cast_expr_to_bigint('config_value') . ' + ' . (int) $increment);
207
208 $this->db->sql_query('UPDATE ' . $this->table . '
209 SET config_value = ' . $sql_update . "
210 WHERE config_name = '" . $this->db->sql_escape($key) . "'");
211
212 if ($use_cache)
213 {
214 $this->cache->destroy('config');
215 }
216
217 $this->config[$key] += $increment;
218 }
219 }
220