00001 <?php
00030 class Zend_Cache_Backend
00031 {
00044 protected $_directives = array(
00045 'lifetime' => 3600,
00046 'logging' => false,
00047 'logger' => null
00048 );
00049
00055 protected $_options = array();
00056
00064 public function __construct(array $options = array())
00065 {
00066 while (list($name, $value) = each($options)) {
00067 $this->setOption($name, $value);
00068 }
00069 }
00070
00078 public function setDirectives($directives)
00079 {
00080 if (!is_array($directives)) Zend_Cache::throwException('Directives parameter must be an array');
00081 while (list($name, $value) = each($directives)) {
00082 if (!is_string($name)) {
00083 Zend_Cache::throwException("Incorrect option name : $name");
00084 }
00085 $name = strtolower($name);
00086 if (array_key_exists($name, $this->_directives)) {
00087 $this->_directives[$name] = $value;
00088 }
00089
00090 }
00091
00092 $this->_loggerSanity();
00093 }
00094
00103 public function setOption($name, $value)
00104 {
00105 if (!is_string($name)) {
00106 Zend_Cache::throwException("Incorrect option name : $name");
00107 }
00108 $name = strtolower($name);
00109 if (array_key_exists($name, $this->_options)) {
00110 $this->_options[$name] = $value;
00111 }
00112 }
00113
00123 public function getLifetime($specificLifetime)
00124 {
00125 if ($specificLifetime === false) {
00126 return $this->_directives['lifetime'];
00127 }
00128 return $specificLifetime;
00129 }
00130
00139 public function isAutomaticCleaningAvailable()
00140 {
00141 return true;
00142 }
00143
00152 public function getTmpDir()
00153 {
00154 $tmpdir = array();
00155 foreach (array($_ENV, $_SERVER) as $tab) {
00156 foreach (array('TMPDIR', 'TEMP', 'TMP', 'windir', 'SystemRoot') as $key) {
00157 if (isset($tab[$key])) {
00158 if (($key == 'windir') or ($key == 'SystemRoot')) {
00159 $dir = realpath($tab[$key] . '\\temp');
00160 } else {
00161 $dir = realpath($tab[$key]);
00162 }
00163 if ($this->_isGoodTmpDir($dir)) {
00164 return $dir;
00165 }
00166 }
00167 }
00168 }
00169 $upload = ini_get('upload_tmp_dir');
00170 if ($upload) {
00171 $dir = realpath($upload);
00172 if ($this->_isGoodTmpDir($dir)) {
00173 return $dir;
00174 }
00175 }
00176 if (function_exists('sys_get_temp_dir')) {
00177 $dir = sys_get_temp_dir();
00178 if ($this->_isGoodTmpDir($dir)) {
00179 return $dir;
00180 }
00181 }
00182
00183 $tempFile = tempnam(md5(uniqid(rand(), TRUE)), '');
00184 if ($tempFile) {
00185 $dir = realpath(dirname($tempFile));
00186 unlink($tempFile);
00187 if ($this->_isGoodTmpDir($dir)) {
00188 return $dir;
00189 }
00190 }
00191 if ($this->_isGoodTmpDir('/tmp')) {
00192 return '/tmp';
00193 }
00194 if ($this->_isGoodTmpDir('\\temp')) {
00195 return '\\temp';
00196 }
00197 Zend_Cache::throwException('Could not determine temp directory, please specify a cache_dir manually');
00198 }
00199
00206 protected function _isGoodTmpDir($dir)
00207 {
00208 if (is_readable($dir)) {
00209 if (is_writable($dir)) {
00210 return true;
00211 }
00212 }
00213 return false;
00214 }
00215
00224 protected function _loggerSanity()
00225 {
00226 if (!isset($this->_directives['logging']) || !$this->_directives['logging']) {
00227 return;
00228 }
00229 try {
00233 require_once 'Zend/Log.php';
00234 } catch (Zend_Exception $e) {
00235 Zend_Cache::throwException('Logging feature is enabled but the Zend_Log class is not available');
00236 }
00237 if (isset($this->_directives['logger'])) {
00238 if ($this->_directives['logger'] instanceof Zend_Log) {
00239 return;
00240 } else {
00241 Zend_Cache::throwException('Logger object is not an instance of Zend_Log class.');
00242 }
00243 }
00244
00245 require_once 'Zend/Log/Writer/Stream.php';
00246 $logger = new Zend_Log(new Zend_Log_Writer_Stream('php://output'));
00247 $this->_directives['logger'] = $logger;
00248 }
00249
00257 protected function _log($message, $priority = 4)
00258 {
00259 if (!$this->_directives['logging']) {
00260 return;
00261 }
00262
00263 if (!isset($this->_directives['logger'])) {
00264 Zend_Cache::throwException('Logging is enabled but logger is not set.');
00265 }
00266 $logger = $this->_directives['logger'];
00267 if (!$logger instanceof Zend_Log) {
00268 Zend_Cache::throwException('Logger object is not an instance of Zend_Log class.');
00269 }
00270 $logger->log($message, $priority);
00271 }
00272 }