1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
19:
20: App::uses('AppHelper', 'View/Helper');
21:
22: 23: 24: 25: 26: 27: 28: 29: 30:
31: class CacheHelper extends AppHelper {
32:
33: 34: 35: 36: 37: 38:
39: protected $_replace = array();
40:
41: 42: 43: 44: 45: 46:
47: protected $_match = array();
48:
49: 50: 51: 52: 53:
54: protected $_counter = 0;
55:
56: 57: 58: 59: 60:
61: protected function _enabled() {
62: return (($this->_View->cacheAction != false)) && (Configure::read('Cache.check') === true);
63: }
64:
65: 66: 67: 68: 69: 70:
71: public function afterRenderFile($viewFile, $output) {
72: if ($this->_enabled()) {
73: return $this->_parseContent($viewFile, $output);
74: }
75: }
76:
77: 78: 79: 80: 81: 82:
83: public function afterLayout($layoutFile) {
84: if ($this->_enabled()) {
85: $this->_View->output = $this->cache($layoutFile, $this->_View->output);
86: }
87: $this->_View->output = preg_replace('/<!--\/?nocache-->/', '', $this->_View->output);
88: }
89:
90: 91: 92: 93: 94: 95: 96: 97: 98:
99: protected function _parseContent($file, $out) {
100: $out = preg_replace_callback('/<!--nocache-->/', array($this, '_replaceSection'), $out);
101: $this->_parseFile($file, $out);
102: return $out;
103: }
104:
105: 106: 107: 108: 109: 110: 111: 112:
113: public function cache($file, $out) {
114: $cacheTime = 0;
115: $useCallbacks = false;
116: $cacheAction = $this->_View->cacheAction;
117:
118: if (is_array($cacheAction)) {
119: $keys = array_keys($cacheAction);
120: $index = null;
121:
122: foreach ($keys as $action) {
123: if ($action == $this->request->params['action']) {
124: $index = $action;
125: break;
126: }
127: }
128:
129: if (!isset($index) && $this->request->params['action'] == 'index') {
130: $index = 'index';
131: }
132:
133: $options = $cacheAction;
134: if (isset($cacheAction[$index])) {
135: if (is_array($cacheAction[$index])) {
136: $options = array_merge(array('duration' => 0, 'callbacks' => false), $cacheAction[$index]);
137: } else {
138: $cacheTime = $cacheAction[$index];
139: }
140: }
141: if (isset($options['duration'])) {
142: $cacheTime = $options['duration'];
143: }
144: if (isset($options['callbacks'])) {
145: $useCallbacks = $options['callbacks'];
146: }
147: } else {
148: $cacheTime = $cacheAction;
149: }
150:
151: if ($cacheTime != '' && $cacheTime > 0) {
152: $cached = $this->_parseOutput($out);
153: try {
154: $this->_writeFile($cached, $cacheTime, $useCallbacks);
155: } catch (Exception $e) {
156: $message = __d(
157: 'cake_dev',
158: 'Unable to write view cache file: "%s" for "%s"',
159: $e->getMessage(),
160: $this->request->here
161: );
162: $this->log($message, 'error');
163: }
164: $out = $this->_stripTags($out);
165: }
166: return $out;
167: }
168:
169: 170: 171: 172: 173: 174: 175:
176: protected function _parseFile($file, $cache) {
177: if (is_file($file)) {
178: $file = file_get_contents($file);
179: } elseif ($file = fileExistsInPath($file)) {
180: $file = file_get_contents($file);
181: }
182: preg_match_all('/(<!--nocache:\d{3}-->(?<=<!--nocache:\d{3}-->)[\\s\\S]*?(?=<!--\/nocache-->)<!--\/nocache-->)/i', $cache, $outputResult, PREG_PATTERN_ORDER);
183: preg_match_all('/(?<=<!--nocache-->)([\\s\\S]*?)(?=<!--\/nocache-->)/i', $file, $fileResult, PREG_PATTERN_ORDER);
184: $fileResult = $fileResult[0];
185: $outputResult = $outputResult[0];
186:
187: if (!empty($this->_replace)) {
188: foreach ($outputResult as $i => $element) {
189: $index = array_search($element, $this->_match);
190: if ($index !== false) {
191: unset($outputResult[$i]);
192: }
193: }
194: $outputResult = array_values($outputResult);
195: }
196:
197: if (!empty($fileResult)) {
198: $i = 0;
199: foreach ($fileResult as $cacheBlock) {
200: if (isset($outputResult[$i])) {
201: $this->_replace[] = $cacheBlock;
202: $this->_match[] = $outputResult[$i];
203: }
204: $i++;
205: }
206: }
207: }
208:
209: 210: 211: 212: 213: 214:
215: protected function _replaceSection() {
216: $this->_counter += 1;
217: return sprintf('<!--nocache:%03d-->', $this->_counter);
218: }
219:
220: 221: 222: 223: 224: 225: 226: 227:
228: protected function _stripTags($content) {
229: return preg_replace('#<!--/?nocache(\:\d{3})?-->#', '', $content);
230: }
231:
232: 233: 234: 235: 236: 237:
238: protected function _parseOutput($cache) {
239: $count = 0;
240: if (!empty($this->_match)) {
241: foreach ($this->_match as $found) {
242: $original = $cache;
243: $length = strlen($found);
244: $position = 0;
245:
246: for ($i = 1; $i <= 1; $i++) {
247: $position = strpos($cache, $found, $position);
248:
249: if ($position !== false) {
250: $cache = substr($original, 0, $position);
251: $cache .= $this->_replace[$count];
252: $cache .= substr($original, $position + $length);
253: } else {
254: break;
255: }
256: }
257: $count++;
258: }
259: return $cache;
260: }
261: return $cache;
262: }
263:
264: 265: 266: 267: 268: 269: 270: 271:
272: protected function _writeFile($content, $timestamp, $useCallbacks = false) {
273: $now = time();
274:
275: if (is_numeric($timestamp)) {
276: $cacheTime = $now + $timestamp;
277: } else {
278: $cacheTime = strtotime($timestamp, $now);
279: }
280: $path = $this->request->here();
281: if ($path == '/') {
282: $path = 'home';
283: }
284: $cache = strtolower(Inflector::slug($path));
285:
286: if (empty($cache)) {
287: return;
288: }
289: $cache = $cache . '.php';
290: $file = '<!--cachetime:' . $cacheTime . '--><?php';
291:
292: if (empty($this->_View->plugin)) {
293: $file .= "
294: App::uses('{$this->_View->name}Controller', 'Controller');
295: ";
296: } else {
297: $file .= "
298: App::uses('{$this->_View->plugin}AppController', '{$this->_View->plugin}.Controller');
299: App::uses('{$this->_View->name}Controller', '{$this->_View->plugin}.Controller');
300: ";
301: }
302:
303: $file .= '
304: $request = unserialize(base64_decode(\'' . base64_encode(serialize($this->request)) . '\'));
305: $response = new CakeResponse(array("charset" => Configure::read("App.encoding")));
306: $controller = new ' . $this->_View->name . 'Controller($request, $response);
307: $controller->plugin = $this->plugin = \'' . $this->_View->plugin . '\';
308: $controller->helpers = $this->helpers = unserialize(base64_decode(\'' . base64_encode(serialize($this->_View->helpers)) . '\'));
309: $controller->layout = $this->layout = \'' . $this->_View->layout . '\';
310: $controller->theme = $this->theme = \'' . $this->_View->theme . '\';
311: $controller->viewVars = unserialize(base64_decode(\'' . base64_encode(serialize($this->_View->viewVars)) . '\'));
312: Router::setRequestInfo($controller->request);
313: $this->request = $request;';
314:
315: if ($useCallbacks == true) {
316: $file .= '
317: $controller->constructClasses();
318: $controller->startupProcess();';
319: }
320:
321: $file .= '
322: $this->viewVars = $controller->viewVars;
323: $this->loadHelpers();
324: extract($this->viewVars, EXTR_SKIP);
325: ?>';
326: $content = preg_replace("/(<\\?xml)/", "<?php echo '$1'; ?>", $content);
327: $file .= $content;
328: return cache('views' . DS . $cache, $file, $timestamp);
329: }
330:
331: }
332: