EVE-KILL /
zKillboard
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 | /* zCache |
||
| 3 | * |
||
| 4 | * This program is free software: you can redistribute it and/or modify |
||
| 5 | * it under the terms of the GNU Affero General Public License as published by |
||
| 6 | * the Free Software Foundation, either version 3 of the License, or |
||
| 7 | * (at your option) any later version. |
||
| 8 | * |
||
| 9 | * This program is distributed in the hope that it will be useful, |
||
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 12 | * GNU Affero General Public License for more details. |
||
| 13 | * |
||
| 14 | * You should have received a copy of the GNU Affero General Public License |
||
| 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
| 16 | */ |
||
| 17 | |||
| 18 | /** |
||
| 19 | * FileCache for zKillboard (Basically copies what Memcached does) |
||
| 20 | */ |
||
| 21 | class FileCache extends AbstractCache |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @param $cacheDir the default cache dir |
||
| 25 | * @param $cacheTime the default cache time (5 minutes) |
||
| 26 | */ |
||
| 27 | |||
| 28 | protected $cacheDir = "cache/queryCache/"; |
||
| 29 | protected $cacheTime = 300; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @param string $cd a dir to use instead of default |
||
| 33 | */ |
||
| 34 | public function __construct($cd = null) |
||
| 35 | { |
||
| 36 | global $cacheDir; |
||
| 37 | if (isset($cacheDir) && $cd === null) { |
||
| 38 | $cd = $cacheDir; |
||
| 39 | } |
||
| 40 | |||
| 41 | if (!is_null($cd)) $this->cacheDir = $cd; |
||
| 42 | if (!is_dir($this->cacheDir)) mkdir($this->cacheDir); |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Gets the data |
||
| 47 | * |
||
| 48 | * @param string $key |
||
| 49 | * @return array|boolean |
||
| 50 | */ |
||
| 51 | public function get($key) |
||
| 52 | { |
||
| 53 | if(file_exists($this->cacheDir.sha1($key))) |
||
| 54 | { |
||
| 55 | $time = time(); |
||
| 56 | $data = self::getData($key); |
||
| 57 | $age = $data["age"]; |
||
| 58 | $data = json_decode($data["data"], true); |
||
| 59 | if($age <= $time) |
||
| 60 | { |
||
| 61 | @unlink($this->cacheDir.sha1($key)); |
||
| 62 | return false; |
||
| 63 | } |
||
| 64 | return $data; |
||
| 65 | } |
||
| 66 | else |
||
| 67 | return false; |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Sets data |
||
| 72 | * |
||
| 73 | * @param string $key |
||
| 74 | * @param string|array $value |
||
| 75 | * @param string $timeout |
||
| 76 | * |
||
| 77 | * return bool |
||
| 78 | */ |
||
| 79 | public function set($key, $value, $timeout) |
||
| 80 | { |
||
| 81 | return self::setData($key, $value, $timeout) !== false; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Replaces data |
||
| 86 | * |
||
| 87 | * @param string $key |
||
| 88 | * @param string|array $value |
||
| 89 | * @param string $timeout |
||
| 90 | * @return boolean |
||
| 91 | */ |
||
| 92 | public function replace($key, $value, $timeout) |
||
| 93 | { |
||
| 94 | if(file_exists($this->cacheDir.sha1($key))) |
||
| 95 | { |
||
| 96 | @unlink($this->cacheDir.sha1($key)); |
||
| 97 | if(self::setData($key, $value, $timeout) !== false) |
||
| 98 | return true; |
||
| 99 | } |
||
| 100 | |||
| 101 | return false; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Deletes a key |
||
| 106 | * |
||
| 107 | * @param string $key |
||
| 108 | * @return bool |
||
| 109 | */ |
||
| 110 | public function delete($key) |
||
| 111 | { |
||
| 112 | try |
||
| 113 | { |
||
| 114 | @unlink($this->cacheDir.sha1($key)); |
||
| 115 | } |
||
| 116 | catch (Exception $e) |
||
| 117 | { |
||
| 118 | return false; |
||
| 119 | } |
||
| 120 | return true; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Increments value |
||
| 125 | * |
||
| 126 | * @param string $key |
||
| 127 | * @param int $step |
||
| 128 | * @param int $timeout |
||
| 129 | * @return bool |
||
| 130 | */ |
||
| 131 | public function increment($key, $step = 1, $timeout = 0) |
||
| 132 | { |
||
| 133 | $data = self::getData($key); |
||
| 134 | $data = json_decode($data["data"], true); |
||
| 135 | |||
| 136 | try |
||
| 137 | { |
||
| 138 | @unlink($this->cacheDir.sha1($key)); |
||
| 139 | return self::setData($key, $data+$step, $timeout); |
||
| 140 | } |
||
| 141 | catch (Exception $e) |
||
| 142 | { |
||
| 143 | return false; |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Decrements value |
||
| 149 | * |
||
| 150 | * @param string $key |
||
| 151 | * @param int $step |
||
| 152 | * @param int $timeout |
||
| 153 | * @return bool |
||
| 154 | */ |
||
| 155 | public function decrement($key, $step = 1, $timeout = 0) |
||
| 156 | { |
||
| 157 | $data = self::getData($key); |
||
| 158 | $data = json_decode($data["data"], true); |
||
| 159 | |||
| 160 | try |
||
| 161 | { |
||
| 162 | @unlink($this->cacheDir.sha1($key)); |
||
| 163 | return self::setData($key, $data-$step, $timeout); |
||
| 164 | } |
||
| 165 | catch (Exception $e) |
||
| 166 | { |
||
| 167 | return false; |
||
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Flushes the cache |
||
| 173 | */ |
||
| 174 | public function flush() |
||
| 175 | { |
||
| 176 | $dir = opendir($this->cacheDir); |
||
| 177 | while($file = readdir($dir)) |
||
| 178 | { |
||
| 179 | @unlink($this->cacheDir.$file); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Sets data to cache file |
||
| 185 | * |
||
| 186 | * @param string $key |
||
| 187 | * @param string|array $value |
||
| 188 | * @param int $timeout |
||
| 189 | * |
||
| 190 | * return bool |
||
| 191 | */ |
||
| 192 | private function setData($key, $value, $timeout = NULL) |
||
| 193 | { |
||
| 194 | if(!$timeout) |
||
|
0 ignored issues
–
show
|
|||
| 195 | $timeout = $this->cacheTime; |
||
| 196 | |||
| 197 | try |
||
| 198 | { |
||
| 199 | // fix, so timeout will be timestamp based |
||
| 200 | $timeout = time() + $timeout; |
||
| 201 | |||
| 202 | $data = $timeout."%".json_encode($value); |
||
| 203 | if(@file_put_contents($this->cacheDir.sha1($key), $data) === false) |
||
| 204 | return false; |
||
| 205 | } |
||
| 206 | catch (Exception $e) |
||
| 207 | { |
||
| 208 | return false; |
||
| 209 | } |
||
| 210 | return $value; |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Gets the data from the cache |
||
| 215 | * |
||
| 216 | * @param string $key |
||
| 217 | * @param bool $sha |
||
| 218 | * @return array |
||
| 219 | */ |
||
| 220 | private function getData($key, $sha = true) |
||
| 221 | { |
||
| 222 | // @todo real error handling, not just surpression. |
||
| 223 | if($sha == true) |
||
| 224 | $data = @file_get_contents($this->cacheDir.sha1($key)); |
||
| 225 | else |
||
| 226 | $data = @file_get_contents($this->cacheDir.$key); |
||
| 227 | |||
| 228 | $f = explode("%", $data, 2); // We only want the first occurance of % exploded, not everything else aswell. |
||
| 229 | $age = array_shift($f); |
||
| 230 | $data = implode($f); |
||
| 231 | return array("age" => (int) $age, "data" => $data); |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Cleans up old and unused query cache files |
||
| 236 | */ |
||
| 237 | public function cleanUp() |
||
| 238 | { |
||
| 239 | $dir = opendir($this->cacheDir); |
||
| 240 | while($file = readdir($dir)) |
||
| 241 | { |
||
| 242 | if($file != "." && $file != "..") |
||
| 243 | { |
||
| 244 | $data = self::getData($file, false); |
||
| 245 | $age = $data["age"]; |
||
| 246 | $time = time(); |
||
| 247 | if($age <= $time) |
||
| 248 | { |
||
| 249 | @unlink($this->cacheDir.$file); |
||
| 250 | } |
||
| 251 | } |
||
| 252 | } |
||
| 253 | } |
||
| 254 | } |
||
| 255 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: