Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 273 lines (262 sloc) 12.378 kb
a7096dda » jakubvrana
2007-07-16 Separate editing functions
1 <?php
471ae2b2 » jakubvrana
2009-09-13 Move select function
2 /** Print select result
3 * @param Min_Result
4 * @param Min_DB connection to examine indexes
5 * @return null
6 */
7 function select($result, $dbh2 = null) {
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();
23 if (strlen($field->orgtable)) {
24 if (!isset($indexes[$field->orgtable])) {
25 // find primary key in each table
26 $indexes[$field->orgtable] = array();
27 foreach (indexes($field->orgtable, $dbh2) as $index) {
28 if ($index["type"] == "PRIMARY") {
29 $indexes[$field->orgtable] = array_flip($index["columns"]);
30 break;
31 }
32 }
33 $columns[$field->orgtable] = $indexes[$field->orgtable];
34 }
35 if (isset($columns[$field->orgtable][$field->orgname])) {
36 unset($columns[$field->orgtable][$field->orgname]);
37 $indexes[$field->orgtable][$field->orgname] = $j;
38 $links[$j] = $field->orgtable;
39 }
40 }
41 if ($field->charsetnr == 63) {
42 $blobs[$j] = true;
43 }
44 $types[$j] = $field->type;
45 echo "<th>" . h($field->name);
46 }
47 echo "</thead>\n";
48 }
49 echo "<tr" . odd() . ">";
50 foreach ($row as $key => $val) {
51 if (!isset($val)) {
52 $val = "<i>NULL</i>";
53 } else {
54 if ($blobs[$key] && !is_utf8($val)) {
55 $val = "<i>" . lang('%d byte(s)', strlen($val)) . "</i>"; //! link to download
24801a0c » jakubvrana
2009-09-14 Display whitespace in texts (bug #2858042)
56 } elseif (!strlen($val)) {
471ae2b2 » jakubvrana
2009-09-13 Move select function
57 $val = "&nbsp;"; // some content to print a border
58 } else {
24801a0c » jakubvrana
2009-09-14 Display whitespace in texts (bug #2858042)
59 $val = whitespace(h($val));
471ae2b2 » jakubvrana
2009-09-13 Move select function
60 if ($types[$key] == 254) {
61 $val = "<code>$val</code>";
62 }
63 }
64 if (isset($links[$key]) && !$columns[$links[$key]]) {
65 $link = "edit=" . urlencode($links[$key]);
66 foreach ($indexes[$links[$key]] as $col => $j) {
67 $link .= "&where" . urlencode("[" . bracket_escape($col) . "]") . "=" . urlencode($row[$j]);
68 }
69 $val = "<a href='" . h(ME . $link) . "'>$val</a>";
70 }
71 }
72 echo "<td>$val";
73 }
74 }
75 echo "</table>\n";
76 }
77 }
78
ced9de9d » jakubvrana
2009-07-02 Create single column foreign key in table structure
79 function referencable_primary($self) {
80 $return = array(); // table_name => field
81 foreach (table_status_referencable() as $table_name => $table) {
82 if ($table_name != $self) {
83 foreach (fields($table_name) as $field) {
84 if ($field["primary"]) {
85 if ($return[$table_name]) { // multi column primary key
86 unset($return[$table_name]);
87 break;
88 }
89 $return[$table_name] = $field;
90 }
91 }
92 }
93 }
94 return $return;
95 }
96
97 function edit_type($key, $field, $collations, $foreign_keys = array()) {
26b07bf7 » jakubvrana
2009-07-14 Separate types to groups in table creation
98 global $structured_types, $unsigned, $inout;
f2813b57 » jakubvrana
2008-03-20 Don't hide/show added/dropped columns (bug 1920167)
99 ?>
464d84af » jakubvrana
2009-09-09 Define functions unconditionally
100 <td><select name="<?php echo $key; ?>[type]" onchange="editing_type_change(this);"><?php echo optionlist($structured_types + ($foreign_keys ? array(lang('Foreign keys') => $foreign_keys) : array()), $field["type"]); // foreign keys can be wide but style="width: 15ex;" narrows expanded optionlist in IE too ?></select>
689699aa » jakubvrana
2009-07-28 Shortcut for htmlspecialchars
101 <td><input name="<?php echo $key; ?>[length]" value="<?php echo h($field["length"]); ?>" size="3">
493eb6b0 » jakubvrana
2009-06-19 Remove space
102 <td><?php
12c042ce » jakubvrana
2009-07-30 Function htmlspecialchars now uses ENT_QUOTES
103 echo "<select name='$key" . "[collation]'" . (ereg('(char|text|enum|set)$', $field["type"]) ? "" : " class='hidden'") . '><option value="">(' . lang('collation') . ')' . optionlist($collations, $field["collation"]) . '</select>';
104 echo ($unsigned ? " <select name='$key" . "[unsigned]'" . (!$field["type"] || ereg('(int|float|double|decimal)$', $field["type"]) ? "" : " class='hidden'") . '><option>' . optionlist($unsigned, $field["unsigned"]) . '</select>' : '');
ace55ed1 » jakubvrana
2009-07-11 HTML instead of XHTML
105 ?>
a7096dda » jakubvrana
2007-07-16 Separate editing functions
106 <?php
107 }
108
f93c84e4 » jakubvrana
2009-07-27 Function process_length used only in Adminer
109 function process_length($length) {
110 global $enum_length;
111 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));
112 }
113
a3c969c0 » jakubvrana
2007-07-17 Routines uses CHARACTER SET
114 function process_type($field, $collate = "COLLATE") {
5f16c75c » jakubvrana
2009-05-08 Prepare for version 2
115 global $dbh, $enum_length, $unsigned;
a7096dda » jakubvrana
2007-07-16 Separate editing functions
116 return " $field[type]"
5abd943c » jakubvrana
2009-07-13 Change simple preg_match to ereg
117 . ($field["length"] && !ereg('^date|time$', $field["type"]) ? "(" . process_length($field["length"]) . ")" : "")
118 . (ereg('int|float|double|decimal', $field["type"]) && in_array($field["unsigned"], $unsigned) ? " $field[unsigned]" : "")
119 . (ereg('char|text|enum|set', $field["type"]) && $field["collation"] ? " $collate " . $dbh->quote($field["collation"]) : "")
a7096dda » jakubvrana
2007-07-16 Separate editing functions
120 ;
121 }
122
2e680bc9 » jakubvrana
2009-09-09 Speedup of simple alter table
123 function process_field($field, $type_field) {
124 global $dbh;
125 return idf_escape($field["field"]) . process_type($type_field)
126 . ($field["null"] ? " NULL" : " NOT NULL") // NULL for timestamp
24801a0c » jakubvrana
2009-09-14 Display whitespace in texts (bug #2858042)
127 . (!$field["has_default"] || $field["auto_increment"] || ereg('text|blob', $field["type"]) ? "" : " DEFAULT " . ($field["type"] == "timestamp" && eregi("^CURRENT_TIMESTAMP( on update CURRENT_TIMESTAMP)?$", $field["default"]) ? $field["default"] : $dbh->quote($field["default"])))
2e680bc9 » jakubvrana
2009-09-09 Speedup of simple alter table
128 . " COMMENT " . $dbh->quote($field["comment"])
129 ;
130 }
131
3015aed7 » jakubvrana
2009-07-14 Separate type_class
132 function type_class($type) {
133 if (ereg('char|text', $type)) {
134 return " class='char'";
135 } elseif (ereg('date|time|year', $type)) {
136 return " class='date'";
137 } elseif (ereg('binary|blob', $type)) {
138 return " class='binary'";
139 } elseif (ereg('enum|set', $type)) {
140 return " class='enum'";
141 }
142 }
143
ced9de9d » jakubvrana
2009-07-02 Create single column foreign key in table structure
144 function edit_fields($fields, $collations, $type = "TABLE", $allowed = 0, $foreign_keys = array()) {
d31d4e9e » jakubvrana
2009-07-28 Emulate REQUEST_URI
145 global $inout;
540e2174 » jakubvrana
2009-05-29 Separate JavaScript
146 $column_comments = false;
147 foreach ($fields as $field) {
148 if (strlen($field["comment"])) {
149 $column_comments = true;
150 }
151 }
f2813b57 » jakubvrana
2008-03-20 Don't hide/show added/dropped columns (bug 1920167)
152 ?>
4e5b1262 » jakubvrana
2009-04-30 Highlight odd and hover rows
153 <thead><tr>
b3496120 » jakubvrana
2009-08-25 Remove useless translations
154 <?php if ($type == "PROCEDURE") { ?><td>&nbsp;<?php } ?>
ace55ed1 » jakubvrana
2009-07-11 HTML instead of XHTML
155 <th><?php echo ($type == "TABLE" ? lang('Column name') : lang('Parameter name')); ?>
156 <td><?php echo lang('Type'); ?>
157 <td><?php echo lang('Length'); ?>
158 <td><?php echo lang('Options'); ?>
a7096dda » jakubvrana
2007-07-16 Separate editing functions
159 <?php if ($type == "TABLE") { ?>
b3496120 » jakubvrana
2009-08-25 Remove useless translations
160 <td>NULL
ace55ed1 » jakubvrana
2009-07-11 HTML instead of XHTML
161 <td><input type="radio" name="auto_increment_col" value=""><?php echo lang('Auto Increment'); ?>
4cead560 » jakubvrana
2009-08-21 Edit default values directly in table creation
162 <td class="hidden"><?php echo lang('Default values'); ?>
ace55ed1 » jakubvrana
2009-07-11 HTML instead of XHTML
163 <td<?php echo ($column_comments ? "" : " class='hidden'"); ?>><?php echo lang('Comment'); ?>
a7096dda » jakubvrana
2007-07-16 Separate editing functions
164 <?php } ?>
ace55ed1 » jakubvrana
2009-07-11 HTML instead of XHTML
165 <td><?php echo "<input type='image' name='add[0]' src='../adminer/plus.gif' alt='+' title='" . lang('Add next') . "'>"; ?><script type="text/javascript">row_count = <?php echo count($fields); ?>;</script>
166 </thead>
a7096dda » jakubvrana
2007-07-16 Separate editing functions
167 <?php
93c27c95 » jakubvrana
2007-07-16 Separate type_change()
168 foreach ($fields as $i => $field) {
169 $i++;
3cd113fa » jakubvrana
2008-10-03 Procedures test
170 $display = (isset($_POST["add"][$i-1]) || (isset($field["field"]) && !$_POST["drop_col"][$i]));
93c27c95 » jakubvrana
2007-07-16 Separate type_change()
171 ?>
a76c555b » jakubvrana
2009-06-11 Remove odd() from create
172 <tr<?php echo ($display ? "" : " style='display: none;'"); ?>>
ace55ed1 » jakubvrana
2009-07-11 HTML instead of XHTML
173 <?php if ($type == "PROCEDURE") { ?><td><select name="fields[<?php echo $i; ?>][inout]"><?php echo optionlist($inout, $field["inout"]); ?></select><?php } ?>
689699aa » jakubvrana
2009-07-28 Shortcut for htmlspecialchars
174 <th><?php if ($display) { ?><input name="fields[<?php echo $i; ?>][field]" value="<?php echo h($field["field"]); ?>" onchange="<?php echo (strlen($field["field"]) || count($fields) > 1 ? "" : "editing_add_row(this, $allowed); "); ?>editing_name_change(this);" maxlength="64"><?php } ?><input type="hidden" name="fields[<?php echo $i; ?>][orig]" value="<?php echo h($field[($_POST ? "orig" : "field")]); ?>">
ced9de9d » jakubvrana
2009-07-02 Create single column foreign key in table structure
175 <?php edit_type("fields[$i]", $field, $collations, $foreign_keys); ?>
a7096dda » jakubvrana
2007-07-16 Separate editing functions
176 <?php if ($type == "TABLE") { ?>
cb6d36c0 » jakubvrana
2009-07-28 HTML instead of XHTML
177 <td><input type="checkbox" name="fields[<?php echo $i; ?>][null]" value="1"<?php if ($field["null"]) { ?> checked<?php } ?>>
178 <td><input type="radio" name="auto_increment_col" value="<?php echo $i; ?>"<?php if ($field["auto_increment"]) { ?> checked<?php } ?>>
4cead560 » jakubvrana
2009-08-21 Edit default values directly in table creation
179 <td class="nowrap hidden"><input type="checkbox" name="fields[<?php echo $i; ?>][has_default]" value="1"<?php echo ($field["has_default"] ? " checked" : ""); ?>><input name="fields[<?php echo $i; ?>][default]" value="<?php echo h($field["default"]); ?>" onchange="this.previousSibling.checked = true;">
689699aa » jakubvrana
2009-07-28 Shortcut for htmlspecialchars
180 <td<?php echo ($column_comments ? "" : " class='hidden'"); ?>><input name="fields[<?php echo $i; ?>][comment]" value="<?php echo h($field["comment"]); ?>" maxlength="255">
a7096dda » jakubvrana
2007-07-16 Separate editing functions
181 <?php } ?>
182 <?php
ace55ed1 » jakubvrana
2009-07-11 HTML instead of XHTML
183 echo "<td class='nowrap'><input type='image' name='add[$i]' src='../adminer/plus.gif' alt='+' title='" . lang('Add next') . "' onclick='var x = editing_add_row(this, $allowed); if (x) { x.focus(); x.onchange = function () { }; } return !x;'>";
184 echo "&nbsp;<input type='image' name='drop_col[$i]' src='../adminer/cross.gif' alt='x' title='" . lang('Remove') . "' onclick='return !editing_remove_row(this);'>";
185 echo "&nbsp;<input type='image' name='up[$i]' src='../adminer/up.gif' alt='^' title='" . lang('Move up') . "'>";
186 echo "&nbsp;<input type='image' name='down[$i]' src='../adminer/down.gif' alt='v' title='" . lang('Move down') . "'>";
187 echo "\n\n";
a7096dda » jakubvrana
2007-07-16 Separate editing functions
188 }
93c27c95 » jakubvrana
2007-07-16 Separate type_change()
189 return $column_comments;
a7096dda » jakubvrana
2007-07-16 Separate editing functions
190 }
93c27c95 » jakubvrana
2007-07-16 Separate type_change()
191
fd8948ff » jakubvrana
2008-05-07 Order of columns in table
192 function process_fields(&$fields) {
193 ksort($fields);
194 $offset = 0;
195 if ($_POST["up"]) {
196 $last = 0;
197 foreach ($fields as $key => $field) {
198 if (key($_POST["up"]) == $key) {
199 unset($fields[$key]);
200 array_splice($fields, $last, 0, array($field));
201 break;
202 }
203 if (isset($field["field"])) {
204 $last = $offset;
205 }
206 $offset++;
207 }
208 }
209 if ($_POST["down"]) {
210 $found = false;
211 foreach ($fields as $key => $field) {
212 if (isset($field["field"]) && $found) {
213 unset($fields[key($_POST["down"])]);
214 array_splice($fields, $offset, 0, array($found));
215 break;
216 }
217 if (key($_POST["down"]) == $key) {
218 $found = $field;
219 }
220 $offset++;
221 }
222 }
223 $fields = array_values($fields);
224 if ($_POST["add"]) {
225 array_splice($fields, key($_POST["add"]), 0, array(array()));
226 }
227 }
228
8060c5bc » jakubvrana
2007-07-17 Routines
229 function normalize_enum($match) {
230 return "'" . str_replace("'", "''", addcslashes(stripcslashes(str_replace($match[0]{0} . $match[0]{0}, $match[0]{0}, substr($match[0], 1, -1))), '\\')) . "'";
231 }
232
a7096dda » jakubvrana
2007-07-16 Separate editing functions
233 function routine($name, $type) {
79c70ad5 » jakubvrana
2009-09-16 Change regular expression (bug #2859386)
234 global $dbh, $enum_length, $inout, $types;
9fa3b9de » jakubvrana
2007-08-16 Type aliases in routine
235 $aliases = array("bit" => "tinyint", "bool" => "tinyint", "boolean" => "tinyint", "integer" => "int", "double precision" => "float", "real" => "float", "dec" => "decimal", "numeric" => "decimal", "fixed" => "decimal", "national char" => "char", "national varchar" => "varchar");
79c70ad5 » jakubvrana
2009-09-16 Change regular expression (bug #2859386)
236 $type_pattern = "(" . implode("|", array_keys($types + $aliases)) . ")(?:\\s*\\(((?:[^'\")]*|$enum_length)+)\\))?\\s*(zerofill\\s*)?(unsigned(?:\\s+zerofill)?)?(?:\\s*(?:CHARSET|CHARACTER\\s+SET)\\s*['\"]?([^'\"\\s]+)['\"]?)?";
26c9d64b » jakubvrana
2009-08-19 Fix long SQL query crash (bug #2839231)
237 $pattern = "\\s*(" . ($type == "FUNCTION" ? "" : implode("|", $inout)) . ")?\\s*(?:`((?:[^`]|``)*)`\\s*|\\b(\\S+)\\s+)$type_pattern";
5f16c75c » jakubvrana
2009-05-08 Prepare for version 2
238 $create = $dbh->result($dbh->query("SHOW CREATE $type " . idf_escape($name)), 2);
8060c5bc » jakubvrana
2007-07-17 Routines
239 preg_match("~\\(((?:$pattern\\s*,?)*)\\)" . ($type == "FUNCTION" ? "\\s*RETURNS\\s+$type_pattern" : "") . "\\s*(.*)~is", $create, $match);
a7096dda » jakubvrana
2007-07-16 Separate editing functions
240 $fields = array();
8060c5bc » jakubvrana
2007-07-17 Routines
241 preg_match_all("~$pattern\\s*,?~is", $match[1], $matches, PREG_SET_ORDER);
2184679b » jakubvrana
2009-07-24 Use field name in routine
242 foreach ($matches as $param) {
243 $name = str_replace("``", "`", $param[2]) . $param[3];
9fa3b9de » jakubvrana
2007-08-16 Type aliases in routine
244 $data_type = strtolower($param[4]);
2184679b » jakubvrana
2009-07-24 Use field name in routine
245 $fields[$name] = array(
246 "field" => $name,
9fa3b9de » jakubvrana
2007-08-16 Type aliases in routine
247 "type" => (isset($aliases[$data_type]) ? $aliases[$data_type] : $data_type),
a7096dda » jakubvrana
2007-07-16 Separate editing functions
248 "length" => preg_replace_callback("~$enum_length~s", 'normalize_enum', $param[5]),
249 "unsigned" => strtolower(preg_replace('~\\s+~', ' ', trim("$param[7] $param[6]"))),
250 "inout" => strtoupper($param[1]),
8060c5bc » jakubvrana
2007-07-17 Routines
251 "collation" => strtolower($param[8]),
a7096dda » jakubvrana
2007-07-16 Separate editing functions
252 );
253 }
254 if ($type != "FUNCTION") {
8060c5bc » jakubvrana
2007-07-17 Routines
255 return array("fields" => $fields, "definition" => $match[10]);
a7096dda » jakubvrana
2007-07-16 Separate editing functions
256 }
8060c5bc » jakubvrana
2007-07-17 Routines
257 $returns = array("type" => $match[10], "length" => $match[11], "unsigned" => $match[13], "collation" => $match[14]);
258 return array("fields" => $fields, "returns" => $returns, "definition" => $match[15]);
a7096dda » jakubvrana
2007-07-16 Separate editing functions
259 }
464d84af » jakubvrana
2009-09-09 Define functions unconditionally
260
261 function grant($grant, $privileges, $columns, $on) {
262 if (!$privileges) {
263 return true;
264 }
265 if ($privileges == array("ALL PRIVILEGES", "GRANT OPTION")) {
266 // can't be granted or revoked together
267 return ($grant == "GRANT"
268 ? queries("$grant ALL PRIVILEGES$on WITH GRANT OPTION")
269 : queries("$grant ALL PRIVILEGES$on") && queries("$grant GRANT OPTION$on")
270 );
271 }
272 return queries("$grant " . preg_replace('~(GRANT OPTION)\\([^)]*\\)~', '\\1', implode("$columns, ", $privileges) . $columns) . $on);
273 }
Something went wrong with that request. Please try again.