BxDolCacheUtilities.php 4.8 KB

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