Passed
Push — master ( 442876...4ec1bc )
by Felipe
15:55 queued 10:33
created

src/controllers/ColpropertiesController.php (4 issues)

1
<?php
2
0 ignored issues
show
You must use "/**" style comments for a file comment
Loading history...
3
/*
4
 * PHPPgAdmin v6.0.0-beta.30
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
use PHPPgAdmin\Decorators\Decorator;
10
11
/**
12
 * Base controller class.
13
 */
14
class ColpropertiesController extends BaseController
15
{
16
    public $controller_name = 'ColpropertiesController';
17
    public $tableName       = '';
18
    public $table_place     = 'colproperties-colproperties';
19
20
    /**
21
     * Default method to render the controller according to the action parameter.
22
     */
23
    public function render()
24
    {
25
        if (isset($_REQUEST['table'])) {
26
            $this->tableName = &$_REQUEST['table'];
27
        } elseif (isset($_REQUEST['view'])) {
28
            $this->tableName = &$_REQUEST['view'];
29
        } else {
30
            die($lang['strnotableprovided']);
31
        }
32
33
        $lang   = $this->lang;
34
        $action = $this->action;
35
36
        $this->printHeader($lang['strtables'] . ' - ' . $this->tableName, null, true, 'header_select2.twig');
37
        $this->printBody();
38
39
        if (isset($_REQUEST['view'])) {
40
            $this->doDefault(null, false);
41
        } else {
42
            switch ($action) {
43
                case 'properties':
44
                    if (isset($_POST['cancel'])) {
45
                        $this->doDefault();
46
                    } else {
47
                        $this->doAlter();
48
                    }
49
50
                    break;
51
                default:
52
                    $this->doDefault();
53
54
                    break;
55
            }
56
        }
57
58
        $this->printFooter();
59
    }
60
61
    /**
62
     * Show default list of columns in the table.
63
     *
64
     * @param mixed $msg
1 ignored issue
show
Missing parameter comment
Loading history...
65
     * @param mixed $isTable
1 ignored issue
show
Missing parameter comment
Loading history...
66
     */
67
    public function doDefault($msg = '', $isTable = true)
68
    {
69
        $lang = $this->lang;
70
        $data = $this->misc->getDatabaseAccessor();
71
72
        $attPre = function (&$rowdata) use ($data) {
73
            $rowdata->fields['+type'] = $data->formatType($rowdata->fields['type'], $rowdata->fields['atttypmod']);
74
        };
75
76
        if (empty($_REQUEST['column'])) {
77
            $msg .= "<br/>{$lang['strnoobjects']}";
78
        }
79
80
        $this->printTrail('column');
81
        //$this->printTitle($lang['strcolprop']);
82
        $this->printTabs('column', 'properties');
83
        $this->printMsg($msg);
84
85
        if (!empty($_REQUEST['column'])) {
86
            // Get table
87
            $tdata = $data->getTable($this->tableName);
88
            // Get columns
89
            $attrs = $data->getTableAttributes($this->tableName, $_REQUEST['column']);
90
91
            // Show comment if any
92
            if (null !== $attrs->fields['comment']) {
93
                echo '<p class="comment">', $this->misc->printVal($attrs->fields['comment']), "</p>\n";
94
            }
95
96
            $column = [
97
                'column' => [
98
                    'title' => $lang['strcolumn'],
99
                    'field' => Decorator::field('attname'),
100
                ],
101
                'type'   => [
102
                    'title' => $lang['strtype'],
103
                    'field' => Decorator::field('+type'),
104
                ],
105
            ];
106
107
            if ($isTable) {
108
                $column['notnull'] = [
109
                    'title'  => $lang['strnotnull'],
110
                    'field'  => Decorator::field('attnotnull'),
111
                    'type'   => 'bool',
112
                    'params' => ['true' => 'NOT NULL', 'false' => ''],
113
                ];
114
                $column['default'] = [
115
                    'title' => $lang['strdefault'],
116
                    'field' => Decorator::field('adsrc'),
117
                ];
118
            }
119
120
            $actions = [];
121
            echo $this->printTable($attrs, $column, $actions, $this->table_place, null, $attPre);
122
123
            echo "<br />\n";
124
125
            $f_attname = $_REQUEST['column'];
126
            $f_table   = $this->tableName;
127
            $f_schema  = $data->_schema;
128
            $data->fieldClean($f_attname);
129
            $data->fieldClean($f_table);
130
            $data->fieldClean($f_schema);
131
            $query = "SELECT \"{$f_attname}\", count(*) AS \"count\" FROM \"{$f_schema}\".\"{$f_table}\" GROUP BY \"{$f_attname}\" ORDER BY \"{$f_attname}\"";
132
133
            if ($isTable) {
134
                // Browse link
135
                /* FIXME browsing a col should somehow be a action so we don't
136
                 * send an ugly SQL in the URL */
137
138
                $navlinks = [
139
                    'browse' => [
140
                        'attr'    => [
141
                            'href' => [
142
                                'url'     => 'display.php',
143
                                'urlvars' => [
144
                                    'subject'  => 'column',
145
                                    'server'   => $_REQUEST['server'],
146
                                    'database' => $_REQUEST['database'],
147
                                    'schema'   => $_REQUEST['schema'],
148
                                    'table'    => $this->tableName,
149
                                    'column'   => $_REQUEST['column'],
150
                                    'return'   => 'column',
151
                                    'query'    => $query,
152
                                ],
153
                            ],
154
                        ],
155
                        'content' => $lang['strbrowse'],
156
                    ],
157
                    'alter'  => [
158
                        'attr'    => [
159
                            'href' => [
160
                                'url'     => 'colproperties.php',
161
                                'urlvars' => [
162
                                    'action'   => 'properties',
163
                                    'server'   => $_REQUEST['server'],
164
                                    'database' => $_REQUEST['database'],
165
                                    'schema'   => $_REQUEST['schema'],
166
                                    'table'    => $this->tableName,
167
                                    'column'   => $_REQUEST['column'],
168
                                ],
169
                            ],
170
                        ],
171
                        'content' => $lang['stralter'],
172
                    ],
173
                    'drop'   => [
174
                        'attr'    => [
175
                            'href' => [
176
                                'url'     => 'tblproperties.php',
177
                                'urlvars' => [
178
                                    'action'   => 'confirm_drop',
179
                                    'server'   => $_REQUEST['server'],
180
                                    'database' => $_REQUEST['database'],
181
                                    'schema'   => $_REQUEST['schema'],
182
                                    'table'    => $this->tableName,
183
                                    'column'   => $_REQUEST['column'],
184
                                ],
185
                            ],
186
                        ],
187
                        'content' => $lang['strdrop'],
188
                    ],
189
                ];
190
            } else {
191
                // Browse link
192
                $navlinks = [
193
                    'browse' => [
194
                        'attr'    => [
195
                            'href' => [
196
                                'url'     => 'display.php',
197
                                'urlvars' => [
198
                                    'subject'  => 'column',
199
                                    'server'   => $_REQUEST['server'],
200
                                    'database' => $_REQUEST['database'],
201
                                    'schema'   => $_REQUEST['schema'],
202
                                    'view'     => $this->tableName,
203
                                    'column'   => $_REQUEST['column'],
204
                                    'return'   => 'column',
205
                                    'query'    => $query,
206
                                ],
207
                            ],
208
                        ],
209
                        'content' => $lang['strbrowse'],
210
                    ],
211
                ];
212
            }
213
214
            $this->printNavLinks($navlinks, $this->table_place, get_defined_vars());
215
        }
216
    }
217
218
    /**
219
     * Displays a screen where they can alter a column.
220
     *
221
     * @param mixed $msg
1 ignored issue
show
Missing parameter comment
Loading history...
222
     */
223
    public function doAlter($msg = '')
224
    {
225
        $lang = $this->lang;
226
        $data = $this->misc->getDatabaseAccessor();
227
228
        if (!isset($_REQUEST['stage'])) {
229
            $_REQUEST['stage'] = 1;
230
        }
231
232
        $this->prtrace('$_REQUEST', $_REQUEST, 'msg', $msg);
233
234
        switch ($_REQUEST['stage']) {
235
            case 1:
236
                $this->printTrail('column');
237
                $this->printTitle($lang['stralter'], 'pg.column.alter');
238
                $this->printMsg($msg);
239
240
                echo '<script src="' . \SUBFOLDER . '/js/tables.js" type="text/javascript"></script>';
241
                echo '<form action="' . \SUBFOLDER . "/src/views/colproperties.php\" method=\"post\">\n";
242
243
                // Output table header
244
                echo "<table>\n";
245
                echo "<tr><th class=\"data required\">{$lang['strname']}</th>\n";
246
                if ($data->hasAlterColumnType()) {
247
                    echo "<th class=\"data required\" colspan=\"2\">{$lang['strtype']}</th>\n";
248
                    echo "<th class=\"data\">{$lang['strlength']}</th>\n";
249
                } else {
250
                    echo "<th class=\"data required\">{$lang['strtype']}</th>\n";
251
                }
252
                echo "<th class=\"data\">{$lang['strnotnull']}</th>\n<th class=\"data\">{$lang['strdefault']}</th>\n<th class=\"data\">{$lang['strcomment']}</th></tr>\n";
253
254
                $column                       = $data->getTableAttributes($_REQUEST['table'], $_REQUEST['column']);
255
                $column->fields['attnotnull'] = $data->phpBool($column->fields['attnotnull']);
256
257
                // Upon first drawing the screen, load the existing column information
258
                // from the database.
259
                if (!isset($_REQUEST['default'])) {
260
                    $_REQUEST['field'] = $column->fields['attname'];
261
                    $_REQUEST['type']  = $column->fields['base_type'];
262
                    // Check to see if its' an array type...
263
                    // XXX: HACKY
264
                    if ('[]' == substr($column->fields['base_type'], strlen($column->fields['base_type']) - 2)) {
265
                        $_REQUEST['type']  = substr($column->fields['base_type'], 0, strlen($column->fields['base_type']) - 2);
266
                        $_REQUEST['array'] = '[]';
267
                    } else {
268
                        $_REQUEST['type']  = $column->fields['base_type'];
269
                        $_REQUEST['array'] = '';
270
                    }
271
                    // To figure out the length, look in the brackets :(
272
                    // XXX: HACKY
273
                    if ($column->fields['type'] != $column->fields['base_type'] && preg_match('/\\(([0-9, ]*)\\)/', $column->fields['type'], $bits)) {
274
                        $_REQUEST['length'] = $bits[1];
275
                    } else {
276
                        $_REQUEST['length'] = '';
277
                    }
278
279
                    $_REQUEST['default'] = $_REQUEST['olddefault'] = $column->fields['adsrc'];
280
                    if ($column->fields['attnotnull']) {
281
                        $_REQUEST['notnull'] = 'YES';
282
                    }
283
284
                    $_REQUEST['comment'] = $column->fields['comment'];
285
                }
286
287
                // Column name
288
                echo "<tr><td><input name=\"field\" size=\"16\" maxlength=\"{$data->_maxNameLen}\" value=\"",
289
                htmlspecialchars($_REQUEST['field']), "\" /></td>\n";
290
291
                // Column type
292
                $escaped_predef_types = []; // the JS escaped array elements
293
                if ($data->hasAlterColumnType()) {
294
                    // Fetch all available types
295
                    $types        = $data->getTypes(true, false, true);
296
                    $types_for_js = [];
297
298
                    echo "<td><select name=\"type\" id=\"type\" class=\"select2\" onchange=\"checkLengths(document.getElementById('type').value,'');\">" . "\n";
299
                    while (!$types->EOF) {
300
                        $typname        = $types->fields['typname'];
301
                        $types_for_js[] = $typname;
302
                        echo "\t<option value=\"", htmlspecialchars($typname), '"', ($typname == $_REQUEST['type']) ? ' selected="selected"' : '', '>',
303
                        $this->misc->printVal($typname), "</option>\n";
304
                        $types->moveNext();
305
                    }
306
                    echo "</select>\n";
307
                    echo "</td>\n";
308
309
                    // Output array type selector
310
                    echo "<td><select name=\"array\">\n";
311
                    echo "\t<option value=\"\"", ('' == $_REQUEST['array']) ? ' selected="selected"' : '', "></option>\n";
312
                    echo "\t<option value=\"[]\"", ('[]' == $_REQUEST['array']) ? ' selected="selected"' : '', ">[ ]</option>\n";
313
                    echo "</select></td>\n";
314
                    $predefined_size_types = array_intersect($data->predefined_size_types, $types_for_js);
315
                    foreach ($predefined_size_types as $value) {
316
                        $escaped_predef_types[] = "'{$value}'";
317
                    }
318
319
                    echo '<td><input name="length" id="lengths" size="8" value="',
320
                    htmlspecialchars($_REQUEST['length']), "\" /></td>\n";
321
                } else {
322
                    // Otherwise draw the read-only type name
323
                    echo '<td>', $this->misc->printVal($data->formatType($column->fields['type'], $column->fields['atttypmod'])), "</td>\n";
324
                }
325
326
                echo '<td><input type="checkbox" name="notnull"', (isset($_REQUEST['notnull'])) ? ' checked="checked"' : '', " /></td>\n";
327
                echo '<td><input name="default" size="20" value="',
328
                htmlspecialchars($_REQUEST['default']), "\" /></td>\n";
329
                echo '<td><input name="comment" size="40" value="',
330
                htmlspecialchars($_REQUEST['comment']), "\" /></td></tr>\n";
331
                echo "</table>\n";
332
                echo "<p><input type=\"hidden\" name=\"action\" value=\"properties\" />\n";
333
                echo "<input type=\"hidden\" name=\"stage\" value=\"2\" />\n";
334
                echo $this->misc->form;
335
                echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n";
336
                echo '<input type="hidden" name="column" value="', htmlspecialchars($_REQUEST['column']), "\" />\n";
337
                echo '<input type="hidden" name="olddefault" value="', htmlspecialchars($_REQUEST['olddefault']), "\" />\n";
338
                if ($column->fields['attnotnull']) {
339
                    echo "<input type=\"hidden\" name=\"oldnotnull\" value=\"on\" />\n";
340
                }
341
342
                echo '<input type="hidden" name="oldtype" value="', htmlspecialchars($data->formatType($column->fields['type'], $column->fields['atttypmod'])), "\" />\n";
343
                // Add hidden variables to suppress error notices if we don't support altering column type
344
                if (!$data->hasAlterColumnType()) {
345
                    echo '<input type="hidden" name="type" value="', htmlspecialchars($_REQUEST['type']), "\" />\n";
346
                    echo '<input type="hidden" name="length" value="', htmlspecialchars($_REQUEST['length']), "\" />\n";
347
                    echo '<input type="hidden" name="array" value="', htmlspecialchars($_REQUEST['array']), "\" />\n";
348
                }
349
                echo "<input type=\"submit\" value=\"{$lang['stralter']}\" />\n";
350
                echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
351
                echo "</form>\n";
352
                echo '<script type="text/javascript">predefined_lengths = new Array(' . implode(',', $escaped_predef_types) . ");checkLengths(document.getElementById('type').value,'');</script>\n";
353
354
                break;
355
            case 2:
356
                // Check inputs
357
                if ('' == trim($_REQUEST['field'])) {
358
                    $_REQUEST['stage'] = 1;
359
                    $this->doAlter($lang['strcolneedsname']);
360
361
                    return;
362
                }
363
                if (!isset($_REQUEST['length'])) {
364
                    $_REQUEST['length'] = '';
365
                }
366
367
                list($status, $sql) = $data->alterColumn(
368
                    $_REQUEST['table'],
369
                    $_REQUEST['column'],
370
                    $_REQUEST['field'],
371
                    isset($_REQUEST['notnull']),
372
                    isset($_REQUEST['oldnotnull']),
373
                    $_REQUEST['default'],
374
                    $_REQUEST['olddefault'],
375
                    $_REQUEST['type'],
376
                    $_REQUEST['length'],
377
                    $_REQUEST['array'],
378
                    $_REQUEST['oldtype'],
379
                    $_REQUEST['comment']
380
                );
381
382
                $this->prtrace('status', $status, 'sql', $sql);
383
                if (0 == $status) {
384
                    if ($_REQUEST['column'] != $_REQUEST['field']) {
385
                        $_REQUEST['column'] = $_REQUEST['field'];
386
                        $this->misc->setReloadBrowser(true);
387
                    }
388
                    $this->doDefault($sql . "<br/>{$lang['strcolumnaltered']}");
389
                } else {
390
                    $_REQUEST['stage'] = 1;
391
                    $this->doAlter($sql . "<br/>{$lang['strcolumnalteredbad']}");
392
393
                    return;
394
                }
395
396
                break;
397
            default:
398
                echo "<p>{$lang['strinvalidparam']}</p>\n";
399
        }
400
    }
401
}
402