diagnoser.lib.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. /**
  16. * The status's
  17. */
  18. const STATUS_OK = 1;
  19. const STATUS_WARNING = 2;
  20. const STATUS_ERROR = 3;
  21. const STATUS_INFORMATION = 4;
  22. function __construct() {
  23. }
  24. function show_html() {
  25. $sections = array('chamilo', 'php', 'mysql', 'webserver');
  26. if (!in_array(trim($_GET['section']), $sections)) {
  27. $current_section = 'chamilo';
  28. } else {
  29. $current_section = $_GET['section'];
  30. }
  31. $html = '<div class="tabbable"><ul class="nav nav-tabs">';
  32. foreach ($sections as $section) {
  33. if ($current_section == $section) {
  34. $html .= '<li class="active">';
  35. } else {
  36. $html .= '<li>';
  37. }
  38. $params['section'] = $section;
  39. $html .='<a href="system_status.php?section='.$section.'">'.get_lang($section).'</a></li>';
  40. }
  41. $html .= '</ul><div class="tab-pane">';
  42. $data = call_user_func(array($this, 'get_' . $current_section . '_data'));
  43. echo $html;
  44. $table = new SortableTableFromArray($data, 1, 100);
  45. $table->set_header(0,'', false);
  46. $table->set_header(1,get_lang('Section'), false);
  47. $table->set_header(2,get_lang('Setting'), false);
  48. $table->set_header(3,get_lang('Current'), false);
  49. $table->set_header(4,get_lang('Expected'), false);
  50. $table->set_header(5,get_lang('Comment'), false);
  51. $table->display();
  52. echo '</div></div>';
  53. }
  54. /**
  55. * Functions to get the data for the chamilo diagnostics
  56. * @return array of data
  57. */
  58. function get_chamilo_data() {
  59. $array = array();
  60. $writable_folders = array('archive', 'courses', 'home', 'main/upload/users/', 'main/default_course_document/images/');
  61. foreach ($writable_folders as $index => $folder) {
  62. $writable = is_writable(api_get_path(SYS_PATH) . $folder);
  63. $status = $writable ? self :: STATUS_OK : self :: STATUS_ERROR;
  64. $array[] = $this->build_setting($status, '[FILES]', get_lang('IsWritable') . ': ' . $folder, 'http://be2.php.net/manual/en/function.is-writable.php', $writable, 1, 'yes_no', get_lang('DirectoryMustBeWritable'));
  65. }
  66. $exists = file_exists(api_get_path(SYS_CODE_PATH).'install');
  67. $status = $exists ? self :: STATUS_WARNING : self :: STATUS_OK;
  68. $array[] = $this->build_setting($status, '[FILES]', get_lang('DirectoryExists') . ': /install', 'http://be2.php.net/file_exists', $exists, 0, 'yes_no', get_lang('DirectoryShouldBeRemoved'));
  69. $app_version = api_get_setting('chamilo_database_version');
  70. $array[] = $this->build_setting(self :: STATUS_INFORMATION, '[DB]', 'chamilo_database_version', '#', $app_version, 0, null, 'Chamilo DB version');
  71. return $array;
  72. }
  73. /**
  74. * Functions to get the data for the php diagnostics
  75. * @return array of data
  76. */
  77. function get_php_data() {
  78. $array = array();
  79. // General Functions
  80. $version = phpversion();
  81. $status = $version > REQUIRED_PHP_VERSION ? self :: STATUS_OK : self :: STATUS_ERROR;
  82. $array[] = $this->build_setting($status, '[PHP]', 'phpversion()', 'http://www.php.net/manual/en/function.phpversion.php', phpversion(), '>= '.REQUIRED_PHP_VERSION, null, get_lang('PHPVersionInfo'));
  83. $setting = ini_get('output_buffering');
  84. $req_setting = 1;
  85. $status = $setting >= $req_setting ? self :: STATUS_OK : self :: STATUS_ERROR;
  86. $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'));
  87. $setting = ini_get('file_uploads');
  88. $req_setting = 1;
  89. $status = $setting == $req_setting ? self :: STATUS_OK : self :: STATUS_ERROR;
  90. $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'));
  91. $setting = ini_get('magic_quotes_runtime');
  92. $req_setting = 0;
  93. $status = $setting == $req_setting ? self :: STATUS_OK : self :: STATUS_ERROR;
  94. $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'));
  95. $setting = ini_get('safe_mode');
  96. $req_setting = 0;
  97. $status = $setting == $req_setting ? self :: STATUS_OK : self :: STATUS_WARNING;
  98. $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'));
  99. $setting = ini_get('register_globals');
  100. $req_setting = 0;
  101. $status = $setting == $req_setting ? self :: STATUS_OK : self :: STATUS_ERROR;
  102. $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'));
  103. $setting = ini_get('short_open_tag');
  104. $req_setting = 0;
  105. $status = $setting == $req_setting ? self :: STATUS_OK : self :: STATUS_WARNING;
  106. $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'));
  107. $setting = ini_get('magic_quotes_gpc');
  108. $req_setting = 0;
  109. $status = $setting == $req_setting ? self :: STATUS_OK : self :: STATUS_ERROR;
  110. $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'));
  111. $setting = ini_get('display_errors');
  112. $req_setting = 0;
  113. $status = $setting == $req_setting ? self :: STATUS_OK : self :: STATUS_WARNING;
  114. $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'));
  115. $setting = ini_get('default_charset');
  116. if ($setting == '')
  117. $setting = null;
  118. $req_setting = null;
  119. $status = $setting == $req_setting ? self :: STATUS_OK : self :: STATUS_ERROR;
  120. $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'));
  121. $setting = ini_get('max_execution_time');
  122. $req_setting = '300 (' . get_lang('minimum') . ')';
  123. $status = $setting >= 300 ? self :: STATUS_OK : self :: STATUS_WARNING;
  124. $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'));
  125. $setting = ini_get('max_input_time');
  126. $req_setting = '300 (' . get_lang('minimum') . ')';
  127. $status = $setting >= 300 ? self :: STATUS_OK : self :: STATUS_WARNING;
  128. $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'));
  129. $setting = ini_get('memory_limit');
  130. $req_setting = '>= '.REQUIRED_MIN_MEMORY_LIMIT.'M';
  131. $status = self :: STATUS_ERROR;
  132. if ((float)$setting >= REQUIRED_MIN_MEMORY_LIMIT)
  133. $status = self :: STATUS_OK;
  134. $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'));
  135. $setting = ini_get('post_max_size');
  136. $req_setting = '>= '.REQUIRED_MIN_POST_MAX_SIZE.'M';
  137. $status = self :: STATUS_ERROR;
  138. if ((float)$setting >= REQUIRED_MIN_POST_MAX_SIZE)
  139. $status = self :: STATUS_OK;
  140. $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'));
  141. $setting = ini_get('upload_max_filesize');
  142. $req_setting = '>= '.REQUIRED_MIN_UPLOAD_MAX_FILESIZE.'M';
  143. $status = self :: STATUS_ERROR;
  144. if ((float)$setting >= REQUIRED_UPLOAD_MAX_FILESIZE)
  145. $status = self :: STATUS_OK;
  146. $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'));
  147. $setting = ini_get('variables_order');
  148. $req_setting = 'GPCS';
  149. $status = $setting == $req_setting ? self :: STATUS_OK : self :: STATUS_ERROR;
  150. $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'));
  151. $setting = ini_get('session.gc_maxlifetime');
  152. $req_setting = '4320';
  153. $status = $setting == $req_setting ? self :: STATUS_OK : self :: STATUS_WARNING;
  154. $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'));
  155. if (api_check_browscap()){$setting = true;}else{$setting=false;}
  156. $req_setting = true;
  157. $status = $setting == $req_setting ? self :: STATUS_OK : self :: STATUS_WARNING;
  158. $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'));
  159. //Extensions
  160. $extensions = array('gd' => array('link'=>'http://www.php.net/gd', 'expected' => 1, 'comment' => get_lang('ExtensionMustBeLoaded')),
  161. 'mysql' => array('link'=>'http://www.php.net/mysql', 'expected' => 1, 'comment' => get_lang('ExtensionMustBeLoaded')),
  162. 'pcre' => array('link'=>'http://www.php.net/pcre', 'expected' => 1, 'comment' => get_lang('ExtensionMustBeLoaded')),
  163. 'session' => array('link'=>'http://www.php.net/session', 'expected' => 1, 'comment' => get_lang('ExtensionMustBeLoaded')),
  164. 'standard' => array('link'=>'http://www.php.net/spl', 'expected' => 1, 'comment' => get_lang('ExtensionMustBeLoaded')),
  165. 'zlib' => array('link'=>'http://www.php.net/zlib', 'expected' => 1, 'comment' => get_lang('ExtensionMustBeLoaded')),
  166. 'xsl' => array('link'=>'http://be2.php.net/xsl', 'expected' => 2, 'comment' => get_lang('ExtensionShouldBeLoaded')),
  167. 'curl' => array('link'=>'http://www.php.net/curl', 'expected' => 2, 'comment' => get_lang('ExtensionShouldBeLoaded')),
  168. );
  169. foreach ($extensions as $extension => $data) {
  170. $url = $data['link'];
  171. $expected_value = $data['expected'];
  172. $comment = $data['comment'];
  173. $loaded = extension_loaded($extension);
  174. $status = $loaded ? self :: STATUS_OK : self :: STATUS_ERROR;
  175. $array[] = $this->build_setting($status, '[EXTENSION]', get_lang('LoadedExtension') . ': ' . $extension, $url, $loaded, $expected_value, 'yes_no_optional', $comment);
  176. }
  177. return $array;
  178. }
  179. /**
  180. * Functions to get the data for the mysql diagnostics
  181. * @return array of data
  182. */
  183. function get_mysql_data() {
  184. $array = array();
  185. // A note: Maybe it would be better if all "MySQL"-like variable names and words on the page to be replaced with "Database"-like ones.
  186. $array[] = $this->build_setting(self :: STATUS_INFORMATION, '[MySQL]', 'mysql_get_host_info()', 'http://www.php.net/manual/en/function.mysql-get-host-info.php', Database::get_host_info(), null, null, get_lang('MysqlHostInfo'));
  187. $array[] = $this->build_setting(self :: STATUS_INFORMATION, '[MySQL]', 'mysql_get_server_info()', 'http://www.php.net/manual/en/function.mysql-get-server-info.php', Database::get_server_info(), null, null, get_lang('MysqlServerInfo'));
  188. $array[] = $this->build_setting(self :: STATUS_INFORMATION, '[MySQL]', 'mysql_get_proto_info()', 'http://www.php.net/manual/en/function.mysql-get-proto-info.php', Database::get_proto_info(), null, null, get_lang('MysqlProtoInfo'));
  189. $array[] = $this->build_setting(self :: STATUS_INFORMATION, '[MySQL]', 'mysql_get_client_info()', 'http://www.php.net/manual/en/function.mysql-get-client-info.php', Database::get_client_info(), null, null, get_lang('MysqlClientInfo'));
  190. return $array;
  191. }
  192. /**
  193. * Functions to get the data for the webserver diagnostics
  194. * @return array of data
  195. */
  196. function get_webserver_data() {
  197. $array = array();
  198. $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'));
  199. $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'));
  200. $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'));
  201. $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'));
  202. $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'));
  203. $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'));
  204. /*$path = $this->manager->get_url(array('section' => Request :: get('section')));
  205. $request = $_SERVER["REQUEST_URI"];
  206. $status = $request != $path ? self :: STATUS_ERROR : self :: STATUS_OK;
  207. $array[] = $this->build_setting($status, '[SERVER]', '$_SERVER["REQUEST_URI"]', 'http://be.php.net/reserved.variables.server', $request, $path, null, get_lang('RequestURIInfo'));
  208. */
  209. $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'));
  210. $array[] = $this->build_setting(self :: STATUS_INFORMATION, '[SERVER]', 'php_uname()', 'http://be2.php.net/php_uname', php_uname(), null, null, get_lang('UnameInfo'));
  211. return $array;
  212. }
  213. /**
  214. * Additional functions needed for fast integration
  215. */
  216. function build_setting($status, $section, $title, $url, $current_value, $expected_value, $formatter, $comment, $img_path = null) {
  217. switch ($status) {
  218. case self :: STATUS_OK :
  219. $img = 'bullet_green.gif';
  220. break;
  221. case self :: STATUS_WARNING :
  222. $img = 'bullet_orange.gif';
  223. break;
  224. case self :: STATUS_ERROR :
  225. $img = 'bullet_red.gif';
  226. break;
  227. case self :: STATUS_INFORMATION :
  228. $img = 'bullet_blue.gif';
  229. break;
  230. }
  231. if (! $img_path) {
  232. $img_path = api_get_path(WEB_IMG_PATH);
  233. }
  234. $image = '<img src="' . $img_path . $img . '" alt="' . $status . '" />';
  235. $url = $this->get_link($title, $url);
  236. $formatted_current_value = $current_value;
  237. $formatted_expected_value = $expected_value;
  238. if ($formatter) {
  239. if (method_exists($this, 'format_' . $formatter)) {
  240. $formatted_current_value = call_user_func(array($this, 'format_' . $formatter), $current_value);
  241. $formatted_expected_value = call_user_func(array($this, 'format_' . $formatter), $expected_value);
  242. }
  243. }
  244. return array($image, $section, $url, $formatted_current_value, $formatted_expected_value, $comment);
  245. }
  246. /**
  247. * Create a link with a url and a title
  248. * @param $title
  249. * @param $url
  250. * @return string the url
  251. */
  252. function get_link($title, $url) {
  253. return '<a href="' . $url . '" target="about:bank">' . $title . '</a>';
  254. }
  255. function format_yes_no_optional($value) {
  256. $return = '';
  257. switch($value) {
  258. case 0:
  259. $return = get_lang('No');
  260. break;
  261. case 1:
  262. $return = get_lang('Yes');
  263. break;
  264. case 2:
  265. $return = get_lang('Optional');
  266. break;
  267. }
  268. return $return;
  269. }
  270. function format_yes_no($value) {
  271. return $value ? get_lang('Yes') : get_lang('No');
  272. }
  273. function format_on_off($value) {
  274. $value = intval($value);
  275. if ($value > 1) {
  276. // Greater than 1 values are shown "as-is", they may be interpreted as "On" later.
  277. return $value;
  278. }
  279. // These are the values 'On' and 'Off' used in the php-ini file. Translation (get_lang()) is not needed here.
  280. return $value ? 'On' : 'Off';
  281. }
  282. }