pCache.class.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /*
  3. pCache - Faster renderding using data cache
  4. Copyright (C) 2008 Jean-Damien POGOLOTTI
  5. Version 1.1.2 last updated on 06/17/08
  6. http://pchart.sourceforge.net
  7. This program is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 1,2,3 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. Class initialisation :
  18. pCache($CacheFolder="Cache/")
  19. Cache management :
  20. IsInCache($Data)
  21. GetFromCache($ID,$Data)
  22. WriteToCache($ID,$Data,$Picture)
  23. DeleteFromCache($ID,$Data)
  24. ClearCache()
  25. Inner functions :
  26. GetHash($ID,$Data)
  27. */
  28. /* pCache class definition */
  29. require_once dirname(__FILE__) . '/../../global.inc.php';
  30. //the cache default folder was changed
  31. class pCache
  32. {
  33. var $HashKey = "";
  34. var $CacheFolder = "";
  35. /* Create the pCache object */
  36. function pCache($CacheFolder="Cache/")
  37. {
  38. $this->CacheFolder = api_get_path(SYS_ARCHIVE_PATH);
  39. }
  40. /* This function is clearing the cache folder */
  41. function ClearCache()
  42. {
  43. if ($handle = opendir($this->CacheFolder))
  44. {
  45. while (false !== ($file = readdir($handle)))
  46. {
  47. if ( $file != "." && $file != ".." )
  48. unlink($this->CacheFolder.$file);
  49. }
  50. closedir($handle);
  51. }
  52. }
  53. /* This function is checking if we have an offline version of this chart */
  54. function IsInCache($ID,$Data,$Hash="")
  55. {
  56. if ( $Hash == "" )
  57. $Hash = $this->GetHash($ID,$Data);
  58. if ( file_exists($this->CacheFolder.$Hash) )
  59. return(TRUE);
  60. else
  61. return(FALSE);
  62. }
  63. /* This function is making a copy of drawn chart in the cache folder */
  64. function WriteToCache($ID,$Data,$Picture)
  65. {
  66. $Hash = $this->GetHash($ID,$Data);
  67. $FileName = $this->CacheFolder.$Hash;
  68. imagepng($Picture->Picture,$FileName);
  69. }
  70. /* This function is removing any cached copy of this chart */
  71. function DeleteFromCache($ID,$Data)
  72. {
  73. $Hash = $this->GetHash($ID,$Data);
  74. $FileName = $this->CacheFolder.$Hash;
  75. if ( file_exists($FileName ) )
  76. unlink($FileName);
  77. }
  78. /* This function is retrieving the cached picture if applicable */
  79. function GetFromCache($ID,$Data)
  80. {
  81. $Hash = $this->GetHash($ID,$Data);
  82. if ( $this->IsInCache("","",$Hash ) )
  83. {
  84. $FileName = $this->CacheFolder.$Hash;
  85. header('Content-type: image/png');
  86. @readfile($FileName);
  87. exit();
  88. }
  89. }
  90. /* This function is building the graph unique hash key */
  91. function GetHash($ID,$Data)
  92. {
  93. $mKey = "$ID";
  94. foreach($Data as $key => $Values)
  95. {
  96. $tKey = "";
  97. foreach($Values as $Serie => $Value)
  98. $tKey = $tKey.$Serie.$Value;
  99. $mKey = $mKey.md5($tKey);
  100. }
  101. return(md5($mKey));
  102. }
  103. }
  104. ?>