Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 331 lines (319 sloc) 13.141 kB
a7096dd Separate editing functions
jakubvrana authored
1 <?php
471ae2b Move select function
jakubvrana authored
2 /** Print select result
3 * @param Min_Result
4 * @param Min_DB connection to examine indexes
5 * @return null
6 */
64d616c Rename get_dbh to connection
jakubvrana authored
7 function select($result, $connection2 = null) {
471ae2b Move select function
jakubvrana authored
8 if (!$result->num_rows) {
9 echo "<p class='message'>" . lang('No rows.') . "\n";
10 } else {
11 echo "<table cellspacing='0' class='nowrap'>\n";
12 $links = array(); // colno => orgtable - create links from these columns
13 $indexes = array(); // orgtable => array(column => colno) - primary keys
14 $columns = array(); // orgtable => array(column => ) - not selected columns in primary key
15 $blobs = array(); // colno => bool - display bytes for blobs
16 $types = array(); // colno => type - display char in <code>
17 odd(''); // reset odd for each result
18 for ($i=0; $row = $result->fetch_row(); $i++) {
19 if (!$i) {
20 echo "<thead><tr>";
21 for ($j=0; $j < count($row); $j++) {
22 $field = $result->fetch_field();
7da9cce Display original column name in select
jakubvrana authored
23 $orgtable = $field->orgtable;
24 $orgname = $field->orgname;
25 if ($orgtable != "") {
26 if (!isset($indexes[$orgtable])) {
471ae2b Move select function
jakubvrana authored
27 // find primary key in each table
7da9cce Display original column name in select
jakubvrana authored
28 $indexes[$orgtable] = array();
29 foreach (indexes($orgtable, $connection2) as $index) {
471ae2b Move select function
jakubvrana authored
30 if ($index["type"] == "PRIMARY") {
7da9cce Display original column name in select
jakubvrana authored
31 $indexes[$orgtable] = array_flip($index["columns"]);
471ae2b Move select function
jakubvrana authored
32 break;
33 }
34 }
7da9cce Display original column name in select
jakubvrana authored
35 $columns[$orgtable] = $indexes[$orgtable];
471ae2b Move select function
jakubvrana authored
36 }
7da9cce Display original column name in select
jakubvrana authored
37 if (isset($columns[$orgtable][$orgname])) {
38 unset($columns[$orgtable][$orgname]);
39 $indexes[$orgtable][$orgname] = $j;
40 $links[$j] = $orgtable;
471ae2b Move select function
jakubvrana authored
41 }
42 }
a29ac72 Improve drivers
jakubvrana authored
43 if ($field->charsetnr == 63) { // 63 - binary
471ae2b Move select function
jakubvrana authored
44 $blobs[$j] = true;
45 }
46 $types[$j] = $field->type;
7da9cce Display original column name in select
jakubvrana authored
47 echo "<th" . ($orgtable != "" || $field->name != $orgname ? " title='" . h(($orgtable != "" ? "$orgtable." : "") . $orgname) . "'" : "") . ">" . h($field->name);
471ae2b Move select function
jakubvrana authored
48 }
49 echo "</thead>\n";
50 }
51 echo "<tr" . odd() . ">";
52 foreach ($row as $key => $val) {
53 if (!isset($val)) {
54 $val = "<i>NULL</i>";
55 } else {
56 if ($blobs[$key] && !is_utf8($val)) {
57 $val = "<i>" . lang('%d byte(s)', strlen($val)) . "</i>"; //! link to download
984d090 SQLite variables
jakubvrana authored
58 } elseif (!strlen($val)) { // strlen - SQLite can return int
471ae2b Move select function
jakubvrana authored
59 $val = "&nbsp;"; // some content to print a border
60 } else {
50881f8 Move whitespace to CSS
jakubvrana authored
61 $val = h($val);
a29ac72 Improve drivers
jakubvrana authored
62 if ($types[$key] == 254) { // 254 - char
471ae2b Move select function
jakubvrana authored
63 $val = "<code>$val</code>";
64 }
65 }
66 if (isset($links[$key]) && !$columns[$links[$key]]) {
67 $link = "edit=" . urlencode($links[$key]);
68 foreach ($indexes[$links[$key]] as $col => $j) {
69 $link .= "&where" . urlencode("[" . bracket_escape($col) . "]") . "=" . urlencode($row[$j]);
70 }
71 $val = "<a href='" . h(ME . $link) . "'>$val</a>";
72 }
73 }
74 echo "<td>$val";
75 }
76 }
77 echo "</table>\n";
78 }
79 }
80
1118566 Documentation
jakubvrana authored
81 /** Get referencable tables with single column primary key except self
82 * @param string
83 * @return array ($table_name => $field)
84 */
ced9de9 Create single column foreign key in table structure
jakubvrana authored
85 function referencable_primary($self) {
86 $return = array(); // table_name => field
3f5b683 Reintegrate sqlite branch
jakubvrana authored
87 foreach (table_status() as $table_name => $table) {
88 if ($table_name != $self && fk_support($table)) {
ced9de9 Create single column foreign key in table structure
jakubvrana authored
89 foreach (fields($table_name) as $field) {
90 if ($field["primary"]) {
91 if ($return[$table_name]) { // multi column primary key
92 unset($return[$table_name]);
93 break;
94 }
95 $return[$table_name] = $field;
96 }
97 }
98 }
99 }
100 return $return;
101 }
102
1118566 Documentation
jakubvrana authored
103 /** Print table columns for type edit
104 * @param string
105 * @param array
106 * @param array
107 * @param array returned by referencable_primary()
108 * @return null
109 */
ced9de9 Create single column foreign key in table structure
jakubvrana authored
110 function edit_type($key, $field, $collations, $foreign_keys = array()) {
dc4fc82 @vrana ON DELETE action for foreign keys in Create table
authored
111 global $structured_types, $unsigned, $inout, $on_actions;
f2813b5 Don't hide/show added/dropped columns (bug 1920167)
jakubvrana authored
112 ?>
3c5c0f0 Use camelCase in JavaScript
jakubvrana authored
113 <td><select name="<?php echo $key; ?>[type]" class="type" onfocus="lastType = selectValue(this);" onchange="editingTypeChange(this);"><?php echo optionlist($structured_types + ($foreign_keys ? array(lang('Foreign keys') => $foreign_keys) : array()), $field["type"]); ?></select>
3a76704 Spaces
jakubvrana authored
114 <td><input name="<?php echo $key; ?>[length]" value="<?php echo h($field["length"]); ?>" size="3" onfocus="editingLengthFocus(this);"><td><?php
5cac902 Work with default values
jakubvrana authored
115 echo "<select name='$key" . "[collation]'" . (ereg('(char|text|enum|set)$', $field["type"]) ? "" : " class='hidden'") . '><option value="">(' . lang('collation') . ')' . optionlist($collations, $field["collation"]) . '</select>';
dc4fc82 @vrana ON DELETE action for foreign keys in Create table
authored
116 echo ($unsigned ? "<select name='$key" . "[unsigned]'" . (!$field["type"] || ereg('(int|float|double|decimal)$', $field["type"]) ? "" : " class='hidden'") . '><option>' . optionlist($unsigned, $field["unsigned"]) . '</select>' : '');
117 echo ($foreign_keys ? "<select name='$key" . "[on_delete]'" . (ereg("`", $field["type"]) ? "" : " class='hidden'") . "><option value=''>(" . lang('ON DELETE') . ")" . optionlist($on_actions, $field["on_delete"]) . "</select> " : " "); // space for IE
a7096dd Separate editing functions
jakubvrana authored
118 }
119
1118566 Documentation
jakubvrana authored
120 /** Filter length value including enums
121 * @param string
122 * @return string
123 */
f93c84e Function process_length used only in Adminer
jakubvrana authored
124 function process_length($length) {
125 global $enum_length;
126 return (preg_match("~^\\s*(?:$enum_length)(?:\\s*,\\s*(?:$enum_length))*\\s*\$~", $length) && preg_match_all("~$enum_length~", $length, $matches) ? implode(",", $matches[0]) : preg_replace('~[^0-9,+-]~', '', $length));
127 }
128
1118566 Documentation
jakubvrana authored
129 /** Create SQL string from field type
130 * @param array
131 * @param string
132 * @return string
133 */
a3c969c Routines uses CHARACTER SET
jakubvrana authored
134 function process_type($field, $collate = "COLLATE") {
4780703 @vrana Respect PostgreSQL custom types
authored
135 global $types, $connection, $unsigned;
136 $type = $field["type"];
137 return " " . (isset($types[$type]) ? $type : idf_escape($type))
138 . ($field["length"] != "" ? "(" . process_length($field["length"]) . ")" : "")
5abd943 Change simple preg_match to ereg
jakubvrana authored
139 . (ereg('int|float|double|decimal', $field["type"]) && in_array($field["unsigned"], $unsigned) ? " $field[unsigned]" : "")
64d616c Rename get_dbh to connection
jakubvrana authored
140 . (ereg('char|text|enum|set', $field["type"]) && $field["collation"] ? " $collate " . $connection->quote($field["collation"]) : "")
a7096dd Separate editing functions
jakubvrana authored
141 ;
142 }
143
1118566 Documentation
jakubvrana authored
144 /** Create SQL string from field
145 * @param array basic field information
146 * @param array information about field type
3f5b683 Reintegrate sqlite branch
jakubvrana authored
147 * @return array array("field", "type", "NULL", "DEFAULT", "ON UPDATE", "COMMENT", "AUTO_INCREMENT")
1118566 Documentation
jakubvrana authored
148 */
2e680bc Speedup of simple alter table
jakubvrana authored
149 function process_field($field, $type_field) {
64d616c Rename get_dbh to connection
jakubvrana authored
150 global $connection;
3f5b683 Reintegrate sqlite branch
jakubvrana authored
151 return array(
152 idf_escape($field["field"]),
153 process_type($type_field),
154 ($field["null"] ? " NULL" : " NOT NULL"), // NULL for timestamp
155 (isset($field["default"]) ? " DEFAULT " . ($field["type"] == "timestamp" && eregi("^CURRENT_TIMESTAMP$", $field["default"]) ? $field["default"] : $connection->quote($field["default"])) : ""),
156 ($field["on_update"] ? " ON UPDATE $field[on_update]" : ""),
157 (support("comment") && $field["comment"] != "" ? " COMMENT " . $connection->quote($field["comment"]) : ""),
736d6e4 @vrana Fix alter table
authored
158 ($field["auto_increment"] ? auto_increment() : null),
3f5b683 Reintegrate sqlite branch
jakubvrana authored
159 );
2e680bc Speedup of simple alter table
jakubvrana authored
160 }
161
1118566 Documentation
jakubvrana authored
162 /** Get type class to use in CSS
163 * @param string
164 * @return string class=''
165 */
3015aed Separate type_class
jakubvrana authored
166 function type_class($type) {
d144e9c Save bytes
jakubvrana authored
167 foreach (array(
168 'char' => 'text',
169 'date' => 'time|year',
170 'binary' => 'blob',
171 'enum' => 'set',
172 ) as $key => $val) {
c0c7ab3 Typo
jakubvrana authored
173 if (ereg("$key|$val", $type)) {
d144e9c Save bytes
jakubvrana authored
174 return " class='$key'";
175 }
3015aed Separate type_class
jakubvrana authored
176 }
177 }
178
1118566 Documentation
jakubvrana authored
179 /** Print table interior for fields editing
180 * @param array
181 * @param array
182 * @param string TABLE or PROCEDURE
183 * @param int number of fields allowed by Suhosin
184 * @param array returned by referencable_primary()
185 * @return bool column comments used
186 */
3f5b683 Reintegrate sqlite branch
jakubvrana authored
187 function edit_fields($fields, $collations, $type = "TABLE", $allowed = 0, $foreign_keys = array(), $comments = false) {
d31d4e9 Emulate REQUEST_URI
jakubvrana authored
188 global $inout;
540e217 Separate JavaScript
jakubvrana authored
189 foreach ($fields as $field) {
7352c28 Replace strlen() by != ""
jakubvrana authored
190 if ($field["comment"] != "") {
3f5b683 Reintegrate sqlite branch
jakubvrana authored
191 $comments = true;
1118566 Documentation
jakubvrana authored
192 break;
540e217 Separate JavaScript
jakubvrana authored
193 }
194 }
f2813b5 Don't hide/show added/dropped columns (bug 1920167)
jakubvrana authored
195 ?>
dfe2540 Centralize nowrap
jakubvrana authored
196 <thead><tr class="wrap">
b349612 Remove useless translations
jakubvrana authored
197 <?php if ($type == "PROCEDURE") { ?><td>&nbsp;<?php } ?>
ace55ed HTML instead of XHTML
jakubvrana authored
198 <th><?php echo ($type == "TABLE" ? lang('Column name') : lang('Parameter name')); ?>
3a76704 Spaces
jakubvrana authored
199 <td><?php echo lang('Type'); ?><textarea id="enum-edit" rows="4" cols="12" wrap="off" style="display: none;" onblur="editingLengthBlur(this);"></textarea>
ace55ed HTML instead of XHTML
jakubvrana authored
200 <td><?php echo lang('Length'); ?>
201 <td><?php echo lang('Options'); ?>
a7096dd Separate editing functions
jakubvrana authored
202 <?php if ($type == "TABLE") { ?>
b349612 Remove useless translations
jakubvrana authored
203 <td>NULL
3f5b683 Reintegrate sqlite branch
jakubvrana authored
204 <td><input type="radio" name="auto_increment_col" value=""><acronym title="<?php echo lang('Auto Increment'); ?>">AI</acronym>
4cead56 Edit default values directly in table creation
jakubvrana authored
205 <td class="hidden"><?php echo lang('Default values'); ?>
3f5b683 Reintegrate sqlite branch
jakubvrana authored
206 <?php echo (support("comment") ? "<td" . ($comments ? "" : " class='hidden'") . ">" . lang('Comment') : ""); ?>
a7096dd Separate editing functions
jakubvrana authored
207 <?php } ?>
da8a1b6 Respect move and drop column support
jakubvrana authored
208 <td><?php echo "<input type='image' name='add[" . (support("move_col") ? 0 : count($fields)) . "]' src='../adminer/static/plus.gif' alt='+' title='" . lang('Add next') . "'>"; ?><script type="text/javascript">row_count = <?php echo count($fields); ?>;</script>
ace55ed HTML instead of XHTML
jakubvrana authored
209 </thead>
a7096dd Separate editing functions
jakubvrana authored
210 <?php
93c27c9 Separate type_change()
jakubvrana authored
211 foreach ($fields as $i => $field) {
212 $i++;
da8a1b6 Respect move and drop column support
jakubvrana authored
213 $orig = $field[($_POST ? "orig" : "field")];
214 $display = (isset($_POST["add"][$i-1]) || (isset($field["field"]) && !$_POST["drop_col"][$i])) && (support("drop_col") || $orig == "");
93c27c9 Separate type_change()
jakubvrana authored
215 ?>
a76c555 Remove odd() from create
jakubvrana authored
216 <tr<?php echo ($display ? "" : " style='display: none;'"); ?>>
3f5b683 Reintegrate sqlite branch
jakubvrana authored
217 <?php echo ($type == "PROCEDURE" ? "<td>" . html_select("fields[$i][inout]", $inout, $field["inout"]) : ""); ?>
da8a1b6 Respect move and drop column support
jakubvrana authored
218 <th><?php if ($display) { ?><input name="fields[<?php echo $i; ?>][field]" value="<?php echo h($field["field"]); ?>" onchange="<?php echo ($field["field"] != "" || count($fields) > 1 ? "" : "editingAddRow(this, $allowed); "); ?>editingNameChange(this);" maxlength="64"><?php } ?><input type="hidden" name="fields[<?php echo $i; ?>][orig]" value="<?php echo h($orig); ?>">
ced9de9 Create single column foreign key in table structure
jakubvrana authored
219 <?php edit_type("fields[$i]", $field, $collations, $foreign_keys); ?>
a7096dd Separate editing functions
jakubvrana authored
220 <?php if ($type == "TABLE") { ?>
6b30cfa Separate checkbox
jakubvrana authored
221 <td><?php echo checkbox("fields[$i][null]", 1, $field["null"]); ?>
cb6d36c HTML instead of XHTML
jakubvrana authored
222 <td><input type="radio" name="auto_increment_col" value="<?php echo $i; ?>"<?php if ($field["auto_increment"]) { ?> checked<?php } ?>>
dfe2540 Centralize nowrap
jakubvrana authored
223 <td class="hidden"><?php echo checkbox("fields[$i][has_default]", 1, $field["has_default"]); ?><input name="fields[<?php echo $i; ?>][default]" value="<?php echo h($field["default"]); ?>" onchange="this.previousSibling.checked = true;">
3f5b683 Reintegrate sqlite branch
jakubvrana authored
224 <?php echo (support("comment") ? "<td" . ($comments ? "" : " class='hidden'") . "><input name='fields[$i][comment]' value='" . h($field["comment"]) . "' maxlength='255'>" : ""); ?>
a7096dd Separate editing functions
jakubvrana authored
225 <?php } ?>
226 <?php
da8a1b6 Respect move and drop column support
jakubvrana authored
227 echo "<td>";
228 echo (support("move_col") ?
229 "<input type='image' name='add[$i]' src='../adminer/static/plus.gif' alt='+' title='" . lang('Add next') . "' onclick='return !editingAddRow(this, $allowed, 1);'>&nbsp;"
230 . "<input type='image' name='up[$i]' src='../adminer/static/up.gif' alt='^' title='" . lang('Move up') . "'>&nbsp;"
231 . "<input type='image' name='down[$i]' src='../adminer/static/down.gif' alt='v' title='" . lang('Move down') . "'>&nbsp;"
232 : "");
233 echo ($orig == "" || support("drop_col") ? "<input type='image' name='drop_col[$i]' src='../adminer/static/cross.gif' alt='x' title='" . lang('Remove') . "' onclick='return !editingRemoveRow(this);'>" : "");
50881f8 Move whitespace to CSS
jakubvrana authored
234 echo "\n";
a7096dd Separate editing functions
jakubvrana authored
235 }
3f5b683 Reintegrate sqlite branch
jakubvrana authored
236 return $comments;
a7096dd Separate editing functions
jakubvrana authored
237 }
93c27c9 Separate type_change()
jakubvrana authored
238
1118566 Documentation
jakubvrana authored
239 /** Move fields up and down or add field
240 * @param array
241 * @return null
242 */
fd8948f Order of columns in table
jakubvrana authored
243 function process_fields(&$fields) {
244 ksort($fields);
245 $offset = 0;
246 if ($_POST["up"]) {
247 $last = 0;
248 foreach ($fields as $key => $field) {
249 if (key($_POST["up"]) == $key) {
250 unset($fields[$key]);
251 array_splice($fields, $last, 0, array($field));
252 break;
253 }
254 if (isset($field["field"])) {
255 $last = $offset;
256 }
257 $offset++;
258 }
259 }
260 if ($_POST["down"]) {
261 $found = false;
262 foreach ($fields as $key => $field) {
263 if (isset($field["field"]) && $found) {
264 unset($fields[key($_POST["down"])]);
265 array_splice($fields, $offset, 0, array($found));
266 break;
267 }
268 if (key($_POST["down"]) == $key) {
269 $found = $field;
270 }
271 $offset++;
272 }
273 }
274 $fields = array_values($fields);
275 if ($_POST["add"]) {
276 array_splice($fields, key($_POST["add"]), 0, array(array()));
277 }
278 }
279
1118566 Documentation
jakubvrana authored
280 /** Callback used in routine()
281 * @param array
282 * @return string
283 */
8060c5b Routines
jakubvrana authored
284 function normalize_enum($match) {
3f5b683 Reintegrate sqlite branch
jakubvrana authored
285 return "'" . str_replace("'", "''", addcslashes(stripcslashes(str_replace($match[0][0] . $match[0][0], $match[0][0], substr($match[0], 1, -1))), '\\')) . "'";
8060c5b Routines
jakubvrana authored
286 }
287
1118566 Documentation
jakubvrana authored
288 /** Issue grant or revoke commands
289 * @param string GRANT or REVOKE
290 * @param array
291 * @param string
292 * @param string
293 * @return
294 */
464d84a Define functions unconditionally
jakubvrana authored
295 function grant($grant, $privileges, $columns, $on) {
296 if (!$privileges) {
297 return true;
298 }
299 if ($privileges == array("ALL PRIVILEGES", "GRANT OPTION")) {
300 // can't be granted or revoked together
301 return ($grant == "GRANT"
302 ? queries("$grant ALL PRIVILEGES$on WITH GRANT OPTION")
303 : queries("$grant ALL PRIVILEGES$on") && queries("$grant GRANT OPTION$on")
304 );
305 }
306 return queries("$grant " . preg_replace('~(GRANT OPTION)\\([^)]*\\)~', '\\1', implode("$columns, ", $privileges) . $columns) . $on);
307 }
32d5b9c Inform about dropped routine, trigger and view
jakubvrana authored
308
1118566 Documentation
jakubvrana authored
309 /** Drop old object and create a new one
310 * @param string drop query
311 * @param string create query
312 * @param string
313 * @param string
314 * @param string
315 * @param string
316 * @param string
317 * @return bool dropped
318 */
32d5b9c Inform about dropped routine, trigger and view
jakubvrana authored
319 function drop_create($drop, $create, $location, $message_drop, $message_alter, $message_create, $name) {
320 if ($_POST["drop"]) {
321 return query_redirect($drop, $location, $message_drop, true, !$_POST["dropped"]);
322 }
7352c28 Replace strlen() by != ""
jakubvrana authored
323 $dropped = $name != "" && ($_POST["dropped"] || queries($drop));
32d5b9c Inform about dropped routine, trigger and view
jakubvrana authored
324 $created = queries($create);
7352c28 Replace strlen() by != ""
jakubvrana authored
325 if (!queries_redirect($location, ($name != "" ? $message_alter : $message_create), $created) && $dropped) {
be49e08 Improve session restarting
jakubvrana authored
326 restart_session();
32d5b9c Inform about dropped routine, trigger and view
jakubvrana authored
327 $_SESSION["messages"][] = $message_drop;
328 }
329 return $dropped;
330 }
Something went wrong with that request. Please try again.