Nico Huber | ee52fbc | 2023-06-24 11:52:57 +0000 | [diff] [blame] | 1 | <?php |
| 2 | /* |
| 3 | * SimpleID |
| 4 | * |
| 5 | * Copyright (C) Kelvin Mo 2007-9 |
| 6 | * |
| 7 | * Includes code Drupal OpenID module (http://drupal.org/project/openid) |
| 8 | * Rowan Kerr <rowan@standardinteractive.com> |
| 9 | * James Walker <james@bryght.com> |
| 10 | * |
| 11 | * Copyright (C) Rowan Kerr and James Walker |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or |
| 14 | * modify it under the terms of the GNU General Public |
| 15 | * License as published by the Free Software Foundation; either |
| 16 | * version 2 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | * General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public |
| 24 | * License along with this program; if not, write to the Free |
| 25 | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 26 | * |
| 27 | * $Id$ |
| 28 | */ |
| 29 | |
| 30 | /** |
| 31 | * Functions for caching and persistence. |
| 32 | * |
| 33 | * @package simpleid |
| 34 | * @filesource |
| 35 | */ |
| 36 | |
| 37 | /** |
| 38 | * Stores data into the cache. |
| 39 | * |
| 40 | * @param string $type the type of data in the cache |
| 41 | * @param string $key an identifier |
| 42 | * @param mixed $data the data to store |
| 43 | * @param int $time if present, sets the modification time of the cache file to this |
| 44 | * time |
| 45 | */ |
| 46 | function cache_set($type, $key, $data, $time = NULL) { |
| 47 | $filename = _cache_get_name($type, $key); |
| 48 | if (!file_exists(dirname($filename))) mkdir(dirname($filename), 0775, true); |
| 49 | $file = fopen($filename, 'w'); |
| 50 | fwrite($file, serialize($data)); |
| 51 | fclose($file); |
| 52 | |
| 53 | if ($time != NULL) { |
| 54 | touch($filename, $time); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Obtains data from the cache. |
| 60 | * |
| 61 | * @param string $type the type of data in the cache |
| 62 | * @param string $key an identifier |
| 63 | * @return mixed the data associated with the type and key, or NULL if the cache |
| 64 | * does not contain the requested data. |
| 65 | */ |
| 66 | function cache_get($type, $key) { |
| 67 | $filename = _cache_get_name($type, $key); |
| 68 | |
| 69 | if (!file_exists($filename)) return NULL; |
| 70 | |
| 71 | return unserialize(file_get_contents($filename)); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Obtains all data of a particular type from the cache. |
| 76 | * |
| 77 | * @param string $type the type of data in the cache |
| 78 | * @return mixed an array of data associated with the type, or NULL if the cache |
| 79 | * does not contain the requested data. |
| 80 | */ |
| 81 | function cache_get_all($type) { |
| 82 | $r = array(); |
| 83 | |
| 84 | if (!is_dir(CACHE_DIR . '/' . $type)) return $r; |
| 85 | $dir = opendir(CACHE_DIR . '/' . $type); |
| 86 | |
| 87 | while (($file = readdir($dir)) !== false) { |
| 88 | $filename = CACHE_DIR . '/' . $type . '/' . $file; |
| 89 | |
| 90 | if (filetype($filename) != "file") continue; |
| 91 | |
| 92 | $r[] = unserialize(file_get_contents($filename)); |
| 93 | } |
| 94 | |
| 95 | closedir($dir); |
| 96 | |
| 97 | return $r; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Deletes data from the cache. |
| 102 | * |
| 103 | * @param string $type the type of data in the cache |
| 104 | * @param string $key an identifier |
| 105 | */ |
| 106 | function cache_delete($type, $key) { |
| 107 | $filename = _cache_get_name($type, $key); |
| 108 | if (file_exists($filename)) unlink($filename); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Garbage collects data stored the cache. Data is deleted if it was stored |
| 113 | * for longer than the specified expiry. |
| 114 | * |
| 115 | * This function is deprecated, use {@link cache_expire()}. |
| 116 | * |
| 117 | * @param int $expiry the expiry time, in seconds, after which data will be deleted |
| 118 | * @param string $type the type of data in the cache |
| 119 | * @deprecated |
| 120 | */ |
| 121 | function cache_gc($expiry, $type = NULL) { |
| 122 | if ($type == NULL) { |
| 123 | $dir = opendir(CACHE_DIR); |
| 124 | |
| 125 | while (($file = readdir($dir)) !== false) { |
| 126 | $filename = CACHE_DIR . '/' . $file; |
| 127 | if (in_array(filetype($filename), array('dir', 'link'))) |
| 128 | cache_gc($expiry, $file); |
| 129 | } |
| 130 | } else { |
| 131 | if (!is_dir(CACHE_DIR . '/' . $type)) return; |
| 132 | $dir = opendir(CACHE_DIR . '/' . $type); |
| 133 | while (($file = readdir($dir)) !== false) { |
| 134 | $filename = CACHE_DIR . '/' . $type . '/' . $file; |
| 135 | |
| 136 | if ((filetype($filename) == "file") && (filectime($filename) < time() - $expiry)) |
| 137 | unlink($filename); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | closedir($dir); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Garbage collects data stored the cache. Data is deleted if it was stored |
| 146 | * for longer than the specified expiry. |
| 147 | * |
| 148 | * The parameter to this function takes either an integer or an array. If the |
| 149 | * parameter is an integer, everything in the cache older than the specified |
| 150 | * time (in seconds) will be deleted. If the parameter is an array, |
| 151 | * cache items of the type specified in the key to the array, older than the |
| 152 | * corresponding value will be deleted. |
| 153 | * |
| 154 | * This function is deprecated, use {@link cache_expire()}. |
| 155 | * |
| 156 | * @param int|array $params the expiry time, in seconds, after which data will be deleted, |
| 157 | * or an array specifiying the expiry time for each type |
| 158 | */ |
| 159 | function cache_expire($params) { |
| 160 | |
| 161 | $dirs = array(); |
| 162 | array_push($dirs, CACHE_DIR); |
| 163 | while (sizeof($dirs)) { |
| 164 | $dirname = array_pop($dirs); |
| 165 | if (($dir = opendir($dirname)) === false) { |
| 166 | continue; |
| 167 | } |
| 168 | |
| 169 | while (($file = readdir($dir)) !== false) { |
| 170 | $expiry = NULL; |
| 171 | if ($file == '.' || $file == '..') { |
| 172 | continue; |
| 173 | } |
| 174 | $filename = $dirname . '/' . $file; |
| 175 | |
| 176 | if (is_dir($filename)) { |
| 177 | array_push($dirs, $filename); |
| 178 | continue; |
| 179 | } |
| 180 | |
| 181 | if (is_int($params)) { |
| 182 | $expiry = $params; |
| 183 | } elseif (is_array($params)) { |
| 184 | foreach ($params as $type => $param) { |
| 185 | if (strpos($filename, $type) !== false) { |
| 186 | $expiry = $param; |
| 187 | break; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | if (!is_null($expiry) && (filetype($filename) == "file") && (filectime($filename) < time() - $expiry)) { |
| 193 | unlink($filename); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | closedir($dir); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Returns the time remaining, in seconds, before the data associated with the |
| 203 | * type and key become subject to garbage collection by {@link cache_gc()}. |
| 204 | * |
| 205 | * @param string $type the type of data in the cache |
| 206 | * @param string $key an identifier |
| 207 | * @param int $expiry the expiry time, in seconds, which would be passed onto the |
| 208 | * {@link cache_gc()} function |
| 209 | * @return int the time remaining before expiry, rounded downwards, |
| 210 | * or zero if the cache does not contain the requested data |
| 211 | * @since 0.8 |
| 212 | */ |
| 213 | function cache_ttl($type, $key, $expiry) { |
| 214 | $filename = _cache_get_name($type, $key); |
| 215 | |
| 216 | if (!file_exists($filename)) return 0; |
| 217 | |
| 218 | return filectime($filename) - (time() - $expiry) - 1; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Returns the name of the cache data file, given a type and an identifier. |
| 223 | * |
| 224 | * @param string $type the type of data in the cache |
| 225 | * @param string $key an identifier |
| 226 | * @return string a file name |
| 227 | */ |
| 228 | function _cache_get_name($type, $key) { |
| 229 | return CACHE_DIR . '/' . $type . '/' . md5($key) . '.cache'; |
| 230 | } |
| 231 | |
| 232 | ?> |