Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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