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
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • mpAutoloaderClassMap
  • None
  • PHP
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • 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 && cString::getPartOfString($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:      *
131:      * @return string
132:      *         full filename
133:      * 
134:      * @throws cInvalidArgumentException
135:      */
136:     public function getDestination($id, $group = '') {
137:         $this->_validateDirectory();
138: 
139:         $directory = $this->_options['cacheDir'];
140:         $filename = $this->generateFileName($id, $group);
141: 
142:         return $directory . $filename;
143:     }
144: 
145:     /**
146:      * Return content of a specific cache stored in filesystem.
147:      *
148:      * If not cached, false is returned.
149:      *
150:      * @param string $id
151:      *                      cache ID
152:      * @param string $group [optional]
153:      *                      cache group
154:      *                      
155:      * @return bool|string
156:      *                      content or false
157:      * 
158:      * @throws cInvalidArgumentException
159:      */
160:     public function get($id, $group = '') {
161:         $data = false;
162: 
163:         $destination = $this->getDestination($id, $group);
164: 
165:         if (cFileHandler::exists($destination) === false) {
166:             return false;
167:         }
168: 
169:         $refreshTime = ($this->_options['lifetime'] == 0) ? 0 : time() - (int) $this->_options['lifetime'];
170: 
171:         clearstatcache();
172:         $info = cFileHandler::info($destination);
173:         $lastModifyTime = $info['mtime'];
174: 
175:         if ($lastModifyTime > $refreshTime) {
176:             $data = cFileHandler::read($destination);
177:         }
178: 
179:         return $data;
180:     }
181: 
182:     /**
183:      * Saves the content of a cache in filesystem.
184:      *
185:      * @param string $data
186:      *                      data to save
187:      * @param string $id
188:      *                      cache ID
189:      * @param string $group [optional]
190:      *                      cache group
191:      *
192:      * @return bool
193:      *         success state
194:      * 
195:      * @throws cInvalidArgumentException
196:      */
197:     public function save($data, $id, $group = '') {
198:         return cFileHandler::write($this->getDestination($id, $group), $data);
199:     }
200: 
201:     /**
202:      * Removes cache from filesystem.
203:      *
204:      * @param string $id
205:      *                      cache ID
206:      * @param string $group [optional]
207:      *                      cache group
208:      *                      
209:      * @return bool
210:      *                      success state
211:      * 
212:      * @throws cInvalidArgumentException
213:      */
214:     public function remove($id, $group = '') {
215:         $destination = $this->getDestination($id, $group);
216:         if (cFileHandler::exists($destination) === false) {
217:             return false;
218:         }
219: 
220:         return cFileHandler::remove($this->getDestination($id, $group));
221:     }
222: 
223:     /**
224:      * Generates a ID for the given variables.
225:      *
226:      * @param mixed $variables
227:      *         variables to generate a ID for
228:      * @return string
229:      *         generated ID
230:      */
231:     public function generateID($variables) {
232:         return md5(serialize($variables));
233:     }
234: 
235: }
236: 
CMS CONTENIDO 4.10.0 API documentation generated by ApiGen 2.8.0