PageRenderTime 59ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/classes/cache_html.php

https://github.com/enigma1/i-metrics-cms
PHP | 293 lines | 233 code | 35 blank | 25 comment | 51 complexity | e542ceaa642f84e30f270788152c1c18 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /*
  3. //----------------------------------------------------------------------------
  4. // Copyright (c) 2006-2011 Asymmetric Software - Innovation & Excellence
  5. // Author: Mark Samios
  6. // http://www.asymmetrics.com
  7. // Cache History class
  8. // Cache history for HTML pages. Sends a 304 header on cache hits.
  9. //----------------------------------------------------------------------------
  10. // Script is intended to be used with:
  11. // osCommerce, Open Source E-Commerce Solutions
  12. // http://www.oscommerce.com
  13. // Copyright (c) 2003 osCommerce
  14. //----------------------------------------------------------------------------
  15. // Released under the GNU General Public License
  16. //----------------------------------------------------------------------------
  17. */
  18. class cache_html {
  19. // Compatibility constructor
  20. function cache_html() {
  21. $this->reset();
  22. }
  23. function reset() {
  24. $this->tags_array = array();
  25. $this->script_type = $this->script_duration = $this->script_params = $this->script_signature = '';
  26. $this->bot_cacheable = false;
  27. $this->declines = 0;
  28. }
  29. function load() {
  30. extract(tep_load('sessions'));
  31. $this->tags_array =& $cSessions->register('cache_html_tags_array', array());
  32. $this->declines =& $cSessions->register('cache_html_declines', 0);
  33. }
  34. function was_bot_cacheable() {
  35. return $this->bot_cacheable;
  36. }
  37. function flush_cache() {
  38. $this->tags_array = array();
  39. }
  40. function flush_tag($tag) {
  41. unset($this->tags_array[$tag]);
  42. }
  43. function get_time_offset($offset, $now=true) {
  44. $newtime = $offset;
  45. if( $now ) {
  46. $newtime += time();
  47. }
  48. $gmt_time = gmdate('D, d M Y H:i:s', $newtime).' GMT';
  49. return $gmt_time;
  50. }
  51. //-MS- Sessions Cache HTML
  52. function check_script() {
  53. extract(tep_load('defs', 'http_validator', 'database', 'sessions', 'message_stack'));
  54. if( SCRIPTS_HTML_CACHE_ENABLE == 'false' || !$cSessions->has_started() )
  55. return;
  56. $this->load();
  57. // Flush caching on POST
  58. if( $http->req == 'POST' ) {
  59. $this->flush_cache();
  60. return;
  61. }
  62. // Abort cacheing on errors
  63. $message_array = $msg->get();
  64. if( count($message_array) ) {
  65. return;
  66. }
  67. $md5_script = md5($cDefs->script);
  68. $check_query = $db->query("select cache_html_type, cache_html_duration, cache_html_params from " . TABLE_CACHE_HTML . " where cache_html_key = '" . $db->filter($md5_script) . "'");
  69. if( !$db->num_rows($check_query) ) {
  70. return;
  71. }
  72. $check_array = $db->fetch_array($check_query);
  73. $this->script_type = $check_array['cache_html_type'];
  74. $this->script_duration = $check_array['cache_html_duration'];
  75. $this->script_params = $check_array['cache_html_params'];
  76. if( $this->script_type == 1 ) {
  77. $this->check_cache();
  78. } elseif($this->script_type == 2) {
  79. $this->flush_cache();
  80. } elseif($this->script_type == 3) {
  81. if( SCRIPTS_HTML_CACHE_PARAMS == 'false' ) {
  82. $this->flush_cache();
  83. return;
  84. }
  85. $params_array = explode(',', $check_array['cache_html_params']);
  86. foreach($params_array as $key => $value) {
  87. if( isset($_GET[trim($value)]) ) {
  88. $this->flush_cache();
  89. return;
  90. }
  91. }
  92. $this->check_cache();
  93. }
  94. }
  95. function check_cache() {
  96. extract(tep_load('defs', 'sessions', 'validator'));
  97. $this->script_signature = md5($cDefs->script . implode('', array_keys($cValidator->get_array)) . implode('', $cValidator->get_array));
  98. if( !isset($this->tags_array[$this->script_signature]) ) {
  99. $this->tags_array[$this->script_signature] = $cDefs->script;
  100. } else {
  101. $this->set_cache();
  102. }
  103. $this->set_headers();
  104. }
  105. function set_cache() {
  106. extract(tep_load('http_validator'));
  107. $oldtime = time() - $this->script_duration;
  108. if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
  109. $if_modified_since = preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE']);
  110. $expiry = strtotime($if_modified_since);
  111. if($expiry > $oldtime) {
  112. $this->set_cache_record(true);
  113. $expiry = $this->get_time_offset($expiry+$this->script_duration, false);
  114. if( GZIP_COMPRESSION == 'true' ) {
  115. ob_end_clean();
  116. }
  117. $http->set_headers(
  118. 'Pragma: private',
  119. 'Expires: ' . $expiry,
  120. 'Cache-Control: must-revalidate, max-age=0, s-maxage=0, private',
  121. 'HTTP/1.1 304 Not Modified'
  122. );
  123. $http->send_headers(true);
  124. }
  125. } else {
  126. // Browser doesn't want to cache content
  127. $this->declines++;
  128. }
  129. }
  130. function set_headers() {
  131. extract(tep_load('http_validator'));
  132. $this->set_cache_record();
  133. $now = $this->get_time_offset(0);
  134. $expiry = $this->get_time_offset($this->script_duration);
  135. $http->set_headers(
  136. 'Pragma: private',
  137. 'Last-Modified: ' . $now,
  138. 'Expires: ' . $expiry,
  139. 'ETag: "' . $this->script_signature . '"',
  140. 'Cache-Control: must-revalidate, max-age=0, s-maxage=0, private'
  141. );
  142. $http->send_headers();
  143. }
  144. function set_cache_record($hit = false) {
  145. extract(tep_load('defs', 'database'));
  146. if( SCRIPTS_HTML_CACHE_HITS == 'false' )
  147. return;
  148. $md5_script = md5($cDefs->script);
  149. $check_query = $db->query("select cache_html_key from " . TABLE_CACHE_HTML_REPORTS . " where cache_html_key = '" . $db->filter($md5_script) . "'");
  150. if( $db->num_rows($check_query) ) {
  151. if( $hit == false ) {
  152. $db->query("update " . TABLE_CACHE_HTML_REPORTS . " set cache_misses = cache_misses+1 where cache_html_key = '" . $db->filter($md5_script) . "'");
  153. } else {
  154. $db->query("update " . TABLE_CACHE_HTML_REPORTS . " set cache_hits = cache_hits+1 where cache_html_key = '" . $db->filter($md5_script) . "'");
  155. }
  156. } else {
  157. $sql_data_array = array(
  158. 'cache_html_key' => $db->prepare_input($md5_script),
  159. 'cache_html_script' => $db->prepare_input($cDefs->script)
  160. );
  161. if( $hit == false ) {
  162. $sql_insert_array = array(
  163. 'cache_misses' => '1'
  164. );
  165. } else {
  166. $sql_insert_array = array(
  167. 'cache_hits' => '1'
  168. );
  169. }
  170. $sql_data_array = array_merge($sql_data_array, $sql_insert_array);
  171. $db->perform(TABLE_CACHE_HTML_REPORTS, $sql_data_array);
  172. }
  173. }
  174. //-MS- Sessions Cache HTML
  175. //-MS- Spiders Cache HTML
  176. // These HTML Cache functions used only for spiders
  177. function bot_check_modified_header() {
  178. extract(tep_load('defs', 'database'));
  179. if( SPIDERS_HTML_CACHE_ENABLE == 'false' )
  180. return;
  181. if( SPIDERS_HTML_CACHE_GLOBAL == 'true' ) {
  182. $this->bot_send_304_header(SPIDERS_HTML_CACHE_TIMEOUT);
  183. return;
  184. }
  185. $md5_script = md5($cDefs->script);
  186. $check_query = $db->query("select cache_html_duration from " . TABLE_CACHE_HTML . " where cache_html_type !='2' and cache_html_key = '" . $db->filter($md5_script) . "'");
  187. if( $db->num_rows($check_query) ) {
  188. $check_array = $db->fetch_array($check_query);
  189. $this->bot_send_304_header($check_array['cache_html_duration']);
  190. }
  191. }
  192. function bot_send_304_header($timeout) {
  193. extract(tep_load('defs', 'http_validator'));
  194. $oldtime = time() - $timeout;
  195. if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
  196. $if_modified_since = preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE']);
  197. $expiry = strtotime($if_modified_since);
  198. if($expiry > $oldtime) {
  199. $this->bot_set_cache_record(true);
  200. $expiry = $this->get_time_offset($expiry+$timeout, false);
  201. if( GZIP_COMPRESSION == 'true' ) {
  202. ob_end_clean();
  203. }
  204. $http->set_headers(
  205. 'Pragma: public',
  206. 'Expires: ' . $expiry,
  207. 'Cache-Control: must-revalidate, max-age=' . $timeout . ', s-maxage=' . $timeout . ', public',
  208. 'HTTP/1.1 304 Not Modified'
  209. );
  210. $http->send_headers(true);
  211. }
  212. }
  213. $this->bot_set_cache_record();
  214. $script_signature = md5($cDefs->script . implode('', array_keys($_GET)) . implode('', $_GET));
  215. $now = $this->get_time_offset(0);
  216. $expiry = $this->get_time_offset($timeout);
  217. $this->bot_cacheable = true;
  218. $http->set_headers(
  219. 'Pragma: public',
  220. 'Last-Modified: ' . $now,
  221. 'Expires: ' . $expiry,
  222. 'ETag: "' . $script_signature . '"',
  223. 'Cache-Control: must-revalidate, max-age=' . $timeout . ', s-maxage=' . $timeout . ', public'
  224. );
  225. $http->send_headers();
  226. }
  227. function bot_set_cache_record($hit = false) {
  228. extract(tep_load('defs', 'database'));
  229. if( SPIDERS_HTML_CACHE_HITS == 'false' ) return;
  230. $md5_script = md5($cDefs->script);
  231. $check_query = $db->query("select cache_html_key from " . TABLE_CACHE_HTML_REPORTS . " where cache_html_key = '" . $db->filter($md5_script) . "'");
  232. if( $db->num_rows($check_query) ) {
  233. if( $hit == false ) {
  234. $db->query("update " . TABLE_CACHE_HTML_REPORTS . " set cache_spider_misses = cache_spider_misses+1 where cache_html_key = '" . $db->filter($md5_script) . "'");
  235. } else {
  236. $db->query("update " . TABLE_CACHE_HTML_REPORTS . " set cache_spider_hits = cache_spider_hits+1 where cache_html_key = '" . $db->filter($md5_script) . "'");
  237. }
  238. } else {
  239. $sql_data_array = array(
  240. 'cache_html_key' => $db->prepare_input($md5_script),
  241. 'cache_html_script' => $db->prepare_input($cDefs->script)
  242. );
  243. if( $hit == false ) {
  244. $sql_insert_array = array(
  245. 'cache_spider_misses' => '1'
  246. );
  247. } else {
  248. $sql_insert_array = array(
  249. 'cache_spider_hits' => '1'
  250. );
  251. }
  252. $sql_data_array = array_merge($sql_data_array, $sql_insert_array);
  253. $db->perform(TABLE_CACHE_HTML_REPORTS, $sql_data_array);
  254. }
  255. }
  256. //-MS- Spiders Cache HTML EOM
  257. }
  258. ?>