Skip to content

HTTPS clone URL

Subversion checkout URL

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