BxDolCacheUtilities.php 4.2 KB

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