diagnoser.lib.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class Diagnoser
  5. * Class that is responsible for generating diagnostic information about the system.
  6. *
  7. * @package chamilo.diagnoser
  8. *
  9. * @author Ivan Tcholakov, 2008, initial proposal and sample code.
  10. * @author spou595, 2009, implementation for Chamilo 2.x
  11. * @author Julio Montoya <gugli100@gmail.com>, 2010, port to chamilo 1.8.7, Some fixes
  12. */
  13. class Diagnoser
  14. {
  15. const STATUS_OK = 1;
  16. const STATUS_WARNING = 2;
  17. const STATUS_ERROR = 3;
  18. const STATUS_INFORMATION = 4;
  19. /**
  20. * Contructor.
  21. */
  22. public function __construct()
  23. {
  24. }
  25. /**
  26. * Show html table.
  27. */
  28. public function show_html()
  29. {
  30. $sections = [
  31. 'chamilo' => [
  32. 'lang' => 'Chamilo',
  33. 'info' => 'State of Chamilo requirements',
  34. ],
  35. 'php' => [
  36. 'lang' => 'PHP',
  37. 'info' => 'State of PHP settings on the server',
  38. ],
  39. 'database' => [
  40. 'lang' => 'Database',
  41. 'info' => 'Configuration settings of the database server. To check the database consistency after an upgrade, if you have access to the command line, you can use "php bin/doctrine.php orm:schema-tool:update --dump-sql". This will print a list of database changes that should be applied to your system in order to get the right structure. Index name changes can be ignored. Use "--force" instead of "--dump" to try and execute them in order.',
  42. ],
  43. 'webserver' => [
  44. 'lang' => get_lang('Web server'),
  45. 'info' => 'Information about your webserver\'s configuration ',
  46. ],
  47. 'paths' => [
  48. 'lang' => 'Paths',
  49. 'info' => 'The following paths are called by their constant throughout Chamilo\'s code using the api_get_path() function. Here is a list of these paths and what they would be translated to on this portal.',
  50. ],
  51. ];
  52. $currentSection = isset($_GET['section']) ? $_GET['section'] : '';
  53. if (!in_array(trim($currentSection), array_keys($sections))) {
  54. $currentSection = 'chamilo';
  55. }
  56. $html = '<div class="tabbable"><ul class="nav nav-tabs">';
  57. foreach ($sections as $section => $details) {
  58. if ($currentSection === $section) {
  59. $html .= '<li class="active">';
  60. } else {
  61. $html .= '<li>';
  62. }
  63. $params['section'] = $section;
  64. $html .= '<a href="system_status.php?section='.$section.'">'.$details['lang'].'</a></li>';
  65. }
  66. $html .= '</ul><div class="tab-pane">';
  67. $data = call_user_func([$this, 'get_'.$currentSection.'_data']);
  68. echo $html;
  69. if ($currentSection != 'paths') {
  70. echo '<br />';
  71. echo Display::return_message($sections[$currentSection]['info'], 'normal');
  72. $table = new SortableTableFromArray($data, 1, 100);
  73. $table->set_header(0, '', false);
  74. $table->set_header(1, get_lang('Section'), false);
  75. $table->set_header(2, get_lang('Setting'), false);
  76. $table->set_header(3, get_lang('Current'), false);
  77. $table->set_header(4, get_lang('Expected'), false);
  78. $table->set_header(5, get_lang('Comment'), false);
  79. $table->display();
  80. } else {
  81. echo '<br />';
  82. echo Display::return_message($sections[$currentSection]['info'], 'normal');
  83. $headers = $data['headers'];
  84. $results = $data['data'];
  85. $table = new HTML_Table(['class' => 'data_table']);
  86. $column = 0;
  87. foreach ($headers as $header) {
  88. $table->setHeaderContents(0, $column, $header);
  89. $column++;
  90. }
  91. $row = 1;
  92. foreach ($results as $index => $rowData) {
  93. $table->setCellContents(
  94. $row,
  95. 0,
  96. $rowData
  97. );
  98. $table->setCellContents(
  99. $row,
  100. 1,
  101. $index
  102. );
  103. $row++;
  104. }
  105. $table->display();
  106. }
  107. echo '</div></div>';
  108. }
  109. /**
  110. * @return array
  111. */
  112. public function get_paths_data()
  113. {
  114. global $paths;
  115. $list = $paths[api_get_path(WEB_PATH)];
  116. $list['url_append'] = api_get_configuration_value('url_append');
  117. asort($list);
  118. return [
  119. 'headers' => ['Path', 'constant'],
  120. 'data' => $list,
  121. ];
  122. }
  123. /**
  124. * Functions to get the data for the chamilo diagnostics.
  125. *
  126. * @return array of data
  127. */
  128. public function get_chamilo_data()
  129. {
  130. $array = [];
  131. $writable_folders = [
  132. api_get_path(SYS_APP_PATH).'cache',
  133. api_get_path(SYS_COURSE_PATH),
  134. api_get_path(SYS_APP_PATH).'upload/users/',
  135. ];
  136. foreach ($writable_folders as $index => $folder) {
  137. $writable = is_writable($folder);
  138. $status = $writable ? self::STATUS_OK : self::STATUS_ERROR;
  139. $array[] = $this->build_setting(
  140. $status,
  141. '[FILES]',
  142. get_lang('Is writable').': '.$folder,
  143. 'http://be2.php.net/manual/en/function.is-writable.php',
  144. $writable,
  145. 1,
  146. 'yes_no',
  147. get_lang('The directory must be writable by the web server')
  148. );
  149. }
  150. $exists = file_exists(api_get_path(SYS_CODE_PATH).'install');
  151. $status = $exists ? self::STATUS_WARNING : self::STATUS_OK;
  152. $array[] = $this->build_setting(
  153. $status,
  154. '[FILES]',
  155. get_lang('The directory exists').': /install',
  156. 'http://be2.php.net/file_exists',
  157. $exists,
  158. 0,
  159. 'yes_no',
  160. get_lang('The directory should be removed (it is no longer necessary)')
  161. );
  162. $app_version = api_get_setting('platform.chamilo_database_version');
  163. $array[] = $this->build_setting(
  164. self::STATUS_INFORMATION,
  165. '[DB]',
  166. 'chamilo_database_version',
  167. '#',
  168. $app_version,
  169. 0,
  170. null,
  171. 'Chamilo DB version'
  172. );
  173. $access_url_id = api_get_current_access_url_id();
  174. if ($access_url_id === 1) {
  175. $size = '-';
  176. global $_configuration;
  177. $message2 = '';
  178. if ($access_url_id === 1) {
  179. if (api_is_windows_os()) {
  180. $message2 .= get_lang('The space used on disk cannot be measured properly on Windows-based systems.');
  181. } else {
  182. $dir = api_get_path(SYS_PATH);
  183. $du = exec('du -sh '.$dir, $err);
  184. list($size, $none) = explode("\t", $du);
  185. unset($none);
  186. $limit = 0;
  187. if (isset($_configuration[$access_url_id])) {
  188. if (isset($_configuration[$access_url_id]['hosting_limit_disk_space'])) {
  189. $limit = $_configuration[$access_url_id]['hosting_limit_disk_space'];
  190. }
  191. }
  192. $message2 .= sprintf(get_lang('Total space used by portal %s limit is %s MB'), $size, $limit);
  193. }
  194. }
  195. $array[] = $this->build_setting(
  196. self::STATUS_OK,
  197. '[FILES]',
  198. 'hosting_limit_disk_space',
  199. '#',
  200. $size,
  201. 0,
  202. null,
  203. $message2
  204. );
  205. }
  206. $new_version = '-';
  207. $new_version_status = '';
  208. $file = api_get_path(SYS_CODE_PATH).'install/version.php';
  209. if (is_file($file)) {
  210. @include $file;
  211. }
  212. $array[] = $this->build_setting(
  213. self::STATUS_INFORMATION,
  214. '[CONFIG]',
  215. get_lang('Version from the version file'),
  216. '#',
  217. $new_version.' '.$new_version_status,
  218. '-',
  219. null,
  220. get_lang('The version from the version.php file is updated with each version but only available if the main/install/ directory is present.')
  221. );
  222. $array[] = $this->build_setting(
  223. self::STATUS_INFORMATION,
  224. '[CONFIG]',
  225. get_lang('Version from the config file'),
  226. '#',
  227. api_get_configuration_value('system_version'),
  228. $new_version,
  229. null,
  230. get_lang('The version from the main configuration file shows on the main administration page, but has to be changed manually on upgrade.')
  231. );
  232. return $array;
  233. }
  234. /**
  235. * Functions to get the data for the php diagnostics.
  236. *
  237. * @return array of data
  238. */
  239. public function get_php_data()
  240. {
  241. $array = [];
  242. // General Functions
  243. $version = phpversion();
  244. $status = $version > REQUIRED_PHP_VERSION ? self::STATUS_OK : self::STATUS_ERROR;
  245. $array[] = $this->build_setting(
  246. $status,
  247. '[PHP]',
  248. 'phpversion()',
  249. 'http://www.php.net/manual/en/function.phpversion.php',
  250. phpversion(),
  251. '>= '.REQUIRED_PHP_VERSION,
  252. null,
  253. get_lang('PHP version')
  254. );
  255. $setting = ini_get('output_buffering');
  256. $req_setting = 1;
  257. $status = $setting >= $req_setting ? self::STATUS_OK : self::STATUS_ERROR;
  258. $array[] = $this->build_setting(
  259. $status,
  260. '[INI]',
  261. 'output_buffering',
  262. 'http://www.php.net/manual/en/outcontrol.configuration.php#ini.output-buffering',
  263. $setting,
  264. $req_setting,
  265. 'on_off',
  266. get_lang('Output buffering setting is "On" for being enabled or "Off" for being disabled. This setting also may be enabled through an integer value (4096 for example) which is the output buffer size.')
  267. );
  268. $setting = ini_get('file_uploads');
  269. $req_setting = 1;
  270. $status = $setting == $req_setting ? self::STATUS_OK : self::STATUS_ERROR;
  271. $array[] = $this->build_setting(
  272. $status,
  273. '[INI]',
  274. 'file_uploads',
  275. 'http://www.php.net/manual/en/ini.core.php#ini.file-uploads',
  276. $setting,
  277. $req_setting,
  278. 'on_off',
  279. get_lang('File uploads indicate whether file uploads are authorized at all')
  280. );
  281. $setting = ini_get('magic_quotes_runtime');
  282. $req_setting = 0;
  283. $status = $setting == $req_setting ? self::STATUS_OK : self::STATUS_ERROR;
  284. $array[] = $this->build_setting(
  285. $status,
  286. '[INI]',
  287. 'magic_quotes_runtime',
  288. 'http://www.php.net/manual/en/ini.core.php#ini.magic-quotes-runtime',
  289. $setting,
  290. $req_setting,
  291. 'on_off',
  292. get_lang('This is a highly unrecommended feature which converts values returned by all functions that returned external values to slash-escaped values. This feature should *not* be enabled.')
  293. );
  294. $setting = ini_get('safe_mode');
  295. $req_setting = 0;
  296. $status = $setting == $req_setting ? self::STATUS_OK : self::STATUS_WARNING;
  297. $array[] = $this->build_setting(
  298. $status,
  299. '[INI]',
  300. 'safe_mode',
  301. 'http://www.php.net/manual/en/ini.core.php#ini.safe-mode',
  302. $setting,
  303. $req_setting,
  304. 'on_off',
  305. get_lang('Safe mode is a deprecated PHP feature which (badly) limits the access of PHP scripts to other resources. It is recommended to leave it off.')
  306. );
  307. $setting = ini_get('register_globals');
  308. $req_setting = 0;
  309. $status = $setting == $req_setting ? self::STATUS_OK : self::STATUS_ERROR;
  310. $array[] = $this->build_setting(
  311. $status,
  312. '[INI]',
  313. 'register_globals',
  314. 'http://www.php.net/manual/en/ini.core.php#ini.register-globals',
  315. $setting,
  316. $req_setting,
  317. 'on_off',
  318. get_lang('Whether to use the register globals feature or not. Using it represents potential security risks with this software.')
  319. );
  320. $setting = ini_get('short_open_tag');
  321. $req_setting = 0;
  322. $status = $setting == $req_setting ? self::STATUS_OK : self::STATUS_WARNING;
  323. $array[] = $this->build_setting(
  324. $status,
  325. '[INI]',
  326. 'short_open_tag',
  327. 'http://www.php.net/manual/en/ini.core.php#ini.short-open-tag',
  328. $setting,
  329. $req_setting,
  330. 'on_off',
  331. get_lang('Whether to allow for short open tags to be used or not. This feature should not be used.')
  332. );
  333. $setting = ini_get('magic_quotes_gpc');
  334. $req_setting = 0;
  335. $status = $setting == $req_setting ? self::STATUS_OK : self::STATUS_ERROR;
  336. $array[] = $this->build_setting(
  337. $status,
  338. '[INI]',
  339. 'magic_quotes_gpc',
  340. 'http://www.php.net/manual/en/ini.core.php#ini.magic_quotes_gpc',
  341. $setting,
  342. $req_setting,
  343. 'on_off',
  344. get_lang('Whether to automatically escape values from GET, POST and COOKIES arrays. A similar feature is provided for the required data inside this software, so using it provokes double slash-escaping of values.')
  345. );
  346. $setting = ini_get('display_errors');
  347. $req_setting = 0;
  348. $status = $setting == $req_setting ? self::STATUS_OK : self::STATUS_WARNING;
  349. $array[] = $this->build_setting(
  350. $status,
  351. '[INI]',
  352. 'display_errors',
  353. 'http://www.php.net/manual/en/ini.core.php#ini.display_errors',
  354. $setting,
  355. $req_setting,
  356. 'on_off',
  357. get_lang('Show errors on screen. Turn this on on development servers, off on production servers.')
  358. );
  359. $setting = ini_get('default_charset');
  360. if ($setting == '') {
  361. $setting = null;
  362. }
  363. $req_setting = 'UTF-8';
  364. $status = $setting == $req_setting ? self::STATUS_OK : self::STATUS_ERROR;
  365. $array[] = $this->build_setting(
  366. $status,
  367. '[INI]',
  368. 'default_charset',
  369. 'http://www.php.net/manual/en/ini.core.php#ini.default-charset',
  370. $setting,
  371. $req_setting,
  372. null,
  373. get_lang('The default character set to be sent when returning pages')
  374. );
  375. $setting = ini_get('max_execution_time');
  376. $req_setting = '300 ('.get_lang('minimum').')';
  377. $status = $setting >= 300 ? self::STATUS_OK : self::STATUS_WARNING;
  378. $array[] = $this->build_setting(
  379. $status,
  380. '[INI]',
  381. 'max_execution_time',
  382. 'http://www.php.net/manual/en/ini.core.php#ini.max-execution-time',
  383. $setting,
  384. $req_setting,
  385. null,
  386. get_lang('Maximum time a script can take to execute. If using more than that, the script is abandoned to avoid slowing down other users.')
  387. );
  388. $setting = ini_get('max_input_time');
  389. $req_setting = '300 ('.get_lang('minimum').')';
  390. $status = $setting >= 300 ? self::STATUS_OK : self::STATUS_WARNING;
  391. $array[] = $this->build_setting(
  392. $status,
  393. '[INI]',
  394. 'max_input_time',
  395. 'http://www.php.net/manual/en/ini.core.php#ini.max-input-time',
  396. $setting,
  397. $req_setting,
  398. null,
  399. get_lang('The maximum time allowed for a form to be processed by the server. If it takes longer, the process is abandonned and a blank page is returned.')
  400. );
  401. $setting = ini_get('memory_limit');
  402. $req_setting = '>= '.REQUIRED_MIN_MEMORY_LIMIT.'M';
  403. $status = self::STATUS_ERROR;
  404. if ((float) $setting >= REQUIRED_MIN_MEMORY_LIMIT) {
  405. $status = self::STATUS_OK;
  406. }
  407. $array[] = $this->build_setting(
  408. $status,
  409. '[INI]',
  410. 'memory_limit',
  411. 'http://www.php.net/manual/en/ini.core.php#ini.memory-limit',
  412. $setting,
  413. $req_setting,
  414. null,
  415. get_lang('Maximum memory limit for one single script run. If the memory needed is higher, the process will stop to avoid consuming all the server\'s available memory and thus slowing down other users.')
  416. );
  417. $setting = ini_get('post_max_size');
  418. $req_setting = '>= '.REQUIRED_MIN_POST_MAX_SIZE.'M';
  419. $status = self::STATUS_ERROR;
  420. if ((float) $setting >= REQUIRED_MIN_POST_MAX_SIZE) {
  421. $status = self::STATUS_OK;
  422. }
  423. $array[] = $this->build_setting(
  424. $status,
  425. '[INI]',
  426. 'post_max_size',
  427. 'http://www.php.net/manual/en/ini.core.php#ini.post-max-size',
  428. $setting,
  429. $req_setting,
  430. null,
  431. get_lang('This is the maximum size of uploads through forms using the POST method (i.e. classical file upload forms)')
  432. );
  433. $setting = ini_get('upload_max_filesize');
  434. $req_setting = '>= '.REQUIRED_MIN_UPLOAD_MAX_FILESIZE.'M';
  435. $status = self::STATUS_ERROR;
  436. if ((float) $setting >= REQUIRED_MIN_UPLOAD_MAX_FILESIZE) {
  437. $status = self::STATUS_OK;
  438. }
  439. $array[] = $this->build_setting(
  440. $status,
  441. '[INI]',
  442. 'upload_max_filesize',
  443. 'http://www.php.net/manual/en/ini.core.php#ini.upload_max_filesize',
  444. $setting,
  445. $req_setting,
  446. null,
  447. get_lang('Maximum volume of an uploaded file. This setting should, most of the time, be matched with the post_max_size variable.')
  448. );
  449. $setting = ini_get('upload_tmp_dir');
  450. $status = self::STATUS_OK;
  451. $array[] = $this->build_setting(
  452. $status,
  453. '[INI]',
  454. 'upload_tmp_dir',
  455. 'http://www.php.net/manual/en/ini.core.php#ini.upload_tmp_dir',
  456. $setting,
  457. '',
  458. null,
  459. get_lang('The temporary upload directory is a space on the server where files are uploaded before being filtered and treated by PHP.')
  460. );
  461. $setting = ini_get('variables_order');
  462. $req_setting = 'GPCS';
  463. $status = $setting == $req_setting ? self::STATUS_OK : self::STATUS_ERROR;
  464. $array[] = $this->build_setting(
  465. $status,
  466. '[INI]',
  467. 'variables_order',
  468. 'http://www.php.net/manual/en/ini.core.php#ini.variables-order',
  469. $setting,
  470. $req_setting,
  471. null,
  472. get_lang('The order of precedence of Environment, GET, POST, COOKIES and SESSION variables')
  473. );
  474. $setting = ini_get('session.gc_maxlifetime');
  475. $req_setting = '4320';
  476. $status = $setting == $req_setting ? self::STATUS_OK : self::STATUS_WARNING;
  477. $array[] = $this->build_setting(
  478. $status,
  479. '[SESSION]',
  480. 'session.gc_maxlifetime',
  481. 'http://www.php.net/manual/en/ini.core.php#session.gc-maxlifetime',
  482. $setting,
  483. $req_setting,
  484. null,
  485. get_lang('The session garbage collector maximum lifetime indicates which maximum time is given between two runs of the garbage collector.')
  486. );
  487. if (api_check_browscap()) {
  488. $setting = true;
  489. } else {
  490. $setting = false;
  491. }
  492. $req_setting = true;
  493. $status = $setting == $req_setting ? self::STATUS_OK : self::STATUS_WARNING;
  494. $array[] = $this->build_setting(
  495. $status,
  496. '[INI]',
  497. 'browscap',
  498. 'http://www.php.net/manual/en/misc.configuration.php#ini.browscap',
  499. $setting,
  500. $req_setting,
  501. 'on_off',
  502. get_lang('Browscap loading browscap.ini file that contains a large amount of data on the browser and its capabilities, so it can be used by the function get_browser () PHP')
  503. );
  504. // Extensions
  505. $extensions = [
  506. 'gd' => [
  507. 'link' => 'http://www.php.net/gd',
  508. 'expected' => 1,
  509. 'comment' => get_lang('This extension must be loaded.'),
  510. ],
  511. 'pdo_mysql' => [
  512. 'link' => 'http://php.net/manual/en/ref.pdo-mysql.php',
  513. 'expected' => 1,
  514. 'comment' => get_lang('This extension must be loaded.'),
  515. ],
  516. 'pcre' => [
  517. 'link' => 'http://www.php.net/pcre',
  518. 'expected' => 1,
  519. 'comment' => get_lang('This extension must be loaded.'),
  520. ],
  521. 'session' => [
  522. 'link' => 'http://www.php.net/session',
  523. 'expected' => 1,
  524. 'comment' => get_lang('This extension must be loaded.'),
  525. ],
  526. 'standard' => [
  527. 'link' => 'http://www.php.net/spl',
  528. 'expected' => 1,
  529. 'comment' => get_lang('This extension must be loaded.'),
  530. ],
  531. 'zlib' => [
  532. 'link' => 'http://www.php.net/zlib',
  533. 'expected' => 1,
  534. 'comment' => get_lang('This extension must be loaded.'),
  535. ],
  536. 'xsl' => [
  537. 'link' => 'http://be2.php.net/xsl',
  538. 'expected' => 2,
  539. 'comment' => get_lang('This extension should be loaded.'),
  540. ],
  541. 'curl' => [
  542. 'link' => 'http://www.php.net/curl',
  543. 'expected' => 2,
  544. 'comment' => get_lang('This extension should be loaded.'),
  545. ],
  546. 'Zend OPcache' => [
  547. 'link' => 'http://www.php.net/opcache',
  548. 'expected' => 2,
  549. 'comment' => get_lang('This extension should be loaded.'),
  550. ],
  551. 'apcu' => [
  552. 'link' => 'http://www.php.net/apcu',
  553. 'expected' => 2,
  554. 'comment' => get_lang('This extension should be loaded.'),
  555. ],
  556. ];
  557. foreach ($extensions as $extension => $data) {
  558. $url = $data['link'];
  559. $expected_value = $data['expected'];
  560. $comment = $data['comment'];
  561. $loaded = extension_loaded($extension);
  562. $status = $loaded ? self::STATUS_OK : self::STATUS_ERROR;
  563. $array[] = $this->build_setting(
  564. $status,
  565. '[EXTENSION]',
  566. get_lang('Extension loaded').': '.$extension,
  567. $url,
  568. $loaded,
  569. $expected_value,
  570. 'yes_no_optional',
  571. $comment
  572. );
  573. }
  574. return $array;
  575. }
  576. /**
  577. * Functions to get the data for the mysql diagnostics.
  578. *
  579. * @return array of data
  580. */
  581. public function get_database_data()
  582. {
  583. $array = [];
  584. $em = Database::getManager();
  585. $connection = $em->getConnection();
  586. $host = $connection->getHost();
  587. $db = $connection->getDatabase();
  588. $port = $connection->getPort();
  589. $driver = $connection->getDriver()->getName();
  590. $array[] = $this->build_setting(
  591. self::STATUS_INFORMATION,
  592. '[Database]',
  593. 'driver',
  594. '',
  595. $driver,
  596. null,
  597. null,
  598. get_lang('Driver')
  599. );
  600. $array[] = $this->build_setting(
  601. self::STATUS_INFORMATION,
  602. '[Database]',
  603. 'host',
  604. '',
  605. $host,
  606. null,
  607. null,
  608. get_lang('MySQL server host')
  609. );
  610. $array[] = $this->build_setting(
  611. self::STATUS_INFORMATION,
  612. '[Database]',
  613. 'port',
  614. '',
  615. $port,
  616. null,
  617. null,
  618. get_lang('Port')
  619. );
  620. $array[] = $this->build_setting(
  621. self::STATUS_INFORMATION,
  622. '[Database]',
  623. 'Database name',
  624. '',
  625. $db,
  626. null,
  627. null,
  628. get_lang('Name')
  629. );
  630. return $array;
  631. }
  632. /**
  633. * Functions to get the data for the webserver diagnostics.
  634. *
  635. * @return array of data
  636. */
  637. public function get_webserver_data()
  638. {
  639. $array = [];
  640. $array[] = $this->build_setting(
  641. self::STATUS_INFORMATION,
  642. '[SERVER]',
  643. '$_SERVER["SERVER_NAME"]',
  644. 'http://be.php.net/reserved.variables.server',
  645. $_SERVER["SERVER_NAME"],
  646. null,
  647. null,
  648. get_lang('Server name (as used in your request)')
  649. );
  650. $array[] = $this->build_setting(
  651. self::STATUS_INFORMATION,
  652. '[SERVER]',
  653. '$_SERVER["SERVER_ADDR"]',
  654. 'http://be.php.net/reserved.variables.server',
  655. $_SERVER["SERVER_ADDR"],
  656. null,
  657. null,
  658. get_lang('Server address')
  659. );
  660. $array[] = $this->build_setting(
  661. self::STATUS_INFORMATION,
  662. '[SERVER]',
  663. '$_SERVER["SERVER_PORT"]',
  664. 'http://be.php.net/reserved.variables.server',
  665. $_SERVER["SERVER_PORT"],
  666. null,
  667. null,
  668. get_lang('Server port')
  669. );
  670. $array[] = $this->build_setting(
  671. self::STATUS_INFORMATION,
  672. '[SERVER]',
  673. '$_SERVER["SERVER_SOFTWARE"]',
  674. 'http://be.php.net/reserved.variables.server',
  675. $_SERVER["SERVER_SOFTWARE"],
  676. null,
  677. null,
  678. get_lang('Software running as a web server')
  679. );
  680. $array[] = $this->build_setting(
  681. self::STATUS_INFORMATION,
  682. '[SERVER]',
  683. '$_SERVER["REMOTE_ADDR"]',
  684. 'http://be.php.net/reserved.variables.server',
  685. $_SERVER["REMOTE_ADDR"],
  686. null,
  687. null,
  688. get_lang('Remote address (your address as received by the server)')
  689. );
  690. $array[] = $this->build_setting(
  691. self::STATUS_INFORMATION,
  692. '[SERVER]',
  693. '$_SERVER["HTTP_USER_AGENT"]',
  694. 'http://be.php.net/reserved.variables.server',
  695. $_SERVER["HTTP_USER_AGENT"],
  696. null,
  697. null,
  698. get_lang('Your user agent as received by the server')
  699. );
  700. $array[] = $this->build_setting(
  701. self::STATUS_INFORMATION,
  702. '[SERVER]',
  703. '$_SERVER["SERVER_PROTOCOL"]',
  704. 'http://be.php.net/reserved.variables.server',
  705. $_SERVER["SERVER_PROTOCOL"],
  706. null,
  707. null,
  708. get_lang('Protocol used by this server')
  709. );
  710. $array[] = $this->build_setting(
  711. self::STATUS_INFORMATION,
  712. '[SERVER]',
  713. 'php_uname()',
  714. 'http://be2.php.net/php_uname',
  715. php_uname(),
  716. null,
  717. null,
  718. get_lang('Information on the system the current server is running on')
  719. );
  720. $array[] = $this->build_setting(
  721. self::STATUS_INFORMATION,
  722. '[SERVER]',
  723. '$_SERVER["HTTP_X_FORWARDED_FOR"]',
  724. 'http://be.php.net/reserved.variables.server',
  725. (!empty($_SERVER["HTTP_X_FORWARDED_FOR"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : ''),
  726. null,
  727. null,
  728. get_lang('If the server is behind a proxy or firewall (and only in those cases), it might be using the X_FORWARDED_FOR HTTP header to show the remote user IP (yours, in this case).')
  729. );
  730. return $array;
  731. }
  732. /**
  733. * Additional functions needed for fast integration.
  734. *
  735. * @param int $status Status constant defining which icon to use to illustrate the info
  736. * @param string $section The name of the section this setting is included in
  737. * @param string $title The name of the setting (usually a translated string)
  738. * @param string $url A URL to point the user to regarding this setting, or '#' otherwise
  739. * @param mixed $current_value The current value for this setting
  740. * @param mixed $expected_value The expected value for this setting
  741. * @param string $formatter If this setting is expressed in some kind of format, which format to use
  742. * @param string $comment A translated string explaining what this setting represents
  743. *
  744. * @return array A list of elements to show in an array's row
  745. */
  746. public function build_setting(
  747. $status,
  748. $section,
  749. $title,
  750. $url,
  751. $current_value,
  752. $expected_value,
  753. $formatter,
  754. $comment
  755. ) {
  756. switch ($status) {
  757. case self::STATUS_OK:
  758. $img = 'bullet_green.png';
  759. break;
  760. case self::STATUS_WARNING:
  761. $img = 'bullet_orange.png';
  762. break;
  763. case self::STATUS_ERROR:
  764. $img = 'bullet_red.png';
  765. break;
  766. case self::STATUS_INFORMATION:
  767. default:
  768. $img = 'bullet_blue.png';
  769. break;
  770. }
  771. $image = Display::return_icon($img, $status);
  772. $url = $this->get_link($title, $url);
  773. $formatted_current_value = $current_value;
  774. $formatted_expected_value = $expected_value;
  775. if ($formatter) {
  776. if (method_exists($this, 'format_'.$formatter)) {
  777. $formatted_current_value = call_user_func([$this, 'format_'.$formatter], $current_value);
  778. $formatted_expected_value = call_user_func([$this, 'format_'.$formatter], $expected_value);
  779. }
  780. }
  781. return [$image, $section, $url, $formatted_current_value, $formatted_expected_value, $comment];
  782. }
  783. /**
  784. * Create a link with a url and a title.
  785. *
  786. * @param $title
  787. * @param $url
  788. *
  789. * @return string the url
  790. */
  791. public function get_link($title, $url)
  792. {
  793. return '<a href="'.$url.'" target="about:bank">'.$title.'</a>';
  794. }
  795. /**
  796. * @param int $value
  797. *
  798. * @return string
  799. */
  800. public function format_yes_no_optional($value)
  801. {
  802. $return = '';
  803. switch ($value) {
  804. case 0:
  805. $return = get_lang('No');
  806. break;
  807. case 1:
  808. $return = get_lang('Yes');
  809. break;
  810. case 2:
  811. $return = get_lang('Optional');
  812. break;
  813. }
  814. return $return;
  815. }
  816. /**
  817. * @param $value
  818. *
  819. * @return string
  820. */
  821. public function format_yes_no($value)
  822. {
  823. return $value ? get_lang('Yes') : get_lang('No');
  824. }
  825. /**
  826. * @param int $value
  827. *
  828. * @return string
  829. */
  830. public function format_on_off($value)
  831. {
  832. $value = intval($value);
  833. if ($value > 1) {
  834. // Greater than 1 values are shown "as-is", they may be interpreted as "On" later.
  835. return $value;
  836. }
  837. // These are the values 'On' and 'Off' used in the php-ini file. Translation (get_lang()) is not needed here.
  838. return $value ? 'On' : 'Off';
  839. }
  840. }