Backend.php 8.2 KB

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