BxDolCacheUtilities.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 TridentCore Trident 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. public function __construct ()
  13. {
  14. parent::__construct();
  15. }
  16. /**
  17. * Get singleton instance of the class
  18. */
  19. public static function getInstance()
  20. {
  21. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  22. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolCacheUtilities();
  23. return $GLOBALS['bxDolClasses'][__CLASS__];
  24. }
  25. function clear($sCache)
  26. {
  27. return $this->_action($sCache, 'clear');
  28. }
  29. function size($sCache, $isFormatted = false)
  30. {
  31. $iSize = $this->_action($sCache, 'size');
  32. return $isFormatted ? sprintf("%.2f", $iSize / 1024 / 1024) : $iSize;
  33. }
  34. protected function _action($sCache, $sMode = 'clear')
  35. {
  36. $sFuncCacheObject = $sMode == 'clear' ? '_clearObject' : '_getSizeObject';
  37. $sFuncCacheFile = $sMode == 'clear' ? '_clearFile' : '_getSizeFile';
  38. $mixedResult = false;
  39. switch ($sCache) {
  40. case 'db':
  41. if(getParam('sys_db_cache_enable') != 'on')
  42. break;
  43. $oCacheDb = BxDolDb::getInstance()->getDbCacheObject();
  44. $mixedResult = $this->$sFuncCacheObject($oCacheDb, 'db_');
  45. break;
  46. case 'template':
  47. if(getParam('sys_template_cache_enable') != 'on')
  48. break;
  49. $oCacheTemplates = BxDolTemplate::getInstance()->getTemplatesCacheObject();
  50. $mixedResult = $this->$sFuncCacheObject($oCacheTemplates, BxDolStudioTemplate::getInstance()->getCacheFilePrefix($sCache));
  51. break;
  52. case 'css':
  53. if(getParam('sys_template_cache_css_enable') != 'on')
  54. break;
  55. $mixedResult = $this->$sFuncCacheFile(BxDolStudioTemplate::getInstance()->getCacheFilePrefix($sCache), BX_DIRECTORY_PATH_CACHE_PUBLIC);
  56. break;
  57. case 'js':
  58. if(getParam('sys_template_cache_js_enable') != 'on')
  59. break;
  60. $mixedResult = $this->$sFuncCacheFile(BxDolStudioTemplate::getInstance()->getCacheFilePrefix($sCache), BX_DIRECTORY_PATH_CACHE_PUBLIC);
  61. break;
  62. }
  63. return $mixedResult;
  64. }
  65. protected function _clearObject($oCache, $sPrefix)
  66. {
  67. if (!$oCache->removeAllByPrefix ($sPrefix))
  68. return array('code' => 1, 'message' => _t('_adm_dbd_err_c_clean_failed'));
  69. else
  70. return array('code' => 0, 'message' => _t('_adm_dbd_msg_c_clean_success'));
  71. }
  72. protected function _clearFile($sPrefix, $sPath)
  73. {
  74. if (!($rHandler = opendir($sPath)))
  75. return array('code' => 1, 'message' => _t('_adm_dbd_err_c_clean_failed'));
  76. $l = strlen($sPrefix);
  77. while (($sFile = readdir($rHandler)) !== false)
  78. if (0 === strncmp($sFile, $sPrefix, $l))
  79. @unlink($sPath . $sFile);
  80. closedir($rHandler);
  81. return array('code' => 0, 'message' => _t('_adm_dbd_msg_c_clean_success'));
  82. }
  83. protected function _getSizeObject($oCache, $sPrefix)
  84. {
  85. return $oCache->getSizeByPrefix ($sPrefix);
  86. }
  87. protected function _getSizeFile($sPrefix, $sPath)
  88. {
  89. if (!($rHandler = opendir($sPath)))
  90. return 0;
  91. $iSize = 0;
  92. $l = strlen($sPrefix);
  93. while (($sFile = readdir($rHandler)) !== false)
  94. if (0 === strncmp($sFile, $sPrefix, $l))
  95. $iSize += filesize($sPath . $sFile);
  96. closedir($rHandler);
  97. return $iSize;
  98. }
  99. }
  100. /** @} */