BxDolCacheUtilities.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. 'opcache' => array(),
  24. 'custom' => array(),
  25. );
  26. }
  27. /**
  28. * Get singleton instance of the class
  29. */
  30. public static function getInstance()
  31. {
  32. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  33. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolCacheUtilities();
  34. return $GLOBALS['bxDolClasses'][__CLASS__];
  35. }
  36. function isEnabled($sCache)
  37. {
  38. if(!isset($this->_aCacheTypes[$sCache]))
  39. return false;
  40. if ('opcache' == $sCache)
  41. return function_exists('opcache_reset');
  42. return !isset($this->_aCacheTypes[$sCache]['option']) || getParam($this->_aCacheTypes[$sCache]['option']) == 'on';
  43. }
  44. function clear($sCache)
  45. {
  46. return $this->_action($sCache, 'clear');
  47. }
  48. function size($sCache, $isFormatted = false)
  49. {
  50. $iSize = $this->_action($sCache, 'size');
  51. return $isFormatted ? sprintf("%.2f", $iSize / 1024 / 1024) : $iSize;
  52. }
  53. protected function _action($sCache, $sMode = 'clear')
  54. {
  55. $mixedResult = false;
  56. if(!$this->isEnabled($sCache))
  57. return $mixedResult;
  58. $bClear = $sMode == 'clear';
  59. $sAction = $bClear ? '_clear' : '_getSize';
  60. $oTemplate = BxDolStudioTemplate::getInstance();
  61. switch ($sCache) {
  62. case 'db':
  63. $oCacheDb = BxDolDb::getInstance()->getDbCacheObject();
  64. $this->{$sAction . 'Object'}($oCacheDb, 'menu_');
  65. $mixedResult = $this->{$sAction . 'Object'}($oCacheDb, 'db_');
  66. break;
  67. case 'template':
  68. $oCacheTemplates = $oTemplate->getTemplatesCacheObject();
  69. $mixedResult = $this->{$sAction . 'Object'}($oCacheTemplates, $oTemplate->getCacheFilePrefix($sCache));
  70. break;
  71. case 'less':
  72. $mixedResult = $this->{$sAction . 'File'}($oTemplate->getCacheFilePrefix($sCache), BX_DIRECTORY_PATH_CACHE_PUBLIC);
  73. break;
  74. case 'css':
  75. if($bClear)
  76. $this->clear('less');
  77. $mixedResult = $this->{$sAction . 'File'}($oTemplate->getCacheFilePrefix($sCache), BX_DIRECTORY_PATH_CACHE_PUBLIC);
  78. break;
  79. case 'js':
  80. $mixedResult = $this->{$sAction . 'File'}($oTemplate->getCacheFilePrefix($sCache), BX_DIRECTORY_PATH_CACHE_PUBLIC);
  81. break;
  82. default:
  83. $sMethod = $sAction . bx_gen_method_name($sCache);
  84. if(!method_exists($this, $sMethod))
  85. $sMethod = $sAction . 'Unsupported';
  86. $mixedResult = $this->$sMethod();
  87. break;
  88. }
  89. return $mixedResult;
  90. }
  91. protected function _clearObject($oCache, $sPrefix)
  92. {
  93. if (!$oCache->removeAllByPrefix ($sPrefix))
  94. return array('code' => 1, 'message' => _t('_adm_dbd_err_c_clean_failed'));
  95. else
  96. return array('code' => 0, 'message' => _t('_adm_dbd_msg_c_clean_success'));
  97. }
  98. protected function _clearFile($sPrefix, $sPath)
  99. {
  100. if (!($rHandler = opendir($sPath)))
  101. return array('code' => 1, 'message' => _t('_adm_dbd_err_c_clean_failed'));
  102. $l = strlen($sPrefix);
  103. while (($sFile = readdir($rHandler)) !== false)
  104. if (0 === strncmp($sFile, $sPrefix, $l))
  105. @unlink($sPath . $sFile);
  106. closedir($rHandler);
  107. return array('code' => 0, 'message' => _t('_adm_dbd_msg_c_clean_success'));
  108. }
  109. protected function _clearPurifier()
  110. {
  111. HTMLPurifier_Bootstrap::registerAutoload();
  112. $oConfig = HTMLPurifier_Config::createDefault();
  113. $oConfig->set('Cache.DefinitionImpl', null);
  114. $oHtmlPurifier = new HTMLPurifier($oConfig);
  115. $oHtmlPurifier->purify('');
  116. return ['code' => 0, 'message' => _t('_adm_dbd_msg_c_clean_success')];
  117. }
  118. protected function _clearOpcache()
  119. {
  120. if(function_exists('opcache_reset'))
  121. opcache_reset();
  122. return ['code' => 0, 'message' => _t('_adm_dbd_msg_c_clean_success')];
  123. }
  124. protected function _clearCustom()
  125. {
  126. return ['code' => 0, 'message' => _t('_adm_dbd_msg_c_clean_success')];
  127. }
  128. protected function _clearUnsupported()
  129. {
  130. return ['code' => 2, 'message' => _t('_adm_dbd_err_c_clean_unsupported')];
  131. }
  132. protected function _getSizeObject($oCache, $sPrefix)
  133. {
  134. return $oCache->getSizeByPrefix ($sPrefix);
  135. }
  136. protected function _getSizeFile($sPrefix, $sPath)
  137. {
  138. if (!($rHandler = opendir($sPath)))
  139. return 0;
  140. $iSize = 0;
  141. $l = strlen($sPrefix);
  142. while (($sFile = readdir($rHandler)) !== false)
  143. if (0 === strncmp($sFile, $sPrefix, $l))
  144. $iSize += filesize($sPath . $sFile);
  145. closedir($rHandler);
  146. return $iSize;
  147. }
  148. protected function _getSizeOpcache()
  149. {
  150. if(!function_exists('opcache_get_status'))
  151. return 0;
  152. $mixedResult = opcache_get_status();
  153. if(empty($mixedResult) || !is_array($mixedResult))
  154. return 0;
  155. return isset($mixedResult['memory_usage'], $mixedResult['memory_usage']['used_memory']) ? $mixedResult['memory_usage']['used_memory'] : 0;
  156. }
  157. protected function _getSizeUnsupported()
  158. {
  159. return 0;
  160. }
  161. }
  162. /** @} */