BxDolStudioToolsAudit.php 37 KB

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