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: public function setOptions($options) {
50:
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: 84: 85: 86: 87: 88: 89: 90: 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: 104: 105: 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: 124: 125: 126: 127: 128: 129: 130: 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: 143: 144: 145: 146: 147: 148: 149: 150: 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: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185:
186: public function save($data, $id, $group = '') {
187: return cFileHandler::write($this->getDestination($id, $group), $data);
188: }
189:
190: 191: 192: 193: 194: 195: 196: 197: 198: 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: 211: 212: 213: 214: 215: 216:
217: public function generateID($variables) {
218: return md5(serialize($variables));
219: }
220:
221: }
222: