[0000226] Fixed checks for js cached files, colors in messages
[fa-stable.git] / admin / system_diagnostics.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 'SA_SOFTWAREUPGRADE';
13 $path_to_root="..";
14
15 include($path_to_root . "/includes/session.inc");
16
17 page(_($help_context = "System Diagnostics"));
18
19 include($path_to_root . "/includes/ui.inc");
20
21 // Type of requirement for positive test result
22 $test_level = array(
23         0 => _('Info'),
24         1 => _('Optional'),
25         2 => _('Recomended'),
26         3 => _('Required ')
27 );
28
29 $system_tests = array('tst_mysql', 'tst_php', 'tst_server', 'tst_system', 'tst_browser',
30         'tst_gettext', 'tst_debug', 'tst_logging',
31         'tst_dbversion', 'tst_subdirs', 'tst_langs', 'tst_tmpdir', 'tst_sessionpath',
32         'tst_dbconfig', 'tst_config', 'tst_extconfig'
33         );
34
35 function tst_mysql() 
36 {
37         $test['descr'] = _('MySQL version'). ' >3.23.58';
38         $test['type'] = 3;
39         $test['test'] = mysql_get_server_info();
40         $test['result'] = $test['test']>'3.23.58';
41         $test['comments'] = _('Upgrade MySQL server to version at least 3.23.58');
42
43         return $test;
44 }
45
46 function tst_php() 
47 {
48         $test['descr'] = _('PHP version').' >4.3.2';
49         $test['type'] = 3;
50         $test['test'] = phpversion();
51         $test['result'] = $test['test']>'4.3.2';
52         $test['comments'] = _('Upgrade PHP to version at least 4.3.2');
53
54         return $test;
55 }
56
57 function tst_system() 
58 {
59         $test['descr'] = _('Server system');
60         $test['type'] = 0;
61         $test['test'] = PHP_OS;
62         $test['result'] = true;
63
64         return $test;
65 }
66
67 function tst_sessionpath() 
68 {
69         $test['descr'] = _('Session save path');
70         $test['type'] = 0;
71         $test['test'] = session_save_path();
72         $test['result'] = true;
73
74         return $test;
75 }
76
77 function tst_browser() 
78 {
79         $test['descr'] = _('Browser type');
80         $test['type'] = 0;
81         $test['test'] = $_SERVER['HTTP_USER_AGENT'];
82         $test['result'] = true;
83         $test['comments'] = _('Any browser is supported');
84
85         return $test;
86 }
87
88 function tst_server() 
89 {
90         $test['descr'] = _('Http server type');
91         $test['test'] = $_SERVER['SERVER_SOFTWARE'];
92         $test['type'] = 0;
93         $test['result'] = true;
94         $test['comments'] = _('Any server is supported');
95
96         return $test;
97 }
98
99 function tst_gettext() 
100 {
101         $test['descr'] = _('Native gettext');
102         $test['test'] = function_exists('gettext') ? _('Yes'): _('No');
103         $test['type'] = 1;
104         $test['result'] = true;
105         $test['comments'] = _('In case of no getext support, php emulation is used');
106
107         return $test;
108 }
109
110 function tst_debug() 
111 {
112         global $go_debug;
113         $test['descr'] = _('Debugging mode');
114         $test['type'] = 0;
115         $test['test'] = $go_debug ? _("Yes") : _("No");
116         $test['result'] = $go_debug != 0;
117         $test['comments'] = _('To switch debugging on set $go_debug=1 in config.php file');
118
119         return $test;
120 }
121
122 function tst_logging() 
123 {
124         global $error_logfile;
125         
126         $test['descr'] = _('Error logging');
127         $test['type'] = 2;
128         // if error lgging is on, but log file does not exists try write
129         if ($error_logfile && !is_file($error_logfile)) 
130         {
131                 @fclose(@fopen($error_logfile, 'w'));
132         }
133         $test['result'] = @$error_logfile != '' && is_writable($error_logfile);
134         $test['test'] = @$error_logfile == '' ? _("Disabled") : $error_logfile;
135         
136         if (@$error_logfile == '')
137                 $test['comments'] = _('To switch error logging set $error_logging in config.php file');
138         else
139         if (!is_writable($error_logfile))
140                 $test['comments'] = _('Log file is not writeable');
141         
142         return $test;
143 }
144 //
145 //      Installed FA database structure version
146 //
147 function tst_dbversion()
148 {
149         $test['descr'] = _('Current database version');
150         $test['type'] = 3;
151         $test['test'] = get_company_pref('version_id');
152         $test['result'] = $test['test'] == '2.2';
153         $test['comments'] = _('Database structure seems to be not upgraded to current version')
154                 .' (2.2)';
155
156         return $test;
157 }
158
159
160 function tst_subdirs()
161 {
162         global $db_connections, $comp_path;
163
164         $comp_subdirs = array('images', 'pdf_files', 'backup','js_cache');
165
166         $test['descr'] = _('Company subdirectories consistency');
167         $test['type'] = 3;
168         $test['test'] = array($comp_path.'/*');
169         foreach($comp_subdirs as $sub) {
170                 $test['test'][] = $comp_path.'/*/'.$sub;
171         }
172         $test['result'] = true;
173
174         if (!is_dir($comp_path) || !is_writable($comp_path) ) {
175                 $test['result'] = false;
176                 $test['comments'][] = sprintf(_("'%s' is not writeable"), $comp_path);
177                 return $test;
178         };
179         foreach ($db_connections as $n => $comp) {
180                 $path = "$comp_path/$n";
181                 if (!is_dir($path) || !is_writable($path) ) {
182                         $test['result'] = false;
183                         $test['comments'][] = sprintf(_("'%s' is not writeable"), $path);
184                         continue;
185                 };
186                 foreach($comp_subdirs as $sub) {
187                         $spath = $path.'/'.$sub;
188                         if (!is_dir($spath) || !is_writable($spath) ) {
189                                 $test['result'] = false;
190                                 $test['comments'][] = sprintf(_("'%s' is not writeable"), $spath);
191                         } else {
192                                 $dir = opendir($spath);
193                                 while (false !== ($fname = readdir($dir))) {
194                                         // check only *.js files. Manually installed package can contain other
195                                         // non-writable files which are non-crucial for normal operations
196                                         if (preg_match('/.*(\.js)/', $fname) && !is_writable("$spath/$fname")) {
197                                                 $test['result'] = false;
198                                                 $test['comments'][] = sprintf(_("'%s' is not writeable"), "$spath/$fname");
199                                         }
200                                 }
201                         }
202                 }
203         }
204         return $test;
205 }
206
207 function tst_tmpdir()
208 {
209         global $path_to_root;
210         
211         $test['descr'] = _('Temporary directory');
212         $test['type'] = 3;
213         $test['test'] = $path_to_root.'/tmp';
214         $test['result'] = is_dir($test['test']) && is_writable($test['test']);
215         $test['comments'][] = sprintf(_("'%s' is not writeable"), $test['test']);
216         return $test;
217 }
218
219 function tst_langs()
220 {
221         global $installed_languages, $path_to_root;
222         
223         $test['descr'] = _('Language configuration consistency');
224         $test['type'] = 3;
225         $test['result'] = true;
226         $test['comments'] = array();
227
228         $old = setlocale(LC_MESSAGES, '0');
229         
230         $langs = array();
231         
232         foreach ($installed_languages as $lang) {
233                 $langs[] = $lang['code'];
234                 if ($lang['code'] == 'en_GB') continue; // native FA language
235                 
236                 $file = $path_to_root.'/lang/'.$lang['code'].'/LC_MESSAGES/'.$lang['code'];
237         $file .= function_exists('gettext') ? '.mo' : '.po';
238
239                 if (!is_file($file)) {
240                         $test['result'] = false;
241                         $test['comments'][] = sprintf( _('Missing %s translation file.'), $file);
242                 }
243         if (!setlocale(LC_MESSAGES, $lang['code'].".".$lang['encoding'])) 
244         {
245                         $test['result'] = false;
246                         $test['comments'][] = sprintf(_('Missing system locale: %s'), $lang['code'].".".$lang['encoding']);
247         };
248         }
249         
250         setlocale(LC_MESSAGES, $old);
251
252         $test['test'] = $langs;
253
254         return $test;
255 }
256
257 function tst_config()
258 {
259         global $path_to_root;
260
261         $test['descr'] = _('Main config file');
262         $test['type'] = 2;
263         $test['test'] = $path_to_root.'/config.php';
264         $test['result'] = is_file($test['test']) && !is_writable($test['test']);
265         $test['comments'][] = sprintf(_("'%s' file should be read-only"), $test['test']);
266         return $test;
267 }
268
269 function tst_dbconfig()
270 {
271         global $path_to_root;
272
273         $test['descr'] = _('Database auth file');
274         $test['type'] = 2;
275         $test['test'] = $path_to_root.'/config_db.php';
276         $test['result'] = is_file($test['test']) && !is_writable($test['test']);
277         $test['comments'][] = sprintf(_("'%s' file should be read-only if you do not plan to add or change companies"), $test['test']);
278
279         return $test;
280 }
281
282 function tst_extconfig()
283 {
284         global $path_to_root, $db_connections, $comp_path;
285
286         $test['descr'] = _('Extensions configuration files');
287         $test['type'] = 3;
288         $test['test'] = $path_to_root.'/installed_extensions.php';
289         $test['result'] = is_file($test['test']) && is_writable($test['test']);
290         $test['test'] . ','.$comp_path.'/*/installed_extensions.php';
291         $test['comments'][] = sprintf(_("'%s' file should be writeable"), $test['test']);
292
293         foreach ($db_connections as $n => $comp) {
294                 $path = "$comp_path/$n";
295                 if (!is_dir($path)) continue;
296
297                 $path .= "/installed_extensions.php";
298                 if (!is_file($path) || !is_writable($path) ) {
299                         $test['result'] = false;
300                         $test['comments'][] = sprintf(_("'%s' is not writeable"), $path);
301                         continue;
302                 };
303         }
304         return $test;
305 }
306 //-------------------------------------------------------------------------------------------------
307
308 start_table("$table_style width=80%");
309 $th = array(_("Test"), _('Test type'), _("Value"), _("Comments"));
310 table_header($th);
311
312 $k = 0; //row colour counter
313 foreach ($system_tests as $test) 
314 {
315         alt_table_row_color($k);
316         $result = $test();
317         if (!$result) continue;
318     label_cell($result['descr']);
319     label_cell($test_level[$result['type']]);
320
321     $res = is_array(@$result['test']) ? implode('<br>', $result['test']) 
322                 : $result['test'];
323     label_cell($res);
324
325     $comm = is_array(@$result['comments']) ? implode('<br>', $result['comments']) 
326                 : @$result['comments'];
327         $color = ($result['result'] ? 'green': 
328         ($result['type']==3 ? 'red' :
329          ($result['type']==2 ? 'orange' : 'green')));
330     label_cell("<span style='color:$color'>".
331         ($result['result'] ? _('Ok') : '<b>'.$comm.'</b>').'</span>');
332     end_row();
333 }
334
335 end_table();
336 end_page();
337
338 ?>