BxDolStudioDashboard.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 UnaStudio UNA Studio
  7. * @{
  8. */
  9. class BxDolStudioDashboard extends BxTemplStudioWidget
  10. {
  11. protected $aBlocks;
  12. protected $aItemsCache;
  13. protected $aItemsHTools;
  14. function __construct()
  15. {
  16. parent::__construct('dashboard');
  17. $this->oDb = new BxDolStudioDashboardQuery();
  18. $this->aBlocks = array(
  19. 'space' => 'serviceGetBlockSpace',
  20. 'htools' => 'serviceGetBlockHostTools',
  21. );
  22. $this->aItemsCache = array (
  23. array('name' => 'all'),
  24. array('name' => 'db'),
  25. array('name' => 'template'),
  26. array('name' => 'less'),
  27. array('name' => 'css'),
  28. array('name' => 'js'),
  29. array('name' => 'purifier'),
  30. array('name' => 'opcache'),
  31. array('name' => 'custom')
  32. );
  33. $this->aItemsHTools = array (
  34. 'PHP' => 'requirementsPHP',
  35. 'MySQL' => 'requirementsMySQL',
  36. 'Web Server' => 'requirementsWebServer',
  37. );
  38. //--- Check actions ---//
  39. if(($sAction = bx_get('dbd_action')) !== false) {
  40. $sAction = bx_process_input($sAction);
  41. $aResult = array('code' => 1, 'message' => _t('_adm_err_cannot_process_action'));
  42. switch($sAction) {
  43. case 'get_block':
  44. $sValue = bx_get('dbd_value');
  45. if($sValue === false)
  46. break;
  47. $sValue = bx_process_input($sValue);
  48. if(!isset($this->aBlocks[$sValue]))
  49. break;
  50. $aBlock = $this->{$this->aBlocks[$sValue]}(false);
  51. if(!empty($aBlock['content']))
  52. $aResult = array('code' => 0, 'data' => $aBlock['content']);
  53. break;
  54. case 'check_for_upgrade':
  55. $aResult = array('code' => 0, 'data' => $this->getPageCodeVersionAvailable());
  56. break;
  57. case 'perform_upgrade':
  58. $oUpgrader = bx_instance('BxDolUpgrader');
  59. if(!$oUpgrader->prepare(false)){
  60. $aResult = array('code' => 1, 'message' => $oUpgrader->getError());
  61. }
  62. else{
  63. BxDolModuleQuery::getInstance()->updateModule(array('updated' => time()), array('name' => 'system'));
  64. $aResult = array('code' => 0, 'message' => _t('_adm_dbd_msg_upgrade_started', BX_DOL_URL_STUDIO));
  65. }
  66. break;
  67. case 'clear_cache':
  68. $sValue = bx_get('dbd_value');
  69. if($sValue === false)
  70. break;
  71. $sValue = bx_process_input($sValue);
  72. $oCacheUtilities = BxDolCacheUtilities::getInstance();
  73. switch ($sValue) {
  74. case 'all':
  75. $aResult = false;
  76. foreach($this->aItemsCache as $aItem) {
  77. if($aItem['name'] == 'all')
  78. continue;
  79. $aResultClear = $oCacheUtilities->clear($aItem['name']);
  80. if($aResultClear === false)
  81. continue;
  82. $aResult = $aResultClear;
  83. if(isset($aResult['code']) && $aResult['code'] != 0)
  84. break;
  85. }
  86. break;
  87. case 'db':
  88. case 'template':
  89. case 'less':
  90. case 'css':
  91. case 'js':
  92. case 'purifier':
  93. case 'opcache':
  94. case 'custom':
  95. $aResult = $oCacheUtilities->clear($sValue);
  96. break;
  97. default:
  98. $aResult = array('code' => 1, 'message' => _t('_error occured'));
  99. }
  100. if($aResult === false)
  101. $aResult['data'] = MsgBox(_t('_adm_dbd_msg_c_all_disabled'));
  102. else if(isset($aResult['code']) && $aResult['code'] == 0) {
  103. bx_alert('system', 'clear_cache', 0, 0, array('type' => $sValue));
  104. $aResult['data'] = $this->getCacheChartData(false);
  105. }
  106. break;
  107. case 'permissions':
  108. $oAdmTools = new BxDolStudioTools();
  109. header( 'Content-type: text/html; charset=utf-8' );
  110. echo $oAdmTools->generateStyles();
  111. $oAdmTools->checkPermissions();
  112. exit;
  113. case 'server_audit':
  114. $oAudit = new BxDolStudioToolsAudit();
  115. header( 'Content-type: text/html; charset=utf-8' );
  116. echo $oAudit->generate();
  117. exit;
  118. }
  119. if(!empty($aResult['message'])) {
  120. $aResult['message'] = BxDolStudioTemplate::getInstance()->parseHtmlByName('page_action_result.html', array('content' => $aResult['message']));
  121. $aResult['message'] = BxTemplStudioFunctions::getInstance()->transBox('', $aResult['message']);
  122. }
  123. echo json_encode($aResult);
  124. exit;
  125. }
  126. }
  127. protected function getDbSize()
  128. {
  129. $iTotalSize = 0;
  130. $oDb = BxDolDb::getInstance();
  131. $aTables = $oDb->getAll('SHOW TABLE STATUS');
  132. foreach($aTables as $aTable)
  133. $iTotalSize += $aTable['Data_length'] + $aTable['Index_length'];
  134. return $iTotalSize;
  135. }
  136. protected function getFolderSize($sPath)
  137. {
  138. $iTotalSize = 0;
  139. $aFiles = scandir($sPath);
  140. $sPath = rtrim($sPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
  141. foreach($aFiles as $sFile) {
  142. if(is_dir($sPath . $sFile))
  143. if($sFile != '.' && $sFile != '..')
  144. $iTotalSize += $this->getFolderSize($sPath . $sFile);
  145. else
  146. $iTotalSize += filesize($sPath . $sFile);
  147. }
  148. return $iTotalSize;
  149. }
  150. protected function getCacheChartData($bAsString = true)
  151. {
  152. $oCacheUtilities = BxDolCacheUtilities::getInstance();
  153. $aChartData = array();
  154. foreach($this->aItemsCache as $aItem) {
  155. if($aItem['name'] == 'all')
  156. continue;
  157. $iSize = $oCacheUtilities->size($aItem['name']);
  158. if($iSize === false)
  159. continue;
  160. $aChartData[] = array(bx_js_string(_t('_adm_dbd_txt_c_' . $aItem['name']), BX_ESCAPE_STR_APOS), array('v' => $iSize, 'f' => bx_js_string(_t_format_size($iSize))));
  161. }
  162. if(empty($aChartData))
  163. return false;
  164. return $bAsString ? json_encode($aChartData) : $aChartData;
  165. }
  166. }
  167. /** @} */