cachepages.class.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. class cachepages
  3. {
  4. public $cacheRoot = '';
  5. public $cacheLimitTime = '';
  6. public $cacheFileName = '';
  7. public $cacheFileExt = 'php';
  8. public function cachepages($cacheLimitTime, $curscript)
  9. {
  10. if ($cacheLimitTime) {
  11. $this->cacheLimitTime = $cacheLimitTime;
  12. }
  13. if (CURSCRIPT == 'information') {
  14. $this->cacheRoot = MYMPS_DATA . '/pagesinfo/';
  15. }
  16. else if (CURSCRIPT == 'category') {
  17. $this->cacheRoot = MYMPS_DATA . '/pageslist/';
  18. }
  19. else {
  20. $this->cacheRoot = MYMPS_DATA . '/pagesmymps/';
  21. }
  22. $this->cacheFileName = $this->getCacheFileName($curscript);
  23. ob_start();
  24. }
  25. public function cacheCheck()
  26. {
  27. global $timestamp;
  28. if (0 < $this->cacheLimitTime && file_exists($this->cacheFileName)) {
  29. $cachePagesTime = $this->getFileCreateTime($this->cacheFileName);
  30. if ($timestamp < $cachePagesTime + $this->cacheLimitTime) {
  31. echo file_get_contents($this->cacheFileName);
  32. ob_end_flush();
  33. exit();
  34. }
  35. }
  36. }
  37. public function caching($staticFileName = '')
  38. {
  39. global $timestamp;
  40. if ($staticFileName) {
  41. $this->saveFile($staticFileName, $cacheContent);
  42. }
  43. $cacheContent = ob_get_contents();
  44. ob_end_flush();
  45. $this->saveFile($this->cacheFileName, $cacheContent);
  46. }
  47. public function clearCache($fileName = 'all')
  48. {
  49. if ($fileName != 'all') {
  50. $fileName = $this->cacheRoot . $fileName . '.' . $this->cacheFileExt;
  51. if (file_exists($fileName)) {
  52. return @unlink($fileName);
  53. }
  54. else {
  55. return false;
  56. }
  57. }
  58. if (is_dir($this->cacheRoot)) {
  59. if ($dir = @opendir($this->cacheRoot)) {
  60. while ($file = @readdir($dir)) {
  61. $check = is_dir($file);
  62. if (!$check) {
  63. @unlink($this->cacheRoot . $file);
  64. }
  65. }
  66. @closedir($dir);
  67. return true;
  68. }
  69. else {
  70. return false;
  71. }
  72. }
  73. else {
  74. return false;
  75. }
  76. }
  77. public function getCacheFileName($curscript)
  78. {
  79. return $this->cacheRoot . $curscript . '.' . $this->cacheFileExt;
  80. }
  81. public function getFileCreateTime($fileName)
  82. {
  83. if (file_exists($fileName)) {
  84. return filemtime($fileName);
  85. }
  86. else {
  87. return 0;
  88. }
  89. }
  90. public function saveFile($fileName, $text)
  91. {
  92. if (!$fileName || !$text) {
  93. return false;
  94. }
  95. if ($this->makeDir(dirname($fileName))) {
  96. if ($fp = fopen($fileName, 'w')) {
  97. if (@fwrite($fp, $text)) {
  98. fclose($fp);
  99. return true;
  100. }
  101. else {
  102. fclose($fp);
  103. return false;
  104. }
  105. }
  106. }
  107. return false;
  108. }
  109. public function makeDir($dir, $mode = '0777')
  110. {
  111. if (!file_exists($dir)) {
  112. makeDir(dirname($dir));
  113. mkdir($dir, $mode);
  114. return true;
  115. }
  116. else if (file_exists($dir)) {
  117. return true;
  118. }
  119. else {
  120. return false;
  121. }
  122. }
  123. }
  124. ?>