diagnoser.lib.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. * @author Ivan Tcholakov, 2008, initiall proposal and sample code.
  9. * @author spou595, 2009, implementation for Chamilo 2.x
  10. * @author Julio Montoya <gugli100@gmail.com>, 2010, port to chamilo 1.8.7, Some fixes
  11. *
  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. public function show_html()
  26. {
  27. $sections = array('chamilo', 'php', 'database', 'webserver', 'paths');
  28. $currentSection = isset($_GET['section']) ? $_GET['section'] : 'chamilo';
  29. if (!in_array(trim($currentSection), $sections)) {
  30. $currentSection = 'chamilo';
  31. }
  32. $html = '<div class="tabbable"><ul class="nav nav-tabs">';
  33. foreach ($sections as $section) {
  34. if ($currentSection === $section) {
  35. $html .= '<li class="active">';
  36. } else {
  37. $html .= '<li>';
  38. }
  39. $params['section'] = $section;
  40. $html .='<a href="system_status.php?section='.$section.'">'.get_lang($section).'</a></li>';
  41. }
  42. $html .= '</ul><div class="tab-pane">';
  43. $data = call_user_func(array($this, 'get_' . $currentSection . '_data'));
  44. echo $html;
  45. if ($currentSection != 'paths') {
  46. $table = new SortableTableFromArray($data, 1, 100);
  47. $table->set_header(0, '', false);
  48. $table->set_header(1, get_lang('Section'), false);
  49. $table->set_header(2, get_lang('Setting'), false);
  50. $table->set_header(3, get_lang('Current'), false);
  51. $table->set_header(4, get_lang('Expected'), false);
  52. $table->set_header(5, get_lang('Comment'), false);
  53. $table->display();
  54. } else {
  55. $headers = $data['headers'];
  56. $results = $data['data'];
  57. $table = new HTML_Table(array('class' => 'data_table'));
  58. $column = 0;
  59. foreach ($headers as $header) {
  60. $table->setHeaderContents(0, $column, $header);
  61. $column++;
  62. }
  63. $row = 1;
  64. foreach ($results as $index => $rowData) {
  65. $table->setCellContents(
  66. $row,
  67. 0,
  68. $rowData
  69. );
  70. $table->setCellContents(
  71. $row,
  72. 1,
  73. $index
  74. );
  75. $row++;
  76. }
  77. echo $table->display();
  78. }
  79. echo '</div></div>';
  80. }
  81. /**
  82. * @return array
  83. */
  84. public function get_paths_data()
  85. {
  86. global $paths;
  87. $list = $paths[api_get_path(WEB_PATH)];
  88. $list['url_append'] = api_get_configuration_value('url_append');
  89. asort($list);
  90. return [
  91. 'headers' => ['Path', 'constant'],
  92. 'data' => $list
  93. ];
  94. }
  95. /**
  96. * Functions to get the data for the chamilo diagnostics
  97. * @return array of data
  98. */
  99. public function get_chamilo_data()
  100. {
  101. $array = array();
  102. $writable_folders = array(
  103. api_get_path(SYS_APP_PATH) .'cache',
  104. api_get_path(SYS_COURSE_PATH),
  105. api_get_path(SYS_APP_PATH) .'home',
  106. api_get_path(SYS_APP_PATH) .'upload/users/',
  107. api_get_path(SYS_PATH) .'main/default_course_document/images/',
  108. );
  109. foreach ($writable_folders as $index => $folder) {
  110. $writable = is_writable($folder);
  111. $status = $writable ? self :: STATUS_OK : self :: STATUS_ERROR;
  112. $array[] = $this->build_setting(
  113. $status,
  114. '[FILES]',
  115. get_lang('IsWritable').': '.$folder,
  116. 'http://be2.php.net/manual/en/function.is-writable.php',
  117. $writable,
  118. 1,
  119. 'yes_no',
  120. get_lang('DirectoryMustBeWritable')
  121. );
  122. }
  123. $exists = file_exists(api_get_path(SYS_CODE_PATH).'install');
  124. $status = $exists ? self :: STATUS_WARNING : self :: STATUS_OK;
  125. $array[] = $this->build_setting($status, '[FILES]', get_lang('DirectoryExists') . ': /install', 'http://be2.php.net/file_exists', $exists, 0, 'yes_no', get_lang('DirectoryShouldBeRemoved'));
  126. $app_version = api_get_setting('chamilo_database_version');
  127. $array[] = $this->build_setting(self :: STATUS_INFORMATION, '[DB]', 'chamilo_database_version', '#', $app_version, 0, null, 'Chamilo DB version');
  128. return $array;
  129. }
  130. /**
  131. * Functions to get the data for the php diagnostics
  132. * @return array of data
  133. */
  134. public function get_php_data()
  135. {
  136. $array = array();
  137. // General Functions
  138. $version = phpversion();
  139. $status = $version > REQUIRED_PHP_VERSION ? self :: STATUS_OK : self :: STATUS_ERROR;
  140. $array[] = $this->build_setting($status, '[PHP]', 'phpversion()', 'http://www.php.net/manual/en/function.phpversion.php', phpversion(), '>= '.REQUIRED_PHP_VERSION, null, get_lang('PHPVersionInfo'));
  141. $setting = ini_get('output_buffering');
  142. $req_setting = 1;
  143. $status = $setting >= $req_setting ? self :: STATUS_OK : self :: STATUS_ERROR;
  144. $array[] = $this->build_setting($status, '[INI]', 'output_buffering', 'http://www.php.net/manual/en/outcontrol.configuration.php#ini.output-buffering', $setting, $req_setting, 'on_off', get_lang('OutputBufferingInfo'));
  145. $setting = ini_get('file_uploads');
  146. $req_setting = 1;
  147. $status = $setting == $req_setting ? self :: STATUS_OK : self :: STATUS_ERROR;
  148. $array[] = $this->build_setting($status, '[INI]', 'file_uploads', 'http://www.php.net/manual/en/ini.core.php#ini.file-uploads', $setting, $req_setting, 'on_off', get_lang('FileUploadsInfo'));
  149. $setting = ini_get('magic_quotes_runtime');
  150. $req_setting = 0;
  151. $status = $setting == $req_setting ? self :: STATUS_OK : self :: STATUS_ERROR;
  152. $array[] = $this->build_setting($status, '[INI]', 'magic_quotes_runtime', 'http://www.php.net/manual/en/ini.core.php#ini.magic-quotes-runtime', $setting, $req_setting, 'on_off', get_lang('MagicQuotesRuntimeInfo'));
  153. $setting = ini_get('safe_mode');
  154. $req_setting = 0;
  155. $status = $setting == $req_setting ? self :: STATUS_OK : self :: STATUS_WARNING;
  156. $array[] = $this->build_setting($status, '[INI]', 'safe_mode', 'http://www.php.net/manual/en/ini.core.php#ini.safe-mode', $setting, $req_setting, 'on_off', get_lang('SafeModeInfo'));
  157. $setting = ini_get('register_globals');
  158. $req_setting = 0;
  159. $status = $setting == $req_setting ? self :: STATUS_OK : self :: STATUS_ERROR;
  160. $array[] = $this->build_setting($status, '[INI]', 'register_globals', 'http://www.php.net/manual/en/ini.core.php#ini.register-globals', $setting, $req_setting, 'on_off', get_lang('RegisterGlobalsInfo'));
  161. $setting = ini_get('short_open_tag');
  162. $req_setting = 0;
  163. $status = $setting == $req_setting ? self :: STATUS_OK : self :: STATUS_WARNING;
  164. $array[] = $this->build_setting($status, '[INI]', 'short_open_tag', 'http://www.php.net/manual/en/ini.core.php#ini.short-open-tag', $setting, $req_setting, 'on_off', get_lang('ShortOpenTagInfo'));
  165. $setting = ini_get('magic_quotes_gpc');
  166. $req_setting = 0;
  167. $status = $setting == $req_setting ? self :: STATUS_OK : self :: STATUS_ERROR;
  168. $array[] = $this->build_setting($status, '[INI]', 'magic_quotes_gpc', 'http://www.php.net/manual/en/ini.core.php#ini.magic_quotes_gpc', $setting, $req_setting, 'on_off', get_lang('MagicQuotesGpcInfo'));
  169. $setting = ini_get('display_errors');
  170. $req_setting = 0;
  171. $status = $setting == $req_setting ? self :: STATUS_OK : self :: STATUS_WARNING;
  172. $array[] = $this->build_setting($status, '[INI]', 'display_errors', 'http://www.php.net/manual/en/ini.core.php#ini.display_errors', $setting, $req_setting, 'on_off', get_lang('DisplayErrorsInfo'));
  173. $setting = ini_get('default_charset');
  174. if ($setting == '')
  175. $setting = null;
  176. $req_setting = null;
  177. $status = $setting == $req_setting ? self :: STATUS_OK : self :: STATUS_ERROR;
  178. $array[] = $this->build_setting($status, '[INI]', 'default_charset', 'http://www.php.net/manual/en/ini.core.php#ini.default-charset', $setting, $req_setting, null, get_lang('DefaultCharsetInfo'));
  179. $setting = ini_get('max_execution_time');
  180. $req_setting = '300 (' . get_lang('Minimum') . ')';
  181. $status = $setting >= 300 ? self :: STATUS_OK : self :: STATUS_WARNING;
  182. $array[] = $this->build_setting($status, '[INI]', 'max_execution_time', 'http://www.php.net/manual/en/ini.core.php#ini.max-execution-time', $setting, $req_setting, null, get_lang('MaxExecutionTimeInfo'));
  183. $setting = ini_get('max_input_time');
  184. $req_setting = '300 (' . get_lang('Minimum') . ')';
  185. $status = $setting >= 300 ? self :: STATUS_OK : self :: STATUS_WARNING;
  186. $array[] = $this->build_setting($status, '[INI]', 'max_input_time', 'http://www.php.net/manual/en/ini.core.php#ini.max-input-time', $setting, $req_setting, null, get_lang('MaxInputTimeInfo'));
  187. $setting = ini_get('memory_limit');
  188. $req_setting = '>= '.REQUIRED_MIN_MEMORY_LIMIT.'M';
  189. $status = self :: STATUS_ERROR;
  190. if ((float)$setting >= REQUIRED_MIN_MEMORY_LIMIT)
  191. $status = self :: STATUS_OK;
  192. $array[] = $this->build_setting($status, '[INI]', 'memory_limit', 'http://www.php.net/manual/en/ini.core.php#ini.memory-limit', $setting, $req_setting, null, get_lang('MemoryLimitInfo'));
  193. $setting = ini_get('post_max_size');
  194. $req_setting = '>= '.REQUIRED_MIN_POST_MAX_SIZE.'M';
  195. $status = self :: STATUS_ERROR;
  196. if ((float)$setting >= REQUIRED_MIN_POST_MAX_SIZE)
  197. $status = self :: STATUS_OK;
  198. $array[] = $this->build_setting($status, '[INI]', 'post_max_size', 'http://www.php.net/manual/en/ini.core.php#ini.post-max-size', $setting, $req_setting, null, get_lang('PostMaxSizeInfo'));
  199. $setting = ini_get('upload_max_filesize');
  200. $req_setting = '>= '.REQUIRED_MIN_UPLOAD_MAX_FILESIZE.'M';
  201. $status = self :: STATUS_ERROR;
  202. if ((float)$setting >= REQUIRED_MIN_UPLOAD_MAX_FILESIZE)
  203. $status = self :: STATUS_OK;
  204. $array[] = $this->build_setting($status, '[INI]', 'upload_max_filesize', 'http://www.php.net/manual/en/ini.core.php#ini.upload_max_filesize', $setting, $req_setting, null, get_lang('UploadMaxFilesizeInfo'));
  205. $setting = ini_get('variables_order');
  206. $req_setting = 'GPCS';
  207. $status = $setting == $req_setting ? self :: STATUS_OK : self :: STATUS_ERROR;
  208. $array[] = $this->build_setting($status, '[INI]', 'variables_order', 'http://www.php.net/manual/en/ini.core.php#ini.variables-order', $setting, $req_setting, null, get_lang('VariablesOrderInfo'));
  209. $setting = ini_get('session.gc_maxlifetime');
  210. $req_setting = '4320';
  211. $status = $setting == $req_setting ? self :: STATUS_OK : self :: STATUS_WARNING;
  212. $array[] = $this->build_setting($status, '[SESSION]', 'session.gc_maxlifetime', 'http://www.php.net/manual/en/ini.core.php#session.gc-maxlifetime', $setting, $req_setting, null, get_lang('SessionGCMaxLifetimeInfo'));
  213. if (api_check_browscap()){$setting = true;}else{$setting=false;}
  214. $req_setting = true;
  215. $status = $setting == $req_setting ? self :: STATUS_OK : self :: STATUS_WARNING;
  216. $array[] = $this->build_setting($status, '[INI]', 'browscap', 'http://www.php.net/manual/en/misc.configuration.php#ini.browscap', $setting, $req_setting, 'on_off', get_lang('BrowscapInfo'));
  217. // Extensions
  218. $extensions = array(
  219. 'gd' => array(
  220. 'link' => 'http://www.php.net/gd',
  221. 'expected' => 1,
  222. 'comment' => get_lang('ExtensionMustBeLoaded'),
  223. ),
  224. 'pdo_mysql' => array(
  225. 'link' => 'http://php.net/manual/en/ref.pdo-mysql.php',
  226. 'expected' => 1,
  227. 'comment' => get_lang('ExtensionMustBeLoaded'),
  228. ),
  229. 'pcre' => array(
  230. 'link' => 'http://www.php.net/pcre',
  231. 'expected' => 1,
  232. 'comment' => get_lang('ExtensionMustBeLoaded'),
  233. ),
  234. 'session' => array(
  235. 'link' => 'http://www.php.net/session',
  236. 'expected' => 1,
  237. 'comment' => get_lang('ExtensionMustBeLoaded'),
  238. ),
  239. 'standard' => array(
  240. 'link' => 'http://www.php.net/spl',
  241. 'expected' => 1,
  242. 'comment' => get_lang('ExtensionMustBeLoaded'),
  243. ),
  244. 'zlib' => array(
  245. 'link' => 'http://www.php.net/zlib',
  246. 'expected' => 1,
  247. 'comment' => get_lang('ExtensionMustBeLoaded'),
  248. ),
  249. 'xsl' => array(
  250. 'link' => 'http://be2.php.net/xsl',
  251. 'expected' => 2,
  252. 'comment' => get_lang('ExtensionShouldBeLoaded'),
  253. ),
  254. 'curl' => array(
  255. 'link' => 'http://www.php.net/curl',
  256. 'expected' => 2,
  257. 'comment' => get_lang('ExtensionShouldBeLoaded'),
  258. ),
  259. );
  260. foreach ($extensions as $extension => $data) {
  261. $url = $data['link'];
  262. $expected_value = $data['expected'];
  263. $comment = $data['comment'];
  264. $loaded = extension_loaded($extension);
  265. $status = $loaded ? self :: STATUS_OK : self :: STATUS_ERROR;
  266. $array[] = $this->build_setting($status, '[EXTENSION]', get_lang('LoadedExtension') . ': ' . $extension, $url, $loaded, $expected_value, 'yes_no_optional', $comment);
  267. }
  268. return $array;
  269. }
  270. /**
  271. * Functions to get the data for the mysql diagnostics
  272. * @return array of data
  273. */
  274. public function get_database_data()
  275. {
  276. $array = array();
  277. $em = Database::getManager();
  278. $connection = $em->getConnection();
  279. $host = $connection->getHost();
  280. $db = $connection->getDatabase();
  281. $port = $connection->getPort();
  282. $driver = $connection->getDriver()->getName();
  283. $array[] = $this->build_setting(
  284. self :: STATUS_INFORMATION,
  285. '[Database]',
  286. 'driver',
  287. '',
  288. $driver, null, null, get_lang('Driver')
  289. );
  290. $array[] = $this->build_setting(
  291. self :: STATUS_INFORMATION,
  292. '[Database]',
  293. 'host',
  294. '',
  295. $host, null, null, get_lang('MysqlHostInfo')
  296. );
  297. $array[] = $this->build_setting(
  298. self :: STATUS_INFORMATION,
  299. '[Database]',
  300. 'port',
  301. '',
  302. $port, null, null, get_lang('Port')
  303. );
  304. $array[] = $this->build_setting(
  305. self :: STATUS_INFORMATION,
  306. '[Database]',
  307. 'Database name',
  308. '',
  309. $db, null, null, get_lang('Name')
  310. );
  311. return $array;
  312. }
  313. /**
  314. * Functions to get the data for the webserver diagnostics
  315. * @return array of data
  316. */
  317. public function get_webserver_data()
  318. {
  319. $array = array();
  320. $array[] = $this->build_setting(self :: STATUS_INFORMATION, '[SERVER]', '$_SERVER["SERVER_NAME"]', 'http://be.php.net/reserved.variables.server', $_SERVER["SERVER_NAME"], null, null, get_lang('ServerNameInfo'));
  321. $array[] = $this->build_setting(self :: STATUS_INFORMATION, '[SERVER]', '$_SERVER["SERVER_ADDR"]', 'http://be.php.net/reserved.variables.server', $_SERVER["SERVER_ADDR"], null, null, get_lang('ServerAddessInfo'));
  322. $array[] = $this->build_setting(self :: STATUS_INFORMATION, '[SERVER]', '$_SERVER["SERVER_PORT"]', 'http://be.php.net/reserved.variables.server', $_SERVER["SERVER_PORT"], null, null, get_lang('ServerPortInfo'));
  323. $array[] = $this->build_setting(self :: STATUS_INFORMATION, '[SERVER]', '$_SERVER["SERVER_SOFTWARE"]', 'http://be.php.net/reserved.variables.server', $_SERVER["SERVER_SOFTWARE"], null, null, get_lang('ServerSoftwareInfo'));
  324. $array[] = $this->build_setting(self :: STATUS_INFORMATION, '[SERVER]', '$_SERVER["REMOTE_ADDR"]', 'http://be.php.net/reserved.variables.server', $_SERVER["REMOTE_ADDR"], null, null, get_lang('ServerRemoteInfo'));
  325. $array[] = $this->build_setting(self :: STATUS_INFORMATION, '[SERVER]', '$_SERVER["HTTP_USER_AGENT"]', 'http://be.php.net/reserved.variables.server', $_SERVER["HTTP_USER_AGENT"], null, null, get_lang('ServerUserAgentInfo'));
  326. /*$path = $this->manager->get_url(array('section' => Request :: get('section')));
  327. $request = $_SERVER["REQUEST_URI"];
  328. $status = $request != $path ? self :: STATUS_ERROR : self :: STATUS_OK;
  329. $array[] = $this->build_setting($status, '[SERVER]', '$_SERVER["REQUEST_URI"]', 'http://be.php.net/reserved.variables.server', $request, $path, null, get_lang('RequestURIInfo'));
  330. */
  331. $array[] = $this->build_setting(self :: STATUS_INFORMATION, '[SERVER]', '$_SERVER["SERVER_PROTOCOL"]', 'http://be.php.net/reserved.variables.server', $_SERVER["SERVER_PROTOCOL"], null, null, get_lang('ServerProtocolInfo'));
  332. $array[] = $this->build_setting(self :: STATUS_INFORMATION, '[SERVER]', 'php_uname()', 'http://be2.php.net/php_uname', php_uname(), null, null, get_lang('UnameInfo'));
  333. return $array;
  334. }
  335. /**
  336. * Additional functions needed for fast integration
  337. */
  338. public function build_setting(
  339. $status,
  340. $section,
  341. $title,
  342. $url,
  343. $current_value,
  344. $expected_value,
  345. $formatter,
  346. $comment
  347. ) {
  348. switch ($status) {
  349. case self :: STATUS_OK :
  350. $img = 'bullet_green.png';
  351. break;
  352. case self :: STATUS_WARNING :
  353. $img = 'bullet_orange.png';
  354. break;
  355. case self :: STATUS_ERROR :
  356. $img = 'bullet_red.png';
  357. break;
  358. case self :: STATUS_INFORMATION :
  359. $img = 'bullet_blue.png';
  360. break;
  361. }
  362. $image = Display::return_icon($img, $status);
  363. $url = $this->get_link($title, $url);
  364. $formatted_current_value = $current_value;
  365. $formatted_expected_value = $expected_value;
  366. if ($formatter) {
  367. if (method_exists($this, 'format_' . $formatter)) {
  368. $formatted_current_value = call_user_func(array($this, 'format_' . $formatter), $current_value);
  369. $formatted_expected_value = call_user_func(array($this, 'format_' . $formatter), $expected_value);
  370. }
  371. }
  372. return array($image, $section, $url, $formatted_current_value, $formatted_expected_value, $comment);
  373. }
  374. /**
  375. * Create a link with a url and a title
  376. * @param $title
  377. * @param $url
  378. * @return string the url
  379. */
  380. public function get_link($title, $url)
  381. {
  382. return '<a href="' . $url . '" target="about:bank">' . $title . '</a>';
  383. }
  384. public function format_yes_no_optional($value)
  385. {
  386. $return = '';
  387. switch($value) {
  388. case 0:
  389. $return = get_lang('No');
  390. break;
  391. case 1:
  392. $return = get_lang('Yes');
  393. break;
  394. case 2:
  395. $return = get_lang('Optional');
  396. break;
  397. }
  398. return $return;
  399. }
  400. function format_yes_no($value)
  401. {
  402. return $value ? get_lang('Yes') : get_lang('No');
  403. }
  404. function format_on_off($value)
  405. {
  406. $value = intval($value);
  407. if ($value > 1) {
  408. // Greater than 1 values are shown "as-is", they may be interpreted as "On" later.
  409. return $value;
  410. }
  411. // These are the values 'On' and 'Off' used in the php-ini file. Translation (get_lang()) is not needed here.
  412. return $value ? 'On' : 'Off';
  413. }
  414. }