frqnck /
apix-cache
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * |
||
| 5 | * This file is part of the Apix Project. |
||
| 6 | * |
||
| 7 | * (c) Franck Cassedanne <franck at ouarz.net> |
||
| 8 | * |
||
| 9 | * @license http://opensource.org/licenses/BSD-3-Clause New BSD License |
||
| 10 | * |
||
| 11 | */ |
||
| 12 | |||
| 13 | namespace Apix\Cache; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * PDO cache wrapper with tag support. |
||
| 17 | * |
||
| 18 | * @author Franck Cassedanne <franck at ouarz.net> |
||
| 19 | */ |
||
| 20 | abstract class AbstractPdo extends AbstractCache |
||
| 21 | { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Holds the SQL definitions. |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | protected $sql_definitions; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Holds the array of TTLs. |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | protected $ttls = array(); |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Constructor. |
||
| 37 | * |
||
| 38 | * @param \PDO $pdo An instance of a PDO class. |
||
| 39 | * @param array $options Array of options. |
||
| 40 | */ |
||
| 41 | 155 | public function __construct(\PDO $pdo, array $options=null) |
|
| 42 | { |
||
| 43 | // default options |
||
| 44 | 155 | $this->options['db_table'] = 'cache'; // table to hold the cache |
|
| 45 | 155 | $this->options['serializer'] = 'php'; // null, php, igBinary, json |
|
| 46 | 155 | // and msgpack |
|
| 47 | 155 | $this->options['preflight'] = true; // wether to preflight the DB |
|
| 48 | $this->options['timestamp'] = 'Y-m-d H:i:s'; // timestamp db format |
||
| 49 | 155 | ||
| 50 | $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
||
| 51 | 155 | ||
| 52 | 155 | parent::__construct($pdo, $options); |
|
| 53 | $this->setSerializer($this->options['serializer']); |
||
| 54 | 155 | ||
| 55 | 155 | if ($this->options['preflight']) { |
|
| 56 | 155 | $this->initDb(); |
|
| 57 | 155 | } |
|
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Initialises the database and its indexes (if required, non-destructive). |
||
| 62 | * |
||
| 63 | * @return self Provides a fluent interface |
||
| 64 | 155 | */ |
|
| 65 | public function initDb() |
||
| 66 | 155 | { |
|
| 67 | $this->adapter->exec( $this->getSql('init') ); |
||
| 68 | 155 | ||
| 69 | 155 | $this->createIndexTable('key_idx'); |
|
| 70 | 155 | $this->createIndexTable('exp_idx'); |
|
| 71 | $this->createIndexTable('tag_idx'); |
||
| 72 | 155 | ||
| 73 | return $this; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Creates the specified indexe table (if missing). |
||
| 78 | * |
||
| 79 | * @param string $index |
||
| 80 | * @return boolean |
||
| 81 | 155 | */ |
|
| 82 | public function createIndexTable($index) |
||
| 83 | 155 | { |
|
| 84 | 50 | if (!isset($this->sql_definitions[$index])) { |
|
| 85 | return false; |
||
| 86 | 155 | } |
|
| 87 | $this->adapter->exec($this->getSql($index, $this->options['db_table'])); |
||
| 88 | 155 | ||
| 89 | return $this->adapter->errorCode() == '00000'; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * {@inheritdoc} |
||
| 94 | 84 | */ |
|
| 95 | public function loadKey($key) |
||
| 96 | 84 | { |
|
| 97 | 84 | $key = $this->mapKey($key); |
|
| 98 | 84 | $sql = $this->getSql('loadKey'); |
|
| 99 | $values = array('key' => $key, 'now' => time()); |
||
| 100 | 84 | ||
| 101 | $cached = $this->exec($sql, $values)->fetch(); |
||
| 102 | |||
| 103 | 78 | // if (isset($cached['expire'])) { |
|
| 104 | $this->ttls[$key] = $cached['expire']; |
||
| 105 | // } |
||
| 106 | 78 | ||
| 107 | 48 | if (null !== $cached['data'] && null !== $this->serializer) { |
|
| 108 | return $this->serializer->unserialize($cached['data']); |
||
| 109 | } |
||
| 110 | 48 | ||
| 111 | return false === $cached ? null : $cached['data']; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * {@inheritdoc} |
||
| 116 | 48 | */ |
|
| 117 | public function loadTag($tag) |
||
| 118 | 48 | { |
|
| 119 | $sql = $this->getSql('loadTag'); |
||
| 120 | 48 | // $tag = $this->mapTag($tag); |
|
| 121 | $values = array('tag' => "%$tag%", 'now' => time()); |
||
| 122 | 48 | ||
| 123 | $items = $this->exec($sql, $values)->fetchAll(); |
||
| 124 | 48 | ||
| 125 | 48 | $keys = array(); |
|
| 126 | 24 | foreach ($items as $item) { |
|
| 127 | 48 | $keys[] = $item['key']; |
|
| 128 | } |
||
| 129 | 48 | ||
| 130 | return empty($keys) ? null : $keys; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * {@inheritdoc} |
||
| 135 | 108 | */ |
|
| 136 | public function save($data, $key, array $tags=null, $ttl=null) |
||
| 137 | 108 | { |
|
| 138 | $key = $this->mapKey($key); |
||
| 139 | 108 | $values = array( |
|
| 140 | 108 | 'key' => $key, |
|
| 141 | 108 | 'data' => null !== $this->serializer |
|
| 142 | 108 | ? $this->serializer->serialize($data) |
|
| 143 | 108 | : $data, |
|
| 144 | 108 | 'exp' => null !== $ttl && 0 !== $ttl ? time()+$ttl : null, |
|
| 145 | 108 | 'dated' => $this->getTimestamp() |
|
| 146 | ); |
||
| 147 | 108 | ||
| 148 | $this->ttls[$key] = $values['exp']; |
||
| 149 | 108 | ||
| 150 | 108 | $values['tags'] = $this->options['tag_enable'] && null !== $tags |
|
| 151 | 108 | ? implode(', ', $tags) |
|
| 152 | : null; |
||
| 153 | |||
| 154 | 108 | // upsert |
|
| 155 | 108 | $sql = $this->getSql('update'); |
|
| 156 | 108 | $nb = $this->exec($sql, $values)->rowCount(); |
|
| 157 | 108 | if ($nb == 0) { |
|
| 158 | 108 | $sql = $this->getSql('insert'); |
|
| 159 | 108 | $nb = $this->exec($sql, $values)->rowCount(); |
|
| 160 | } |
||
| 161 | 108 | ||
| 162 | return (boolean) $nb; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * {@inheritdoc} |
||
| 167 | 18 | */ |
|
| 168 | public function delete($key) |
||
| 169 | 18 | { |
|
| 170 | 18 | $sql = $this->getSql('delete'); |
|
| 171 | $values = array($this->mapKey($key)); |
||
| 172 | 18 | ||
| 173 | return (boolean) $this->exec($sql, $values)->rowCount(); |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * {@inheritdoc} |
||
| 178 | 6 | */ |
|
| 179 | public function clean(array $tags) |
||
| 180 | 6 | { |
|
| 181 | 6 | $values = array(); |
|
| 182 | foreach ($tags as $tag) { |
||
| 183 | 6 | // $tag = $this->mapTag($tag); |
|
| 184 | 6 | $values[] = '%' . $tag . '%'; |
|
| 185 | } |
||
| 186 | 6 | ||
| 187 | 6 | $sql = $this->getSql( |
|
| 188 | 6 | 'clean', implode(' OR ', array_fill( |
|
| 189 | 6 | 0, count($tags), $this->getSql('clean_like'))) |
|
| 190 | ); |
||
| 191 | 6 | ||
| 192 | return (boolean) $this->exec($sql, $values)->rowCount(); |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * {@inheritdoc} |
||
| 197 | 138 | */ |
|
| 198 | public function flush($all=false) |
||
| 199 | 138 | { |
|
| 200 | 138 | if (true === $all) { |
|
| 201 | return false !== $this->adapter->exec($this->getSql('flush_all')); |
||
| 202 | } |
||
| 203 | 6 | ||
| 204 | return (boolean) $this->adapter->exec($this->getSql('flush')); |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Purges expired items. |
||
| 209 | * |
||
| 210 | * @param integer|null $add Extra time in second to add. |
||
| 211 | * @return boolean Returns True on success or False on failure. |
||
| 212 | 6 | */ |
|
| 213 | public function purge($add=null) |
||
| 214 | 6 | { |
|
| 215 | $time = null == $add ? time() : time()+$add; |
||
| 216 | 6 | ||
| 217 | return (boolean) $this->adapter->exec($this->getSql('purge', $time)); |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Gets the named SQL definition. |
||
| 222 | * |
||
| 223 | * @param string $key |
||
| 224 | * @param string|integer $value An additional value. |
||
| 225 | * @return string |
||
| 226 | 155 | */ |
|
| 227 | protected function getSql($key, $value=null) |
||
| 228 | 155 | { |
|
| 229 | 155 | return sprintf( |
|
| 230 | 155 | $this->sql_definitions[$key], |
|
| 231 | $this->options['db_table'], |
||
| 232 | 155 | $value |
|
| 233 | ); |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Prepares and executes a SQL query. |
||
| 238 | * |
||
| 239 | * @param string $sql The SQL to prepare. |
||
| 240 | * @param array $values The values to execute. |
||
| 241 | * @return \PDOStatement Provides a fluent interface |
||
| 242 | 126 | */ |
|
| 243 | protected function exec($sql, array $values) |
||
| 244 | 126 | { |
|
| 245 | 126 | $prep = $this->adapter->prepare($sql); |
|
| 246 | $prep->execute($values); |
||
| 247 | 126 | ||
| 248 | return $prep; |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Returns the current driver's name. |
||
| 253 | * |
||
| 254 | * @param \PDO $pdo An instance of a PDO class. |
||
| 255 | * @return string Either 'Mysql', 'Pgsql', 'Sqlite' or 'Sql1999'. |
||
| 256 | 23 | */ |
|
| 257 | public static function getDriverName(\PDO $pdo) |
||
| 258 | 23 | { |
|
| 259 | 23 | $name = $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); |
|
| 260 | if (!in_array($name, array('sqlite', 'mysql', 'pgsql'))) { |
||
| 261 | // @codeCoverageIgnoreStart |
||
| 262 | // sql1999 specific tests are run on Sqlite. |
||
| 263 | $name = 'sql1999'; |
||
| 264 | } |
||
| 265 | // @codeCoverageIgnoreEnd |
||
| 266 | 23 | ||
| 267 | return ucfirst($name); |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Returns a formated timestamp. |
||
| 272 | * |
||
| 273 | * @param integer|null $time If null, use the current time. |
||
| 274 | 108 | */ |
|
| 275 | public function getTimestamp($time=null) |
||
| 276 | 108 | { |
|
| 277 | 108 | return date( |
|
| 278 | 108 | $this->options['timestamp'], |
|
| 279 | 108 | null != $time ? $time : time() |
|
| 280 | ); |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * {@inheritdoc} |
||
| 285 | 12 | */ |
|
| 286 | View Code Duplication | public function getTtl($key) |
|
| 287 | 12 | { |
|
| 288 | $mKey = $this->mapKey($key); |
||
| 289 | 12 | ||
| 290 | 12 | return isset($this->ttls[$mKey]) |
|
|
0 ignored issues
–
show
Bug
Compatibility
introduced
by
Loading history...
|
|||
| 291 | 12 | ? $this->ttls[$mKey]-time() |
|
| 292 | : false; |
||
| 293 | } |
||
| 294 | |||
| 295 | } |
||
| 296 |