00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 class CacheHelper extends AppHelper {
00038
00039
00040
00041
00042
00043
00044
00045 var $__replace = array();
00046
00047
00048
00049
00050
00051
00052
00053 var $__match = array();
00054
00055
00056
00057
00058
00059
00060 var $view;
00061
00062
00063
00064
00065
00066
00067 var $cacheAction;
00068
00069
00070
00071
00072
00073
00074
00075
00076 function cache($file, $out, $cache = false) {
00077 $cacheTime = 0;
00078 $useCallbacks = false;
00079 if (is_array($this->cacheAction)) {
00080 $check = str_replace('/', '_', $this->here);
00081 $replace = str_replace('/', '_', $this->base);
00082 $match = str_replace($this->base, '', $this->here);
00083 $match = str_replace('
00084 $match = str_replace('/' . $this->controllerName . '/', '', $match);
00085 $check = str_replace($replace, '', $check);
00086 $check = str_replace('_' . $this->controllerName . '_', '', $check);
00087 $check = Inflector::slug($check);
00088 $check = preg_replace('/^_+/', '', $check);
00089 $keys = str_replace('/', '_', array_keys($this->cacheAction));
00090 $found = array_keys($this->cacheAction);
00091 $index = null;
00092 $count = 0;
00093
00094 foreach ($keys as $key => $value) {
00095 if (strpos($check, $value) === 0) {
00096 $index = $found[$count];
00097 break;
00098 }
00099 $count++;
00100 }
00101
00102 if (isset($index)) {
00103 $pos1 = strrpos($match, '/');
00104 $char = strlen($match) - 1;
00105
00106 if ($pos1 == $char) {
00107 $match = substr($match, 0, $char);
00108 }
00109
00110 $key = $match;
00111 } elseif ($this->action == 'index') {
00112 $index = 'index';
00113 }
00114
00115 $options = $this->cacheAction;
00116 if (isset($this->cacheAction[$index])) {
00117 if (is_array($this->cacheAction[$index])) {
00118 $options = array_merge(array('duration'=> 0, 'callbacks' => false), $this->cacheAction[$index]);
00119 } else {
00120 $cacheTime = $this->cacheAction[$index];
00121 }
00122 }
00123
00124 if (array_key_exists('duration', $options)) {
00125 $cacheTime = $options['duration'];
00126 }
00127 if (array_key_exists('callbacks', $options)) {
00128 $useCallbacks = $options['callbacks'];
00129 }
00130
00131 } else {
00132 $cacheTime = $this->cacheAction;
00133 }
00134
00135 if ($cacheTime != '' && $cacheTime > 0) {
00136 $this->__parseFile($file, $out);
00137 if ($cache === true) {
00138 $cached = $this->__parseOutput($out);
00139 $this->__writeFile($cached, $cacheTime, $useCallbacks);
00140 }
00141 return $out;
00142 } else {
00143 return $out;
00144 }
00145 }
00146
00147
00148
00149
00150
00151
00152
00153 function __parseFile($file, $cache) {
00154 if (is_file($file)) {
00155 $file = file_get_contents($file);
00156 } elseif ($file = fileExistsInPath($file)) {
00157 $file = file_get_contents($file);
00158 }
00159
00160 preg_match_all('/(<cake:nocache>(?<=<cake:nocache>)[\\s\\S]*?(?=<\/cake:nocache>)<\/cake:nocache>)/i', $cache, $oresult, PREG_PATTERN_ORDER);
00161 preg_match_all('/(?<=<cake:nocache>)([\\s\\S]*?)(?=<\/cake:nocache>)/i', $file, $result, PREG_PATTERN_ORDER);
00162
00163 if (!empty($result['0'])) {
00164 $count = 0;
00165
00166 foreach ($result['0'] as $result) {
00167 if (isset($oresult['0'][$count])) {
00168 $this->__replace[] = $result;
00169 $this->__match[] = $oresult['0'][$count];
00170 }
00171 $count++;
00172 }
00173 }
00174 }
00175
00176
00177
00178
00179
00180
00181
00182 function __parseOutput($cache) {
00183 $count = 0;
00184 if (!empty($this->__match)) {
00185
00186 foreach ($this->__match as $found) {
00187 $original = $cache;
00188 $length = strlen($found);
00189 $position = 0;
00190
00191 for ($i = 1; $i <= 1; $i++) {
00192 $position = strpos($cache, $found, $position);
00193
00194 if ($position !== false) {
00195 $cache = substr($original, 0, $position);
00196 $cache .= $this->__replace[$count];
00197 $cache .= substr($original, $position + $length);
00198 } else {
00199 break;
00200 }
00201 }
00202 $count++;
00203 }
00204 return $cache;
00205 }
00206 return $cache;
00207 }
00208
00209
00210
00211
00212
00213
00214
00215
00216 function __writeFile($content, $timestamp, $useCallbacks = false) {
00217 $now = time();
00218
00219 if (is_numeric($timestamp)) {
00220 $cacheTime = $now + $timestamp;
00221 } else {
00222 $cacheTime = strtotime($timestamp, $now);
00223 }
00224 $path = $this->here;
00225 if ($this->here == '/') {
00226 $path = 'home';
00227 }
00228 $cache = Inflector::slug($path);
00229
00230 if (empty($cache)) {
00231 return;
00232 }
00233 $cache = $cache . '.php';
00234 $file = '<!--cachetime:' . $cacheTime . '--><?php';
00235
00236 if (empty($this->plugin)) {
00237 $file .= '
00238 App::import(\'Controller\', \'' . $this->controllerName. '\');
00239 ';
00240 } else {
00241 $file .= '
00242 App::import(\'Controller\', \'' . $this->plugin . '.' . $this->controllerName. '\');
00243 ';
00244 }
00245
00246 $file .= '$controller =& new ' . $this->controllerName . 'Controller();
00247 $controller->plugin = $this->plugin = \''.$this->plugin.'\';
00248 $controller->helpers = $this->helpers = unserialize(\'' . serialize($this->helpers) . '\');
00249 $controller->base = $this->base = \'' . $this->base . '\';
00250 $controller->layout = $this->layout = \'' . $this->layout. '\';
00251 $controller->webroot = $this->webroot = \'' . $this->webroot . '\';
00252 $controller->here = $this->here = \'' . $this->here . '\';
00253 $controller->namedArgs = $this->namedArgs = \'' . $this->namedArgs . '\';
00254 $controller->argSeparator = $this->argSeparator = \'' . $this->argSeparator . '\';
00255 $controller->params = $this->params = unserialize(stripslashes(\'' . addslashes(serialize($this->params)) . '\'));
00256 $controller->action = $this->action = unserialize(\'' . serialize($this->action) . '\');
00257 $controller->data = $this->data = unserialize(stripslashes(\'' . addslashes(serialize($this->data)) . '\'));
00258 $controller->themeWeb = $this->themeWeb = \'' . $this->themeWeb . '\';';
00259
00260 if ($useCallbacks == true) {
00261 $file .= '
00262 $controller->constructClasses();
00263 $controller->Component->initialize($controller);
00264 $controller->beforeFilter();
00265 $controller->Component->startup($controller);';
00266 }
00267
00268 $file .= '
00269 Router::setRequestInfo(array($this->params, array(\'base\' => $this->base, \'webroot\' => $this->webroot)));
00270 $loadedHelpers = array();
00271 $loadedHelpers = $this->_loadHelpers($loadedHelpers, $this->helpers);
00272 foreach (array_keys($loadedHelpers) as $helper) {
00273 $camelBackedHelper = Inflector::variable($helper);
00274 ${$camelBackedHelper} =& $loadedHelpers[$helper];
00275 $this->loaded[$camelBackedHelper] =& ${$camelBackedHelper};
00276 }
00277 ?>';
00278 $content = preg_replace("/(<\\?xml)/", "<?php echo '$1';?>",$content);
00279 $file .= $content;
00280 return cache('views' . DS . $cache, $file, $timestamp);
00281 }
00282 }
00283
00284 ?>