Skip to content
Newer
Older
100644 531 lines (494 sloc) 16.1 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) {
60 return (strlen(trim($string)) ? h($string) : "&nbsp;");
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'>";
75 return (strlen($label) ? "<label for='checkbox-$id'>$return" . h($label) . "</label>" : $return);
6b30cfa Separate checkbox
jakubvrana authored
76 }
77
e40612a Use radio in export
jakubvrana authored
78 /** Generate HTML radio list
79 * @param string
80 * @param array
81 * @param string
0698409 Utilize html_select
jakubvrana authored
82 * @param string true for no onchange, false for radio
e40612a Use radio in export
jakubvrana authored
83 * @return string
84 */
0698409 Utilize html_select
jakubvrana authored
85 function html_select($name, $options, $value = "", $onchange = true) {
86 if ($onchange) {
87 return "<select name='" . h($name) . "'" . (is_string($onchange) ? " onchange=\"$onchange\"" : "") . ">" . optionlist($options, $value) . "</select>";
e40612a Use radio in export
jakubvrana authored
88 }
89 $return = "";
90 foreach ($options as $key => $val) {
91 $return .= "<label><input type='radio' name='" . h($name) . "' value='" . h($key) . "'" . ($key == $value ? " checked" : "") . ">" . h($val) . "</label>";
92 }
93 return $return;
94 }
95
9a176b0 Comments
jakubvrana authored
96 /** Generate list of HTML options
97 * @param array array of strings or arrays (creates optgroup)
98 * @param mixed
99 * @param bool always use array keys for value="", otherwise only string keys are used
100 * @return string
101 */
1e4d11e Select from foreign keys in Editor
jakubvrana authored
102 function optionlist($options, $selected = null, $use_keys = false) {
667bfec Decomposition
jakubvrana authored
103 $return = "";
104 foreach ($options as $k => $v) {
105 if (is_array($v)) {
689699a Shortcut for htmlspecialchars
jakubvrana authored
106 $return .= '<optgroup label="' . h($k) . '">';
667bfec Decomposition
jakubvrana authored
107 }
ced9de9 Create single column foreign key in table structure
jakubvrana authored
108 foreach ((is_array($v) ? $v : array($k => $v)) as $key => $val) {
cb6d36c HTML instead of XHTML
jakubvrana authored
109 $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
110 }
111 if (is_array($v)) {
112 $return .= '</optgroup>';
113 }
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()
138 * @return string query string
139 */
667bfec Decomposition
jakubvrana authored
140 function unique_idf($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 }
430034c Cottage homework
jakubvrana authored
148 $return[] = urlencode("where[" . bracket_escape($key) . "]") . "=" . urlencode($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
988a29f Don't use aggregation functions in unique_idf
jakubvrana authored
156 $return[] = (isset($val) ? urlencode("where[" . bracket_escape($key) . "]") . "=" . urlencode($val) : "null%5B%5D=" . urlencode($key));
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();
116faf2 Mass delete
jakubvrana authored
168 foreach ((array) $where["where"] as $key => $val) {
581bd35 Respect function results in where()
jakubvrana authored
169 $key = bracket_escape($key, "back");
5e01a62 Prepare for SQLite
jakubvrana authored
170 $return[] = (preg_match('~^[A-Z0-9_]+\\(`(?:[^`]|``)+`\\)$~', $key) ? $key : idf_escape($key)) . " = " . exact_value($val); //! enum and set, columns looking like functions
430034c Cottage homework
jakubvrana authored
171 }
116faf2 Mass delete
jakubvrana authored
172 foreach ((array) $where["null"] as $key) {
581bd35 Respect function results in where()
jakubvrana authored
173 $key = bracket_escape($key, "back");
26c9d64 Fix long SQL query crash (bug #2839231)
jakubvrana authored
174 $return[] = (preg_match('~^[A-Z0-9_]+\\(`(?:[^`]|``)+`\\)$~', $key) ? $key : idf_escape($key)) . " IS NULL";
667bfec Decomposition
jakubvrana authored
175 }
c16c57b E-mail sending
jakubvrana authored
176 return implode(" AND ", $return);
667bfec Decomposition
jakubvrana authored
177 }
178
9a176b0 Comments
jakubvrana authored
179 /** Create SQL condition from query string
180 * @param string
181 * @return string
182 */
0cd2afb Remove single item clone
jakubvrana authored
183 function where_check($val) {
184 parse_str($val, $check);
185 return where($check);
186 }
187
9a176b0 Comments
jakubvrana authored
188 /** Create query string where condition from value
189 * @param int condition order
190 * @param string column identifier
191 * @param string
192 * @return string
193 */
c42c46a Table relations
jakubvrana authored
194 function where_link($i, $column, $value) {
db46fc2 Maintain insert prefill value
jakubvrana authored
195 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
196 }
197
319cba4 Save e-mail from to cookie
jakubvrana authored
198 /** Set cookie valid for 1 month
199 * @param string
200 * @param string
201 * @return bool
202 */
203 function cookie($name, $value) {
3675fe6 Simplify cookie expiration
jakubvrana authored
204 return setcookie($name, $value, time() + 2592000, preg_replace('~\\?.*~', '', $_SERVER["REQUEST_URI"])); // 2592000 = 30 * 24 * 60 * 60
319cba4 Save e-mail from to cookie
jakubvrana authored
205 }
206
9a176b0 Comments
jakubvrana authored
207 /** Send Location header and exit
208 * @param string
209 * @param string
210 * @return null
211 */
430034c Cottage homework
jakubvrana authored
212 function redirect($location, $message = null) {
213 if (isset($message)) {
ff74bf0 Allow multiple messages
jakubvrana authored
214 $_SESSION["messages"][] = $message;
430034c Cottage homework
jakubvrana authored
215 }
216 header("Location: " . (strlen($location) ? $location : "."));
217 exit;
218 }
219
9a176b0 Comments
jakubvrana authored
220 /** Execute query and redirect if successful
221 * @param string
222 * @param string
223 * @param string
224 * @param bool
225 * @param bool
226 * @param bool
227 * @return bool
228 */
748ee83 Print SQL command with multiple queries
jakubvrana authored
229 function query_redirect($query, $location, $message, $redirect = true, $execute = true, $failed = false) {
64d616c Rename get_dbh to connection
jakubvrana authored
230 global $connection, $error, $adminer;
10bdbed Save queries to history
jakubvrana authored
231 $sql = "";
232 if ($query) {
c64c4fd Adminer class
jakubvrana authored
233 $sql = $adminer->messageQuery($query);
10bdbed Save queries to history
jakubvrana authored
234 }
748ee83 Print SQL command with multiple queries
jakubvrana authored
235 if ($execute) {
64d616c Rename get_dbh to connection
jakubvrana authored
236 $failed = !$connection->query($query);
748ee83 Print SQL command with multiple queries
jakubvrana authored
237 }
238 if ($failed) {
64d616c Rename get_dbh to connection
jakubvrana authored
239 $error = h($connection->error) . $sql;
c48a017 Print SQL query by error
jakubvrana authored
240 return false;
f91b2c4 Print SQL query by message
jakubvrana authored
241 }
c48a017 Print SQL query by error
jakubvrana authored
242 if ($redirect) {
243 redirect($location, $message . $sql);
244 }
245 return true;
f91b2c4 Print SQL query by message
jakubvrana authored
246 }
247
9a176b0 Comments
jakubvrana authored
248 /** Execute and remember query
249 * @param string null to return remembered queries
250 * @return Min_Result
251 */
748ee83 Print SQL command with multiple queries
jakubvrana authored
252 function queries($query = null) {
64d616c Rename get_dbh to connection
jakubvrana authored
253 global $connection;
748ee83 Print SQL command with multiple queries
jakubvrana authored
254 static $queries = array();
255 if (!isset($query)) {
64ba924 Comments
jakubvrana authored
256 // return executed queries without parameter
36950eb Tables and views
jakubvrana authored
257 return implode(";\n", $queries);
748ee83 Print SQL command with multiple queries
jakubvrana authored
258 }
259 $queries[] = $query;
64d616c Rename get_dbh to connection
jakubvrana authored
260 return $connection->query($query);
748ee83 Print SQL command with multiple queries
jakubvrana authored
261 }
262
dbdd40a Introduce queries_redirect function
jakubvrana authored
263 /** Redirect by remembered queries
264 * @param string
265 * @param string
266 * @param bool
267 */
268 function queries_redirect($location, $message, $redirect) {
269 return query_redirect(queries(), $location, $message, $redirect, false, !$redirect);
270 }
271
9a176b0 Comments
jakubvrana authored
272 /** Remove parameter from query string
273 * @param string
274 * @return string
275 */
d41d446 remove_from_uri()
jakubvrana authored
276 function remove_from_uri($param = "") {
6272cae Unconditionally remove session_name() in remove_from_uri()
jakubvrana authored
277 $param = "($param|" . session_name() . ")";
5f83619 Session management
jakubvrana authored
278 return substr(preg_replace("~([?&])$param=[^&]*&~", '\\1', "$_SERVER[REQUEST_URI]&"), 0, -1);
d41d446 remove_from_uri()
jakubvrana authored
279 }
280
9a176b0 Comments
jakubvrana authored
281 /** Generate page number for pagination
282 * @param int
283 * @return string
284 */
c565c54 Let pagination function return
jakubvrana authored
285 function pagination($page) {
689699a Shortcut for htmlspecialchars
jakubvrana authored
286 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
287 }
288
9a176b0 Comments
jakubvrana authored
289 /** Get file contents from $_FILES or $_POST["files"]
290 * @param string
291 * @param bool
292 * @return string
293 */
25f01d3 Compress export and import
jakubvrana authored
294 function get_file($key, $decompress = false) {
64ba924 Comments
jakubvrana authored
295 // returns int for error, string otherwise
25f01d3 Compress export and import
jakubvrana authored
296 $file = $_POST["files"][$key];
297 if (isset($file)) {
64ba924 Comments
jakubvrana authored
298 // get the file from hidden field if the user was logged out
25f01d3 Compress export and import
jakubvrana authored
299 $length = strlen($file);
300 if ($length && $length < 4) {
301 return intval($file);
302 }
303 return base64_decode($file);
304 }
305 $file = $_FILES[$key];
306 if (!$file || $file["error"]) {
307 return $file["error"];
430034c Cottage homework
jakubvrana authored
308 }
0a2d81d Bzip2 import
jakubvrana authored
309 return file_get_contents($decompress && ereg('\\.gz$', $file["name"]) ? "compress.zlib://$file[tmp_name]"
310 : ($decompress && ereg('\\.bz2$', $file["name"]) ? "compress.bzip2://$file[tmp_name]"
311 : $file["tmp_name"]
312 )); //! may not be reachable because of open_basedir
430034c Cottage homework
jakubvrana authored
313 }
314
9a176b0 Comments
jakubvrana authored
315 /** Determine upload error
316 * @param int
317 * @return string
318 */
e2a03c0 Show upload_max_filesize
jakubvrana authored
319 function upload_error($error) {
320 $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
321 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
322 }
323
9a176b0 Comments
jakubvrana authored
324 /** Generate class for odd rows
325 * @param string return this for odd rows, empty to reset counter
326 * @return string
327 */
328 function odd($return = ' class="odd"') {
4e5b126 Highlight odd and hover rows
jakubvrana authored
329 static $i = 0;
9a176b0 Comments
jakubvrana authored
330 if (!$return) { // reset counter
1b97f48 Reset odd() for each result
jakubvrana authored
331 $i = -1;
4e5b126 Highlight odd and hover rows
jakubvrana authored
332 }
9a176b0 Comments
jakubvrana authored
333 return ($i++ % 2 ? $return : '');
4e5b126 Highlight odd and hover rows
jakubvrana authored
334 }
335
9a176b0 Comments
jakubvrana authored
336 /** Check whether the string is in UTF-8
337 * @param string
338 * @return bool
339 */
017ffb5 Better UTF-8 detection
jakubvrana authored
340 function is_utf8($val) {
64ba924 Comments
jakubvrana authored
341 // don't print control chars except \t\r\n
017ffb5 Better UTF-8 detection
jakubvrana authored
342 return (preg_match('~~u', $val) && !preg_match('~[\\0-\\x8\\xB\\xC\\xE-\\x1F]~', $val));
343 }
344
9a176b0 Comments
jakubvrana authored
345 /** Shorten UTF-8 string
346 * @param string
347 * @param int
348 * @param string
349 * @return string escaped string with appended ...
350 */
cd07584 Save SQL query to history
jakubvrana authored
351 function shorten_utf8($string, $length = 80, $suffix = "") {
11982f1 Shorten printed SQL query
jakubvrana authored
352 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
353 preg_match("(^([\t\r\n -~]{0,$length})($)?)", $string, $match);
95764b0 Shorten binary strings
jakubvrana authored
354 }
11982f1 Shorten printed SQL query
jakubvrana authored
355 return h($match[1]) . $suffix . (isset($match[2]) ? "" : "<em>...</em>");
02a851c Shorten texts in PHP
jakubvrana authored
356 }
3920ebb Remove InnoDB table comment in Tables and views
jakubvrana authored
357
9a176b0 Comments
jakubvrana authored
358 /** Generate friendly URL
359 * @param string
360 * @return string
361 */
8251896 Downloaded file name
jakubvrana authored
362 function friendly_url($val) {
64ba924 Comments
jakubvrana authored
363 // used for blobs and export
8251896 Downloaded file name
jakubvrana authored
364 return preg_replace('~[^a-z0-9_]~i', '-', $val);
365 }
366
9a176b0 Comments
jakubvrana authored
367 /** Print hidden fields
368 * @param array
369 * @param array
370 * @return null
371 */
820c882 Bulk clone
jakubvrana authored
372 function hidden_fields($process, $ignore = array()) {
5d7071a Choose language through option-list
jakubvrana authored
373 while (list($key, $val) = each($process)) {
374 if (is_array($val)) {
375 foreach ($val as $k => $v) {
376 $process[$key . "[$k]"] = $v;
377 }
378 } elseif (!in_array($key, $ignore)) {
689699a Shortcut for htmlspecialchars
jakubvrana authored
379 echo '<input type="hidden" name="' . h($key) . '" value="' . h($val) . '">';
5d7071a Choose language through option-list
jakubvrana authored
380 }
381 }
382 }
61640ee Move common functions
jakubvrana authored
383
9a176b0 Comments
jakubvrana authored
384 /** Find out foreign keys for each column
385 * @param string
386 * @return array array($col => array())
387 */
1e4d11e Select from foreign keys in Editor
jakubvrana authored
388 function column_foreign_keys($table) {
389 $return = array();
390 foreach (foreign_keys($table) as $foreign_key) {
391 foreach ($foreign_key["source"] as $val) {
392 $return[$val][] = $foreign_key;
393 }
394 }
395 return $return;
396 }
397
9a176b0 Comments
jakubvrana authored
398 /** Print edit input field
399 * @param array one field from fields()
400 * @param mixed
401 * @param string
402 * @return null
403 */
1e4d11e Select from foreign keys in Editor
jakubvrana authored
404 function input($field, $value, $function) {
c64c4fd Adminer class
jakubvrana authored
405 global $types, $adminer;
689699a Shortcut for htmlspecialchars
jakubvrana authored
406 $name = h(bracket_escape($field["field"]));
61640ee Move common functions
jakubvrana authored
407 echo "<td class='function'>";
6758477 Hide NULL in Editor enum
jakubvrana authored
408 $functions = (isset($_GET["select"]) ? array("orig" => lang('original')) : array()) + $adminer->editFunctions($field);
61640ee Move common functions
jakubvrana authored
409 if ($field["type"] == "enum") {
6758477 Hide NULL in Editor enum
jakubvrana authored
410 echo "&nbsp;<td>" . ($functions["orig"] ? "<label><input type='radio' name='fields[$name]' value='-1' checked><em>$functions[orig]</em></label> " : "");
411 if (in_array("NULL", $functions)) {
412 echo "<label><input type='radio' name='fields[$name]' value=''" . (isset($value) || $functions["orig"] ? '' : ' checked') . "><em>NULL</em></label> ";
61640ee Move common functions
jakubvrana authored
413 }
4cead56 Edit default values directly in table creation
jakubvrana authored
414 echo "<input type='radio' name='fields[$name]' value='0'" . ($value === 0 ? ' checked' : '') . '>';
26c9d64 Fix long SQL query crash (bug #2839231)
jakubvrana authored
415 preg_match_all("~'((?:[^']|'')*)'~", $field["length"], $matches);
61640ee Move common functions
jakubvrana authored
416 foreach ($matches[1] as $i => $val) {
417 $val = stripcslashes(str_replace("''", "'", $val));
418 $checked = (is_int($value) ? $value == $i+1 : $value === $val);
4cead56 Edit default values directly in table creation
jakubvrana authored
419 echo " <label><input type='radio' name='fields[$name]' value='" . ($i+1) . "'" . ($checked ? ' checked' : '') . '>' . h($val) . '</label>';
61640ee Move common functions
jakubvrana authored
420 }
421 } else {
9903946 Use original values in Editor multi edit
jakubvrana authored
422 $first = 0;
423 foreach ($functions as $key => $val) {
424 if ($key === "" || !$val) {
425 break;
426 }
427 $first++;
428 }
689699a Shortcut for htmlspecialchars
jakubvrana authored
429 $onchange = ($first ? " onchange=\"var f = this.form['function[" . addcslashes($name, "\r\n'\\") . "]']; if ($first > f.selectedIndex) f.selectedIndex = $first;\"" : "");
0698409 Utilize html_select
jakubvrana authored
430 echo (count($functions) > 1 ? html_select("function[$name]", $functions, !isset($function) || in_array($function, $functions) ? $function : "") : nbsp(reset($functions))) . '<td>';
689699a Shortcut for htmlspecialchars
jakubvrana authored
431 $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
432 if (strlen($input)) {
433 echo $input;
1e4d11e Select from foreign keys in Editor
jakubvrana authored
434 } elseif ($field["type"] == "set") { //! 64 bits
26c9d64 Fix long SQL query crash (bug #2839231)
jakubvrana authored
435 preg_match_all("~'((?:[^']|'')*)'~", $field["length"], $matches);
61640ee Move common functions
jakubvrana authored
436 foreach ($matches[1] as $i => $val) {
437 $val = stripcslashes(str_replace("''", "'", $val));
438 $checked = (is_int($value) ? ($value >> $i) & 1 : in_array($val, explode(",", $value), true));
4cead56 Edit default values directly in table creation
jakubvrana authored
439 echo " <label><input type='checkbox' name='fields[$name][$i]' value='" . (1 << $i) . "'" . ($checked ? ' checked' : '') . "$onchange>" . h($val) . '</label>';
61640ee Move common functions
jakubvrana authored
440 }
441 } elseif (strpos($field["type"], "text") !== false) {
689699a Shortcut for htmlspecialchars
jakubvrana authored
442 echo "<textarea name='fields[$name]' cols='50' rows='12'$onchange>" . h($value) . '</textarea>';
5abd943 Change simple preg_match to ereg
jakubvrana authored
443 } elseif (ereg('binary|blob', $field["type"])) {
689699a Shortcut for htmlspecialchars
jakubvrana authored
444 echo (ini_get("file_uploads") ? "<input type='file' name='$name'$onchange>" : lang('File uploads are disabled.'));
61640ee Move common functions
jakubvrana authored
445 } else {
446 // int(3) is only a display hint
447 $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));
ad237d3 Enlarge fields for long strings
jakubvrana authored
448 echo "<input name='fields[$name]' value='" . h($value) . "'" . ($maxlength ? " maxlength='$maxlength'" : "") . (ereg('char', $field["type"]) && $field["length"] > 20 ? " size='40'" : "") . "$onchange>";
61640ee Move common functions
jakubvrana authored
449 }
450 }
451 }
452
9a176b0 Comments
jakubvrana authored
453 /** Process edit input field
454 * @param one field from fields()
455 * @return string
456 */
a0def47 Date localization
jakubvrana authored
457 function process_input($field) {
64d616c Rename get_dbh to connection
jakubvrana authored
458 global $connection, $adminer;
a0def47 Date localization
jakubvrana authored
459 $idf = bracket_escape($field["field"]);
61640ee Move common functions
jakubvrana authored
460 $function = $_POST["function"][$idf];
461 $value = $_POST["fields"][$idf];
462 if ($field["type"] == "enum" ? $value == -1 : $function == "orig") {
463 return false;
464 } elseif ($field["type"] == "enum" || $field["auto_increment"] ? !strlen($value) : $function == "NULL") {
465 return "NULL";
466 } elseif ($field["type"] == "enum") {
4cead56 Edit default values directly in table creation
jakubvrana authored
467 return intval($value);
61640ee Move common functions
jakubvrana authored
468 } elseif ($field["type"] == "set") {
4cead56 Edit default values directly in table creation
jakubvrana authored
469 return array_sum((array) $value);
5abd943 Change simple preg_match to ereg
jakubvrana authored
470 } elseif (ereg('binary|blob', $field["type"])) {
61640ee Move common functions
jakubvrana authored
471 $file = get_file($idf);
472 if (!is_string($file)) {
473 return false; //! report errors
474 }
64d616c Rename get_dbh to connection
jakubvrana authored
475 return "_binary" . $connection->quote($file);
61640ee Move common functions
jakubvrana authored
476 } else {
a0def47 Date localization
jakubvrana authored
477 return $adminer->processInput($field, $value, $function);
61640ee Move common functions
jakubvrana authored
478 }
479 }
480
9a176b0 Comments
jakubvrana authored
481 /** Print CSV row
482 * @param array
483 * @return null
484 */
61640ee Move common functions
jakubvrana authored
485 function dump_csv($row) {
486 foreach ($row as $key => $val) {
487 if (preg_match("~[\"\n,]~", $val) || (isset($val) && !strlen($val))) {
488 $row[$key] = '"' . str_replace('"', '""', $val) . '"';
489 }
490 }
89a60e7 Use output buffering for dump compression
jakubvrana authored
491 echo implode(",", $row) . "\n";
61640ee Move common functions
jakubvrana authored
492 }
c16c57b E-mail sending
jakubvrana authored
493
9a176b0 Comments
jakubvrana authored
494 /** Apply SQL function
495 * @param string
496 * @param string escaped column identifier
497 * @return string
498 */
e331067 Move apply_sql_function
jakubvrana authored
499 function apply_sql_function($function, $column) {
500 return ($function ? ($function == "count distinct" ? "COUNT(DISTINCT " : strtoupper("$function(")) . "$column)" : $column);
501 }
502
9a176b0 Comments
jakubvrana authored
503 /** Check whether the string is e-mail address
504 * @param string
505 * @return bool
506 */
c16c57b E-mail sending
jakubvrana authored
507 function is_email($email) {
508 $atom = '[-a-z0-9!#$%&\'*+/=?^_`{|}~]'; // characters of local-name
509 $domain = '[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])'; // one domain component
fb141cc Speedup
jakubvrana authored
510 return preg_match("(^$atom+(\\.$atom+)*@($domain?\\.)+$domain\$)i", $email);
c16c57b E-mail sending
jakubvrana authored
511 }
dc667ea Hide select export and import
jakubvrana authored
512
bc78866 Link URLs in select
jakubvrana authored
513 /** Check whether the string is URL address
514 * @param string
515 * @return bool
516 */
517 function is_url($string) {
518 $domain = '[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])'; // one domain component
519 return preg_match("~^https?://($domain?\\.)+$domain(:[0-9]+)?(/.*)?(\\?.*)?(#.*)?\$~i", $string); //! restrict path, query and fragment characters
520 }
521
dc667ea Hide select export and import
jakubvrana authored
522 /** Print header for hidden fieldset (close by </div></fieldset>)
523 * @param string
524 * @param string
525 * @param bool
526 * @return null
527 */
528 function print_fieldset($id, $legend, $visible = false) {
529 echo "<fieldset><legend><a href='#fieldset-$id' onclick=\"return !toggle('fieldset-$id');\">$legend</a></legend><div id='fieldset-$id'" . ($visible ? "" : " class='hidden'") . ">\n";
530 }
Something went wrong with that request. Please try again.