BxDolStudioToolsAudit.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. <?php defined('BX_DOL') or defined('BX_DOL_INSTALL') 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. define('BX_DOL_AUDIT_FAIL', 'fail');
  10. define('BX_DOL_AUDIT_WARN', 'warn');
  11. define('BX_DOL_AUDIT_UNDEF', 'undef');
  12. define('BX_DOL_AUDIT_OK', 'ok');
  13. class BxDolStudioToolsAudit extends BxDol
  14. {
  15. protected $aType2ClassCSS;
  16. protected $aType2Title;
  17. protected $sMinPhpVer;
  18. protected $sLatestPhpVersion = null;
  19. protected $aPhpSettings;
  20. protected $iPhpErrorReporting;
  21. protected $sMinMysqlVer;
  22. protected $aMysqlOptimizationSettings;
  23. protected $aOptimizationSettings;
  24. protected $aRequiredApacheModules;
  25. function __construct()
  26. {
  27. parent::__construct();
  28. $this->aType2ClassCSS = array (
  29. BX_DOL_AUDIT_FAIL => 'fail',
  30. BX_DOL_AUDIT_WARN => 'warn',
  31. BX_DOL_AUDIT_UNDEF => 'undef',
  32. BX_DOL_AUDIT_OK => 'ok',
  33. );
  34. $this->aType2Title = array (
  35. BX_DOL_AUDIT_FAIL => _t('_sys_audit_title_fail'),
  36. BX_DOL_AUDIT_WARN => _t('_sys_audit_title_warn'),
  37. BX_DOL_AUDIT_UNDEF => _t('_sys_audit_title_undef'),
  38. BX_DOL_AUDIT_OK => _t('_sys_audit_title_ok'),
  39. );
  40. $this->sMinPhpVer = '8.1.0';
  41. $this->aPhpSettings = array (
  42. 'allow_url_fopen' => array('op' => '=', 'val' => true, 'type' => 'bool'),
  43. 'allow_url_include' => array('op' => '=', 'val' => false, 'type' => 'bool'),
  44. 'magic_quotes_gpc' => array('op' => '=', 'val' => false, 'type' => 'bool', 'warn' => 1),
  45. 'memory_limit' => array('op' => '>=', 'val' => 128*1024*1024, 'type' => 'bytes', 'unlimited' => -1),
  46. 'post_max_size' => array('op' => '>=', 'val' => 2*1024*1024, 'type' => 'bytes', 'warn' => 1),
  47. 'upload_max_filesize' => array('op' => '>=', 'val' => 2*1024*1024, 'type' => 'bytes', 'warn' => 1),
  48. 'register_globals' => array('op' => '=', 'val' => false, 'type' => 'bool'),
  49. 'safe_mode' => array('op' => '=', 'val' => false, 'type' => 'bool'),
  50. 'short_open_tag' => array('op' => '=', 'val' => true, 'type' => 'bool'),
  51. 'disable_functions' => array('op' => 'without', 'val' => 'shell_exec,eval,assert,phpinfo,getenv,ini_set,fsockopen,chmod,parse_ini_file,readfile,escapeshellcmd,fput,popen'),
  52. 'php module: curl' => array('op' => 'module', 'val' => 'curl'),
  53. 'php module: gd' => array('op' => 'module', 'val' => 'gd'),
  54. 'php module: mbstring' => array('op' => 'module', 'val' => 'mbstring'),
  55. 'php module: json' => array('op' => 'module', 'val' => 'json'),
  56. 'php module: fileinfo' => array('op' => 'module', 'val' => 'fileinfo'),
  57. 'php module: zip' => array('op' => 'module', 'val' => 'zip'),
  58. 'php module: openssl' => array('op' => 'module', 'val' => 'openssl'),
  59. 'php module: exif' => array('op' => 'module', 'val' => 'exif'),
  60. );
  61. $this->sMinMysqlVer = '5.5.3';
  62. $this->aMysqlOptimizationSettings = array (
  63. 'key_buffer_size' => array('op' => '>=', 'val' => 128*1024, 'type' => 'bytes'),
  64. 'max_heap_table_size' => array('op' => '>=', 'val' => 16*1024*1024, 'type' => 'bytes'),
  65. 'tmp_table_size' => array('op' => '>=', 'val' => 16*1024*1024, 'type' => 'bytes'),
  66. 'thread_cache_size ' => array('op' => '>', 'val' => 0),
  67. );
  68. $this->aOptimizationSettings = array (
  69. 'DB cache' => array('enabled' => 'sys_db_cache_enable', 'cache_engine' => 'sys_db_cache_engine', 'check_accel' => true),
  70. 'Pages cache' => array('enabled' => 'sys_page_cache_enable', 'cache_engine' => 'sys_page_cache_engine', 'check_accel' => true),
  71. 'Page blocks cache' => array('enabled' => 'sys_pb_cache_enable', 'cache_engine' => 'sys_pb_cache_engine', 'check_accel' => true),
  72. 'Templates Cache' => array('enabled' => 'sys_template_cache_enable', 'cache_engine' => 'sys_template_cache_engine', 'check_accel' => true),
  73. 'CSS files cache' => array('enabled' => 'sys_template_cache_css_enable', 'cache_engine' => '', 'check_accel' => false),
  74. 'JS files cache' => array('enabled' => 'sys_template_cache_js_enable', 'cache_engine' => '', 'check_accel' => false),
  75. 'Compression for CSS/JS cache' => array('enabled' => 'sys_template_cache_compress_enable', 'cache_engine' => '', 'check_accel' => false),
  76. );
  77. $this->aRequiredApacheModules = array (
  78. 'rewrite_module' => 'mod_rewrite',
  79. );
  80. if (isset($_GET['action'])) {
  81. $sOutput = null;
  82. switch ($_GET['action']) {
  83. case 'audit_send_test_email':
  84. $sOutput = $this->sendTestEmail();
  85. break;
  86. case 'phpinfo':
  87. ob_start();
  88. phpinfo();
  89. $sOutput = ob_get_clean();
  90. break;
  91. case 'phpinfo_popup':
  92. $sUrlSelf = bx_js_string($_SERVER['PHP_SELF'], BX_ESCAPE_STR_APOS);
  93. $sUrlSelf = bx_append_url_params($sUrlSelf, array('action' => 'phpinfo'));
  94. $sOutput = '<iframe width="640" height="480" src="' . $sUrlSelf . '"></iframe>';
  95. break;
  96. }
  97. if ($sOutput) {
  98. header('Content-type: text/html; charset=utf-8');
  99. echo $sOutput;
  100. exit;
  101. }
  102. }
  103. }
  104. public function generate()
  105. {
  106. ob_start();
  107. $this->setErrorReporting();
  108. $this->generateStyles();
  109. $this->generateJs();
  110. $this->requirements();
  111. if (!defined('BX_DOL_INSTALL'))
  112. $this->siteSetup();
  113. $this->optimization();
  114. $this->manualCheck();
  115. $this->restoreErrorReporting();
  116. return ob_get_clean();
  117. }
  118. public function generateStyles($bReturn = false)
  119. {
  120. ob_start();
  121. ?>
  122. <style>
  123. .ok {
  124. color:green;
  125. }
  126. .fail {
  127. color:red;
  128. }
  129. .warn {
  130. color:orange;
  131. }
  132. .undef {
  133. color:gray;
  134. }
  135. </style>
  136. <?php
  137. $sStyles = ob_get_clean();
  138. if($bReturn)
  139. return $sStyles;
  140. echo $sStyles;
  141. }
  142. public function generateJs()
  143. {
  144. $sUrlSelf = bx_js_string($_SERVER['PHP_SELF'], BX_ESCAPE_STR_APOS);
  145. ?>
  146. <script language="javascript">
  147. function bx_sys_adm_audit_test_email()
  148. {
  149. bx_prompt('<?php echo _t('_Email'); ?>', '<?php echo class_exists('BxDolDb') && BxDolDb::getInstance() ? BxDolDb::getInstance()->getParam('site_email') : ''; ?>', function(oPopup) {
  150. var sEmail = oPopup.getValue();
  151. if (null == sEmail || ('string' == (typeof sEmail) && !sEmail.length))
  152. return;
  153. $('#bx-sys-adm-audit-test-email').html('Sending...');
  154. $.post('<?php echo bx_append_url_params($sUrlSelf, array('action' => 'audit_send_test_email')); ?>&email=' + sEmail, function(data) {
  155. $('#bx-sys-adm-audit-test-email').html(data);
  156. });
  157. });
  158. return false;
  159. }
  160. function bx_sys_adm_audit_phpinfo()
  161. {
  162. $(window).dolPopupAjax({url: '<?php echo bx_append_url_params($sUrlSelf, array('action' => 'phpinfo_popup')); ?>'});
  163. }
  164. </script>
  165. <?php
  166. }
  167. /**
  168. * Check minimal requirements
  169. * @param $sType - BX_DOL_AUDIT_FAIL, BX_DOL_AUDIT_WARN, BX_DOL_AUDIT_UNDEF or BX_DOL_AUDIT_OK
  170. * @param $sFunc - requirementsPHP, requirementsMySQL or requirementsWebServer
  171. * @return array of FAIL, WARN, UNDEF or OK items, if array is empty then no specified items are found
  172. */
  173. public function checkRequirements($sType = BX_DOL_AUDIT_FAIL, $sFunc = 'requirementsPHP')
  174. {
  175. $this->setErrorReporting();
  176. $aRet = array ();
  177. $aMessages = array ();
  178. $this->$sFunc(false, $aMessages);
  179. foreach ($aMessages as $sName => $r) {
  180. if ($sType != $r['type'])
  181. continue;
  182. $aSetting = isset($this->aPhpSettings[$sName]) ? $this->aPhpSettings[$sName] : array();
  183. $sLabel = (!empty($aSetting['op']) && $aSetting['op'] === 'module') ? $aSetting['val'] : $sName;
  184. $sValue = (!empty($aSetting['op']) && $aSetting['op'] === 'module') ? '' : $this->format_output($r['params']['real_val'], $aSetting);
  185. $s = $sLabel;
  186. if ($sValue !== '' && $sValue !== null)
  187. $s .= ' = ' . $sValue;
  188. if ($s !== '')
  189. $s .= ' ';
  190. $aRet[] = trim($s . $this->getMsgHTML($sName, $r));
  191. }
  192. $this->restoreErrorReporting();
  193. return $aRet;
  194. }
  195. public function typeToTitle ($sType)
  196. {
  197. return $this->aType2Title[$sType];
  198. }
  199. protected function requirements()
  200. {
  201. echo '<h1>' . _t('_sys_audit_header_requirements') . '</h1>';
  202. $this->requirementsPHP();
  203. if (!defined('BX_DOL_INSTALL'))
  204. $this->requirementsMySQL();
  205. $this->requirementsWebServer();
  206. $this->requirementsOS();
  207. $this->requirementsHardware();
  208. }
  209. protected function requirementsPHP($bEcho = true, &$aOutputMessages = null)
  210. {
  211. // php.net is asked once per audit, however many sections need the latest version
  212. if($this->sLatestPhpVersion === null) {
  213. $sHttpCode = null;
  214. $s = bx_file_get_contents('http://php.net/releases/index.php?serialize=1', array(), 'get', array(), $sHttpCode, array(), 20, array(CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4));
  215. $this->sLatestPhpVersion = $s && ($a = unserialize($s)) ? $a[5]['version'] : '';
  216. }
  217. $sLatestPhpVersion = $this->sLatestPhpVersion;
  218. if (version_compare(phpversion(), "5.4", ">=") == 1)
  219. unset($this->aPhpSettings['short_open_tag']);
  220. $aMessages = array ();
  221. $sPhpVer = PHP_VERSION;
  222. if (empty($sLatestPhpVersion))
  223. $aVer = array('type' => BX_DOL_AUDIT_UNDEF, 'msg' => _t('_sys_audit_msg_value_checking_failed'), 'params' => array ('real_val' => $sPhpVer));
  224. elseif (version_compare($sPhpVer, $this->sMinPhpVer, '<'))
  225. $aVer = array('type' => BX_DOL_AUDIT_FAIL, 'msg' => _t('_sys_audit_msg_version_is_incompatible', $this->sMinPhpVer), 'params' => array ('real_val' => $sPhpVer));
  226. elseif (version_compare($sPhpVer, '5.3.0', '>=') && version_compare($sPhpVer, '5.4.0', '<'))
  227. $aVer = array('type' => BX_DOL_AUDIT_WARN, 'msg' => _t('_sys_audit_msg_version_is_outdated', $sLatestPhpVersion), 'params' => array ('real_val' => $sPhpVer));
  228. else
  229. $aVer = array('type' => BX_DOL_AUDIT_OK, 'params' => array ('real_val' => $sPhpVer));
  230. $aMessages[_t('_sys_audit_version')] = $aVer;
  231. foreach ($this->aPhpSettings as $sName => $r) {
  232. $a = $this->checkPhpSetting($sName, $r);
  233. if ($a['res'])
  234. $aMessages[$sName] = array('type' => BX_DOL_AUDIT_OK, 'params' => $a);
  235. elseif (isset($r['warn']) && $r['warn'])
  236. $aMessages[$sName] = array('type' => BX_DOL_AUDIT_WARN, 'msg' => _t('_sys_audit_msg_should_be', $r['op'], $this->format_output($r['val'], $r)), 'params' => $a);
  237. else
  238. $aMessages[$sName] = array('type' => BX_DOL_AUDIT_FAIL, 'msg' => _t('_sys_audit_msg_must_be', $r['op'], $this->format_output($r['val'], $r)), 'params' => $a);
  239. }
  240. if (null !== $aOutputMessages)
  241. $aOutputMessages = $aMessages;
  242. if ($bEcho) {
  243. $s = '';
  244. foreach ($aMessages as $sName => $r) {
  245. $aSetting = isset($this->aPhpSettings[$sName]) ? $this->aPhpSettings[$sName] : array();
  246. $sLabel = (!empty($aSetting['op']) && $aSetting['op'] === 'module') ? $aSetting['val'] : $sName;
  247. $sValue = (!empty($aSetting['op']) && $aSetting['op'] === 'module') ? '' : $this->format_output($r['params']['real_val'], $aSetting);
  248. $s .= $this->getBlock($sLabel, $sValue, $this->getMsgHTML($sName, $r));
  249. }
  250. echo $this->getSection('PHP', '', $s);
  251. }
  252. }
  253. protected function requirementsMySQL($bEcho = true, &$aOutputMessages = null)
  254. {
  255. $sMysqlVer = BxDolDb::getInstance()->getServerInfo();
  256. if (preg_match ('/^(\d+)\.(\d+)\.(\d+)/', $sMysqlVer, $m)) {
  257. $sMysqlVer = "{$m[1]}.{$m[2]}.{$m[3]}";
  258. if (version_compare($sMysqlVer, $this->sMinMysqlVer, '<'))
  259. $aMessage = array('type' => BX_DOL_AUDIT_FAIL, 'msg' => _t('_sys_audit_msg_version_is_incompatible', $this->sMinMysqlVer), 'params' => array('real_val' => $sMysqlVer));
  260. else
  261. $aMessage = array('type' => BX_DOL_AUDIT_OK, 'params' => array('real_val' => $sMysqlVer));
  262. } else {
  263. $aMessage = array('type' => BX_DOL_AUDIT_UNDEF, 'msg' => _t('_sys_audit_msg_value_checking_failed'), 'params' => array('real_val' => $sMysqlVer));
  264. }
  265. if (null !== $aOutputMessages)
  266. $aOutputMessages['mysql'] = array($aMessage);
  267. if ($bEcho) {
  268. $s = $this->getBlock(_t('_sys_audit_version'), $sMysqlVer, $this->getMsgHTML(_t('_sys_audit_version'), $aMessage));
  269. echo $this->getSection('MySQL', '', $s);
  270. }
  271. }
  272. protected function requirementsWebServer($bEcho = true, &$aOutputMessages = null)
  273. {
  274. $aMessages = array();
  275. foreach ($this->aRequiredApacheModules as $sName => $sNameCompiledName)
  276. $aMessages[$sName] = $this->checkApacheModule($sName, $sNameCompiledName);
  277. if (null !== $aOutputMessages)
  278. $aOutputMessages = $aMessages;
  279. if ($bEcho) {
  280. $s = '';
  281. foreach ($aMessages as $sName => $r) {
  282. $s .= $this->getBlock($sName, '', $this->getBlock('', '', $this->getMsgHTML($sName, $r), false));
  283. }
  284. echo $this->getSection(_t('_sys_audit_section_webserver'), $_SERVER['SERVER_SOFTWARE'], $s);
  285. }
  286. }
  287. protected function requirementsOS()
  288. {
  289. echo $this->getSection(_t('_sys_audit_section_os'), '', $this->rowsToBlocks($this->getRowsOs()));
  290. }
  291. protected function requirementsHardware()
  292. {
  293. echo $this->getSection(_t('_sys_audit_section_hardware'), '', $this->rowsToBlocks($this->getRowsHardware()));
  294. }
  295. protected function siteSetup()
  296. {
  297. echo '<h1>' . _t('_sys_audit_header_site_setup') . '</h1>';
  298. echo '<ul>' . $this->rowsToBlocks($this->getRowsSiteSetup()) . '</ul>';
  299. }
  300. protected function optimization()
  301. {
  302. echo '<h1>' . _t('_sys_audit_header_site_optimization') . '</h1>';
  303. echo $this->getSection('PHP', '', $this->rowsToBlocks($this->getRowsOptimizationPhp()));
  304. if (!defined('BX_DOL_INSTALL'))
  305. echo $this->getSection('MySQL', '', $this->rowsToBlocks($this->getRowsOptimizationMysql()));
  306. echo $this->getSection(_t('_sys_audit_section_webserver'), '', $this->rowsToBlocks($this->getRowsOptimizationWebServer()));
  307. if (!defined('BX_DOL_INSTALL')) {
  308. echo $this->getSection('UNA', '', $this->rowsToBlocks($this->getRowsOptimizationScript()));
  309. echo $this->getSection(_t('_sys_audit_section_cache_engines'), '', $this->rowsToBlocks($this->getRowsOptimizationCache()));
  310. }
  311. }
  312. protected function manualCheck()
  313. {
  314. echo '<a name="manual_audit"></a>';
  315. echo '<h1>' . _t('_sys_audit_header_manual_audit') . '</h1>';
  316. echo _t('_sys_audit_msg_manual_audit');
  317. }
  318. /**
  319. * The whole audit as data, for the Studio Dashboard (which stores it as the last audit): when it ran and eight sections,
  320. * each with a name, title, Lucide icon, headline value (a version; '' when the section is a plain check-list) and rows.
  321. * A row is name, value, type (BX_DOL_AUDIT_*) and msg, a short note that may carry HTML.
  322. */
  323. public function getReport()
  324. {
  325. $this->setErrorReporting();
  326. $aRowsMysql = $this->getRowsMysql();
  327. $aRowCron = $this->getRowCron();
  328. $aSections = array(
  329. array('name' => 'php', 'title' => 'PHP', 'icon' => 'code', 'value' => PHP_VERSION, 'rows' => array_merge($this->getRowsPhp(), $this->getRowsPhpExtensions(), $this->getRowsOptimizationPhp())),
  330. array('name' => 'database', 'title' => _t('_adm_dbd_txt_su_database'), 'icon' => 'database', 'value' => $aRowsMysql[0]['value'], 'rows' => array_merge($aRowsMysql, $this->getRowsOptimizationMysql())),
  331. array('name' => 'webserver', 'title' => _t('_sys_audit_section_webserver'), 'icon' => 'globe', 'value' => isset($_SERVER['SERVER_SOFTWARE']) ? $_SERVER['SERVER_SOFTWARE'] : '', 'rows' => array_merge($this->getRowsWebServer(), $this->getRowsOptimizationWebServer())),
  332. array('name' => 'system', 'title' => _t('_adm_dbd_txt_ht_system'), 'icon' => 'cpu', 'value' => php_uname('s'), 'rows' => array_merge($this->getRowsOs(), $this->getRowsHardware())),
  333. array('name' => 'permissions', 'title' => _t('_adm_dbd_txt_ht_permissions'), 'icon' => 'folder-lock', 'value' => '', 'rows' => $this->getRowsPermissions()),
  334. array('name' => 'security', 'title' => _t('_adm_dbd_txt_ht_security'), 'icon' => 'shield-check', 'value' => '', 'rows' => $this->getRowsSecurity()),
  335. array('name' => 'cron', 'title' => _t('_sys_audit_cron_jobs'), 'icon' => 'clock', 'value' => $aRowCron['value'], 'rows' => array($aRowCron)),
  336. array('name' => 'services', 'title' => _t('_adm_dbd_txt_ht_services'), 'icon' => 'plug', 'value' => '', 'rows' => array($this->getRowFfmpeg(), $this->getRowMail())),
  337. array('name' => 'caching', 'title' => _t('_adm_dbd_txt_ht_caching'), 'icon' => 'layers', 'value' => '', 'rows' => array_merge($this->getRowsOptimizationScript(), $this->getRowsOptimizationCache())),
  338. );
  339. $this->restoreErrorReporting();
  340. return array('time' => time(), 'sections' => $aSections);
  341. }
  342. protected function getRow($sName, $sValue, $sType, $sMsg = '')
  343. {
  344. return array('name' => $sName, 'value' => (string)$sValue, 'type' => $sType, 'msg' => (string)$sMsg);
  345. }
  346. protected function rowsToBlocks($aRows)
  347. {
  348. $s = '';
  349. foreach ($aRows as $aRow)
  350. $s .= $this->getBlock($aRow['name'], $aRow['value'], $this->getMsgHTML($aRow['name'], $aRow));
  351. return $s;
  352. }
  353. /**
  354. * PHP version and ini settings (extensions are listed separately).
  355. */
  356. protected function getRowsPhp()
  357. {
  358. return $this->getRowsFromPhpMessages(false);
  359. }
  360. protected function getRowsPhpExtensions()
  361. {
  362. return $this->getRowsFromPhpMessages(true);
  363. }
  364. protected function getRowsFromPhpMessages($bExtensions)
  365. {
  366. $aMessages = array();
  367. $this->requirementsPHP(false, $aMessages);
  368. $aRows = array();
  369. foreach ($aMessages as $sName => $r) {
  370. $aSetting = isset($this->aPhpSettings[$sName]) ? $this->aPhpSettings[$sName] : array();
  371. $bExtension = !empty($aSetting['op']) && $aSetting['op'] === 'module';
  372. if ($bExtension != $bExtensions)
  373. continue;
  374. $aRows[] = $this->getRow(
  375. $bExtension ? $aSetting['val'] : $sName,
  376. $bExtension ? '' : $this->format_output($r['params']['real_val'], $aSetting),
  377. $r['type'],
  378. isset($r['msg']) ? $r['msg'] : ''
  379. );
  380. }
  381. return $aRows;
  382. }
  383. protected function getRowsMysql()
  384. {
  385. $aMessages = array();
  386. $this->requirementsMySQL(false, $aMessages);
  387. $r = $aMessages['mysql'][0];
  388. return array($this->getRow(_t('_sys_audit_version'), $r['params']['real_val'], $r['type'], isset($r['msg']) ? $r['msg'] : ''));
  389. }
  390. protected function getRowsWebServer()
  391. {
  392. $aMessages = array();
  393. $this->requirementsWebServer(false, $aMessages);
  394. $aRows = array();
  395. foreach ($aMessages as $sName => $r)
  396. $aRows[] = $this->getRow($sName, '', $r['type'], isset($r['msg']) ? $r['msg'] : '');
  397. return $aRows;
  398. }
  399. protected function getRowsOs()
  400. {
  401. return array($this->getRow(php_uname('s'), trim(php_uname('r') . ' ' . php_uname('m')), BX_DOL_AUDIT_OK));
  402. }
  403. protected function getRowsHardware()
  404. {
  405. return array($this->getRow(_t('_sys_audit_section_hardware'), '', BX_DOL_AUDIT_UNDEF, _t('_sys_audit_msg_hardware_requirements')));
  406. }
  407. protected function getRowsSiteSetup()
  408. {
  409. $aRows = array();
  410. $oUpgrader = new BxDolUpgrader();
  411. if (!($sLatestVer = $oUpgrader->getLatestVersionNumber()))
  412. $sLatestVer = 'undefined';
  413. $sVer = bx_get_ver();
  414. if (version_compare($sVer, $sLatestVer, '>='))
  415. $aRows[] = $this->getRow(_t('_sys_audit_version_script'), $sVer, BX_DOL_AUDIT_OK);
  416. else
  417. $aRows[] = $this->getRow(_t('_sys_audit_version_script'), $sVer, BX_DOL_AUDIT_WARN, _t('_sys_audit_msg_version_is_outdated', $sLatestVer));
  418. $aRows[] = $this->getRowPermissions();
  419. $aRows[] = $this->getRowFfmpeg();
  420. $aRows[] = $this->getRowMail();
  421. $aRows[] = $this->getRowCron();
  422. return $aRows;
  423. }
  424. /**
  425. * Security: the hardening a live site should have. Every miss is a warning, since the site works without it.
  426. */
  427. protected function getRowsSecurity()
  428. {
  429. $aRows = array();
  430. $bHttps = strncasecmp(BX_DOL_URL_ROOT, 'https://', 8) === 0;
  431. $aRows[] = $this->getRow(_t('_sys_audit_https'), $bHttps ? 'https' : 'http', $bHttps ? BX_DOL_AUDIT_OK : BX_DOL_AUDIT_WARN, $bHttps ? '' : _t('_sys_audit_msg_https'));
  432. $bInstallDir = file_exists(BX_DIRECTORY_PATH_ROOT . 'install');
  433. $aRows[] = $this->getRow(_t('_sys_audit_install_dir'), _t($bInstallDir ? '_sys_audit_present' : '_sys_audit_removed'), $bInstallDir ? BX_DOL_AUDIT_WARN : BX_DOL_AUDIT_OK, $bInstallDir ? _t('_sys_audit_msg_install_dir') : '');
  434. // display_errors also accepts stdout/stderr, which filter_var would read as off
  435. foreach (array('display_errors' => '_sys_audit_display_errors', 'expose_php' => '_sys_audit_expose_php') as $sSetting => $sTitle) {
  436. $sValue = strtolower(trim((string)ini_get($sSetting)));
  437. $bOn = filter_var($sValue, FILTER_VALIDATE_BOOLEAN) || in_array($sValue, array('stdout', 'stderr'));
  438. $aRows[] = $this->getRow(_t($sTitle), _t($bOn ? '_sys_audit_on' : '_sys_audit_off'), $bOn ? BX_DOL_AUDIT_WARN : BX_DOL_AUDIT_OK, $bOn ? _t('_sys_audit_msg_' . $sSetting) : '');
  439. }
  440. // the configuration file holds the database password: nobody but the owner should be able to write it
  441. $iMode = @fileperms(BX_DIRECTORY_PATH_ROOT . 'inc/header.inc.php');
  442. if ($iMode === false)
  443. $aRows[] = $this->getRow(_t('_sys_audit_config_file'), '', BX_DOL_AUDIT_UNDEF, _t('_sys_audit_msg_value_checking_failed'));
  444. else {
  445. $bOwnerOnly = ($iMode & 0022) == 0;
  446. $aRows[] = $this->getRow(_t('_sys_audit_config_file'), sprintf('%04o', $iMode & 0777), $bOwnerOnly ? BX_DOL_AUDIT_OK : BX_DOL_AUDIT_WARN, $bOwnerOnly ? '' : _t('_sys_audit_msg_config_file'));
  447. }
  448. return $aRows;
  449. }
  450. /**
  451. * Single checks that the Studio Dashboard's Status tab shows on their own as well.
  452. */
  453. public function getRowPermissions()
  454. {
  455. $sType = BX_DOL_AUDIT_UNDEF;
  456. if (class_exists('BxDolStudioTools')) {
  457. $oTools = new BxDolStudioTools();
  458. $sType = $oTools->checkPermissions(false, false) ? BX_DOL_AUDIT_OK : BX_DOL_AUDIT_FAIL;
  459. }
  460. return $this->getRow(_t('_sys_audit_permissions'), '', $sType, $sType == BX_DOL_AUDIT_OK ? '' : _t('_sys_audit_msg_permissions'));
  461. }
  462. public function getRowFfmpeg()
  463. {
  464. $sFfmpegPath = defined('BX_SYSTEM_FFMPEG') ? BX_SYSTEM_FFMPEG : BX_DIRECTORY_PATH_PLUGINS . 'ffmpeg/ffmpeg.exe';
  465. $sFfmpegOut = (string)@shell_exec(escapeshellarg($sFfmpegPath) . ' -version 2>&1');
  466. if (preg_match('/ffmpeg version (\S+)/', $sFfmpegOut, $m))
  467. return $this->getRow('ffmpeg', $m[1], BX_DOL_AUDIT_OK);
  468. return $this->getRow('ffmpeg', '', BX_DOL_AUDIT_WARN, _t('_sys_audit_msg_ffmpeg', $sFfmpegPath));
  469. }
  470. public function getRowCron()
  471. {
  472. $iCronTime = (int)getParam('sys_cron_time');
  473. if ($iCronTime && time() - $iCronTime < 3600)
  474. return $this->getRow(_t('_sys_audit_cron_jobs'), bx_time_js($iCronTime, BX_FORMAT_DATE_TIME), BX_DOL_AUDIT_OK);
  475. return $this->getRow(_t('_sys_audit_cron_jobs'), $iCronTime ? bx_time_js($iCronTime, BX_FORMAT_DATE_TIME) : _t('_None'), BX_DOL_AUDIT_WARN, _t('_sys_audit_msg_cron_jobs'));
  476. }
  477. /**
  478. * Email delivery cannot be checked automatically: the row carries the "send a test email" link.
  479. */
  480. public function getRowMail()
  481. {
  482. return $this->getRow(_t('_sys_audit_mail_sending'), '', BX_DOL_AUDIT_UNDEF, _t('_sys_audit_msg_mail_sending'));
  483. }
  484. /**
  485. * A row per checked path (BxDolStudioTools): its current state as the value, the desired one as the note when it fails.
  486. */
  487. protected function getRowsPermissions()
  488. {
  489. if (!class_exists('BxDolStudioTools'))
  490. return array($this->getRowPermissions());
  491. $oTools = new BxDolStudioTools();
  492. $aRows = array();
  493. foreach ($oTools->getPermissionsReport() as $aPerm)
  494. $aRows[] = $this->getRow($aPerm['path'], $aPerm['current'], $aPerm['ok'] ? BX_DOL_AUDIT_OK : BX_DOL_AUDIT_FAIL, $aPerm['ok'] ? '' : _t('_adm_admtools_Desired_level') . ': ' . $aPerm['desired']);
  495. return $aRows;
  496. }
  497. public function getRowPhpAccelerator()
  498. {
  499. $sAccel = $this->getPhpAccelerator();
  500. if ($sAccel)
  501. return $this->getRow(_t('_sys_audit_php_accelerator'), $sAccel, BX_DOL_AUDIT_OK);
  502. return $this->getRow(_t('_sys_audit_php_accelerator'), '', BX_DOL_AUDIT_WARN, _t('_sys_audit_msg_php_accelerator_missing'));
  503. }
  504. protected function getRowsOptimizationPhp()
  505. {
  506. $aRows = array();
  507. $aRows[] = $this->getRowPhpAccelerator();
  508. $sSapi = php_sapi_name();
  509. if (0 === strcasecmp('cgi', $sSapi))
  510. $aRows[] = $this->getRow(_t('_sys_audit_php_setup'), $sSapi, BX_DOL_AUDIT_WARN, _t('_sys_audit_msg_php_setup_inefficient'));
  511. else
  512. $aRows[] = $this->getRow(_t('_sys_audit_php_setup'), $sSapi, BX_DOL_AUDIT_OK);
  513. return $aRows;
  514. }
  515. protected function getRowsOptimizationMysql()
  516. {
  517. $aRows = array();
  518. $oDb = BxDolDb::getInstance();
  519. foreach ($this->aMysqlOptimizationSettings as $sName => $r) {
  520. $a = $this->checkMysqlSetting($sName, $r, $oDb);
  521. if ($a['res'])
  522. $aRows[] = $this->getRow($sName, $this->format_output($a['real_val'], $r), BX_DOL_AUDIT_OK);
  523. else
  524. $aRows[] = $this->getRow($sName, $this->format_output($a['real_val'], $r), BX_DOL_AUDIT_FAIL, _t('_sys_audit_msg_must_be', $r['op'], $this->format_output($r['val'], $r)));
  525. }
  526. return $aRows;
  527. }
  528. /**
  529. * Browser caching and response compression: the Apache module check gives the status; on other servers it stays unknown.
  530. */
  531. protected function getRowsOptimizationWebServer()
  532. {
  533. $aExpires = $this->checkApacheModule('expires_module');
  534. $aDeflate = $this->checkApacheModule('deflate_module');
  535. return array(
  536. $this->getRow(_t('_sys_audit_userside_caching'), 'mod_expires', $aExpires['type'], _t('_sys_audit_msg_userside_caching', $this->getUrlForGooglePageSpeed('LeverageBrowserCaching'))),
  537. $this->getRow(_t('_sys_audit_serverside_compression'), 'mod_deflate', $aDeflate['type']),
  538. );
  539. }
  540. protected function getRowsOptimizationScript()
  541. {
  542. $aRows = array();
  543. foreach ($this->aOptimizationSettings as $sName => $a) {
  544. $sVal = ('always_on' == $a['enabled'] || getParam($a['enabled'])) ? 'On' : 'Off';
  545. if ($a['cache_engine'])
  546. $sVal .= _t('_sys_audit_msg_x_based_cache_engine', getParam($a['cache_engine']));
  547. if ('always_on' != $a['enabled'] && !getParam($a['enabled']))
  548. $aRows[] = $this->getRow($sName, $sVal, BX_DOL_AUDIT_FAIL, _t('_sys_audit_msg_optimization_fail'));
  549. elseif ($a['check_accel'] && !$this->getPhpAccelerator() && 'File' == getParam($a['cache_engine']))
  550. $aRows[] = $this->getRow($sName, $sVal, BX_DOL_AUDIT_WARN, _t('_sys_audit_msg_optimization_warn'));
  551. else
  552. $aRows[] = $this->getRow($sName, $sVal, BX_DOL_AUDIT_OK);
  553. }
  554. return $aRows;
  555. }
  556. protected function getRowsOptimizationCache()
  557. {
  558. $aRows = array();
  559. foreach (explode(',', 'File,APC,Memcache,Memcached,Redis') as $sName) {
  560. $sClass = 'BxDolCache' . $sName;
  561. $o = class_exists($sClass) ? new $sClass() : null;
  562. $bAvailable = $o && $o->isInstalled() && $o->isAvailable();
  563. $aRows[] = $this->getRow($sName, $this->format_output($bAvailable, array('type' => 'bool')), $bAvailable ? BX_DOL_AUDIT_OK : BX_DOL_AUDIT_FAIL);
  564. }
  565. return $aRows;
  566. }
  567. protected function checkPhpSetting($sName, $a)
  568. {
  569. $mixedVal = ini_get($sName);
  570. $mixedVal = $this->format_input ($mixedVal, $a);
  571. switch ($a['op']) {
  572. case 'without':
  573. $aFuncsDisabled = explode(',', $mixedVal);
  574. array_walk($aFuncsDisabled, function (&$sVal, $sKey) {
  575. $sVal = trim($sVal);
  576. });
  577. $aFuncsMustBeEnabled = explode(',', $a['val']);
  578. $a = array_intersect($aFuncsDisabled, $aFuncsMustBeEnabled);
  579. $bResult = !$a;
  580. break;
  581. case 'module':
  582. $bResult = extension_loaded($a['val']);
  583. $mixedVal = $bResult ? $a['val'] : '';
  584. break;
  585. case '>':
  586. $bResult = (isset($a['unlimited']) && $mixedVal == $a['unlimited']) ? true : ($mixedVal > $a['val']);
  587. break;
  588. case '>=':
  589. $bResult = (isset($a['unlimited']) && $mixedVal == $a['unlimited']) ? true : ($mixedVal >= $a['val']);
  590. break;
  591. case '=':
  592. default:
  593. $bResult = ($mixedVal == $a['val']);
  594. }
  595. return array ('res' => $bResult, 'real_val' => $mixedVal);
  596. }
  597. protected function checkMysqlSetting($sName, $a, $oDb)
  598. {
  599. $mixedVal = $oDb->getOption($sName);
  600. $mixedVal = $this->format_input ($mixedVal, $a);
  601. switch ($a['op']) {
  602. case '>':
  603. $bResult = ($mixedVal > $a['val']);
  604. break;
  605. case '>=':
  606. $bResult = ($mixedVal >= $a['val']);
  607. break;
  608. case 'strcasecmp':
  609. $bResult = 0 === strcasecmp($mixedVal, $a['val']);
  610. break;
  611. case '=':
  612. default:
  613. $bResult = ($mixedVal == $a['val']);
  614. }
  615. return array ('res' => $bResult, 'real_val' => $mixedVal);
  616. }
  617. protected function format_output ($mixedVal, $a)
  618. {
  619. if (isset($a['type']) && 'bytes' == $a['type'])
  620. return function_exists('_t_format_size') ? _t_format_size($mixedVal) : $mixedVal;
  621. if (isset($a['type']) && 'bool' == $a['type'])
  622. return $mixedVal ? 'On' : 'Off';
  623. else
  624. return $mixedVal;
  625. }
  626. protected function format_input ($mixedVal, $a)
  627. {
  628. if (isset($a['type']) && 'bytes' == $a['type'])
  629. return $this->format_bytes ($mixedVal);
  630. else
  631. return $mixedVal;
  632. }
  633. protected function format_bytes($val)
  634. {
  635. return return_bytes($val);
  636. }
  637. protected function checkApacheModule ($sModule, $sNameCompiledName = '')
  638. {
  639. $a = array (
  640. 'deflate_module' => 'mod_deflate',
  641. 'expires_module' => 'mod_expires',
  642. );
  643. if (!$sNameCompiledName && isset($a[$sModule]))
  644. $sNameCompiledName = $a[$sModule];
  645. if (function_exists('apache_get_modules')) {
  646. $aModules = apache_get_modules();
  647. $ret = in_array($sNameCompiledName, $aModules);
  648. } else {
  649. $sApachectlPath = trim(shell_exec("which apachectl"));
  650. if (!$sApachectlPath)
  651. $sApachectlPath = trim(shell_exec("which apache2ctl"));
  652. if (!$sApachectlPath)
  653. $sApachectlPath = trim(shell_exec("which /usr/local/apache/bin/apachectl"));
  654. if (!$sApachectlPath)
  655. $sApachectlPath = trim(shell_exec("which /usr/local/apache/bin/apache2ctl"));
  656. if (!$sApachectlPath) {
  657. return array('type' => BX_DOL_AUDIT_UNDEF);
  658. }
  659. $ret = (bool)shell_exec("$sApachectlPath -M 2>&1 | grep $sModule");
  660. if (!$ret)
  661. $ret = (bool)shell_exec("$sApachectlPath -l 2>&1 | grep $sNameCompiledName");
  662. }
  663. $aMessage = array('type' => BX_DOL_AUDIT_OK);
  664. if (!$ret)
  665. $aMessage = array('type' => BX_DOL_AUDIT_FAIL, 'msg' => _t('_sys_audit_msg_apache_module_fail', $sModule));
  666. return $aMessage;
  667. }
  668. protected function getPhpAccelerator ()
  669. {
  670. $aAccelerators = array (
  671. 'APC' => array('op' => 'module', 'val' => 'apc'),
  672. 'XCache' => array('op' => 'module', 'val' => 'xcache'),
  673. 'ZendOPcache' => array('op' => 'module', 'val' => 'Zend OPcache'),
  674. );
  675. foreach ($aAccelerators as $sName => $r) {
  676. $a = $this->checkPhpSetting($sName, $r);
  677. if ($a['res'])
  678. return $sName;
  679. }
  680. return false;
  681. }
  682. protected function getUrlForGooglePageSpeed ($sRule)
  683. {
  684. if (defined('BX_DOL_URL_ROOT')) {
  685. $sUrl = BX_DOL_URL_ROOT;
  686. }
  687. else {
  688. $sUrl = 'http://';
  689. if (
  690. (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']) ||
  691. (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && 'https' == strtolower($_SERVER['HTTP_X_FORWARDED_PROTO'])) ||
  692. getenv('UNA_HTTPS')
  693. )
  694. $sUrl = 'https://';
  695. $sUrl .= $_SERVER['HTTP_HOST'] . str_replace('/install/index.php', '', $_SERVER['PHP_SELF']);
  696. }
  697. return 'https://pagespeed.web.dev/report?url=' . $sUrl;
  698. }
  699. protected function sendTestEmail ()
  700. {
  701. $sEmailToCkeckMailSending = isset($_GET['email']) ? $_GET['email'] : getParam('site_email');
  702. $mixedRet = sendMail($sEmailToCkeckMailSending, 'Audit Test Email', 'Sample text for testing<br /><u><b>Sample text for testing</b></u>', '', array(), BX_EMAIL_SYSTEM);
  703. if (!$mixedRet) {
  704. $aMessage = array('type' => BX_DOL_AUDIT_FAIL, 'msg' => _t('_sys_audit_msg_mail_send_failed'));
  705. return $this->getBlock('', '', $this->getMsgHTML(_t('_sys_audit_mail_sending'), $aMessage), false);
  706. } else {
  707. return _t('_sys_audit_msg_mail_sent', $sEmailToCkeckMailSending);
  708. }
  709. }
  710. protected function setErrorReporting ()
  711. {
  712. if (version_compare(phpversion(), "5.3.0", ">=") == 1)
  713. $this->iPhpErrorReporting = error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING & ~E_DEPRECATED);
  714. else
  715. $this->iPhpErrorReporting = error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
  716. }
  717. protected function restoreErrorReporting ()
  718. {
  719. error_reporting($this->iPhpErrorReporting);
  720. }
  721. protected function getSection($sTitle, $sTitleAddon, $sContent)
  722. {
  723. $s = '<div class="bx-audit-section-head">';
  724. if ($sTitle !== '')
  725. $s .= '<b>' . $sTitle . '</b>';
  726. if ($sTitleAddon !== '')
  727. $s .= ($sTitle !== '' ? ': ' : '') . $sTitleAddon;
  728. $s .= '</div>';
  729. $s .= '<ul>';
  730. $s .= $sContent;
  731. $s .= '</ul>';
  732. return $s;
  733. }
  734. protected function getBlock($sName, $sValue = '', $sMsg = '', $bWrapAsListItem = true)
  735. {
  736. $s = $bWrapAsListItem ? '<li>' : '';
  737. if ($sName !== '')
  738. $s .= $sName;
  739. if ($sValue !== '')
  740. $s .= ' = ' . $sValue;
  741. if ($sMsg) {
  742. if ($sName !== '' || $sValue !== '')
  743. $s .= ' ';
  744. $s .= $sMsg;
  745. }
  746. return $s . ($bWrapAsListItem ? '</li>' : '') . "\n";
  747. }
  748. protected function getMsgHTML($sName, $a)
  749. {
  750. $s = '';
  751. $s .= '<b class="' . $this->aType2ClassCSS[$a['type']]. '">' . $this->aType2Title[$a['type']]. '</b> ';
  752. if (!empty($a['msg']))
  753. $s .= '(' . $a['msg'] . ')';
  754. return $s;
  755. }
  756. }
  757. /** @} */