PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/core/helpers/Cache.php

http://kancms.googlecode.com/
PHP | 129 lines | 66 code | 26 blank | 37 comment | 22 complexity | 409cde96439728f70d8691a157b2a8c1 MD5 | raw file
Possible License(s): BSD-2-Clause, GPL-2.0, LGPL-2.1
  1. <?php
  2. if (!defined('CORE_PATH'))
  3. die('Access Denied');
  4. /**
  5. * Description of Cache
  6. *
  7. * @author Francis Adu-Gyamfi
  8. */
  9. class Cache extends Helper {
  10. public function beginCaching() {
  11. /**
  12. * Begin Caching Script, courtesy of
  13. * http://www.addedbytes.com/articles/caching-output-in-php/
  14. */
  15. $cacheEnabled = getSetting('CacheEnabled', false);
  16. // we'll only run the caching when the request method is a GET, hence when users
  17. // POST data we will skip caching all together and allow the scripts to process the requests
  18. // normally
  19. if ($_SERVER['REQUEST_METHOD'] == 'GET' && $cacheEnabled == 'true') {
  20. // Settings
  21. $cachedir = kan_fix_path('../assets/cache/'); // Directory to cache files in (keep outside web root)
  22. if (!is_dir($cachedir)) {
  23. @mkdir($cachedir);
  24. }
  25. $cachetime = 600; // Seconds to cache files for
  26. $cacheext = 'cache'; // Extension to give cached files (usually cache, htm, txt)
  27. $siteid = isset($_GET['siteid']) ? kan_clean_input($_GET['siteid']) : "";
  28. // Ignore List
  29. $ignore_list = array(
  30. 'xxx'
  31. );
  32. // Script
  33. $page = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; // Requested page
  34. $cachefile = $cachedir . $siteid . '_' . md5($page) . '.' . $cacheext; // Cache file to either load or create
  35. $ignore_page = false;
  36. for ($i = 0; $i < count($ignore_list); $i++) {
  37. $ignore_page = (strpos($page, $ignore_list[$i]) !== false) ? true : $ignore_page;
  38. }
  39. $cachefile_created = ((@file_exists($cachefile)) && ($ignore_page === false)) ? @filemtime($cachefile) : 0;
  40. @clearstatcache();
  41. // Show file from cache if still valid
  42. if (time() - $cachetime < $cachefile_created) {
  43. //ob_start('ob_gzhandler');
  44. @readfile($cachefile);
  45. //ob_end_flush();
  46. exit();
  47. }
  48. // If we're still here, we need to generate a cache file
  49. ob_start();
  50. }
  51. }
  52. public function endCaching() {
  53. /**
  54. * End Caching Script, courtesy of
  55. * http://www.addedbytes.com/articles/caching-output-in-php/
  56. */
  57. // we'll only run the caching when the request method is a GET, hence when users
  58. // POST data we will skip caching all together and allow the scripts to process the requests
  59. // normally
  60. $cacheEnabled = getSetting('CacheEnabled', false);
  61. if ($_SERVER['REQUEST_METHOD'] == 'GET' && $cacheEnabled == 'true') {
  62. $cachedir = kan_fix_path('../assets/cache/'); // Directory to cache files in (keep outside web root)
  63. $cacheext = 'cache'; // Extension to give cached files (usually cache, htm, txt)
  64. $page = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; // Requested page
  65. $siteid = isset($_GET['siteid']) ? kan_clean_input($_GET['siteid']) : "";
  66. $cachefile = $cachedir . $siteid . '_' . md5($page) . '.' . $cacheext; // Cache file to either load or create
  67. // Now the script has run, generate a new cache file
  68. $fp = @fopen($cachefile, 'w');
  69. $cache_track = "\n<!-- Cache Of $page -->";
  70. $contents = ob_get_contents() . $cache_track;
  71. // save the contents of output buffer to the file
  72. @fwrite($fp, $contents);
  73. @fclose($fp);
  74. ob_end_flush();
  75. }
  76. }
  77. /**
  78. * Empties the current system cache completely, or only for the site with
  79. * the specified tag if supplied
  80. *
  81. * @param string $site_tag the Unique Identifier for the site
  82. */
  83. public function clearCache($site_tag = '') {
  84. // Settings
  85. $cachedir = kan_fix_path('../assets/cache/'); // Directory to cache files in
  86. $handle = @opendir($cachedir);
  87. if ($handle) {
  88. while (false !== ($file = @readdir($handle))) {
  89. if ($file != '.' and $file != '..') {
  90. if ($site_tag == '') {
  91. //echo $file . ' deleted.<br>';
  92. @unlink($cachedir . '/' . $file);
  93. } else if (preg_match("/^" . $site_tag . "/", $file)) {
  94. //echo $file . ' deleted.<br>';
  95. @unlink($cachedir . '/' . $file);
  96. }
  97. }
  98. }
  99. @closedir($handle);
  100. }
  101. }
  102. }
  103. ?>