Issues (217)

src/classes/ContainerHandlers.php (1 issue)

Severity
1
<?php
2
3
/**
4
 * PHPPgAdmin 6.1.3
5
 */
6
7
namespace PHPPgAdmin;
8
9
/**
10
 * Auxiliary class to handle injection of dependencies to avoid
11
 * declaring them in the container class.
12
 */
13
class ContainerHandlers
14
{
15
    /**
16
     * @var \PHPPgAdmin\ContainerUtils
17
     * */
18
    private $container;
19
20
    /**
21
     * @param \PHPPgAdmin\ContainerUtils $container
22
     */
23
    public function __construct(\PHPPgAdmin\ContainerUtils $container)
24
    {
25
        $this->container = $container;
26
    }
27
28
    public function storeMainRequestParams(): self
29
    {
30
        $this->container['action'] = $_REQUEST['action'] ?? '';
31
        // This should be deprecated once we're sure no php scripts are required directly
32
        $this->container->offsetSet('server', $_REQUEST['server'] ?? null);
33
        $this->container->offsetSet('database', $_REQUEST['database'] ?? null);
34
        $this->container->offsetSet('schema', $_REQUEST['schema'] ?? null);
35
36
        return $this;
37
    }
38
39
    /**
40
     * Sets the views.
41
     *
42
     * @return self ( description_of_the_return_value )
43
     */
44
    public function setViews(): self
45
    {
46
        $container = $this->container;
47
48
        /**
49
         * @return \PHPPgAdmin\ViewManager
50
         */
51
        $container['view'] = static function (\PHPPgAdmin\ContainerUtils $c): \PHPPgAdmin\ViewManager {
52
            $misc = $c->misc;
53
            $view = new ViewManager(BASE_PATH . '/assets/templates', [
54
                'cache' => BASE_PATH . '/temp/twigcache',
55
                'auto_reload' => $c->get('settings')['debug'],
56
                'debug' => $c->get('settings')['debug'],
57
            ], $c);
58
59
            $misc->setView($view);
60
61
            return $view;
62
        };
63
64
        return $this;
65
    }
66
67
    /**
68
     * Sets the instance of Misc class.
69
     *
70
     * @return self ( description_of_the_return_value )
71
     */
72
    public function setMisc(): self
73
    {
74
        $container = $this->container;
75
        /**
76
         * @return \PHPPgAdmin\Misc
77
         */
78
        $container['misc'] = static function (\PHPPgAdmin\ContainerUtils $c): \PHPPgAdmin\Misc {
79
            $misc = new \PHPPgAdmin\Misc($c);
80
81
            $conf = $c->get('conf');
0 ignored issues
show
The assignment to $conf is dead and can be removed.
Loading history...
82
83
            // 4. Check for theme by server/db/user
84
            $_server_info = $misc->getServerInfo();
85
86
            /* starting with PostgreSQL 9.0, we can set the application name */
87
            if (isset($_server_info['pgVersion']) && 9 <= $_server_info['pgVersion']) {
88
                \putenv('PGAPPNAME=' . $c->get('settings')['appName'] . '_' . $c->get('settings')['appVersion']);
89
            }
90
91
            return $misc;
92
        };
93
94
        return $this;
95
    }
96
97
    public function setExtra(): self
98
    {
99
        $container = $this->container;
100
        $container['flash'] = static function (): \Slim\Flash\Messages {
101
            return new \Slim\Flash\Messages();
102
        };
103
104
        $container['lang'] = static function (\PHPPgAdmin\ContainerUtils $c): array {
105
            $translations = new \PHPPgAdmin\Translations($c);
106
107
            return $translations->lang;
108
        };
109
110
        return $this;
111
    }
112
113
    public function setHaltHandler(): self
114
    {
115
        $this->container['haltHandler'] = static function (\PHPPgAdmin\ContainerUtils $c) {
116
            return static function ($request, $response, $exits, $status = 500) {
117
                $title = 'PHPPgAdmin Error';
118
119
                $html = '<p>The application could not run because of the following error:</p>';
120
121
                $output = \sprintf(
122
                    "<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'>" .
123
                        '<title>%s</title><style>' .
124
                        'body{margin:0;padding:30px;font:12px/1.5 Helvetica,Arial,Verdana,sans-serif;}' .
125
                        'h3{margin:0;font-size:28px;font-weight:normal;line-height:30px;}' .
126
                        'span{display:inline-block;font-size:16px;}' .
127
                        '</style></head><body><h3>%s</h3><p>%s</p><span>%s</span></body></html>',
128
                    $title,
129
                    $title,
130
                    $html,
131
                    \implode('<br>', $exits)
132
                );
133
134
                $body = $response->getBody(); //new \Slim\Http\Body(fopen('php://temp', 'r+'));
135
                $body->write($output);
136
137
                return $response
138
                    ->withStatus($status)
139
                    ->withHeader('Content-type', 'text/html')
140
                    ->withBody($body);
141
            };
142
        };
143
144
        return $this;
145
    }
146
}
147