BxDolCacheUtilities.php 6.4 KB

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