Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 551 lines (519 sloc) 17.197 kb
667bfec Decomposition
jakubvrana authored
1 <?php
9a176b0 Comments
jakubvrana authored
2 /** Get database connection
3 * @return Min_DB
4 */
c64c4fd Adminer class
jakubvrana authored
5 function get_dbh() {
6 // can be used in customization, $dbh is minified
7 global $dbh;
8 return $dbh;
9 }
10
9a176b0 Comments
jakubvrana authored
11 /** Escape database identifier
12 * @param string
13 * @return string
14 */
667bfec Decomposition
jakubvrana authored
15 function idf_escape($idf) {
16 return "`" . str_replace("`", "``", $idf) . "`";
17 }
18
9a176b0 Comments
jakubvrana authored
19 /** Unescape database identifier
20 * @param string text inside ``
21 * @return string
22 */
667bfec Decomposition
jakubvrana authored
23 function idf_unescape($idf) {
24 return str_replace("``", "`", $idf);
25 }
26
9a176b0 Comments
jakubvrana authored
27 /** Escape or unescape string to use inside form []
28 * @param string
29 * @param bool
30 * @return string
31 */
667bfec Decomposition
jakubvrana authored
32 function bracket_escape($idf, $back = false) {
64ba924 Comments
jakubvrana authored
33 // escape brackets inside name="x[]"
73e8631 Move stuff to functions.inc.php
jakubvrana authored
34 static $trans = array(':' => ':1', ']' => ':2', '[' => ':3');
667bfec Decomposition
jakubvrana authored
35 return strtr($idf, ($back ? array_flip($trans) : $trans));
36 }
37
9a176b0 Comments
jakubvrana authored
38 /** Escape for HTML
39 * @param string
40 * @return string
41 */
689699a Shortcut for htmlspecialchars
jakubvrana authored
42 function h($string) {
43 return htmlspecialchars($string, ENT_QUOTES);
44 }
45
9a176b0 Comments
jakubvrana authored
46 /** Escape for TD
47 * @param string
48 * @return string
49 */
c196985 Function nbsp
jakubvrana authored
50 function nbsp($string) {
51 return (strlen(trim($string)) ? h($string) : "&nbsp;");
52 }
53
9a176b0 Comments
jakubvrana authored
54 /** Generate list of HTML options
55 * @param array array of strings or arrays (creates optgroup)
56 * @param mixed
57 * @param bool always use array keys for value="", otherwise only string keys are used
58 * @return string
59 */
1e4d11e Select from foreign keys in Editor
jakubvrana authored
60 function optionlist($options, $selected = null, $use_keys = false) {
667bfec Decomposition
jakubvrana authored
61 $return = "";
62 foreach ($options as $k => $v) {
63 if (is_array($v)) {
689699a Shortcut for htmlspecialchars
jakubvrana authored
64 $return .= '<optgroup label="' . h($k) . '">';
667bfec Decomposition
jakubvrana authored
65 }
ced9de9 Create single column foreign key in table structure
jakubvrana authored
66 foreach ((is_array($v) ? $v : array($k => $v)) as $key => $val) {
cb6d36c HTML instead of XHTML
jakubvrana authored
67 $return .= '<option' . ($use_keys || is_string($key) ? ' value="' . h($key) . '"' : '') . (($use_keys || is_string($key) ? (string) $key : $val) === $selected ? ' selected' : '') . '>' . h($val);
667bfec Decomposition
jakubvrana authored
68 }
69 if (is_array($v)) {
70 $return .= '</optgroup>';
71 }
72 }
73 return $return;
74 }
75
9a176b0 Comments
jakubvrana authored
76 /** Get list of values from database
77 * @param string
78 * @param mixed
79 * @return array
80 */
0bd864e Allow specifying column in get_vals
jakubvrana authored
81 function get_vals($query, $column = 0) {
5f16c75 Prepare for version 2
jakubvrana authored
82 global $dbh;
7769f2c Separate get_vals
jakubvrana authored
83 $return = array();
5f16c75 Prepare for version 2
jakubvrana authored
84 $result = $dbh->query($query);
49f7886 SHOW DATABASES can be revoked
jakubvrana authored
85 if ($result) {
86 while ($row = $result->fetch_row()) {
0bd864e Allow specifying column in get_vals
jakubvrana authored
87 $return[] = $row[$column];
49f7886 SHOW DATABASES can be revoked
jakubvrana authored
88 }
7769f2c Separate get_vals
jakubvrana authored
89 }
90 return $return;
91 }
92
9a176b0 Comments
jakubvrana authored
93 /** Find unique identifier of a row
94 * @param array
95 * @param array result of indexes()
96 * @return string query string
97 */
667bfec Decomposition
jakubvrana authored
98 function unique_idf($row, $indexes) {
720f5fc Restructure indexes
jakubvrana authored
99 foreach ($indexes as $index) {
100 if ($index["type"] == "PRIMARY" || $index["type"] == "UNIQUE") {
101 $return = array();
102 foreach ($index["columns"] as $key) {
64ba924 Comments
jakubvrana authored
103 if (!isset($row[$key])) { // NULL is ambiguous
720f5fc Restructure indexes
jakubvrana authored
104 continue 2;
667bfec Decomposition
jakubvrana authored
105 }
430034c Cottage homework
jakubvrana authored
106 $return[] = urlencode("where[" . bracket_escape($key) . "]") . "=" . urlencode($row[$key]);
667bfec Decomposition
jakubvrana authored
107 }
720f5fc Restructure indexes
jakubvrana authored
108 return $return;
667bfec Decomposition
jakubvrana authored
109 }
110 }
111 $return = array();
112 foreach ($row as $key => $val) {
26c9d64 Fix long SQL query crash (bug #2839231)
jakubvrana authored
113 if (!preg_match('~^(COUNT\\((\\*|(DISTINCT )?`(?:[^`]|``)+`)\\)|(AVG|GROUP_CONCAT|MAX|MIN|SUM)\\(`(?:[^`]|``)+`\\))$~', $key)) { //! columns looking like functions
988a29f Don't use aggregation functions in unique_idf
jakubvrana authored
114 $return[] = (isset($val) ? urlencode("where[" . bracket_escape($key) . "]") . "=" . urlencode($val) : "null%5B%5D=" . urlencode($key));
115 }
430034c Cottage homework
jakubvrana authored
116 }
117 return $return;
118 }
119
9a176b0 Comments
jakubvrana authored
120 /** Create SQL condition from parsed query string
121 * @param array parsed query string
122 * @return string
123 */
9ea3112 Mandatory $where in where()
jakubvrana authored
124 function where($where) {
5f16c75 Prepare for version 2
jakubvrana authored
125 global $dbh;
430034c Cottage homework
jakubvrana authored
126 $return = array();
116faf2 Mass delete
jakubvrana authored
127 foreach ((array) $where["where"] as $key => $val) {
581bd35 Respect function results in where()
jakubvrana authored
128 $key = bracket_escape($key, "back");
26c9d64 Fix long SQL query crash (bug #2839231)
jakubvrana authored
129 $return[] = (preg_match('~^[A-Z0-9_]+\\(`(?:[^`]|``)+`\\)$~', $key) ? $key : idf_escape($key)) . " = BINARY " . $dbh->quote($val); //! enum and set, columns looking like functions
430034c Cottage homework
jakubvrana authored
130 }
116faf2 Mass delete
jakubvrana authored
131 foreach ((array) $where["null"] as $key) {
581bd35 Respect function results in where()
jakubvrana authored
132 $key = bracket_escape($key, "back");
26c9d64 Fix long SQL query crash (bug #2839231)
jakubvrana authored
133 $return[] = (preg_match('~^[A-Z0-9_]+\\(`(?:[^`]|``)+`\\)$~', $key) ? $key : idf_escape($key)) . " IS NULL";
667bfec Decomposition
jakubvrana authored
134 }
c16c57b E-mail sending
jakubvrana authored
135 return implode(" AND ", $return);
667bfec Decomposition
jakubvrana authored
136 }
137
9a176b0 Comments
jakubvrana authored
138 /** Create SQL condition from query string
139 * @param string
140 * @return string
141 */
0cd2afb Remove single item clone
jakubvrana authored
142 function where_check($val) {
143 parse_str($val, $check);
144 return where($check);
145 }
146
9a176b0 Comments
jakubvrana authored
147 /** Create query string where condition from value
148 * @param int condition order
149 * @param string column identifier
150 * @param string
151 * @return string
152 */
c42c46a Table relations
jakubvrana authored
153 function where_link($i, $column, $value) {
db46fc2 Maintain insert prefill value
jakubvrana authored
154 return "&where%5B$i%5D%5Bcol%5D=" . urlencode($column) . "&where%5B$i%5D%5Bop%5D=%3D&where%5B$i%5D%5Bval%5D=" . urlencode($value);
c42c46a Table relations
jakubvrana authored
155 }
156
9a176b0 Comments
jakubvrana authored
157 /** Send Location header and exit
158 * @param string
159 * @param string
160 * @return null
161 */
430034c Cottage homework
jakubvrana authored
162 function redirect($location, $message = null) {
163 if (isset($message)) {
ff74bf0 Allow multiple messages
jakubvrana authored
164 $_SESSION["messages"][] = $message;
430034c Cottage homework
jakubvrana authored
165 }
166 header("Location: " . (strlen($location) ? $location : "."));
167 exit;
168 }
169
9a176b0 Comments
jakubvrana authored
170 /** Execute query and redirect if successful
171 * @param string
172 * @param string
173 * @param string
174 * @param bool
175 * @param bool
176 * @param bool
177 * @return bool
178 */
748ee83 Print SQL command with multiple queries
jakubvrana authored
179 function query_redirect($query, $location, $message, $redirect = true, $execute = true, $failed = false) {
98507da Function verify_version doesn't use version parameter
jakubvrana authored
180 global $dbh, $error, $adminer;
10bdbed Save queries to history
jakubvrana authored
181 $sql = "";
182 if ($query) {
c64c4fd Adminer class
jakubvrana authored
183 $sql = $adminer->messageQuery($query);
10bdbed Save queries to history
jakubvrana authored
184 }
748ee83 Print SQL command with multiple queries
jakubvrana authored
185 if ($execute) {
5f16c75 Prepare for version 2
jakubvrana authored
186 $failed = !$dbh->query($query);
748ee83 Print SQL command with multiple queries
jakubvrana authored
187 }
188 if ($failed) {
689699a Shortcut for htmlspecialchars
jakubvrana authored
189 $error = h($dbh->error) . $sql;
c48a017 Print SQL query by error
jakubvrana authored
190 return false;
f91b2c4 Print SQL query by message
jakubvrana authored
191 }
c48a017 Print SQL query by error
jakubvrana authored
192 if ($redirect) {
193 redirect($location, $message . $sql);
194 }
195 return true;
f91b2c4 Print SQL query by message
jakubvrana authored
196 }
197
9a176b0 Comments
jakubvrana authored
198 /** Execute and remember query
199 * @param string null to return remembered queries
200 * @return Min_Result
201 */
748ee83 Print SQL command with multiple queries
jakubvrana authored
202 function queries($query = null) {
5f16c75 Prepare for version 2
jakubvrana authored
203 global $dbh;
748ee83 Print SQL command with multiple queries
jakubvrana authored
204 static $queries = array();
205 if (!isset($query)) {
64ba924 Comments
jakubvrana authored
206 // return executed queries without parameter
36950eb Tables and views
jakubvrana authored
207 return implode(";\n", $queries);
748ee83 Print SQL command with multiple queries
jakubvrana authored
208 }
209 $queries[] = $query;
5f16c75 Prepare for version 2
jakubvrana authored
210 return $dbh->query($query);
748ee83 Print SQL command with multiple queries
jakubvrana authored
211 }
212
9a176b0 Comments
jakubvrana authored
213 /** Remove parameter from query string
214 * @param string
215 * @return string
216 */
d41d446 remove_from_uri()
jakubvrana authored
217 function remove_from_uri($param = "") {
6272cae Unconditionally remove session_name() in remove_from_uri()
jakubvrana authored
218 $param = "($param|" . session_name() . ")";
d41d446 remove_from_uri()
jakubvrana authored
219 return preg_replace("~\\?$param=[^&]*&~", '?', preg_replace("~\\?$param=[^&]*\$|&$param=[^&]*~", '', $_SERVER["REQUEST_URI"]));
220 }
221
9a176b0 Comments
jakubvrana authored
222 /** Generate page number for pagination
223 * @param int
224 * @return string
225 */
c565c54 Let pagination function return
jakubvrana authored
226 function pagination($page) {
689699a Shortcut for htmlspecialchars
jakubvrana authored
227 return " " . ($page == $_GET["page"] ? $page + 1 : '<a href="' . h(remove_from_uri("page") . ($page ? "&page=$page" : "")) . '">' . ($page + 1) . "</a>");
1456283 Pages on last page
jakubvrana authored
228 }
229
9a176b0 Comments
jakubvrana authored
230 /** Get file contents from $_FILES or $_POST["files"]
231 * @param string
232 * @param bool
233 * @return string
234 */
25f01d3 Compress export and import
jakubvrana authored
235 function get_file($key, $decompress = false) {
64ba924 Comments
jakubvrana authored
236 // returns int for error, string otherwise
25f01d3 Compress export and import
jakubvrana authored
237 $file = $_POST["files"][$key];
238 if (isset($file)) {
64ba924 Comments
jakubvrana authored
239 // get the file from hidden field if the user was logged out
25f01d3 Compress export and import
jakubvrana authored
240 $length = strlen($file);
241 if ($length && $length < 4) {
242 return intval($file);
243 }
244 return base64_decode($file);
245 }
246 $file = $_FILES[$key];
247 if (!$file || $file["error"]) {
248 return $file["error"];
430034c Cottage homework
jakubvrana authored
249 }
0a2d81d Bzip2 import
jakubvrana authored
250 return file_get_contents($decompress && ereg('\\.gz$', $file["name"]) ? "compress.zlib://$file[tmp_name]"
251 : ($decompress && ereg('\\.bz2$', $file["name"]) ? "compress.bzip2://$file[tmp_name]"
252 : $file["tmp_name"]
253 )); //! may not be reachable because of open_basedir
430034c Cottage homework
jakubvrana authored
254 }
255
9a176b0 Comments
jakubvrana authored
256 /** Determine upload error
257 * @param int
258 * @return string
259 */
e2a03c0 Show upload_max_filesize
jakubvrana authored
260 function upload_error($error) {
261 $max_size = ($error == UPLOAD_ERR_INI_SIZE ? ini_get("upload_max_filesize") : null); // post_max_size is checked in index.php
b2a7c7a Execute SQL file stored on server disk
jakubvrana authored
262 return ($error ? lang('Unable to upload a file.') . ($max_size ? " " . lang('Maximum allowed file size is %sB.', $max_size) : "") : lang('File does not exist.'));
e2a03c0 Show upload_max_filesize
jakubvrana authored
263 }
264
9a176b0 Comments
jakubvrana authored
265 /** Generate class for odd rows
266 * @param string return this for odd rows, empty to reset counter
267 * @return string
268 */
269 function odd($return = ' class="odd"') {
4e5b126 Highlight odd and hover rows
jakubvrana authored
270 static $i = 0;
9a176b0 Comments
jakubvrana authored
271 if (!$return) { // reset counter
1b97f48 Reset odd() for each result
jakubvrana authored
272 $i = -1;
4e5b126 Highlight odd and hover rows
jakubvrana authored
273 }
9a176b0 Comments
jakubvrana authored
274 return ($i++ % 2 ? $return : '');
4e5b126 Highlight odd and hover rows
jakubvrana authored
275 }
276
9a176b0 Comments
jakubvrana authored
277 /** Print select result
278 * @param Min_Result
279 * @param Min_DB connection to examine indexes
280 * @return null
281 */
fbebb08 Use separate connection for exploring indexes
jakubvrana authored
282 function select($result, $dbh2 = null) {
ae372aa Database abstraction
jakubvrana authored
283 if (!$result->num_rows) {
ace55ed HTML instead of XHTML
jakubvrana authored
284 echo "<p class='message'>" . lang('No rows.') . "\n";
430034c Cottage homework
jakubvrana authored
285 } else {
be8f88e Don't wrap select results
jakubvrana authored
286 echo "<table cellspacing='0' class='nowrap'>\n";
64ba924 Comments
jakubvrana authored
287 $links = array(); // colno => orgtable - create links from these columns
288 $indexes = array(); // orgtable => array(column => colno) - primary keys
289 $columns = array(); // orgtable => array(column => ) - not selected columns in primary key
290 $blobs = array(); // colno => bool - display bytes for blobs
291 $types = array(); // colno => type - display char in <code>
292 odd(''); // reset odd for each result
ae372aa Database abstraction
jakubvrana authored
293 for ($i=0; $row = $result->fetch_row(); $i++) {
430034c Cottage homework
jakubvrana authored
294 if (!$i) {
295 echo "<thead><tr>";
296 for ($j=0; $j < count($row); $j++) {
ae372aa Database abstraction
jakubvrana authored
297 $field = $result->fetch_field();
038e688 Primary key flag is not always set
jakubvrana authored
298 if (strlen($field->orgtable)) {
ae372aa Database abstraction
jakubvrana authored
299 if (!isset($indexes[$field->orgtable])) {
64ba924 Comments
jakubvrana authored
300 // find primary key in each table
ae372aa Database abstraction
jakubvrana authored
301 $indexes[$field->orgtable] = array();
fbebb08 Use separate connection for exploring indexes
jakubvrana authored
302 foreach (indexes($field->orgtable, $dbh2) as $index) {
430034c Cottage homework
jakubvrana authored
303 if ($index["type"] == "PRIMARY") {
ae372aa Database abstraction
jakubvrana authored
304 $indexes[$field->orgtable] = array_flip($index["columns"]);
430034c Cottage homework
jakubvrana authored
305 break;
306 }
307 }
ae372aa Database abstraction
jakubvrana authored
308 $columns[$field->orgtable] = $indexes[$field->orgtable];
430034c Cottage homework
jakubvrana authored
309 }
a01944e Primary key links
jakubvrana authored
310 if (isset($columns[$field->orgtable][$field->orgname])) {
311 unset($columns[$field->orgtable][$field->orgname]);
312 $indexes[$field->orgtable][$field->orgname] = $j;
313 $links[$j] = $field->orgtable;
314 }
430034c Cottage homework
jakubvrana authored
315 }
ae372aa Database abstraction
jakubvrana authored
316 if ($field->charsetnr == 63) {
430034c Cottage homework
jakubvrana authored
317 $blobs[$j] = true;
318 }
ae6892d Display char type in <code>
jakubvrana authored
319 $types[$j] = $field->type;
689699a Shortcut for htmlspecialchars
jakubvrana authored
320 echo "<th>" . h($field->name);
430034c Cottage homework
jakubvrana authored
321 }
ace55ed HTML instead of XHTML
jakubvrana authored
322 echo "</thead>\n";
430034c Cottage homework
jakubvrana authored
323 }
4e5b126 Highlight odd and hover rows
jakubvrana authored
324 echo "<tr" . odd() . ">";
430034c Cottage homework
jakubvrana authored
325 foreach ($row as $key => $val) {
326 if (!isset($val)) {
327 $val = "<i>NULL</i>";
328 } else {
017ffb5 Better UTF-8 detection
jakubvrana authored
329 if ($blobs[$key] && !is_utf8($val)) {
8251896 Downloaded file name
jakubvrana authored
330 $val = "<i>" . lang('%d byte(s)', strlen($val)) . "</i>"; //! link to download
a69fff7 Print \n as <br />, not &nbsp;
jakubvrana authored
331 } elseif (!strlen(trim($val, " \t"))) {
64ba924 Comments
jakubvrana authored
332 $val = "&nbsp;"; // some content to print a border
e9ee9b8 Reformat
jakubvrana authored
333 } else {
689699a Shortcut for htmlspecialchars
jakubvrana authored
334 $val = nl2br(h($val));
ae6892d Display char type in <code>
jakubvrana authored
335 if ($types[$key] == 254) {
336 $val = "<code>$val</code>";
337 }
e9ee9b8 Reformat
jakubvrana authored
338 }
430034c Cottage homework
jakubvrana authored
339 if (isset($links[$key]) && !$columns[$links[$key]]) {
340 $link = "edit=" . urlencode($links[$key]);
341 foreach ($indexes[$links[$key]] as $col => $j) {
98507da Function verify_version doesn't use version parameter
jakubvrana authored
342 $link .= "&where" . urlencode("[" . bracket_escape($col) . "]") . "=" . urlencode($row[$j]);
430034c Cottage homework
jakubvrana authored
343 }
98507da Function verify_version doesn't use version parameter
jakubvrana authored
344 $val = "<a href='" . h(ME . $link) . "'>$val</a>";
430034c Cottage homework
jakubvrana authored
345 }
346 }
ace55ed HTML instead of XHTML
jakubvrana authored
347 echo "<td>$val";
430034c Cottage homework
jakubvrana authored
348 }
349 }
350 echo "</table>\n";
351 }
73e8631 Move stuff to functions.inc.php
jakubvrana authored
352 }
353
9a176b0 Comments
jakubvrana authored
354 /** Check whether the string is in UTF-8
355 * @param string
356 * @return bool
357 */
017ffb5 Better UTF-8 detection
jakubvrana authored
358 function is_utf8($val) {
64ba924 Comments
jakubvrana authored
359 // don't print control chars except \t\r\n
017ffb5 Better UTF-8 detection
jakubvrana authored
360 return (preg_match('~~u', $val) && !preg_match('~[\\0-\\x8\\xB\\xC\\xE-\\x1F]~', $val));
361 }
362
9a176b0 Comments
jakubvrana authored
363 /** Shorten UTF-8 string
364 * @param string
365 * @param int
366 * @param string
367 * @return string escaped string with appended ...
368 */
cd07584 Save SQL query to history
jakubvrana authored
369 function shorten_utf8($string, $length = 80, $suffix = "") {
95764b0 Shorten binary strings
jakubvrana authored
370 if (!preg_match("~^((?:.|\n){0,$length})(.|\n)?~u", $string, $match)) { // ~s causes trash in $match[2] under some PHP versions
371 preg_match("(^([\t\r\n -~]{0,$length})(.?))s", $string, $match);
372 }
689699a Shortcut for htmlspecialchars
jakubvrana authored
373 return h($match[1]) . $suffix . ($match[2] ? "<em>...</em>" : "");
02a851c Shorten texts in PHP
jakubvrana authored
374 }
3920ebb Remove InnoDB table comment in Tables and views
jakubvrana authored
375
9a176b0 Comments
jakubvrana authored
376 /** Generate friendly URL
377 * @param string
378 * @return string
379 */
8251896 Downloaded file name
jakubvrana authored
380 function friendly_url($val) {
64ba924 Comments
jakubvrana authored
381 // used for blobs and export
8251896 Downloaded file name
jakubvrana authored
382 return preg_replace('~[^a-z0-9_]~i', '-', $val);
383 }
384
9a176b0 Comments
jakubvrana authored
385 /** Print hidden fields
386 * @param array
387 * @param array
388 * @return null
389 */
820c882 Bulk clone
jakubvrana authored
390 function hidden_fields($process, $ignore = array()) {
5d7071a Choose language through option-list
jakubvrana authored
391 while (list($key, $val) = each($process)) {
392 if (is_array($val)) {
393 foreach ($val as $k => $v) {
394 $process[$key . "[$k]"] = $v;
395 }
396 } elseif (!in_array($key, $ignore)) {
689699a Shortcut for htmlspecialchars
jakubvrana authored
397 echo '<input type="hidden" name="' . h($key) . '" value="' . h($val) . '">';
5d7071a Choose language through option-list
jakubvrana authored
398 }
399 }
400 }
61640ee Move common functions
jakubvrana authored
401
9a176b0 Comments
jakubvrana authored
402 /** Find out foreign keys for each column
403 * @param string
404 * @return array array($col => array())
405 */
1e4d11e Select from foreign keys in Editor
jakubvrana authored
406 function column_foreign_keys($table) {
407 $return = array();
408 foreach (foreign_keys($table) as $foreign_key) {
409 foreach ($foreign_key["source"] as $val) {
410 $return[$val][] = $foreign_key;
411 }
412 }
413 return $return;
414 }
415
9a176b0 Comments
jakubvrana authored
416 /** Print edit input field
417 * @param array one field from fields()
418 * @param mixed
419 * @param string
420 * @return null
421 */
1e4d11e Select from foreign keys in Editor
jakubvrana authored
422 function input($field, $value, $function) {
c64c4fd Adminer class
jakubvrana authored
423 global $types, $adminer;
689699a Shortcut for htmlspecialchars
jakubvrana authored
424 $name = h(bracket_escape($field["field"]));
61640ee Move common functions
jakubvrana authored
425 echo "<td class='function'>";
426 if ($field["type"] == "enum") {
cb6d36c HTML instead of XHTML
jakubvrana authored
427 echo "&nbsp;<td>" . (isset($_GET["select"]) ? " <label><input type='radio' name='fields[$name]' value='-1' checked><em>" . lang('original') . "</em></label>" : "");
4cead56 Edit default values directly in table creation
jakubvrana authored
428 if ($field["null"]) {
cb6d36c HTML instead of XHTML
jakubvrana authored
429 echo " <label><input type='radio' name='fields[$name]' value=''" . (($field["null"] ? isset($value) : strlen($value)) || isset($_GET["select"]) ? '' : ' checked') . '>' . ($field["null"] ? '<em>NULL</em>' : '') . '</label>';
61640ee Move common functions
jakubvrana authored
430 }
4cead56 Edit default values directly in table creation
jakubvrana authored
431 echo "<input type='radio' name='fields[$name]' value='0'" . ($value === 0 ? ' checked' : '') . '>';
26c9d64 Fix long SQL query crash (bug #2839231)
jakubvrana authored
432 preg_match_all("~'((?:[^']|'')*)'~", $field["length"], $matches);
61640ee Move common functions
jakubvrana authored
433 foreach ($matches[1] as $i => $val) {
434 $val = stripcslashes(str_replace("''", "'", $val));
435 $checked = (is_int($value) ? $value == $i+1 : $value === $val);
4cead56 Edit default values directly in table creation
jakubvrana authored
436 echo " <label><input type='radio' name='fields[$name]' value='" . ($i+1) . "'" . ($checked ? ' checked' : '') . '>' . h($val) . '</label>';
61640ee Move common functions
jakubvrana authored
437 }
438 } else {
9fc052b Unselect original after value input
jakubvrana authored
439 $functions = (isset($_GET["select"]) ? array("orig" => lang('original')) : array()) + $adminer->editFunctions($field);
9903946 Use original values in Editor multi edit
jakubvrana authored
440 $first = 0;
441 foreach ($functions as $key => $val) {
442 if ($key === "" || !$val) {
443 break;
444 }
445 $first++;
446 }
689699a Shortcut for htmlspecialchars
jakubvrana authored
447 $onchange = ($first ? " onchange=\"var f = this.form['function[" . addcslashes($name, "\r\n'\\") . "]']; if ($first > f.selectedIndex) f.selectedIndex = $first;\"" : "");
b74dfdc Print first function if only one
jakubvrana authored
448 echo (count($functions) > 1 ? "<select name='function[$name]'>" . optionlist($functions, !isset($function) || in_array($function, $functions) ? $function : "") . "</select>" : nbsp(reset($functions))) . '<td>';
689699a Shortcut for htmlspecialchars
jakubvrana authored
449 $input = $adminer->editInput($_GET["edit"], $field, " name='fields[$name]'$onchange", $value); // usage in call is without a table
e1abcda Treat tinyint(1) as boolean
jakubvrana authored
450 if (strlen($input)) {
451 echo $input;
1e4d11e Select from foreign keys in Editor
jakubvrana authored
452 } elseif ($field["type"] == "set") { //! 64 bits
26c9d64 Fix long SQL query crash (bug #2839231)
jakubvrana authored
453 preg_match_all("~'((?:[^']|'')*)'~", $field["length"], $matches);
61640ee Move common functions
jakubvrana authored
454 foreach ($matches[1] as $i => $val) {
455 $val = stripcslashes(str_replace("''", "'", $val));
456 $checked = (is_int($value) ? ($value >> $i) & 1 : in_array($val, explode(",", $value), true));
4cead56 Edit default values directly in table creation
jakubvrana authored
457 echo " <label><input type='checkbox' name='fields[$name][$i]' value='" . (1 << $i) . "'" . ($checked ? ' checked' : '') . "$onchange>" . h($val) . '</label>';
61640ee Move common functions
jakubvrana authored
458 }
459 } elseif (strpos($field["type"], "text") !== false) {
689699a Shortcut for htmlspecialchars
jakubvrana authored
460 echo "<textarea name='fields[$name]' cols='50' rows='12'$onchange>" . h($value) . '</textarea>';
5abd943 Change simple preg_match to ereg
jakubvrana authored
461 } elseif (ereg('binary|blob', $field["type"])) {
689699a Shortcut for htmlspecialchars
jakubvrana authored
462 echo (ini_get("file_uploads") ? "<input type='file' name='$name'$onchange>" : lang('File uploads are disabled.'));
61640ee Move common functions
jakubvrana authored
463 } else {
464 // int(3) is only a display hint
465 $maxlength = (!ereg('int', $field["type"]) && preg_match('~^([0-9]+)(,([0-9]+))?$~', $field["length"], $match) ? ($match[1] + ($match[3] ? 1 : 0) + ($match[2] && !$field["unsigned"] ? 1 : 0)) : ($types[$field["type"]] ? $types[$field["type"]] + ($field["unsigned"] ? 0 : 1) : 0));
689699a Shortcut for htmlspecialchars
jakubvrana authored
466 echo "<input name='fields[$name]' value='" . h($value) . "'" . ($maxlength ? " maxlength='$maxlength'" : "") . "$onchange>";
61640ee Move common functions
jakubvrana authored
467 }
468 }
469 }
470
9a176b0 Comments
jakubvrana authored
471 /** Process edit input field
472 * @param one field from fields()
473 * @return string
474 */
a0def47 Date localization
jakubvrana authored
475 function process_input($field) {
c64c4fd Adminer class
jakubvrana authored
476 global $dbh, $adminer;
a0def47 Date localization
jakubvrana authored
477 $idf = bracket_escape($field["field"]);
61640ee Move common functions
jakubvrana authored
478 $function = $_POST["function"][$idf];
479 $value = $_POST["fields"][$idf];
480 if ($field["type"] == "enum" ? $value == -1 : $function == "orig") {
481 return false;
482 } elseif ($field["type"] == "enum" || $field["auto_increment"] ? !strlen($value) : $function == "NULL") {
483 return "NULL";
484 } elseif ($field["type"] == "enum") {
4cead56 Edit default values directly in table creation
jakubvrana authored
485 return intval($value);
61640ee Move common functions
jakubvrana authored
486 } elseif ($field["type"] == "set") {
4cead56 Edit default values directly in table creation
jakubvrana authored
487 return array_sum((array) $value);
5abd943 Change simple preg_match to ereg
jakubvrana authored
488 } elseif (ereg('binary|blob', $field["type"])) {
61640ee Move common functions
jakubvrana authored
489 $file = get_file($idf);
490 if (!is_string($file)) {
491 return false; //! report errors
492 }
572d86c Change simple preg_match to ereg
jakubvrana authored
493 return "_binary" . $dbh->quote($file);
61640ee Move common functions
jakubvrana authored
494 } else {
a0def47 Date localization
jakubvrana authored
495 return $adminer->processInput($field, $value, $function);
61640ee Move common functions
jakubvrana authored
496 }
497 }
498
9a176b0 Comments
jakubvrana authored
499 /** Print data with optional compression
500 * @param string null to force output
501 * @return null
502 */
702e949 Bzip2 compression
jakubvrana authored
503 function dump($string = null) { // null $string forces sending of buffer
504 static $buffer = ""; // used to improve compression and to allow GZ archives unpackable in Total Commander
505 if ($_POST["compress"]) {
506 $buffer .= $string;
507 if (!isset($string) || strlen($buffer) > 1e6) {
508 if ($_POST["compress"] == "bz2") {
509 echo bzcompress($buffer); // should not be called repeatedly but it would require whole buffer in memory or temporary file
510 } else {
511 echo gzencode($buffer);
512 }
513 $buffer = "";
514 }
25f01d3 Compress export and import
jakubvrana authored
515 } else {
516 echo $string;
517 }
518 }
519
9a176b0 Comments
jakubvrana authored
520 /** Print CSV row
521 * @param array
522 * @return null
523 */
61640ee Move common functions
jakubvrana authored
524 function dump_csv($row) {
525 foreach ($row as $key => $val) {
526 if (preg_match("~[\"\n,]~", $val) || (isset($val) && !strlen($val))) {
527 $row[$key] = '"' . str_replace('"', '""', $val) . '"';
528 }
529 }
25f01d3 Compress export and import
jakubvrana authored
530 dump(implode(",", $row) . "\n");
61640ee Move common functions
jakubvrana authored
531 }
c16c57b E-mail sending
jakubvrana authored
532
9a176b0 Comments
jakubvrana authored
533 /** Apply SQL function
534 * @param string
535 * @param string escaped column identifier
536 * @return string
537 */
e331067 Move apply_sql_function
jakubvrana authored
538 function apply_sql_function($function, $column) {
539 return ($function ? ($function == "count distinct" ? "COUNT(DISTINCT " : strtoupper("$function(")) . "$column)" : $column);
540 }
541
9a176b0 Comments
jakubvrana authored
542 /** Check whether the string is e-mail address
543 * @param string
544 * @return bool
545 */
c16c57b E-mail sending
jakubvrana authored
546 function is_email($email) {
547 $atom = '[-a-z0-9!#$%&\'*+/=?^_`{|}~]'; // characters of local-name
548 $domain = '[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])'; // one domain component
549 return eregi("^$atom+(\\.$atom+)*@($domain?\\.)+$domain\$", $email);
550 }
Something went wrong with that request. Please try again.