]> git.immae.eu Git - github/wallabag/wallabag.git/blame - inc/3rdparty/libraries/Zend/Cache/Backend.php
htmlawed via composer
[github/wallabag/wallabag.git] / inc / 3rdparty / libraries / Zend / Cache / Backend.php
CommitLineData
42c80841
NL
1<?php
2/**
3 * Zend Framework
4 *
5 * LICENSE
6 *
7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
14 *
15 * @category Zend
16 * @package Zend_Cache
17 * @subpackage Zend_Cache_Backend
18 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id: Backend.php 24989 2012-06-21 07:24:13Z mabe $
21 */
22
23
24/**
25 * @package Zend_Cache
26 * @subpackage Zend_Cache_Backend
27 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
28 * @license http://framework.zend.com/license/new-bsd New BSD License
29 */
30class Zend_Cache_Backend
31{
32 /**
33 * Frontend or Core directives
34 *
35 * =====> (int) lifetime :
36 * - Cache lifetime (in seconds)
37 * - If null, the cache is valid forever
38 *
39 * =====> (int) logging :
40 * - if set to true, a logging is activated throw Zend_Log
41 *
42 * @var array directives
43 */
44 protected $_directives = array(
45 'lifetime' => 3600,
46 'logging' => false,
47 'logger' => null
48 );
49
50 /**
51 * Available options
52 *
53 * @var array available options
54 */
55 protected $_options = array();
56
57 /**
58 * Constructor
59 *
60 * @param array $options Associative array of options
61 * @throws Zend_Cache_Exception
62 * @return void
63 */
64 public function __construct(array $options = array())
65 {
66 while (list($name, $value) = each($options)) {
67 $this->setOption($name, $value);
68 }
69 }
70
71 /**
72 * Set the frontend directives
73 *
74 * @param array $directives Assoc of directives
75 * @throws Zend_Cache_Exception
76 * @return void
77 */
78 public function setDirectives($directives)
79 {
80 if (!is_array($directives)) Zend_Cache::throwException('Directives parameter must be an array');
81 while (list($name, $value) = each($directives)) {
82 if (!is_string($name)) {
83 Zend_Cache::throwException("Incorrect option name : $name");
84 }
85 $name = strtolower($name);
86 if (array_key_exists($name, $this->_directives)) {
87 $this->_directives[$name] = $value;
88 }
89
90 }
91
92 $this->_loggerSanity();
93 }
94
95 /**
96 * Set an option
97 *
98 * @param string $name
99 * @param mixed $value
100 * @throws Zend_Cache_Exception
101 * @return void
102 */
103 public function setOption($name, $value)
104 {
105 if (!is_string($name)) {
106 Zend_Cache::throwException("Incorrect option name : $name");
107 }
108 $name = strtolower($name);
109 if (array_key_exists($name, $this->_options)) {
110 $this->_options[$name] = $value;
111 }
112 }
113
114 /**
115 * Returns an option
116 *
117 * @param string $name Optional, the options name to return
118 * @throws Zend_Cache_Exceptions
119 * @return mixed
120 */
121 public function getOption($name)
122 {
123 $name = strtolower($name);
124
125 if (array_key_exists($name, $this->_options)) {
126 return $this->_options[$name];
127 }
128
129 if (array_key_exists($name, $this->_directives)) {
130 return $this->_directives[$name];
131 }
132
133 Zend_Cache::throwException("Incorrect option name : {$name}");
134 }
135
136 /**
137 * Get the life time
138 *
139 * if $specificLifetime is not false, the given specific life time is used
140 * else, the global lifetime is used
141 *
142 * @param int $specificLifetime
143 * @return int Cache life time
144 */
145 public function getLifetime($specificLifetime)
146 {
147 if ($specificLifetime === false) {
148 return $this->_directives['lifetime'];
149 }
150 return $specificLifetime;
151 }
152
153 /**
154 * Return true if the automatic cleaning is available for the backend
155 *
156 * DEPRECATED : use getCapabilities() instead
157 *
158 * @deprecated
159 * @return boolean
160 */
161 public function isAutomaticCleaningAvailable()
162 {
163 return true;
164 }
165
166 /**
167 * Determine system TMP directory and detect if we have read access
168 *
169 * inspired from Zend_File_Transfer_Adapter_Abstract
170 *
171 * @return string
172 * @throws Zend_Cache_Exception if unable to determine directory
173 */
174 public function getTmpDir()
175 {
176 $tmpdir = array();
177 foreach (array($_ENV, $_SERVER) as $tab) {
178 foreach (array('TMPDIR', 'TEMP', 'TMP', 'windir', 'SystemRoot') as $key) {
179 if (isset($tab[$key]) && is_string($tab[$key])) {
180 if (($key == 'windir') or ($key == 'SystemRoot')) {
181 $dir = realpath($tab[$key] . '\\temp');
182 } else {
183 $dir = realpath($tab[$key]);
184 }
185 if ($this->_isGoodTmpDir($dir)) {
186 return $dir;
187 }
188 }
189 }
190 }
191 $upload = ini_get('upload_tmp_dir');
192 if ($upload) {
193 $dir = realpath($upload);
194 if ($this->_isGoodTmpDir($dir)) {
195 return $dir;
196 }
197 }
198 if (function_exists('sys_get_temp_dir')) {
199 $dir = sys_get_temp_dir();
200 if ($this->_isGoodTmpDir($dir)) {
201 return $dir;
202 }
203 }
204 // Attemp to detect by creating a temporary file
205 $tempFile = tempnam(md5(uniqid(rand(), TRUE)), '');
206 if ($tempFile) {
207 $dir = realpath(dirname($tempFile));
208 unlink($tempFile);
209 if ($this->_isGoodTmpDir($dir)) {
210 return $dir;
211 }
212 }
213 if ($this->_isGoodTmpDir('/tmp')) {
214 return '/tmp';
215 }
216 if ($this->_isGoodTmpDir('\\temp')) {
217 return '\\temp';
218 }
219 Zend_Cache::throwException('Could not determine temp directory, please specify a cache_dir manually');
220 }
221
222 /**
223 * Verify if the given temporary directory is readable and writable
224 *
225 * @param string $dir temporary directory
226 * @return boolean true if the directory is ok
227 */
228 protected function _isGoodTmpDir($dir)
229 {
230 if (is_readable($dir)) {
231 if (is_writable($dir)) {
232 return true;
233 }
234 }
235 return false;
236 }
237
238 /**
239 * Make sure if we enable logging that the Zend_Log class
240 * is available.
241 * Create a default log object if none is set.
242 *
243 * @throws Zend_Cache_Exception
244 * @return void
245 */
246 protected function _loggerSanity()
247 {
248 if (!isset($this->_directives['logging']) || !$this->_directives['logging']) {
249 return;
250 }
251
252 if (isset($this->_directives['logger'])) {
253 if ($this->_directives['logger'] instanceof Zend_Log) {
254 return;
255 }
256 Zend_Cache::throwException('Logger object is not an instance of Zend_Log class.');
257 }
258
259 // Create a default logger to the standard output stream
260 require_once 'Zend/Log.php';
261 require_once 'Zend/Log/Writer/Stream.php';
262 require_once 'Zend/Log/Filter/Priority.php';
263 $logger = new Zend_Log(new Zend_Log_Writer_Stream('php://output'));
264 $logger->addFilter(new Zend_Log_Filter_Priority(Zend_Log::WARN, '<='));
265 $this->_directives['logger'] = $logger;
266 }
267
268 /**
269 * Log a message at the WARN (4) priority.
270 *
271 * @param string $message
272 * @throws Zend_Cache_Exception
273 * @return void
274 */
275 protected function _log($message, $priority = 4)
276 {
277 if (!$this->_directives['logging']) {
278 return;
279 }
280
281 if (!isset($this->_directives['logger'])) {
282 Zend_Cache::throwException('Logging is enabled but logger is not set.');
283 }
284 $logger = $this->_directives['logger'];
285 if (!$logger instanceof Zend_Log) {
286 Zend_Cache::throwException('Logger object is not an instance of Zend_Log class.');
287 }
288 $logger->log($message, $priority);
289 }
290}