BxDolStudioDashboard.php 7.2 KB

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