Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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