File indexing completed on 2025-02-02 05:48:53
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Cache 0017 * @subpackage Zend_Cache_Backend 0018 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0019 * @license http://framework.zend.com/license/new-bsd New BSD License 0020 * @version $Id$ 0021 */ 0022 0023 0024 /** 0025 * @package Zend_Cache 0026 * @subpackage Zend_Cache_Backend 0027 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0028 * @license http://framework.zend.com/license/new-bsd New BSD License 0029 */ 0030 class Zend_Cache_Backend 0031 { 0032 /** 0033 * Frontend or Core directives 0034 * 0035 * =====> (int) lifetime : 0036 * - Cache lifetime (in seconds) 0037 * - If null, the cache is valid forever 0038 * 0039 * =====> (int) logging : 0040 * - if set to true, a logging is activated throw Zend_Log 0041 * 0042 * @var array directives 0043 */ 0044 protected $_directives = array( 0045 'lifetime' => 3600, 0046 'logging' => false, 0047 'logger' => null 0048 ); 0049 0050 /** 0051 * Available options 0052 * 0053 * @var array available options 0054 */ 0055 protected $_options = array(); 0056 0057 /** 0058 * Constructor 0059 * 0060 * @param array $options Associative array of options 0061 */ 0062 public function __construct(array $options = array()) 0063 { 0064 foreach ($options as $name => $value) { 0065 $this->setOption($name, $value); 0066 } 0067 } 0068 0069 /** 0070 * Set the frontend directives 0071 * 0072 * @param array $directives Assoc of directives 0073 * @throws Zend_Cache_Exception 0074 * @return void 0075 */ 0076 public function setDirectives($directives) 0077 { 0078 if (!is_array($directives)) Zend_Cache::throwException('Directives parameter must be an array'); 0079 while (list($name, $value) = each($directives)) { 0080 if (!is_string($name)) { 0081 Zend_Cache::throwException("Incorrect option name : $name"); 0082 } 0083 $name = strtolower($name); 0084 if (array_key_exists($name, $this->_directives)) { 0085 $this->_directives[$name] = $value; 0086 } 0087 0088 } 0089 0090 $this->_loggerSanity(); 0091 } 0092 0093 /** 0094 * Set an option 0095 * 0096 * @param string $name 0097 * @param mixed $value 0098 * @throws Zend_Cache_Exception 0099 * @return void 0100 */ 0101 public function setOption($name, $value) 0102 { 0103 if (!is_string($name)) { 0104 Zend_Cache::throwException("Incorrect option name : $name"); 0105 } 0106 $name = strtolower($name); 0107 if (array_key_exists($name, $this->_options)) { 0108 $this->_options[$name] = $value; 0109 } 0110 } 0111 0112 /** 0113 * Returns an option 0114 * 0115 * @param string $name Optional, the options name to return 0116 * @throws Zend_Cache_Exceptions 0117 * @return mixed 0118 */ 0119 public function getOption($name) 0120 { 0121 $name = strtolower($name); 0122 0123 if (array_key_exists($name, $this->_options)) { 0124 return $this->_options[$name]; 0125 } 0126 0127 if (array_key_exists($name, $this->_directives)) { 0128 return $this->_directives[$name]; 0129 } 0130 0131 Zend_Cache::throwException("Incorrect option name : {$name}"); 0132 } 0133 0134 /** 0135 * Get the life time 0136 * 0137 * if $specificLifetime is not false, the given specific life time is used 0138 * else, the global lifetime is used 0139 * 0140 * @param int $specificLifetime 0141 * @return int Cache life time 0142 */ 0143 public function getLifetime($specificLifetime) 0144 { 0145 if ($specificLifetime === false) { 0146 return $this->_directives['lifetime']; 0147 } 0148 return $specificLifetime; 0149 } 0150 0151 /** 0152 * Return true if the automatic cleaning is available for the backend 0153 * 0154 * DEPRECATED : use getCapabilities() instead 0155 * 0156 * @deprecated 0157 * @return boolean 0158 */ 0159 public function isAutomaticCleaningAvailable() 0160 { 0161 return true; 0162 } 0163 0164 /** 0165 * Determine system TMP directory and detect if we have read access 0166 * 0167 * inspired from Zend_File_Transfer_Adapter_Abstract 0168 * 0169 * @return string 0170 * @throws Zend_Cache_Exception if unable to determine directory 0171 */ 0172 public function getTmpDir() 0173 { 0174 $tmpdir = array(); 0175 foreach (array($_ENV, $_SERVER) as $tab) { 0176 foreach (array('TMPDIR', 'TEMP', 'TMP', 'windir', 'SystemRoot') as $key) { 0177 if (isset($tab[$key]) && is_string($tab[$key])) { 0178 if (($key == 'windir') or ($key == 'SystemRoot')) { 0179 $dir = realpath($tab[$key] . '\\temp'); 0180 } else { 0181 $dir = realpath($tab[$key]); 0182 } 0183 if ($this->_isGoodTmpDir($dir)) { 0184 return $dir; 0185 } 0186 } 0187 } 0188 } 0189 $upload = ini_get('upload_tmp_dir'); 0190 if ($upload) { 0191 $dir = realpath($upload); 0192 if ($this->_isGoodTmpDir($dir)) { 0193 return $dir; 0194 } 0195 } 0196 if (function_exists('sys_get_temp_dir')) { 0197 $dir = sys_get_temp_dir(); 0198 if ($this->_isGoodTmpDir($dir)) { 0199 return $dir; 0200 } 0201 } 0202 // Attemp to detect by creating a temporary file 0203 $tempFile = tempnam(md5(uniqid(rand(), TRUE)), ''); 0204 if ($tempFile) { 0205 $dir = realpath(dirname($tempFile)); 0206 unlink($tempFile); 0207 if ($this->_isGoodTmpDir($dir)) { 0208 return $dir; 0209 } 0210 } 0211 if ($this->_isGoodTmpDir('/tmp')) { 0212 return '/tmp'; 0213 } 0214 if ($this->_isGoodTmpDir('\\temp')) { 0215 return '\\temp'; 0216 } 0217 Zend_Cache::throwException('Could not determine temp directory, please specify a cache_dir manually'); 0218 } 0219 0220 /** 0221 * Verify if the given temporary directory is readable and writable 0222 * 0223 * @param string $dir temporary directory 0224 * @return boolean true if the directory is ok 0225 */ 0226 protected function _isGoodTmpDir($dir) 0227 { 0228 if (is_readable($dir)) { 0229 if (is_writable($dir)) { 0230 return true; 0231 } 0232 } 0233 return false; 0234 } 0235 0236 /** 0237 * Make sure if we enable logging that the Zend_Log class 0238 * is available. 0239 * Create a default log object if none is set. 0240 * 0241 * @throws Zend_Cache_Exception 0242 * @return void 0243 */ 0244 protected function _loggerSanity() 0245 { 0246 if (!isset($this->_directives['logging']) || !$this->_directives['logging']) { 0247 return; 0248 } 0249 0250 if (isset($this->_directives['logger'])) { 0251 if ($this->_directives['logger'] instanceof Zend_Log) { 0252 return; 0253 } 0254 Zend_Cache::throwException('Logger object is not an instance of Zend_Log class.'); 0255 } 0256 0257 // Create a default logger to the standard output stream 0258 // require_once 'Zend/Log.php'; 0259 // require_once 'Zend/Log/Writer/Stream.php'; 0260 // require_once 'Zend/Log/Filter/Priority.php'; 0261 $logger = new Zend_Log(new Zend_Log_Writer_Stream('php://output')); 0262 $logger->addFilter(new Zend_Log_Filter_Priority(Zend_Log::WARN, '<=')); 0263 $this->_directives['logger'] = $logger; 0264 } 0265 0266 /** 0267 * Log a message at the WARN (4) priority. 0268 * 0269 * @param string $message 0270 * @param int $priority 0271 * @return void 0272 */ 0273 protected function _log($message, $priority = 4) 0274 { 0275 if (!$this->_directives['logging']) { 0276 return; 0277 } 0278 0279 if (!isset($this->_directives['logger'])) { 0280 Zend_Cache::throwException('Logging is enabled but logger is not set.'); 0281 } 0282 $logger = $this->_directives['logger']; 0283 if (!$logger instanceof Zend_Log) { 0284 Zend_Cache::throwException('Logger object is not an instance of Zend_Log class.'); 0285 } 0286 $logger->log($message, $priority); 0287 } 0288 }