Test Failed
Pull Request — develop (#380)
by Felipe
04:40
created

BaseController::renderInitialPageIfNotLogged()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 4
nop 0
dl 0
loc 19
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * PHPPgAdmin 6.1.3
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
use PHPPgAdmin\ADORecordSet;
10
use PHPPgAdmin\ArrayRecordSet;
11
use PHPPgAdmin\ContainerUtils;
12
use PHPPgAdmin\Misc;
13
use PHPPgAdmin\Traits\HelperTrait;
14
use PHPPgAdmin\ViewManager;
15
use PHPPgAdmin\XHtml;
16
use PHPPgAdmin\XHtml\HTMLFooterController;
17
use PHPPgAdmin\XHtml\HTMLHeaderController;
18
use PHPPgAdmin\XHtml\HTMLNavbarController;
19
use PHPPgAdmin\XHtml\HTMLTableController;
20
use Slim\Http\Response;
21
22
/**
23
 * Base controller class.
24
 */
25
class BaseController
26
{
27
    use HelperTrait;
28
29
    public $appLangFiles = [];
30
31
    public $appThemes = [];
32
33
    public $appName = '';
34
35
    public $appVersion = '';
36
37
    public $form = '';
38
39
    public $href = '';
40
41
    public $lang = [];
42
43
    public $action = '';
44
45
    public $controller_name;
46
47
    /**
48
     * Used.
49
     *
50
     * @var string
51
     */
52
    public $view_name;
53
54
    /**
55
     * Used to print the title passing its value to $lang.
56
     *
57
     * @var string
58
     */
59
    public $controller_title = 'base';
60
61
    public $msg = '';
62
63
    /**
64
     * @var ViewManager
65
     */
66
    public $view;
67
68
    /**
69
     * @var Misc
70
     */
71
    public $misc;
72
73
    public $conf;
74
75
    public $phpMinVer;
76
77
    /**
78
     * @var ContainerUtils
79
     */
80
    protected $container;
81
82
    protected $script;
83
84
    protected $data;
85
86
    protected $database;
87
88
    protected $server_id;
89
90
    /**
91
     * @var XHtml\HTMLTableController
92
     * @psalm-suppress PropertyNotSetInConstructor
93
     */
94
    protected $_table_controller;
95
96
    /**
97
     * @var XHtml\HTMLFooterController
98
     * @psalm-suppress PropertyNotSetInConstructor
99
     */
100
    protected $_footer_controller;
101
102
    /**
103
     * @var XHtml\HTMLHeaderController
104
     * @psalm-suppress PropertyNotSetInConstructor
105
     */
106
    protected $_header_controller;
107
108
    /**
109
     * @var XHtml\HTMLNavbarController
110
     * @psalm-suppress PropertyNotSetInConstructor
111
     */
112
    protected $_trail_controller;
113
114
    /**
115
     * @var TreeController
116
     * @psalm-suppress PropertyNotSetInConstructor
117
     */
118
    protected $_tree_controller;
119
120
    protected $scripts = '';
121
122
    protected $no_db_connection = false;
123
124
    /**
125
     * Constructs the base controller (common for almost all controllers).
126
     *
127
     * @param ContainerUtils $container        the $app container
128
     * @param bool           $no_db_connection [optional] if true, sets  $this->misc->setNoDBConnection(true);
129
     */
130
    public function __construct(ContainerUtils $container)
131
    {
132
        $this->container = $container;
133
        $this->lang = $container->get('lang');
134
135
        $this->controller_name = \str_replace(__NAMESPACE__ . '\\', '', \get_class($this));
136
        $this->view_name = \str_replace('controller', '', \mb_strtolower($this->controller_name));
137
        $this->script = $this->view_name;
138
139
        $this->view = $container->get('view');
140
141
        $this->msg = $container->get('msg');
142
        $this->appLangFiles = $container->get('appLangFiles');
143
144
        $this->misc = $container->get('misc');
145
        $this->conf = $this->misc->getConf();
146
147
        $this->appThemes = $container->get('appThemes');
148
        $this->action = $container->get('action');
149
150
        $this->appName = $container->get('settings')['appName'];
151
        $this->appVersion = $container->get('settings')['appVersion'];
152
        $this->postgresqlMinVer = $container->get('settings')['postgresqlMinVer'];
153
        $this->phpMinVer = $container->get('settings')['phpMinVer'];
154
155
        $msg = $container->get('msg');
156
157
        if (true === $this->no_db_connection) {
158
            $this->misc->setNoDBConnection(true);
159
        }
160
161
        if (!$this->container->IN_TEST) {
162
            $this->renderInitialPageIfNotLogged();
163
        }
164
    }
165
166
    /**
167
     * Default method to render the controller according to the action parameter. It should return with a PSR
168
     * responseObject but it prints texts whatsoeever.
169
     */
170
    public function render()
171
    {
172
        $this->misc = $this->misc;
173
174
        $this->printHeader();
175
        $this->printBody();
176
177
        switch ($this->action) {
178
            default:
179
                $this->doDefault();
180
181
                break;
182
        }
183
184
        $this->printFooter();
185
    }
186
187
    /**
188
     * @return string
189
     */
190
    public function doDefault()
191
    {
192
        $html = '<div><h2>Section title</h2> <p>Main content</p></div>';
193
        echo $html;
194
195
        return $html;
196
    }
197
198
    /**
199
     * Returns the page title for each controller.
200
     *
201
     * @param string $title  The title
202
     * @param string $prefix The prefix
203
     * @param string $suffix The suffix
204
     *
205
     * @return string the page title
206
     */
207
    public function headerTitle($title = '', $prefix = '', $suffix = '')
208
    {
209
        $title = $title ? $title : $this->controller_title;
210
211
        return $prefix . $this->lang[$title] . ($suffix ? ': ' . $suffix : '');
212
    }
213
214
    /**
215
     * @return ContainerUtils
216
     */
217
    public function getContainer()
218
    {
219
        return $this->container;
220
    }
221
222
    /**
223
     * Display a table of data.
224
     *
225
     * @param ADORecordSet|ArrayRecordSet $tabledata a set of data to be formatted
226
     * @param array                       $columns   An associative array of columns to be displayed:
227
     * @param array                       $actions   Actions that can be performed on each object:
228
     * @param string                      $place     Place where the $actions are displayed. Like 'display-browse',
229
     * @param string                      $nodata    (optional) Message to display if data set is empty
230
     * @param callable                    $pre_fn    (optional) callback closure for each row
231
     *
232
     * @return string the html of the table
233
     */
234
    public function printTable(&$tabledata, &$columns, &$actions, $place, $nodata = '', $pre_fn = null)
235
    {
236
        $html_table = $this->_getTableController();
237
238
        $html_table->initialize($tabledata, $columns, $actions, $place, $nodata, $pre_fn);
239
240
        return $html_table->printTable();
241
    }
242
243
    /**
244
     * Hides or show tree tabs according to their properties.
245
     *
246
     * @param array $tabs The tabs
247
     *
248
     * @return ADORecordSet|ArrayRecordSet filtered tabs in the form of an ArrayRecordSet
249
     */
250
    public function adjustTabsForTree(&$tabs)
251
    {
252
        $tree = $this->_getTreeController();
253
254
        return $tree->adjustTabsForTree($tabs);
255
    }
256
257
    /**
258
     * Produce JSON data for the browser tree.
259
     *
260
     * @param ADORecordSet|ArrayRecordSet $_treedata a set of records to populate the tree
261
     * @param array                       $attrs     Attributes for tree items
262
     * @param string                      $section   The section where the branch is linked in the tree
263
     * @param bool                        $print     either to return or echo the result
264
     *
265
     * @return Response|string the json rendered tree
266
     */
267
    public function printTree(&$_treedata, &$attrs, $section, $print = true)
268
    {
269
        $tree = $this->_getTreeController();
270
271
        return $tree->printTree($_treedata, $attrs, $section, $print);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $tree->printTree(...ttrs, $section, $print) returns the type array<mixed,array|boolean|string> which is incompatible with the documented return type Slim\Http\Response|string.
Loading history...
Bug introduced by
It seems like $_treedata can also be of type PHPPgAdmin\ADORecordSet; however, parameter $_treedata of PHPPgAdmin\Controller\TreeController::printTree() does only seem to accept PHPPgAdmin\ArrayRecordSet, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

271
        return $tree->printTree(/** @scrutinizer ignore-type */ $_treedata, $attrs, $section, $print);
Loading history...
272
    }
273
274
    /**
275
     * Prints a trail.
276
     *
277
     * @param array|string $trail
278
     * @param bool         $do_print The do print
279
     *
280
     * @return string ( description_of_the_return_value )
281
     */
282
    public function printTrail($trail = [], bool $do_print = true)
283
    {
284
        $from = __METHOD__;
285
        $html_trail = $this->_getNavbarController();
286
287
        return $html_trail->printTrail($trail, $do_print, $from);
288
    }
289
290
    /**
291
     * @param (array[][]|mixed)[][] $navlinks
292
     * @param string                $place
293
     * @param array                 $env
294
     * @param mixed                 $do_print
295
     */
296
    public function printNavLinks(array $navlinks, string $place, array $env = [], $do_print = true)
297
    {
298
        $from = __METHOD__;
299
        $footer_controller = $this->_getFooterController();
300
301
        return $footer_controller->printNavLinks($navlinks, $place, $env, $do_print, $from);
302
    }
303
304
    public function printTabs(string $tabs, string $activetab, bool $do_print = true)
305
    {
306
        $from = __METHOD__;
307
        $html_trail = $this->_getNavbarController();
308
309
        return $html_trail->printTabs($tabs, $activetab, $do_print, $from);
310
    }
311
312
    /**
313
     * @param true        $do_print
314
     * @param null|string $from
315
     * @param mixed       $link
316
     */
317
    public function printLink($link, bool $do_print = true, ?string $from = null)
318
    {
319
        if (null === $from) {
320
            $from = __METHOD__;
321
        }
322
323
        $html_trail = $this->_getNavbarController();
324
325
        return $html_trail->printLink($link, $do_print, $from);
326
    }
327
328
    /**
329
     * @param true $flag
330
     */
331
    public function setReloadDropDatabase(bool $flag)
332
    {
333
        $footer_controller = $this->_getFooterController();
334
335
        return $footer_controller->setReloadDropDatabase($flag);
336
    }
337
338
    /**
339
     * @param true $flag
340
     */
341
    public function setNoBottomLink(bool $flag)
342
    {
343
        $footer_controller = $this->_getFooterController();
344
345
        return $footer_controller->setNoBottomLink($flag);
346
    }
347
348
    public function printFooter(bool $doBody = true, string $template = 'footer.twig')
349
    {
350
        $footer_controller = $this->_getFooterController();
351
352
        return $footer_controller->printFooter($doBody, $template);
353
    }
354
355
    public function printReload($database, $do_print = true)
356
    {
357
        $footer_controller = $this->_getFooterController();
358
359
        return $footer_controller->printReload($database, $do_print);
0 ignored issues
show
Bug introduced by
The method printReload() does not exist on PHPPgAdmin\XHtml\HTMLFooterController. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

359
        return $footer_controller->/** @scrutinizer ignore-call */ printReload($database, $do_print);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
360
    }
361
362
    /**
363
     * Outputs JavaScript to set default focus.
364
     *
365
     * @param mixed $object eg. forms[0].username
366
     */
367
    public function setFocus($object)
368
    {
369
        $footer_controller = $this->_getFooterController();
370
371
        return $footer_controller->setFocus($object);
372
    }
373
374
    /**
375
     * Outputs JavaScript to set the name of the browser window.
376
     *
377
     * @param string $name      the window name
378
     * @param bool   $addServer if true (default) then the server id is
379
     *                          attached to the name
380
     */
381
    public function setWindowName($name, $addServer = true)
382
    {
383
        $footer_controller = $this->_getFooterController();
384
385
        return $footer_controller->setWindowName($name, $addServer);
386
    }
387
388
    /**
389
     * @param true $flag
390
     */
391
    public function setNoOutput(bool $flag)
392
    {
393
        $header_controller = $this->_getHeaderController();
394
395
        return $header_controller->setNoOutput((bool) $flag);
396
    }
397
398
    /**
399
     * @param null|string $script
400
     * @param string      $title
401
     * @param bool        $do_print
402
     * @param string      $template
403
     */
404
    public function printHeader(string $title = '', ?string $script = null, bool $do_print = true, string $template = 'header.twig')
405
    {
406
        $title = $title ? $title : $this->headerTitle();
407
        $header_controller = $this->_getHeaderController();
408
409
        return $header_controller->printHeader($title, $script, $do_print, $template);
410
    }
411
412
    public function printBody(bool $doBody = true, string $bodyClass = 'detailbody', bool $onloadInit = false)
413
    {
414
        $header_controller = $this->_getHeaderController();
415
416
        return $header_controller->printBody($doBody, $bodyClass, $onloadInit);
417
    }
418
419
    /**
420
     * @param null|string $help
421
     * @param string      $title
422
     * @param bool        $do_print
423
     */
424
    public function printTitle(string $title, ?string $help = null, bool $do_print = true)
425
    {
426
        $header_controller = $this->_getHeaderController();
427
428
        return $header_controller->printTitle($title, $help, $do_print);
429
    }
430
431
    /**
432
     * @param string      $key
433
     * @param null|string $default
434
     */
435
    public function getRequestParam(string $key, ?string $default = null)
436
    {
437
        return \requestInstance()->getParam($key, $default);
438
    }
439
440
    /**
441
     * @param string                           $key
442
     * @param null|array|bool|float|int|string $default
443
     *
444
     * @return bool| null|array|string|int|float
445
     */
446
    public function getPostParam(string $key, $default = null)
447
    {
448
        return \requestInstance()->getParsedBodyParam($key, $default);
449
    }
450
451
    /**
452
     * @param string                      $key
453
     * @param null|array|float|int|string $default
454
     *
455
     * @return null|array|float|int|string
456
     */
457
    public function getQueryStrinParam($key, $default = null)
458
    {
459
        return \requestInstance()->getQueryParam($key, $default);
460
    }
461
462
    /**
463
     * @return array
464
     */
465
    public function getAllParams(): array
466
    {
467
        return \array_merge(
468
            \requestInstance()->getQueryParams() ?? [],
469
            \requestInstance()->getParsedBody() ?? []
470
        );
471
    }
472
473
    /**
474
     * Print out a message.
475
     *
476
     * @param string $msg      The message
477
     * @param bool   $do_print if true, print the message. Return the string otherwise
478
     *
479
     * @return string a paragraph containing the message, whose linebreaks are replaced by <br> elements
480
     */
481
    public function printMsg($msg, $do_print = true)
482
    {
483
        $html = '';
484
        $msg = \htmlspecialchars(ContainerUtils::br2ln($msg));
485
486
        if ('' !== $msg) {
487
            $html .= '<p class="message">' . \nl2br($msg) . '</p>' . \PHP_EOL;
488
        }
489
490
        if ($do_print) {
491
            echo $html;
492
493
            return $html;
494
        }
495
496
        return $html;
497
    }
498
499
    private function renderInitialPageIfNotLogged(): void
500
    {
501
        if (false === $this->misc->getNoDBConnection()) {
502
            if (null === $this->misc->getServerId()) {
503
                $servers_controller = new ServersController($this->container);
504
505
                $servers_controller->render();
506
            } else {
507
                $_server_info = $this->misc->getServerInfo();
508
                // Redirect to the login form if not logged in
509
                if (!isset($_server_info['username'])) {
510
                    $msg = \sprintf(
511
                        $this->lang['strlogoutmsg'],
512
                        $_server_info['desc']
513
                    );
514
515
                    $servers_controller = new ServersController($this->container);
516
517
                    $servers_controller->render();
518
                }
519
            }
520
        }
521
    }
522
523
    /**
524
     * @return HTMLTableController
525
     */
526
    private function _getTableController()
527
    {
528
        if (null === $this->_table_controller) {
529
            $this->_table_controller = new HTMLTableController($this->getContainer(), $this->controller_name);
530
        }
531
532
        return $this->_table_controller;
533
    }
534
535
    /**
536
     * @return HTMLFooterController
537
     */
538
    private function _getFooterController()
539
    {
540
        if (null === $this->_footer_controller) {
541
            $this->_footer_controller = new HTMLFooterController($this->getContainer(), $this->controller_name);
542
        }
543
544
        return $this->_footer_controller;
545
    }
546
547
    /**
548
     * @return HTMLHeaderController
549
     */
550
    private function _getHeaderController()
551
    {
552
        if (null === $this->_header_controller) {
553
            $this->_header_controller = new HTMLHeaderController($this->getContainer(), $this->controller_name);
554
        }
555
556
        return $this->_header_controller;
557
    }
558
559
    /**
560
     * @return HTMLNavbarController
561
     */
562
    private function _getNavbarController()
563
    {
564
        if (null === $this->_trail_controller) {
565
            $this->_trail_controller = new HTMLNavbarController($this->getContainer(), $this->controller_name);
566
        }
567
568
        return $this->_trail_controller;
569
    }
570
571
    /**
572
     * @return TreeController
573
     */
574
    private function _getTreeController()
575
    {
576
        if (null === $this->_tree_controller) {
577
            $this->_tree_controller = new TreeController($this->getContainer(), $this->controller_name);
578
        }
579
580
        return $this->_tree_controller;
581
    }
582
}
583