1
0

BxDolCacheUtilities.php 4.4 KB

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