Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 650 lines (604 sloc) 19.442 kb
667bfec Decomposition
jakubvrana authored
1 <?php
9a176b0 Comments
jakubvrana authored
2 /** Get database connection
3 * @return Min_DB
4 */
64d616c Rename get_dbh to connection
jakubvrana authored
5 function connection() {
6 // can be used in customization, $connection is minified
7 global $connection;
8 return $connection;
c64c4fd Adminer class
jakubvrana authored
9 }
10
9a176b0 Comments
jakubvrana authored
11 /** Unescape database identifier
12 * @param string text inside ``
13 * @return string
14 */
667bfec Decomposition
jakubvrana authored
15 function idf_unescape($idf) {
3f5b683 Reintegrate sqlite branch
jakubvrana authored
16 return str_replace($idf[0] . $idf[0], $idf[0], substr($idf, 1, -1));
667bfec Decomposition
jakubvrana authored
17 }
18
5e01a62 Prepare for SQLite
jakubvrana authored
19 /** Escape string to use inside ''
20 * @param string
21 * @return string
22 */
23 function escape_string($val) {
24 global $connection;
25 return substr($connection->quote($val), 1, -1);
26 }
27
9a176b0 Comments
jakubvrana authored
28 /** Escape or unescape string to use inside form []
29 * @param string
30 * @param bool
31 * @return string
32 */
667bfec Decomposition
jakubvrana authored
33 function bracket_escape($idf, $back = false) {
64ba924 Comments
jakubvrana authored
34 // escape brackets inside name="x[]"
73e8631 Move stuff to functions.inc.php
jakubvrana authored
35 static $trans = array(':' => ':1', ']' => ':2', '[' => ':3');
667bfec Decomposition
jakubvrana authored
36 return strtr($idf, ($back ? array_flip($trans) : $trans));
37 }
38
9a176b0 Comments
jakubvrana authored
39 /** Escape for HTML
40 * @param string
41 * @return string
42 */
689699a Shortcut for htmlspecialchars
jakubvrana authored
43 function h($string) {
44 return htmlspecialchars($string, ENT_QUOTES);
45 }
46
9a176b0 Comments
jakubvrana authored
47 /** Escape for TD
48 * @param string
49 * @return string
50 */
c196985 Function nbsp
jakubvrana authored
51 function nbsp($string) {
7352c28 Replace strlen() by != ""
jakubvrana authored
52 return (trim($string) != "" ? h($string) : "&nbsp;");
c196985 Function nbsp
jakubvrana authored
53 }
54
3f5b683 Reintegrate sqlite branch
jakubvrana authored
55 /** Convert \n to <br>
56 * @param string
57 * @return string
58 */
59 function nl_br($string) {
60 return str_replace("\n", "<br>", $string); // nl2br() uses XHTML before PHP 5.3
61 }
62
6b30cfa Separate checkbox
jakubvrana authored
63 /** Generate HTML checkbox
64 * @param string
65 * @param string
66 * @param bool
67 * @param string
68 * @param string
69 * @return string
70 */
71 function checkbox($name, $value, $checked, $label = "", $onclick = "") {
c4fe12a Clickable labels in IE6
jakubvrana authored
72 static $id = 0;
73 $id++;
3f5b683 Reintegrate sqlite branch
jakubvrana authored
74 $return = "<input type='checkbox'" . ($name ? " name='$name' value='" . h($value) . "'" : "") . ($checked ? " checked" : "") . ($onclick ? " onclick=\"$onclick\"" : "") . " id='checkbox-$id'>";
7352c28 Replace strlen() by != ""
jakubvrana authored
75 return ($label != "" ? "<label for='checkbox-$id'>$return" . h($label) . "</label>" : $return);
6b30cfa Separate checkbox
jakubvrana authored
76 }
77
9a176b0 Comments
jakubvrana authored
78 /** Generate list of HTML options
79 * @param array array of strings or arrays (creates optgroup)
80 * @param mixed
81 * @param bool always use array keys for value="", otherwise only string keys are used
82 * @return string
83 */
1e4d11e Select from foreign keys in Editor
jakubvrana authored
84 function optionlist($options, $selected = null, $use_keys = false) {
667bfec Decomposition
jakubvrana authored
85 $return = "";
86 foreach ($options as $k => $v) {
87 if (is_array($v)) {
689699a Shortcut for htmlspecialchars
jakubvrana authored
88 $return .= '<optgroup label="' . h($k) . '">';
667bfec Decomposition
jakubvrana authored
89 }
ced9de9 Create single column foreign key in table structure
jakubvrana authored
90 foreach ((is_array($v) ? $v : array($k => $v)) as $key => $val) {
cb6d36c HTML instead of XHTML
jakubvrana authored
91 $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
92 }
93 if (is_array($v)) {
94 $return .= '</optgroup>';
95 }
96 }
97 return $return;
98 }
99
098b74b Move function
jakubvrana authored
100 /** Generate HTML radio list
101 * @param string
102 * @param array
103 * @param string
104 * @param string true for no onchange, false for radio
105 * @return string
106 */
107 function html_select($name, $options, $value = "", $onchange = true) {
108 if ($onchange) {
109 return "<select name='" . h($name) . "'" . (is_string($onchange) ? " onchange=\"$onchange\"" : "") . ">" . optionlist($options, $value) . "</select>";
110 }
111 $return = "";
112 foreach ($options as $key => $val) {
113 $return .= "<label><input type='radio' name='" . h($name) . "' value='" . h($key) . "'" . ($key == $value ? " checked" : "") . ">" . h($val) . "</label>";
114 }
115 return $return;
116 }
117
3f5b683 Reintegrate sqlite branch
jakubvrana authored
118 /** Get INI boolean value
119 * @param string
120 * @return bool
121 */
122 function ini_bool($ini) {
123 $val = ini_get($ini);
124 return (eregi('^(on|true|yes)$', $val) || (int) $val); // boolean values set by php_value are strings
125 }
126
9a176b0 Comments
jakubvrana authored
127 /** Get list of values from database
128 * @param string
129 * @param mixed
130 * @return array
131 */
0bd864e Allow specifying column in get_vals
jakubvrana authored
132 function get_vals($query, $column = 0) {
64d616c Rename get_dbh to connection
jakubvrana authored
133 global $connection;
7769f2c Separate get_vals
jakubvrana authored
134 $return = array();
64d616c Rename get_dbh to connection
jakubvrana authored
135 $result = $connection->query($query);
49f7886 SHOW DATABASES can be revoked
jakubvrana authored
136 if ($result) {
137 while ($row = $result->fetch_row()) {
0bd864e Allow specifying column in get_vals
jakubvrana authored
138 $return[] = $row[$column];
49f7886 SHOW DATABASES can be revoked
jakubvrana authored
139 }
7769f2c Separate get_vals
jakubvrana authored
140 }
141 return $return;
142 }
143
3f5b683 Reintegrate sqlite branch
jakubvrana authored
144 /** Get keys from first column and values from second
145 * @param string
146 * @param Min_DB
147 * @return array
148 */
149 function get_key_vals($query, $connection2 = null) {
150 global $connection;
151 if (!is_object($connection2)) {
152 $connection2 = $connection;
153 }
154 $return = array();
155 $result = $connection2->query($query);
156 while ($row = $result->fetch_row()) {
157 $return[$row[0]] = $row[1];
158 }
159 return $return;
160 }
161
9a176b0 Comments
jakubvrana authored
162 /** Find unique identifier of a row
163 * @param array
164 * @param array result of indexes()
35ec64c Link COUNT(*) result to listing
jakubvrana authored
165 * @return array
9a176b0 Comments
jakubvrana authored
166 */
35ec64c Link COUNT(*) result to listing
jakubvrana authored
167 function unique_array($row, $indexes) {
720f5fc Restructure indexes
jakubvrana authored
168 foreach ($indexes as $index) {
4b3d569 Save bytes
jakubvrana authored
169 if (ereg("PRIMARY|UNIQUE", $index["type"])) {
720f5fc Restructure indexes
jakubvrana authored
170 $return = array();
171 foreach ($index["columns"] as $key) {
64ba924 Comments
jakubvrana authored
172 if (!isset($row[$key])) { // NULL is ambiguous
720f5fc Restructure indexes
jakubvrana authored
173 continue 2;
667bfec Decomposition
jakubvrana authored
174 }
35ec64c Link COUNT(*) result to listing
jakubvrana authored
175 $return[$key] = $row[$key];
667bfec Decomposition
jakubvrana authored
176 }
720f5fc Restructure indexes
jakubvrana authored
177 return $return;
667bfec Decomposition
jakubvrana authored
178 }
179 }
180 $return = array();
181 foreach ($row as $key => $val) {
26c9d64 Fix long SQL query crash (bug #2839231)
jakubvrana authored
182 if (!preg_match('~^(COUNT\\((\\*|(DISTINCT )?`(?:[^`]|``)+`)\\)|(AVG|GROUP_CONCAT|MAX|MIN|SUM)\\(`(?:[^`]|``)+`\\))$~', $key)) { //! columns looking like functions
35ec64c Link COUNT(*) result to listing
jakubvrana authored
183 $return[$key] = $val;
988a29f Don't use aggregation functions in unique_idf
jakubvrana authored
184 }
430034c Cottage homework
jakubvrana authored
185 }
186 return $return;
187 }
188
9a176b0 Comments
jakubvrana authored
189 /** Create SQL condition from parsed query string
190 * @param array parsed query string
191 * @return string
192 */
9ea3112 Mandatory $where in where()
jakubvrana authored
193 function where($where) {
430034c Cottage homework
jakubvrana authored
194 $return = array();
16290b0 Respect where in COUNT(*) link
jakubvrana authored
195 foreach (array("where", "null") as $type) {
196 foreach ((array) $where[$type] as $key => $val) {
197 $key = bracket_escape($key, "back");
198 $return[] = (preg_match('~^[A-Z0-9_]+\\(`(?:[^`]|``)+`\\)$~', $key) ? $key : idf_escape($key)) . ($type == "null" ? " IS NULL" : (ereg('\\.', $val) ? " LIKE " . exact_value(addcslashes($val, "%_")) : " = " . exact_value($val))); // LIKE because of floats, but slow with ints //! enum and set, columns looking like functions
199 }
667bfec Decomposition
jakubvrana authored
200 }
c16c57b E-mail sending
jakubvrana authored
201 return implode(" AND ", $return);
667bfec Decomposition
jakubvrana authored
202 }
203
9a176b0 Comments
jakubvrana authored
204 /** Create SQL condition from query string
205 * @param string
206 * @return string
207 */
0cd2afb Remove single item clone
jakubvrana authored
208 function where_check($val) {
209 parse_str($val, $check);
210 return where($check);
211 }
212
9a176b0 Comments
jakubvrana authored
213 /** Create query string where condition from value
214 * @param int condition order
215 * @param string column identifier
216 * @param string
217 * @return string
186c058 Utilize where_link function
jakubvrana authored
218 * @return string
9a176b0 Comments
jakubvrana authored
219 */
186c058 Utilize where_link function
jakubvrana authored
220 function where_link($i, $column, $value, $operator = "=") {
221 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);
c42c46a Table relations
jakubvrana authored
222 }
223
319cba4 Save e-mail from to cookie
jakubvrana authored
224 /** Set cookie valid for 1 month
225 * @param string
226 * @param string
227 * @return bool
228 */
229 function cookie($name, $value) {
3f5b683 Reintegrate sqlite branch
jakubvrana authored
230 $params = array(
231 $name,
232 (ereg("\n", $value) ? "" : $value), // HTTP Response Splitting protection in PHP < 5.1.2
233 time() + 2592000, // 2592000 - 30 days
234 preg_replace('~\\?.*~', '', $_SERVER["REQUEST_URI"]),
235 "",
236 $_SERVER["HTTPS"] && strcasecmp($_SERVER["HTTPS"], "off")
237 );
c2334db Secure cookies
jakubvrana authored
238 if (version_compare(PHP_VERSION, '5.2.0') >= 0) {
239 $params[] = true; // HttpOnly
240 }
241 return call_user_func_array('setcookie', $params);
319cba4 Save e-mail from to cookie
jakubvrana authored
242 }
243
f9a2023 Improve concurrency
jakubvrana authored
244 /** Restart stopped session
245 * @return null
246 */
247 function restart_session() {
3f5b683 Reintegrate sqlite branch
jakubvrana authored
248 if (!ini_bool("session.use_cookies")) {
f9a2023 Improve concurrency
jakubvrana authored
249 session_start();
250 }
251 }
252
3f5b683 Reintegrate sqlite branch
jakubvrana authored
253 /** Get session variable for current server
254 * @param string
255 * @return mixed
256 */
257 function &get_session($key) {
258 return $_SESSION[$key][DRIVER][SERVER][$_GET["username"]];
259 }
260
261 /** Set session variable for current server
262 * @param string
263 * @param mixed
264 * @return mixed
265 */
266 function set_session($key, $val) {
267 $_SESSION[$key][DRIVER][SERVER][$_GET["username"]] = $val; // used also in auth.inc.php
268 }
269
9a176b0 Comments
jakubvrana authored
270 /** Send Location header and exit
9bb1909 Fix Save and continue edit
jakubvrana authored
271 * @param string null to only set a message
9a176b0 Comments
jakubvrana authored
272 * @param string
273 * @return null
274 */
430034c Cottage homework
jakubvrana authored
275 function redirect($location, $message = null) {
276 if (isset($message)) {
be49e08 Improve session restarting
jakubvrana authored
277 restart_session();
ff74bf0 Allow multiple messages
jakubvrana authored
278 $_SESSION["messages"][] = $message;
430034c Cottage homework
jakubvrana authored
279 }
9bb1909 Fix Save and continue edit
jakubvrana authored
280 if (isset($location)) {
281 header("Location: " . ($location != "" ? $location : "."));
282 exit;
283 }
430034c Cottage homework
jakubvrana authored
284 }
285
9a176b0 Comments
jakubvrana authored
286 /** Execute query and redirect if successful
287 * @param string
288 * @param string
289 * @param string
290 * @param bool
291 * @param bool
292 * @param bool
293 * @return bool
294 */
748ee83 Print SQL command with multiple queries
jakubvrana authored
295 function query_redirect($query, $location, $message, $redirect = true, $execute = true, $failed = false) {
64d616c Rename get_dbh to connection
jakubvrana authored
296 global $connection, $error, $adminer;
f9a2023 Improve concurrency
jakubvrana authored
297 if ($execute) {
298 $failed = !$connection->query($query);
299 }
10bdbed Save queries to history
jakubvrana authored
300 $sql = "";
301 if ($query) {
c64c4fd Adminer class
jakubvrana authored
302 $sql = $adminer->messageQuery($query);
10bdbed Save queries to history
jakubvrana authored
303 }
748ee83 Print SQL command with multiple queries
jakubvrana authored
304 if ($failed) {
c1130ed Simplify SQL syntax errors everywhere
jakubvrana authored
305 $error = error() . $sql;
c48a017 Print SQL query by error
jakubvrana authored
306 return false;
f91b2c4 Print SQL query by message
jakubvrana authored
307 }
c48a017 Print SQL query by error
jakubvrana authored
308 if ($redirect) {
309 redirect($location, $message . $sql);
310 }
311 return true;
f91b2c4 Print SQL query by message
jakubvrana authored
312 }
313
9a176b0 Comments
jakubvrana authored
314 /** Execute and remember query
315 * @param string null to return remembered queries
316 * @return Min_Result
317 */
748ee83 Print SQL command with multiple queries
jakubvrana authored
318 function queries($query = null) {
64d616c Rename get_dbh to connection
jakubvrana authored
319 global $connection;
748ee83 Print SQL command with multiple queries
jakubvrana authored
320 static $queries = array();
321 if (!isset($query)) {
64ba924 Comments
jakubvrana authored
322 // return executed queries without parameter
36950eb Tables and views
jakubvrana authored
323 return implode(";\n", $queries);
748ee83 Print SQL command with multiple queries
jakubvrana authored
324 }
325 $queries[] = $query;
64d616c Rename get_dbh to connection
jakubvrana authored
326 return $connection->query($query);
748ee83 Print SQL command with multiple queries
jakubvrana authored
327 }
328
dbdd40a Introduce queries_redirect function
jakubvrana authored
329 /** Redirect by remembered queries
330 * @param string
331 * @param string
332 * @param bool
333 */
334 function queries_redirect($location, $message, $redirect) {
335 return query_redirect(queries(), $location, $message, $redirect, false, !$redirect);
336 }
337
9a176b0 Comments
jakubvrana authored
338 /** Remove parameter from query string
339 * @param string
340 * @return string
341 */
d41d446 remove_from_uri()
jakubvrana authored
342 function remove_from_uri($param = "") {
483d7dc Remove session_name() only without SID (bug #2910681)
jakubvrana authored
343 return substr(preg_replace("~(?<=[?&])($param" . (SID ? "" : "|" . session_name()) . ")=[^&]*&~", '', "$_SERVER[REQUEST_URI]&"), 0, -1);
d41d446 remove_from_uri()
jakubvrana authored
344 }
345
9a176b0 Comments
jakubvrana authored
346 /** Generate page number for pagination
347 * @param int
348 * @return string
349 */
c565c54 Let pagination function return
jakubvrana authored
350 function pagination($page) {
689699a Shortcut for htmlspecialchars
jakubvrana authored
351 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
352 }
353
3e9b1ba Don't store files in hidden fields
jakubvrana authored
354 /** Get file contents from $_FILES
9a176b0 Comments
jakubvrana authored
355 * @param string
356 * @param bool
3e9b1ba Don't store files in hidden fields
jakubvrana authored
357 * @return mixed int for error, string otherwise
9a176b0 Comments
jakubvrana authored
358 */
25f01d3 Compress export and import
jakubvrana authored
359 function get_file($key, $decompress = false) {
360 $file = $_FILES[$key];
361 if (!$file || $file["error"]) {
362 return $file["error"];
430034c Cottage homework
jakubvrana authored
363 }
0a2d81d Bzip2 import
jakubvrana authored
364 return file_get_contents($decompress && ereg('\\.gz$', $file["name"]) ? "compress.zlib://$file[tmp_name]"
365 : ($decompress && ereg('\\.bz2$', $file["name"]) ? "compress.bzip2://$file[tmp_name]"
366 : $file["tmp_name"]
367 )); //! may not be reachable because of open_basedir
430034c Cottage homework
jakubvrana authored
368 }
369
9a176b0 Comments
jakubvrana authored
370 /** Determine upload error
371 * @param int
372 * @return string
373 */
e2a03c0 Show upload_max_filesize
jakubvrana authored
374 function upload_error($error) {
375 $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
376 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
377 }
378
9a176b0 Comments
jakubvrana authored
379 /** Generate class for odd rows
380 * @param string return this for odd rows, empty to reset counter
381 * @return string
382 */
383 function odd($return = ' class="odd"') {
4e5b126 Highlight odd and hover rows
jakubvrana authored
384 static $i = 0;
9a176b0 Comments
jakubvrana authored
385 if (!$return) { // reset counter
1b97f48 Reset odd() for each result
jakubvrana authored
386 $i = -1;
4e5b126 Highlight odd and hover rows
jakubvrana authored
387 }
9a176b0 Comments
jakubvrana authored
388 return ($i++ % 2 ? $return : '');
4e5b126 Highlight odd and hover rows
jakubvrana authored
389 }
390
9a176b0 Comments
jakubvrana authored
391 /** Check whether the string is in UTF-8
392 * @param string
393 * @return bool
394 */
017ffb5 Better UTF-8 detection
jakubvrana authored
395 function is_utf8($val) {
64ba924 Comments
jakubvrana authored
396 // don't print control chars except \t\r\n
017ffb5 Better UTF-8 detection
jakubvrana authored
397 return (preg_match('~~u', $val) && !preg_match('~[\\0-\\x8\\xB\\xC\\xE-\\x1F]~', $val));
398 }
399
9a176b0 Comments
jakubvrana authored
400 /** Shorten UTF-8 string
401 * @param string
402 * @param int
403 * @param string
404 * @return string escaped string with appended ...
405 */
cd07584 Save SQL query to history
jakubvrana authored
406 function shorten_utf8($string, $length = 80, $suffix = "") {
11982f1 Shorten printed SQL query
jakubvrana authored
407 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
408 preg_match("(^([\t\r\n -~]{0,$length})($)?)", $string, $match);
95764b0 Shorten binary strings
jakubvrana authored
409 }
11982f1 Shorten printed SQL query
jakubvrana authored
410 return h($match[1]) . $suffix . (isset($match[2]) ? "" : "<em>...</em>");
02a851c Shorten texts in PHP
jakubvrana authored
411 }
3920ebb Remove InnoDB table comment in Tables and views
jakubvrana authored
412
9a176b0 Comments
jakubvrana authored
413 /** Generate friendly URL
414 * @param string
415 * @return string
416 */
8251896 Downloaded file name
jakubvrana authored
417 function friendly_url($val) {
64ba924 Comments
jakubvrana authored
418 // used for blobs and export
8251896 Downloaded file name
jakubvrana authored
419 return preg_replace('~[^a-z0-9_]~i', '-', $val);
420 }
421
9a176b0 Comments
jakubvrana authored
422 /** Print hidden fields
423 * @param array
424 * @param array
425 * @return null
426 */
820c882 Bulk clone
jakubvrana authored
427 function hidden_fields($process, $ignore = array()) {
5d7071a Choose language through option-list
jakubvrana authored
428 while (list($key, $val) = each($process)) {
429 if (is_array($val)) {
430 foreach ($val as $k => $v) {
431 $process[$key . "[$k]"] = $v;
432 }
433 } elseif (!in_array($key, $ignore)) {
689699a Shortcut for htmlspecialchars
jakubvrana authored
434 echo '<input type="hidden" name="' . h($key) . '" value="' . h($val) . '">';
5d7071a Choose language through option-list
jakubvrana authored
435 }
436 }
437 }
61640ee Move common functions
jakubvrana authored
438
3f5b683 Reintegrate sqlite branch
jakubvrana authored
439 /** Print hidden fields for GET forms
440 * @return null
441 */
442 function hidden_fields_get() {
443 echo (SID && !$_COOKIE ? '<input type="hidden" name="' . session_name() . '" value="' . h(session_id()) . '">' : '');
444 echo (SERVER !== null ? '<input type="hidden" name="' . DRIVER . '" value="' . h(SERVER) . '">' : "");
445 echo '<input type="hidden" name="username" value="' . h($_GET["username"]) . '">';
446 }
447
9a176b0 Comments
jakubvrana authored
448 /** Find out foreign keys for each column
449 * @param string
450 * @return array array($col => array())
451 */
1e4d11e Select from foreign keys in Editor
jakubvrana authored
452 function column_foreign_keys($table) {
453 $return = array();
454 foreach (foreign_keys($table) as $foreign_key) {
455 foreach ($foreign_key["source"] as $val) {
456 $return[$val][] = $foreign_key;
457 }
458 }
459 return $return;
460 }
461
3f5b683 Reintegrate sqlite branch
jakubvrana authored
462 /** Print enum input field
463 * @param string "radio"|"checkbox"
464 * @param string
465 * @param array
466 * @param mixed int|string|array
467 * @return null
468 */
469 function enum_input($type, $name, $field, $value) {
470 preg_match_all("~'((?:[^']|'')*)'~", $field["length"], $matches);
471 foreach ($matches[1] as $i => $val) {
472 $val = stripcslashes(str_replace("''", "'", $val));
473 $checked = (is_int($value) ? $value == $i+1 : (is_array($value) ? in_array($i+1, $value) : $value === $val));
474 echo " <label><input type='$type' name='$name' value='" . ($i+1) . "'" . ($checked ? ' checked' : '') . '>' . h($val) . '</label>';
475 }
476 }
477
9a176b0 Comments
jakubvrana authored
478 /** Print edit input field
479 * @param array one field from fields()
480 * @param mixed
481 * @param string
482 * @return null
483 */
1e4d11e Select from foreign keys in Editor
jakubvrana authored
484 function input($field, $value, $function) {
3f5b683 Reintegrate sqlite branch
jakubvrana authored
485 global $types, $adminer, $driver;
689699a Shortcut for htmlspecialchars
jakubvrana authored
486 $name = h(bracket_escape($field["field"]));
61640ee Move common functions
jakubvrana authored
487 echo "<td class='function'>";
6758477 Hide NULL in Editor enum
jakubvrana authored
488 $functions = (isset($_GET["select"]) ? array("orig" => lang('original')) : array()) + $adminer->editFunctions($field);
61640ee Move common functions
jakubvrana authored
489 if ($field["type"] == "enum") {
ec0282b Enum editing
jakubvrana authored
490 echo nbsp($functions[""]) . "<td>" . ($functions["orig"] ? "<label><input type='radio' name='fields[$name]' value='-1' checked><em>$functions[orig]</em></label> " : "");
491 echo $adminer->editInput($_GET["edit"], $field, " name='fields[$name]'", $value);
3f5b683 Reintegrate sqlite branch
jakubvrana authored
492 enum_input("radio", "fields[$name]", $field, $value);
61640ee Move common functions
jakubvrana authored
493 } else {
9903946 Use original values in Editor multi edit
jakubvrana authored
494 $first = 0;
495 foreach ($functions as $key => $val) {
496 if ($key === "" || !$val) {
497 break;
498 }
499 $first++;
500 }
689699a Shortcut for htmlspecialchars
jakubvrana authored
501 $onchange = ($first ? " onchange=\"var f = this.form['function[" . addcslashes($name, "\r\n'\\") . "]']; if ($first > f.selectedIndex) f.selectedIndex = $first;\"" : "");
a561252 Use distinct name for blob inputs
jakubvrana authored
502 $attrs = " name='fields[$name]'$onchange";
0698409 Utilize html_select
jakubvrana authored
503 echo (count($functions) > 1 ? html_select("function[$name]", $functions, !isset($function) || in_array($function, $functions) ? $function : "") : nbsp(reset($functions))) . '<td>';
a561252 Use distinct name for blob inputs
jakubvrana authored
504 $input = $adminer->editInput($_GET["edit"], $field, $attrs, $value); // usage in call is without a table
7352c28 Replace strlen() by != ""
jakubvrana authored
505 if ($input != "") {
e1abcda Treat tinyint(1) as boolean
jakubvrana authored
506 echo $input;
1e4d11e Select from foreign keys in Editor
jakubvrana authored
507 } elseif ($field["type"] == "set") { //! 64 bits
26c9d64 Fix long SQL query crash (bug #2839231)
jakubvrana authored
508 preg_match_all("~'((?:[^']|'')*)'~", $field["length"], $matches);
61640ee Move common functions
jakubvrana authored
509 foreach ($matches[1] as $i => $val) {
510 $val = stripcslashes(str_replace("''", "'", $val));
511 $checked = (is_int($value) ? ($value >> $i) & 1 : in_array($val, explode(",", $value), true));
4cead56 Edit default values directly in table creation
jakubvrana authored
512 echo " <label><input type='checkbox' name='fields[$name][$i]' value='" . (1 << $i) . "'" . ($checked ? ' checked' : '') . "$onchange>" . h($val) . '</label>';
61640ee Move common functions
jakubvrana authored
513 }
3f5b683 Reintegrate sqlite branch
jakubvrana authored
514 } elseif (ereg('binary|blob|bytea', $field["type"]) && ini_bool("file_uploads")) {
a561252 Use distinct name for blob inputs
jakubvrana authored
515 echo "<input type='file' name='fields-$name'$onchange>";
2204cef Use textarea for blob with disabled file uploads
jakubvrana authored
516 } elseif (ereg('text|blob', $field["type"])) {
3f5b683 Reintegrate sqlite branch
jakubvrana authored
517 echo "<textarea cols='50' rows='" . ($driver != "sqlite" || ereg("\n", $value) ? 12 : 1) . "'$attrs>" . h($value) . '</textarea>';
61640ee Move common functions
jakubvrana authored
518 } else {
519 // int(3) is only a display hint
520 $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));
a561252 Use distinct name for blob inputs
jakubvrana authored
521 echo "<input value='" . h($value) . "'" . ($maxlength ? " maxlength='$maxlength'" : "") . (ereg('char', $field["type"]) && $field["length"] > 20 ? " size='40'" : "") . "$attrs>";
61640ee Move common functions
jakubvrana authored
522 }
523 }
524 }
525
9a176b0 Comments
jakubvrana authored
526 /** Process edit input field
527 * @param one field from fields()
528 * @return string
529 */
a0def47 Date localization
jakubvrana authored
530 function process_input($field) {
64d616c Rename get_dbh to connection
jakubvrana authored
531 global $connection, $adminer;
a0def47 Date localization
jakubvrana authored
532 $idf = bracket_escape($field["field"]);
61640ee Move common functions
jakubvrana authored
533 $function = $_POST["function"][$idf];
534 $value = $_POST["fields"][$idf];
3f5b683 Reintegrate sqlite branch
jakubvrana authored
535 if ($field["type"] == "enum") {
536 if ($value == -1) {
537 return false;
538 }
539 if ($value == "") {
540 return "NULL";
541 }
542 return intval($value);
543 }
544 if ($field["auto_increment"] && $value == "") {
545 return null;
546 }
547 if ($function == "orig") {
61640ee Move common functions
jakubvrana authored
548 return false;
3f5b683 Reintegrate sqlite branch
jakubvrana authored
549 }
550 if ($function == "NULL") {
61640ee Move common functions
jakubvrana authored
551 return "NULL";
3f5b683 Reintegrate sqlite branch
jakubvrana authored
552 }
553 if ($field["type"] == "set") {
4cead56 Edit default values directly in table creation
jakubvrana authored
554 return array_sum((array) $value);
3f5b683 Reintegrate sqlite branch
jakubvrana authored
555 }
556 if (ereg('binary|blob|bytea', $field["type"]) && ini_bool("file_uploads")) {
a561252 Use distinct name for blob inputs
jakubvrana authored
557 $file = get_file("fields-$idf");
61640ee Move common functions
jakubvrana authored
558 if (!is_string($file)) {
559 return false; //! report errors
560 }
91cb95a Remove unnecessary _binary
jakubvrana authored
561 return $connection->quote($file);
61640ee Move common functions
jakubvrana authored
562 }
3f5b683 Reintegrate sqlite branch
jakubvrana authored
563 return $adminer->processInput($field, $value, $function);
61640ee Move common functions
jakubvrana authored
564 }
565
db07325 Search in all tables
jakubvrana authored
566 /** Print results of search in all tables
567 * @uses $_GET["where"][0]
568 * @uses $_POST["tables"]
569 * @return null
570 */
571 function search_tables() {
572 global $adminer, $connection;
573 $found = false;
574 foreach (table_status() as $table => $table_status) {
575 $name = $adminer->tableName($table_status);
576 if (isset($table_status["Engine"]) && $name != "" && (!$_POST["tables"] || in_array($table, $_POST["tables"]))) {
3f5b683 Reintegrate sqlite branch
jakubvrana authored
577 $result = $connection->query("SELECT" . limit("1 FROM " . idf_escape($table) . " WHERE " . implode(" AND ", $adminer->selectSearchProcess(fields($table), array())), 1));
db07325 Search in all tables
jakubvrana authored
578 if ($result->num_rows) {
579 if (!$found) {
580 echo "<ul>\n";
581 $found = true;
582 }
583 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";
584 }
585 }
586 }
587 echo ($found ? "</ul>" : "<p class='message'>" . lang('No tables.')) . "\n";
588 }
589
9a176b0 Comments
jakubvrana authored
590 /** Print CSV row
591 * @param array
592 * @return null
593 */
61640ee Move common functions
jakubvrana authored
594 function dump_csv($row) {
595 foreach ($row as $key => $val) {
3f5b683 Reintegrate sqlite branch
jakubvrana authored
596 if (preg_match("~[\"\n,;]~", $val) || $val === "") {
61640ee Move common functions
jakubvrana authored
597 $row[$key] = '"' . str_replace('"', '""', $val) . '"';
598 }
599 }
3f5b683 Reintegrate sqlite branch
jakubvrana authored
600 echo implode(($_POST["format"] == "csv;" ? ";" : ","), $row) . "\n";
61640ee Move common functions
jakubvrana authored
601 }
c16c57b E-mail sending
jakubvrana authored
602
9a176b0 Comments
jakubvrana authored
603 /** Apply SQL function
604 * @param string
605 * @param string escaped column identifier
606 * @return string
607 */
e331067 Move apply_sql_function
jakubvrana authored
608 function apply_sql_function($function, $column) {
3f5b683 Reintegrate sqlite branch
jakubvrana authored
609 return ($function ? ($function == "unixepoch" ? "DATETIME($column, '$function')" : ($function == "count distinct" ? "COUNT(DISTINCT " : strtoupper("$function(")) . "$column)") : $column);
e331067 Move apply_sql_function
jakubvrana authored
610 }
611
9a176b0 Comments
jakubvrana authored
612 /** Check whether the string is e-mail address
613 * @param string
614 * @return bool
615 */
c16c57b E-mail sending
jakubvrana authored
616 function is_email($email) {
617 $atom = '[-a-z0-9!#$%&\'*+/=?^_`{|}~]'; // characters of local-name
618 $domain = '[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])'; // one domain component
28e1dd2 Allow multiple e-mails
jakubvrana authored
619 $pattern = "$atom+(\\.$atom+)*@($domain?\\.)+$domain";
620 return preg_match("(^$pattern(,\\s*$pattern)*\$)i", $email);
c16c57b E-mail sending
jakubvrana authored
621 }
dc667ea Hide select export and import
jakubvrana authored
622
bc78866 Link URLs in select
jakubvrana authored
623 /** Check whether the string is URL address
624 * @param string
625 * @return bool
626 */
627 function is_url($string) {
628 $domain = '[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])'; // one domain component
629 return preg_match("~^https?://($domain?\\.)+$domain(:[0-9]+)?(/.*)?(\\?.*)?(#.*)?\$~i", $string); //! restrict path, query and fragment characters
630 }
631
dc667ea Hide select export and import
jakubvrana authored
632 /** Print header for hidden fieldset (close by </div></fieldset>)
633 * @param string
634 * @param string
635 * @param bool
636 * @return null
637 */
638 function print_fieldset($id, $legend, $visible = false) {
639 echo "<fieldset><legend><a href='#fieldset-$id' onclick=\"return !toggle('fieldset-$id');\">$legend</a></legend><div id='fieldset-$id'" . ($visible ? "" : " class='hidden'") . ">\n";
640 }
94a2be5 Highlight current links
jakubvrana authored
641
642 /** Enclose $string to <b> if $bold is true
643 * @param string
644 * @param bool
645 * @return string
646 */
647 function bold($string, $bold) {
648 return ($bold ? "<b>$string</b>" : $string);
649 }
Something went wrong with that request. Please try again.