[ Index ] |
PHP Cross Reference of osCMax 2.0.4 |
[Summary view] [Print] [Text view]
1 <?php 2 /*=======================================================================*\ 3 || #################### //-- SCRIPT INFO --// ########################## || 4 || # Script name: page_cache.php 5 || # Contribution: Page Cache 6 || # Version: 1.6 7 || # Date: 20 February 2005 8 || # ------------------------------------------------------------------ # || 9 || #################### //-- COPYRIGHT INFO --// ######################## || 10 || # Copyright 2006 osCMax2005 Bobby Easland # || 11 || # Internet moniker: Chemo # || 12 || # Contact: [email protected] # || 13 || # Commercial Site: http://gigabyte-hosting.com/ # || 14 || # GPL Dev Server: http://mesoimpact.com/ # || 15 || # # || 16 || # This script is free software; you can redistribute it and/or # || 17 || # modify it under the terms of the GNU General Public License # || 18 || # as published by the Free Software Foundation; either version 2 # || 19 || # of the License, or (at your option) any later version. # || 20 || # # || 21 || # This script is distributed in the hope that it will be useful, # || 22 || # but WITHOUT ANY WARRANTY; without even the implied warranty of # || 23 || # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # || 24 || # GNU General Public License for more details. # || 25 || # # || 26 || # Script is intended to be used with: # || 27 || # osCMax Power E-Commerce # || 28 || # http://oscdox.com # || 29 || # Copyright 2006 osCMax # || 30 || ###################################################################### || 31 \*========================================================================*/ 32 33 class page_cache { 34 # Define class variables 35 var $cache_dir, $cache_filename, $cache_param, $cache_file, $customer_cart, $cache_compress, $cache_language, $cache_currency, $customer_sid; 36 var $debug_output = array(); 37 var $cache_lifetime = 5; 38 var $is_guest = false; 39 var $debug_messages = false; 40 var $cache_file_exists = false; 41 var $cache_file_is_expired = true; 42 var $write_cache_file = false; 43 var $output_ob = false; 44 45 /*################################################################################## 46 47 Class constructor must have the $cart_cache passed to keep the customer shopping cart dynamic 48 49 ###################################################################################*/ 50 function page_cache($cart_info, $cache_time = PAGE_CACHE_LIFETIME, $debug_switch = PAGE_CACHE_DEBUG_MODE) { 51 global $SID; 52 # Start populating the debug array with the current database settings 53 $this->debug_output['admin_config_settings']['ENABLE_PAGE_CACHE'] = ENABLE_PAGE_CACHE; 54 $this->debug_output['admin_config_settings']['PAGE_CACHE_LIFETIME'] = PAGE_CACHE_LIFETIME; 55 $this->debug_output['admin_config_settings']['PAGE_CACHE_DEBUG_MODE'] = PAGE_CACHE_DEBUG_MODE; 56 $this->debug_output['admin_config_settings']['PAGE_CACHE_DISABLE_PARAMETERS'] = PAGE_CACHE_DISABLE_PARAMETERS; 57 $this->debug_output['admin_config_settings']['PAGE_CACHE_DELETE_FILES'] = PAGE_CACHE_DELETE_FILES; 58 $this->debug_output['admin_config_settings']['PAGE_CACHE_UPDATE_CONFIG_FILES'] = PAGE_CACHE_UPDATE_CONFIG_FILES; 59 60 # Set the cache directory and store the setting in the debug array 61 $this->cache_dir = DIR_FS_CATALOG . '/cache/'; 62 $this->debug_output['cache_dir'] = $this->cache_dir; 63 64 # If the delete cache file switch is true then let's go through this code 65 if (PAGE_CACHE_DELETE_FILES == 'true') { 66 # Check to see if there is a configuration cache contribution installed and the file exists 67 $this->debug_output['PAGE_CACHE_UPDATE_CONFIG_FILE_EXISTS'] = (file_exists(PAGE_CACHE_UPDATE_CONFIG_FILES) ? 'true' : 'false'); 68 # Call the delete_cache_files() method 69 $this->delete_cache_files(); 70 # Add the "deleted cache files" message to the debug array 71 $this->debug_output['delete_cache_files'] = 'cache files deleted - top level'; 72 # Change the delete cache file switch to "false" 73 tep_db_query("UPDATE configuration SET configuration_value = 'false' WHERE configuration_key = 'PAGE_CACHE_DELETE_FILES'"); 74 # Add the switch change to the debug array 75 $this->debug_output['update_configuration_setting'] = 'PAGE_CACHE_DELETE_FILES reset to false'; 76 # If a configuration cache file is specified include it if it exists 77 if ( file_exists(PAGE_CACHE_UPDATE_CONFIG_FILES) ) { 78 require(PAGE_CACHE_UPDATE_CONFIG_FILES); 79 # Add the "config cache file updated" message to the debug array 80 $this->debug_output['update_config_files'] = 'configuration cache files updated'; 81 } # END if file_exists 82 else { 83 $this->debug_output['update_config_files'] = 'ERROR: update file does not exist'; 84 } 85 } # END delete cache files 86 87 # Take the PHP_SELF global and replace all the /'s 88 # This should work with ALL URL's but best for search engine safe 89 $this->cache_filename = str_replace("/", "_", $_SERVER['PHP_SELF']); 90 # Add the constructed cache_filename to the debug output 91 $this->debug_output['cache_filename'] = $this->cache_filename; 92 # Replace all the /'s in the URL parameters. Should not be needed but just in case... 93 $this->cache_param = str_replace("/", "_", $_SERVER["QUERY_STRING"]); 94 # Remove the osCsid from the cache parameters 95 $this->cache_param = preg_replace("/([&]|)osCsid=.{32}/", "", $_SERVER["QUERY_STRING"]); 96 # Add the constructed cache_param to debug output 97 $this->debug_output['cache_param'] = $this->cache_param; 98 #Add the cache language and store it 99 $this->cache_language = '_' . $_SESSION['language']; 100 $this->debug_output['cache_language'] = $this->cache_language; 101 # Set the cache file currency and store it 102 $this->cache_currency = '_' . $_SESSION['currency']; 103 $this->debug_output['cache_currency'] = $this->cache_currency; 104 # Check whether page parameters are disabled 105 # May be needed in some cases where live help is offered and the LHO session ID is appended to URL's, etc. 106 if (PAGE_CACHE_DISABLE_PARAMETERS == 'false') { 107 # Parameters are NOT disabled so include them in the cache_file naming 108 $this->cache_file = $this->cache_dir.$this->cache_filename.'_'.$this->cache_param.$this->cache_language.$this->cache_currency.".cache"; 109 } else { 110 # Parameters ARE disabled so DO NOT include them in the cache_file naming 111 $this->cache_file = $this->cache_dir.$this->cache_filename.$this->cache_language.$this->cache_currency.".cache"; 112 } 113 # Add the constructed cache_file setting to the debug array 114 $this->debug_output['cache_file'] = $this->cache_file; 115 # Convert cache time from minutes to seconds 116 $this->cache_lifetime = $cache_time * 60; 117 # Add the lifetime to the debug array 118 $this->debug_output['cache_lifetime'] = $this->cache_lifetime . ' seconds'; 119 # collect the garbage 120 if ( rand(0, 100) <= 5 ) $this->collect_garbage(); // 5% chance of triggering GC 121 # Add a way to output the debug array other than globally 122 # To activate this switch append ?debug=1 to the end of a cached file URL 123 ($_GET['debug'] ? $this->debug_messages = 'true' : $this->debug_messages = $debug_switch); 124 # Add the setting to the debug array 125 $this->debug_output['debug_output'] = $this->debug_messages; 126 # Check to see if the customer is a guest and add to the debug array 127 if ( !tep_session_is_registered('customer_id') ) $this->is_guest = true; 128 $this->debug_output['is_guest'] = $this->is_guest; 129 # See if the cache_file exists 130 if ( file_exists($this->cache_file) ) { 131 # File does exist and add to debug array 132 $this->cache_file_exists = true; 133 $this->debug_output['cache_file_exists'] = $this->cache_file_exists; 134 # Check to see whether the file is expired and add to the debug array 135 ( (filemtime($this->cache_file) + $this->cache_lifetime) < time() ) 136 ? $this->cache_file_is_expired = true 137 : $this->cache_file_is_expired = false; 138 $this->debug_output['cache_file_is_expired'] = $this->cache_file_is_expired; 139 } # END if file_exists 140 # Define the customer_cart variable and compress the data then add the message to debug array 141 # The data used here is passed to the class contructor 142 $this->customer_cart = $this->compress_buffer( $cart_info ); 143 $this->debug_output['customer_cart'] = ( is_string($this->customer_cart) ? true : false ); 144 # Set the customers_sid and store it 145 $this->customer_sid = $SID; 146 $this->debug_output['customer_sid'] = $this->customer_sid; 147 } # END of page_cache constructor 148 149 /*################################################################################## 150 151 cache_this_page() method 152 - no arguments 153 - no return 154 155 ###################################################################################*/ 156 function cache_this_page () { 157 global $debug; 158 # Check to see if the customer is logged in and whether the page cache is enabled 159 if ($this->is_guest && ENABLE_PAGE_CACHE =='true') { 160 # Add to the debug array 161 $this->debug_output['is_guest_check'] = 'customer_id not set - cache_this_page()'; 162 # Check to see if the file exists and it is not expired 163 if ( $this->cache_file_exists && !$this->cache_file_is_expired ) { 164 # Good...it does exist and is not expired 165 $this->debug_output['file_exists_and_is_not_expired'] = 'file exists and is not expired'; 166 # Now let's see if they have cookie support enabled 167 if ( !tep_not_null($this->customer_sid) ) { 168 # They have cookies enabled...let's remove the osCsid from the URL's since it is being pulled from cookie 169 echo str_replace(array("<%CART_CACHE%>", "?<osCsid>", "&<osCsid>"), array($this->customer_cart, $this->customer_sid, $this->customer_sid), file_get_contents($this->cache_file) ); 170 } else { 171 # No cookies enabled so let's append the osCsid to every URL 172 echo str_replace(array("<%CART_CACHE%>", "<osCsid>"), array($this->customer_cart, $this->customer_sid), file_get_contents($this->cache_file) ); 173 } 174 $this->debug(); 175 include (DIR_WS_INCLUDES . 'counter.php'); 176 include_once (DIR_WS_INCLUDES . 'application_bottom.php'); 177 exit(); 178 } # END file exists and is not expired 179 180 # Does the file exist or is it expired? 181 if ( !$this->cache_file_exists || $this->cache_file_is_expired ) { 182 # Either it does not exist or is expired. Let's start the cache 183 # Add the info to the debug array 184 $this->debug_output['no_file_or_expired'] = 'file does not exist or is expired'; 185 # Set the flag to write the cache file 186 $this->write_cache_file = true; 187 # We'll need to output the buffer as well 188 $this->output_ob = true; 189 # Start the output buffer 190 ob_start(); 191 # Add the info to the debug array 192 $this->debug_output['ob_started'] = 'ob started @ '. time(); 193 } # END is file does not exist or is expired 194 } # END if $this->is_guest 195 } # END cache_this_page method 196 197 /*################################################################################## 198 199 end_page_cache() method 200 - no arguments 201 - no return 202 203 ###################################################################################*/ 204 function end_page_cache() { 205 # Check to see if the customer is logged in and whether the page cache is enabled 206 if ($this->is_guest && ENABLE_PAGE_CACHE =='true') { 207 # Passed the check...let's add the info to the debug array 208 $this->debug_output['is_guess_check_end'] = 'customer_id not set - end_page_cache()'; 209 # If the output switch is true lets output the buffer 210 if ( $this->output_ob ) { 211 # Switch enabled...add the info to the debug array 212 $this->debug_output['output_ob'] = 'output_ob = true'; 213 # Store the compressed buffer as a variable 214 $this->cache_compress = $this->compress_buffer( ob_get_clean() ); 215 # Add the info to the debug array 216 $this->debug_output['ob_compressed'] = 'output buffer flushed and compressed'; 217 # Output the compressed buffer 218 $this->output_buffer($this->cache_compress); 219 # Add the info to the debug array 220 $this->debug_output['output'] = 'compressed ob sent to screen'; 221 } # END if $this->output_buffer 222 223 # If the write to file swith is true let's write the file 224 if ( $this->write_cache_file ) { 225 # Passed check. Write the file 226 $this->write_file ( $this->cache_compress ); 227 # Let's add the info to the debug array 228 $this->debug_output['write_file'] = 'compressed ob written to file'; 229 } # END if write cache file 230 231 # Unset the buffer 232 unset ($this->cache_compress); 233 # Add the info to the debug array 234 $this->debug_output['unset_cache_compress'] = 'cache compress unset'; 235 } #END if $this->is_guest 236 $this->debug(); 237 } #END end_page_cache method 238 239 /*################################################################################## 240 241 compress_buffer() method 242 - arguments: $buffer (string) 243 - return: compressed $buffer 244 245 ###################################################################################*/ 246 function compress_buffer ($buffer) { 247 # Add the info to the debug array 248 $this->debug_output['compress_buffer'] = 'compressing buffer'; 249 # Return the compressed buffer 250 return preg_replace('/\>\s+\</', '> <', $buffer); 251 } # END compress buffer 252 253 /*################################################################################## 254 255 write_file() method 256 - arguments: $buffer (string) 257 - return: true; 258 259 ###################################################################################*/ 260 function write_file($buffer) { 261 $fp = fopen($this->cache_file,'w+'); 262 fwrite($fp, $buffer); 263 fclose($fp); 264 $this->debug_output['file_write'] = 'buffer writtent to file'; 265 unset($buffer); 266 $this->debug_output['unset_write_buffer'] = 'write buffer unset'; 267 return true; 268 } # END write_cache_file method 269 270 /*################################################################################## 271 272 outpur_buffer() method 273 - arguments: $buffer (string) 274 - return: buffer with customer cart 275 276 ###################################################################################*/ 277 function output_buffer($buffer) { 278 if ( !tep_not_null($this->customer_sid) ) { 279 echo str_replace(array("<%CART_CACHE%>", "?<osCsid>", "&<osCsid>"), array($this->customer_cart, $this->customer_sid, $this->customer_sid), $buffer ); 280 } else { 281 echo str_replace(array("<%CART_CACHE%>", "<osCsid>"), array($this->customer_cart, $this->customer_sid), $buffer); 282 } 283 $this->debug_output['output_2_screen'] = 'successfully output to screen'; 284 unset($buffer); 285 $this->debug_output['unset_screen_buffer'] = 'screen buffer unset'; 286 } # END output_buffer() method 287 288 /*################################################################################## 289 290 debug() method 291 - arguments: none 292 - return: debug array 293 294 ###################################################################################*/ 295 function debug () { 296 if ($this->debug_messages == 'true') { 297 $data_store['PAGE_CACHE'] = $this->debug_output; 298 $data_store['COOKIE_INFO'] = $_COOKIE; 299 $data_store['SESSION_INFO'] = $_SESSION; 300 echo '<pre>'; 301 print_r($data_store); 302 echo '</pre>'; 303 } 304 } # END debug() method 305 306 /*################################################################################## 307 308 delete_cache_file() method 309 - arguments: none 310 - return: true; 311 312 ###################################################################################*/ 313 function delete_cache_files () { 314 foreach (glob($this->cache_dir."*.cache") as $filename) { 315 $this->debug_output['deleting_file'][]= $filename; 316 @unlink($filename); 317 } 318 return true; 319 } # END delete_cache_files() method 320 321 /*################################################################################## 322 323 collect_garbage() method 324 - arguments: none 325 - return: true; 326 327 ###################################################################################*/ 328 function collect_garbage() { 329 foreach (glob($this->cache_dir."{*.cache}", GLOB_BRACE) as $filename) { 330 if ( ( filemtime($filename) + $this->cache_lifetime) < time() ) 331 @unlink($filename); 332 } 333 return true; 334 } # END collect_garbage() method 335 336 } #END of class 337 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Jan 1 13:43:16 2010 | Cross-referenced by PHPXref 0.7 |