BxDolStudioToolsAudit.php 25 KB

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