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 to create an instance of this class.
 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:      *
 45:      * Validates incoming options and sets the default of the missing options.
 46:      *
 47:      * @param array $options
 48:      *         array with option
 49:      */
 50:     public function setOptions($options) {
 51:         // complete all options
 52:         if (isset($options['cacheDir']) === true && substr($options['cacheDir'], -1) != '/') {
 53:             $options['cacheDir'] = $options['cacheDir'] . '/';
 54:         }
 55: 
 56:         if (isset($options['cacheDir']) === false) {
 57:             $options['cacheDir'] = '/tmp/';
 58:         }
 59: 
 60:         if (isset($options['lifeTime']) !== false && isset($options['lifetime']) === false) {
 61:             $options['lifetime'] = $options['lifeTime'];
 62:         }
 63: 
 64:         if (isset($options['lifetime']) === false) {
 65:             $options['lifetime'] = 3600;
 66:         }
 67: 
 68:         if (isset($options['fileNamePrefix']) === false) {
 69:             $options['fileNamePrefix'] = 'cache_';
 70:         }
 71: 
 72:         if (isset($options['fileExtension']) === false) {
 73:             $options['fileExtension'] = 'tmp';
 74:         }
 75: 
 76:         if (isset($options['fileNameProtection']) === false) {
 77:             $options['fileNameProtection'] = false;
 78:         }
 79: 
 80:         $this->_options = $options;
 81:     }
 82: 
 83:     /**
 84:      * Generates the filename based on set options.
 85:      *
 86:      * @param string $id
 87:      *         cache ID
 88:      * @param string $group [optional]
 89:      *         cache group
 90:      * @return string
 91:      *         filename
 92:      */
 93:     public function generateFileName($id, $group = '') {
 94:         $id = ($this->_options['fileNameProtection'] === true) ? md5($id) : $id;
 95:         if ($group != '') {
 96:             $groupName = ($this->_options['fileNameProtection'] === true ? md5($group) : $group) . '_';
 97:             $group = $groupName . '_';
 98:         }
 99: 
100:         return $this->_options['fileNamePrefix'] . $group . $id . '.' . $this->_options['fileExtension'];
101:     }
102: 
103:     /**
104:      * Validates the caching directory and throws exception on error.
105:      *
106:      * @throws cInvalidArgumentException
107:      */
108:     protected function _validateDirectory() {
109:         $directory = $this->_options['cacheDir'];
110:         if ($directory == '') {
111:             throw new cInvalidArgumentException('The caching directory is empty.');
112:         }
113: 
114:         if (is_dir($directory) === false) {
115:             throw new cInvalidArgumentException('The specified caching directory is not a directory.');
116:         }
117: 
118:         if (cFileHandler::writeable($directory) === false) {
119:             throw new cInvalidArgumentException('The caching directory is not writable.');
120:         }
121:     }
122: 
123:     /**
124:      * Returns full destination to the cached file.
125:      *
126:      * @param string $id
127:      *         cache ID
128:      * @param string $group [optional]
129:      *         cache group
130:      * @return string
131:      *         full filename
132:      */
133:     public function getDestination($id, $group = '') {
134:         $this->_validateDirectory();
135: 
136:         $directory = $this->_options['cacheDir'];
137:         $filename = $this->generateFileName($id, $group);
138: 
139:         return $directory . $filename;
140:     }
141: 
142:     /**
143:      * Return content of a specific cache stored in filesystem.
144:      *
145:      * If not cached, false is returned.
146:      *
147:      * @param string $id
148:      *         cache ID
149:      * @param string $group [optional]
150:      *         cache group
151:      * @return bool|string
152:      *         content or false
153:      */
154:     public function get($id, $group = '') {
155:         $data = false;
156: 
157:         $destination = $this->getDestination($id, $group);
158: 
159:         if (cFileHandler::exists($destination) === false) {
160:             return false;
161:         }
162: 
163:         $refreshTime = ($this->_options['lifetime'] == 0) ? 0 : time() - (int) $this->_options['lifetime'];
164: 
165:         clearstatcache();
166:         $info = cFileHandler::info($destination);
167:         $lastModifyTime = $info['mtime'];
168: 
169:         if ($lastModifyTime > $refreshTime) {
170:             $data = cFileHandler::read($destination);
171:         }
172: 
173:         return $data;
174:     }
175: 
176:     /**
177:      * Saves the content of a cache in filesystem.
178:      *
179:      * @param string $data
180:      *         data to save
181:      * @param string $id
182:      *         cache ID
183:      * @param string $group [optional]
184:      *         cache group
185:      * @return bool
186:      *         success state
187:      */
188:     public function save($data, $id, $group = '') {
189:         return cFileHandler::write($this->getDestination($id, $group), $data);
190:     }
191: 
192:     /**
193:      * Removes cache from filesystem.
194:      *
195:      * @param string $id
196:      *         cache ID
197:      * @param string $group [optional]
198:      *         cache group
199:      * @return bool
200:      *         success state
201:      */
202:     public function remove($id, $group = '') {
203:         $destination = $this->getDestination($id, $group);
204:         if (cFileHandler::exists($destination) === false) {
205:             return false;
206:         }
207: 
208:         return cFileHandler::remove($this->getDestination($id, $group));
209:     }
210: 
211:     /**
212:      * Generates a ID for the given variables.
213:      *
214:      * @param mixed $variables
215:      *         variables to generate a ID for
216:      * @return string
217:      *         generated ID
218:      */
219:     public function generateID($variables) {
220:         return md5(serialize($variables));
221:     }
222: 
223: }
224: 
CMS CONTENIDO 4.9.11 API documentation generated by ApiGen 2.8.0