BxDolStudioToolsAudit.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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 $aPhpSettings;
  19. protected $iPhpErrorReporting;
  20. protected $sMinMysqlVer;
  21. protected $aMysqlOptimizationSettings;
  22. protected $aOptimizationSettings;
  23. protected $aRequiredApacheModules;
  24. function __construct()
  25. {
  26. parent::__construct();
  27. $this->aType2ClassCSS = array (
  28. BX_DOL_AUDIT_FAIL => 'fail',
  29. BX_DOL_AUDIT_WARN => 'warn',
  30. BX_DOL_AUDIT_UNDEF => 'undef',
  31. BX_DOL_AUDIT_OK => 'ok',
  32. );
  33. $this->aType2Title = array (
  34. BX_DOL_AUDIT_FAIL => _t('_sys_audit_title_fail'),
  35. BX_DOL_AUDIT_WARN => _t('_sys_audit_title_warn'),
  36. BX_DOL_AUDIT_UNDEF => _t('_sys_audit_title_undef'),
  37. BX_DOL_AUDIT_OK => _t('_sys_audit_title_ok'),
  38. );
  39. $this->sMinPhpVer = '8.1.0';
  40. $this->aPhpSettings = array (
  41. 'allow_url_fopen' => array('op' => '=', 'val' => true, 'type' => 'bool'),
  42. 'allow_url_include' => array('op' => '=', 'val' => false, 'type' => 'bool'),
  43. 'magic_quotes_gpc' => array('op' => '=', 'val' => false, 'type' => 'bool', 'warn' => 1),
  44. 'memory_limit' => array('op' => '>=', 'val' => 128*1024*1024, 'type' => 'bytes', 'unlimited' => -1),
  45. 'post_max_size' => array('op' => '>=', 'val' => 2*1024*1024, 'type' => 'bytes', 'warn' => 1),
  46. 'upload_max_filesize' => array('op' => '>=', 'val' => 2*1024*1024, 'type' => 'bytes', 'warn' => 1),
  47. 'register_globals' => array('op' => '=', 'val' => false, 'type' => 'bool'),
  48. 'safe_mode' => array('op' => '=', 'val' => false, 'type' => 'bool'),
  49. 'short_open_tag' => array('op' => '=', 'val' => true, 'type' => 'bool'),
  50. 'disable_functions' => array('op' => 'without', 'val' => 'shell_exec,eval,assert,phpinfo,getenv,ini_set,fsockopen,chmod,parse_ini_file,readfile,escapeshellcmd,fput,popen'),
  51. 'php module: curl' => array('op' => 'module', 'val' => 'curl'),
  52. 'php module: gd' => array('op' => 'module', 'val' => 'gd'),
  53. 'php module: mbstring' => array('op' => 'module', 'val' => 'mbstring'),
  54. 'php module: json' => array('op' => 'module', 'val' => 'json'),
  55. 'php module: fileinfo' => array('op' => 'module', 'val' => 'fileinfo'),
  56. 'php module: zip' => array('op' => 'module', 'val' => 'zip'),
  57. 'php module: openssl' => array('op' => 'module', 'val' => 'openssl'),
  58. 'php module: exif' => array('op' => 'module', 'val' => 'exif'),
  59. );
  60. $this->sMinMysqlVer = '5.5.3';
  61. $this->aMysqlOptimizationSettings = array (
  62. 'key_buffer_size' => array('op' => '>=', 'val' => 128*1024, 'type' => 'bytes'),
  63. 'max_heap_table_size' => array('op' => '>=', 'val' => 16*1024*1024, 'type' => 'bytes'),
  64. 'tmp_table_size' => array('op' => '>=', 'val' => 16*1024*1024, 'type' => 'bytes'),
  65. 'thread_cache_size ' => array('op' => '>', 'val' => 0),
  66. );
  67. $this->aOptimizationSettings = array (
  68. 'DB cache' => array('enabled' => 'sys_db_cache_enable', 'cache_engine' => 'sys_db_cache_engine', 'check_accel' => true),
  69. 'Pages cache' => array('enabled' => 'sys_page_cache_enable', 'cache_engine' => 'sys_page_cache_engine', 'check_accel' => true),
  70. 'Page blocks cache' => array('enabled' => 'sys_pb_cache_enable', 'cache_engine' => 'sys_pb_cache_engine', 'check_accel' => true),
  71. 'Templates Cache' => array('enabled' => 'sys_template_cache_enable', 'cache_engine' => 'sys_template_cache_engine', 'check_accel' => true),
  72. 'CSS files cache' => array('enabled' => 'sys_template_cache_css_enable', 'cache_engine' => '', 'check_accel' => false),
  73. 'JS files cache' => array('enabled' => 'sys_template_cache_js_enable', 'cache_engine' => '', 'check_accel' => false),
  74. 'Compression for CSS/JS cache' => array('enabled' => 'sys_template_cache_compress_enable', 'cache_engine' => '', 'check_accel' => false),
  75. );
  76. $this->aRequiredApacheModules = array (
  77. 'rewrite_module' => 'mod_rewrite',
  78. );
  79. if (isset($_GET['action'])) {
  80. $sOutput = null;
  81. switch ($_GET['action']) {
  82. case 'audit_send_test_email':
  83. $sOutput = $this->sendTestEmail();
  84. break;
  85. case 'phpinfo':
  86. ob_start();
  87. phpinfo();
  88. $sOutput = ob_get_clean();
  89. break;
  90. case 'phpinfo_popup':
  91. $sUrlSelf = bx_js_string($_SERVER['PHP_SELF'], BX_ESCAPE_STR_APOS);
  92. $sUrlSelf = bx_append_url_params($sUrlSelf, array('action' => 'phpinfo'));
  93. $sOutput = '<iframe width="640" height="480" src="' . $sUrlSelf . '"></iframe>';
  94. break;
  95. }
  96. if ($sOutput) {
  97. header('Content-type: text/html; charset=utf-8');
  98. echo $sOutput;
  99. exit;
  100. }
  101. }
  102. }
  103. public function generate()
  104. {
  105. ob_start();
  106. $this->setErrorReporting();
  107. $this->generateStyles();
  108. $this->generateJs();
  109. $this->requirements();
  110. if (!defined('BX_DOL_INSTALL'))
  111. $this->siteSetup();
  112. $this->optimization();
  113. $this->manualCheck();
  114. $this->restoreErrorReporting();
  115. return ob_get_clean();
  116. }
  117. public function generateStyles($bReturn = false)
  118. {
  119. ob_start();
  120. ?>
  121. <style>
  122. .ok {
  123. color:green;
  124. }
  125. .fail {
  126. color:red;
  127. }
  128. .warn {
  129. color:orange;
  130. }
  131. .undef {
  132. color:gray;
  133. }
  134. </style>
  135. <?php
  136. $sStyles = ob_get_clean();
  137. if($bReturn)
  138. return $sStyles;
  139. echo $sStyles;
  140. }
  141. public function generateJs()
  142. {
  143. $sUrlSelf = bx_js_string($_SERVER['PHP_SELF'], BX_ESCAPE_STR_APOS);
  144. ?>
  145. <script language="javascript">
  146. function bx_sys_adm_audit_test_email()
  147. {
  148. bx_prompt('<?php echo _t('_Email'); ?>', '<?php echo class_exists('BxDolDb') && BxDolDb::getInstance() ? BxDolDb::getInstance()->getParam('site_email') : ''; ?>', function(oPopup) {
  149. var sEmail = oPopup.getValue();
  150. if (null == sEmail || ('string' == (typeof sEmail) && !sEmail.length))
  151. return;
  152. $('#bx-sys-adm-audit-test-email').html('Sending...');
  153. $.post('<?php echo bx_append_url_params($sUrlSelf, array('action' => 'audit_send_test_email')); ?>&email=' + sEmail, function(data) {
  154. $('#bx-sys-adm-audit-test-email').html(data);
  155. });
  156. });
  157. return false;
  158. }
  159. function bx_sys_adm_audit_phpinfo()
  160. {
  161. $(window).dolPopupAjax({url: '<?php echo bx_append_url_params($sUrlSelf, array('action' => 'phpinfo_popup')); ?>'});
  162. }
  163. </script>
  164. <?php
  165. }
  166. /**
  167. * Check minimal requirements
  168. * @param $sType - BX_DOL_AUDIT_FAIL, BX_DOL_AUDIT_WARN, BX_DOL_AUDIT_UNDEF or BX_DOL_AUDIT_OK
  169. * @param $sFunc - requirementsPHP, requirementsMySQL or requirementsWebServer
  170. * @return array of FAIL, WARN, UNDEF or OK items, if array is empty then no specified items are found
  171. */
  172. public function checkRequirements($sType = BX_DOL_AUDIT_FAIL, $sFunc = 'requirementsPHP')
  173. {
  174. $this->setErrorReporting();
  175. $aRet = array ();
  176. $aMessages = array ();
  177. $this->$sFunc(false, $aMessages);
  178. foreach ($aMessages as $sName => $r) {
  179. if ($sType != $r['type'])
  180. continue;
  181. $aSetting = isset($this->aPhpSettings[$sName]) ? $this->aPhpSettings[$sName] : array();
  182. $sLabel = (!empty($aSetting['op']) && $aSetting['op'] === 'module') ? $aSetting['val'] : $sName;
  183. $sValue = (!empty($aSetting['op']) && $aSetting['op'] === 'module') ? '' : $this->format_output($r['params']['real_val'], $aSetting);
  184. $s = $sLabel;
  185. if ($sValue !== '' && $sValue !== null)
  186. $s .= ' = ' . $sValue;
  187. if ($s !== '')
  188. $s .= ' ';
  189. $aRet[] = trim($s . $this->getMsgHTML($sName, $r));
  190. }
  191. $this->restoreErrorReporting();
  192. return $aRet;
  193. }
  194. public function typeToTitle ($sType)
  195. {
  196. return $this->aType2Title[$sType];
  197. }
  198. protected function requirements()
  199. {
  200. echo '<h1>' . _t('_sys_audit_header_requirements') . '</h1>';
  201. $this->requirementsPHP();
  202. if (!defined('BX_DOL_INSTALL'))
  203. $this->requirementsMySQL();
  204. $this->requirementsWebServer();
  205. $this->requirementsOS();
  206. $this->requirementsHardware();
  207. }
  208. protected function requirementsPHP($bEcho = true, &$aOutputMessages = null)
  209. {
  210. $sHttpCode = null;
  211. $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));
  212. $sLatestPhpVersion = $s && ($a = unserialize($s)) ? $a[5]['version'] : '';
  213. if (version_compare(phpversion(), "5.4", ">=") == 1)
  214. unset($this->aPhpSettings['short_open_tag']);
  215. $aMessages = array ();
  216. $sPhpVer = PHP_VERSION;
  217. if (empty($sLatestPhpVersion))
  218. $aVer = array('type' => BX_DOL_AUDIT_UNDEF, 'msg' => _t('_sys_audit_msg_value_checking_failed'), 'params' => array ('real_val' => $sPhpVer));
  219. elseif (version_compare($sPhpVer, $this->sMinPhpVer, '<'))
  220. $aVer = array('type' => BX_DOL_AUDIT_FAIL, 'msg' => _t('_sys_audit_msg_version_is_incompatible', $this->sMinPhpVer), 'params' => array ('real_val' => $sPhpVer));
  221. elseif (version_compare($sPhpVer, '5.3.0', '>=') && version_compare($sPhpVer, '5.4.0', '<'))
  222. $aVer = array('type' => BX_DOL_AUDIT_WARN, 'msg' => _t('_sys_audit_msg_version_is_outdated', $sLatestPhpVersion), 'params' => array ('real_val' => $sPhpVer));
  223. else
  224. $aVer = array('type' => BX_DOL_AUDIT_OK, 'params' => array ('real_val' => $sPhpVer));
  225. $aMessages[_t('_sys_audit_version')] = $aVer;
  226. foreach ($this->aPhpSettings as $sName => $r) {
  227. $a = $this->checkPhpSetting($sName, $r);
  228. if ($a['res'])
  229. $aMessages[$sName] = array('type' => BX_DOL_AUDIT_OK, 'params' => $a);
  230. elseif (isset($r['warn']) && $r['warn'])
  231. $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);
  232. else
  233. $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);
  234. }
  235. if (null !== $aOutputMessages)
  236. $aOutputMessages = $aMessages;
  237. if ($bEcho) {
  238. $s = '';
  239. foreach ($aMessages as $sName => $r) {
  240. $aSetting = isset($this->aPhpSettings[$sName]) ? $this->aPhpSettings[$sName] : array();
  241. $sLabel = (!empty($aSetting['op']) && $aSetting['op'] === 'module') ? $aSetting['val'] : $sName;
  242. $sValue = (!empty($aSetting['op']) && $aSetting['op'] === 'module') ? '' : $this->format_output($r['params']['real_val'], $aSetting);
  243. $s .= $this->getBlock($sLabel, $sValue, $this->getMsgHTML($sName, $r));
  244. }
  245. echo $this->getSection('PHP', '', $s);
  246. }
  247. }
  248. protected function requirementsMySQL($bEcho = true, &$aOutputMessages = null)
  249. {
  250. $sMysqlVer = BxDolDb::getInstance()->getServerInfo();
  251. if (preg_match ('/^(\d+)\.(\d+)\.(\d+)/', $sMysqlVer, $m)) {
  252. $sMysqlVer = "{$m[1]}.{$m[2]}.{$m[3]}";
  253. if (version_compare($sMysqlVer, $this->sMinMysqlVer, '<'))
  254. $aMessage = array('type' => BX_DOL_AUDIT_FAIL, 'msg' => _t('_sys_audit_msg_version_is_incompatible', $this->sMinMysqlVer));
  255. else
  256. $aMessage = array('type' => BX_DOL_AUDIT_OK);
  257. } else {
  258. $aMessage = array('type' => BX_DOL_AUDIT_UNDEF, 'msg' => _t('_sys_audit_msg_value_checking_failed'));
  259. }
  260. if (null !== $aOutputMessages)
  261. $aOutputMessages['mysql'] = array($aMessage);
  262. if ($bEcho) {
  263. $s = $this->getBlock(_t('_sys_audit_version'), $sMysqlVer, $this->getMsgHTML(_t('_sys_audit_version'), $aMessage));
  264. echo $this->getSection('MySQL', '', $s);
  265. }
  266. }
  267. protected function requirementsWebServer($bEcho = true, &$aOutputMessages = null)
  268. {
  269. $aMessages = array();
  270. foreach ($this->aRequiredApacheModules as $sName => $sNameCompiledName)
  271. $aMessages[$sName] = $this->checkApacheModule($sName, $sNameCompiledName);
  272. if (null !== $aOutputMessages)
  273. $aOutputMessages = $aMessages;
  274. if ($bEcho) {
  275. $s = '';
  276. foreach ($aMessages as $sName => $r) {
  277. $s .= $this->getBlock($sName, '', $this->getBlock('', '', $this->getMsgHTML($sName, $r), false));
  278. }
  279. echo $this->getSection(_t('_sys_audit_section_webserver'), $_SERVER['SERVER_SOFTWARE'], $s);
  280. }
  281. }
  282. protected function requirementsOS()
  283. {
  284. $s = $this->getBlock(php_uname());
  285. echo $this->getSection(_t('_sys_audit_section_os'), '', $s);
  286. }
  287. protected function requirementsHardware()
  288. {
  289. $s = $this->getBlock(_t('_sys_audit_msg_hardware_requirements'));
  290. echo $this->getSection(_t('_sys_audit_section_hardware'), '', $s);
  291. }
  292. protected function siteSetup()
  293. {
  294. $sFfmpegPath = defined('BX_SYSTEM_FFMPEG') ? BX_SYSTEM_FFMPEG : BX_DIRECTORY_PATH_PLUGINS . 'ffmpeg/ffmpeg.exe';
  295. $sEmailToCkeckMailSending = class_exists('BxDolDb') && BxDolDb::getInstance() ? BxDolDb::getInstance()->getParam('site_email') : '';
  296. $oUpgrader = new BxDolUpgrader();
  297. if (!($sLatestVer = $oUpgrader->getLatestVersionNumber()))
  298. $sLatestVer = 'undefined';
  299. $sVer = bx_get_ver();
  300. $aMessage = array('type' => BX_DOL_AUDIT_OK);
  301. if (!version_compare($sVer, $sLatestVer, '>='))
  302. $aMessage = array('type' => BX_DOL_AUDIT_WARN, 'msg' => _t('_sys_audit_msg_version_is_outdated', $sLatestVer));
  303. $s = $this->getBlock(_t('_sys_audit_version_script'), $sVer, $this->getMsgHTML(_t('_sys_audit_version_script'), $aMessage));
  304. $s .= $this->getBlock(_t('_sys_audit_permissions'), '', _t('_sys_audit_msg_permissions'));
  305. $s .= $this->getBlock('ffmpeg', '', _t('_sys_audit_msg_ffmpeg', shell_exec("{$sFfmpegPath} 2>&1")));
  306. $s .= $this->getBlock(_t('_sys_audit_mail_sending'), '', _t('_sys_audit_msg_mail_sending'));
  307. $s .= $this->getBlock(_t('_sys_audit_cron_jobs'), '', _t('_sys_audit_msg_cron_jobs', shell_exec("crontab -l 2>&1")));
  308. $iCronTime = getParam('sys_cron_time');
  309. $s .= $this->getBlock(_t('_sys_audit_cron_jobs_exec_time'), '', !empty($iCronTime) ? bx_time_js($iCronTime, BX_FORMAT_DATE_TIME, true) : _t('_None'));
  310. echo '<h1>' . _t('_sys_audit_header_site_setup') . '</h1>';
  311. echo "<ul>$s</ul>";
  312. }
  313. protected function optimization()
  314. {
  315. echo '<h1>' . _t('_sys_audit_header_site_optimization') . '</h1>';
  316. $this->optimizationPhp();
  317. if (!defined('BX_DOL_INSTALL'))
  318. $this->optimizationMySQL();
  319. $this->optimizationWebServer();
  320. if (!defined('BX_DOL_INSTALL'))
  321. $this->optimizationScript();
  322. if (!defined('BX_DOL_INSTALL'))
  323. $this->optimizationCache();
  324. }
  325. protected function optimizationPhp()
  326. {
  327. $s = '';
  328. $aMessage = array();
  329. $sAccel = $this->getPhpAccelerator();
  330. if (!$sAccel)
  331. $aMessage = array('type' => BX_DOL_AUDIT_WARN, 'msg' => _t('_sys_audit_msg_php_accelerator_missing'));
  332. else
  333. $aMessage = array('type' => BX_DOL_AUDIT_OK);
  334. $s .= $this->getBlock(_t('_sys_audit_php_accelerator'), $sAccel, $this->getMsgHTML(_t('_sys_audit_php_accelerator'), $aMessage));
  335. $sSapi = php_sapi_name();
  336. if (0 === strcasecmp('cgi', $sSapi))
  337. $aMessage = array('type' => BX_DOL_AUDIT_WARN, 'msg' => _t('_sys_audit_msg_php_setup_inefficient'));
  338. else
  339. $aMessage = array('type' => BX_DOL_AUDIT_OK);
  340. $s .= $this->getBlock(_t('_sys_audit_php_setup'), $sSapi, $this->getMsgHTML(_t('_sys_audit_php_setup'), $aMessage));
  341. echo $this->getSection('PHP', '', $s);
  342. }
  343. protected function optimizationMySQL()
  344. {
  345. $s = '';
  346. $oDb = BxDolDb::getInstance();
  347. foreach ($this->aMysqlOptimizationSettings as $sName => $r) {
  348. $a = $this->checkMysqlSetting($sName, $r, $oDb);
  349. $aMessage = array('type' => BX_DOL_AUDIT_OK);
  350. if (!$a['res'])
  351. $aMessage = array('type' => BX_DOL_AUDIT_FAIL, 'msg' => _t('_sys_audit_msg_must_be', $r['op'], $this->format_output($r['val'], $r)));
  352. $s .= $this->getBlock($sName, $this->format_output($a['real_val'], $r), $this->getMsgHTML($sName, $aMessage));
  353. }
  354. echo $this->getSection('MySQL', '', $s);
  355. }
  356. protected function optimizationWebServer()
  357. {
  358. $s = '';
  359. $sName = 'expires_module';
  360. $sApacheModuleChack = _t('_sys_audit_msg_optimization_apache_module', $sName, $this->getMsgHTML($sName, $this->checkApacheModule($sName)));
  361. $s .= $this->getBlock(_t('_sys_audit_userside_caching'), '', _t('_sys_audit_msg_userside_caching', $this->getUrlForGooglePageSpeed('LeverageBrowserCaching'), $sApacheModuleChack));
  362. $sName = 'deflate_module';
  363. $sApacheModuleChack = _t('_sys_audit_msg_optimization_apache_module', $sName, $this->getMsgHTML($sName, $this->checkApacheModule($sName)));
  364. $s .= $this->getBlock(_t('_sys_audit_serverside_compression'), '', _t('_sys_audit_msg_serverside_compression', $sApacheModuleChack));
  365. echo $this->getSection(_t('_sys_audit_section_webserver'), '', $s);
  366. }
  367. protected function optimizationScript()
  368. {
  369. $s = '';
  370. foreach ($this->aOptimizationSettings as $sName => $a) {
  371. $sVal = ('always_on' == $a['enabled'] || getParam($a['enabled'])) ? 'On' : 'Off';
  372. if ($a['cache_engine'])
  373. $sVal .= _t('_sys_audit_msg_x_based_cache_engine', getParam($a['cache_engine']));
  374. if ('always_on' != $a['enabled'] && !getParam($a['enabled']))
  375. $aMessage = array('type' => BX_DOL_AUDIT_FAIL, 'msg' => _t('_sys_audit_msg_optimization_fail'));
  376. elseif ($a['check_accel'] && !$this->getPhpAccelerator() && 'File' == getParam($a['cache_engine']))
  377. $aMessage = array('type' => BX_DOL_AUDIT_WARN, 'msg' => _t('_sys_audit_msg_optimization_warn'));
  378. else
  379. $aMessage = array('type' => BX_DOL_AUDIT_OK);
  380. $s .= $this->getBlock($sName, $sVal, $this->getMsgHTML($sName, $aMessage));
  381. }
  382. echo $this->getSection('UNA', '', $s);
  383. }
  384. protected function optimizationCache()
  385. {
  386. $a = explode(',', 'File,APC,Memcache,Memcached,Redis');
  387. $s = '';
  388. foreach ($a as $sName) {
  389. $sClass = 'BxDolCache' . $sName;
  390. if (class_exists($sClass))
  391. $o = new $sClass();
  392. if ($o && $o->isInstalled() && $o->isAvailable()) {
  393. $val = true;
  394. $aMessage = array('type' => BX_DOL_AUDIT_OK);
  395. } else {
  396. $val = false;
  397. $aMessage = array('type' => BX_DOL_AUDIT_FAIL);
  398. }
  399. $s .= $this->getBlock($sName, $this->format_output($val, ['type' => 'bool' ]), $this->getMsgHTML($sName, $aMessage));
  400. }
  401. echo $this->getSection('Cache engines', '', $s);
  402. }
  403. protected function manualCheck()
  404. {
  405. echo '<a name="manual_audit"></a>';
  406. echo '<h1>' . _t('_sys_audit_header_manual_audit') . '</h1>';
  407. echo _t('_sys_audit_msg_manual_audit');
  408. }
  409. protected function checkPhpSetting($sName, $a)
  410. {
  411. $mixedVal = ini_get($sName);
  412. $mixedVal = $this->format_input ($mixedVal, $a);
  413. switch ($a['op']) {
  414. case 'without':
  415. $aFuncsDisabled = explode(',', $mixedVal);
  416. array_walk($aFuncsDisabled, function (&$sVal, $sKey) {
  417. $sVal = trim($sVal);
  418. });
  419. $aFuncsMustBeEnabled = explode(',', $a['val']);
  420. $a = array_intersect($aFuncsDisabled, $aFuncsMustBeEnabled);
  421. $bResult = !$a;
  422. break;
  423. case 'module':
  424. $bResult = extension_loaded($a['val']);
  425. $mixedVal = $bResult ? $a['val'] : '';
  426. break;
  427. case '>':
  428. $bResult = (isset($a['unlimited']) && $mixedVal == $a['unlimited']) ? true : ($mixedVal > $a['val']);
  429. break;
  430. case '>=':
  431. $bResult = (isset($a['unlimited']) && $mixedVal == $a['unlimited']) ? true : ($mixedVal >= $a['val']);
  432. break;
  433. case '=':
  434. default:
  435. $bResult = ($mixedVal == $a['val']);
  436. }
  437. return array ('res' => $bResult, 'real_val' => $mixedVal);
  438. }
  439. protected function checkMysqlSetting($sName, $a, $oDb)
  440. {
  441. $mixedVal = $oDb->getOption($sName);
  442. $mixedVal = $this->format_input ($mixedVal, $a);
  443. switch ($a['op']) {
  444. case '>':
  445. $bResult = ($mixedVal > $a['val']);
  446. break;
  447. case '>=':
  448. $bResult = ($mixedVal >= $a['val']);
  449. break;
  450. case 'strcasecmp':
  451. $bResult = 0 === strcasecmp($mixedVal, $a['val']);
  452. break;
  453. case '=':
  454. default:
  455. $bResult = ($mixedVal == $a['val']);
  456. }
  457. return array ('res' => $bResult, 'real_val' => $mixedVal);
  458. }
  459. protected function format_output ($mixedVal, $a)
  460. {
  461. if (isset($a['type']) && 'bytes' == $a['type'])
  462. return function_exists('_t_format_size') ? _t_format_size($mixedVal) : $mixedVal;
  463. if (isset($a['type']) && 'bool' == $a['type'])
  464. return $mixedVal ? 'On' : 'Off';
  465. else
  466. return $mixedVal;
  467. }
  468. protected function format_input ($mixedVal, $a)
  469. {
  470. if (isset($a['type']) && 'bytes' == $a['type'])
  471. return $this->format_bytes ($mixedVal);
  472. else
  473. return $mixedVal;
  474. }
  475. protected function format_bytes($val)
  476. {
  477. return return_bytes($val);
  478. }
  479. protected function checkApacheModule ($sModule, $sNameCompiledName = '')
  480. {
  481. $a = array (
  482. 'deflate_module' => 'mod_deflate',
  483. 'expires_module' => 'mod_expires',
  484. );
  485. if (!$sNameCompiledName && isset($a[$sModule]))
  486. $sNameCompiledName = $a[$sModule];
  487. if (function_exists('apache_get_modules')) {
  488. $aModules = apache_get_modules();
  489. $ret = in_array($sNameCompiledName, $aModules);
  490. } else {
  491. $sApachectlPath = trim(shell_exec("which apachectl"));
  492. if (!$sApachectlPath)
  493. $sApachectlPath = trim(shell_exec("which apache2ctl"));
  494. if (!$sApachectlPath)
  495. $sApachectlPath = trim(shell_exec("which /usr/local/apache/bin/apachectl"));
  496. if (!$sApachectlPath)
  497. $sApachectlPath = trim(shell_exec("which /usr/local/apache/bin/apache2ctl"));
  498. if (!$sApachectlPath) {
  499. return array('type' => BX_DOL_AUDIT_UNDEF);
  500. }
  501. $ret = (bool)shell_exec("$sApachectlPath -M 2>&1 | grep $sModule");
  502. if (!$ret)
  503. $ret = (bool)shell_exec("$sApachectlPath -l 2>&1 | grep $sNameCompiledName");
  504. }
  505. $aMessage = array('type' => BX_DOL_AUDIT_OK);
  506. if (!$ret)
  507. $aMessage = array('type' => BX_DOL_AUDIT_FAIL, 'msg' => _t('_sys_audit_msg_apache_module_fail', $sModule));
  508. return $aMessage;
  509. }
  510. protected function getPhpAccelerator ()
  511. {
  512. $aAccelerators = array (
  513. 'APC' => array('op' => 'module', 'val' => 'apc'),
  514. 'XCache' => array('op' => 'module', 'val' => 'xcache'),
  515. 'ZendOPcache' => array('op' => 'module', 'val' => 'Zend OPcache'),
  516. );
  517. foreach ($aAccelerators as $sName => $r) {
  518. $a = $this->checkPhpSetting($sName, $r);
  519. if ($a['res'])
  520. return $sName;
  521. }
  522. return false;
  523. }
  524. protected function getUrlForGooglePageSpeed ($sRule)
  525. {
  526. if (defined('BX_DOL_URL_ROOT')) {
  527. $sUrl = BX_DOL_URL_ROOT;
  528. }
  529. else {
  530. $sUrl = 'http://';
  531. if (
  532. (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']) ||
  533. (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && 'https' == strtolower($_SERVER['HTTP_X_FORWARDED_PROTO'])) ||
  534. getenv('UNA_HTTPS')
  535. )
  536. $sUrl = 'https://';
  537. $sUrl .= $_SERVER['HTTP_HOST'] . str_replace('/install/index.php', '', $_SERVER['PHP_SELF']);
  538. }
  539. return 'https://pagespeed.web.dev/report?url=' . $sUrl;
  540. }
  541. protected function sendTestEmail ()
  542. {
  543. $sEmailToCkeckMailSending = isset($_GET['email']) ? $_GET['email'] : getParam('site_email');
  544. $mixedRet = sendMail($sEmailToCkeckMailSending, 'Audit Test Email', 'Sample text for testing<br /><u><b>Sample text for testing</b></u>', '', array(), BX_EMAIL_SYSTEM);
  545. if (!$mixedRet) {
  546. $aMessage = array('type' => BX_DOL_AUDIT_FAIL, 'msg' => _t('_sys_audit_msg_mail_send_failed'));
  547. return $this->getBlock('', '', $this->getMsgHTML(_t('_sys_audit_mail_sending'), $aMessage), false);
  548. } else {
  549. return _t('_sys_audit_msg_mail_sent', $sEmailToCkeckMailSending);
  550. }
  551. }
  552. protected function setErrorReporting ()
  553. {
  554. if (version_compare(phpversion(), "5.3.0", ">=") == 1)
  555. $this->iPhpErrorReporting = error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING & ~E_DEPRECATED);
  556. else
  557. $this->iPhpErrorReporting = error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
  558. }
  559. protected function restoreErrorReporting ()
  560. {
  561. error_reporting($this->iPhpErrorReporting);
  562. }
  563. protected function getSection($sTitle, $sTitleAddon, $sContent)
  564. {
  565. $s = '<div class="bx-audit-section-head">';
  566. if ($sTitle !== '')
  567. $s .= '<b>' . $sTitle . '</b>';
  568. if ($sTitleAddon !== '')
  569. $s .= ($sTitle !== '' ? ': ' : '') . $sTitleAddon;
  570. $s .= '</div>';
  571. $s .= '<ul>';
  572. $s .= $sContent;
  573. $s .= '</ul>';
  574. return $s;
  575. }
  576. protected function getBlock($sName, $sValue = '', $sMsg = '', $bWrapAsListItem = true)
  577. {
  578. $s = $bWrapAsListItem ? '<li>' : '';
  579. if ($sName !== '')
  580. $s .= $sName;
  581. if ($sValue !== '')
  582. $s .= ' = ' . $sValue;
  583. if ($sMsg) {
  584. if ($sName !== '' || $sValue !== '')
  585. $s .= ' ';
  586. $s .= $sMsg;
  587. }
  588. return $s . ($bWrapAsListItem ? '</li>' : '') . "\n";
  589. }
  590. protected function getMsgHTML($sName, $a)
  591. {
  592. $s = '';
  593. $s .= '<b class="' . $this->aType2ClassCSS[$a['type']]. '">' . $this->aType2Title[$a['type']]. '</b> ';
  594. if (isset($a['msg']))
  595. $s .= '(' . $a['msg'] . ')';
  596. return $s;
  597. }
  598. }
  599. /** @} */