Overview

Packages

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