Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RulesController.php « Controller « src - github.com/HuasoFoundries/phpPgAdmin6.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9e70e821838869017035c1d81d6694aca721dbc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php

/**
 * PHPPgAdmin6
 */

namespace PHPPgAdmin\Controller;

use PHPPgAdmin\Decorators\Decorator;
use Slim\Http\Response;

/**
 * Base controller class.
 */
class RulesController extends BaseController
{
    /**
     * Default method to render the controller according to the action parameter.
     *
     * @return null|Response|string
     */
    public function render()
    {
        if ('tree' === $this->action) {
            return $this->doTree();
        }

        // Different header if we're view rules or table rules
        $this->printHeader($_REQUEST[$_REQUEST['subject']] . ' - ' . $this->lang['strrules']);
        $this->printBody();

        switch ($this->action) {
            case 'create_rule':
                $this->createRule(true);

                break;
            case 'save_create_rule':
                if (null !== $this->getPostParam('cancel')) {
                    $this->doDefault();
                } else {
                    $this->createRule(false);
                }

                break;
            case 'drop':
                if (isset($_POST['yes'])) {
                    $this->doDrop(false);
                } else {
                    $this->doDefault();
                }

                break;
            case 'confirm_drop':
                $this->doDrop(true);

                break;

            default:
                $this->doDefault();

                break;
        }

        return $this->printFooter();
    }

    /**
     * List all the rules on the table.
     *
     * @param mixed $msg
     */
    public function doDefault($msg = '')
    {
        $data = $this->misc->getDatabaseAccessor();

        $this->printTrail($_REQUEST['subject']);
        $this->printTabs($_REQUEST['subject'], 'rules');
        $this->printMsg($msg);

        $rules = $data->getRules($_REQUEST[$_REQUEST['subject']]);

        $columns = [
            'rule' => [
                'title' => $this->lang['strname'],
                'field' => Decorator::field('rulename'),
            ],
            'definition' => [
                'title' => $this->lang['strdefinition'],
                'field' => Decorator::field('definition'),
            ],
            'actions' => [
                'title' => $this->lang['stractions'],
            ],
        ];

        $subject = \urlencode($_REQUEST['subject']);
        $object = \urlencode($_REQUEST[$_REQUEST['subject']]);

        $actions = [
            'drop' => [
                'content' => $this->lang['strdrop'],
                'attr' => [
                    'href' => [
                        'url' => 'rules',
                        'urlvars' => [
                            'action' => 'confirm_drop',
                            'reltype' => $subject,
                            $subject => $object,
                            'subject' => 'rule',
                            'rule' => Decorator::field('rulename'),
                        ],
                    ],
                ],
            ],
        ];

        if (self::isRecordset($rules)) {
            echo $this->printTable($rules, $columns, $actions, 'rules-rules', $this->lang['strnorules']);
        }

        return $this->printNavLinks(['create' => [
            'attr' => [
                'href' => [
                    'url' => 'rules',
                    'urlvars' => [
                        'action' => 'create_rule',
                        'server' => $_REQUEST['server'],
                        'database' => $_REQUEST['database'],
                        'schema' => $_REQUEST['schema'],
                        $subject => $object,
                        'subject' => $subject,
                    ],
                ],
            ],
            'content' => $this->lang['strcreaterule'],
        ]], 'rules-rules', \get_defined_vars());
    }

    /**
     * @return Response|string
     */
    public function doTree()
    {
        $data = $this->misc->getDatabaseAccessor();

        $rules = $data->getRules($_REQUEST[$_REQUEST['subject']]);

        $attrs = [
            'text' => Decorator::field('rulename'),
            'icon' => 'Rule',
        ];

        return $this->printTree($rules, $attrs, 'rules');
    }

    /**
     * Confirm and then actually create a rule.
     *
     * @param mixed $confirm
     * @param mixed $msg
     */
    public function createRule($confirm, $msg = ''): void
    {
        $data = $this->misc->getDatabaseAccessor();

        $this->coalesceArr($_POST, 'name', '');

        $this->coalesceArr($_POST, 'event', '');

        $this->coalesceArr($_POST, 'where', '');

        $this->coalesceArr($_POST, 'type', 'SOMETHING');

        $this->coalesceArr($_POST, 'raction', '');

        if ($confirm) {
            $this->printTrail($_REQUEST['subject']);
            $this->printTitle($this->lang['strcreaterule'], 'pg.rule.create');
            $this->printMsg($msg);
            echo '<form action="rules" method="post">' . \PHP_EOL;
            echo '<table>' . \PHP_EOL;
            echo \sprintf(
                '<tr><th class="data left required">%s</th>',
                $this->lang['strname']
            ) . \PHP_EOL;
            echo \sprintf(
                '<td class="data1"><input name="name" size="16" maxlength="%s" value="',
                $data->_maxNameLen
            ),
            \htmlspecialchars($_POST['name']), '" /></td></tr>' . \PHP_EOL;
            echo \sprintf(
                '<tr><th class="data left required">%s</th>',
                $this->lang['strevent']
            ) . \PHP_EOL;
            echo '<td class="data1"><select name="event">' . \PHP_EOL;

            foreach ($data->rule_events as $v) {
                echo \sprintf(
                    '<option value="%s"',
                    $v
                ), ($v === $_POST['event']) ? ' selected="selected"' : '',
                \sprintf(
                    '>%s</option>',
                    $v
                ) . \PHP_EOL;
            }
            echo '</select></td></tr>' . \PHP_EOL;
            echo \sprintf(
                '<tr><th class="data left">%s</th>',
                $this->lang['strwhere']
            ) . \PHP_EOL;
            echo '<td class="data1"><input name="where" size="32" value="',
            \htmlspecialchars($_POST['where']), '" /></td></tr>' . \PHP_EOL;
            echo \sprintf(
                '<tr><th class="data left"><label for="instead">%s</label></th>',
                $this->lang['strinstead']
            ) . \PHP_EOL;
            echo '<td class="data1">';
            echo '<input type="checkbox" id="instead" name="instead" ', (isset($_POST['instead'])) ? ' checked="checked"' : '', ' />' . \PHP_EOL;
            echo '</td></tr>' . \PHP_EOL;
            echo \sprintf(
                '<tr><th class="data left required">%s</th>',
                $this->lang['straction']
            ) . \PHP_EOL;
            echo '<td class="data1">';
            echo '<input type="radio" id="type1" name="type" value="NOTHING"', ('NOTHING' === $_POST['type']) ? ' checked="checked"' : '', ' /> <label for="type1">NOTHING</label><br />' . \PHP_EOL;
            echo '<input type="radio" name="type" value="SOMETHING"', ('SOMETHING' === $_POST['type']) ? ' checked="checked"' : '', ' />' . \PHP_EOL;
            echo '(<input name="raction" size="32" value="',
            \htmlspecialchars($_POST['raction']), '" />)</td></tr>' . \PHP_EOL;
            echo '</table>' . \PHP_EOL;
            echo '<input type="hidden" name="action" value="save_create_rule" />' . \PHP_EOL;
            echo '<input type="hidden" name="subject" value="', \htmlspecialchars($_REQUEST['subject']), '" />' . \PHP_EOL;
            echo '<input type="hidden" name="', \htmlspecialchars($_REQUEST['subject']),
            '" value="', \htmlspecialchars($_REQUEST[$_REQUEST['subject']]), '" />' . \PHP_EOL;
            echo $this->view->form;
            echo \sprintf(
                '<p><input type="submit" name="ok" value="%s" />',
                $this->lang['strcreate']
            ) . \PHP_EOL;
            echo \sprintf(
                '<input type="submit" name="cancel" value="%s"  /></p>%s',
                $this->lang['strcancel'],
                \PHP_EOL
            );
            echo '</form>' . \PHP_EOL;
        } elseif ('' === \trim($_POST['name'])) {
            $this->createRule(true, $this->lang['strruleneedsname']);
        } else {
            $status = $data->createRule(
                $_POST['name'],
                $_POST['event'],
                $_POST[$_POST['subject']],
                $_POST['where'],
                isset($_POST['instead']),
                $_POST['type'],
                $_POST['raction']
            );

            if (0 === $status) {
                $this->doDefault($this->lang['strrulecreated']);
            } else {
                $this->createRule(true, $this->lang['strrulecreatedbad']);
            }
        }
    }

    /**
     * Show confirmation of drop and perform actual drop.
     *
     * @param mixed $confirm
     */
    public function doDrop($confirm): void
    {
        $data = $this->misc->getDatabaseAccessor();

        if ($confirm) {
            $this->printTrail($_REQUEST['subject']);
            $this->printTitle($this->lang['strdrop'], 'pg.rule.drop');

            echo '<p>', \sprintf(
                $this->lang['strconfdroprule'],
                $this->misc->printVal($_REQUEST['rule']),
                $this->misc->printVal($_REQUEST[$_REQUEST['reltype']])
            ), '</p>' . \PHP_EOL;

            echo '<form action="rules" method="post">' . \PHP_EOL;
            echo '<input type="hidden" name="action" value="drop" />' . \PHP_EOL;
            echo '<input type="hidden" name="subject" value="', \htmlspecialchars($_REQUEST['reltype']), '" />' . \PHP_EOL;
            echo '<input type="hidden" name="', \htmlspecialchars($_REQUEST['reltype']),
            '" value="', \htmlspecialchars($_REQUEST[$_REQUEST['reltype']]), '" />' . \PHP_EOL;
            echo '<input type="hidden" name="rule" value="', \htmlspecialchars($_REQUEST['rule']), '" />' . \PHP_EOL;
            echo $this->view->form;
            echo \sprintf(
                '<p><input type="checkbox" id="cascade" name="cascade" /> <label for="cascade">%s</label></p>',
                $this->lang['strcascade']
            ) . \PHP_EOL;
            echo \sprintf(
                '<input type="submit" name="yes" value="%s" />',
                $this->lang['stryes']
            ) . \PHP_EOL;
            echo \sprintf(
                '<input type="submit" name="no" value="%s" />',
                $this->lang['strno']
            ) . \PHP_EOL;
            echo '</form>' . \PHP_EOL;
        } else {
            $status = $data->dropRule($_POST['rule'], $_POST[$_POST['subject']], isset($_POST['cascade']));

            if (0 === $status) {
                $this->doDefault($this->lang['strruledropped']);
            } else {
                $this->doDefault($this->lang['strruledroppedbad']);
            }
        }
    }
}