API
6 years ago
Access
6 years ago
Application
6 years ago
Archive
6 years ago
ArchiveProcessor
6 years ago
Archiver
6 years ago
AssetManager
6 years ago
Auth
6 years ago
Category
6 years ago
CliMulti
6 years ago
Columns
6 years ago
Composer
6 years ago
Concurrency
6 years ago
Config
6 years ago
Container
6 years ago
CronArchive
6 years ago
DataAccess
6 years ago
DataFiles
6 years ago
DataTable
6 years ago
Db
6 years ago
DeviceDetector
6 years ago
Email
6 years ago
Exception
6 years ago
Http
6 years ago
Intl
6 years ago
Mail
6 years ago
Measurable
6 years ago
Menu
6 years ago
Metrics
6 years ago
Notification
6 years ago
Period
6 years ago
Plugin
6 years ago
ProfessionalServices
6 years ago
Report
6 years ago
ReportRenderer
6 years ago
Scheduler
6 years ago
Segment
6 years ago
Session
6 years ago
Settings
6 years ago
Tracker
6 years ago
Translation
6 years ago
UpdateCheck
6 years ago
Updater
6 years ago
Updates
6 years ago
Validators
6 years ago
View
6 years ago
ViewDataTable
6 years ago
Visualization
6 years ago
Widget
6 years ago
.htaccess
6 years ago
Access.php
6 years ago
Archive.php
6 years ago
ArchiveProcessor.php
6 years ago
AssetManager.php
6 years ago
Auth.php
6 years ago
BaseFactory.php
6 years ago
Cache.php
6 years ago
CacheId.php
6 years ago
CliMulti.php
6 years ago
Common.php
6 years ago
Config.php
6 years ago
Console.php
6 years ago
Context.php
6 years ago
Cookie.php
6 years ago
CronArchive.php
6 years ago
DataArray.php
6 years ago
DataTable.php
6 years ago
Date.php
6 years ago
Db.php
6 years ago
DbHelper.php
6 years ago
Development.php
6 years ago
DeviceDetectorFactory.php
6 years ago
ErrorHandler.php
6 years ago
EventDispatcher.php
6 years ago
ExceptionHandler.php
6 years ago
FileIntegrity.php
6 years ago
Filechecks.php
6 years ago
Filesystem.php
6 years ago
FrontController.php
6 years ago
Http.php
6 years ago
IP.php
6 years ago
Log.php
6 years ago
LogDeleter.php
6 years ago
Mail.php
6 years ago
Metrics.php
6 years ago
MetricsFormatter.php
6 years ago
Nonce.php
6 years ago
Notification.php
6 years ago
NumberFormatter.php
6 years ago
Option.php
6 years ago
Period.php
6 years ago
Piwik.php
6 years ago
Plugin.php
6 years ago
Profiler.php
6 years ago
ProxyHeaders.php
6 years ago
ProxyHttp.php
6 years ago
QuickForm2.php
6 years ago
RankingQuery.php
6 years ago
Registry.php
6 years ago
ReportRenderer.php
6 years ago
ScheduledTask.php
6 years ago
Segment.php
6 years ago
Sequence.php
6 years ago
Session.php
6 years ago
SettingsPiwik.php
6 years ago
SettingsServer.php
6 years ago
Singleton.php
6 years ago
Site.php
6 years ago
TCPDF.php
6 years ago
TaskScheduler.php
6 years ago
Theme.php
6 years ago
Timer.php
6 years ago
Tracker.php
6 years ago
Translate.php
6 years ago
Twig.php
6 years ago
Unzip.php
6 years ago
UpdateCheck.php
6 years ago
Updater.php
6 years ago
Updates.php
6 years ago
Url.php
6 years ago
UrlHelper.php
6 years ago
Version.php
6 years ago
View.php
6 years ago
bootstrap.php
6 years ago
dispatch.php
6 years ago
testMinimumPhpVersion.php
6 years ago
Option.php
276 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * |
| 8 | */ |
| 9 | namespace Piwik; |
| 10 | |
| 11 | /** |
| 12 | * Convenient key-value storage for user specified options and temporary |
| 13 | * data that needs to be persisted beyond one request. |
| 14 | * |
| 15 | * ### Examples |
| 16 | * |
| 17 | * **Setting and getting options** |
| 18 | * |
| 19 | * $optionValue = Option::get('MyPlugin.MyOptionName'); |
| 20 | * if ($optionValue === false) { |
| 21 | * // if not set, set it |
| 22 | * Option::set('MyPlugin.MyOptionName', 'my option value'); |
| 23 | * } |
| 24 | * |
| 25 | * **Storing user specific options** |
| 26 | * |
| 27 | * $userName = // ... |
| 28 | * Option::set('MyPlugin.MyOptionName.' . $userName, 'my option value'); |
| 29 | * |
| 30 | * **Clearing user specific options** |
| 31 | * |
| 32 | * Option::deleteLike('MyPlugin.MyOptionName.%'); |
| 33 | * |
| 34 | * @api |
| 35 | */ |
| 36 | class Option |
| 37 | { |
| 38 | /** |
| 39 | * Returns the option value for the requested option `$name`. |
| 40 | * |
| 41 | * @param string $name The option name. |
| 42 | * @return string|false The value or `false`, if not found. |
| 43 | */ |
| 44 | public static function get($name) |
| 45 | { |
| 46 | return self::getInstance()->getValue($name); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Returns option values for options whose names are like a given pattern. |
| 51 | * |
| 52 | * @param string $namePattern The pattern used in the SQL `LIKE` expression |
| 53 | * used to SELECT options. |
| 54 | * @return array Array mapping option names with option values. |
| 55 | */ |
| 56 | public static function getLike($namePattern) |
| 57 | { |
| 58 | return self::getInstance()->getNameLike($namePattern); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Sets an option value by name. |
| 63 | * |
| 64 | * @param string $name The option name. |
| 65 | * @param string $value The value to set the option to. |
| 66 | * @param int $autoLoad If set to 1, this option value will be automatically loaded when Piwik is initialzed; |
| 67 | * should be set to 1 for options that will be used in every Piwik request. |
| 68 | */ |
| 69 | public static function set($name, $value, $autoload = 0) |
| 70 | { |
| 71 | self::getInstance()->setValue($name, $value, $autoload); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Deletes an option. |
| 76 | * |
| 77 | * @param string $name Option name to match exactly. |
| 78 | * @param string $value If supplied the option will be deleted only if its value matches this value. |
| 79 | */ |
| 80 | public static function delete($name, $value = null) |
| 81 | { |
| 82 | self::getInstance()->deleteValue($name, $value); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Deletes all options that match the supplied pattern. |
| 87 | * |
| 88 | * @param string $namePattern Pattern of key to match. `'%'` characters should be used as wildcards, and literal |
| 89 | * `'_'` characters should be escaped. |
| 90 | * @param string $value If supplied, options will be deleted only if their value matches this value. |
| 91 | */ |
| 92 | public static function deleteLike($namePattern, $value = null) |
| 93 | { |
| 94 | self::getInstance()->deleteNameLike($namePattern, $value); |
| 95 | } |
| 96 | |
| 97 | public static function clearCachedOption($name) |
| 98 | { |
| 99 | self::getInstance()->clearCachedOptionByName($name); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Clears the option value cache and forces a reload from the Database. |
| 104 | * Used in unit tests to reset the state of the object between tests. |
| 105 | * |
| 106 | * @return void |
| 107 | * @ignore |
| 108 | */ |
| 109 | public static function clearCache() |
| 110 | { |
| 111 | $option = self::getInstance(); |
| 112 | $option->loaded = false; |
| 113 | $option->all = array(); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @var array |
| 118 | */ |
| 119 | private $all = array(); |
| 120 | |
| 121 | /** |
| 122 | * @var bool |
| 123 | */ |
| 124 | private $loaded = false; |
| 125 | |
| 126 | /** |
| 127 | * Singleton instance |
| 128 | * @var \Piwik\Option |
| 129 | */ |
| 130 | private static $instance = null; |
| 131 | |
| 132 | /** |
| 133 | * Returns Singleton instance |
| 134 | * |
| 135 | * @return \Piwik\Option |
| 136 | */ |
| 137 | private static function getInstance() |
| 138 | { |
| 139 | if (self::$instance == null) { |
| 140 | self::$instance = new self; |
| 141 | } |
| 142 | |
| 143 | return self::$instance; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Sets the singleton instance. For testing purposes. |
| 148 | * |
| 149 | * @param mixed |
| 150 | * @ignore |
| 151 | */ |
| 152 | public static function setSingletonInstance($instance) |
| 153 | { |
| 154 | self::$instance = $instance; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Private Constructor |
| 159 | */ |
| 160 | private function __construct() |
| 161 | { |
| 162 | } |
| 163 | |
| 164 | protected function clearCachedOptionByName($name) |
| 165 | { |
| 166 | if (isset($this->all[$name])) { |
| 167 | unset($this->all[$name]); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | protected function getValue($name) |
| 172 | { |
| 173 | $this->autoload(); |
| 174 | if (isset($this->all[$name])) { |
| 175 | return $this->all[$name]; |
| 176 | } |
| 177 | |
| 178 | $value = Db::fetchOne('SELECT option_value FROM `' . Common::prefixTable('option') . '` ' . |
| 179 | 'WHERE option_name = ?', $name); |
| 180 | |
| 181 | $this->all[$name] = $value; |
| 182 | return $value; |
| 183 | } |
| 184 | |
| 185 | protected function setValue($name, $value, $autoLoad = 0) |
| 186 | { |
| 187 | $autoLoad = (int)$autoLoad; |
| 188 | |
| 189 | $sql = 'UPDATE `' . Common::prefixTable('option') . '` SET option_value = ?, autoload = ? WHERE option_name = ?'; |
| 190 | $bind = array($value, $autoLoad, $name); |
| 191 | |
| 192 | $result = Db::query($sql, $bind); |
| 193 | |
| 194 | $rowsUpdated = Db::get()->rowCount($result); |
| 195 | |
| 196 | if (! $rowsUpdated) { |
| 197 | try { |
| 198 | $sql = 'INSERT IGNORE INTO `' . Common::prefixTable('option') . '` (option_name, option_value, autoload) ' . |
| 199 | 'VALUES (?, ?, ?) '; |
| 200 | $bind = array($name, $value, $autoLoad); |
| 201 | |
| 202 | Db::query($sql, $bind); |
| 203 | } catch (\Exception $e) { |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | $this->all[$name] = $value; |
| 208 | } |
| 209 | |
| 210 | protected function deleteValue($name, $value) |
| 211 | { |
| 212 | $sql = 'DELETE FROM `' . Common::prefixTable('option') . '` WHERE option_name = ?'; |
| 213 | $bind[] = $name; |
| 214 | |
| 215 | if (isset($value)) { |
| 216 | $sql .= ' AND option_value = ?'; |
| 217 | $bind[] = $value; |
| 218 | } |
| 219 | |
| 220 | Db::query($sql, $bind); |
| 221 | |
| 222 | $this->clearCache(); |
| 223 | } |
| 224 | |
| 225 | protected function deleteNameLike($name, $value = null) |
| 226 | { |
| 227 | $sql = 'DELETE FROM `' . Common::prefixTable('option') . '` WHERE option_name LIKE ?'; |
| 228 | $bind[] = $name; |
| 229 | |
| 230 | if (isset($value)) { |
| 231 | $sql .= ' AND option_value = ?'; |
| 232 | $bind[] = $value; |
| 233 | } |
| 234 | |
| 235 | Db::query($sql, $bind); |
| 236 | |
| 237 | $this->clearCache(); |
| 238 | } |
| 239 | |
| 240 | protected function getNameLike($name) |
| 241 | { |
| 242 | $sql = 'SELECT option_name, option_value FROM `' . Common::prefixTable('option') . '` WHERE option_name LIKE ?'; |
| 243 | $bind = array($name); |
| 244 | $rows = Db::fetchAll($sql, $bind); |
| 245 | |
| 246 | $result = array(); |
| 247 | foreach ($rows as $row) { |
| 248 | $result[$row['option_name']] = $row['option_value']; |
| 249 | } |
| 250 | |
| 251 | return $result; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Initialize cache with autoload settings. |
| 256 | * |
| 257 | * @return void |
| 258 | */ |
| 259 | protected function autoload() |
| 260 | { |
| 261 | if ($this->loaded) { |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | $table = Common::prefixTable('option'); |
| 266 | $sql = 'SELECT option_value, option_name FROM `' . $table . '` WHERE autoload = 1'; |
| 267 | $all = Db::fetchAll($sql); |
| 268 | |
| 269 | foreach ($all as $option) { |
| 270 | $this->all[$option['option_name']] = $option['option_value']; |
| 271 | } |
| 272 | |
| 273 | $this->loaded = true; |
| 274 | } |
| 275 | } |
| 276 |