Completed
Push — 1.10.x ( 58dd38...3c862d )
by
unknown
71:36 queued 22:19
created

Diagnoser::build_setting()   C

Complexity

Conditions 7
Paths 15

Size

Total Lines 40
Code Lines 31

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 40
rs 6.7272
cc 7
eloc 31
nc 15
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * Class Diagnoser
6
 * Class that is responsible for generating diagnostic information about the system
7
 *
8
 * @package chamilo.diagnoser
9
 * @author Ivan Tcholakov, 2008, initiall proposal and sample code.
10
 * @author spou595, 2009, implementation for Chamilo 2.x
11
 * @author Julio Montoya <[email protected]>, 2010, port to chamilo 1.8.7, Some fixes
12
 *
13
 */
14
class Diagnoser
15
{
16
    const STATUS_OK = 1;
17
    const STATUS_WARNING = 2;
18
    const STATUS_ERROR 	= 3;
19
    const STATUS_INFORMATION = 4;
20
21
    /**
22
     * Contructor
23
     */
24
    public function __construct()
25
    {
26
    }
27
28
    public function show_html()
29
    {
30
        $sections = array('chamilo', 'php', 'database', 'webserver');
31
32
        if (isset($_GET['section']) && in_array(trim($_GET['section']), $sections)) {
33
            $current_section = $_GET['section'];
34
        } else {
35
            $current_section = 'chamilo';
36
        }
37
38
        $html = '<div class="tabbable"><ul class="nav nav-tabs">';
39
40
        foreach ($sections as $section) {
41
            if ($current_section == $section) {
42
                $html .= '<li class="active">';
43
            } else {
44
                $html .= '<li>';
45
            }
46
            $params['section'] = $section;
47
            $html .='<a href="system_status.php?section='.$section.'">'.get_lang($section).'</a></li>';
48
        }
49
50
        $html .= '</ul><div class="tab-pane">';
51
52
        $data = call_user_func(array($this, 'get_' . $current_section . '_data'));
53
        echo $html;
54
        $table = new SortableTableFromArray($data, 1, 100);
55
56
        $table->set_header(0,'', false);
57
        $table->set_header(1,get_lang('Section'), false);
58
        $table->set_header(2,get_lang('Setting'), false);
59
        $table->set_header(3,get_lang('Current'), false);
60
        $table->set_header(4,get_lang('Expected'), false);
61
        $table->set_header(5,get_lang('Comment'), false);
62
63
        $table->display();
64
        echo '</div></div>';
65
    }
66
67
    /**
68
     * Functions to get the data for the chamilo diagnostics
69
     * @return array of data
70
     */
71
    public function get_chamilo_data()
72
    {
73
        $array = array();
74
        $writable_folders = array(
75
            api_get_path(SYS_APP_PATH) .'cache',
76
            api_get_path(SYS_COURSE_PATH),
77
            api_get_path(SYS_APP_PATH) .'home',
78
            api_get_path(SYS_APP_PATH) .'upload/users/',
79
            api_get_path(SYS_PATH) .'main/default_course_document/images/',
80
        );
81
82
        foreach ($writable_folders as $index => $folder) {
83
            $writable = is_writable($folder);
84
            $status = $writable ? self :: STATUS_OK : self :: STATUS_ERROR;
85
            $array[] = $this->build_setting(
86
                $status,
87
                '[FILES]',
88
                get_lang('IsWritable').': '.$folder,
89
                'http://be2.php.net/manual/en/function.is-writable.php',
90
                $writable,
91
                1,
92
                'yes_no',
93
                get_lang('DirectoryMustBeWritable')
94
            );
95
        }
96
97
        $exists = file_exists(api_get_path(SYS_CODE_PATH).'install');
98
        $status = $exists ? self :: STATUS_WARNING : self :: STATUS_OK;
99
        $array[] = $this->build_setting(
100
            $status,
101
            '[FILES]',
102
            get_lang('DirectoryExists') . ': /install',
103
            'http://be2.php.net/file_exists',
104
            $exists,
105
            0,
106
            'yes_no', get_lang('DirectoryShouldBeRemoved')
107
        );
108
109
        $app_version = api_get_setting('chamilo_database_version');
110
111
        $array[] = $this->build_setting(
112
            self :: STATUS_INFORMATION,
113
            '[DB]',
114
            'chamilo_database_version',
115
            '#',
116
            $app_version,
117
            0,
118
            null,
119
            'Chamilo DB version'
120
        );
121
122
        $access_url_id = api_get_current_access_url_id();
123
124
        if ($access_url_id === 1) {
125
            global $_configuration;
126
            $message2 = '';
127
            if ($access_url_id === 1) {
128
                if (api_is_windows_os()) {
129
                    $message2 .= get_lang('SpaceUsedOnSystemCannotBeMeasuredOnWindows');
130
                } else {
131
                    $dir = api_get_path(SYS_PATH);
132
                    $du = exec('du -sh ' . $dir, $err);
133
                    list($size, $none) = explode("\t", $du);
0 ignored issues
show
Unused Code introduced by
The assignment to $none is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
134
                    $limit = $_configuration[$access_url_id]['hosting_limit_disk_space'];
135
                    $message2 .= sprintf(get_lang('TotalSpaceUsedByPortalXLimitIsYMB'), $size, $limit);
136
                }
137
            }
138
139
            $array[] = $this->build_setting(
140
                self :: STATUS_OK,
141
                '[FILES]',
142
                'hosting_limit_disk_space',
143
                '#',
144
                $size,
145
                0,
146
                null,
147
                $message2
148
            );
149
        }
150
151
152
153
154
        return $array;
155
    }
156
157
    /**
158
     * Functions to get the data for the php diagnostics
159
     * @return array of data
160
     */
161
    public function get_php_data()
162
    {
163
        $array = array();
164
165
        // General Functions
166
167
        $version = phpversion();
168
        $status = $version > REQUIRED_PHP_VERSION ? self :: STATUS_OK : self :: STATUS_ERROR;
169
        $array[] = $this->build_setting($status, '[PHP]', 'phpversion()', 'http://www.php.net/manual/en/function.phpversion.php', phpversion(), '>= '.REQUIRED_PHP_VERSION, null, get_lang('PHPVersionInfo'));
170
171
        $setting = ini_get('output_buffering');
172
        $req_setting = 1;
173
        $status = $setting >= $req_setting ? self :: STATUS_OK : self :: STATUS_ERROR;
174
        $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'));
175
176
        $setting = ini_get('file_uploads');
177
        $req_setting = 1;
178
        $status = $setting == $req_setting ? self :: STATUS_OK : self :: STATUS_ERROR;
179
        $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'));
180
181
        $setting = ini_get('magic_quotes_runtime');
182
        $req_setting = 0;
183
        $status = $setting == $req_setting ? self :: STATUS_OK : self :: STATUS_ERROR;
184
        $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'));
185
186
        $setting = ini_get('safe_mode');
187
        $req_setting = 0;
188
        $status = $setting == $req_setting ? self :: STATUS_OK : self :: STATUS_WARNING;
189
        $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'));
190
191
        $setting = ini_get('register_globals');
192
        $req_setting = 0;
193
        $status = $setting == $req_setting ? self :: STATUS_OK : self :: STATUS_ERROR;
194
        $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'));
195
196
        $setting = ini_get('short_open_tag');
197
        $req_setting = 0;
198
        $status = $setting == $req_setting ? self :: STATUS_OK : self :: STATUS_WARNING;
199
        $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'));
200
201
        $setting = ini_get('magic_quotes_gpc');
202
        $req_setting = 0;
203
        $status = $setting == $req_setting ? self :: STATUS_OK : self :: STATUS_ERROR;
204
        $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'));
205
206
        $setting = ini_get('display_errors');
207
        $req_setting = 0;
208
        $status = $setting == $req_setting ? self :: STATUS_OK : self :: STATUS_WARNING;
209
        $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'));
210
211
        $setting = ini_get('default_charset');
212
        if ($setting == '')
213
            $setting = null;
214
        $req_setting = null;
215
        $status = $setting == $req_setting ? self :: STATUS_OK : self :: STATUS_ERROR;
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $setting of type null|string against $req_setting of type null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
216
        $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'));
217
218
        $setting = ini_get('max_execution_time');
219
        $req_setting = '300 (' . get_lang('Minimum') . ')';
220
        $status = $setting >= 300 ? self :: STATUS_OK : self :: STATUS_WARNING;
221
        $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'));
222
223
        $setting = ini_get('max_input_time');
224
        $req_setting = '300 (' . get_lang('Minimum') . ')';
225
        $status = $setting >= 300 ? self :: STATUS_OK : self :: STATUS_WARNING;
226
        $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'));
227
228
        $setting = ini_get('memory_limit');
229
        $req_setting = '>= '.REQUIRED_MIN_MEMORY_LIMIT.'M';
230
        $status = self :: STATUS_ERROR;
231
        if ((float)$setting >= REQUIRED_MIN_MEMORY_LIMIT)
232
            $status = self :: STATUS_OK;
233
        $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'));
234
235
        $setting = ini_get('post_max_size');
236
        $req_setting = '>= '.REQUIRED_MIN_POST_MAX_SIZE.'M';
237
        $status = self :: STATUS_ERROR;
238
        if ((float)$setting >= REQUIRED_MIN_POST_MAX_SIZE)
239
            $status = self :: STATUS_OK;
240
        $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'));
241
242
        $setting = ini_get('upload_max_filesize');
243
        $req_setting = '>= '.REQUIRED_MIN_UPLOAD_MAX_FILESIZE.'M';
244
        $status = self :: STATUS_ERROR;
245
        if ((float)$setting >= REQUIRED_MIN_UPLOAD_MAX_FILESIZE)
246
            $status = self :: STATUS_OK;
247
        $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'));
248
249
        $setting = ini_get('variables_order');
250
        $req_setting = 'GPCS';
251
        $status = $setting == $req_setting ? self :: STATUS_OK : self :: STATUS_ERROR;
252
        $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'));
253
254
        $setting = ini_get('session.gc_maxlifetime');
255
        $req_setting = '4320';
256
        $status = $setting == $req_setting ? self :: STATUS_OK : self :: STATUS_WARNING;
257
        $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'));
258
259
        if (api_check_browscap()){$setting = true;}else{$setting=false;}
260
        $req_setting = true;
261
        $status = $setting == $req_setting ? self :: STATUS_OK : self :: STATUS_WARNING;
262
        $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'));
263
264
        // Extensions
265
        $extensions = array(
266
            'gd' => array(
267
                'link' => 'http://www.php.net/gd',
268
                'expected' => 1,
269
                'comment' => get_lang('ExtensionMustBeLoaded'),
270
            ),
271
            'mysql' => array(
272
                'link' => 'http://www.php.net/mysql',
273
                'expected' => 1,
274
                'comment' => get_lang('ExtensionMustBeLoaded'),
275
            ),
276
            'pcre' => array(
277
                'link' => 'http://www.php.net/pcre',
278
                'expected' => 1,
279
                'comment' => get_lang('ExtensionMustBeLoaded'),
280
            ),
281
            'session' => array(
282
                'link' => 'http://www.php.net/session',
283
                'expected' => 1,
284
                'comment' => get_lang('ExtensionMustBeLoaded'),
285
            ),
286
            'standard' => array(
287
                'link' => 'http://www.php.net/spl',
288
                'expected' => 1,
289
                'comment' => get_lang('ExtensionMustBeLoaded'),
290
            ),
291
            'zlib' => array(
292
                'link' => 'http://www.php.net/zlib',
293
                'expected' => 1,
294
                'comment' => get_lang('ExtensionMustBeLoaded'),
295
            ),
296
            'xsl' => array(
297
                'link' => 'http://be2.php.net/xsl',
298
                'expected' => 2,
299
                'comment' => get_lang('ExtensionShouldBeLoaded'),
300
            ),
301
            'curl' => array(
302
                'link' => 'http://www.php.net/curl',
303
                'expected' => 2,
304
                'comment' => get_lang('ExtensionShouldBeLoaded'),
305
            ),
306
        );
307
308
        foreach ($extensions as $extension => $data) {
309
            $url = $data['link'];
310
            $expected_value = $data['expected'];
311
            $comment = $data['comment'];
312
313
            $loaded = extension_loaded($extension);
314
            $status = $loaded ? self :: STATUS_OK : self :: STATUS_ERROR;
315
            $array[] = $this->build_setting($status, '[EXTENSION]', get_lang('LoadedExtension') . ': ' . $extension, $url, $loaded, $expected_value, 'yes_no_optional', $comment);
316
        }
317
318
        return $array;
319
    }
320
321
    /**
322
     * Functions to get the data for the mysql diagnostics
323
     * @return array of data
324
     */
325
    public function get_database_data()
326
    {
327
        $array = array();
328
        $em = Database::getManager();
329
        $connection = $em->getConnection();
330
        $host = $connection->getHost();
331
        $db = $connection->getDatabase();
332
        $port = $connection->getPort();
333
        $driver = $connection->getDriver()->getName();
334
335
        $array[] = $this->build_setting(
336
            self :: STATUS_INFORMATION,
337
            '[Database]',
338
            'driver',
339
            '',
340
            $driver, null, null, get_lang('Driver')
341
        );
342
343
        $array[] = $this->build_setting(
344
            self :: STATUS_INFORMATION,
345
            '[Database]',
346
            'host',
347
            '',
348
            $host, null, null, get_lang('MysqlHostInfo')
349
        );
350
351
        $array[] = $this->build_setting(
352
            self :: STATUS_INFORMATION,
353
            '[Database]',
354
            'port',
355
            '',
356
            $port, null, null, get_lang('Port')
357
        );
358
359
360
        $array[] = $this->build_setting(
361
            self :: STATUS_INFORMATION,
362
            '[Database]',
363
            'Database name',
364
            '',
365
            $db, null, null, get_lang('Name')
366
        );
367
368
        return $array;
369
    }
370
371
    /**
372
     * Functions to get the data for the webserver diagnostics
373
     * @return array of data
374
     */
375
    public function get_webserver_data()
376
    {
377
        $array = array();
378
379
        $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'));
380
381
        $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'));
382
383
        $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'));
384
385
        $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'));
386
387
        $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'));
388
389
        $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'));
390
391
        /*$path = $this->manager->get_url(array('section' => Request :: get('section')));
392
        $request = $_SERVER["REQUEST_URI"];
393
        $status = $request != $path ? self :: STATUS_ERROR : self :: STATUS_OK;
394
        $array[] = $this->build_setting($status, '[SERVER]', '$_SERVER["REQUEST_URI"]', 'http://be.php.net/reserved.variables.server', $request, $path, null, get_lang('RequestURIInfo'));
395
        */
396
        $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'));
397
398
        $array[] = $this->build_setting(self :: STATUS_INFORMATION, '[SERVER]', 'php_uname()', 'http://be2.php.net/php_uname', php_uname(), null, null, get_lang('UnameInfo'));
399
400
        return $array;
401
    }
402
403
    /**
404
     * Additional functions needed for fast integration
405
     */
406
    public function build_setting(
407
        $status,
408
        $section,
409
        $title,
410
        $url,
411
        $current_value,
412
        $expected_value,
413
        $formatter,
414
        $comment
415
    ) {
416
        switch ($status) {
417
            case self :: STATUS_OK :
418
                $img = 'bullet_green.png';
419
                break;
420
            case self :: STATUS_WARNING :
421
                $img = 'bullet_orange.png';
422
                break;
423
            case self :: STATUS_ERROR :
424
                $img = 'bullet_red.png';
425
                break;
426
            case self :: STATUS_INFORMATION :
427
                $img = 'bullet_blue.png';
428
                break;
429
        }
430
431
        $image = Display::return_icon($img, $status);
432
        $url = $this->get_link($title, $url);
433
434
        $formatted_current_value = $current_value;
435
        $formatted_expected_value = $expected_value;
436
437
        if ($formatter) {
438
            if (method_exists($this, 'format_' . $formatter)) {
439
                $formatted_current_value = call_user_func(array($this, 'format_' . $formatter), $current_value);
440
                $formatted_expected_value = call_user_func(array($this, 'format_' . $formatter), $expected_value);
441
            }
442
        }
443
444
        return array($image, $section, $url, $formatted_current_value, $formatted_expected_value, $comment);
445
    }
446
447
    /**
448
     * Create a link with a url and a title
449
     * @param $title
450
     * @param $url
451
     * @return string the url
452
     */
453
    public function get_link($title, $url)
454
    {
455
        return '<a href="' . $url . '" target="about:bank">' . $title . '</a>';
456
    }
457
458
    public function format_yes_no_optional($value)
459
    {
460
    	$return = '';
461
    	switch($value) {
462
     		case 0:
463
     			$return = get_lang('No');
464
     			break;
465
     		case 1:
466
     			$return = get_lang('Yes');
467
     			break;
468
			case 2:
469
				$return = get_lang('Optional');
470
				break;
471
    	}
472
    	return $return;
473
474
    }
475
476
    function format_yes_no($value)
477
    {
478
        return $value ? get_lang('Yes') : get_lang('No');
479
    }
480
481
    function format_on_off($value)
482
    {
483
        $value = intval($value);
484
        if ($value > 1) {
485
            // Greater than 1 values are shown "as-is", they may be interpreted as "On" later.
486
            return $value;
487
        }
488
        // These are the values 'On' and 'Off' used in the php-ini file. Translation (get_lang()) is not needed here.
489
        return $value ? 'On' : 'Off';
490
    }
491
}
492