1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* PHPPgAdmin v6.0.0-beta.33 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace PHPPgAdmin\Controller; |
8
|
|
|
|
9
|
|
|
use PHPPgAdmin\Decorators\Decorator; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Base controller class. |
13
|
|
|
* |
14
|
|
|
* @package PHPPgAdmin |
15
|
|
|
*/ |
16
|
|
|
class SqleditController extends BaseController |
17
|
|
|
{ |
18
|
|
|
public $controller_name = 'SqleditController'; |
19
|
|
|
public $query = ''; |
20
|
|
|
public $subject = ''; |
21
|
|
|
public $start_time; |
22
|
|
|
public $duration; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Default method to render the controller according to the action parameter. |
26
|
|
|
*/ |
27
|
|
|
public function render() |
28
|
|
|
{ |
29
|
|
|
$lang = $this->lang; |
|
|
|
|
30
|
|
|
|
31
|
|
|
$action = $this->action; |
32
|
|
|
|
33
|
|
|
if ('tree' == $action) { |
34
|
|
|
return $this->doTree(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
switch ($action) { |
38
|
|
|
case 'find': |
39
|
|
|
$title = $this->lang['strfind']; |
40
|
|
|
$body_text = $this->doFind(); |
41
|
|
|
|
42
|
|
|
break; |
43
|
|
|
case 'sql': |
44
|
|
|
default: |
45
|
|
|
$title = $this->lang['strsql']; |
46
|
|
|
$body_text = $this->doDefault(); |
47
|
|
|
|
48
|
|
|
break; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this->setWindowName('sqledit'); |
52
|
|
|
|
53
|
|
|
$this->scripts = '<script type="text/javascript">window.inPopUp=true;</script>'; |
54
|
|
|
|
55
|
|
|
$this->printHeader($title, $this->scripts, true, 'header_sqledit.twig'); |
56
|
|
|
$this->printBody(true, 'sql_edit'); |
57
|
|
|
echo $body_text; |
58
|
|
|
|
59
|
|
|
$this->printFooter(true, 'footer_sqledit.twig'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Allow execution of arbitrary SQL statements on a database. |
64
|
|
|
*/ |
65
|
|
|
public function doDefault() |
66
|
|
|
{ |
67
|
|
|
$lang = $this->lang; |
68
|
|
|
$data = $this->misc->getDatabaseAccessor(); |
69
|
|
|
|
70
|
|
|
if (!isset($_SESSION['sqlquery'])) { |
71
|
|
|
$_SESSION['sqlquery'] = ''; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (!isset($_REQUEST['search_path'])) { |
75
|
|
|
$_REQUEST['search_path'] = implode(',', $data->getSearchPath()); |
76
|
|
|
} |
77
|
|
|
$search_path = htmlspecialchars($_REQUEST['search_path']); |
78
|
|
|
$sqlquery = htmlspecialchars($_SESSION['sqlquery']); |
79
|
|
|
|
80
|
|
|
$default_html = $this->printTabs($this->misc->getNavTabs('popup'), 'sql', false); |
81
|
|
|
|
82
|
|
|
$default_html .= '<form action="' . \SUBFOLDER . '/src/views/sql" method="post" enctype="multipart/form-data" class="sqlform" id="sqlform" target="detail">'; |
83
|
|
|
$default_html .= "\n"; |
84
|
|
|
$default_html .= $this->_printConnection('sql'); |
85
|
|
|
|
86
|
|
|
$default_html .= "\n"; |
87
|
|
|
|
88
|
|
|
$default_html .= ' <div class="searchpath">'; |
89
|
|
|
$default_html .= '<label>'; |
90
|
|
|
$default_html .= $this->misc->printHelp($lang['strsearchpath'], 'pg.schema.search_path', false); |
91
|
|
|
|
92
|
|
|
$default_html .= ': <input type="text" name="search_path" size="45" value="' . $search_path . '" />'; |
93
|
|
|
$default_html .= "</label>\n"; |
94
|
|
|
|
95
|
|
|
$default_html .= "</div>\n"; |
96
|
|
|
|
97
|
|
|
$default_html .= '<div id="queryedition" style="padding:1%;width:98%;float:left;">'; |
98
|
|
|
$default_html .= "\n"; |
99
|
|
|
$default_html .= '<textarea style="width:98%;" rows="10" cols="50" name="query" id="query" resizable="true">' . $sqlquery . '</textarea>'; |
100
|
|
|
$default_html .= "\n"; |
101
|
|
|
$default_html .= "</div>\n"; |
102
|
|
|
|
103
|
|
|
$default_html .= '<div class="sqledit_bottom_inputs" >'; |
104
|
|
|
|
105
|
|
|
if (ini_get('file_uploads')) { |
106
|
|
|
// Don't show upload option if max size of uploads is zero |
107
|
|
|
$max_size = $this->misc->inisizeToBytes(ini_get('upload_max_filesize')); |
108
|
|
|
if (is_double($max_size) && $max_size > 0) { |
109
|
|
|
$default_html .= '<p class="upload_sql_script">'; |
110
|
|
|
$default_html .= '<input type="hidden" name="MAX_FILE_SIZE" value="' . $max_size . '" />'; |
111
|
|
|
$default_html .= "\n"; |
112
|
|
|
$default_html .= '<label for="script">' . $lang['struploadscript'] . '</label>'; |
113
|
|
|
$default_html .= ' <input class="btn btn-small" id="script" name="script" type="file" /></p>'; |
114
|
|
|
$default_html .= "</p>\n"; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// Check that file uploads are enabled |
119
|
|
|
$checked = (isset($_REQUEST['paginate']) ? ' checked="checked"' : ''); |
120
|
|
|
|
121
|
|
|
$default_html .= '<p><input type="submit" class="btn btn-small" name="execute" accesskey="r" value="' . $lang['strexecute'] . '" />'; |
122
|
|
|
$default_html .= "\n"; |
123
|
|
|
|
124
|
|
|
$default_html .= '<input type="reset" class="btn btn-small" accesskey="q" value="' . $lang['strreset'] . '" /></p>'; |
125
|
|
|
$default_html .= "\n"; |
126
|
|
|
|
127
|
|
|
$default_html .= '<p>'; |
128
|
|
|
$default_html .= '<label for="paginate">'; |
129
|
|
|
$default_html .= '<input type="checkbox" id="paginate" name="paginate"' . $checked . ' /> ' . $lang['strpaginate'] . ' '; |
130
|
|
|
$default_html .= "</label>\n"; |
131
|
|
|
$default_html .= "</p>\n"; |
132
|
|
|
|
133
|
|
|
$default_html .= "</div>\n"; |
134
|
|
|
$default_html .= '</form>'; |
135
|
|
|
$default_html .= "\n"; |
136
|
|
|
|
137
|
|
|
// Default focus |
138
|
|
|
//$this->setFocus('forms[0].query'); |
139
|
|
|
return $default_html; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function doTree() |
143
|
|
|
{ |
144
|
|
|
$treedata = new \PHPPgAdmin\ArrayRecordSet([]); |
145
|
|
|
$reqvars = []; |
146
|
|
|
|
147
|
|
|
$attrs = [ |
148
|
|
|
'text' => 'Servers', |
149
|
|
|
'icon' => 'Servers', |
150
|
|
|
'is_root' => 'true', |
151
|
|
|
'action' => Decorator::url('/src/views/servers'), |
152
|
|
|
'branch' => Decorator::url('/src/views/servers', $reqvars, ['action' => 'tree']), |
153
|
|
|
]; |
154
|
|
|
|
155
|
|
|
return $this->printTree($treedata, $attrs, 'server'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Private function to display server and list of databases. |
160
|
|
|
* |
161
|
|
|
* @param mixed $action |
162
|
|
|
*/ |
163
|
|
|
public function _printConnection($action) |
|
|
|
|
164
|
|
|
{ |
165
|
|
|
$lang = $this->lang; |
|
|
|
|
166
|
|
|
$data = $this->misc->getDatabaseAccessor(); |
|
|
|
|
167
|
|
|
|
168
|
|
|
// The javascript action on the select box reloads the |
169
|
|
|
// popup whenever the server or database is changed. |
170
|
|
|
// This ensures that the correct page encoding is used. |
171
|
|
|
$onchange = "onchange=\"location.href='" . \SUBFOLDER . '/sqledit/' . |
172
|
|
|
urlencode($action) . "?server=' + encodeURI(server.options[server.selectedIndex].value) + '&database=' + encodeURI(database.options[database.selectedIndex].value) + "; |
173
|
|
|
|
174
|
|
|
// The exact URL to reload to is different between SQL and Find mode, however. |
175
|
|
|
if ('find' == $action) { |
176
|
|
|
$onchange .= "'&term=' + encodeURI(term.value) + '&filter=' + encodeURI(filter.value) + '&'\""; |
177
|
|
|
} else { |
178
|
|
|
$onchange .= "'&query=' + encodeURI(query.value) + '&search_path=' + encodeURI(search_path.value) + (paginate.checked ? '&paginate=on' : '') + '&'\""; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $this->misc->printConnection($onchange, false); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Searches for a named database object. |
186
|
|
|
*/ |
187
|
|
|
public function doFind() |
188
|
|
|
{ |
189
|
|
|
$lang = $this->lang; |
190
|
|
|
$data = $this->misc->getDatabaseAccessor(); |
191
|
|
|
|
192
|
|
|
if (!isset($_REQUEST['term'])) { |
193
|
|
|
$_REQUEST['term'] = ''; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
if (!isset($_REQUEST['filter'])) { |
197
|
|
|
$_REQUEST['filter'] = ''; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
$default_html = $this->printTabs($this->misc->getNavTabs('popup'), 'find', false); |
201
|
|
|
|
202
|
|
|
$default_html .= "<form action=\"database\" method=\"post\" target=\"detail\">\n"; |
203
|
|
|
$default_html .= $this->_printConnection('find'); |
204
|
|
|
$default_html .= '<p><input class="focusme" name="term" value="' . htmlspecialchars($_REQUEST['term']) . "\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" />\n"; |
205
|
|
|
|
206
|
|
|
// Output list of filters. This is complex due to all the 'has' and 'conf' feature possibilities |
207
|
|
|
$default_html .= "<select name=\"filter\">\n"; |
208
|
|
|
$default_html .= "\t<option value=\"\"" . ('' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['strallobjects']}</option>\n"; |
209
|
|
|
$default_html .= "\t<option value=\"SCHEMA\"" . ('SCHEMA' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['strschemas']}</option>\n"; |
210
|
|
|
$default_html .= "\t<option value=\"TABLE\"" . ('TABLE' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['strtables']}</option>\n"; |
211
|
|
|
$default_html .= "\t<option value=\"VIEW\"" . ('VIEW' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['strviews']}</option>\n"; |
212
|
|
|
$default_html .= "\t<option value=\"SEQUENCE\"" . ('SEQUENCE' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['strsequences']}</option>\n"; |
213
|
|
|
$default_html .= "\t<option value=\"COLUMN\"" . ('COLUMN' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['strcolumns']}</option>\n"; |
214
|
|
|
$default_html .= "\t<option value=\"RULE\"" . ('RULE' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['strrules']}</option>\n"; |
215
|
|
|
$default_html .= "\t<option value=\"INDEX\"" . ('INDEX' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['strindexes']}</option>\n"; |
216
|
|
|
$default_html .= "\t<option value=\"TRIGGER\"" . ('TRIGGER' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['strtriggers']}</option>\n"; |
217
|
|
|
$default_html .= "\t<option value=\"CONSTRAINT\"" . ('CONSTRAINT' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['strconstraints']}</option>\n"; |
218
|
|
|
$default_html .= "\t<option value=\"FUNCTION\"" . ('FUNCTION' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['strfunctions']}</option>\n"; |
219
|
|
|
$default_html .= "\t<option value=\"DOMAIN\"" . ('DOMAIN' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['strdomains']}</option>\n"; |
220
|
|
|
if ($this->conf['show_advanced']) { |
221
|
|
|
$default_html .= "\t<option value=\"AGGREGATE\"" . ('AGGREGATE' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['straggregates']}</option>\n"; |
222
|
|
|
$default_html .= "\t<option value=\"TYPE\"" . ('TYPE' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['strtypes']}</option>\n"; |
223
|
|
|
$default_html .= "\t<option value=\"OPERATOR\"" . ('OPERATOR' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['stroperators']}</option>\n"; |
224
|
|
|
$default_html .= "\t<option value=\"OPCLASS\"" . ('OPCLASS' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['stropclasses']}</option>\n"; |
225
|
|
|
$default_html .= "\t<option value=\"CONVERSION\"" . ('CONVERSION' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['strconversions']}</option>\n"; |
226
|
|
|
$default_html .= "\t<option value=\"LANGUAGE\"" . ('LANGUAGE' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['strlanguages']}</option>\n"; |
227
|
|
|
} |
228
|
|
|
$default_html .= "</select>\n"; |
229
|
|
|
|
230
|
|
|
$default_html .= "<input type=\"submit\" value=\"{$lang['strfind']}\" />\n"; |
231
|
|
|
$default_html .= "<input type=\"hidden\" name=\"action\" value=\"find\" /></p>\n"; |
232
|
|
|
$default_html .= "</form>\n"; |
233
|
|
|
|
234
|
|
|
// Default focus |
235
|
|
|
$this->setFocus('forms[0].term'); |
236
|
|
|
|
237
|
|
|
return $default_html; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|