Permalink
Newer
Older
100644 817 lines (761 sloc) 23 KB
Jul 2, 2007
1
<?php
Aug 31, 2009
2
/** Get database connection
3
* @return Min_DB
4
*/
5
function connection() {
6
// can be used in customization, $connection is minified
7
global $connection;
8
return $connection;
Jul 27, 2009
9
}
10
Aug 31, 2009
11
/** Unescape database identifier
12
* @param string text inside ``
13
* @return string
14
*/
Jul 2, 2007
15
function idf_unescape($idf) {
Apr 21, 2010
16
$last = substr($idf, -1);
17
return str_replace($last . $last, $last, substr($idf, 1, -1));
Jul 2, 2007
18
}
19
Oct 16, 2009
20
/** Escape string to use inside ''
21
* @param string
22
* @return string
23
*/
24
function escape_string($val) {
25
return substr(q($val), 1, -1);
Oct 16, 2009
26
}
27
28
/** Disable magic_quotes_gpc
29
* @param array e.g. (&$_GET, &$_POST, &$_COOKIE)
30
* @return null modified in place
31
*/
32
function remove_slashes($process) {
33
if (get_magic_quotes_gpc()) {
34
while (list($key, $val) = each($process)) {
35
foreach ($val as $k => $v) {
36
unset($process[$key][$k]);
37
if (is_array($v)) {
38
$process[$key][stripslashes($k)] = $v;
39
$process[] = &$process[$key][stripslashes($k)];
40
} else {
41
$process[$key][stripslashes($k)] = ($filter ? $v : stripslashes($v));
42
}
43
}
44
}
45
}
46
}
47
Aug 31, 2009
48
/** Escape or unescape string to use inside form []
49
* @param string
50
* @param bool
51
* @return string
52
*/
Jul 2, 2007
53
function bracket_escape($idf, $back = false) {
Jun 21, 2009
54
// escape brackets inside name="x[]"
55
static $trans = array(':' => ':1', ']' => ':2', '[' => ':3');
Jul 2, 2007
56
return strtr($idf, ($back ? array_flip($trans) : $trans));
57
}
58
Aug 31, 2009
59
/** Escape for HTML
60
* @param string
61
* @return string
62
*/
63
function h($string) {
64
return htmlspecialchars($string, ENT_QUOTES);
65
}
66
Aug 31, 2009
67
/** Escape for TD
68
* @param string
69
* @return string
70
*/
Aug 25, 2009
71
function nbsp($string) {
72
return (trim($string) != "" ? h($string) : "&nbsp;");
Aug 25, 2009
73
}
74
75
/** Convert \n to <br>
76
* @param string
77
* @return string
78
*/
79
function nl_br($string) {
80
return str_replace("\n", "<br>", $string); // nl2br() uses XHTML before PHP 5.3
81
}
82
Sep 25, 2009
83
/** Generate HTML checkbox
84
* @param string
85
* @param string
86
* @param bool
87
* @param string
88
* @param string
89
* @return string
90
*/
91
function checkbox($name, $value, $checked, $label = "", $onclick = "") {
92
static $id = 0;
93
$id++;
94
$return = "<input type='checkbox'" . ($name ? " name='$name' value='" . h($value) . "'" : "") . ($checked ? " checked" : "") . ($onclick ? " onclick=\"$onclick\"" : "") . " id='checkbox-$id'>";
95
return ($label != "" ? "<label for='checkbox-$id'>$return" . h($label) . "</label>" : $return);
Sep 25, 2009
96
}
97
Aug 31, 2009
98
/** Generate list of HTML options
99
* @param array array of strings or arrays (creates optgroup)
100
* @param mixed
101
* @param bool always use array keys for value="", otherwise only string keys are used
102
* @return string
103
*/
104
function optionlist($options, $selected = null, $use_keys = false) {
Jul 2, 2007
105
$return = "";
106
foreach ($options as $k => $v) {
107
if (is_array($v)) {
108
$return .= '<optgroup label="' . h($k) . '">';
Jul 2, 2007
109
}
110
foreach ((is_array($v) ? $v : array($k => $v)) as $key => $val) {
Jul 28, 2009
111
$return .= '<option' . ($use_keys || is_string($key) ? ' value="' . h($key) . '"' : '') . (($use_keys || is_string($key) ? (string) $key : $val) === $selected ? ' selected' : '') . '>' . h($val);
Jul 2, 2007
112
}
113
if (is_array($v)) {
114
$return .= '</optgroup>';
115
}
116
}
117
return $return;
118
}
119
Nov 9, 2009
120
/** Generate HTML radio list
121
* @param string
122
* @param array
123
* @param string
124
* @param string true for no onchange, false for radio
125
* @return string
126
*/
127
function html_select($name, $options, $value = "", $onchange = true) {
128
if ($onchange) {
129
return "<select name='" . h($name) . "'" . (is_string($onchange) ? " onchange=\"$onchange\"" : "") . ">" . optionlist($options, $value) . "</select>";
130
}
131
$return = "";
132
foreach ($options as $key => $val) {
133
$return .= "<label><input type='radio' name='" . h($name) . "' value='" . h($key) . "'" . ($key == $value ? " checked" : "") . ">" . h($val) . "</label>";
134
}
135
return $return;
136
}
137
138
/** Get onclick confirmation
139
* @param string JavaScript expression
140
* @return string
141
*/
142
function confirm($count = "") {
143
return " onclick=\"return confirm('" . lang('Are you sure?') . ($count ? " (' + $count + ')" : "") . "');\"";
144
}
145
146
/** Escape string for JavaScript apostrophes
147
* @param string
148
* @return string
149
*/
150
function js_escape($string) {
151
return addcslashes($string, "\r\n'\\/"); // slash for <script>
152
}
153
154
/** Get INI boolean value
155
* @param string
156
* @return bool
157
*/
158
function ini_bool($ini) {
159
$val = ini_get($ini);
160
return (eregi('^(on|true|yes)$', $val) || (int) $val); // boolean values set by php_value are strings
161
}
162
163
function q($string) {
164
global $connection;
165
return $connection->quote($string);
166
}
167
Aug 31, 2009
168
/** Get list of values from database
169
* @param string
170
* @param mixed
171
* @return array
172
*/
173
function get_vals($query, $column = 0) {
174
global $connection;
Jul 11, 2007
175
$return = array();
176
$result = $connection->query($query);
177
if (is_object($result)) {
178
while ($row = $result->fetch_row()) {
179
$return[] = $row[$column];
180
}
Jul 11, 2007
181
}
182
return $return;
183
}
184
185
/** Get keys from first column and values from second
186
* @param string
187
* @param Min_DB
188
* @return array
189
*/
190
function get_key_vals($query, $connection2 = null) {
191
global $connection;
192
if (!is_object($connection2)) {
193
$connection2 = $connection;
194
}
195
$return = array();
196
$result = $connection2->query($query);
197
while ($row = $result->fetch_row()) {
198
$return[$row[0]] = $row[1];
199
}
200
return $return;
201
}
202
@vrana
Oct 13, 2010
203
/** Get all rows of result
204
* @param string
205
* @return array associative
206
*/
207
function get_rows($query, $connection2 = null, $error = "<p class='error'>") {
@vrana
Oct 13, 2010
208
global $connection;
209
if (!is_object($connection2)) {
210
$connection2 = $connection;
211
}
212
$return = array();
213
$result = $connection2->query($query);
214
if (is_object($result)) { // can return true
215
while ($row = $result->fetch_assoc()) {
216
$return[] = $row;
217
}
218
} elseif (!$result && $error && (headers_sent() || ob_get_level())) {
219
echo $error . error() . "\n";
@vrana
Oct 13, 2010
220
}
221
return $return;
222
}
223
Aug 31, 2009
224
/** Find unique identifier of a row
225
* @param array
226
* @param array result of indexes()
227
* @return array
Aug 31, 2009
228
*/
229
function unique_array($row, $indexes) {
Jul 5, 2007
230
foreach ($indexes as $index) {
Oct 7, 2009
231
if (ereg("PRIMARY|UNIQUE", $index["type"])) {
Jul 5, 2007
232
$return = array();
233
foreach ($index["columns"] as $key) {
Jun 21, 2009
234
if (!isset($row[$key])) { // NULL is ambiguous
Jul 5, 2007
235
continue 2;
Jul 2, 2007
236
}
237
$return[$key] = $row[$key];
Jul 2, 2007
238
}
Jul 5, 2007
239
return $return;
Jul 2, 2007
240
}
241
}
242
$return = array();
243
foreach ($row as $key => $val) {
244
if (!preg_match('~^(COUNT\\((\\*|(DISTINCT )?`(?:[^`]|``)+`)\\)|(AVG|GROUP_CONCAT|MAX|MIN|SUM)\\(`(?:[^`]|``)+`\\))$~', $key)) { //! columns looking like functions
245
$return[$key] = $val;
Jul 9, 2007
247
}
248
return $return;
249
}
250
Aug 31, 2009
251
/** Create SQL condition from parsed query string
252
* @param array parsed query string
253
* @return string
254
*/
255
function where($where) {
Jul 9, 2007
257
$return = array();
258
foreach ((array) $where["where"] as $key => $val) {
259
$return[] = idf_escape(bracket_escape($key, 1)) // 1 - back
260
. (ereg('\\.', $val) || $jush == "mssql" ? " LIKE " . exact_value(addcslashes($val, "%_")) : " = " . exact_value($val)) // LIKE because of floats, but slow with ints, in MS SQL because of text
261
; //! enum and set
262
}
263
foreach ((array) $where["null"] as $key) {
264
$return[] = idf_escape($key) . " IS NULL";
Jul 2, 2007
265
}
Jul 11, 2009
266
return implode(" AND ", $return);
Jul 2, 2007
267
}
268
Aug 31, 2009
269
/** Create SQL condition from query string
270
* @param string
271
* @return string
272
*/
273
function where_check($val) {
274
parse_str($val, $check);
275
remove_slashes(array(&$check));
276
return where($check);
277
}
278
Aug 31, 2009
279
/** Create query string where condition from value
280
* @param int condition order
281
* @param string column identifier
282
* @param string
283
* @return string
284
* @return string
Aug 31, 2009
285
*/
286
function where_link($i, $column, $value, $operator = "=") {
287
return "&where%5B$i%5D%5Bcol%5D=" . urlencode($column) . "&where%5B$i%5D%5Bop%5D=" . urlencode($operator) . "&where%5B$i%5D%5Bval%5D=" . urlencode($value);
Jul 15, 2009
288
}
289
290
/** Set cookie valid for 1 month
291
* @param string
292
* @param string
293
* @return bool
294
*/
295
function cookie($name, $value) {
296
global $HTTPS;
297
$params = array(
298
$name,
299
(ereg("\n", $value) ? "" : $value), // HTTP Response Splitting protection in PHP < 5.1.2
300
time() + 2592000, // 2592000 - 30 days
301
preg_replace('~\\?.*~', '', $_SERVER["REQUEST_URI"]),
302
"",
303
$HTTPS
304
);
Dec 18, 2009
305
if (version_compare(PHP_VERSION, '5.2.0') >= 0) {
306
$params[] = true; // HttpOnly
307
}
308
return call_user_func_array('setcookie', $params);
309
}
310
Nov 2, 2009
311
/** Restart stopped session
312
* @return null
313
*/
314
function restart_session() {
315
if (!ini_bool("session.use_cookies")) {
Nov 2, 2009
316
session_start();
317
}
318
}
319
320
/** Get session variable for current server
321
* @param string
322
* @return mixed
323
*/
324
function &get_session($key) {
325
return $_SESSION[$key][DRIVER][SERVER][$_GET["username"]];
326
}
327
328
/** Set session variable for current server
329
* @param string
330
* @param mixed
331
* @return mixed
332
*/
333
function set_session($key, $val) {
334
$_SESSION[$key][DRIVER][SERVER][$_GET["username"]] = $val; // used also in auth.inc.php
335
}
336
337
/** Get authenticated URL
338
* @param string
339
* @param string
340
* @param string
341
* @return string
342
*/
343
function auth_url($driver, $server, $username) {
344
global $drivers;
345
preg_match('~([^?]*)\\??(.*)~', remove_from_uri(implode("|", array_keys($drivers)) . "|username|" . session_name()), $match);
346
return "$match[1]?"
347
. (SID && !$_COOKIE ? SID . "&" : "")
348
. ($driver != "server" || $server != "" ? urlencode($driver) . "=" . urlencode($server) . "&" : "")
349
. "username=" . urlencode($username)
350
. ($match[2] ? "&$match[2]" : "")
351
;
352
}
353
354
/** Find whether it is an AJAX request
355
* @return bool
356
*/
357
function is_ajax() {
358
return ($_SERVER["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest");
359
}
360
Aug 31, 2009
361
/** Send Location header and exit
362
* @param string null to only set a message
Aug 31, 2009
363
* @param string
364
* @return null
365
*/
Jul 9, 2007
366
function redirect($location, $message = null) {
367
if (isset($message)) {
368
restart_session();
369
$_SESSION["messages"][] = $message;
Jul 9, 2007
370
}
371
if (isset($location)) {
372
if ($location == "") {
373
$location = ".";
374
}
375
if (!is_ajax()) {
376
header("Location: $location");
377
exit;
378
}
379
header("X-AJAX-Redirect: $location");
380
$_POST = array();
381
}
Jul 9, 2007
382
}
383
Aug 31, 2009
384
/** Execute query and redirect if successful
385
* @param string
386
* @param string
387
* @param string
388
* @param bool
389
* @param bool
390
* @param bool
391
* @return bool
392
*/
393
function query_redirect($query, $location, $message, $redirect = true, $execute = true, $failed = false) {
394
global $connection, $error, $adminer;
Nov 2, 2009
395
if ($execute) {
396
$failed = !$connection->query($query);
397
}
398
$sql = "";
399
if ($query) {
Jul 27, 2009
400
$sql = $adminer->messageQuery($query);
401
}
402
if ($failed) {
403
$error = error() . $sql;
404
return false;
405
}
406
if ($redirect) {
407
redirect($location, $message . $sql);
408
}
409
return true;
410
}
411
Aug 31, 2009
412
/** Execute and remember query
413
* @param string null to return remembered queries
414
* @return Min_Result
415
*/
416
function queries($query = null) {
417
global $connection;
418
static $queries = array();
419
if (!isset($query)) {
Jun 21, 2009
420
// return executed queries without parameter
Sep 25, 2008
421
return implode(";\n", $queries);
422
}
423
$queries[] = $query;
424
return $connection->query($query);
427
/** Apply command to all array items
428
* @param string
429
* @param array
430
* @param callback
431
* @return bool
432
*/
433
function apply_queries($query, $tables, $escape = 'table') {
434
foreach ($tables as $table) {
435
if (!queries("$query " . $escape($table))) {
436
return false;
437
}
438
}
439
return true;
440
}
441
442
/** Redirect by remembered queries
443
* @param string
444
* @param string
445
* @param bool
@vrana
Oct 15, 2010
446
* @return bool
447
*/
448
function queries_redirect($location, $message, $redirect) {
449
return query_redirect(queries(), $location, $message, $redirect, false, !$redirect);
450
}
451
Aug 31, 2009
452
/** Remove parameter from query string
453
* @param string
454
* @return string
455
*/
Jul 27, 2007
456
function remove_from_uri($param = "") {
457
return substr(preg_replace("~(?<=[?&])($param" . (SID ? "" : "|" . session_name()) . ")=[^&]*&~", '', "$_SERVER[REQUEST_URI]&"), 0, -1);
Jul 27, 2007
458
}
459
Aug 31, 2009
460
/** Generate page number for pagination
461
* @param int
462
* @return string
463
*/
464
function pagination($page, $current) {
@vrana
Nov 22, 2010
465
return " " . ($page == $current ? $page + 1 : '<a href="' . h(remove_from_uri("page") . ($page ? "&page=$page" : "")) . '">' . ($page + 1) . "</a>");
Aug 7, 2008
466
}
467
468
/** Get file contents from $_FILES
Aug 31, 2009
469
* @param string
470
* @param bool
471
* @return mixed int for error, string otherwise
Aug 31, 2009
472
*/
473
function get_file($key, $decompress = false) {
474
$file = $_FILES[$key];
475
if (!$file || $file["error"]) {
476
return $file["error"];
Jul 9, 2007
477
}
Aug 29, 2009
478
return file_get_contents($decompress && ereg('\\.gz$', $file["name"]) ? "compress.zlib://$file[tmp_name]"
479
: ($decompress && ereg('\\.bz2$', $file["name"]) ? "compress.bzip2://$file[tmp_name]"
480
: $file["tmp_name"]
481
)); //! may not be reachable because of open_basedir
Jul 9, 2007
482
}
483
Aug 31, 2009
484
/** Determine upload error
485
* @param int
486
* @return string
487
*/
488
function upload_error($error) {
489
$max_size = ($error == UPLOAD_ERR_INI_SIZE ? ini_get("upload_max_filesize") : null); // post_max_size is checked in index.php
490
return ($error ? lang('Unable to upload a file.') . ($max_size ? " " . lang('Maximum allowed file size is %sB.', $max_size) : "") : lang('File does not exist.'));
491
}
492
Aug 31, 2009
493
/** Generate class for odd rows
494
* @param string return this for odd rows, empty to reset counter
495
* @return string
496
*/
497
function odd($return = ' class="odd"') {
498
static $i = 0;
Aug 31, 2009
499
if (!$return) { // reset counter
500
$i = -1;
501
}
Aug 31, 2009
502
return ($i++ % 2 ? $return : '');
503
}
504
505
/** Print one row in JSON object
506
* @param string or "" to close the object
507
* @param string
508
* @return null
509
*/
510
function json_row($key, $val = null) {
511
static $first = true;
512
if ($first) {
513
echo "{";
514
}
515
if ($key != "") {
516
echo ($first ? "" : ",") . "\n\t\"" . addcslashes($key, '\\"') . '": ' . (isset($val) ? '"' . addcslashes($val, '\\"') . '"' : 'undefined');
517
$first = false;
518
} else {
519
echo "\n}\n";
520
$first = true;
521
}
522
}
523
Aug 31, 2009
524
/** Check whether the string is in UTF-8
525
* @param string
526
* @return bool
527
*/
Jun 11, 2009
528
function is_utf8($val) {
Jun 21, 2009
529
// don't print control chars except \t\r\n
Jun 11, 2009
530
return (preg_match('~~u', $val) && !preg_match('~[\\0-\\x8\\xB\\xC\\xE-\\x1F]~', $val));
531
}
532
Aug 31, 2009
533
/** Shorten UTF-8 string
534
* @param string
535
* @param int
536
* @param string
537
* @return string escaped string with appended ...
538
*/
539
function shorten_utf8($string, $length = 80, $suffix = "") {
540
if (!preg_match("(^([\t\r\n -\x{FFFF}]{0,$length})($)?)u", $string, $match)) { // ~s causes trash in $match[2] under some PHP versions, (.|\n) is slow
541
preg_match("(^([\t\r\n -~]{0,$length})($)?)", $string, $match);
Aug 25, 2009
542
}
543
return h($match[1]) . $suffix . (isset($match[2]) ? "" : "<i>...</i>");
Aug 24, 2007
544
}
Aug 31, 2009
546
/** Generate friendly URL
547
* @param string
548
* @return string
549
*/
Jun 11, 2009
550
function friendly_url($val) {
Jun 21, 2009
551
// used for blobs and export
Jun 11, 2009
552
return preg_replace('~[^a-z0-9_]~i', '-', $val);
553
}
554
Aug 31, 2009
555
/** Print hidden fields
556
* @param array
557
* @param array
558
* @return null
559
*/
Oct 9, 2008
560
function hidden_fields($process, $ignore = array()) {
561
while (list($key, $val) = each($process)) {
562
if (is_array($val)) {
563
foreach ($val as $k => $v) {
564
$process[$key . "[$k]"] = $v;
565
}
566
} elseif (!in_array($key, $ignore)) {
567
echo '<input type="hidden" name="' . h($key) . '" value="' . h($val) . '">';
568
}
569
}
570
}
Jul 3, 2009
571
572
/** Print hidden fields for GET forms
573
* @return null
574
*/
575
function hidden_fields_get() {
576
echo (SID && !$_COOKIE ? '<input type="hidden" name="' . session_name() . '" value="' . h(session_id()) . '">' : '');
577
echo (SERVER !== null ? '<input type="hidden" name="' . DRIVER . '" value="' . h(SERVER) . '">' : "");
578
echo '<input type="hidden" name="username" value="' . h($_GET["username"]) . '">';
579
}
580
Aug 31, 2009
581
/** Find out foreign keys for each column
582
* @param string
583
* @return array array($col => array())
584
*/
585
function column_foreign_keys($table) {
586
global $adminer;
587
$return = array();
588
foreach ($adminer->foreignKeys($table) as $foreign_key) {
589
foreach ($foreign_key["source"] as $val) {
590
$return[$val][] = $foreign_key;
591
}
592
}
593
return $return;
594
}
595
596
/** Print enum input field
597
* @param string "radio"|"checkbox"
598
* @param string
599
* @param array
600
* @param mixed int|string|array
601
* @return null
602
*/
@vrana
Sep 9, 2010
603
function enum_input($type, $attrs, $field, $value) {
604
preg_match_all("~'((?:[^']|'')*)'~", $field["length"], $matches);
606
foreach ($matches[1] as $i => $val) {
607
$val = stripcslashes(str_replace("''", "'", $val));
608
$checked = (is_int($value) ? $value == $i+1 : (is_array($value) ? in_array($i+1, $value) : $value === $val));
609
$return .= " <label><input type='$type'$attrs value='" . ($i+1) . "'" . ($checked ? ' checked' : '') . '>' . h($val) . '</label>';
610
}
612
}
613
Aug 31, 2009
614
/** Print edit input field
615
* @param array one field from fields()
616
* @param mixed
617
* @param string
618
* @return null
619
*/
620
function input($field, $value, $function) {
621
global $types, $adminer, $jush;
622
$name = h(bracket_escape($field["field"]));
Jul 3, 2009
623
echo "<td class='function'>";
624
$functions = (isset($_GET["select"]) ? array("orig" => lang('original')) : array()) + $adminer->editFunctions($field);
@vrana
Sep 9, 2010
625
$attrs = " name='fields[$name]'";
Jul 3, 2009
626
if ($field["type"] == "enum") {
627
echo nbsp($functions[""]) . "<td>" . $adminer->editInput($_GET["edit"], $field, $attrs, $value);
Jul 3, 2009
628
} else {
629
$first = 0;
630
foreach ($functions as $key => $val) {
631
if ($key === "" || !$val) {
632
break;
633
}
634
$first++;
635
}
636
$onchange = ($first ? " onchange=\"var f = this.form['function[" . js_escape($name) . "]']; if ($first > f.selectedIndex) f.selectedIndex = $first;\"" : "");
@vrana
Sep 9, 2010
637
$attrs .= $onchange;
638
echo (count($functions) > 1 ? html_select("function[$name]", $functions, !isset($function) || in_array($function, $functions) || isset($functions[$function]) ? $function : "") : nbsp(reset($functions))) . '<td>';
639
$input = $adminer->editInput($_GET["edit"], $field, $attrs, $value); // usage in call is without a table
640
if ($input != "") {
641
echo $input;
642
} elseif ($field["type"] == "set") { //! 64 bits
643
preg_match_all("~'((?:[^']|'')*)'~", $field["length"], $matches);
Jul 3, 2009
644
foreach ($matches[1] as $i => $val) {
645
$val = stripcslashes(str_replace("''", "'", $val));
646
$checked = (is_int($value) ? ($value >> $i) & 1 : in_array($val, explode(",", $value), true));
647
echo " <label><input type='checkbox' name='fields[$name][$i]' value='" . (1 << $i) . "'" . ($checked ? ' checked' : '') . "$onchange>" . h($val) . '</label>';
Jul 3, 2009
648
}
649
} elseif (ereg('blob|bytea|raw|file', $field["type"]) && ini_bool("file_uploads")) {
650
echo "<input type='file' name='fields-$name'$onchange>";
651
} elseif (ereg('text|lob', $field["type"])) {
652
echo "<textarea " . ($jush != "sqlite" || ereg("\n", $value) ? "cols='50' rows='12'" : "cols='30' rows='1' style='height: 1.2em;'") . "$attrs onkeypress='return textareaKeypress(this, event);'>" . h($value) . '</textarea>'; // 1.2em - line-height
Jul 3, 2009
653
} else {
654
// int(3) is only a display hint
655
$maxlength = (!ereg('int', $field["type"]) && preg_match('~^(\\d+)(,(\\d+))?$~', $field["length"], $match) ? ((ereg("binary", $field["type"]) ? 2 : 1) * $match[1] + ($match[3] ? 1 : 0) + ($match[2] && !$field["unsigned"] ? 1 : 0)) : ($types[$field["type"]] ? $types[$field["type"]] + ($field["unsigned"] ? 0 : 1) : 0));
656
echo "<input value='" . h($value) . "'" . ($maxlength ? " maxlength='$maxlength'" : "") . (ereg('char|binary', $field["type"]) && $maxlength > 20 ? " size='40'" : "") . "$attrs>";
Jul 3, 2009
657
}
658
}
659
}
660
Aug 31, 2009
661
/** Process edit input field
662
* @param one field from fields()
663
* @return string
664
*/
Jul 27, 2009
665
function process_input($field) {
666
global $adminer;
Jul 27, 2009
667
$idf = bracket_escape($field["field"]);
Jul 3, 2009
668
$function = $_POST["function"][$idf];
669
$value = $_POST["fields"][$idf];
670
if ($field["type"] == "enum") {
671
if ($value == -1) {
672
return false;
673
}
674
if ($value == "") {
675
return "NULL";
676
}
677
return +$value;
678
}
679
if ($field["auto_increment"] && $value == "") {
680
return null;
681
}
682
if ($function == "orig") {
Jul 3, 2009
683
return false;
684
}
685
if ($function == "NULL") {
Jul 3, 2009
686
return "NULL";
687
}
688
if ($field["type"] == "set") {
689
return array_sum((array) $value);
690
}
691
if (ereg('blob|bytea|raw|file', $field["type"]) && ini_bool("file_uploads")) {
692
$file = get_file("fields-$idf");
Jul 3, 2009
693
if (!is_string($file)) {
694
return false; //! report errors
695
}
696
return q($file);
Jul 3, 2009
697
}
698
return $adminer->processInput($field, $value, $function);
Jul 3, 2009
699
}
700
Feb 16, 2010
701
/** Print results of search in all tables
702
* @uses $_GET["where"][0]
703
* @uses $_POST["tables"]
704
* @return null
705
*/
706
function search_tables() {
707
global $adminer, $connection;
708
$_GET["where"][0]["op"] = "LIKE %%";
709
$_GET["where"][0]["val"] = $_POST["query"];
Feb 16, 2010
710
$found = false;
711
foreach (table_status() as $table => $table_status) {
712
$name = $adminer->tableName($table_status);
713
if (isset($table_status["Engine"]) && $name != "" && (!$_POST["tables"] || in_array($table, $_POST["tables"]))) {
714
$result = $connection->query("SELECT" . limit("1 FROM " . table($table), " WHERE " . implode(" AND ", $adminer->selectSearchProcess(fields($table), array())), 1));
@vrana
May 27, 2010
715
if ($result->fetch_row()) {
Feb 16, 2010
716
if (!$found) {
717
echo "<ul>\n";
718
$found = true;
719
}
720
echo "<li><a href='" . h(ME . "select=" . urlencode($table) . "&where[0][op]=" . urlencode($_GET["where"][0]["op"]) . "&where[0][val]=" . urlencode($_GET["where"][0]["val"])) . "'>" . h($name) . "</a>\n";
721
}
722
}
723
}
724
echo ($found ? "</ul>" : "<p class='message'>" . lang('No tables.')) . "\n";
725
}
726
Aug 31, 2009
727
/** Print CSV row
728
* @param array
729
* @return null
730
*/
Jul 3, 2009
731
function dump_csv($row) {
732
foreach ($row as $key => $val) {
733
if (preg_match("~[\"\n,;\t]~", $val) || $val === "") {
Jul 3, 2009
734
$row[$key] = '"' . str_replace('"', '""', $val) . '"';
735
}
736
}
737
echo implode(($_POST["format"] == "csv" ? "," : ($_POST["format"] == "tsv" ? "\t" : ";")), $row) . "\n";
Jul 3, 2009
738
}
Jul 11, 2009
739
Aug 31, 2009
740
/** Apply SQL function
741
* @param string
742
* @param string escaped column identifier
743
* @return string
744
*/
745
function apply_sql_function($function, $column) {
746
return ($function ? ($function == "unixepoch" ? "DATETIME($column, '$function')" : ($function == "count distinct" ? "COUNT(DISTINCT " : strtoupper("$function(")) . "$column)") : $column);