1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: 19: 20: 21: 22: 23:
24: class cFileCache {
25: 26: 27: 28:
29: protected $_options = array();
30:
31: 32: 33: 34: 35:
36: public function __construct($options = array()) {
37: $this->setOptions($options);
38: }
39:
40: 41: 42: 43: 44:
45: public function setOptions($options) {
46:
47: if (isset($options['cacheDir']) === true && substr($options['cacheDir'], -1) != '/') {
48: $options['cacheDir'] = $options['cacheDir'] . '/';
49: }
50:
51: if (isset($options['cacheDir']) === false) {
52: $options['cacheDir'] = '/tmp/';
53: }
54:
55: if (isset($options['lifeTime']) !== false && isset($options['lifetime']) === false) {
56: $options['lifetime'] = $options['lifeTime'];
57: }
58:
59: if (isset($options['lifetime']) === false) {
60: $options['lifetime'] = 3600;
61: }
62:
63: if (isset($options['fileNamePrefix']) === false) {
64: $options['fileNamePrefix'] = 'cache_';
65: }
66:
67: if (isset($options['fileExtension']) === false) {
68: $options['fileExtension'] = 'tmp';
69: }
70:
71: if (isset($options['fileNameProtection']) === false) {
72: $options['fileNameProtection'] = false;
73: }
74:
75: $this->_options = $options;
76: }
77:
78: 79: 80: 81: 82: 83: 84: 85:
86: public function generateFileName($id, $group = '') {
87: $id = ($this->_options['fileNameProtection'] === true) ? md5($id) : $id;
88: if ($group != '') {
89: $groupName = ($this->_options['fileNameProtection'] === true ? md5($group) : $group) . '_';
90: $group = $groupName . '_';
91: }
92:
93: return $this->_options['fileNamePrefix'] . $group . $id . '.' . $this->_options['fileExtension'];
94: }
95:
96: 97: 98: 99:
100: protected function _validateDirectory() {
101: $directory = $this->_options['cacheDir'];
102: if ($directory == '') {
103: throw new cInvalidArgumentException('The caching directory is empty.');
104: }
105:
106: if (is_dir($directory) === false) {
107: throw new cInvalidArgumentException('The specified caching directory is not a directory.');
108: }
109:
110: if (cFileHandler::writeable($directory) === false) {
111: throw new cInvalidArgumentException('The caching directory is not writable.');
112: }
113: }
114:
115: 116: 117: 118: 119: 120: 121: 122:
123: public function getDestination($id, $group = '') {
124: $this->_validateDirectory();
125:
126: $directory = $this->_options['cacheDir'];
127: $filename = $this->generateFileName($id, $group);
128:
129: return $directory . $filename;
130: }
131:
132: 133: 134: 135: 136: 137: 138: 139:
140: public function get($id, $group = '') {
141: $data = false;
142:
143: $destination = $this->getDestination($id, $group);
144:
145: if (cFileHandler::exists($destination) === false) {
146: return false;
147: }
148:
149: $refreshTime = ($this->_options['lifetime'] == 0) ? 0 : time() - (int)$this->_options['lifetime'];
150:
151: clearstatcache();
152: $info = cFileHandler::info($destination);
153: $lastModifyTime = $info['mtime'];
154:
155: if ($lastModifyTime > $refreshTime) {
156: $data = cFileHandler::read($destination);
157: }
158:
159: return $data;
160: }
161:
162: 163: 164: 165: 166: 167: 168: 169: 170:
171: public function save($data, $id, $group = '') {
172: return cFileHandler::write($this->getDestination($id, $group), $data);
173: }
174:
175: 176: 177: 178: 179: 180: 181: 182:
183: public function remove($id, $group = '') {
184: $destination = $this->getDestination($id, $group);
185: if (cFileHandler::exists($destination) === false) {
186: return false;
187: }
188:
189: return cFileHandler::remove($this->getDestination($id, $group));
190: }
191:
192: 193: 194: 195: 196: 197: 198:
199: public function generateID($variables) {
200: return md5(serialize($variables));
201: }
202: }