1: <?php
  2: 
  3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13: 
 14: 
 15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 16: 
 17:  18:  19:  20:  21:  22: 
 23: class cFileCache {
 24: 
 25:      26:  27:  28:  29: 
 30:     protected $_options = array();
 31: 
 32:      33:  34:  35:  36:  37: 
 38:     public function __construct($options = array()) {
 39:         $this->setOptions($options);
 40:     }
 41: 
 42:      43:  44:  45:  46:  47:  48:  49: 
 50:     public function setOptions($options) {
 51:         
 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:  85:  86:  87:  88:  89:  90:  91:  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: 105: 106: 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: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 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: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 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: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 
197:     public function save($data, $id, $group = '') {
198:         return cFileHandler::write($this->getDestination($id, $group), $data);
199:     }
200: 
201:     202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 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: 225: 226: 227: 228: 229: 230: 
231:     public function generateID($variables) {
232:         return md5(serialize($variables));
233:     }
234: 
235: }
236: