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 Helper{
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
00068
00069 function cache($file, $out, $cache = false) {
00070 if (is_array($this->cacheAction)) {
00071 $check = str_replace('/', '_', $this->here);
00072 $replace = str_replace('/', '_', $this->base);
00073 $match = str_replace($this->base, '', $this->here);
00074 $match = str_replace('
00075 $match = str_replace('/' . $this->controllerName . '/', '', $match);
00076 $check = str_replace($replace, '', $check);
00077 $check = str_replace('_' . $this->controllerName . '_', '', $check);
00078 $check = convertSlash($check);
00079 $check = preg_replace('/^_+/', '', $check);
00080 $keys = str_replace('/', '_', array_keys($this->cacheAction));
00081 $found = array_keys($this->cacheAction);
00082 $index = null;
00083 $count = 0;
00084
00085 foreach ($keys as $key => $value) {
00086 if (strpos($check, $value) === 0) {
00087 $index = $found[$count];
00088 break;
00089 }
00090 $count++;
00091 }
00092
00093 if (isset($index)) {
00094 $pos1 = strrpos($match, '/');
00095 $char = strlen($match) - 1;
00096
00097 if ($pos1 == $char) {
00098 $match = substr($match, 0, $char);
00099 }
00100
00101 $key = $match;
00102 } elseif ($this->action == 'index') {
00103 $index = 'index';
00104 }
00105 if (isset($this->cacheAction[$index])) {
00106 $cacheTime = $this->cacheAction[$index];
00107 } else {
00108 $cacheTime = 0;
00109 }
00110 } else {
00111 $cacheTime = $this->cacheAction;
00112 }
00113
00114 if ($cacheTime != '' && $cacheTime > 0) {
00115 $this->__parseFile($file, $out);
00116
00117 if ($cache === true) {
00118 $cached = $this->__parseOutput($out);
00119 $this->__writeFile($cached, $cacheTime);
00120 }
00121 }
00122 return $out;
00123 }
00124
00125
00126
00127
00128
00129
00130
00131 function __parseFile($file, $cache) {
00132 if (is_file($file)) {
00133 $file = file_get_contents($file);
00134 } elseif ($file = fileExistsInPath($file)) {
00135 $file = file_get_contents($file);
00136 }
00137
00138 preg_match_all('/(<cake:nocache>(?<=<cake:nocache>)[\\s\\S]*?(?=<\/cake:nocache>)<\/cake:nocache>)/i', $cache, $oresult, PREG_PATTERN_ORDER);
00139 preg_match_all('/(?<=<cake:nocache>)([\\s\\S]*?)(?=<\/cake:nocache>)/i', $file, $result, PREG_PATTERN_ORDER);
00140
00141 if (!empty($this->__replace)) {
00142 foreach ($oresult['0'] as $k => $element) {
00143 if (array_search($element, $this->__match) !== false) {
00144 array_splice($oresult[0], $k, 1);
00145 }
00146 }
00147 }
00148
00149 if (!empty($result['0'])) {
00150 $count = 0;
00151
00152 foreach ($result['0'] as $block) {
00153 if (isset($oresult['0'][$count])) {
00154 $this->__replace[] = $block;
00155 $this->__match[] = $oresult['0'][$count];
00156 }
00157 $count++;
00158 }
00159 }
00160 }
00161
00162
00163
00164
00165
00166
00167
00168 function __parseOutput($cache) {
00169 $count = 0;
00170 if (!empty($this->__match)) {
00171
00172 foreach ($this->__match as $found) {
00173 $original = $cache;
00174 $length = strlen($found);
00175 $position = 0;
00176
00177 for ($i = 1; $i <= 1; $i++) {
00178 $position = strpos($cache, $found, $position);
00179
00180 if ($position !== false) {
00181 $cache = substr($original, 0, $position);
00182 $cache .= $this->__replace[$count];
00183 $cache .= substr($original, $position + $length);
00184 } else {
00185 break;
00186 }
00187 }
00188 $count++;
00189 }
00190 return $cache;
00191 }
00192 return $cache;
00193 }
00194
00195
00196
00197
00198
00199
00200
00201
00202 function __writeFile($content, $timestamp) {
00203 $now = time();
00204
00205 if (is_numeric($timestamp)) {
00206 $cacheTime = $now + $timestamp;
00207 } else {
00208 $cacheTime = strtotime($timestamp, $now);
00209 }
00210
00211 $cache = convertSlash($this->here);
00212 if (empty($cache)) {
00213 return;
00214 }
00215
00216 $cache = $cache . '.php';
00217 $file = '<!--cachetime:' . $cacheTime . '--><?php';
00218 if (empty($this->plugin)) {
00219 $file .= '
00220 loadController(\'' . $this->view->name. '\');
00221 loadModels();
00222 ';
00223 } else {
00224 $file .= '
00225 if (!class_exists(\'AppController\')) {
00226 if (file_exists(\'' . APP . 'app_controller.php\')) {
00227 require(\''. APP . 'app_controller.php\');
00228 } else {
00229 require(\''.CAKE . 'app_controller.php\');
00230 }
00231 }
00232 loadPluginController(\''.$this->plugin.'\',\''.$this->view->name.'\');
00233 loadPluginModels(\''.$this->plugin.'\');
00234 ';
00235 }
00236 $file .= '$this->controller = new ' . $this->view->name . 'Controller();
00237 $this->controller->plugin = \''.$this->plugin.'\';
00238 $this->controller->_initComponents();
00239 $this->helpers = unserialize(\'' . serialize($this->view->helpers) . '\');
00240 $this->base = \'' . $this->view->base . '\';
00241 $this->layout = \'' . $this->view->layout. '\';
00242 $this->webroot = \'' . $this->view->webroot . '\';
00243 $this->here = \'' . $this->view->here . '\';
00244 $this->params = unserialize(stripslashes(\'' . addslashes(serialize($this->view->params)) . '\'));
00245 $this->action = unserialize(\'' . serialize($this->view->action) . '\');
00246 $this->data = unserialize(stripslashes(\'' . addslashes(serialize($this->view->data)) . '\'));
00247 $this->themeWeb = \'' . $this->view->themeWeb . '\';
00248 $this->plugin = \'' . $this->view->plugin . '\';
00249 $loadedHelpers = array();
00250 $loadedHelpers = $this->_loadHelpers($loadedHelpers, $this->helpers);
00251 foreach (array_keys($loadedHelpers) as $helper)
00252 {
00253 $replace = strtolower(substr($helper, 0, 1));
00254 $camelBackedHelper = preg_replace(\'/\\w/\', $replace, $helper, 1);
00255 ${$camelBackedHelper} =& $loadedHelpers[$helper];
00256
00257 if (isset(${$camelBackedHelper}->helpers) && is_array(${$camelBackedHelper}->helpers))
00258 {
00259 foreach (${$camelBackedHelper}->helpers as $subHelper)
00260 {
00261 ${$camelBackedHelper}->{$subHelper} =& $loadedHelpers[$subHelper];
00262 }
00263 }
00264 $this->loaded[$camelBackedHelper] = (${$camelBackedHelper});
00265 }
00266 ?>';
00267 $content = preg_replace("/(<\\?xml)/", "<?php echo '$1';?>",$content);
00268 $file .= $content;
00269 return cache('views' . DS . $cache, $file, $timestamp);
00270 }
00271 }
00272 ?>