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