BxDolCacheUtilities.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup DolphinCore Dolphin Core
  7. * @{
  8. */
  9. // TODO: check if it is used or maybe usefull for future use, also it needs some refactoring
  10. class BxDolCacheUtilities extends BxDol
  11. {
  12. protected $oCacheDb;
  13. protected $oCacheTemplates;
  14. function __construct ()
  15. {
  16. parent::__construct();
  17. // DB
  18. $this->oCacheDb = BxDolDb::getInstance()->getDbCacheObject();
  19. // templates
  20. $this->oCacheTemplates = BxDolTemplate::getInstance()->getTemplatesCacheObject();
  21. }
  22. /**
  23. * Get singleton instance of the class
  24. */
  25. public static function getInstance()
  26. {
  27. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  28. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolCacheUtilities();
  29. return $GLOBALS['bxDolClasses'][__CLASS__];
  30. }
  31. function clear($sCache)
  32. {
  33. return $this->_action($sCache, 'clear');
  34. }
  35. function size($sCache, $isFormatted = false)
  36. {
  37. $iSize = $this->_action($sCache, 'size');
  38. return $isFormatted ? sprintf("%.2f", $iSize / 1024 / 1024) : $iSize;
  39. }
  40. protected function _action($sCache, $sMode = 'clear')
  41. {
  42. $sFuncCacheObject = $sMode == 'clear' ? '_clearCacheObject' : '_getSizeCacheObject';
  43. $sFuncCacheFile = $sMode == 'clear' ? '_clearCache' : '_getSizeCache';
  44. switch ($sCache) {
  45. case 'users':
  46. $mixedResult = $this->$sFuncCacheFile('user', BX_DIRECTORY_PATH_CACHE);
  47. break;
  48. case 'db':
  49. $mixedResult = $this->$sFuncCacheObject ($this->oCacheDb, 'db_');
  50. break;
  51. case 'template':
  52. $mixedResult = $this->$sFuncCacheObject ($this->oCacheTemplates, BxDolStudioTemplate::getInstance()->getCacheFilePrefix($sCache));
  53. break;
  54. case 'css':
  55. $mixedResult = $this->$sFuncCacheFile(BxDolStudioTemplate::getInstance()->getCacheFilePrefix($sCache), BX_DIRECTORY_PATH_CACHE_PUBLIC);
  56. break;
  57. case 'js':
  58. $mixedResult = $this->$sFuncCacheFile(BxDolStudioTemplate::getInstance()->getCacheFilePrefix($sCache), BX_DIRECTORY_PATH_CACHE_PUBLIC);
  59. break;
  60. }
  61. return $mixedResult;
  62. }
  63. function _clearCacheObject($oCache, $sPrefix)
  64. {
  65. if (!$oCache->removeAllByPrefix ($sPrefix))
  66. return array('code' => 1, 'message' => _t('_adm_dbd_err_c_clean_failed'));
  67. else
  68. return array('code' => 0, 'message' => _t('_adm_dbd_msg_c_clean_success'));
  69. }
  70. function _getSizeCacheObject($oCache, $sPrefix)
  71. {
  72. return $oCache->getSizeByPrefix ($sPrefix);
  73. }
  74. function _clearCache($sPrefix, $sPath)
  75. {
  76. if (!($rHandler = opendir($sPath)))
  77. return array('code' => 1, 'message' => _t('_adm_dbd_err_c_clean_failed'));
  78. $l = strlen($sPrefix);
  79. while (($sFile = readdir($rHandler)) !== false)
  80. if (0 == strncmp($sFile, $sPrefix, $l))
  81. @unlink($sPath . $sFile);
  82. closedir($rHandler);
  83. return array('code' => 0, 'message' => _t('_adm_dbd_msg_c_clean_success'));
  84. }
  85. function _getSizeCache($sPrefix, $sPath)
  86. {
  87. if (!($rHandler = opendir($sPath)))
  88. return 0;
  89. $iSize = 0;
  90. $l = strlen($sPrefix);
  91. while (($sFile = readdir($rHandler)) !== false)
  92. if (0 == strncmp($sFile, $sPrefix, $l))
  93. $iSize += filesize($sPath . $sFile);
  94. closedir($rHandler);
  95. return $iSize;
  96. }
  97. }
  98. /** @} */