Changeset 3135

Show
Ignore:
Timestamp:
04/27/08 11:47:59 (2 months ago)
Author:
pmjones
Message:

Solar_Cache_Adapter_File: [CHG] New config param 'hash' allows you to turn hashing of the entry key on and off (default 'true', on).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Solar/Cache/Adapter/File.php

    r2977 r3135  
    6969        'mode'    => 0740, 
    7070        'context' => null, 
     71        'hash'    => true, 
    7172    ); 
    7273     
     
    7980     */ 
    8081    protected $_path; 
     82     
     83    /** 
     84     *  
     85     * Whether or not to hash key names. 
     86     *  
     87     * @var bool 
     88     *  
     89     */ 
     90    protected $_hash; 
    8191     
    8292    /** 
     
    105115        parent::__construct($config); 
    106116         
    107         // keep local values so they can't be changed 
     117        // path to storage 
    108118        $this->_path = Solar_Dir::fix($this->_config['path']); 
     119         
     120        // whether or not to hash 
     121        $this->_hash = $this->_config['hash']; 
    109122         
    110123        // build the context property 
     
    155168        } 
    156169         
     170        // what file should we write to? 
     171        $file = $this->entry($key); 
     172         
     173        // does the directory exist? 
     174        $dir = dirname($file); 
     175        if (! is_dir($dir)) { 
     176            mkdir($dir, $this->_config['mode'], true, $this->_context); 
     177        } 
     178         
    157179        // open the file for over-writing. not using file_put_contents  
    158180        // becuase we may need to write a serial file too (and avoid race 
    159181        // conditions while doing so). don't use include path. 
    160         $file = $this->entry($key); 
    161         $fp = @fopen($file, 'wb', false, $this->_context); 
     182        $fp = fopen($file, 'wb', false, $this->_context); 
    162183         
    163184        // was it opened? 
    164         if ($fp) { 
    165              
    166             // yes.  exclusive lock for writing. 
    167             flock($fp, LOCK_EX); 
    168              
    169             // don't need the 3rd param (byte length) because Solar has 
    170             // already turned off magic_quotes_runtime. 
    171             // <http://php.net/fwrite> 
    172             fwrite($fp, $data); 
    173              
    174             // add a .serial file? (do this while the file is locked to avoid 
    175             // race conditions) 
    176             if ($serial) { 
    177                 // use this instead of touch() because it supports stream 
    178                 // contexts. 
    179                 file_put_contents($file . '.serial', null, LOCK_EX, $this->_context); 
    180             } else { 
    181                 // make sure no serial file is there from any previous entries 
    182                 // with the same name 
    183                 @unlink($file . '.serial', $this->_context); 
    184             } 
    185              
    186             // unlock and close, then done. 
    187             flock($fp, LOCK_UN); 
    188             fclose($fp); 
    189             return true; 
    190         } 
    191          
    192         // could not open the file for writing. 
    193         return false; 
     185        if (! $fp) { 
     186            // could not open the file for writing. 
     187            return false; 
     188        } 
     189         
     190        // set exclusive lock for writing. 
     191        flock($fp, LOCK_EX); 
     192         
     193        // don't need the 3rd param (byte length) because Solar has 
     194        // already turned off magic_quotes_runtime. 
     195        // <http://php.net/fwrite> 
     196        fwrite($fp, $data); 
     197         
     198        // add a .serial file? (do this while the file is locked to avoid 
     199        // race conditions) 
     200        if ($serial) { 
     201            // use this instead of touch() because it supports stream 
     202            // contexts. 
     203            file_put_contents($file . '.serial', null, LOCK_EX, $this->_context); 
     204        } else { 
     205            // make sure no serial file is there from any previous entries 
     206            // with the same name 
     207            @unlink($file . '.serial', $this->_context); 
     208        } 
     209         
     210        // unlock and close, then done. 
     211        flock($fp, LOCK_UN); 
     212        fclose($fp); 
     213        return true; 
     214         
    194215    } 
    195216     
     
    372393    public function entry($key) 
    373394    { 
    374         return $this->_path . hash('md5', $key); 
     395        if ($this->_config['hash']) { 
     396            return $this->_path . hash('md5', $key); 
     397        } else { 
     398            // try to avoid file traversal exploits 
     399            $key = str_replace('..', '_', $key); 
     400            // colons mess up Mac OS X 
     401            $key = str_replace(':', '_', $key); 
     402            // done 
     403            return $this->_path . $key; 
     404        } 
    375405    } 
    376406}  
    377