BxDolStudioDashboard.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. }
  60. else{
  61. BxDolModuleQuery::getInstance()->updateModule(array('updated' => time()), array('name' => 'system'));
  62. $aResult = array('code' => 0, 'message' => _t('_adm_dbd_msg_upgrade_started', BX_DOL_URL_STUDIO));
  63. }
  64. break;
  65. case 'clear_cache':
  66. $sValue = bx_get('dbd_value');
  67. if($sValue === false)
  68. break;
  69. $sValue = bx_process_input($sValue);
  70. $oCacheUtilities = BxDolCacheUtilities::getInstance();
  71. switch ($sValue) {
  72. case 'all':
  73. $aResult = false;
  74. foreach($this->aItemsCache as $aItem) {
  75. if($aItem['name'] == 'all')
  76. continue;
  77. $aResultClear = $oCacheUtilities->clear($aItem['name']);
  78. if($aResultClear === false)
  79. continue;
  80. $aResult = $aResultClear;
  81. if(isset($aResult['code']) && $aResult['code'] != 0)
  82. break;
  83. }
  84. break;
  85. case 'db':
  86. case 'template':
  87. case 'less':
  88. case 'css':
  89. case 'js':
  90. $aResult = $oCacheUtilities->clear($sValue);
  91. break;
  92. case 'custom':
  93. $aResult = array('code' => 0, 'message' => _t('_adm_dbd_msg_c_clean_success'));
  94. break;
  95. default:
  96. $aResult = array('code' => 1, 'message' => _t('_Error Occured'));
  97. }
  98. if($aResult === false)
  99. $aResult['data'] = MsgBox(_t('_adm_dbd_msg_c_all_disabled'));
  100. else if(isset($aResult['code']) && $aResult['code'] == 0) {
  101. bx_alert('system', 'clear_cache', 0, 0, array('type' => $sValue));
  102. $aResult['data'] = $this->getCacheChartData(false);
  103. }
  104. break;
  105. case 'permissions':
  106. $oAdmTools = new BxDolStudioTools();
  107. header( 'Content-type: text/html; charset=utf-8' );
  108. echo $oAdmTools->generateStyles();
  109. $oAdmTools->checkPermissions();
  110. exit;
  111. case 'server_audit':
  112. $oAudit = new BxDolStudioToolsAudit();
  113. header( 'Content-type: text/html; charset=utf-8' );
  114. echo $oAudit->generate();
  115. exit;
  116. }
  117. if(!empty($aResult['message'])) {
  118. $aResult['message'] = BxDolStudioTemplate::getInstance()->parseHtmlByName('page_action_result.html', array('content' => $aResult['message']));
  119. $aResult['message'] = BxTemplStudioFunctions::getInstance()->transBox('', $aResult['message']);
  120. }
  121. echo json_encode($aResult);
  122. exit;
  123. }
  124. }
  125. protected function getDbSize()
  126. {
  127. $iTotalSize = 0;
  128. $oDb = BxDolDb::getInstance();
  129. $aTables = $oDb->getAll('SHOW TABLE STATUS');
  130. foreach($aTables as $aTable)
  131. $iTotalSize += $aTable['Data_length'] + $aTable['Index_length'];
  132. return $iTotalSize;
  133. }
  134. protected function getFolderSize($sPath)
  135. {
  136. $iTotalSize = 0;
  137. $aFiles = scandir($sPath);
  138. $sPath = rtrim($sPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
  139. foreach($aFiles as $sFile) {
  140. if(is_dir($sPath . $sFile))
  141. if($sFile != '.' && $sFile != '..')
  142. $iTotalSize += $this->getFolderSize($sPath . $sFile);
  143. else
  144. $iTotalSize += filesize($sPath . $sFile);
  145. }
  146. return $iTotalSize;
  147. }
  148. protected function getCacheChartData($bAsString = true)
  149. {
  150. $oCacheUtilities = BxDolCacheUtilities::getInstance();
  151. $aChartData = array();
  152. foreach($this->aItemsCache as $aItem) {
  153. if($aItem['name'] == 'all')
  154. continue;
  155. $iSize = $oCacheUtilities->size($aItem['name']);
  156. if($iSize === false)
  157. continue;
  158. $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))));
  159. }
  160. if(empty($aChartData))
  161. return false;
  162. return $bAsString ? json_encode($aChartData) : $aChartData;
  163. }
  164. }
  165. /** @} */