Skip to content

HTTPS clone URL

Subversion checkout URL

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