Passed
Push — master ( 48fb1f...9a7ade )
by Yannick
14:18 queued 06:02
created

Diagnoser::get_webserver_data()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 96
Code Lines 83

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 83
nc 1
nop 0
dl 0
loc 96
rs 8.3709
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
 *
10
 * @author Ivan Tcholakov, 2008, initial proposal and sample code.
11
 * @author spou595, 2009, implementation for Chamilo 2.x
12
 * @author Julio Montoya <[email protected]>, 2010, port to chamilo 1.8.7, Some fixes
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
    /**
29
     * Show html table.
30
     */
31
    public function show_html()
32
    {
33
        $sections = [
34
            'chamilo' => [
35
                'lang' => 'Chamilo',
36
                'info' => 'State of Chamilo requirements',
37
            ],
38
            'php' => [
39
                'lang' => 'PHP',
40
                'info' => 'State of PHP settings on the server',
41
            ],
42
            'database' => [
43
                'lang' => 'Database',
44
                'info' => 'Configuration settings of the database server. To check the database consistency after an upgrade, if you have access to the command line, you can use "php bin/doctrine.php orm:schema-tool:update --dump-sql". This will print a list of database changes that should be applied to your system in order to get the right structure. Index name changes can be ignored. Use "--force" instead of "--dump" to try and execute them in order.',
45
            ],
46
            'webserver' => [
47
                'lang' => get_lang('WebServer'),
48
                'info' => 'Information about your webserver\'s configuration ',
49
            ],
50
            'paths' => [
51
                'lang' => 'Paths',
52
                'info' => 'The following paths are called by their constant throughout Chamilo\'s code using the api_get_path() function. Here is a list of these paths and what they would be translated to on this portal.',
53
            ],
54
        ];
55
        $currentSection = isset($_GET['section']) ? $_GET['section'] : '';
56
        if (!in_array(trim($currentSection), array_keys($sections))) {
57
            $currentSection = 'chamilo';
58
        }
59
60
        $html = '<div class="tabbable"><ul class="nav nav-tabs">';
61
62
        foreach ($sections as $section => $details) {
63
            if ($currentSection === $section) {
64
                $html .= '<li class="active">';
65
            } else {
66
                $html .= '<li>';
67
            }
68
            $params['section'] = $section;
69
            $html .= '<a href="system_status.php?section='.$section.'">'.$details['lang'].'</a></li>';
70
        }
71
72
        $html .= '</ul><div class="tab-pane">';
73
74
        $data = call_user_func([$this, 'get_'.$currentSection.'_data']);
75
        echo $html;
76
77
        if ($currentSection != 'paths') {
78
            echo '<br />';
79
            echo Display::return_message($sections[$currentSection]['info'], 'normal');
80
81
            $table = new SortableTableFromArray($data, 1, 100);
82
            $table->set_header(0, '', false);
83
            $table->set_header(1, get_lang('Section'), false);
84
            $table->set_header(2, get_lang('Setting'), false);
85
            $table->set_header(3, get_lang('Current'), false);
86
            $table->set_header(4, get_lang('Expected'), false);
87
            $table->set_header(5, get_lang('Comment'), false);
88
89
            $table->display();
90
        } else {
91
            echo '<br />';
92
            echo Display::return_message($sections[$currentSection]['info'], 'normal');
93
94
            $headers = $data['headers'];
95
            $results = $data['data'];
96
            $table = new HTML_Table(['class' => 'data_table']);
97
98
            $column = 0;
99
            foreach ($headers as $header) {
100
                $table->setHeaderContents(0, $column, $header);
101
                $column++;
102
            }
103
            $row = 1;
104
            foreach ($results as $index => $rowData) {
105
                $table->setCellContents(
106
                    $row,
107
                    0,
108
                    $rowData
109
                );
110
                $table->setCellContents(
111
                    $row,
112
                    1,
113
                    $index
114
                );
115
                $row++;
116
            }
117
118
            $table->display();
119
        }
120
        echo '</div></div>';
121
    }
122
123
    /**
124
     * @return array
125
     */
126
    public function get_paths_data()
127
    {
128
        global $paths;
129
        $list = $paths[api_get_path(WEB_PATH)];
130
        $list['url_append'] = api_get_configuration_value('url_append');
131
        asort($list);
132
133
        return [
134
            'headers' => ['Path', 'constant'],
135
            'data' => $list,
136
        ];
137
    }
138
139
    /**
140
     * Functions to get the data for the chamilo diagnostics.
141
     *
142
     * @return array of data
143
     */
144
    public function get_chamilo_data()
145
    {
146
        $array = [];
147
        $writable_folders = [
148
            api_get_path(SYS_APP_PATH).'cache',
149
            api_get_path(SYS_COURSE_PATH),
150
            api_get_path(SYS_APP_PATH).'upload/users/',
151
        ];
152
        foreach ($writable_folders as $index => $folder) {
153
            $writable = is_writable($folder);
154
            $status = $writable ? self::STATUS_OK : self::STATUS_ERROR;
155
            $array[] = $this->build_setting(
156
                $status,
157
                '[FILES]',
158
                get_lang('IsWritable').': '.$folder,
159
                'http://be2.php.net/manual/en/function.is-writable.php',
160
                $writable,
161
                1,
162
                'yes_no',
163
                get_lang('DirectoryMustBeWritable')
164
            );
165
        }
166
167
        $exists = file_exists(api_get_path(SYS_CODE_PATH).'install');
168
        $status = $exists ? self::STATUS_WARNING : self::STATUS_OK;
169
        $array[] = $this->build_setting(
170
            $status,
171
            '[FILES]',
172
            get_lang('DirectoryExists').': /install',
173
            'http://be2.php.net/file_exists',
174
            $exists,
175
            0,
176
            'yes_no',
177
            get_lang('DirectoryShouldBeRemoved')
178
        );
179
180
        $app_version = api_get_setting('chamilo_database_version');
181
        $array[] = $this->build_setting(
182
            self::STATUS_INFORMATION,
183
            '[DB]',
184
            'chamilo_database_version',
185
            '#',
186
            $app_version,
187
            0,
188
            null,
189
            'Chamilo DB version'
190
        );
191
192
        $access_url_id = api_get_current_access_url_id();
193
194
        if ($access_url_id === 1) {
195
            $size = '-';
196
            global $_configuration;
197
            $message2 = '';
198
            if ($access_url_id === 1) {
199
                if (api_is_windows_os()) {
200
                    $message2 .= get_lang('SpaceUsedOnSystemCannotBeMeasuredOnWindows');
201
                } else {
202
                    $dir = api_get_path(SYS_PATH);
203
                    $du = exec('du -sh '.$dir, $err);
204
                    list($size, $none) = explode("\t", $du);
205
                    unset($none);
206
                    $limit = 0;
207
                    if (isset($_configuration[$access_url_id])) {
208
                        if (isset($_configuration[$access_url_id]['hosting_limit_disk_space'])) {
209
                            $limit = $_configuration[$access_url_id]['hosting_limit_disk_space'];
210
                        }
211
                    }
212
                    $message2 .= sprintf(get_lang('TotalSpaceUsedByPortalXLimitIsYMB'), $size, $limit);
213
                }
214
            }
215
216
            $array[] = $this->build_setting(
217
                self::STATUS_OK,
218
                '[FILES]',
219
                'hosting_limit_disk_space',
220
                '#',
221
                $size,
222
                0,
223
                null,
224
                $message2
225
            );
226
        }
227
        $new_version = '-';
228
        $new_version_status = '';
229
        $file = api_get_path(SYS_CODE_PATH).'install/version.php';
230
        if (is_file($file)) {
231
            @include($file);
232
        }
233
        $array[] = $this->build_setting(
234
            self::STATUS_INFORMATION,
235
            '[CONFIG]',
236
            get_lang('VersionFromVersionFile'),
237
            '#',
238
            $new_version.' '.$new_version_status,
239
            '-',
240
            null,
241
            get_lang('TheVersionFromTheVersionFileIsUpdatedWithEachVersionIfMainInstallDirectoryIsPresent')
242
        );
243
        $array[] = $this->build_setting(
244
            self::STATUS_INFORMATION,
245
            '[CONFIG]',
246
            get_lang('VersionFromConfigFile'),
247
            '#',
248
            api_get_configuration_value('system_version'),
249
            $new_version,
250
            null,
251
            get_lang('TheVersionFromTheConfigurationFileShowsOnTheAdminPageButHasToBeChangedManuallyOnUpgrade')
252
        );
253
254
        return $array;
255
    }
256
257
    /**
258
     * Functions to get the data for the php diagnostics.
259
     *
260
     * @return array of data
261
     */
262
    public function get_php_data()
263
    {
264
        $array = [];
265
266
        // General Functions
267
268
        $version = phpversion();
269
        $status = $version > REQUIRED_PHP_VERSION ? self::STATUS_OK : self::STATUS_ERROR;
270
        $array[] = $this->build_setting(
271
            $status,
272
            '[PHP]',
273
            'phpversion()',
274
            'http://www.php.net/manual/en/function.phpversion.php',
275
            phpversion(),
276
            '>= '.REQUIRED_PHP_VERSION,
277
            null,
278
            get_lang('PHPVersionInfo')
279
        );
280
281
        $setting = ini_get('output_buffering');
282
        $req_setting = 1;
283
        $status = $setting >= $req_setting ? self::STATUS_OK : self::STATUS_ERROR;
284
        $array[] = $this->build_setting(
285
            $status,
286
            '[INI]',
287
            'output_buffering',
288
            'http://www.php.net/manual/en/outcontrol.configuration.php#ini.output-buffering',
289
            $setting,
290
            $req_setting,
291
            'on_off',
292
            get_lang('OutputBufferingInfo')
293
        );
294
295
        $setting = ini_get('file_uploads');
296
        $req_setting = 1;
297
        $status = $setting == $req_setting ? self::STATUS_OK : self::STATUS_ERROR;
298
        $array[] = $this->build_setting(
299
            $status,
300
            '[INI]',
301
            'file_uploads',
302
            'http://www.php.net/manual/en/ini.core.php#ini.file-uploads',
303
            $setting,
304
            $req_setting,
305
            'on_off',
306
            get_lang('FileUploadsInfo')
307
        );
308
309
        $setting = ini_get('magic_quotes_runtime');
310
        $req_setting = 0;
311
        $status = $setting == $req_setting ? self::STATUS_OK : self::STATUS_ERROR;
312
        $array[] = $this->build_setting(
313
            $status,
314
            '[INI]',
315
            'magic_quotes_runtime',
316
            'http://www.php.net/manual/en/ini.core.php#ini.magic-quotes-runtime',
317
            $setting,
318
            $req_setting,
319
            'on_off',
320
            get_lang('MagicQuotesRuntimeInfo')
321
        );
322
323
        $setting = ini_get('safe_mode');
324
        $req_setting = 0;
325
        $status = $setting == $req_setting ? self::STATUS_OK : self::STATUS_WARNING;
326
        $array[] = $this->build_setting(
327
            $status,
328
            '[INI]',
329
            'safe_mode',
330
            'http://www.php.net/manual/en/ini.core.php#ini.safe-mode',
331
            $setting,
332
            $req_setting,
333
            'on_off',
334
            get_lang('SafeModeInfo')
335
        );
336
337
        $setting = ini_get('register_globals');
338
        $req_setting = 0;
339
        $status = $setting == $req_setting ? self::STATUS_OK : self::STATUS_ERROR;
340
        $array[] = $this->build_setting(
341
            $status,
342
            '[INI]',
343
            'register_globals',
344
            'http://www.php.net/manual/en/ini.core.php#ini.register-globals',
345
            $setting,
346
            $req_setting,
347
            'on_off',
348
            get_lang('RegisterGlobalsInfo')
349
        );
350
351
        $setting = ini_get('short_open_tag');
352
        $req_setting = 0;
353
        $status = $setting == $req_setting ? self::STATUS_OK : self::STATUS_WARNING;
354
        $array[] = $this->build_setting(
355
            $status,
356
            '[INI]',
357
            'short_open_tag',
358
            'http://www.php.net/manual/en/ini.core.php#ini.short-open-tag',
359
            $setting,
360
            $req_setting,
361
            'on_off',
362
            get_lang('ShortOpenTagInfo')
363
        );
364
365
        $setting = ini_get('magic_quotes_gpc');
366
        $req_setting = 0;
367
        $status = $setting == $req_setting ? self::STATUS_OK : self::STATUS_ERROR;
368
        $array[] = $this->build_setting(
369
            $status,
370
            '[INI]',
371
            'magic_quotes_gpc',
372
            'http://www.php.net/manual/en/ini.core.php#ini.magic_quotes_gpc',
373
            $setting,
374
            $req_setting,
375
            'on_off',
376
            get_lang('MagicQuotesGpcInfo')
377
        );
378
379
        $setting = ini_get('display_errors');
380
        $req_setting = 0;
381
        $status = $setting == $req_setting ? self::STATUS_OK : self::STATUS_WARNING;
382
        $array[] = $this->build_setting(
383
            $status,
384
            '[INI]',
385
            'display_errors',
386
            'http://www.php.net/manual/en/ini.core.php#ini.display_errors',
387
            $setting,
388
            $req_setting,
389
            'on_off',
390
            get_lang('DisplayErrorsInfo')
391
        );
392
393
        $setting = ini_get('default_charset');
394
        if ($setting == '') {
395
            $setting = null;
396
        }
397
        $req_setting = 'UTF-8';
398
        $status = $setting == $req_setting ? self::STATUS_OK : self::STATUS_ERROR;
399
        $array[] = $this->build_setting(
400
            $status,
401
            '[INI]',
402
            'default_charset',
403
            'http://www.php.net/manual/en/ini.core.php#ini.default-charset',
404
            $setting,
405
            $req_setting,
406
            null,
407
            get_lang('DefaultCharsetInfo')
408
        );
409
410
        $setting = ini_get('max_execution_time');
411
        $req_setting = '300 ('.get_lang('Minimum').')';
412
        $status = $setting >= 300 ? self::STATUS_OK : self::STATUS_WARNING;
413
        $array[] = $this->build_setting(
414
            $status,
415
            '[INI]',
416
            'max_execution_time',
417
            'http://www.php.net/manual/en/ini.core.php#ini.max-execution-time',
418
            $setting,
419
            $req_setting,
420
            null,
421
            get_lang('MaxExecutionTimeInfo')
422
        );
423
424
        $setting = ini_get('max_input_time');
425
        $req_setting = '300 ('.get_lang('Minimum').')';
426
        $status = $setting >= 300 ? self::STATUS_OK : self::STATUS_WARNING;
427
        $array[] = $this->build_setting(
428
            $status,
429
            '[INI]',
430
            'max_input_time',
431
            'http://www.php.net/manual/en/ini.core.php#ini.max-input-time',
432
            $setting,
433
            $req_setting,
434
            null,
435
            get_lang('MaxInputTimeInfo')
436
        );
437
438
        $setting = ini_get('memory_limit');
439
        $req_setting = '>= '.REQUIRED_MIN_MEMORY_LIMIT.'M';
440
        $status = self::STATUS_ERROR;
441
        if ((float) $setting >= REQUIRED_MIN_MEMORY_LIMIT) {
442
            $status = self::STATUS_OK;
443
        }
444
        $array[] = $this->build_setting(
445
            $status,
446
            '[INI]',
447
            'memory_limit',
448
            'http://www.php.net/manual/en/ini.core.php#ini.memory-limit',
449
            $setting,
450
            $req_setting,
451
            null,
452
            get_lang('MemoryLimitInfo')
453
        );
454
455
        $setting = ini_get('post_max_size');
456
        $req_setting = '>= '.REQUIRED_MIN_POST_MAX_SIZE.'M';
457
        $status = self::STATUS_ERROR;
458
        if ((float) $setting >= REQUIRED_MIN_POST_MAX_SIZE) {
459
            $status = self::STATUS_OK;
460
        }
461
        $array[] = $this->build_setting(
462
            $status,
463
            '[INI]',
464
            'post_max_size',
465
            'http://www.php.net/manual/en/ini.core.php#ini.post-max-size',
466
            $setting,
467
            $req_setting,
468
            null,
469
            get_lang('PostMaxSizeInfo')
470
        );
471
472
        $setting = ini_get('upload_max_filesize');
473
        $req_setting = '>= '.REQUIRED_MIN_UPLOAD_MAX_FILESIZE.'M';
474
        $status = self::STATUS_ERROR;
475
        if ((float) $setting >= REQUIRED_MIN_UPLOAD_MAX_FILESIZE) {
476
            $status = self::STATUS_OK;
477
        }
478
        $array[] = $this->build_setting(
479
            $status,
480
            '[INI]',
481
            'upload_max_filesize',
482
            'http://www.php.net/manual/en/ini.core.php#ini.upload_max_filesize',
483
            $setting,
484
            $req_setting,
485
            null,
486
            get_lang('UploadMaxFilesizeInfo')
487
        );
488
489
        $setting = ini_get('upload_tmp_dir');
490
        $status = self::STATUS_OK;
491
        $array[] = $this->build_setting(
492
            $status,
493
            '[INI]',
494
            'upload_tmp_dir',
495
            'http://www.php.net/manual/en/ini.core.php#ini.upload_tmp_dir',
496
            $setting,
497
            '',
498
            null,
499
            get_lang('UploadTmpDirInfo')
500
        );
501
502
        $setting = ini_get('variables_order');
503
        $req_setting = 'GPCS';
504
        $status = $setting == $req_setting ? self::STATUS_OK : self::STATUS_ERROR;
505
        $array[] = $this->build_setting(
506
            $status,
507
            '[INI]',
508
            'variables_order',
509
            'http://www.php.net/manual/en/ini.core.php#ini.variables-order',
510
            $setting,
511
            $req_setting,
512
            null,
513
            get_lang('VariablesOrderInfo')
514
        );
515
516
        $setting = ini_get('session.gc_maxlifetime');
517
        $req_setting = '4320';
518
        $status = $setting == $req_setting ? self::STATUS_OK : self::STATUS_WARNING;
519
        $array[] = $this->build_setting(
520
            $status,
521
            '[SESSION]',
522
            'session.gc_maxlifetime',
523
            'http://www.php.net/manual/en/ini.core.php#session.gc-maxlifetime',
524
            $setting,
525
            $req_setting,
526
            null,
527
            get_lang('SessionGCMaxLifetimeInfo')
528
        );
529
530
        if (api_check_browscap()) {
531
            $setting = true;
532
        } else {
533
            $setting = false;
534
        }
535
        $req_setting = true;
536
        $status = $setting == $req_setting ? self::STATUS_OK : self::STATUS_WARNING;
537
        $array[] = $this->build_setting(
538
            $status,
539
            '[INI]',
540
            'browscap',
541
            'http://www.php.net/manual/en/misc.configuration.php#ini.browscap',
542
            $setting,
543
            $req_setting,
544
            'on_off',
545
            get_lang('BrowscapInfo')
546
        );
547
548
        // Extensions
549
        $extensions = [
550
            'gd' => [
551
                'link' => 'http://www.php.net/gd',
552
                'expected' => 1,
553
                'comment' => get_lang('ExtensionMustBeLoaded'),
554
            ],
555
            'pdo_mysql' => [
556
                'link' => 'http://php.net/manual/en/ref.pdo-mysql.php',
557
                'expected' => 1,
558
                'comment' => get_lang('ExtensionMustBeLoaded'),
559
            ],
560
            'pcre' => [
561
                'link' => 'http://www.php.net/pcre',
562
                'expected' => 1,
563
                'comment' => get_lang('ExtensionMustBeLoaded'),
564
            ],
565
            'session' => [
566
                'link' => 'http://www.php.net/session',
567
                'expected' => 1,
568
                'comment' => get_lang('ExtensionMustBeLoaded'),
569
            ],
570
            'standard' => [
571
                'link' => 'http://www.php.net/spl',
572
                'expected' => 1,
573
                'comment' => get_lang('ExtensionMustBeLoaded'),
574
            ],
575
            'zlib' => [
576
                'link' => 'http://www.php.net/zlib',
577
                'expected' => 1,
578
                'comment' => get_lang('ExtensionMustBeLoaded'),
579
            ],
580
            'xsl' => [
581
                'link' => 'http://be2.php.net/xsl',
582
                'expected' => 2,
583
                'comment' => get_lang('ExtensionShouldBeLoaded'),
584
            ],
585
            'curl' => [
586
                'link' => 'http://www.php.net/curl',
587
                'expected' => 2,
588
                'comment' => get_lang('ExtensionShouldBeLoaded'),
589
            ],
590
            'Zend OPcache' => [
591
                'link' => 'http://www.php.net/opcache',
592
                'expected' => 2,
593
                'comment' => get_lang('ExtensionShouldBeLoaded'),
594
            ],
595
            'apcu' => [
596
                'link' => 'http://www.php.net/apcu',
597
                'expected' => 2,
598
                'comment' => get_lang('ExtensionShouldBeLoaded'),
599
            ],
600
        ];
601
602
        foreach ($extensions as $extension => $data) {
603
            $url = $data['link'];
604
            $expected_value = $data['expected'];
605
            $comment = $data['comment'];
606
607
            $loaded = extension_loaded($extension);
608
            $status = $loaded ? self::STATUS_OK : self::STATUS_ERROR;
609
            $array[] = $this->build_setting(
610
                $status,
611
                '[EXTENSION]',
612
                get_lang('LoadedExtension').': '.$extension,
613
                $url,
614
                $loaded,
615
                $expected_value,
616
                'yes_no_optional',
617
                $comment
618
            );
619
        }
620
621
        return $array;
622
    }
623
624
    /**
625
     * Functions to get the data for the mysql diagnostics.
626
     *
627
     * @return array of data
628
     */
629
    public function get_database_data()
630
    {
631
        $array = [];
632
        $em = Database::getManager();
633
        $connection = $em->getConnection();
634
        $host = $connection->getHost();
635
        $db = $connection->getDatabase();
636
        $port = $connection->getPort();
637
        $driver = $connection->getDriver()->getName();
638
639
        $array[] = $this->build_setting(
640
            self::STATUS_INFORMATION,
641
            '[Database]',
642
            'driver',
643
            '',
644
            $driver,
645
            null,
646
            null,
647
            get_lang('Driver')
648
        );
649
650
        $array[] = $this->build_setting(
651
            self::STATUS_INFORMATION,
652
            '[Database]',
653
            'host',
654
            '',
655
            $host,
656
            null,
657
            null,
658
            get_lang('MysqlHostInfo')
659
        );
660
661
        $array[] = $this->build_setting(
662
            self::STATUS_INFORMATION,
663
            '[Database]',
664
            'port',
665
            '',
666
            $port,
667
            null,
668
            null,
669
            get_lang('Port')
670
        );
671
672
        $array[] = $this->build_setting(
673
            self::STATUS_INFORMATION,
674
            '[Database]',
675
            'Database name',
676
            '',
677
            $db,
678
            null,
679
            null,
680
            get_lang('Name')
681
        );
682
683
        return $array;
684
    }
685
686
    /**
687
     * Functions to get the data for the webserver diagnostics.
688
     *
689
     * @return array of data
690
     */
691
    public function get_webserver_data()
692
    {
693
        $array = [];
694
695
        $array[] = $this->build_setting(
696
            self::STATUS_INFORMATION,
697
            '[SERVER]',
698
            '$_SERVER["SERVER_NAME"]',
699
            'http://be.php.net/reserved.variables.server',
700
            $_SERVER["SERVER_NAME"],
701
            null,
702
            null,
703
            get_lang('ServerNameInfo')
704
        );
705
        $array[] = $this->build_setting(
706
            self::STATUS_INFORMATION,
707
            '[SERVER]',
708
            '$_SERVER["SERVER_ADDR"]',
709
            'http://be.php.net/reserved.variables.server',
710
            $_SERVER["SERVER_ADDR"],
711
            null,
712
            null,
713
            get_lang('ServerAddessInfo')
714
        );
715
        $array[] = $this->build_setting(
716
            self::STATUS_INFORMATION,
717
            '[SERVER]',
718
            '$_SERVER["SERVER_PORT"]',
719
            'http://be.php.net/reserved.variables.server',
720
            $_SERVER["SERVER_PORT"],
721
            null,
722
            null,
723
            get_lang('ServerPortInfo')
724
        );
725
        $array[] = $this->build_setting(
726
            self::STATUS_INFORMATION,
727
            '[SERVER]',
728
            '$_SERVER["SERVER_SOFTWARE"]',
729
            'http://be.php.net/reserved.variables.server',
730
            $_SERVER["SERVER_SOFTWARE"],
731
            null,
732
            null,
733
            get_lang('ServerSoftwareInfo')
734
        );
735
        $array[] = $this->build_setting(
736
            self::STATUS_INFORMATION,
737
            '[SERVER]',
738
            '$_SERVER["REMOTE_ADDR"]',
739
            'http://be.php.net/reserved.variables.server',
740
            $_SERVER["REMOTE_ADDR"],
741
            null,
742
            null,
743
            get_lang('ServerRemoteInfo')
744
        );
745
        $array[] = $this->build_setting(
746
            self::STATUS_INFORMATION,
747
            '[SERVER]',
748
            '$_SERVER["HTTP_USER_AGENT"]',
749
            'http://be.php.net/reserved.variables.server',
750
            $_SERVER["HTTP_USER_AGENT"],
751
            null,
752
            null,
753
            get_lang('ServerUserAgentInfo')
754
        );
755
        $array[] = $this->build_setting(
756
            self::STATUS_INFORMATION,
757
            '[SERVER]',
758
            '$_SERVER["SERVER_PROTOCOL"]',
759
            'http://be.php.net/reserved.variables.server',
760
            $_SERVER["SERVER_PROTOCOL"],
761
            null,
762
            null,
763
            get_lang('ServerProtocolInfo')
764
        );
765
        $array[] = $this->build_setting(
766
            self::STATUS_INFORMATION,
767
            '[SERVER]',
768
            'php_uname()',
769
            'http://be2.php.net/php_uname',
770
            php_uname(),
771
            null,
772
            null,
773
            get_lang('UnameInfo')
774
        );
775
        $array[] = $this->build_setting(
776
            self::STATUS_INFORMATION,
777
            '[SERVER]',
778
            '$_SERVER["HTTP_X_FORWARDED_FOR"]',
779
            'http://be.php.net/reserved.variables.server',
780
            (!empty($_SERVER["HTTP_X_FORWARDED_FOR"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : ''),
781
            null,
782
            null,
783
            get_lang('ServerXForwardedForInfo')
784
        );
785
786
        return $array;
787
    }
788
789
    /**
790
     * Additional functions needed for fast integration.
791
     * @param   int   $status Status constant defining which icon to use to illustrate the info
792
     * @param   string  $section    The name of the section this setting is included in
793
     * @param   string  $title  The name of the setting (usually a translated string)
794
     * @param   string  $url    A URL to point the user to regarding this setting, or '#' otherwise
795
     * @param   mixed   $current_value   The current value for this setting
796
     * @param   mixed   $expected_value  The expected value for this setting
797
     * @param   string  $formatter  If this setting is expressed in some kind of format, which format to use
798
     * @param   string  $comment    A translated string explaining what this setting represents
799
     * @return array    A list of elements to show in an array's row
800
     */
801
    public function build_setting(
802
        $status,
803
        $section,
804
        $title,
805
        $url,
806
        $current_value,
807
        $expected_value,
808
        $formatter,
809
        $comment
810
    ) {
811
        switch ($status) {
812
            case self::STATUS_OK:
813
                $img = 'bullet_green.png';
814
                break;
815
            case self::STATUS_WARNING:
816
                $img = 'bullet_orange.png';
817
                break;
818
            case self::STATUS_ERROR:
819
                $img = 'bullet_red.png';
820
                break;
821
            case self::STATUS_INFORMATION:
822
            default:
823
                $img = 'bullet_blue.png';
824
                break;
825
        }
826
827
        $image = Display::return_icon($img, $status);
828
        $url = $this->get_link($title, $url);
829
830
        $formatted_current_value = $current_value;
831
        $formatted_expected_value = $expected_value;
832
833
        if ($formatter) {
834
            if (method_exists($this, 'format_'.$formatter)) {
835
                $formatted_current_value = call_user_func([$this, 'format_'.$formatter], $current_value);
836
                $formatted_expected_value = call_user_func([$this, 'format_'.$formatter], $expected_value);
837
            }
838
        }
839
840
        return [$image, $section, $url, $formatted_current_value, $formatted_expected_value, $comment];
841
    }
842
843
    /**
844
     * Create a link with a url and a title.
845
     *
846
     * @param $title
847
     * @param $url
848
     *
849
     * @return string the url
850
     */
851
    public function get_link($title, $url)
852
    {
853
        return '<a href="'.$url.'" target="about:bank">'.$title.'</a>';
854
    }
855
856
    /**
857
     * @param int $value
858
     *
859
     * @return string
860
     */
861
    public function format_yes_no_optional($value)
862
    {
863
        $return = '';
864
        switch ($value) {
865
            case 0:
866
                $return = get_lang('No');
867
                break;
868
            case 1:
869
                $return = get_lang('Yes');
870
                break;
871
            case 2:
872
                $return = get_lang('Optional');
873
                break;
874
        }
875
876
        return $return;
877
    }
878
879
    /**
880
     * @param $value
881
     *
882
     * @return string
883
     */
884
    public function format_yes_no($value)
885
    {
886
        return $value ? get_lang('Yes') : get_lang('No');
887
    }
888
889
    /**
890
     * @param int $value
891
     *
892
     * @return string
893
     */
894
    public function format_on_off($value)
895
    {
896
        $value = intval($value);
897
        if ($value > 1) {
898
            // Greater than 1 values are shown "as-is", they may be interpreted as "On" later.
899
            return $value;
900
        }
901
        // These are the values 'On' and 'Off' used in the php-ini file. Translation (get_lang()) is not needed here.
902
        return $value ? 'On' : 'Off';
903
    }
904
}
905