Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 885 lines (825 sloc) 26.238 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
71e0db4 @vrana Log to "$database.sql" by default
authored
11 /** Get Adminer object
12 * @return Adminer
13 */
14 function adminer() {
15 global $adminer;
16 return $adminer;
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) {
a29ac72 Improve drivers
jakubvrana authored
24 $last = substr($idf, -1);
25 return str_replace($last . $last, $last, substr($idf, 1, -1));
667bfec Decomposition
jakubvrana authored
26 }
27
5e01a62 Prepare for SQLite
jakubvrana authored
28 /** Escape string to use inside ''
29 * @param string
30 * @return string
31 */
32 function escape_string($val) {
7e644b4 @vrana Save bytes ($connection->quote shortcut)
authored
33 return substr(q($val), 1, -1);
5e01a62 Prepare for SQLite
jakubvrana authored
34 }
35
72f4d9e @vrana Function parse_str respects magic_quotes_gpc (bug #3034575)
authored
36 /** Disable magic_quotes_gpc
37 * @param array e.g. (&$_GET, &$_POST, &$_COOKIE)
83d82d6 @vrana Pass $filter to remove_slashes (thanks to juzna)
authored
38 * @param bool whether to leave values as is
72f4d9e @vrana Function parse_str respects magic_quotes_gpc (bug #3034575)
authored
39 * @return null modified in place
40 */
83d82d6 @vrana Pass $filter to remove_slashes (thanks to juzna)
authored
41 function remove_slashes($process, $filter = false) {
72f4d9e @vrana Function parse_str respects magic_quotes_gpc (bug #3034575)
authored
42 if (get_magic_quotes_gpc()) {
43 while (list($key, $val) = each($process)) {
44 foreach ($val as $k => $v) {
45 unset($process[$key][$k]);
46 if (is_array($v)) {
47 $process[$key][stripslashes($k)] = $v;
48 $process[] = &$process[$key][stripslashes($k)];
49 } else {
50 $process[$key][stripslashes($k)] = ($filter ? $v : stripslashes($v));
51 }
52 }
53 }
54 }
55 }
56
9a176b0 Comments
jakubvrana authored
57 /** Escape or unescape string to use inside form []
58 * @param string
59 * @param bool
60 * @return string
61 */
667bfec Decomposition
jakubvrana authored
62 function bracket_escape($idf, $back = false) {
64ba924 Comments
jakubvrana authored
63 // escape brackets inside name="x[]"
73e8631 Move stuff to functions.inc.php
jakubvrana authored
64 static $trans = array(':' => ':1', ']' => ':2', '[' => ':3');
667bfec Decomposition
jakubvrana authored
65 return strtr($idf, ($back ? array_flip($trans) : $trans));
66 }
67
9a176b0 Comments
jakubvrana authored
68 /** Escape for HTML
69 * @param string
70 * @return string
71 */
689699a Shortcut for htmlspecialchars
jakubvrana authored
72 function h($string) {
0474c21 @vrana Hide null byte in HTML (bug #3358372)
authored
73 return htmlspecialchars(str_replace("\0", "", $string), ENT_QUOTES);
689699a Shortcut for htmlspecialchars
jakubvrana authored
74 }
75
9a176b0 Comments
jakubvrana authored
76 /** Escape for TD
77 * @param string
78 * @return string
79 */
c196985 Function nbsp
jakubvrana authored
80 function nbsp($string) {
7352c28 Replace strlen() by != ""
jakubvrana authored
81 return (trim($string) != "" ? h($string) : "&nbsp;");
c196985 Function nbsp
jakubvrana authored
82 }
83
3f5b683 Reintegrate sqlite branch
jakubvrana authored
84 /** Convert \n to <br>
85 * @param string
86 * @return string
87 */
88 function nl_br($string) {
89 return str_replace("\n", "<br>", $string); // nl2br() uses XHTML before PHP 5.3
90 }
91
6b30cfa Separate checkbox
jakubvrana authored
92 /** Generate HTML checkbox
93 * @param string
94 * @param string
95 * @param bool
96 * @param string
97 * @param string
3a14845 @vrana Respect defaults and comments checkbox after post (bug #3311791)
authored
98 * @param bool
6b30cfa Separate checkbox
jakubvrana authored
99 * @return string
100 */
3a14845 @vrana Respect defaults and comments checkbox after post (bug #3311791)
authored
101 function checkbox($name, $value, $checked, $label = "", $onclick = "", $jsonly = false) {
c4fe12a Clickable labels in IE6
jakubvrana authored
102 static $id = 0;
103 $id++;
be4f2ef @vrana More thorough escaping
authored
104 $return = "<input type='checkbox' name='$name' value='" . h($value) . "'" . ($checked ? " checked" : "") . ($onclick ? ' onclick="' . h($onclick) . '"' : '') . ($jsonly ? " class='jsonly'" : "") . " id='checkbox-$id'>";
7352c28 Replace strlen() by != ""
jakubvrana authored
105 return ($label != "" ? "<label for='checkbox-$id'>$return" . h($label) . "</label>" : $return);
6b30cfa Separate checkbox
jakubvrana authored
106 }
107
9a176b0 Comments
jakubvrana authored
108 /** Generate list of HTML options
109 * @param array array of strings or arrays (creates optgroup)
110 * @param mixed
111 * @param bool always use array keys for value="", otherwise only string keys are used
112 * @return string
113 */
1e4d11e Select from foreign keys in Editor
jakubvrana authored
114 function optionlist($options, $selected = null, $use_keys = false) {
667bfec Decomposition
jakubvrana authored
115 $return = "";
116 foreach ($options as $k => $v) {
2f76084 @vrana Avoid big ternary operator (saves memory)
authored
117 $opts = array($k => $v);
667bfec Decomposition
jakubvrana authored
118 if (is_array($v)) {
689699a Shortcut for htmlspecialchars
jakubvrana authored
119 $return .= '<optgroup label="' . h($k) . '">';
2f76084 @vrana Avoid big ternary operator (saves memory)
authored
120 $opts = $v;
667bfec Decomposition
jakubvrana authored
121 }
2f76084 @vrana Avoid big ternary operator (saves memory)
authored
122 foreach ($opts as $key => $val) {
cb6d36c HTML instead of XHTML
jakubvrana authored
123 $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
124 }
125 if (is_array($v)) {
126 $return .= '</optgroup>';
127 }
128 }
129 return $return;
130 }
131
098b74b Move function
jakubvrana authored
132 /** Generate HTML radio list
133 * @param string
134 * @param array
135 * @param string
136 * @param string true for no onchange, false for radio
137 * @return string
138 */
139 function html_select($name, $options, $value = "", $onchange = true) {
140 if ($onchange) {
037c547 @vrana Fix XSS (thanks to Jigal van Hemert)
authored
141 return "<select name='" . h($name) . "'" . (is_string($onchange) ? ' onchange="' . h($onchange) . '"' : "") . ">" . optionlist($options, $value) . "</select>";
098b74b Move function
jakubvrana authored
142 }
143 $return = "";
144 foreach ($options as $key => $val) {
145 $return .= "<label><input type='radio' name='" . h($name) . "' value='" . h($key) . "'" . ($key == $value ? " checked" : "") . ">" . h($val) . "</label>";
146 }
147 return $return;
148 }
149
a93bc19 @vrana Create confirm function
authored
150 /** Get onclick confirmation
151 * @param string JavaScript expression
b7cd652 @vrana No AJAX in drop table, drop database and logout
authored
152 * @param bool stop event propagation
a93bc19 @vrana Create confirm function
authored
153 * @return string
154 */
b7cd652 @vrana No AJAX in drop table, drop database and logout
authored
155 function confirm($count = "", $stop = false) {
156 return " onclick=\"" . ($stop ? "eventStop(event); " : "") . "return confirm('" . lang('Are you sure?') . ($count ? " (' + $count + ')" : "") . "');\"";
a93bc19 @vrana Create confirm function
authored
157 }
158
4b40554 @vrana Reorganize functions
authored
159 /** Print header for hidden fieldset (close by </div></fieldset>)
160 * @param string
161 * @param string
162 * @param bool
163 * @param string
164 * @return null
165 */
166 function print_fieldset($id, $legend, $visible = false, $onclick = "") {
167 echo "<fieldset><legend><a href='#fieldset-$id' onclick=\"" . h($onclick) . "return !toggle('fieldset-$id');\">$legend</a></legend><div id='fieldset-$id'" . ($visible ? "" : " class='hidden'") . ">\n";
168 }
169
170 /** Return class='active' if $bold is true
171 * @param bool
172 * @return string
173 */
174 function bold($bold) {
175 return ($bold ? " class='active'" : "");
176 }
177
178 /** Generate class for odd rows
179 * @param string return this for odd rows, empty to reset counter
180 * @return string
181 */
182 function odd($return = ' class="odd"') {
183 static $i = 0;
184 if (!$return) { // reset counter
185 $i = -1;
186 }
187 return ($i++ % 2 ? $return : '');
188 }
189
28f21aa @vrana Escape JavaScript strings (bug #3093243)
authored
190 /** Escape string for JavaScript apostrophes
191 * @param string
192 * @return string
193 */
194 function js_escape($string) {
195 return addcslashes($string, "\r\n'\\/"); // slash for <script>
196 }
197
4b40554 @vrana Reorganize functions
authored
198 /** Print one row in JSON object
199 * @param string or "" to close the object
200 * @param string
201 * @return null
202 */
203 function json_row($key, $val = null) {
204 static $first = true;
205 if ($first) {
206 echo "{";
207 }
208 if ($key != "") {
209 echo ($first ? "" : ",") . "\n\t\"" . addcslashes($key, "\r\n\"\\") . '": ' . (isset($val) ? '"' . addcslashes($val, "\r\n\"\\") . '"' : 'undefined');
210 $first = false;
211 } else {
212 echo "\n}\n";
213 $first = true;
214 }
215 }
216
3f5b683 Reintegrate sqlite branch
jakubvrana authored
217 /** Get INI boolean value
218 * @param string
219 * @return bool
220 */
221 function ini_bool($ini) {
222 $val = ini_get($ini);
223 return (eregi('^(on|true|yes)$', $val) || (int) $val); // boolean values set by php_value are strings
224 }
225
f30fd12 @vrana Work without session.use_cookies (bug #3138640)
authored
226 /** Check if SID is neccessary
227 * @return bool
228 */
229 function sid() {
876af58 @vrana Fix sid function
authored
230 static $return;
231 if (!isset($return)) { // restart_session() defines SID
232 $return = (SID && !($_COOKIE && ini_bool("session.use_cookies"))); // $_COOKIE - don't pass SID with permanent login
233 }
234 return $return;
f30fd12 @vrana Work without session.use_cookies (bug #3138640)
authored
235 }
236
237 /** Shortcut for $connection->quote($string)
238 * @param string
239 * @return string
240 */
7e644b4 @vrana Save bytes ($connection->quote shortcut)
authored
241 function q($string) {
242 global $connection;
243 return $connection->quote($string);
244 }
245
9a176b0 Comments
jakubvrana authored
246 /** Get list of values from database
247 * @param string
248 * @param mixed
249 * @return array
250 */
0bd864e Allow specifying column in get_vals
jakubvrana authored
251 function get_vals($query, $column = 0) {
64d616c Rename get_dbh to connection
jakubvrana authored
252 global $connection;
7769f2c Separate get_vals
jakubvrana authored
253 $return = array();
64d616c Rename get_dbh to connection
jakubvrana authored
254 $result = $connection->query($query);
b84252f Display SQLite compile options
jakubvrana authored
255 if (is_object($result)) {
49f7886 SHOW DATABASES can be revoked
jakubvrana authored
256 while ($row = $result->fetch_row()) {
0bd864e Allow specifying column in get_vals
jakubvrana authored
257 $return[] = $row[$column];
49f7886 SHOW DATABASES can be revoked
jakubvrana authored
258 }
7769f2c Separate get_vals
jakubvrana authored
259 }
260 return $return;
261 }
262
3f5b683 Reintegrate sqlite branch
jakubvrana authored
263 /** Get keys from first column and values from second
264 * @param string
265 * @param Min_DB
266 * @return array
267 */
268 function get_key_vals($query, $connection2 = null) {
269 global $connection;
270 if (!is_object($connection2)) {
271 $connection2 = $connection;
272 }
273 $return = array();
274 $result = $connection2->query($query);
e26b186 @vrana Avoid fatal error (bug #3110257)
authored
275 if (is_object($result)) {
276 while ($row = $result->fetch_row()) {
277 $return[$row[0]] = $row[1];
278 }
3f5b683 Reintegrate sqlite branch
jakubvrana authored
279 }
280 return $return;
281 }
282
b0d637b @vrana Avoid fatal errors
authored
283 /** Get all rows of result
284 * @param string
285 * @return array associative
286 */
96544ba @vrana Report errors in get_rows()
authored
287 function get_rows($query, $connection2 = null, $error = "<p class='error'>") {
b0d637b @vrana Avoid fatal errors
authored
288 global $connection;
289 if (!is_object($connection2)) {
290 $connection2 = $connection;
291 }
292 $return = array();
293 $result = $connection2->query($query);
294 if (is_object($result)) { // can return true
295 while ($row = $result->fetch_assoc()) {
296 $return[] = $row;
297 }
850d1aa @vrana Report errors only
authored
298 } elseif (!$result && $connection->error && $error && defined("PAGE_HEADER")) {
96544ba @vrana Report errors in get_rows()
authored
299 echo $error . error() . "\n";
b0d637b @vrana Avoid fatal errors
authored
300 }
301 return $return;
302 }
303
9a176b0 Comments
jakubvrana authored
304 /** Find unique identifier of a row
305 * @param array
306 * @param array result of indexes()
35ec64c Link COUNT(*) result to listing
jakubvrana authored
307 * @return array
9a176b0 Comments
jakubvrana authored
308 */
35ec64c Link COUNT(*) result to listing
jakubvrana authored
309 function unique_array($row, $indexes) {
720f5fc Restructure indexes
jakubvrana authored
310 foreach ($indexes as $index) {
4b3d569 Save bytes
jakubvrana authored
311 if (ereg("PRIMARY|UNIQUE", $index["type"])) {
720f5fc Restructure indexes
jakubvrana authored
312 $return = array();
313 foreach ($index["columns"] as $key) {
64ba924 Comments
jakubvrana authored
314 if (!isset($row[$key])) { // NULL is ambiguous
720f5fc Restructure indexes
jakubvrana authored
315 continue 2;
667bfec Decomposition
jakubvrana authored
316 }
35ec64c Link COUNT(*) result to listing
jakubvrana authored
317 $return[$key] = $row[$key];
667bfec Decomposition
jakubvrana authored
318 }
720f5fc Restructure indexes
jakubvrana authored
319 return $return;
667bfec Decomposition
jakubvrana authored
320 }
321 }
322 $return = array();
323 foreach ($row as $key => $val) {
26c9d64 Fix long SQL query crash (bug #2839231)
jakubvrana authored
324 if (!preg_match('~^(COUNT\\((\\*|(DISTINCT )?`(?:[^`]|``)+`)\\)|(AVG|GROUP_CONCAT|MAX|MIN|SUM)\\(`(?:[^`]|``)+`\\))$~', $key)) { //! columns looking like functions
35ec64c Link COUNT(*) result to listing
jakubvrana authored
325 $return[$key] = $val;
988a29f Don't use aggregation functions in unique_idf
jakubvrana authored
326 }
430034c Cottage homework
jakubvrana authored
327 }
328 return $return;
329 }
330
9a176b0 Comments
jakubvrana authored
331 /** Create SQL condition from parsed query string
332 * @param array parsed query string
333 * @return string
334 */
9ea3112 Mandatory $where in where()
jakubvrana authored
335 function where($where) {
1a8d7f1 @vrana Use LIKE for text comparison in MS SQL (bug #3088222)
authored
336 global $jush;
430034c Cottage homework
jakubvrana authored
337 $return = array();
341362a Driver specific INSERT INTO
jakubvrana authored
338 foreach ((array) $where["where"] as $key => $val) {
66ff153 Use bracket_escape function
jakubvrana authored
339 $return[] = idf_escape(bracket_escape($key, 1)) // 1 - back
d26fb62 @vrana Backslash is special with LIKE (bug #3344307)
authored
340 . (ereg('\\.', $val) || $jush == "mssql" ? " LIKE " . exact_value(addcslashes($val, "%_\\")) : " = " . exact_value($val)) // LIKE because of floats, but slow with ints, in MS SQL because of text
66ff153 Use bracket_escape function
jakubvrana authored
341 ; //! enum and set
341362a Driver specific INSERT INTO
jakubvrana authored
342 }
343 foreach ((array) $where["null"] as $key) {
344 $return[] = idf_escape($key) . " IS NULL";
667bfec Decomposition
jakubvrana authored
345 }
c16c57b E-mail sending
jakubvrana authored
346 return implode(" AND ", $return);
667bfec Decomposition
jakubvrana authored
347 }
348
9a176b0 Comments
jakubvrana authored
349 /** Create SQL condition from query string
350 * @param string
351 * @return string
352 */
0cd2afb Remove single item clone
jakubvrana authored
353 function where_check($val) {
354 parse_str($val, $check);
72f4d9e @vrana Function parse_str respects magic_quotes_gpc (bug #3034575)
authored
355 remove_slashes(array(&$check));
0cd2afb Remove single item clone
jakubvrana authored
356 return where($check);
357 }
358
9a176b0 Comments
jakubvrana authored
359 /** Create query string where condition from value
360 * @param int condition order
361 * @param string column identifier
362 * @param string
363 * @return string
186c058 Utilize where_link function
jakubvrana authored
364 * @return string
9a176b0 Comments
jakubvrana authored
365 */
186c058 Utilize where_link function
jakubvrana authored
366 function where_link($i, $column, $value, $operator = "=") {
8062648 @vrana Use IS NULL operator for null value in where_link function
authored
367 return "&where%5B$i%5D%5Bcol%5D=" . urlencode($column) . "&where%5B$i%5D%5Bop%5D=" . urlencode((isset($value) ? $operator : "IS NULL")) . "&where%5B$i%5D%5Bval%5D=" . urlencode($value);
c42c46a Table relations
jakubvrana authored
368 }
369
319cba4 Save e-mail from to cookie
jakubvrana authored
370 /** Set cookie valid for 1 month
371 * @param string
372 * @param string
373 * @return bool
374 */
375 function cookie($name, $value) {
2cec758 @vrana Direct links from HTTPS to HTTP
authored
376 global $HTTPS;
3f5b683 Reintegrate sqlite branch
jakubvrana authored
377 $params = array(
378 $name,
379 (ereg("\n", $value) ? "" : $value), // HTTP Response Splitting protection in PHP < 5.1.2
380 time() + 2592000, // 2592000 - 30 days
381 preg_replace('~\\?.*~', '', $_SERVER["REQUEST_URI"]),
382 "",
2cec758 @vrana Direct links from HTTPS to HTTP
authored
383 $HTTPS
3f5b683 Reintegrate sqlite branch
jakubvrana authored
384 );
c2334db Secure cookies
jakubvrana authored
385 if (version_compare(PHP_VERSION, '5.2.0') >= 0) {
386 $params[] = true; // HttpOnly
387 }
388 return call_user_func_array('setcookie', $params);
319cba4 Save e-mail from to cookie
jakubvrana authored
389 }
390
f9a2023 Improve concurrency
jakubvrana authored
391 /** Restart stopped session
392 * @return null
393 */
394 function restart_session() {
3f5b683 Reintegrate sqlite branch
jakubvrana authored
395 if (!ini_bool("session.use_cookies")) {
f9a2023 Improve concurrency
jakubvrana authored
396 session_start();
397 }
398 }
399
3f5b683 Reintegrate sqlite branch
jakubvrana authored
400 /** Get session variable for current server
401 * @param string
402 * @return mixed
403 */
404 function &get_session($key) {
405 return $_SESSION[$key][DRIVER][SERVER][$_GET["username"]];
406 }
407
408 /** Set session variable for current server
409 * @param string
410 * @param mixed
411 * @return mixed
412 */
413 function set_session($key, $val) {
414 $_SESSION[$key][DRIVER][SERVER][$_GET["username"]] = $val; // used also in auth.inc.php
415 }
416
605b093 @vrana List authentications
authored
417 /** Get authenticated URL
418 * @param string
419 * @param string
420 * @param string
421 * @return string
422 */
423 function auth_url($driver, $server, $username) {
424 global $drivers;
425 preg_match('~([^?]*)\\??(.*)~', remove_from_uri(implode("|", array_keys($drivers)) . "|username|" . session_name()), $match);
426 return "$match[1]?"
f30fd12 @vrana Work without session.use_cookies (bug #3138640)
authored
427 . (sid() ? SID . "&" : "")
605b093 @vrana List authentications
authored
428 . ($driver != "server" || $server != "" ? urlencode($driver) . "=" . urlencode($server) . "&" : "")
429 . "username=" . urlencode($username)
430 . ($match[2] ? "&$match[2]" : "")
431 ;
432 }
433
390e38b @vrana Load long texts for inline-edit by AJAX
authored
434 /** Find whether it is an AJAX request
435 * @return bool
436 */
437 function is_ajax() {
fdea197 @vrana Simplify AJAX redirect
authored
438 return ($_SERVER["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest");
390e38b @vrana Load long texts for inline-edit by AJAX
authored
439 }
440
9a176b0 Comments
jakubvrana authored
441 /** Send Location header and exit
9bb1909 Fix Save and continue edit
jakubvrana authored
442 * @param string null to only set a message
9a176b0 Comments
jakubvrana authored
443 * @param string
444 * @return null
445 */
430034c Cottage homework
jakubvrana authored
446 function redirect($location, $message = null) {
447 if (isset($message)) {
be49e08 Improve session restarting
jakubvrana authored
448 restart_session();
a379bde @vrana Save bytes
authored
449 $_SESSION["messages"][preg_replace('~^[^?]*~', '', (isset($location) ? $location : $_SERVER["REQUEST_URI"]))][] = $message;
430034c Cottage homework
jakubvrana authored
450 }
de24145 @vrana Clear POST with AJAX redirect
authored
451 if (isset($location)) {
b535853 @vrana Full AJAX only with pushState to work correctly with history
authored
452 if ($location == "") {
453 $location = ".";
454 }
fdea197 @vrana Simplify AJAX redirect
authored
455 header((is_ajax() ? "X-AJAX-Redirect" : "Location") . ": $location");
38894b2 @vrana Send all forms by AJAX
authored
456 exit;
9bb1909 Fix Save and continue edit
jakubvrana authored
457 }
430034c Cottage homework
jakubvrana authored
458 }
459
9a176b0 Comments
jakubvrana authored
460 /** Execute query and redirect if successful
461 * @param string
462 * @param string
463 * @param string
464 * @param bool
465 * @param bool
466 * @param bool
467 * @return bool
468 */
748ee83 Print SQL command with multiple queries
jakubvrana authored
469 function query_redirect($query, $location, $message, $redirect = true, $execute = true, $failed = false) {
64d616c Rename get_dbh to connection
jakubvrana authored
470 global $connection, $error, $adminer;
f9a2023 Improve concurrency
jakubvrana authored
471 if ($execute) {
472 $failed = !$connection->query($query);
473 }
10bdbed Save queries to history
jakubvrana authored
474 $sql = "";
475 if ($query) {
2e6ad20 @vrana Display ; in history (thanks to Jan Cerny)
authored
476 $sql = $adminer->messageQuery("$query;");
10bdbed Save queries to history
jakubvrana authored
477 }
748ee83 Print SQL command with multiple queries
jakubvrana authored
478 if ($failed) {
c1130ed Simplify SQL syntax errors everywhere
jakubvrana authored
479 $error = error() . $sql;
c48a017 Print SQL query by error
jakubvrana authored
480 return false;
f91b2c4 Print SQL query by message
jakubvrana authored
481 }
c48a017 Print SQL query by error
jakubvrana authored
482 if ($redirect) {
483 redirect($location, $message . $sql);
484 }
485 return true;
f91b2c4 Print SQL query by message
jakubvrana authored
486 }
487
9a176b0 Comments
jakubvrana authored
488 /** Execute and remember query
d06de55 @vrana Use DELIMITER in history
authored
489 * @param string null to return remembered queries, end with ';' to use DELIMITER
9a176b0 Comments
jakubvrana authored
490 * @return Min_Result
491 */
748ee83 Print SQL command with multiple queries
jakubvrana authored
492 function queries($query = null) {
64d616c Rename get_dbh to connection
jakubvrana authored
493 global $connection;
748ee83 Print SQL command with multiple queries
jakubvrana authored
494 static $queries = array();
495 if (!isset($query)) {
64ba924 Comments
jakubvrana authored
496 // return executed queries without parameter
2e6ad20 @vrana Display ; in history (thanks to Jan Cerny)
authored
497 return implode(";\n", $queries);
748ee83 Print SQL command with multiple queries
jakubvrana authored
498 }
2e6ad20 @vrana Display ; in history (thanks to Jan Cerny)
authored
499 $queries[] = (ereg(';$', $query) ? "DELIMITER ;;\n$query;\nDELIMITER " : $query);
64d616c Rename get_dbh to connection
jakubvrana authored
500 return $connection->query($query);
748ee83 Print SQL command with multiple queries
jakubvrana authored
501 }
502
f9bb1c5 @vrana Introduce apply_queries function
authored
503 /** Apply command to all array items
504 * @param string
505 * @param array
506 * @param callback
507 * @return bool
508 */
509 function apply_queries($query, $tables, $escape = 'table') {
510 foreach ($tables as $table) {
511 if (!queries("$query " . $escape($table))) {
512 return false;
513 }
514 }
515 return true;
516 }
517
dbdd40a Introduce queries_redirect function
jakubvrana authored
518 /** Redirect by remembered queries
519 * @param string
520 * @param string
521 * @param bool
52b9820 @vrana Comment
authored
522 * @return bool
dbdd40a Introduce queries_redirect function
jakubvrana authored
523 */
524 function queries_redirect($location, $message, $redirect) {
525 return query_redirect(queries(), $location, $message, $redirect, false, !$redirect);
526 }
527
9a176b0 Comments
jakubvrana authored
528 /** Remove parameter from query string
529 * @param string
530 * @return string
531 */
d41d446 remove_from_uri()
jakubvrana authored
532 function remove_from_uri($param = "") {
483d7dc Remove session_name() only without SID (bug #2910681)
jakubvrana authored
533 return substr(preg_replace("~(?<=[?&])($param" . (SID ? "" : "|" . session_name()) . ")=[^&]*&~", '', "$_SERVER[REQUEST_URI]&"), 0, -1);
d41d446 remove_from_uri()
jakubvrana authored
534 }
535
9a176b0 Comments
jakubvrana authored
536 /** Generate page number for pagination
537 * @param int
538 * @return string
539 */
bd25295 Don't redirect from last page
jakubvrana authored
540 function pagination($page, $current) {
6585b23 @vrana Generic AJAX links
authored
541 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
542 }
543
3e9b1ba Don't store files in hidden fields
jakubvrana authored
544 /** Get file contents from $_FILES
9a176b0 Comments
jakubvrana authored
545 * @param string
546 * @param bool
3e9b1ba Don't store files in hidden fields
jakubvrana authored
547 * @return mixed int for error, string otherwise
9a176b0 Comments
jakubvrana authored
548 */
25f01d3 Compress export and import
jakubvrana authored
549 function get_file($key, $decompress = false) {
550 $file = $_FILES[$key];
551 if (!$file || $file["error"]) {
552 return $file["error"];
430034c Cottage homework
jakubvrana authored
553 }
9ff10f8 @vrana Parse UTF-16 and UTF-8 BOM in all text uploads
authored
554 $return = file_get_contents($decompress && ereg('\\.gz$', $file["name"]) ? "compress.zlib://$file[tmp_name]"
0a2d81d Bzip2 import
jakubvrana authored
555 : ($decompress && ereg('\\.bz2$', $file["name"]) ? "compress.bzip2://$file[tmp_name]"
556 : $file["tmp_name"]
557 )); //! may not be reachable because of open_basedir
9ff10f8 @vrana Parse UTF-16 and UTF-8 BOM in all text uploads
authored
558 if ($decompress) {
b5cbbce @vrana Save memory
authored
559 $start = substr($return, 0, 3);
560 if (function_exists("iconv") && ereg("^\xFE\xFF|^\xFF\xFE", $start, $regs)) { // not ternary operator to save memory
9ff10f8 @vrana Parse UTF-16 and UTF-8 BOM in all text uploads
authored
561 $return = iconv("utf-16", "utf-8", $return);
b5cbbce @vrana Save memory
authored
562 } elseif ($start == "\xEF\xBB\xBF") { // UTF-8 BOM
563 $return = substr($return, 3);
9ff10f8 @vrana Parse UTF-16 and UTF-8 BOM in all text uploads
authored
564 }
565 }
566 return $return;
430034c Cottage homework
jakubvrana authored
567 }
568
9a176b0 Comments
jakubvrana authored
569 /** Determine upload error
570 * @param int
571 * @return string
572 */
e2a03c0 Show upload_max_filesize
jakubvrana authored
573 function upload_error($error) {
574 $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
575 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
576 }
577
4b40554 @vrana Reorganize functions
authored
578 /** Create repeat pattern for preg
c313dd8 @vrana Deferred operations by AJAX instead of JS (doesn't require sending to…
authored
579 * @param string
4b40554 @vrana Reorganize functions
authored
580 * @param int
581 * @return string
c313dd8 @vrana Deferred operations by AJAX instead of JS (doesn't require sending to…
authored
582 */
4b40554 @vrana Reorganize functions
authored
583 function repeat_pattern($pattern, $length) {
584 // fix for Compilation failed: number too big in {} quantifier
585 return str_repeat("$pattern{0,65535}", $length / 65535) . "$pattern{0," . ($length % 65535) . "}"; // can create {0,0} which is OK
c313dd8 @vrana Deferred operations by AJAX instead of JS (doesn't require sending to…
authored
586 }
587
9a176b0 Comments
jakubvrana authored
588 /** Check whether the string is in UTF-8
589 * @param string
590 * @return bool
591 */
017ffb5 Better UTF-8 detection
jakubvrana authored
592 function is_utf8($val) {
64ba924 Comments
jakubvrana authored
593 // don't print control chars except \t\r\n
017ffb5 Better UTF-8 detection
jakubvrana authored
594 return (preg_match('~~u', $val) && !preg_match('~[\\0-\\x8\\xB\\xC\\xE-\\x1F]~', $val));
595 }
596
9a176b0 Comments
jakubvrana authored
597 /** Shorten UTF-8 string
598 * @param string
599 * @param int
600 * @param string
601 * @return string escaped string with appended ...
602 */
cd07584 Save SQL query to history
jakubvrana authored
603 function shorten_utf8($string, $length = 80, $suffix = "") {
1315537 @vrana No error for big text length (bug #264133)
authored
604 if (!preg_match("(^(" . repeat_pattern("[\t\r\n -\x{FFFF}]", $length) . ")($)?)u", $string, $match)) { // ~s causes trash in $match[2] under some PHP versions, (.|\n) is slow
605 preg_match("(^(" . repeat_pattern("[\t\r\n -~]", $length) . ")($)?)", $string, $match);
95764b0 Shorten binary strings
jakubvrana authored
606 }
687b3fd @vrana Replace <strong> by <b> and <em> by <i>
authored
607 return h($match[1]) . $suffix . (isset($match[2]) ? "" : "<i>...</i>");
02a851c Shorten texts in PHP
jakubvrana authored
608 }
3920ebb Remove InnoDB table comment in Tables and views
jakubvrana authored
609
9a176b0 Comments
jakubvrana authored
610 /** Generate friendly URL
611 * @param string
612 * @return string
613 */
8251896 Downloaded file name
jakubvrana authored
614 function friendly_url($val) {
64ba924 Comments
jakubvrana authored
615 // used for blobs and export
8251896 Downloaded file name
jakubvrana authored
616 return preg_replace('~[^a-z0-9_]~i', '-', $val);
617 }
618
9a176b0 Comments
jakubvrana authored
619 /** Print hidden fields
620 * @param array
621 * @param array
622 * @return null
623 */
820c882 Bulk clone
jakubvrana authored
624 function hidden_fields($process, $ignore = array()) {
5d7071a Choose language through option-list
jakubvrana authored
625 while (list($key, $val) = each($process)) {
626 if (is_array($val)) {
627 foreach ($val as $k => $v) {
628 $process[$key . "[$k]"] = $v;
629 }
630 } elseif (!in_array($key, $ignore)) {
689699a Shortcut for htmlspecialchars
jakubvrana authored
631 echo '<input type="hidden" name="' . h($key) . '" value="' . h($val) . '">';
5d7071a Choose language through option-list
jakubvrana authored
632 }
633 }
634 }
61640ee Move common functions
jakubvrana authored
635
3f5b683 Reintegrate sqlite branch
jakubvrana authored
636 /** Print hidden fields for GET forms
637 * @return null
638 */
639 function hidden_fields_get() {
f30fd12 @vrana Work without session.use_cookies (bug #3138640)
authored
640 echo (sid() ? '<input type="hidden" name="' . session_name() . '" value="' . h(session_id()) . '">' : '');
3f5b683 Reintegrate sqlite branch
jakubvrana authored
641 echo (SERVER !== null ? '<input type="hidden" name="' . DRIVER . '" value="' . h(SERVER) . '">' : "");
642 echo '<input type="hidden" name="username" value="' . h($_GET["username"]) . '">';
643 }
644
9a176b0 Comments
jakubvrana authored
645 /** Find out foreign keys for each column
646 * @param string
647 * @return array array($col => array())
648 */
1e4d11e Select from foreign keys in Editor
jakubvrana authored
649 function column_foreign_keys($table) {
753909e @vrana Support for virtual foreign keys
authored
650 global $adminer;
1e4d11e Select from foreign keys in Editor
jakubvrana authored
651 $return = array();
753909e @vrana Support for virtual foreign keys
authored
652 foreach ($adminer->foreignKeys($table) as $foreign_key) {
1e4d11e Select from foreign keys in Editor
jakubvrana authored
653 foreach ($foreign_key["source"] as $val) {
654 $return[$val][] = $foreign_key;
655 }
656 }
657 return $return;
658 }
659
3f5b683 Reintegrate sqlite branch
jakubvrana authored
660 /** Print enum input field
661 * @param string "radio"|"checkbox"
662 * @param string
663 * @param array
664 * @param mixed int|string|array
684e70d @vrana Search for empty enum
authored
665 * @param string
3f5b683 Reintegrate sqlite branch
jakubvrana authored
666 * @return null
667 */
684e70d @vrana Search for empty enum
authored
668 function enum_input($type, $attrs, $field, $value, $empty = null) {
c068206 @vrana Use $adminer->editVal in enum_input function
authored
669 global $adminer;
3f5b683 Reintegrate sqlite branch
jakubvrana authored
670 preg_match_all("~'((?:[^']|'')*)'~", $field["length"], $matches);
684e70d @vrana Search for empty enum
authored
671 $return = (isset($empty) ? "<label><input type='$type'$attrs value='$empty'" . ((is_array($value) ? in_array($empty, $value) : $value === 0) ? " checked" : "") . "><i>" . lang('empty') . "</i></label>" : "");
3f5b683 Reintegrate sqlite branch
jakubvrana authored
672 foreach ($matches[1] as $i => $val) {
673 $val = stripcslashes(str_replace("''", "'", $val));
674 $checked = (is_int($value) ? $value == $i+1 : (is_array($value) ? in_array($i+1, $value) : $value === $val));
c068206 @vrana Use $adminer->editVal in enum_input function
authored
675 $return .= " <label><input type='$type'$attrs value='" . ($i+1) . "'" . ($checked ? ' checked' : '') . '>' . h($adminer->editVal($val, $field)) . '</label>';
3f5b683 Reintegrate sqlite branch
jakubvrana authored
676 }
fe06908 @vrana Allow redefining editInput for enum (bug #3048711)
authored
677 return $return;
3f5b683 Reintegrate sqlite branch
jakubvrana authored
678 }
679
9a176b0 Comments
jakubvrana authored
680 /** Print edit input field
681 * @param array one field from fields()
682 * @param mixed
683 * @param string
684 * @return null
685 */
1e4d11e Select from foreign keys in Editor
jakubvrana authored
686 function input($field, $value, $function) {
e672694 @vrana Rename $driver to $jush
authored
687 global $types, $adminer, $jush;
689699a Shortcut for htmlspecialchars
jakubvrana authored
688 $name = h(bracket_escape($field["field"]));
61640ee Move common functions
jakubvrana authored
689 echo "<td class='function'>";
9f69d7a @vrana MS SQL: Do not update AI value
authored
690 $reset = ($jush == "mssql" && $field["auto_increment"]);
691 if ($reset && !$_POST["save"]) {
692 $function = null;
693 }
694 $functions = (isset($_GET["select"]) || $reset ? array("orig" => lang('original')) : array()) + $adminer->editFunctions($field);
454c80f @vrana Save bytes
authored
695 $attrs = " name='fields[$name]'";
61640ee Move common functions
jakubvrana authored
696 if ($field["type"] == "enum") {
fe06908 @vrana Allow redefining editInput for enum (bug #3048711)
authored
697 echo nbsp($functions[""]) . "<td>" . $adminer->editInput($_GET["edit"], $field, $attrs, $value);
61640ee Move common functions
jakubvrana authored
698 } else {
9903946 Use original values in Editor multi edit
jakubvrana authored
699 $first = 0;
700 foreach ($functions as $key => $val) {
701 if ($key === "" || !$val) {
702 break;
703 }
704 $first++;
705 }
f80e15f @vrana Typo
authored
706 $onchange = ($first ? " onchange=\"var f = this.form['function[" . h(js_escape(bracket_escape($field["field"]))) . "]']; if ($first > f.selectedIndex) f.selectedIndex = $first;\"" : "");
454c80f @vrana Save bytes
authored
707 $attrs .= $onchange;
622845b @vrana Disable maxlength with edit functions
authored
708 echo (count($functions) > 1 ? html_select("function[$name]", $functions, !isset($function) || in_array($function, $functions) || isset($functions[$function]) ? $function : "", "functionChange(this);") : nbsp(reset($functions))) . '<td>';
a561252 Use distinct name for blob inputs
jakubvrana authored
709 $input = $adminer->editInput($_GET["edit"], $field, $attrs, $value); // usage in call is without a table
7352c28 Replace strlen() by != ""
jakubvrana authored
710 if ($input != "") {
e1abcda Treat tinyint(1) as boolean
jakubvrana authored
711 echo $input;
1e4d11e Select from foreign keys in Editor
jakubvrana authored
712 } elseif ($field["type"] == "set") { //! 64 bits
26c9d64 Fix long SQL query crash (bug #2839231)
jakubvrana authored
713 preg_match_all("~'((?:[^']|'')*)'~", $field["length"], $matches);
61640ee Move common functions
jakubvrana authored
714 foreach ($matches[1] as $i => $val) {
715 $val = stripcslashes(str_replace("''", "'", $val));
716 $checked = (is_int($value) ? ($value >> $i) & 1 : in_array($val, explode(",", $value), true));
045bce3 @vrana Use editVal for set values
authored
717 echo " <label><input type='checkbox' name='fields[$name][$i]' value='" . (1 << $i) . "'" . ($checked ? ' checked' : '') . "$onchange>" . h($adminer->editVal($val, $field)) . '</label>';
61640ee Move common functions
jakubvrana authored
718 }
5a73c01 @vrana Treat binary type as hex
authored
719 } elseif (ereg('blob|bytea|raw|file', $field["type"]) && ini_bool("file_uploads")) {
a561252 Use distinct name for blob inputs
jakubvrana authored
720 echo "<input type='file' name='fields-$name'$onchange>";
375a5c8 @vrana Non-MySQL binary types
authored
721 } elseif (ereg('text|lob', $field["type"])) {
7d152fb @vrana Utilize bodyKeydown function
authored
722 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
723 } else {
724 // int(3) is only a display hint
5002b89 @vrana Big numbers without E
authored
725 $maxlength = (!ereg('int', $field["type"]) && preg_match('~^(\\d+)(,(\\d+))?$~', $field["length"], $match) ? ((ereg("binary", $field["type"]) ? 2 : 1) * $match[1] + ($match[3] ? 1 : 0) + ($match[2] && !$field["unsigned"] ? 1 : 0)) : ($types[$field["type"]] ? $types[$field["type"]] + ($field["unsigned"] ? 0 : 1) : 0));
5a73c01 @vrana Treat binary type as hex
authored
726 echo "<input value='" . h($value) . "'" . ($maxlength ? " maxlength='$maxlength'" : "") . (ereg('char|binary', $field["type"]) && $maxlength > 20 ? " size='40'" : "") . "$attrs>";
61640ee Move common functions
jakubvrana authored
727 }
728 }
729 }
730
9a176b0 Comments
jakubvrana authored
731 /** Process edit input field
732 * @param one field from fields()
733 * @return string
734 */
a0def47 Date localization
jakubvrana authored
735 function process_input($field) {
7e644b4 @vrana Save bytes ($connection->quote shortcut)
authored
736 global $adminer;
a0def47 Date localization
jakubvrana authored
737 $idf = bracket_escape($field["field"]);
61640ee Move common functions
jakubvrana authored
738 $function = $_POST["function"][$idf];
739 $value = $_POST["fields"][$idf];
3f5b683 Reintegrate sqlite branch
jakubvrana authored
740 if ($field["type"] == "enum") {
741 if ($value == -1) {
742 return false;
743 }
744 if ($value == "") {
745 return "NULL";
746 }
04eccba @vrana Allow bigger numbers under 32 bits
authored
747 return +$value;
3f5b683 Reintegrate sqlite branch
jakubvrana authored
748 }
749 if ($field["auto_increment"] && $value == "") {
750 return null;
751 }
752 if ($function == "orig") {
c0c21d4 @vrana Preserve original timestamp value in multiple update (bug #3312614)
authored
753 return ($field["on_update"] == "CURRENT_TIMESTAMP" ? idf_escape($field["field"]) : false);
3f5b683 Reintegrate sqlite branch
jakubvrana authored
754 }
755 if ($function == "NULL") {
61640ee Move common functions
jakubvrana authored
756 return "NULL";
3f5b683 Reintegrate sqlite branch
jakubvrana authored
757 }
758 if ($field["type"] == "set") {
4cead56 Edit default values directly in table creation
jakubvrana authored
759 return array_sum((array) $value);
3f5b683 Reintegrate sqlite branch
jakubvrana authored
760 }
5a73c01 @vrana Treat binary type as hex
authored
761 if (ereg('blob|bytea|raw|file', $field["type"]) && ini_bool("file_uploads")) {
a561252 Use distinct name for blob inputs
jakubvrana authored
762 $file = get_file("fields-$idf");
61640ee Move common functions
jakubvrana authored
763 if (!is_string($file)) {
764 return false; //! report errors
765 }
7e644b4 @vrana Save bytes ($connection->quote shortcut)
authored
766 return q($file);
61640ee Move common functions
jakubvrana authored
767 }
3f5b683 Reintegrate sqlite branch
jakubvrana authored
768 return $adminer->processInput($field, $value, $function);
61640ee Move common functions
jakubvrana authored
769 }
770
db07325 Search in all tables
jakubvrana authored
771 /** Print results of search in all tables
772 * @uses $_GET["where"][0]
773 * @uses $_POST["tables"]
774 * @return null
775 */
776 function search_tables() {
777 global $adminer, $connection;
dfa6cbf @vrana Show tables overview in Editor
authored
778 $_GET["where"][0]["op"] = "LIKE %%";
779 $_GET["where"][0]["val"] = $_POST["query"];
db07325 Search in all tables
jakubvrana authored
780 $found = false;
781 foreach (table_status() as $table => $table_status) {
782 $name = $adminer->tableName($table_status);
783 if (isset($table_status["Engine"]) && $name != "" && (!$_POST["tables"] || in_array($table, $_POST["tables"]))) {
ee3e045 @vrana Separate $where in limit function
authored
784 $result = $connection->query("SELECT" . limit("1 FROM " . table($table), " WHERE " . implode(" AND ", $adminer->selectSearchProcess(fields($table), array())), 1));
782921b @vrana Finish SQLite
authored
785 if ($result->fetch_row()) {
db07325 Search in all tables
jakubvrana authored
786 if (!$found) {
787 echo "<ul>\n";
788 $found = true;
789 }
5ee1407 @vrana Avoid double escaping
authored
790 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"])) . "'>$name</a>\n";
db07325 Search in all tables
jakubvrana authored
791 }
792 }
793 }
794 echo ($found ? "</ul>" : "<p class='message'>" . lang('No tables.')) . "\n";
795 }
796
8ab6fff @vrana Centralize dump_headers
authored
797 /** Send headers for export
798 * @param string
799 * @param bool
800 * @return string extension
801 */
802 function dump_headers($identifier, $multi_table = false) {
803 global $adminer;
804 $return = $adminer->dumpHeaders($identifier, $multi_table);
805 $output = $_POST["output"];
806 if ($output != "text") {
5792eab @vrana Use server name for server dump
authored
807 header("Content-Disposition: attachment; filename=" . friendly_url($identifier != "" ? $identifier : (SERVER != "" ? SERVER : "localhost")) . ".$return" . ($output != "file" && !ereg('[^0-9a-z]', $output) ? ".$output" : ""));
8ab6fff @vrana Centralize dump_headers
authored
808 }
809 session_write_close();
810 return $return;
811 }
812
9a176b0 Comments
jakubvrana authored
813 /** Print CSV row
814 * @param array
815 * @return null
816 */
61640ee Move common functions
jakubvrana authored
817 function dump_csv($row) {
818 foreach ($row as $key => $val) {
e225d22 @vrana TSV export and import (bug #3097657)
authored
819 if (preg_match("~[\"\n,;\t]~", $val) || $val === "") {
61640ee Move common functions
jakubvrana authored
820 $row[$key] = '"' . str_replace('"', '""', $val) . '"';
821 }
822 }
02d6b90 @vrana CR+LF in CSV export
authored
823 echo implode(($_POST["format"] == "csv" ? "," : ($_POST["format"] == "tsv" ? "\t" : ";")), $row) . "\r\n";
61640ee Move common functions
jakubvrana authored
824 }
c16c57b E-mail sending
jakubvrana authored
825
9a176b0 Comments
jakubvrana authored
826 /** Apply SQL function
827 * @param string
828 * @param string escaped column identifier
829 * @return string
830 */
e331067 Move apply_sql_function
jakubvrana authored
831 function apply_sql_function($function, $column) {
3f5b683 Reintegrate sqlite branch
jakubvrana authored
832 return ($function ? ($function == "unixepoch" ? "DATETIME($column, '$function')" : ($function == "count distinct" ? "COUNT(DISTINCT " : strtoupper("$function(")) . "$column)") : $column);
e331067 Move apply_sql_function
jakubvrana authored
833 }
834
4ba2d85 @vrana Allow permanent login without customization
authored
835 /** Read password from file adminer.key in temporary directory or create one
836 * @return string or false if the file can not be created
837 */
838 function password_file() {
839 $dir = ini_get("upload_tmp_dir"); // session_save_path() may contain other storage path
840 if (!$dir) {
841 if (function_exists('sys_get_temp_dir')) {
842 $dir = sys_get_temp_dir();
843 } else {
844 $filename = @tempnam("", ""); // @ - temp directory can be disabled by open_basedir
845 if (!$filename) {
846 return false;
847 }
848 $dir = dirname($filename);
849 unlink($filename);
850 }
851 }
852 $filename = "$dir/adminer.key";
853 $return = @file_get_contents($filename); // @ - can not exist
854 if ($return) {
855 return $return;
856 }
857 $fp = @fopen($filename, "w"); // @ - can have insufficient rights //! is not atomic
858 if ($fp) {
859 $return = md5(uniqid(mt_rand(), true));
860 fwrite($fp, $return);
861 fclose($fp);
862 }
863 return $return;
864 }
865
9a176b0 Comments
jakubvrana authored
866 /** Check whether the string is e-mail address
867 * @param string
868 * @return bool
869 */
1cf374b @vrana Rename functions (avoid conflict with WordPress)
authored
870 function is_mail($email) {
c16c57b E-mail sending
jakubvrana authored
871 $atom = '[-a-z0-9!#$%&\'*+/=?^_`{|}~]'; // characters of local-name
872 $domain = '[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])'; // one domain component
28e1dd2 Allow multiple e-mails
jakubvrana authored
873 $pattern = "$atom+(\\.$atom+)*@($domain?\\.)+$domain";
874 return preg_match("(^$pattern(,\\s*$pattern)*\$)i", $email);
c16c57b E-mail sending
jakubvrana authored
875 }
dc667ea Hide select export and import
jakubvrana authored
876
bc78866 Link URLs in select
jakubvrana authored
877 /** Check whether the string is URL address
878 * @param string
2cec758 @vrana Direct links from HTTPS to HTTP
authored
879 * @return string "http", "https" or ""
bc78866 Link URLs in select
jakubvrana authored
880 */
881 function is_url($string) {
2cec758 @vrana Direct links from HTTPS to HTTP
authored
882 $domain = '[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])'; // one domain component //! IDN
5002b89 @vrana Big numbers without E
authored
883 return (preg_match("~^(https?)://($domain?\\.)+$domain(:\\d+)?(/.*)?(\\?.*)?(#.*)?\$~i", $string, $match) ? strtolower($match[1]) : ""); //! restrict path, query and fragment characters
bc78866 Link URLs in select
jakubvrana authored
884 }
Something went wrong with that request. Please try again.