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