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