Fixed many issues in output HTML code according to HTML 4.01 Transitional format.
[fa-stable.git] / includes / system_tests.inc
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
13 // Type of requirement for positive test result
14 $test_level = array(
15         0 => _('Info'),
16         1 => _('Optional'),
17         2 => _('Recommended'),
18         3 => _('Required ')
19 );
20
21 // test mysql server version
22 function tst_mysql() 
23 {
24         $test['descr'] = _('MySQL version'). ' >=4.1';
25         $test['type'] = 3;
26         $test['test'] = mysql_get_server_info();
27         $test['result'] = $test['test']>='4.1';
28         $test['comments'] = _('Upgrade MySQL server to version at least 4.1');
29
30         return $test;
31 }
32 // test php mysql extension
33 function tst_phpmysql() 
34 {
35         $test['descr'] = _('PHP MySQL extension');
36         $test['type'] = 3;
37         $test['result'] = function_exists('mysql_connect');
38         $test['test'] = $test['result'] ? _('Yes'): _('No');
39         
40         $test['comments'] = _('Your PHP has to have MySQL extension enabled.');
41         return $test;
42 }
43
44 function tst_php() 
45 {
46         $test['descr'] = _('PHP version').' >=4.3.3';
47         $test['type'] = 3;
48         $test['test'] = phpversion();
49         $test['result'] = $test['test']>='4.3.3';
50         $test['comments'] = _('Upgrade PHP to version at least 4.3.3');
51
52         return $test;
53 }
54
55 function tst_system() 
56 {
57         $test['descr'] = _('Server system');
58         $test['type'] = 0;
59         $test['test'] = PHP_OS;
60         $test['result'] = true;
61
62         return $test;
63 }
64
65 function tst_sessionpath() 
66 {
67         $test['descr'] = _('Session save path');
68         $test['type'] = 0;
69         $test['test'] = session_save_path();
70         $test['result'] = true;
71
72         return $test;
73 }
74
75 function tst_install() 
76 {
77         global $path_to_root;
78
79         $test['descr'] = _('Removed install wizard folder');
80         $test['type'] = 2;
81         $test['result'] = !is_dir($path_to_root.'/install');
82         $test['test'] = _('Not removed');
83         $test['comments'] = _('Remove or rename install wizard folder for security reasons.');
84
85         return $test;
86 }
87
88 function tst_browser() 
89 {
90         $test['descr'] = _('Browser type');
91         $test['type'] = 0;
92         $test['test'] = $_SERVER['HTTP_USER_AGENT'];
93         $test['result'] = true;
94         $test['comments'] = _('Any browser is supported');
95
96         return $test;
97 }
98
99 function tst_server() 
100 {
101         $test['descr'] = _('Http server type');
102         $test['test'] = $_SERVER['SERVER_SOFTWARE'];
103         $test['type'] = 0;
104         $test['result'] = true;
105         $test['comments'] = _('Any server is supported');
106
107         return $test;
108 }
109
110 function tst_gettext() 
111 {
112         $test['descr'] = _('Native gettext');
113         $test['test'] = function_exists('gettext') ? _('Yes'): _('No');
114         $test['type'] = 1;
115         $test['result'] = true;
116         $test['comments'] = _('In case of no gettext support, php emulation is used');
117
118         return $test;
119 }
120
121 function tst_debug() 
122 {
123         global $go_debug;
124         $test['descr'] = _('Debugging mode');
125         $test['type'] = 0;
126         $test['test'] = $go_debug ? _("Yes") : _("No");
127         $test['result'] = $go_debug != 0;
128         $test['comments'] = _('To switch debugging on set $go_debug>0 in config.php file');
129
130         return $test;
131 }
132
133 function tst_logging() 
134 {
135         global $error_logfile;
136         
137         $test['descr'] = _('Error logging');
138         $test['type'] = 2;
139         // if error lgging is on, but log file does not exists try write
140         if ($error_logfile && !is_file($error_logfile)) 
141         {
142                 @fclose(@fopen($error_logfile, 'w'));
143         }
144         $test['result'] = @$error_logfile != '' && is_writable($error_logfile);
145         $test['test'] = @$error_logfile == '' ? _("Disabled") : $error_logfile;
146         
147         if (@$error_logfile == '')
148                 $test['comments'] = _('To switch error logging set $error_logging in config.php file');
149         else
150         if (!is_writable($error_logfile))
151                 $test['comments'] = _('Log file is not writeable');
152         
153         return $test;
154 }
155 //
156 //      Installed FA database structure version
157 //
158 function tst_dbversion()
159 {
160         global $db_version;
161         $test['descr'] = _('Current database version');
162         $test['type'] = 3;
163         $test['test'] = get_company_pref('version_id');
164         $test['result'] = $test['test'] == $db_version;
165         $test['comments'] = _('Database structure seems to be not upgraded to current version')
166                 ." ($db_version)";
167
168         return $test;
169 }
170
171
172 function tst_subdirs($install=false)
173 {
174         global $db_connections;
175
176         $comps = $install ? array('0') : array_keys($db_connections);
177
178         $comp_subdirs = array('images', 'pdf_files', 'backup','js_cache');
179
180         $test['descr'] = _('Company subdirectories consistency');
181         $test['type'] = 3;
182         $test['test'] = array(company_path().'/*');
183         foreach($comp_subdirs as $sub) {
184                 $test['test'][] = company_path().'/*/'.$sub;
185         }
186         $test['result'] = true;
187         
188         $comp_path = company_path();
189         foreach ($comps as $n) {
190                 $path = company_path($n);
191                 if (!is_dir($path) || !is_writable($path) ) {
192                         $test['result'] = false;
193                         $test['comments'][] = sprintf(_("'%s' is not writeable"), $path);
194                         continue;
195                 };
196                 foreach($comp_subdirs as $sub) {
197                         $spath = $path.'/'.$sub;
198                         if (!is_dir($spath) || !is_writable($spath) ) {
199                                 $test['result'] = false;
200                                 $test['comments'][] = sprintf(_("'%s' is not writeable"), $spath);
201                         } else {
202                                 $dir = opendir($spath);
203                                 while (false !== ($fname = readdir($dir))) {
204                                         // check only *.js files. Manually installed package can contain other
205                                         // non-writable files which are non-crucial for normal operations
206                                         if (preg_match('/.*(\.js)/', $fname) && !is_writable("$spath/$fname")) {
207                                                 $test['result'] = false;
208                                                 $test['comments'][] = sprintf(_("'%s' is not writeable"), "$spath/$fname");
209                                         }
210                                 }
211                         }
212                 }
213         }
214         return $test;
215 }
216
217 function tst_tmpdir()
218 {
219         global $path_to_root;
220         
221         $test['descr'] = _('Temporary directory');
222         $test['type'] = 3;
223         $test['test'] = $path_to_root.'/tmp';
224         $test['result'] = is_dir($test['test']) && is_writable($test['test']);
225         $test['comments'][] = sprintf(_("'%s' is not writeable"), $test['test']);
226         return $test;
227 }
228
229 function tst_langs($install)
230 {
231         global $installed_languages, $path_to_root, $GetText;
232         
233         $test['descr'] = _('Language configuration consistency');
234         $test['type'] = 3;
235         $test['result'] = true;
236         $test['comments'] = array();
237
238         $fname =  $path_to_root.'/lang';
239         $test['test'] = $fname;
240         if (!(is_dir($fname) && is_writable($fname))) {
241                 $test['result'] = false;
242                 $test['comments'][] = _("Languages folder should be writeable.");
243                 return $test;
244         }
245         
246         if (!$install) {
247                 $fname =  $path_to_root.'/lang/installed_languages.inc';
248                 $test['test'] = $fname;
249                 if (!(is_file($fname) && is_writable($fname))) {
250                         $test['result'] = false;
251                         $test['comments'][] = _("Languages configuration file should be writeable.");
252                         return $test;
253                 }
254         }
255
256         $langs = array();
257         
258         foreach ($installed_languages as $lang) {
259
260                 if ($lang['code'] == 'C') continue; // no translation (English)
261                 $langs[] = $lang['code'];
262
263                 $file = $path_to_root.'/lang/'.$lang['code'].'/LC_MESSAGES/'.$lang['code'];
264                 if (@$lang['version'])
265                         $file .= '-'.$lang['version'];
266                 $file .= function_exists('gettext') ? '.mo' : '.po';
267
268                 if (!is_file($file)) {
269                         $test['result'] = false;
270                         $test['comments'][] = sprintf( _('Missing %s translation file.'), $file);
271                 }
272                 if (!$GetText->check_support($lang['code'], $lang['encoding']))
273                 {
274                         $test['result'] = false;
275                         $test['comments'][] = sprintf(_('Missing system locale: %s'), $lang['code'].".".$lang['encoding']);
276                 };
277         }
278
279         $test['test'] = $langs;
280
281         return $test;
282 }
283
284 function tst_config($install)
285 {
286         global $path_to_root;
287
288         $test['descr'] = _('Main config file');
289         $test['test'] = $path_to_root.'/config.php';
290         if ($install) {
291                 $test['type'] = 3;
292                 $writable = check_write($test['test']);
293                 $test['result'] = $writable==1;
294                 $test['comments'][] = $writable == 0 ?
295                         sprintf(_("Can't write '%s' file. Check FA directory write permissions."), $test['test'])
296                         : sprintf(_("'%s' file exists."), $test['test']);
297         } else {
298                 $test['type'] = 2;
299                 $test['result'] = is_file($test['test']) && !is_writable($test['test']);
300                 $test['comments'][] = sprintf(_("'%s' file should be read-only"), $test['test']);
301         }
302         return $test;
303 }
304
305 function tst_dbconfig($install)
306 {
307         global $path_to_root;
308
309         $test['descr'] = _('Database auth file');
310         $test['test'] = $path_to_root.'/config_db.php';
311
312         if ($install) {
313                 $test['type'] = 3;
314                 $writable = check_write($test['test']);
315                 $test['result'] = $writable==1;
316                 $test['comments'][] = $writable == 0 ?
317                         sprintf(_("Can't write '%s' file. Check FA directory write permissions."), $test['test'])
318                         : sprintf(_("'%s' file exists."), $test['test']);
319         } else {
320                 $test['type'] = 2;
321                 $test['result'] = is_file($test['test']) && !is_writable($test['test']);
322                 $test['comments'][] = sprintf(_("'%s' file should be read-only if you do not plan to add or change companies"), $test['test']);
323         }
324         return $test;
325 }
326
327 function tst_extconfig($install)
328 {
329         global $path_to_root, $db_connections;
330
331         $comps = $install ? array('0') : array_keys($db_connections);
332         
333         $test['descr'] = _('Extensions system');
334         $test['type'] = 3;
335         $fname =  $path_to_root.'/installed_extensions.php';
336         $test['test'][] = $fname;
337         $test['result'] = ($install || is_file($fname)) && check_write($fname);
338         $test['test'][] = company_path().'/*/installed_extensions.php';
339         if (!$test['result'])
340                 $test['comments'][] = sprintf(_("'%s' is not writeable"), $fname);
341
342         foreach ($comps as $n) {
343                 $path = company_path($n);
344                 if (!is_dir($path)) continue;
345
346                 $path .= "/installed_extensions.php";
347                 if ((!$install && !is_file($path)) || !check_write($path) ) {
348                         $test['result'] = false;
349                         $test['comments'][] = sprintf(_("'%s' is not writeable"), $path);
350                         continue;
351                 };
352         }
353         foreach(array('modules', 'modules/_cache', 'themes', 'sql') as $dir) {
354                 $fname =  $path_to_root.'/'.$dir;
355                 $test['test'][] = $fname;
356                 $t = is_dir($fname) && is_writable($fname);
357                 if (!$t)
358                         $test['comments'][] = sprintf(_("'%s' is not writeable"), $fname);
359                 $test['result'] &= $t;
360         }
361
362         foreach(array('Release', 'Themes', 'Languages', 'Extensions', 'Charts') as $file) {
363                 $fname = $path_to_root."/modules/_cache/".$file.".gz";
364                 $t = !file_exists($fname) || is_writable($fname);
365                 if (!$t)
366                         $test['comments'][] = sprintf(_("'%s' is not writeable"), $fname);
367                 $test['result'] &= $t;
368         }
369
370         if(!$test['result'])
371                 $test['comments'][] = _("Extensions configuration files and directories should be writeable");
372
373         $fname = $path_to_root."/themes";
374         $themedir = opendir($fname);
375         while (false !== ($fname = readdir($themedir)))
376         {
377                 if ($fname!='.' && $fname!='..' && $fname!='CVS' && is_dir($path_to_root.'/themes/'.$fname)
378                         && !in_array($fname, array('aqua', 'cool', 'default')))
379                 {
380                         $test['test'][] = $fname;
381                         $test['result'] = is_writable($path_to_root.'/themes/'.$fname);
382                         if (!$test['result']) {
383                                 $test['comments'][] = 
384                                         sprintf(_("Non-standard theme directory '%s' is not writable"), $fname);
385                                 break;
386                         }
387                 }
388         }
389         closedir($themedir);
390
391         $test['test'][] = 'OpenSSL PHP extension';
392         if (!extension_loaded('openssl')) {
393                 $test['result'] = false;
394                 $test['comments'][] = _("OpenSSL PHP extension have to be enabled to use extension repository system.");
395         } elseif (!function_exists('openssl_verify')) {
396                 $test['result'] = false;
397                 $test['comments'][] = _("OpenSSL have to be available on your server to use extension repository system.");
398         }
399         return $test;
400 }
401
402 function display_system_tests($install = false)
403 {
404         global $test_level;
405
406         if ($install)
407                 $system_tests = array('tst_php', 'tst_phpmysql', 'tst_system', 'tst_dbconfig', 
408                         'tst_config',
409                         'tst_subdirs', 'tst_langs', 'tst_tmpdir', 'tst_sessionpath', 'tst_extconfig'
410                 );
411         else
412                 $system_tests = array('tst_mysql', 'tst_php', 'tst_server', 'tst_system', 'tst_browser',
413                         'tst_gettext', 'tst_debug', 'tst_logging',
414                         'tst_dbversion', 'tst_subdirs', 'tst_langs', 'tst_tmpdir', 'tst_sessionpath',
415                         'tst_install', 'tst_dbconfig', 'tst_config', 'tst_extconfig'
416                 );
417
418
419         start_table(TABLESTYLE, "width='80%'");
420         $th = array(_("Test"), _('Test type'), _("Value"), _("Comments"));
421         table_header($th);
422
423         $ret = true;
424         $k = 0; //row colour counter
425         foreach ($system_tests as $test) 
426         {
427                 alt_table_row_color($k);
428                 $result = $test($install);
429                 if (!$result) continue;
430                 
431                 label_cell($result['descr']);
432                 label_cell($test_level[$result['type']]);
433
434                 $res = is_array(@$result['test']) ? implode('<br>', $result['test']) 
435                         : $result['test'];
436                 label_cell($res);
437
438                 $comm = is_array(@$result['comments']) ? implode('<br>', $result['comments']) 
439                         : @$result['comments'];
440                 $color = ($result['result'] ? 'green': 
441                         ($result['type']==3 ? 'red' :
442                          ($result['type']==2 ? 'orange' : 'green')));
443                 label_cell("<span style='color:$color'>".
444                         ($result['result'] ? _('Ok') : '<b>'.$comm.'</b>').'</span>');
445                 end_row();
446                 $ret = $ret && (($result['result']!=0) || ($result['type'] < 3));
447         }
448         end_table();
449
450         return $ret;
451 }