$maxSize) || ($maxAge !== null && time() - $stat['mtime'] >= $maxAge)) { return null; } $data = @file_get_contents($file); return is_string($data) && ($maxSize === null || strlen($data) <= $maxSize) ? $data : null; } /** * Re-check the cache directory, then atomically replace a 0600 entry in it. * * @param string $dir cache directory * @param string $file cache filename * @param string $data cache contents * @param string $tmpPrefix temporary file prefix * @param string $cleanupLog error_log prefix when the temporary file cannot be removed * * @return bool true when the cache entry was written */ public static function write($dir, $file, $data, $tmpPrefix, $cleanupLog) { if (!self::ensureDir($dir)) { return false; } return self::writeAtomic($dir, $file, $data, $tmpPrefix, 0600, $cleanupLog); } /** * Atomically replace a file without following a pre-created link. * * @param string $dir directory for the temporary file * @param string $file target filename * @param string $data file contents * @param string $tmpPrefix temporary file prefix * @param int $mode permissions of the new file * @param string $cleanupLog error_log prefix when the temporary file cannot be removed * * @return bool true when the file was replaced */ public static function writeAtomic($dir, $file, $data, $tmpPrefix, $mode, $cleanupLog) { $tmpFile = tempnam($dir, $tmpPrefix); if ($tmpFile === false) { return false; } try { $written = file_put_contents($tmpFile, $data, LOCK_EX); if ($written !== strlen($data) || !@chmod($tmpFile, $mode)) { return false; } return @rename($tmpFile, $file); } finally { if ((is_file($tmpFile) || is_link($tmpFile)) && !@unlink($tmpFile)) { error_log("{$cleanupLog}: {$tmpFile}"); } } } }