web/lib/Zend/Cache/Backend/Static.php
changeset 807 877f952ae2bd
parent 207 621fa6caec0c
child 1230 68c69c656a2c
equal deleted inserted replaced
805:5e7a0fedabdf 807:877f952ae2bd
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Cache
    16  * @package    Zend_Cache
    17  * @subpackage Zend_Cache_Backend
    17  * @subpackage Zend_Cache_Backend
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    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
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    20  * @version    $Id: Static.php 22950 2010-09-16 19:33:00Z mabe $
    20  * @version    $Id: Static.php 24989 2012-06-21 07:24:13Z mabe $
    21  */
    21  */
    22 
    22 
    23 /**
    23 /**
    24  * @see Zend_Cache_Backend_Interface
    24  * @see Zend_Cache_Backend_Interface
    25  */
    25  */
    31 require_once 'Zend/Cache/Backend.php';
    31 require_once 'Zend/Cache/Backend.php';
    32 
    32 
    33 /**
    33 /**
    34  * @package    Zend_Cache
    34  * @package    Zend_Cache
    35  * @subpackage Zend_Cache_Backend
    35  * @subpackage Zend_Cache_Backend
    36  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    36  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    37  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    37  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    38  */
    38  */
    39 class Zend_Cache_Backend_Static
    39 class Zend_Cache_Backend_Static
    40     extends Zend_Cache_Backend
    40     extends Zend_Cache_Backend
    41     implements Zend_Cache_Backend_Interface
    41     implements Zend_Cache_Backend_Interface
    97      * @param  string $name
    97      * @param  string $name
    98      * @return mixed
    98      * @return mixed
    99      */
    99      */
   100     public function getOption($name)
   100     public function getOption($name)
   101     {
   101     {
       
   102         $name = strtolower($name);
       
   103 
   102         if ($name == 'tag_cache') {
   104         if ($name == 'tag_cache') {
   103             return $this->getInnerCache();
   105             return $this->getInnerCache();
   104         } else {
   106         }
   105             if (in_array($name, $this->_options)) {
   107 
   106                 return $this->_options[$name];
   108         return parent::getOption($name);
   107             }
       
   108             if ($name == 'lifetime') {
       
   109                 return parent::getLifetime();
       
   110             }
       
   111             return null;
       
   112         }
       
   113     }
   109     }
   114 
   110 
   115     /**
   111     /**
   116      * Test if a cache is available for the given id and (if yes) return it (false else)
   112      * Test if a cache is available for the given id and (if yes) return it (false else)
   117      *
   113      *
   121      * @param  boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
   117      * @param  boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
   122      * @return string|false cached datas
   118      * @return string|false cached datas
   123      */
   119      */
   124     public function load($id, $doNotTestCacheValidity = false)
   120     public function load($id, $doNotTestCacheValidity = false)
   125     {
   121     {
   126         if (empty($id)) {
   122         if (($id = (string)$id) === '') {
   127             $id = $this->_detectId();
   123             $id = $this->_detectId();
   128         } else {
   124         } else {
   129             $id = $this->_decodeId($id);
   125             $id = $this->_decodeId($id);
   130         }
   126         }
   131         if (!$this->_verifyPath($id)) {
   127         if (!$this->_verifyPath($id)) {
   134         if ($doNotTestCacheValidity) {
   130         if ($doNotTestCacheValidity) {
   135             $this->_log("Zend_Cache_Backend_Static::load() : \$doNotTestCacheValidity=true is unsupported by the Static backend");
   131             $this->_log("Zend_Cache_Backend_Static::load() : \$doNotTestCacheValidity=true is unsupported by the Static backend");
   136         }
   132         }
   137 
   133 
   138         $fileName = basename($id);
   134         $fileName = basename($id);
   139         if (empty($fileName)) {
   135         if ($fileName === '') {
   140             $fileName = $this->_options['index_filename'];
   136             $fileName = $this->_options['index_filename'];
   141         }
   137         }
   142         $pathName = $this->_options['public_dir'] . dirname($id);
   138         $pathName = $this->_options['public_dir'] . dirname($id);
   143         $file     = rtrim($pathName, '/') . '/' . $fileName . $this->_options['file_extension'];
   139         $file     = rtrim($pathName, '/') . '/' . $fileName . $this->_options['file_extension'];
   144         if (file_exists($file)) {
   140         if (file_exists($file)) {
   161         if (!$this->_verifyPath($id)) {
   157         if (!$this->_verifyPath($id)) {
   162             Zend_Cache::throwException('Invalid cache id: does not match expected public_dir path');
   158             Zend_Cache::throwException('Invalid cache id: does not match expected public_dir path');
   163         }
   159         }
   164 
   160 
   165         $fileName = basename($id);
   161         $fileName = basename($id);
   166         if (empty($fileName)) {
   162         if ($fileName === '') {
   167             $fileName = $this->_options['index_filename'];
   163             $fileName = $this->_options['index_filename'];
   168         }
   164         }
   169         if ($this->_tagged === null && $tagged = $this->getInnerCache()->load(self::INNER_CACHE_NAME)) {
   165         if ($this->_tagged === null && $tagged = $this->getInnerCache()->load(self::INNER_CACHE_NAME)) {
   170             $this->_tagged = $tagged;
   166             $this->_tagged = $tagged;
   171         } elseif (!$this->_tagged) {
   167         } elseif (!$this->_tagged) {
   209             $extension = '.' . ltrim($data[1], '.');
   205             $extension = '.' . ltrim($data[1], '.');
   210             $data = $data[0];
   206             $data = $data[0];
   211         }
   207         }
   212 
   208 
   213         clearstatcache();
   209         clearstatcache();
   214         if ($id === null || strlen($id) == 0) {
   210         if (($id = (string)$id) === '') {
   215             $id = $this->_detectId();
   211             $id = $this->_detectId();
   216         } else {
   212         } else {
   217             $id = $this->_decodeId($id);
   213             $id = $this->_decodeId($id);
   218         }
   214         }
   219 
   215 
   220         $fileName = basename($id);
   216         $fileName = basename($id);
   221         if (empty($fileName)) {
   217         if ($fileName === '') {
   222             $fileName = $this->_options['index_filename'];
   218             $fileName = $this->_options['index_filename'];
   223         }
   219         }
   224 
   220 
   225         $pathName = realpath($this->_options['public_dir']) . dirname($id);
   221         $pathName = realpath($this->_options['public_dir']) . dirname($id);
   226         $this->_createDirectoriesFor($pathName);
   222         $this->_createDirectoriesFor($pathName);
   306         if (isset($this->_tagged[$id])) {
   302         if (isset($this->_tagged[$id])) {
   307             $extension = $this->_tagged[$id]['extension'];
   303             $extension = $this->_tagged[$id]['extension'];
   308         } else {
   304         } else {
   309             $extension = $this->_options['file_extension'];
   305             $extension = $this->_options['file_extension'];
   310         }
   306         }
   311         if (empty($fileName)) {
   307         if ($fileName === '') {
   312             $fileName = $this->_options['index_filename'];
   308             $fileName = $this->_options['index_filename'];
   313         }
   309         }
   314         $pathName = $this->_options['public_dir'] . dirname($id);
   310         $pathName = $this->_options['public_dir'] . dirname($id);
   315         $file     = realpath($pathName) . '/' . $fileName . $extension;
   311         $file     = realpath($pathName) . '/' . $fileName . $extension;
   316         if (!file_exists($file)) {
   312         if (!file_exists($file)) {
   331     {
   327     {
   332         if (!$this->_verifyPath($id)) {
   328         if (!$this->_verifyPath($id)) {
   333             Zend_Cache::throwException('Invalid cache id: does not match expected public_dir path');
   329             Zend_Cache::throwException('Invalid cache id: does not match expected public_dir path');
   334         }
   330         }
   335         $fileName = basename($id);
   331         $fileName = basename($id);
   336         if (empty($fileName)) {
   332         if ($fileName === '') {
   337             $fileName = $this->_options['index_filename'];
   333             $fileName = $this->_options['index_filename'];
   338         }
   334         }
   339         $pathName  = $this->_options['public_dir'] . dirname($id);
   335         $pathName  = $this->_options['public_dir'] . dirname($id);
   340         $file      = $pathName . '/' . $fileName . $this->_options['file_extension'];
   336         $file      = $pathName . '/' . $fileName . $this->_options['file_extension'];
   341         $directory = $pathName . '/' . $fileName;
   337         $directory = $pathName . '/' . $fileName;
   342         if (file_exists($directory)) {
   338         if (file_exists($directory)) {
   343             if (!is_writable($directory)) {
   339             if (!is_writable($directory)) {
   344                 return false;
   340                 return false;
   345             }
   341             }
   346             foreach (new DirectoryIterator($directory) as $file) {
   342             if (is_dir($directory)) {
   347                 if (true === $file->isFile()) {
   343                 foreach (new DirectoryIterator($directory) as $file) {
   348                     if (false === unlink($file->getPathName())) {
   344                     if (true === $file->isFile()) {
   349                         return false;
   345                         if (false === unlink($file->getPathName())) {
       
   346                             return false;
       
   347                         }
   350                     }
   348                     }
   351                 }
   349                 }
   352             }
   350             }
   353             rmdir(dirname($path));
   351             rmdir($directory);
   354         }
   352         }
   355         if (file_exists($file)) {
   353         if (file_exists($file)) {
   356             if (!is_writable($file)) {
   354             if (!is_writable($file)) {
   357                 return false;
   355                 return false;
   358             }
   356             }
   536 
   534 
   537     /**
   535     /**
   538      * Detect an octal string and return its octal value for file permission ops
   536      * Detect an octal string and return its octal value for file permission ops
   539      * otherwise return the non-string (assumed octal or decimal int already)
   537      * otherwise return the non-string (assumed octal or decimal int already)
   540      *
   538      *
   541      * @param $val The potential octal in need of conversion
   539      * @param string $val The potential octal in need of conversion
   542      * @return int
   540      * @return int
   543      */
   541      */
   544     protected function _octdec($val)
   542     protected function _octdec($val)
   545     {
   543     {
   546         if (is_string($val) && decoct(octdec($val)) == $val) {
   544         if (is_string($val) && decoct(octdec($val)) == $val) {
   549         return $val;
   547         return $val;
   550     }
   548     }
   551 
   549 
   552     /**
   550     /**
   553      * Decode a request URI from the provided ID
   551      * Decode a request URI from the provided ID
       
   552      *
       
   553      * @param string $id
       
   554      * @return string
   554      */
   555      */
   555     protected function _decodeId($id)
   556     protected function _decodeId($id)
   556     {
   557     {
   557         return pack('H*', $id);;
   558         return pack('H*', $id);
   558     }
   559     }
   559 }
   560 }