Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 8 | * |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 9 | * Copyright (c) 2014 - 2018, British Columbia Institute of Technology |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 32 | * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 35 | * @since Version 2.0 |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 36 | * @filesource |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 39 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 40 | /** |
Andrey Andreev | c9195a7 | 2012-06-12 03:49:03 +0300 | [diff] [blame] | 41 | * CodeIgniter File Caching Class |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 42 | * |
| 43 | * @package CodeIgniter |
| 44 | * @subpackage Libraries |
| 45 | * @category Core |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 46 | * @author EllisLab Dev Team |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 47 | * @link |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 48 | */ |
Phil Sturgeon | eb2dcda | 2011-04-02 14:44:58 +0100 | [diff] [blame] | 49 | class CI_Cache_file extends CI_Driver { |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 50 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 51 | /** |
| 52 | * Directory in which to save cache files |
| 53 | * |
| 54 | * @var string |
| 55 | */ |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 56 | protected $_cache_path; |
| 57 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 58 | /** |
| 59 | * Initialize file-based cache |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 60 | * |
| 61 | * @return void |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 62 | */ |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 63 | public function __construct() |
| 64 | { |
| 65 | $CI =& get_instance(); |
| 66 | $CI->load->helper('file'); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 67 | $path = $CI->config->item('cache_path'); |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 68 | $this->_cache_path = ($path === '') ? APPPATH.'cache/' : $path; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | // ------------------------------------------------------------------------ |
| 72 | |
| 73 | /** |
| 74 | * Fetch from cache |
| 75 | * |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 76 | * @param string $id Cache ID |
| 77 | * @return mixed Data on success, FALSE on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 78 | */ |
| 79 | public function get($id) |
| 80 | { |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 81 | $data = $this->_get($id); |
| 82 | return is_array($data) ? $data['data'] : FALSE; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | // ------------------------------------------------------------------------ |
| 86 | |
| 87 | /** |
| 88 | * Save into cache |
| 89 | * |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 90 | * @param string $id Cache ID |
| 91 | * @param mixed $data Data to store |
| 92 | * @param int $ttl Time to live in seconds |
| 93 | * @param bool $raw Whether to store the raw value (unused) |
| 94 | * @return bool TRUE on success, FALSE on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 95 | */ |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 96 | public function save($id, $data, $ttl = 60, $raw = FALSE) |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 97 | { |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 98 | $contents = array( |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 99 | 'time' => time(), |
| 100 | 'ttl' => $ttl, |
| 101 | 'data' => $data |
| 102 | ); |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 103 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 104 | if (write_file($this->_cache_path.$id, serialize($contents))) |
| 105 | { |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 106 | chmod($this->_cache_path.$id, 0640); |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 107 | return TRUE; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | return FALSE; |
| 111 | } |
| 112 | |
| 113 | // ------------------------------------------------------------------------ |
| 114 | |
| 115 | /** |
| 116 | * Delete from Cache |
| 117 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 118 | * @param mixed unique identifier of item in cache |
| 119 | * @return bool true on success/false on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 120 | */ |
| 121 | public function delete($id) |
| 122 | { |
Andrey Andreev | 04649d9 | 2016-08-11 14:13:11 +0300 | [diff] [blame] | 123 | return is_file($this->_cache_path.$id) ? unlink($this->_cache_path.$id) : FALSE; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | // ------------------------------------------------------------------------ |
| 127 | |
| 128 | /** |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 129 | * Increment a raw value |
| 130 | * |
| 131 | * @param string $id Cache ID |
| 132 | * @param int $offset Step/value to add |
| 133 | * @return New value on success, FALSE on failure |
| 134 | */ |
| 135 | public function increment($id, $offset = 1) |
| 136 | { |
| 137 | $data = $this->_get($id); |
| 138 | |
Andrey Andreev | 5f0799a | 2014-07-31 13:32:41 +0300 | [diff] [blame] | 139 | if ($data === FALSE) |
| 140 | { |
| 141 | $data = array('data' => 0, 'ttl' => 60); |
| 142 | } |
| 143 | elseif ( ! is_int($data['data'])) |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 144 | { |
| 145 | return FALSE; |
| 146 | } |
| 147 | |
| 148 | $new_value = $data['data'] + $offset; |
| 149 | return $this->save($id, $new_value, $data['ttl']) |
| 150 | ? $new_value |
| 151 | : FALSE; |
| 152 | } |
| 153 | |
| 154 | // ------------------------------------------------------------------------ |
| 155 | |
| 156 | /** |
| 157 | * Decrement a raw value |
| 158 | * |
| 159 | * @param string $id Cache ID |
| 160 | * @param int $offset Step/value to reduce by |
| 161 | * @return New value on success, FALSE on failure |
| 162 | */ |
| 163 | public function decrement($id, $offset = 1) |
| 164 | { |
| 165 | $data = $this->_get($id); |
| 166 | |
Andrey Andreev | 5f0799a | 2014-07-31 13:32:41 +0300 | [diff] [blame] | 167 | if ($data === FALSE) |
| 168 | { |
| 169 | $data = array('data' => 0, 'ttl' => 60); |
| 170 | } |
| 171 | elseif ( ! is_int($data['data'])) |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 172 | { |
| 173 | return FALSE; |
| 174 | } |
| 175 | |
| 176 | $new_value = $data['data'] - $offset; |
| 177 | return $this->save($id, $new_value, $data['ttl']) |
| 178 | ? $new_value |
| 179 | : FALSE; |
| 180 | } |
| 181 | |
| 182 | // ------------------------------------------------------------------------ |
| 183 | |
| 184 | /** |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 185 | * Clean the Cache |
| 186 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 187 | * @return bool false on failure/true on success |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 188 | */ |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 189 | public function clean() |
| 190 | { |
vlakoff | 83c344e | 2013-03-04 14:30:08 +0100 | [diff] [blame] | 191 | return delete_files($this->_cache_path, FALSE, TRUE); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | // ------------------------------------------------------------------------ |
| 195 | |
| 196 | /** |
| 197 | * Cache Info |
| 198 | * |
| 199 | * Not supported by file-based caching |
| 200 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 201 | * @param string user/filehits |
| 202 | * @return mixed FALSE |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 203 | */ |
| 204 | public function cache_info($type = NULL) |
| 205 | { |
| 206 | return get_dir_file_info($this->_cache_path); |
| 207 | } |
| 208 | |
| 209 | // ------------------------------------------------------------------------ |
| 210 | |
| 211 | /** |
| 212 | * Get Cache Metadata |
| 213 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 214 | * @param mixed key to get cache metadata on |
| 215 | * @return mixed FALSE on failure, array on success. |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 216 | */ |
| 217 | public function get_metadata($id) |
| 218 | { |
Andrey Andreev | 04649d9 | 2016-08-11 14:13:11 +0300 | [diff] [blame] | 219 | if ( ! is_file($this->_cache_path.$id)) |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 220 | { |
| 221 | return FALSE; |
| 222 | } |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 223 | |
Andrey Andreev | 0f0b769 | 2012-06-07 14:57:04 +0300 | [diff] [blame] | 224 | $data = unserialize(file_get_contents($this->_cache_path.$id)); |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 225 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 226 | if (is_array($data)) |
| 227 | { |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 228 | $mtime = filemtime($this->_cache_path.$id); |
| 229 | |
Andrey Andreev | 9514dc3 | 2016-08-11 15:14:33 +0300 | [diff] [blame] | 230 | if ( ! isset($data['ttl'], $data['time'])) |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 231 | { |
| 232 | return FALSE; |
| 233 | } |
| 234 | |
| 235 | return array( |
Andrey Andreev | 9514dc3 | 2016-08-11 15:14:33 +0300 | [diff] [blame] | 236 | 'expire' => $data['time'] + $data['ttl'], |
Ben Edmunds | 3db2849 | 2011-08-20 14:12:12 -0500 | [diff] [blame] | 237 | 'mtime' => $mtime |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 238 | ); |
| 239 | } |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 240 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 241 | return FALSE; |
| 242 | } |
| 243 | |
| 244 | // ------------------------------------------------------------------------ |
| 245 | |
| 246 | /** |
| 247 | * Is supported |
| 248 | * |
| 249 | * In the file driver, check to see that the cache directory is indeed writable |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 250 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 251 | * @return bool |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 252 | */ |
| 253 | public function is_supported() |
| 254 | { |
| 255 | return is_really_writable($this->_cache_path); |
| 256 | } |
| 257 | |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 258 | // ------------------------------------------------------------------------ |
| 259 | |
| 260 | /** |
| 261 | * Get all data |
| 262 | * |
| 263 | * Internal method to get all the relevant data about a cache item |
| 264 | * |
| 265 | * @param string $id Cache ID |
| 266 | * @return mixed Data array on success or FALSE on failure |
| 267 | */ |
| 268 | protected function _get($id) |
| 269 | { |
Andrey Andreev | 4dac6eb | 2015-08-31 17:52:27 +0300 | [diff] [blame] | 270 | if ( ! is_file($this->_cache_path.$id)) |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 271 | { |
| 272 | return FALSE; |
| 273 | } |
| 274 | |
| 275 | $data = unserialize(file_get_contents($this->_cache_path.$id)); |
| 276 | |
| 277 | if ($data['ttl'] > 0 && time() > $data['time'] + $data['ttl']) |
| 278 | { |
| 279 | unlink($this->_cache_path.$id); |
| 280 | return FALSE; |
| 281 | } |
| 282 | |
| 283 | return $data; |
| 284 | } |
| 285 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 286 | } |