Passed
Push — 1.11.x ( 11cb86...9ffdbf )
by
unknown
10:06
created

Diagnoser::get_courses_space_data()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 49
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 49
rs 8.9848
cc 5
nc 8
nop 0
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
    public const STATUS_OK = 1;
17
    public const STATUS_WARNING = 2;
18
    public const STATUS_ERROR = 3;
19
    public 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
            'courses_space' => [
55
                'lang' => 'Courses space',
56
                'info' => 'Information about space used by courses on disk. The space used on disk represents the total space used, whereas the quota only limits files in the documents tool. Only 1000 courses are shown, by order of last access and alphabetical code order. For more, please go to the courses folder and use "du -sh *" to get the size of the courses.',
57
            ],
58
        ];
59
        $currentSection = isset($_GET['section']) ? $_GET['section'] : '';
60
        if (!in_array(trim($currentSection), array_keys($sections))) {
61
            $currentSection = 'chamilo';
62
        }
63
64
        $html = '<div class="tabbable"><ul class="nav nav-tabs">';
65
66
        foreach ($sections as $section => $details) {
67
            if ($currentSection === $section) {
68
                $html .= '<li class="active">';
69
            } else {
70
                $html .= '<li>';
71
            }
72
            $params['section'] = $section;
73
            $html .= '<a href="system_status.php?section='.$section.'">'.$details['lang'].'</a></li>';
74
        }
75
76
        $html .= '</ul><div class="tab-pane">';
77
78
        $data = call_user_func([$this, 'get_'.$currentSection.'_data']);
79
        echo $html;
80
81
        if ($currentSection == 'paths') {
82
            echo '<br />';
83
            echo Display::return_message($sections[$currentSection]['info'], 'normal');
84
85
            $headers = $data['headers'];
86
            $results = $data['data'];
87
            $table = new HTML_Table(['class' => 'table table-hover table-striped data_table']);
88
89
            $column = 0;
90
            foreach ($headers as $header) {
91
                $table->setHeaderContents(0, $column, $header);
92
                $column++;
93
            }
94
            $row = 1;
95
            foreach ($results as $index => $rowData) {
96
                $table->setCellContents(
97
                    $row,
98
                    0,
99
                    $rowData
100
                );
101
                $table->setCellContents(
102
                    $row,
103
                    1,
104
                    $index
105
                );
106
                $row++;
107
            }
108
109
            $table->display();
110
        } elseif ($currentSection == 'courses_space') {
111
            echo '<br />';
112
            echo Display::return_message($sections[$currentSection]['info'], 'normal');
113
114
            $table = new SortableTableFromArray($data, 1, 1000);
115
            $table->set_additional_parameters(['section' => 'courses_space']);
116
            $table->set_header(0, '', false);
117
            $table->set_header(1, get_lang('CourseCode'), true);
118
            $table->set_header(2, 'Space used on disk (MB)', true);
119
            $table->set_header(3, 'Set max course space (MB)', false);
120
            $table->set_header(4, get_lang('Edit'), false);
121
            $table->set_header(5, get_lang('LastVisit'), true);
122
            $table->set_header(6, get_lang('CurrentDirectory'), false);
123
124
            $table->display();
125
        } else {
126
            echo '<br />';
127
            echo Display::return_message($sections[$currentSection]['info'], 'normal');
128
129
            $table = new SortableTableFromArray($data, 1, 100);
130
            $table->set_header(0, '', false);
131
            $table->set_header(1, get_lang('Section'), false);
132
            $table->set_header(2, get_lang('Setting'), false);
133
            $table->set_header(3, get_lang('Current'), false);
134
            $table->set_header(4, get_lang('Expected'), false);
135
            $table->set_header(5, get_lang('Comment'), false);
136
137
            $table->display();
138
        }
139
        echo '</div></div>';
140
    }
141
142
    /**
143
     * @return array
144
     */
145
    public function get_paths_data()
146
    {
147
        global $paths;
148
        $list = $paths[api_get_path(WEB_PATH)];
149
        $list['url_append'] = api_get_configuration_value('url_append');
150
        asort($list);
151
152
        return [
153
            'headers' => ['Path', 'constant'],
154
            'data' => $list,
155
        ];
156
    }
157
158
    /**
159
     * Functions to get the data for the chamilo diagnostics.
160
     *
161
     * @return array of data
162
     */
163
    public function get_chamilo_data()
164
    {
165
        $array = [];
166
        $writable_folders = [
167
            api_get_path(SYS_APP_PATH).'cache',
168
            api_get_path(SYS_COURSE_PATH),
169
            api_get_path(SYS_APP_PATH).'home',
170
            api_get_path(SYS_APP_PATH).'upload/users/',
171
            api_get_path(SYS_PATH).'main/default_course_document/images/',
172
        ];
173
        foreach ($writable_folders as $index => $folder) {
174
            $writable = is_writable($folder);
175
            $status = $writable ? self::STATUS_OK : self::STATUS_ERROR;
176
            $array[] = $this->build_setting(
177
                $status,
178
                '[FILES]',
179
                get_lang('IsWritable').': '.$folder,
180
                'https://php.net/manual/en/function.is-writable.php',
181
                $writable,
182
                1,
183
                'yes_no',
184
                get_lang('DirectoryMustBeWritable')
185
            );
186
        }
187
188
        $exists = file_exists(api_get_path(SYS_CODE_PATH).'install');
189
        $status = $exists ? self::STATUS_WARNING : self::STATUS_OK;
190
        $array[] = $this->build_setting(
191
            $status,
192
            '[FILES]',
193
            get_lang('DirectoryExists').': /install',
194
            'https://php.net/file_exists',
195
            $exists,
196
            0,
197
            'yes_no',
198
            get_lang('DirectoryShouldBeRemoved')
199
        );
200
201
        $app_version = api_get_setting('chamilo_database_version');
202
        $array[] = $this->build_setting(
203
            self::STATUS_INFORMATION,
204
            '[DB]',
205
            'chamilo_database_version',
206
            '#',
207
            $app_version,
208
            0,
209
            null,
210
            'Chamilo DB version'
211
        );
212
213
        $access_url_id = api_get_current_access_url_id();
214
215
        if ($access_url_id === 1) {
216
            $size = '-';
217
            global $_configuration;
218
            $message2 = '';
219
            if ($access_url_id === 1) {
220
                if (api_is_windows_os()) {
221
                    $message2 .= get_lang('SpaceUsedOnSystemCannotBeMeasuredOnWindows');
222
                } else {
223
                    $dir = api_get_path(SYS_PATH);
224
                    $du = exec('du -sh '.$dir, $err);
225
                    list($size, $none) = explode("\t", $du);
226
                    unset($none);
227
                    $limit = 0;
228
                    if (isset($_configuration[$access_url_id])) {
229
                        if (isset($_configuration[$access_url_id]['hosting_limit_disk_space'])) {
230
                            $limit = $_configuration[$access_url_id]['hosting_limit_disk_space'];
231
                        }
232
                    }
233
                    $message2 .= sprintf(get_lang('TotalSpaceUsedByPortalXLimitIsYMB'), $size, $limit);
234
                }
235
            }
236
237
            $array[] = $this->build_setting(
238
                self::STATUS_OK,
239
                '[FILES]',
240
                'hosting_limit_disk_space',
241
                '#',
242
                $size,
243
                0,
244
                null,
245
                $message2
246
            );
247
        }
248
        $new_version = '-';
249
        $new_version_status = '';
250
        $file = api_get_path(SYS_CODE_PATH).'install/version.php';
251
        if (is_file($file)) {
252
            @include $file;
253
        }
254
        $array[] = $this->build_setting(
255
            self::STATUS_INFORMATION,
256
            '[CONFIG]',
257
            get_lang('VersionFromVersionFile'),
258
            '#',
259
            $new_version.' '.$new_version_status,
260
            '-',
261
            null,
262
            get_lang('TheVersionFromTheVersionFileIsUpdatedWithEachVersionIfMainInstallDirectoryIsPresent')
263
        );
264
        $array[] = $this->build_setting(
265
            self::STATUS_INFORMATION,
266
            '[CONFIG]',
267
            get_lang('VersionFromConfigFile'),
268
            '#',
269
            api_get_configuration_value('system_version'),
270
            $new_version,
271
            null,
272
            get_lang('TheVersionFromTheConfigurationFileShowsOnTheAdminPageButHasToBeChangedManuallyOnUpgrade')
273
        );
274
275
        return $array;
276
    }
277
278
    /**
279
     * Functions to get the data for the php diagnostics.
280
     *
281
     * @return array of data
282
     */
283
    public function get_php_data()
284
    {
285
        $array = [];
286
287
        // General Functions
288
289
        $version = phpversion();
290
        $status = $version > REQUIRED_PHP_VERSION ? self::STATUS_OK : self::STATUS_ERROR;
291
        $array[] = $this->build_setting(
292
            $status,
293
            '[PHP]',
294
            'phpversion()',
295
            'https://php.net/manual/en/function.phpversion.php',
296
            phpversion(),
297
            '>= '.REQUIRED_PHP_VERSION,
298
            null,
299
            get_lang('PHPVersionInfo')
300
        );
301
302
        $setting = ini_get('output_buffering');
303
        $req_setting = 1;
304
        $status = $setting >= $req_setting ? self::STATUS_OK : self::STATUS_ERROR;
305
        $array[] = $this->build_setting(
306
            $status,
307
            '[INI]',
308
            'output_buffering',
309
            'https://php.net/manual/en/outcontrol.configuration.php#ini.output-buffering',
310
            $setting,
311
            $req_setting,
312
            'on_off',
313
            get_lang('OutputBufferingInfo')
314
        );
315
316
        $setting = ini_get('file_uploads');
317
        $req_setting = 1;
318
        $status = $setting == $req_setting ? self::STATUS_OK : self::STATUS_ERROR;
319
        $array[] = $this->build_setting(
320
            $status,
321
            '[INI]',
322
            'file_uploads',
323
            'https://php.net/manual/en/ini.core.php#ini.file-uploads',
324
            $setting,
325
            $req_setting,
326
            'on_off',
327
            get_lang('FileUploadsInfo')
328
        );
329
330
        $setting = ini_get('magic_quotes_runtime');
331
        $req_setting = 0;
332
        $status = $setting == $req_setting ? self::STATUS_OK : self::STATUS_ERROR;
333
        $array[] = $this->build_setting(
334
            $status,
335
            '[INI]',
336
            'magic_quotes_runtime',
337
            'https://php.net/manual/en/ini.core.php#ini.magic-quotes-runtime',
338
            $setting,
339
            $req_setting,
340
            'on_off',
341
            get_lang('MagicQuotesRuntimeInfo')
342
        );
343
344
        $setting = ini_get('safe_mode');
345
        $req_setting = 0;
346
        $status = $setting == $req_setting ? self::STATUS_OK : self::STATUS_WARNING;
347
        $array[] = $this->build_setting(
348
            $status,
349
            '[INI]',
350
            'safe_mode',
351
            'https://php.net/manual/en/ini.core.php#ini.safe-mode',
352
            $setting,
353
            $req_setting,
354
            'on_off',
355
            get_lang('SafeModeInfo')
356
        );
357
358
        $setting = ini_get('register_globals');
359
        $req_setting = 0;
360
        $status = $setting == $req_setting ? self::STATUS_OK : self::STATUS_ERROR;
361
        $array[] = $this->build_setting(
362
            $status,
363
            '[INI]',
364
            'register_globals',
365
            'https://php.net/manual/en/ini.core.php#ini.register-globals',
366
            $setting,
367
            $req_setting,
368
            'on_off',
369
            get_lang('RegisterGlobalsInfo')
370
        );
371
372
        $setting = ini_get('short_open_tag');
373
        $req_setting = 0;
374
        $status = $setting == $req_setting ? self::STATUS_OK : self::STATUS_WARNING;
375
        $array[] = $this->build_setting(
376
            $status,
377
            '[INI]',
378
            'short_open_tag',
379
            'https://php.net/manual/en/ini.core.php#ini.short-open-tag',
380
            $setting,
381
            $req_setting,
382
            'on_off',
383
            get_lang('ShortOpenTagInfo')
384
        );
385
386
        $setting = ini_get('magic_quotes_gpc');
387
        $req_setting = 0;
388
        $status = $setting == $req_setting ? self::STATUS_OK : self::STATUS_ERROR;
389
        $array[] = $this->build_setting(
390
            $status,
391
            '[INI]',
392
            'magic_quotes_gpc',
393
            'https://php.net/manual/en/ini.core.php#ini.magic_quotes_gpc',
394
            $setting,
395
            $req_setting,
396
            'on_off',
397
            get_lang('MagicQuotesGpcInfo')
398
        );
399
400
        $setting = ini_get('display_errors');
401
        $req_setting = 0;
402
        $status = $setting == $req_setting ? self::STATUS_OK : self::STATUS_WARNING;
403
        $array[] = $this->build_setting(
404
            $status,
405
            '[INI]',
406
            'display_errors',
407
            'https://php.net/manual/en/ini.core.php#ini.display_errors',
408
            $setting,
409
            $req_setting,
410
            'on_off',
411
            get_lang('DisplayErrorsInfo')
412
        );
413
414
        $setting = ini_get('default_charset');
415
        if ($setting == '') {
416
            $setting = null;
417
        }
418
        $req_setting = 'UTF-8';
419
        $status = $setting == $req_setting ? self::STATUS_OK : self::STATUS_ERROR;
420
        $array[] = $this->build_setting(
421
            $status,
422
            '[INI]',
423
            'default_charset',
424
            'https://php.net/manual/en/ini.core.php#ini.default-charset',
425
            $setting,
426
            $req_setting,
427
            null,
428
            get_lang('DefaultCharsetInfo')
429
        );
430
431
        $setting = ini_get('max_execution_time');
432
        $req_setting = '300 ('.get_lang('Minimum').')';
433
        $status = $setting >= 300 ? self::STATUS_OK : self::STATUS_WARNING;
434
        $array[] = $this->build_setting(
435
            $status,
436
            '[INI]',
437
            'max_execution_time',
438
            'https://php.net/manual/en/ini.core.php#ini.max-execution-time',
439
            $setting,
440
            $req_setting,
441
            null,
442
            get_lang('MaxExecutionTimeInfo')
443
        );
444
445
        $setting = ini_get('max_input_time');
446
        $req_setting = '300 ('.get_lang('Minimum').')';
447
        $status = $setting >= 300 ? self::STATUS_OK : self::STATUS_WARNING;
448
        $array[] = $this->build_setting(
449
            $status,
450
            '[INI]',
451
            'max_input_time',
452
            'https://php.net/manual/en/ini.core.php#ini.max-input-time',
453
            $setting,
454
            $req_setting,
455
            null,
456
            get_lang('MaxInputTimeInfo')
457
        );
458
459
        $setting = ini_get('memory_limit');
460
        $req_setting = '>= '.REQUIRED_MIN_MEMORY_LIMIT.'M';
461
        $status = self::STATUS_ERROR;
462
        if ((float) $setting >= REQUIRED_MIN_MEMORY_LIMIT) {
463
            $status = self::STATUS_OK;
464
        }
465
        $array[] = $this->build_setting(
466
            $status,
467
            '[INI]',
468
            'memory_limit',
469
            'https://php.net/manual/en/ini.core.php#ini.memory-limit',
470
            $setting,
471
            $req_setting,
472
            null,
473
            get_lang('MemoryLimitInfo')
474
        );
475
476
        $setting = ini_get('post_max_size');
477
        $req_setting = '>= '.REQUIRED_MIN_POST_MAX_SIZE.'M';
478
        $status = self::STATUS_ERROR;
479
        if ((float) $setting >= REQUIRED_MIN_POST_MAX_SIZE) {
480
            $status = self::STATUS_OK;
481
        }
482
        $array[] = $this->build_setting(
483
            $status,
484
            '[INI]',
485
            'post_max_size',
486
            'https://php.net/manual/en/ini.core.php#ini.post-max-size',
487
            $setting,
488
            $req_setting,
489
            null,
490
            get_lang('PostMaxSizeInfo')
491
        );
492
493
        $setting = ini_get('upload_max_filesize');
494
        $req_setting = '>= '.REQUIRED_MIN_UPLOAD_MAX_FILESIZE.'M';
495
        $status = self::STATUS_ERROR;
496
        if ((float) $setting >= REQUIRED_MIN_UPLOAD_MAX_FILESIZE) {
497
            $status = self::STATUS_OK;
498
        }
499
        $array[] = $this->build_setting(
500
            $status,
501
            '[INI]',
502
            'upload_max_filesize',
503
            'https://php.net/manual/en/ini.core.php#ini.upload_max_filesize',
504
            $setting,
505
            $req_setting,
506
            null,
507
            get_lang('UploadMaxFilesizeInfo')
508
        );
509
510
        $setting = ini_get('upload_tmp_dir');
511
        $status = self::STATUS_OK;
512
        $array[] = $this->build_setting(
513
            $status,
514
            '[INI]',
515
            'upload_tmp_dir',
516
            'https://php.net/manual/en/ini.core.php#ini.upload_tmp_dir',
517
            $setting,
518
            '',
519
            null,
520
            get_lang('UploadTmpDirInfo')
521
        );
522
523
        $setting = ini_get('variables_order');
524
        $req_setting = 'GPCS';
525
        $status = $setting == $req_setting ? self::STATUS_OK : self::STATUS_ERROR;
526
        $array[] = $this->build_setting(
527
            $status,
528
            '[INI]',
529
            'variables_order',
530
            'https://php.net/manual/en/ini.core.php#ini.variables-order',
531
            $setting,
532
            $req_setting,
533
            null,
534
            get_lang('VariablesOrderInfo')
535
        );
536
537
        $setting = ini_get('session.gc_maxlifetime');
538
        $req_setting = '4320';
539
        $status = $setting == $req_setting ? self::STATUS_OK : self::STATUS_WARNING;
540
        $array[] = $this->build_setting(
541
            $status,
542
            '[SESSION]',
543
            'session.gc_maxlifetime',
544
            'https://php.net/manual/en/ini.core.php#session.gc-maxlifetime',
545
            $setting,
546
            $req_setting,
547
            null,
548
            get_lang('SessionGCMaxLifetimeInfo')
549
        );
550
551
        if (api_check_browscap()) {
552
            $setting = true;
553
        } else {
554
            $setting = false;
555
        }
556
        $req_setting = true;
557
        $status = $setting == $req_setting ? self::STATUS_OK : self::STATUS_WARNING;
558
        $array[] = $this->build_setting(
559
            $status,
560
            '[INI]',
561
            'browscap',
562
            'https://php.net/manual/en/misc.configuration.php#ini.browscap',
563
            $setting,
564
            $req_setting,
565
            'on_off',
566
            get_lang('BrowscapInfo')
567
        );
568
569
        // Extensions
570
        $extensions = [
571
            'gd' => [
572
                'link' => 'https://php.net/gd',
573
                'expected' => 1,
574
                'comment' => get_lang('ExtensionMustBeLoaded'),
575
            ],
576
            'pdo_mysql' => [
577
                'link' => 'https://php.net/manual/en/ref.pdo-mysql.php',
578
                'expected' => 1,
579
                'comment' => get_lang('ExtensionMustBeLoaded'),
580
            ],
581
            'pcre' => [
582
                'link' => 'https://php.net/pcre',
583
                'expected' => 1,
584
                'comment' => get_lang('ExtensionMustBeLoaded'),
585
            ],
586
            'session' => [
587
                'link' => 'https://php.net/session',
588
                'expected' => 1,
589
                'comment' => get_lang('ExtensionMustBeLoaded'),
590
            ],
591
            'standard' => [
592
                'link' => 'https://php.net/spl',
593
                'expected' => 1,
594
                'comment' => get_lang('ExtensionMustBeLoaded'),
595
            ],
596
            'zlib' => [
597
                'link' => 'https://php.net/zlib',
598
                'expected' => 1,
599
                'comment' => get_lang('ExtensionMustBeLoaded'),
600
            ],
601
            'curl' => [
602
                'link' => 'https://php.net/curl',
603
                'expected' => 1,
604
                'comment' => get_lang('ExtensionMustBeLoaded'),
605
            ],
606
            'fileinfo' => [
607
                'link' => 'https://php.net/fileinfo',
608
                'expected' => 1,
609
                'comment' => get_lang('ExtensionMustBeLoaded'),
610
            ],
611
            'xsl' => [
612
                'link' => 'https://php.net/xsl',
613
                'expected' => 2,
614
                'comment' => get_lang('ExtensionShouldBeLoaded'),
615
            ],
616
            'Zend OPcache' => [
617
                'link' => 'https://php.net/opcache',
618
                'expected' => 2,
619
                'comment' => get_lang('ExtensionShouldBeLoaded'),
620
            ],
621
            'apcu' => [
622
                'link' => 'https://php.net/apcu',
623
                'expected' => 2,
624
                'comment' => get_lang('ExtensionShouldBeLoaded'),
625
            ],
626
            'openssl' => [ //required only for DKIM e-mail signatures
627
                'link' => 'https://php.net/openssl',
628
                'expected' => 2,
629
                'comment' => get_lang('ExtensionShouldBeLoaded'),
630
            ],
631
            'bcmath' => [
632
                'link' => 'https://php.net/bcmath',
633
                'expected' => 2,
634
                'comment' => get_lang('ExtensionShouldBeLoaded'),
635
            ],
636
        ];
637
638
        foreach ($extensions as $extension => $data) {
639
            $url = $data['link'];
640
            $expected_value = $data['expected'];
641
            $comment = $data['comment'];
642
643
            $loaded = extension_loaded($extension);
644
            $status = $loaded ? self::STATUS_OK : self::STATUS_ERROR;
645
            $array[] = $this->build_setting(
646
                $status,
647
                '[EXTENSION]',
648
                get_lang('LoadedExtension').': '.$extension,
649
                $url,
650
                $loaded,
651
                $expected_value,
652
                'yes_no_optional',
653
                $comment
654
            );
655
        }
656
657
        return $array;
658
    }
659
660
    /**
661
     * Functions to get the data for the mysql diagnostics.
662
     *
663
     * @return array of data
664
     */
665
    public function get_database_data()
666
    {
667
        $array = [];
668
        $em = Database::getManager();
669
        $connection = $em->getConnection();
670
        $host = $connection->getHost();
671
        $db = $connection->getDatabase();
672
        $port = $connection->getPort();
673
        $driver = $connection->getDriver()->getName();
674
675
        $array[] = $this->build_setting(
676
            self::STATUS_INFORMATION,
677
            '[Database]',
678
            'driver',
679
            '',
680
            $driver,
681
            null,
682
            null,
683
            get_lang('Driver')
684
        );
685
686
        $array[] = $this->build_setting(
687
            self::STATUS_INFORMATION,
688
            '[Database]',
689
            'host',
690
            '',
691
            $host,
692
            null,
693
            null,
694
            get_lang('MysqlHostInfo')
695
        );
696
697
        $array[] = $this->build_setting(
698
            self::STATUS_INFORMATION,
699
            '[Database]',
700
            'port',
701
            '',
702
            $port,
703
            null,
704
            null,
705
            get_lang('Port')
706
        );
707
708
        $array[] = $this->build_setting(
709
            self::STATUS_INFORMATION,
710
            '[Database]',
711
            'Database name',
712
            '',
713
            $db,
714
            null,
715
            null,
716
            get_lang('Name')
717
        );
718
719
        return $array;
720
    }
721
722
    /**
723
     * Functions to get the data for the webserver diagnostics.
724
     *
725
     * @return array of data
726
     */
727
    public function get_webserver_data()
728
    {
729
        $array = [];
730
731
        $array[] = $this->build_setting(
732
            self::STATUS_INFORMATION,
733
            '[SERVER]',
734
            '$_SERVER["SERVER_NAME"]',
735
            'http://be.php.net/reserved.variables.server',
736
            $_SERVER["SERVER_NAME"],
737
            null,
738
            null,
739
            get_lang('ServerNameInfo')
740
        );
741
        $array[] = $this->build_setting(
742
            self::STATUS_INFORMATION,
743
            '[SERVER]',
744
            '$_SERVER["SERVER_ADDR"]',
745
            'http://be.php.net/reserved.variables.server',
746
            $_SERVER["SERVER_ADDR"],
747
            null,
748
            null,
749
            get_lang('ServerAddessInfo')
750
        );
751
        $array[] = $this->build_setting(
752
            self::STATUS_INFORMATION,
753
            '[SERVER]',
754
            '$_SERVER["SERVER_PORT"]',
755
            'http://be.php.net/reserved.variables.server',
756
            $_SERVER["SERVER_PORT"],
757
            null,
758
            null,
759
            get_lang('ServerPortInfo')
760
        );
761
        $array[] = $this->build_setting(
762
            self::STATUS_INFORMATION,
763
            '[SERVER]',
764
            '$_SERVER["SERVER_SOFTWARE"]',
765
            'http://be.php.net/reserved.variables.server',
766
            $_SERVER["SERVER_SOFTWARE"],
767
            null,
768
            null,
769
            get_lang('ServerSoftwareInfo')
770
        );
771
        $array[] = $this->build_setting(
772
            self::STATUS_INFORMATION,
773
            '[SERVER]',
774
            '$_SERVER["REMOTE_ADDR"]',
775
            'http://be.php.net/reserved.variables.server',
776
            $_SERVER["REMOTE_ADDR"],
777
            null,
778
            null,
779
            get_lang('ServerRemoteInfo')
780
        );
781
        $array[] = $this->build_setting(
782
            self::STATUS_INFORMATION,
783
            '[SERVER]',
784
            '$_SERVER["HTTP_USER_AGENT"]',
785
            'http://be.php.net/reserved.variables.server',
786
            $_SERVER["HTTP_USER_AGENT"],
787
            null,
788
            null,
789
            get_lang('ServerUserAgentInfo')
790
        );
791
        $array[] = $this->build_setting(
792
            self::STATUS_INFORMATION,
793
            '[SERVER]',
794
            '$_SERVER["SERVER_PROTOCOL"]',
795
            'http://be.php.net/reserved.variables.server',
796
            $_SERVER["SERVER_PROTOCOL"],
797
            null,
798
            null,
799
            get_lang('ServerProtocolInfo')
800
        );
801
        $array[] = $this->build_setting(
802
            self::STATUS_INFORMATION,
803
            '[SERVER]',
804
            'php_uname()',
805
            'http://be2.php.net/php_uname',
806
            php_uname(),
807
            null,
808
            null,
809
            get_lang('UnameInfo')
810
        );
811
        $array[] = $this->build_setting(
812
            self::STATUS_INFORMATION,
813
            '[SERVER]',
814
            '$_SERVER["HTTP_X_FORWARDED_FOR"]',
815
            'http://be.php.net/reserved.variables.server',
816
            (!empty($_SERVER["HTTP_X_FORWARDED_FOR"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : ''),
817
            null,
818
            null,
819
            get_lang('ServerXForwardedForInfo')
820
        );
821
822
        return $array;
823
    }
824
825
    /**
826
     * Functions to get the data for the courses space usage.
827
     *
828
     * @throws Exception
829
     *
830
     * @return array of data
831
     */
832
    public function get_courses_space_data()
833
    {
834
        $array = [];
835
836
        $em = Database::getManager();
837
	$connection = $em->getConnection();
838
        $multiUrlQueryExtra = "";
839
        if (api_is_multiple_url_enabled()) {
840
            $access_url_id = api_get_current_access_url_id();
841
            $multiUrlQueryExtra = " WHERE id in (select c_id from access_url_rel_course where access_url_id = " . $access_url_id . ")";
842
        }
843
        $res = $connection->query('SELECT id, code, directory, disk_quota, last_visit FROM course' . $multiUrlQueryExtra . ' ORDER BY last_visit DESC, code LIMIT 500');
844
        $systemPath = api_get_path(SYS_COURSE_PATH);
845
        $webPath = api_get_path(WEB_COURSE_PATH);
846
        $courseHomeIcon = Display::return_icon('home.png', get_lang('CourseHome'));
847
        $courseEditIcon = Display::return_icon('edit.png', get_lang('Edit'));
848
        $windows = api_is_windows_os();
849
        $courseEditPath = api_get_path(WEB_CODE_PATH).'admin/course_edit.php?id=';
850
        while ($row = $res->fetch()) {
851
            $quota = $row['disk_quota'] / (1024 * 1024);
852
            $dir = $systemPath.$row['directory'].'/';
853
            $path = '<a href="'.$webPath.$row['code'].'/index.php?id_session=0">'.$courseHomeIcon.'</a>';
854
            $size = '-';
855
            $courseEditLink = '<a href="'.$courseEditPath.$row['id'].'">'.$courseEditIcon.'</a>';
856
857
            if (!$windows) {
858
                $err = [];
859
                $du = exec('du -s '.$dir, $err);
860
                list($size, $none) = explode("\t", $du);
861
                unset($none);
862
                $size = intval($size);
863
                if ($size < 1024) {
864
                    $size = 1;
865
                } else {
866
                    $size = round($size / 1024);
867
                }
868
            }
869
            $array[] = [
870
                $path,
871
                $row['code'],
872
                $size,
873
                round($quota),
874
                $courseEditLink,
875
                $row['last_visit'],
876
                $dir,
877
            ];
878
        }
879
880
        return $array;
881
    }
882
883
    /**
884
     * Functions to get the number of courses in the database.
885
     *
886
     * @throws Exception
887
     *
888
     * @return array of data
889
     */
890
    public function get_courses_space_count()
891
    {
892
        $em = Database::getManager();
893
        $connection = $em->getConnection();
894
        $res = $connection->query('SELECT count(id) FROM course');
895
        while ($row = $res->fetch()) {
896
            $count = $row[0];
897
        }
898
899
        return $count;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $count does not seem to be defined for all execution paths leading up to this point.
Loading history...
900
    }
901
902
    /**
903
     * Additional functions needed for fast integration.
904
     *
905
     * @param int    $status         Status constant defining which icon to use to illustrate the info
906
     * @param string $section        The name of the section this setting is included in
907
     * @param string $title          The name of the setting (usually a translated string)
908
     * @param string $url            A URL to point the user to regarding this setting, or '#' otherwise
909
     * @param mixed  $current_value  The current value for this setting
910
     * @param mixed  $expected_value The expected value for this setting
911
     * @param string $formatter      If this setting is expressed in some kind of format, which format to use
912
     * @param string $comment        A translated string explaining what this setting represents
913
     *
914
     * @return array A list of elements to show in an array's row
915
     */
916
    public function build_setting(
917
        $status,
918
        $section,
919
        $title,
920
        $url,
921
        $current_value,
922
        $expected_value,
923
        $formatter,
924
        $comment
925
    ) {
926
        switch ($status) {
927
            case self::STATUS_OK:
928
                $img = 'bullet_green.png';
929
                break;
930
            case self::STATUS_WARNING:
931
                $img = 'bullet_orange.png';
932
                break;
933
            case self::STATUS_ERROR:
934
                $img = 'bullet_red.png';
935
                break;
936
            case self::STATUS_INFORMATION:
937
            default:
938
                $img = 'bullet_blue.png';
939
                break;
940
        }
941
942
        $image = Display::return_icon($img, $status);
943
        $url = $this->get_link($title, $url);
944
945
        $formatted_current_value = $current_value;
946
        $formatted_expected_value = $expected_value;
947
948
        if ($formatter) {
949
            if (method_exists($this, 'format_'.$formatter)) {
950
                $formatted_current_value = call_user_func([$this, 'format_'.$formatter], $current_value);
951
                $formatted_expected_value = call_user_func([$this, 'format_'.$formatter], $expected_value);
952
            }
953
        }
954
955
        return [$image, $section, $url, $formatted_current_value, $formatted_expected_value, $comment];
956
    }
957
958
    /**
959
     * Create a link with a url and a title.
960
     *
961
     * @param $title
962
     * @param $url
963
     *
964
     * @return string the url
965
     */
966
    public function get_link($title, $url)
967
    {
968
        return '<a href="'.$url.'" target="about:bank">'.$title.'</a>';
969
    }
970
971
    /**
972
     * @param int $value
973
     *
974
     * @return string
975
     */
976
    public function format_yes_no_optional($value)
977
    {
978
        $return = '';
979
        switch ($value) {
980
            case 0:
981
                $return = get_lang('No');
982
                break;
983
            case 1:
984
                $return = get_lang('Yes');
985
                break;
986
            case 2:
987
                $return = get_lang('Optional');
988
                break;
989
        }
990
991
        return $return;
992
    }
993
994
    /**
995
     * @param $value
996
     *
997
     * @return string
998
     */
999
    public function format_yes_no($value)
1000
    {
1001
        return $value ? get_lang('Yes') : get_lang('No');
1002
    }
1003
1004
    /**
1005
     * @param int $value
1006
     *
1007
     * @return string
1008
     */
1009
    public function format_on_off($value)
1010
    {
1011
        $value = intval($value);
1012
        if ($value > 1) {
1013
            // Greater than 1 values are shown "as-is", they may be interpreted as "On" later.
1014
            return $value;
1015
        }
1016
        // These are the values 'On' and 'Off' used in the php-ini file. Translation (get_lang()) is not needed here.
1017
        return $value ? 'On' : 'Off';
1018
    }
1019
}
1020