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 | * APC Cache and APCu (User-Cache) wrapper with emulated tag support. |
||
| 17 | * |
||
| 18 | * @package Apix\Cache |
||
| 19 | * @author Franck Cassedanne <franck at ouarz.net> |
||
| 20 | */ |
||
| 21 | class Apc extends AbstractCache |
||
| 22 | { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Constructor. |
||
| 26 | */ |
||
| 27 | 77 | public function __construct(array $options = array()) |
|
| 28 | { |
||
| 29 | 77 | parent::__construct(null, $options); |
|
| 30 | 77 | } |
|
| 31 | |||
| 32 | /** |
||
| 33 | * {@inheritdoc} |
||
| 34 | */ |
||
| 35 | 30 | public function loadKey($key) |
|
| 36 | { |
||
| 37 | 30 | return $this->get($this->mapKey($key)); |
|
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * {@inheritdoc} |
||
| 42 | */ |
||
| 43 | 27 | public function loadTag($tag) |
|
| 44 | { |
||
| 45 | 27 | return $this->get($this->mapTag($tag)); |
|
| 46 | // TODO: return $this->getIndex($this->mapTag($tag))->load(); |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Retrieves the cache item for the given id. |
||
| 51 | * |
||
| 52 | * @param string $id The cache id to retrieve. |
||
| 53 | * @param boolean $success The variable to store the success value. |
||
| 54 | * @return mixed|null Returns the cached data or null. |
||
| 55 | */ |
||
| 56 | 45 | public function get($id, $success = null) |
|
| 57 | { |
||
| 58 | 45 | $cached = apc_fetch($id, $success); |
|
| 59 | |||
| 60 | 45 | return false === $success ? null : $cached; |
|
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Returns the named indexer. |
||
| 65 | * |
||
| 66 | * @param string $name The name of the index. |
||
| 67 | * @return Indexer\Adapter |
||
| 68 | */ |
||
| 69 | // public function getIndex($name) |
||
| 70 | // { |
||
| 71 | // return new Indexer\ApcIndexer($name, $this); |
||
| 72 | // } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * {@inheritdoc} |
||
| 76 | * |
||
| 77 | * APC does not support natively cache-tags so we simulate them. |
||
| 78 | */ |
||
| 79 | 48 | public function save($data, $key, array $tags = null, $ttl = null) |
|
| 80 | { |
||
| 81 | 48 | $key = $this->mapKey($key); |
|
| 82 | 48 | $store = array($key => $data); |
|
| 83 | |||
| 84 | 48 | if ($this->options['tag_enable'] && !empty($tags)) { |
|
| 85 | |||
| 86 | // add all the tags to the index key. |
||
| 87 | // TODO: $this->getIndex($key)->add($tags); |
||
| 88 | |||
| 89 | 21 | View Code Duplication | foreach ($tags as $tag) { |
| 90 | 21 | $tag = $this->mapTag($tag); |
|
| 91 | 21 | $keys = apc_fetch($tag, $success); |
|
|
0 ignored issues
–
show
|
|||
| 92 | 21 | if (false === $success) { |
|
| 93 | 21 | $store[$tag] = array($key); |
|
| 94 | 21 | } else { |
|
| 95 | 15 | $keys[] = $key; |
|
| 96 | 15 | $store[$tag] = array_unique($keys); |
|
| 97 | } |
||
| 98 | 21 | } |
|
| 99 | 21 | } |
|
| 100 | |||
| 101 | 48 | return !in_array(false, apc_store($store, null, $ttl)); |
|
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * {@inheritdoc} |
||
| 106 | * |
||
| 107 | * APC does not support natively cache-tags so we simulate them. |
||
| 108 | */ |
||
| 109 | 9 | public function delete($key) |
|
| 110 | { |
||
| 111 | 9 | $key = $this->mapKey($key); |
|
| 112 | |||
| 113 | 9 | if ( ($success = apc_delete($key)) && $this->options['tag_enable']) { |
|
| 114 | |||
| 115 | 6 | $iterator = $this->getIterator( |
|
| 116 | 6 | '/^' . preg_quote($this->options['prefix_tag']) . '/', |
|
| 117 | \APC_ITER_VALUE |
||
| 118 | 6 | ); |
|
| 119 | 6 | foreach ($iterator as $tag => $keys) { |
|
| 120 | 3 | if ( false !== ($k = array_search($key, $keys['value'])) ) { |
|
| 121 | 3 | unset($keys['value'][$k]); |
|
| 122 | 3 | if (empty($keys['value'])) { |
|
| 123 | 3 | apc_delete($tag); |
|
| 124 | 3 | } else { |
|
| 125 | 3 | apc_store($tag, $keys['value']); |
|
| 126 | } |
||
| 127 | 3 | } |
|
| 128 | 3 | continue; |
|
| 129 | 6 | } |
|
| 130 | 6 | } |
|
| 131 | |||
| 132 | 9 | return $success; |
|
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * {@inheritdoc} |
||
| 137 | * |
||
| 138 | * APC does not support natively cache-tags so we simulate them. |
||
| 139 | */ |
||
| 140 | 3 | public function clean(array $tags) |
|
| 141 | { |
||
| 142 | 3 | $rmed = array(); |
|
| 143 | 3 | View Code Duplication | foreach ($tags as $tag) { |
| 144 | 3 | $tag = $this->mapTag($tag); |
|
| 145 | 3 | $keys = apc_fetch($tag, $success); |
|
|
0 ignored issues
–
show
|
|||
| 146 | 3 | if ($success) { |
|
| 147 | 3 | foreach ($keys as $key) { |
|
| 148 | 3 | $rmed[] = apc_delete($key); |
|
| 149 | 3 | } |
|
| 150 | 3 | $rmed[] = apc_delete($tag); |
|
| 151 | 3 | } else { |
|
| 152 | 3 | $rmed[] = false; |
|
| 153 | } |
||
| 154 | 3 | } |
|
| 155 | |||
| 156 | 3 | return !in_array(false, $rmed); |
|
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * {@inheritdoc} |
||
| 161 | * |
||
| 162 | * APC does not support natively cache-tags so we simulate them. |
||
| 163 | */ |
||
| 164 | 60 | public function flush($all = false) |
|
| 165 | { |
||
| 166 | 60 | if (true === $all) { |
|
| 167 | 3 | return apc_clear_cache('user'); |
|
| 168 | } |
||
| 169 | |||
| 170 | 60 | $iterator = $this->getIterator( |
|
| 171 | 60 | '/^' . preg_quote($this->options['prefix_key']) |
|
| 172 | 60 | .'|' . preg_quote($this->options['prefix_tag']) . '/', |
|
| 173 | \APC_ITER_KEY |
||
| 174 | 60 | ); |
|
| 175 | |||
| 176 | 60 | $rmed = array(); |
|
| 177 | 60 | foreach ($iterator as $key => $data) { |
|
| 178 | 45 | $rmed[] = apc_delete($key); |
|
| 179 | 60 | } |
|
| 180 | |||
| 181 | 60 | return empty($rmed) || in_array(false, $rmed) ? false : true; |
|
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Returns an APC iterator. |
||
| 186 | * |
||
| 187 | * @param string $search |
||
| 188 | * @param integer $format |
||
| 189 | * @return \APCIterator |
||
| 190 | */ |
||
| 191 | 60 | protected function getIterator($search = null, $format = \APC_ITER_ALL) |
|
| 192 | { |
||
| 193 | 60 | return new \APCIterator('user', $search, $format, 100, \APC_LIST_ACTIVE); |
|
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Returns some internal informations about a APC cached item. |
||
| 198 | * |
||
| 199 | * @param string $key |
||
| 200 | * @return array|false |
||
| 201 | */ |
||
| 202 | 12 | public function getInternalInfos($key) |
|
| 203 | { |
||
| 204 | 12 | $iterator = $this->getIterator( |
|
| 205 | 12 | '/^' . preg_quote($this->options['prefix_key']) . '/', |
|
| 206 | 12 | \APC_ITER_KEY | \APC_ITER_VALUE | \APC_ITER_TTL |
|
| 207 | 12 | ); |
|
| 208 | |||
| 209 | 12 | $key = $this->mapKey($key); |
|
| 210 | 12 | foreach ($iterator as $k => $v) { |
|
| 211 | 9 | if ($k != $key) |
|
| 212 | 9 | continue; |
|
| 213 | |||
| 214 | 9 | return $v; |
|
| 215 | 9 | } |
|
| 216 | |||
| 217 | 9 | return false; |
|
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * {@inheritdoc} |
||
| 222 | */ |
||
| 223 | 6 | public function getTtl($key) |
|
| 224 | { |
||
| 225 | 6 | $info = $this->getInternalInfos($key); |
|
| 226 | 6 | if ( $info && isset($info['ttl']) ) { |
|
| 227 | return $info['ttl']; |
||
| 228 | // return $info['ttl'] > 0 ? $info['creation_time']+$info['ttl'] : 0; |
||
| 229 | } |
||
| 230 | |||
| 231 | 6 | return false; |
|
| 232 | } |
||
| 233 | |||
| 234 | } |
||
| 235 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.