BxDolStudioDashboard.php 6.9 KB

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