Overview

Packages

  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Datatype
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
  • mpAutoloaderClassMap
  • None
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SearchSolr
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob

Classes

  • cFileCache
  • cOutputCache
  • cOutputCacheHandler
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: /**
  3:  * This file contains the file cache class.
  4:  *
  5:  * @package    Core
  6:  * @subpackage Cache
  7:  * @version    SVN Revision $Rev:$
  8:  *
  9:  * @author     Dominik Ziegler
 10:  * @copyright  four for business AG <www.4fb.de>
 11:  * @license    http://www.contenido.org/license/LIZENZ.txt
 12:  * @link       http://www.4fb.de
 13:  * @link       http://www.contenido.org
 14:  */
 15: 
 16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 17: 
 18: /**
 19:  * This class contains functions for the file cache in CONTENIDO.
 20:  *
 21:  * @package    Core
 22:  * @subpackage Cache
 23:  */
 24: class cFileCache {
 25:     /**
 26:      * Options for the cache.
 27:      * @var array
 28:      */
 29:     protected $_options = array();
 30: 
 31:     /**
 32:      * Constructor.
 33:      *
 34:      * @param array $options array with options for the cache (optional, default: empty array)
 35:      */
 36:     public function __construct($options = array()) {
 37:         $this->setOptions($options);
 38:     }
 39: 
 40:     /**
 41:      * Setter for the cache options. Validates incoming options and sets the default of the missing options.
 42:      *
 43:      * @param array $options array with option
 44:      */
 45:     public function setOptions($options) {
 46:         // complete all options
 47:         if (isset($options['cacheDir']) === true && substr($options['cacheDir'], -1) != '/') {
 48:             $options['cacheDir'] = $options['cacheDir'] . '/';
 49:         }
 50: 
 51:         if (isset($options['cacheDir']) === false) {
 52:             $options['cacheDir'] = '/tmp/';
 53:         }
 54: 
 55:         if (isset($options['lifeTime']) !== false && isset($options['lifetime']) === false) {
 56:             $options['lifetime'] = $options['lifeTime'];
 57:         }
 58: 
 59:         if (isset($options['lifetime']) === false) {
 60:             $options['lifetime'] = 3600;
 61:         }
 62: 
 63:         if (isset($options['fileNamePrefix']) === false) {
 64:             $options['fileNamePrefix'] = 'cache_';
 65:         }
 66: 
 67:         if (isset($options['fileExtension']) === false) {
 68:             $options['fileExtension'] = 'tmp';
 69:         }
 70: 
 71:         if (isset($options['fileNameProtection']) === false) {
 72:             $options['fileNameProtection'] = false;
 73:         }
 74: 
 75:         $this->_options = $options;
 76:     }
 77: 
 78:     /**
 79:      * Generates the filename based on set options.
 80:      *
 81:      * @param string $id    cache ID
 82:      * @param string $group cache group
 83:      *
 84:      * @return string filename
 85:      */
 86:     public function generateFileName($id, $group = '') {
 87:         $id = ($this->_options['fileNameProtection'] === true) ? md5($id) : $id;
 88:         if ($group != '') {
 89:             $groupName = ($this->_options['fileNameProtection'] === true ? md5($group) : $group) . '_';
 90:             $group = $groupName . '_';
 91:         }
 92: 
 93:         return $this->_options['fileNamePrefix'] . $group . $id . '.' . $this->_options['fileExtension'];
 94:     }
 95: 
 96:     /**
 97:      * Validates the caching directory and throws exception on error.
 98:      * @throws cInvalidArgumentException
 99:      */
100:     protected function _validateDirectory() {
101:         $directory = $this->_options['cacheDir'];
102:         if ($directory == '') {
103:             throw new cInvalidArgumentException('The caching directory is empty.');
104:         }
105: 
106:         if (is_dir($directory) === false) {
107:             throw new cInvalidArgumentException('The specified caching directory is not a directory.');
108:         }
109: 
110:         if (cFileHandler::writeable($directory) === false) {
111:             throw new cInvalidArgumentException('The caching directory is not writable.');
112:         }
113:     }
114: 
115:     /**
116:      * Returns full destination to the cache file.
117:      *
118:      * @param string $id    cache ID
119:      * @param string $group cache group
120:      *
121:      * @return string full filename
122:      */
123:     public function getDestination($id, $group = '') {
124:         $this->_validateDirectory();
125: 
126:         $directory = $this->_options['cacheDir'];
127:         $filename = $this->generateFileName($id, $group);
128: 
129:         return $directory . $filename;
130:     }
131: 
132:     /**
133:      * Return content of a specific cache stored in filesystem. If not cached, false is returned.
134:      *
135:      * @param string $id    cache ID
136:      * @param string $group cache group
137:      *
138:      * @return bool|string content or false
139:      */
140:     public function get($id, $group = '') {
141:         $data = false;
142: 
143:         $destination = $this->getDestination($id, $group);
144: 
145:         if (cFileHandler::exists($destination) === false) {
146:             return false;
147:         }
148: 
149:         $refreshTime = ($this->_options['lifetime'] == 0) ? 0 : time() - (int)$this->_options['lifetime'];
150: 
151:         clearstatcache();
152:         $info = cFileHandler::info($destination);
153:         $lastModifyTime = $info['mtime'];
154: 
155:         if ($lastModifyTime > $refreshTime) {
156:             $data = cFileHandler::read($destination);
157:         }
158: 
159:         return $data;
160:     }
161: 
162:     /**
163:      * Saves the content of a cache in filesystem.
164:      *
165:      * @param string $data  data to save
166:      * @param string $id    cache ID
167:      * @param string $group cache group
168:      *
169:      * @return bool success state
170:      */
171:     public function save($data, $id, $group = '') {
172:         return cFileHandler::write($this->getDestination($id, $group), $data);
173:     }
174: 
175:     /**
176:      * Removes cache from filesystem.
177:      *
178:      * @param string $id    cache ID
179:      * @param string $group cache group
180:      *
181:      * @return bool success state
182:      */
183:     public function remove($id, $group = '') {
184:         $destination = $this->getDestination($id, $group);
185:         if (cFileHandler::exists($destination) === false) {
186:             return false;
187:         }
188: 
189:         return cFileHandler::remove($this->getDestination($id, $group));
190:     }
191: 
192:     /**
193:      * Generates a ID for the given variables.
194:      *
195:      * @param mixed $variables variables to generate a ID for
196:      *
197:      * @return string generated ID
198:      */
199:     public function generateID($variables) {
200:         return md5(serialize($variables));
201:     }
202: }
CMS CONTENIDO 4.9.3 API documentation generated by ApiGen 2.8.0