Backend.php 7.6 KB

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