/usr/share/php/Icinga/Web/FileCache.php is in php-icinga 2.1.0-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | <?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
namespace Icinga\Web;
class FileCache
{
/**
* FileCache singleton instances
*
* @var array
*/
protected static $instances = array();
/**
* Cache instance base directory
*
* @var string
*/
protected $basedir;
/**
* Instance name
*
* @var string
*/
protected $name;
/**
* Whether the cache is enabled
*
* @var bool
*/
protected $enabled = false;
/**
* The protected constructor creates a new instance with the given name
*
* @param string $name Cache instance name
*/
protected function __construct($name)
{
$this->name = $name;
$tmpdir = sys_get_temp_dir();
$basedir = $tmpdir . '/FileCache_' . $name;
if (file_exists($basedir) && is_writeable($basedir)) {
$this->basedir = $basedir;
$this->enabled = true;
} elseif (file_exists($tmpdir) && is_writeable($tmpdir)) {
if (mkdir($basedir, '0750', true)) {
$this->enabled = true;
$this->basedir = $basedir;
}
}
}
/**
* Store the given content to the desired file name
*
* @param string $file new (relative) filename
* @param string $content the content to be stored
*
* @return bool whether the file has been stored
*/
public function store($file, $content)
{
if (! $this->enabled) {
return false;
}
return file_put_contents($this->filename($file), $content);
}
/**
* Find out whether a given file exists
*
* @param string $file the (relative) filename
* @param int $newerThan optional timestamp to compare against
*
* @return bool whether such file exists
*/
public function has($file, $newerThan = null)
{
if (! $this->enabled) {
return false;
}
$filename = $this->filename($file);
if (! file_exists($filename) || ! is_readable($filename)) {
return false;
}
if ($newerThan === null) {
return true;
}
$info = stat($file);
if ($info === false) {
return false;
}
return (int) $newerThan < $info['mtime'];
}
/**
* Get a specific file or false if no such file available
*
* @param string $file the disired file name
*
* @return string|bool Filename content or false
*/
public function get($file)
{
if ($this->has($file)) {
return file_get_contents($this->filename($file));
}
return false;
}
/**
* Send a specific file to the browser (output)
*
* @param string $file the disired file name
*
* @return bool Whether the file has been sent
*/
public function send($file)
{
if ($this->has($file)) {
readfile($this->filename($file));
return true;
}
return false;
}
/**
* Get absolute filename for a given file
*
* @param string $file the disired file name
*
* @return string absolute filename
*/
protected function filename($file)
{
return $this->basedir . '/' . $file;
}
/**
* Whether the given ETag matches a cached file
*
* If no ETag is given we'll try to fetch the one from the current
* HTTP request.
*
* @param string $file The cached file you want to check
* @param string $match The ETag to match against
*
* @return string|bool ETag on match, otherwise false
*/
public function etagMatchesCachedFile($file, $match = null)
{
return self::etagMatchesFiles($this->filename($file), $match);
}
/**
* Create an ETag for the given file
*
* @param string $file The desired cache file
*
* @return string your ETag
*/
public function etagForCachedFile($file)
{
return self::etagForFiles($this->filename($file));
}
/**
* Whether the given ETag matchesspecific file(s) on disk
*
* If no ETag is given we'll try to fetch the one from the current
* HTTP request. Respects HTTP Cache-Control: no-cache, if set.
*
* @param string|array $files file(s) to check
* @param string $match ETag to match against
*
* @return string|bool ETag on match, otherwise false
*/
public static function etagMatchesFiles($files, $match = null)
{
if ($match === null) {
$match = isset($_SERVER['HTTP_IF_NONE_MATCH'])
? trim($_SERVER['HTTP_IF_NONE_MATCH'], '"')
: false;
}
if (! $match) {
return false;
}
if (isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'no-cache') {
return false;
}
$etag = self::etagForFiles($files);
return $match === $etag ? $etag : false;
}
/**
* Create ETag for the given files
*
* Custom algorithm creating an ETag based on filenames, mtimes
* and file sizes. Supports single files or a list of files. This
* way we are able to create ETags for virtual files depending on
* multiple source files (e.g. compressed JS, CSS).
*
* @param string|array $files Single file or a list of such
*
* @return string The generated ETag
*/
public static function etagForFiles($files)
{
if (is_string($files)) {
$files = array($files);
}
$sizes = array();
$mtimes = array();
foreach ($files as $file) {
$file = realpath($file);
if ($file !== false && $info = stat($file)) {
$mtimes[] = $info['mtime'];
$sizes[] = $info['size'];
} else {
$mtimes[] = time();
$sizes[] = 0;
}
}
return sprintf(
'%s-%s-%s',
hash('crc32', implode('|', $files)),
hash('crc32', implode('|', $sizes)),
hash('crc32', implode('|', $mtimes))
);
}
/**
* Factory creating your cache instance
*
* @param string $name Instance name
*
* @return FileCache
*/
public static function instance($name = 'icingaweb')
{
if ($name !== 'icingaweb') {
$name = 'icingaweb/modules/' . $name;
}
if (!array_key_exists($name, self::$instances)) {
self::$instances[$name] = new static($name);
}
return self::$instances[$name];
}
}
|