Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 1384 lines (1302 sloc) 40.724 kb
667bfec4 » jakubvrana
2007-07-02 Decomposition
1 <?php
9a176b0c » jakubvrana
2009-08-31 Comments
2 /** Get database connection
3 * @return Min_DB
4 */
64d616c0 » jakubvrana
2009-09-22 Rename get_dbh to connection
5 function connection() {
6 // can be used in customization, $connection is minified
7 global $connection;
8 return $connection;
c64c4fd1 » jakubvrana
2009-07-27 Adminer class
9 }
10
71e0db41 »
2011-08-10 Log to "$database.sql" by default
11 /** Get Adminer object
12 * @return Adminer
13 */
14 function adminer() {
15 global $adminer;
16 return $adminer;
17 }
18
9a176b0c » jakubvrana
2009-08-31 Comments
19 /** Unescape database identifier
20 * @param string text inside ``
21 * @return string
22 */
667bfec4 » jakubvrana
2007-07-02 Decomposition
23 function idf_unescape($idf) {
a29ac72c » jakubvrana
2010-04-21 Improve drivers
24 $last = substr($idf, -1);
25 return str_replace($last . $last, $last, substr($idf, 1, -1));
667bfec4 » jakubvrana
2007-07-02 Decomposition
26 }
27
5e01a627 » jakubvrana
2009-10-16 Prepare for SQLite
28 /** Escape string to use inside ''
29 * @param string
30 * @return string
31 */
32 function escape_string($val) {
7e644b43 »
2010-10-13 Save bytes ($connection->quote shortcut)
33 return substr(q($val), 1, -1);
5e01a627 » jakubvrana
2009-10-16 Prepare for SQLite
34 }
35
72f4d9e2 »
2010-07-26 Function parse_str respects magic_quotes_gpc (bug #3034575)
36 /** Disable magic_quotes_gpc
37 * @param array e.g. (&$_GET, &$_POST, &$_COOKIE)
83d82d6e »
2011-07-22 Pass $filter to remove_slashes (thanks to juzna)
38 * @param bool whether to leave values as is
72f4d9e2 »
2010-07-26 Function parse_str respects magic_quotes_gpc (bug #3034575)
39 * @return null modified in place
40 */
83d82d6e »
2011-07-22 Pass $filter to remove_slashes (thanks to juzna)
41 function remove_slashes($process, $filter = false) {
72f4d9e2 »
2010-07-26 Function parse_str respects magic_quotes_gpc (bug #3034575)
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
9a176b0c » jakubvrana
2009-08-31 Comments
57 /** Escape or unescape string to use inside form []
58 * @param string
59 * @param bool
60 * @return string
61 */
667bfec4 » jakubvrana
2007-07-02 Decomposition
62 function bracket_escape($idf, $back = false) {
64ba9242 » jakubvrana
2009-06-21 Comments
63 // escape brackets inside name="x[]"
73e8631b » jakubvrana
2007-07-09 Move stuff to functions.inc.php
64 static $trans = array(':' => ':1', ']' => ':2', '[' => ':3');
667bfec4 » jakubvrana
2007-07-02 Decomposition
65 return strtr($idf, ($back ? array_flip($trans) : $trans));
66 }
67
8bd3dca2 »
2014-06-26 Use utf8mb4 on more places
68 /** Get connection charset
69 * @param Min_DB
70 * @return string
71 */
72 function charset($connection) {
73 return (version_compare($connection->server_info, "5.5.3") > 0 ? "utf8mb4" : "utf8"); // SHOW CHARSET would require an extra query
74 }
75
9a176b0c » jakubvrana
2009-08-31 Comments
76 /** Escape for HTML
77 * @param string
78 * @return string
79 */
689699aa » jakubvrana
2009-07-28 Shortcut for htmlspecialchars
80 function h($string) {
e62fc3a0 »
2014-05-02 Preserve null byte in HTML
81 return str_replace("\0", "&#0;", htmlspecialchars($string, ENT_QUOTES));
689699aa » jakubvrana
2009-07-28 Shortcut for htmlspecialchars
82 }
83
9a176b0c » jakubvrana
2009-08-31 Comments
84 /** Escape for TD
85 * @param string
86 * @return string
87 */
c1969850 » jakubvrana
2009-08-25 Function nbsp
88 function nbsp($string) {
7352c281 » jakubvrana
2010-01-09 Replace strlen() by != ""
89 return (trim($string) != "" ? h($string) : "&nbsp;");
c1969850 » jakubvrana
2009-08-25 Function nbsp
90 }
91
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
92 /** Convert \n to <br>
93 * @param string
94 * @return string
95 */
96 function nl_br($string) {
97 return str_replace("\n", "<br>", $string); // nl2br() uses XHTML before PHP 5.3
98 }
99
6b30cfa7 » jakubvrana
2009-09-25 Separate checkbox
100 /** Generate HTML checkbox
101 * @param string
102 * @param string
103 * @param bool
104 * @param string
105 * @param string
68aba96c »
2013-05-20 Increase click target for checkboxes (thanks to Roman)
106 * @param string
6b30cfa7 » jakubvrana
2009-09-25 Separate checkbox
107 * @return string
108 */
68aba96c »
2013-05-20 Increase click target for checkboxes (thanks to Roman)
109 function checkbox($name, $value, $checked, $label = "", $onclick = "", $class = "") {
110 $return = "<input type='checkbox' name='$name' value='" . h($value) . "'"
111 . ($checked ? " checked" : "")
112 . ($onclick ? ' onclick="' . h($onclick) . '"' : '')
26ad18ba »
2013-06-03 Avoid duplicate values of HTML id attributes (bug #3614245)
113 . ">"
68aba96c »
2013-05-20 Increase click target for checkboxes (thanks to Roman)
114 ;
26ad18ba »
2013-06-03 Avoid duplicate values of HTML id attributes (bug #3614245)
115 return ($label != "" || $class ? "<label" . ($class ? " class='$class'" : "") . ">$return" . h($label) . "</label>" : $return);
6b30cfa7 » jakubvrana
2009-09-25 Separate checkbox
116 }
117
9a176b0c » jakubvrana
2009-08-31 Comments
118 /** Generate list of HTML options
119 * @param array array of strings or arrays (creates optgroup)
120 * @param mixed
121 * @param bool always use array keys for value="", otherwise only string keys are used
122 * @return string
123 */
1e4d11e9 » jakubvrana
2009-07-23 Select from foreign keys in Editor
124 function optionlist($options, $selected = null, $use_keys = false) {
667bfec4 » jakubvrana
2007-07-02 Decomposition
125 $return = "";
126 foreach ($options as $k => $v) {
2f76084b »
2011-03-21 Avoid big ternary operator (saves memory)
127 $opts = array($k => $v);
667bfec4 » jakubvrana
2007-07-02 Decomposition
128 if (is_array($v)) {
689699aa » jakubvrana
2009-07-28 Shortcut for htmlspecialchars
129 $return .= '<optgroup label="' . h($k) . '">';
2f76084b »
2011-03-21 Avoid big ternary operator (saves memory)
130 $opts = $v;
667bfec4 » jakubvrana
2007-07-02 Decomposition
131 }
2f76084b »
2011-03-21 Avoid big ternary operator (saves memory)
132 foreach ($opts as $key => $val) {
cb6d36c0 » jakubvrana
2009-07-28 HTML instead of XHTML
133 $return .= '<option' . ($use_keys || is_string($key) ? ' value="' . h($key) . '"' : '') . (($use_keys || is_string($key) ? (string) $key : $val) === $selected ? ' selected' : '') . '>' . h($val);
667bfec4 » jakubvrana
2007-07-02 Decomposition
134 }
135 if (is_array($v)) {
136 $return .= '</optgroup>';
137 }
138 }
139 return $return;
140 }
141
098b74b6 » jakubvrana
2009-11-09 Move function
142 /** Generate HTML radio list
143 * @param string
144 * @param array
145 * @param string
146 * @param string true for no onchange, false for radio
147 * @return string
148 */
149 function html_select($name, $options, $value = "", $onchange = true) {
150 if ($onchange) {
037c5473 »
2011-07-26 Fix XSS (thanks to Jigal van Hemert)
151 return "<select name='" . h($name) . "'" . (is_string($onchange) ? ' onchange="' . h($onchange) . '"' : "") . ">" . optionlist($options, $value) . "</select>";
098b74b6 » jakubvrana
2009-11-09 Move function
152 }
153 $return = "";
154 foreach ($options as $key => $val) {
155 $return .= "<label><input type='radio' name='" . h($name) . "' value='" . h($key) . "'" . ($key == $value ? " checked" : "") . ">" . h($val) . "</label>";
156 }
157 return $return;
158 }
159
d17b17e5 »
2013-07-06 Driver for SimpleDB
160 /** Generate HTML <select> or <input> if $options are empty
161 * @param string
162 * @param array
163 * @param string
164 * @param string
165 * @return string
166 */
167 function select_input($attrs, $options, $value = "", $placeholder = "") {
168 return ($options
169 ? "<select$attrs><option value=''>$placeholder" . optionlist($options, $value, true) . "</select>"
63a04ff7 »
2014-01-11 MongoDB: alter indexes form
170 : "<input$attrs size='10' value='" . h($value) . "' placeholder='$placeholder'>"
d17b17e5 »
2013-07-06 Driver for SimpleDB
171 );
172 }
173
a93bc191 »
2010-10-18 Create confirm function
174 /** Get onclick confirmation
175 * @return string
176 */
af4269f2 »
2013-07-11 Remove number of selected rows from confirmation
177 function confirm() {
178 return " onclick=\"return confirm('" . lang('Are you sure?') . "');\"";
a93bc191 »
2010-10-18 Create confirm function
179 }
180
4b405543 »
2011-07-27 Reorganize functions
181 /** Print header for hidden fieldset (close by </div></fieldset>)
182 * @param string
183 * @param string
184 * @param bool
185 * @param string
186 * @return null
187 */
188 function print_fieldset($id, $legend, $visible = false, $onclick = "") {
189 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";
190 }
191
192 /** Return class='active' if $bold is true
193 * @param bool
8d9ef7af »
2013-10-24 Differentiate views in navigation (thanks to Petr Kobelka)
194 * @param string
4b405543 »
2011-07-27 Reorganize functions
195 * @return string
196 */
8d9ef7af »
2013-10-24 Differentiate views in navigation (thanks to Petr Kobelka)
197 function bold($bold, $class = "") {
198 return ($bold ? " class='active $class'" : ($class ? " class='$class'" : ""));
4b405543 »
2011-07-27 Reorganize functions
199 }
200
201 /** Generate class for odd rows
202 * @param string return this for odd rows, empty to reset counter
203 * @return string
204 */
205 function odd($return = ' class="odd"') {
206 static $i = 0;
207 if (!$return) { // reset counter
208 $i = -1;
209 }
210 return ($i++ % 2 ? $return : '');
211 }
212
28f21aaa »
2010-10-22 Escape JavaScript strings (bug #3093243)
213 /** Escape string for JavaScript apostrophes
214 * @param string
215 * @return string
216 */
217 function js_escape($string) {
218 return addcslashes($string, "\r\n'\\/"); // slash for <script>
219 }
220
4b405543 »
2011-07-27 Reorganize functions
221 /** Print one row in JSON object
222 * @param string or "" to close the object
223 * @param string
224 * @return null
225 */
226 function json_row($key, $val = null) {
227 static $first = true;
228 if ($first) {
229 echo "{";
230 }
231 if ($key != "") {
be6b7c6c »
2013-07-12 Allow using json_row() in HTML
232 echo ($first ? "" : ",") . "\n\t\"" . addcslashes($key, "\r\n\"\\/") . '": ' . ($val !== null ? '"' . addcslashes($val, "\r\n\"\\/") . '"' : 'undefined');
4b405543 »
2011-07-27 Reorganize functions
233 $first = false;
234 } else {
235 echo "\n}\n";
236 $first = true;
237 }
238 }
239
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
240 /** Get INI boolean value
241 * @param string
242 * @return bool
243 */
244 function ini_bool($ini) {
245 $val = ini_get($ini);
e8c6ef04 »
2013-07-24 Replace ereg*() by preg_*()
246 return (preg_match('~^(on|true|yes)$~i', $val) || (int) $val); // boolean values set by php_value are strings
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
247 }
248
f30fd121 »
2010-12-20 Work without session.use_cookies (bug #3138640)
249 /** Check if SID is neccessary
250 * @return bool
251 */
252 function sid() {
876af589 »
2010-12-29 Fix sid function
253 static $return;
6591d485 »
2012-05-13 Replace isset($var) by $var !== null
254 if ($return === null) { // restart_session() defines SID
876af589 »
2010-12-29 Fix sid function
255 $return = (SID && !($_COOKIE && ini_bool("session.use_cookies"))); // $_COOKIE - don't pass SID with permanent login
256 }
257 return $return;
f30fd121 »
2010-12-20 Work without session.use_cookies (bug #3138640)
258 }
259
1602db30 »
2014-02-19 Document password functions
260 /** Set password to session
261 * @param string
262 * @param string
263 * @param string
264 * @param string
265 * @return null
266 */
267 function set_password($vendor, $server, $username, $password) {
268 $_SESSION["pwds"][$vendor][$server][$username] = ($_COOKIE["adminer_key"] && is_string($password)
269 ? array(encrypt_string($password, $_COOKIE["adminer_key"]))
270 : $password
271 );
272 }
273
274 /** Get password from session
275 * @return string
276 */
277 function get_password() {
278 $return = get_session("pwds");
279 if (is_array($return)) {
280 $return = ($_COOKIE["adminer_key"]
281 ? decrypt_string($return[0], $_COOKIE["adminer_key"])
282 : false
283 );
284 }
285 return $return;
286 }
287
ed130065 »
2014-01-11 MongoDB: Don't store unset values
288 /** Shortcut for $driver->quote($string)
f30fd121 »
2010-12-20 Work without session.use_cookies (bug #3138640)
289 * @param string
290 * @return string
291 */
7e644b43 »
2010-10-13 Save bytes ($connection->quote shortcut)
292 function q($string) {
ed130065 »
2014-01-11 MongoDB: Don't store unset values
293 global $driver;
294 return $driver->quote($string);
7e644b43 »
2010-10-13 Save bytes ($connection->quote shortcut)
295 }
296
9a176b0c » jakubvrana
2009-08-31 Comments
297 /** Get list of values from database
298 * @param string
299 * @param mixed
300 * @return array
301 */
0bd864ea » jakubvrana
2009-05-21 Allow specifying column in get_vals
302 function get_vals($query, $column = 0) {
64d616c0 » jakubvrana
2009-09-22 Rename get_dbh to connection
303 global $connection;
7769f2c6 » jakubvrana
2007-07-11 Separate get_vals
304 $return = array();
64d616c0 » jakubvrana
2009-09-22 Rename get_dbh to connection
305 $result = $connection->query($query);
b84252fc » jakubvrana
2010-04-21 Display SQLite compile options
306 if (is_object($result)) {
49f7886f » jakubvrana
2008-09-30 SHOW DATABASES can be revoked
307 while ($row = $result->fetch_row()) {
0bd864ea » jakubvrana
2009-05-21 Allow specifying column in get_vals
308 $return[] = $row[$column];
49f7886f » jakubvrana
2008-09-30 SHOW DATABASES can be revoked
309 }
7769f2c6 » jakubvrana
2007-07-11 Separate get_vals
310 }
311 return $return;
312 }
313
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
314 /** Get keys from first column and values from second
315 * @param string
316 * @param Min_DB
d17b17e5 »
2013-07-06 Driver for SimpleDB
317 * @param float
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
318 * @return array
319 */
d17b17e5 »
2013-07-06 Driver for SimpleDB
320 function get_key_vals($query, $connection2 = null, $timeout = 0) {
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
321 global $connection;
322 if (!is_object($connection2)) {
323 $connection2 = $connection;
324 }
325 $return = array();
d17b17e5 »
2013-07-06 Driver for SimpleDB
326 $connection2->timeout = $timeout;
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
327 $result = $connection2->query($query);
d17b17e5 »
2013-07-06 Driver for SimpleDB
328 $connection2->timeout = 0;
e26b1864 »
2011-01-31 Avoid fatal error (bug #3110257)
329 if (is_object($result)) {
330 while ($row = $result->fetch_row()) {
331 $return[$row[0]] = $row[1];
332 }
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
333 }
334 return $return;
335 }
336
b0d637b6 »
2010-10-13 Avoid fatal errors
337 /** Get all rows of result
338 * @param string
6bcbb0f1 »
2012-01-06 Don't report previous error on primary connection
339 * @param Min_DB
340 * @param string
b0d637b6 »
2010-10-13 Avoid fatal errors
341 * @return array associative
342 */
96544baf »
2010-10-13 Report errors in get_rows()
343 function get_rows($query, $connection2 = null, $error = "<p class='error'>") {
b0d637b6 »
2010-10-13 Avoid fatal errors
344 global $connection;
6bcbb0f1 »
2012-01-06 Don't report previous error on primary connection
345 $conn = (is_object($connection2) ? $connection2 : $connection);
b0d637b6 »
2010-10-13 Avoid fatal errors
346 $return = array();
6bcbb0f1 »
2012-01-06 Don't report previous error on primary connection
347 $result = $conn->query($query);
b0d637b6 »
2010-10-13 Avoid fatal errors
348 if (is_object($result)) { // can return true
349 while ($row = $result->fetch_assoc()) {
350 $return[] = $row;
351 }
6bcbb0f1 »
2012-01-06 Don't report previous error on primary connection
352 } elseif (!$result && !is_object($connection2) && $error && defined("PAGE_HEADER")) {
96544baf »
2010-10-13 Report errors in get_rows()
353 echo $error . error() . "\n";
b0d637b6 »
2010-10-13 Avoid fatal errors
354 }
355 return $return;
356 }
357
9a176b0c » jakubvrana
2009-08-31 Comments
358 /** Find unique identifier of a row
359 * @param array
360 * @param array result of indexes()
af30f597 »
2013-05-11 Don't use LIMIT 1 if updating unique row (bug #3613109)
361 * @return array or null if there is no unique identifier
9a176b0c » jakubvrana
2009-08-31 Comments
362 */
35ec64c6 » jakubvrana
2010-04-02 Link COUNT(*) result to listing
363 function unique_array($row, $indexes) {
720f5fc8 » jakubvrana
2007-07-05 Restructure indexes
364 foreach ($indexes as $index) {
e8c6ef04 »
2013-07-24 Replace ereg*() by preg_*()
365 if (preg_match("~PRIMARY|UNIQUE~", $index["type"])) {
720f5fc8 » jakubvrana
2007-07-05 Restructure indexes
366 $return = array();
367 foreach ($index["columns"] as $key) {
64ba9242 » jakubvrana
2009-06-21 Comments
368 if (!isset($row[$key])) { // NULL is ambiguous
720f5fc8 » jakubvrana
2007-07-05 Restructure indexes
369 continue 2;
667bfec4 » jakubvrana
2007-07-02 Decomposition
370 }
35ec64c6 » jakubvrana
2010-04-02 Link COUNT(*) result to listing
371 $return[$key] = $row[$key];
667bfec4 » jakubvrana
2007-07-02 Decomposition
372 }
720f5fc8 » jakubvrana
2007-07-05 Restructure indexes
373 return $return;
667bfec4 » jakubvrana
2007-07-02 Decomposition
374 }
375 }
430034c3 » jakubvrana
2007-07-09 Cottage homework
376 }
377
7e3f2d9b »
2014-06-26 Fix edit by long non-utf8 string (thanks Robert Vlach)
378 /** Escape column key used in where()
379 * @param string
380 * @return string
381 */
382 function escape_key($key) {
383 if (preg_match('(^([\w(]+)(' . str_replace("_", ".*", preg_quote(idf_escape("_"))) . ')([ \w)]+)$)', $key, $match)) { //! columns looking like functions
384 return $match[1] . idf_escape(idf_unescape($match[2])) . $match[3]; //! SQL injection
385 }
386 return idf_escape($key);
387 }
388
9a176b0c » jakubvrana
2009-08-31 Comments
389 /** Create SQL condition from parsed query string
390 * @param array parsed query string
2703eb96 »
2012-12-11 Allow editing values with significant binary column (bug #3572781)
391 * @param array
9a176b0c » jakubvrana
2009-08-31 Comments
392 * @return string
393 */
2703eb96 »
2012-12-11 Allow editing values with significant binary column (bug #3572781)
394 function where($where, $fields = array()) {
8bd3dca2 »
2014-06-26 Use utf8mb4 on more places
395 global $connection, $jush;
430034c3 » jakubvrana
2007-07-09 Cottage homework
396 $return = array();
341362a8 » jakubvrana
2010-04-23 Driver specific INSERT INTO
397 foreach ((array) $where["where"] as $key => $val) {
51b71043 »
2013-01-23 Allow editing function results
398 $key = bracket_escape($key, 1); // 1 - back
7e3f2d9b »
2014-06-26 Fix edit by long non-utf8 string (thanks Robert Vlach)
399 $column = escape_key($key);
7dd90f56 »
2013-06-04 MySQL: Speed up updating rows without numeric or UTF-8 primary key
400 $return[] = $column
e8c6ef04 »
2013-07-24 Replace ereg*() by preg_*()
401 . (($jush == "sql" && preg_match('~^[0-9]*\\.[0-9]*$~', $val)) || $jush == "mssql"
7dd90f56 »
2013-06-04 MySQL: Speed up updating rows without numeric or UTF-8 primary key
402 ? " LIKE " . q(addcslashes($val, "%_\\"))
403 : " = " . unconvert_field($fields[$key], q($val))
404 ) // LIKE because of floats but slow with ints, in MS SQL because of text
66ff1531 » jakubvrana
2010-04-26 Use bracket_escape function
405 ; //! enum and set
4d8667db »
2014-02-23 MySQL: Fix editing rows by binary values (bug #371)
406 if ($jush == "sql" && preg_match('~char|text~', $fields[$key]["type"]) && preg_match("~[^ -@]~", $val)) { // not just [a-z] to catch non-ASCII characters
8bd3dca2 »
2014-06-26 Use utf8mb4 on more places
407 $return[] = "$column = " . q($val) . " COLLATE " . charset($connection) . "_bin";
7dd90f56 »
2013-06-04 MySQL: Speed up updating rows without numeric or UTF-8 primary key
408 }
341362a8 » jakubvrana
2010-04-23 Driver specific INSERT INTO
409 }
410 foreach ((array) $where["null"] as $key) {
7e3f2d9b »
2014-06-26 Fix edit by long non-utf8 string (thanks Robert Vlach)
411 $return[] = escape_key($key) . " IS NULL";
667bfec4 » jakubvrana
2007-07-02 Decomposition
412 }
c16c57b1 » jakubvrana
2009-07-11 E-mail sending
413 return implode(" AND ", $return);
667bfec4 » jakubvrana
2007-07-02 Decomposition
414 }
415
9a176b0c » jakubvrana
2009-08-31 Comments
416 /** Create SQL condition from query string
417 * @param string
2703eb96 »
2012-12-11 Allow editing values with significant binary column (bug #3572781)
418 * @param array
9a176b0c » jakubvrana
2009-08-31 Comments
419 * @return string
420 */
2703eb96 »
2012-12-11 Allow editing values with significant binary column (bug #3572781)
421 function where_check($val, $fields = array()) {
0cd2afbb » jakubvrana
2009-06-07 Remove single item clone
422 parse_str($val, $check);
72f4d9e2 »
2010-07-26 Function parse_str respects magic_quotes_gpc (bug #3034575)
423 remove_slashes(array(&$check));
2703eb96 »
2012-12-11 Allow editing values with significant binary column (bug #3572781)
424 return where($check, $fields);
0cd2afbb » jakubvrana
2009-06-07 Remove single item clone
425 }
426
9a176b0c » jakubvrana
2009-08-31 Comments
427 /** Create query string where condition from value
428 * @param int condition order
429 * @param string column identifier
430 * @param string
534f4ee5 »
2012-09-16 Fix doc comments
431 * @param string
186c0589 » jakubvrana
2010-04-15 Utilize where_link function
432 * @return string
9a176b0c » jakubvrana
2009-08-31 Comments
433 */
186c0589 » jakubvrana
2010-04-15 Utilize where_link function
434 function where_link($i, $column, $value, $operator = "=") {
6591d485 »
2012-05-13 Replace isset($var) by $var !== null
435 return "&where%5B$i%5D%5Bcol%5D=" . urlencode($column) . "&where%5B$i%5D%5Bop%5D=" . urlencode(($value !== null ? $operator : "IS NULL")) . "&where%5B$i%5D%5Bval%5D=" . urlencode($value);
c42c46a2 » jakubvrana
2009-07-15 Table relations
436 }
437
7541ceb1 »
2013-04-26 Improve export of binary data types (bug #3526494)
438 /** Get select clause for convertible fields
439 * @param array
440 * @param array
441 * @param array
442 * @return string
443 */
444 function convert_fields($columns, $fields, $select = array()) {
445 $return = "";
446 foreach ($columns as $key => $val) {
447 if ($select && !in_array(idf_escape($key), $select)) {
448 continue;
449 }
450 $as = convert_field($fields[$key]);
451 if ($as) {
452 $return .= ", $as AS " . idf_escape($key);
453 }
454 }
455 return $return;
456 }
457
61606040 »
2013-08-11 Encrypt passwords stored in session by a key stored in cookie (thanks…
458 /** Set cookie valid on current path
319cba4b » jakubvrana
2009-09-09 Save e-mail from to cookie
459 * @param string
460 * @param string
61606040 »
2013-08-11 Encrypt passwords stored in session by a key stored in cookie (thanks…
461 * @param int number of seconds, 0 for session cookie
319cba4b » jakubvrana
2009-09-09 Save e-mail from to cookie
462 * @return bool
463 */
61606040 »
2013-08-11 Encrypt passwords stored in session by a key stored in cookie (thanks…
464 function cookie($name, $value, $lifetime = 2592000) { // 2592000 - 30 days
2cec7584 »
2010-05-25 Direct links from HTTPS to HTTP
465 global $HTTPS;
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
466 $params = array(
467 $name,
e8c6ef04 »
2013-07-24 Replace ereg*() by preg_*()
468 (preg_match("~\n~", $value) ? "" : $value), // HTTP Response Splitting protection in PHP < 5.1.2
61606040 »
2013-08-11 Encrypt passwords stored in session by a key stored in cookie (thanks…
469 ($lifetime ? time() + $lifetime : 0),
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
470 preg_replace('~\\?.*~', '', $_SERVER["REQUEST_URI"]),
471 "",
2cec7584 »
2010-05-25 Direct links from HTTPS to HTTP
472 $HTTPS
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
473 );
c2334dbc » jakubvrana
2009-12-18 Secure cookies
474 if (version_compare(PHP_VERSION, '5.2.0') >= 0) {
475 $params[] = true; // HttpOnly
476 }
477 return call_user_func_array('setcookie', $params);
319cba4b » jakubvrana
2009-09-09 Save e-mail from to cookie
478 }
479
f9a20232 » jakubvrana
2009-11-02 Improve concurrency
480 /** Restart stopped session
481 * @return null
482 */
483 function restart_session() {
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
484 if (!ini_bool("session.use_cookies")) {
f9a20232 » jakubvrana
2009-11-02 Improve concurrency
485 session_start();
486 }
487 }
488
5e0038b6 »
2012-08-19 Improve session management
489 /** Stop session if it would be possible to restart it later
490 * @return null
491 */
492 function stop_session() {
493 if (!ini_bool("session.use_cookies")) {
494 session_write_close();
495 }
496 }
497
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
498 /** Get session variable for current server
499 * @param string
500 * @return mixed
501 */
502 function &get_session($key) {
503 return $_SESSION[$key][DRIVER][SERVER][$_GET["username"]];
504 }
505
506 /** Set session variable for current server
507 * @param string
508 * @param mixed
509 * @return mixed
510 */
511 function set_session($key, $val) {
512 $_SESSION[$key][DRIVER][SERVER][$_GET["username"]] = $val; // used also in auth.inc.php
513 }
514
605b0930 »
2010-05-06 List authentications
515 /** Get authenticated URL
516 * @param string
517 * @param string
518 * @param string
8be29afb »
2012-05-14 Allow specifying database in login form (bug #3499359)
519 * @param string
605b0930 »
2010-05-06 List authentications
520 * @return string
521 */
48356d8d »
2013-07-05 Rename variable
522 function auth_url($vendor, $server, $username, $db = null) {
605b0930 »
2010-05-06 List authentications
523 global $drivers;
8be29afb »
2012-05-14 Allow specifying database in login form (bug #3499359)
524 preg_match('~([^?]*)\\??(.*)~', remove_from_uri(implode("|", array_keys($drivers)) . "|username|" . ($db !== null ? "db|" : "") . session_name()), $match);
605b0930 »
2010-05-06 List authentications
525 return "$match[1]?"
f30fd121 »
2010-12-20 Work without session.use_cookies (bug #3138640)
526 . (sid() ? SID . "&" : "")
48356d8d »
2013-07-05 Rename variable
527 . ($vendor != "server" || $server != "" ? urlencode($vendor) . "=" . urlencode($server) . "&" : "")
605b0930 »
2010-05-06 List authentications
528 . "username=" . urlencode($username)
8be29afb »
2012-05-14 Allow specifying database in login form (bug #3499359)
529 . ($db != "" ? "&db=" . urlencode($db) : "")
605b0930 »
2010-05-06 List authentications
530 . ($match[2] ? "&$match[2]" : "")
531 ;
532 }
533
390e38b8 »
2010-10-18 Load long texts for inline-edit by AJAX
534 /** Find whether it is an AJAX request
535 * @return bool
536 */
537 function is_ajax() {
fdea1970 »
2011-01-25 Simplify AJAX redirect
538 return ($_SERVER["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest");
390e38b8 »
2010-10-18 Load long texts for inline-edit by AJAX
539 }
540
9a176b0c » jakubvrana
2009-08-31 Comments
541 /** Send Location header and exit
9bb1909f » jakubvrana
2010-04-15 Fix Save and continue edit
542 * @param string null to only set a message
9a176b0c » jakubvrana
2009-08-31 Comments
543 * @param string
544 * @return null
545 */
430034c3 » jakubvrana
2007-07-09 Cottage homework
546 function redirect($location, $message = null) {
6591d485 »
2012-05-13 Replace isset($var) by $var !== null
547 if ($message !== null) {
be49e08b » jakubvrana
2009-11-03 Improve session restarting
548 restart_session();
6591d485 »
2012-05-13 Replace isset($var) by $var !== null
549 $_SESSION["messages"][preg_replace('~^[^?]*~', '', ($location !== null ? $location : $_SERVER["REQUEST_URI"]))][] = $message;
430034c3 » jakubvrana
2007-07-09 Cottage homework
550 }
6591d485 »
2012-05-13 Replace isset($var) by $var !== null
551 if ($location !== null) {
b5358536 »
2010-11-12 Full AJAX only with pushState to work correctly with history
552 if ($location == "") {
553 $location = ".";
554 }
4815c7d0 »
2012-04-15 Don't use AJAX links and forms
555 header("Location: $location");
38894b29 »
2010-11-23 Send all forms by AJAX
556 exit;
9bb1909f » jakubvrana
2010-04-15 Fix Save and continue edit
557 }
430034c3 » jakubvrana
2007-07-09 Cottage homework
558 }
559
9a176b0c » jakubvrana
2009-08-31 Comments
560 /** Execute query and redirect if successful
561 * @param string
562 * @param string
563 * @param string
564 * @param bool
565 * @param bool
566 * @param bool
0c8cd8e5 »
2014-04-18 Fix doc comment
567 * @param string
9a176b0c » jakubvrana
2009-08-31 Comments
568 * @return bool
569 */
6a3ede75 »
2014-03-07 Print elapsed time in HTML instead of SQL command comment
570 function query_redirect($query, $location, $message, $redirect = true, $execute = true, $failed = false, $time = "") {
64d616c0 » jakubvrana
2009-09-22 Rename get_dbh to connection
571 global $connection, $error, $adminer;
f9a20232 » jakubvrana
2009-11-02 Improve concurrency
572 if ($execute) {
28856804 »
2013-08-08 Simplify format_time()
573 $start = microtime(true);
f9a20232 » jakubvrana
2009-11-02 Improve concurrency
574 $failed = !$connection->query($query);
fc668ea3 »
2014-03-11 Simplify format_time()
575 $time = format_time($start);
f9a20232 » jakubvrana
2009-11-02 Improve concurrency
576 }
10bdbed1 » jakubvrana
2009-06-04 Save queries to history
577 $sql = "";
578 if ($query) {
6a3ede75 »
2014-03-07 Print elapsed time in HTML instead of SQL command comment
579 $sql = $adminer->messageQuery($query, $time);
10bdbed1 » jakubvrana
2009-06-04 Save queries to history
580 }
748ee836 » jakubvrana
2008-09-03 Print SQL command with multiple queries
581 if ($failed) {
c1130ed5 » jakubvrana
2009-12-17 Simplify SQL syntax errors everywhere
582 $error = error() . $sql;
c48a017e » jakubvrana
2008-09-02 Print SQL query by error
583 return false;
f91b2c4b » jakubvrana
2008-09-02 Print SQL query by message
584 }
c48a017e » jakubvrana
2008-09-02 Print SQL query by error
585 if ($redirect) {
586 redirect($location, $message . $sql);
587 }
588 return true;
f91b2c4b » jakubvrana
2008-09-02 Print SQL query by message
589 }
590
9a176b0c » jakubvrana
2009-08-31 Comments
591 /** Execute and remember query
6a3ede75 »
2014-03-07 Print elapsed time in HTML instead of SQL command comment
592 * @param string or null to return remembered queries, end with ';' to use DELIMITER
fc668ea3 »
2014-03-11 Simplify format_time()
593 * @return Min_Result or array($queries, $time) if $query = null
9a176b0c » jakubvrana
2009-08-31 Comments
594 */
6a3ede75 »
2014-03-07 Print elapsed time in HTML instead of SQL command comment
595 function queries($query) {
64d616c0 » jakubvrana
2009-09-22 Rename get_dbh to connection
596 global $connection;
748ee836 » jakubvrana
2008-09-03 Print SQL command with multiple queries
597 static $queries = array();
6a3ede75 »
2014-03-07 Print elapsed time in HTML instead of SQL command comment
598 static $start;
599 if (!$start) {
600 $start = microtime(true);
601 }
6591d485 »
2012-05-13 Replace isset($var) by $var !== null
602 if ($query === null) {
6a3ede75 »
2014-03-07 Print elapsed time in HTML instead of SQL command comment
603 // return executed queries
fc668ea3 »
2014-03-11 Simplify format_time()
604 return array(implode("\n", $queries), format_time($start));
748ee836 » jakubvrana
2008-09-03 Print SQL command with multiple queries
605 }
6a3ede75 »
2014-03-07 Print elapsed time in HTML instead of SQL command comment
606 $queries[] = (preg_match('~;$~', $query) ? "DELIMITER ;;\n$query;\nDELIMITER " : $query) . ";";
607 return $connection->query($query);
748ee836 » jakubvrana
2008-09-03 Print SQL command with multiple queries
608 }
609
f9bb1c5c »
2010-05-17 Introduce apply_queries function
610 /** Apply command to all array items
611 * @param string
612 * @param array
613 * @param callback
614 * @return bool
615 */
616 function apply_queries($query, $tables, $escape = 'table') {
617 foreach ($tables as $table) {
618 if (!queries("$query " . $escape($table))) {
619 return false;
620 }
621 }
622 return true;
623 }
624
dbdd40a1 » jakubvrana
2009-10-13 Introduce queries_redirect function
625 /** Redirect by remembered queries
626 * @param string
627 * @param string
628 * @param bool
52b9820e »
2010-10-15 Comment
629 * @return bool
dbdd40a1 » jakubvrana
2009-10-13 Introduce queries_redirect function
630 */
631 function queries_redirect($location, $message, $redirect) {
6a3ede75 »
2014-03-07 Print elapsed time in HTML instead of SQL command comment
632 list($queries, $time) = queries(null);
633 return query_redirect($queries, $location, $message, $redirect, false, !$redirect, $time);
dbdd40a1 » jakubvrana
2009-10-13 Introduce queries_redirect function
634 }
635
fc668ea3 »
2014-03-11 Simplify format_time()
636 /** Format elapsed time
637 * @param float output of microtime(true)
6a41240c »
2013-05-08 Move common function
638 * @return string HTML code
639 */
fc668ea3 »
2014-03-11 Simplify format_time()
640 function format_time($start) {
641 return lang('%.3f s', max(0, microtime(true) - $start));
6a41240c »
2013-05-08 Move common function
642 }
643
9a176b0c » jakubvrana
2009-08-31 Comments
644 /** Remove parameter from query string
645 * @param string
646 * @return string
647 */
d41d4460 » jakubvrana
2007-07-27 remove_from_uri()
648 function remove_from_uri($param = "") {
483d7dc0 » jakubvrana
2009-12-08 Remove session_name() only without SID (bug #2910681)
649 return substr(preg_replace("~(?<=[?&])($param" . (SID ? "" : "|" . session_name()) . ")=[^&]*&~", '', "$_SERVER[REQUEST_URI]&"), 0, -1);
d41d4460 » jakubvrana
2007-07-27 remove_from_uri()
650 }
651
9a176b0c » jakubvrana
2009-08-31 Comments
652 /** Generate page number for pagination
653 * @param int
534f4ee5 »
2012-09-16 Fix doc comments
654 * @param int
9a176b0c » jakubvrana
2009-08-31 Comments
655 * @return string
656 */
bd252953 » jakubvrana
2010-04-22 Don't redirect from last page
657 function pagination($page, $current) {
d17b17e5 »
2013-07-06 Driver for SimpleDB
658 return " " . ($page == $current
659 ? $page + 1
660 : '<a href="' . h(remove_from_uri("page") . ($page ? "&page=$page" . ($_GET["next"] ? "&next=" . urlencode($_GET["next"]) : "") : "")) . '">' . ($page + 1) . "</a>"
661 );
1456283e » jakubvrana
2008-08-07 Pages on last page
662 }
663
3e9b1ba1 » jakubvrana
2009-11-21 Don't store files in hidden fields
664 /** Get file contents from $_FILES
9a176b0c » jakubvrana
2009-08-31 Comments
665 * @param string
666 * @param bool
3e9b1ba1 » jakubvrana
2009-11-21 Don't store files in hidden fields
667 * @return mixed int for error, string otherwise
9a176b0c » jakubvrana
2009-08-31 Comments
668 */
25f01d35 » jakubvrana
2009-08-28 Compress export and import
669 function get_file($key, $decompress = false) {
670 $file = $_FILES[$key];
b0b4cb15 »
2013-04-26 Allow more SQL files to be uploaded at the same time (thanks to Frant…
671 if (!$file) {
672 return null;
430034c3 » jakubvrana
2007-07-09 Cottage homework
673 }
b0b4cb15 »
2013-04-26 Allow more SQL files to be uploaded at the same time (thanks to Frant…
674 foreach ($file as $key => $val) {
675 $file[$key] = (array) $val;
676 }
7f05141b »
2013-04-28 Save memory in get_file()
677 $return = '';
b0b4cb15 »
2013-04-26 Allow more SQL files to be uploaded at the same time (thanks to Frant…
678 foreach ($file["error"] as $key => $error) {
679 if ($error) {
680 return $error;
9ff10f83 »
2011-03-07 Parse UTF-16 and UTF-8 BOM in all text uploads
681 }
b0b4cb15 »
2013-04-26 Allow more SQL files to be uploaded at the same time (thanks to Frant…
682 $name = $file["name"][$key];
683 $tmp_name = $file["tmp_name"][$key];
e8c6ef04 »
2013-07-24 Replace ereg*() by preg_*()
684 $content = file_get_contents($decompress && preg_match('~\\.gz$~', $name)
1ecdde05 »
2013-04-29 Remove bzip2 compression support
685 ? "compress.zlib://$tmp_name"
b0b4cb15 »
2013-04-26 Allow more SQL files to be uploaded at the same time (thanks to Frant…
686 : $tmp_name
1ecdde05 »
2013-04-29 Remove bzip2 compression support
687 ); //! may not be reachable because of open_basedir
b0b4cb15 »
2013-04-26 Allow more SQL files to be uploaded at the same time (thanks to Frant…
688 if ($decompress) {
689 $start = substr($content, 0, 3);
e8c6ef04 »
2013-07-24 Replace ereg*() by preg_*()
690 if (function_exists("iconv") && preg_match("~^\xFE\xFF|^\xFF\xFE~", $start, $regs)) { // not ternary operator to save memory
b0b4cb15 »
2013-04-26 Allow more SQL files to be uploaded at the same time (thanks to Frant…
691 $content = iconv("utf-16", "utf-8", $content);
692 } elseif ($start == "\xEF\xBB\xBF") { // UTF-8 BOM
693 $content = substr($content, 3);
694 }
feeda916 »
2013-07-13 Don't append newlines to uploaded files
695 $return .= $content . "\n\n";
44ad4e17 »
2014-01-10 Don't print query in edit
696 } else {
697 $return .= $content;
b0b4cb15 »
2013-04-26 Allow more SQL files to be uploaded at the same time (thanks to Frant…
698 }
9ff10f83 »
2011-03-07 Parse UTF-16 and UTF-8 BOM in all text uploads
699 }
b0b4cb15 »
2013-04-26 Allow more SQL files to be uploaded at the same time (thanks to Frant…
700 //! support SQL files not ending with semicolon
7f05141b »
2013-04-28 Save memory in get_file()
701 return $return;
430034c3 » jakubvrana
2007-07-09 Cottage homework
702 }
703
9a176b0c » jakubvrana
2009-08-31 Comments
704 /** Determine upload error
705 * @param int
706 * @return string
707 */
e2a03c08 » jakubvrana
2009-07-16 Show upload_max_filesize
708 function upload_error($error) {
9d944c8f »
2011-08-24 Respect original memory_limit
709 $max_size = ($error == UPLOAD_ERR_INI_SIZE ? ini_get("upload_max_filesize") : 0); // post_max_size is checked in index.php
b2a7c7ac » jakubvrana
2009-08-25 Execute SQL file stored on server disk
710 return ($error ? lang('Unable to upload a file.') . ($max_size ? " " . lang('Maximum allowed file size is %sB.', $max_size) : "") : lang('File does not exist.'));
e2a03c08 » jakubvrana
2009-07-16 Show upload_max_filesize
711 }
712
4b405543 »
2011-07-27 Reorganize functions
713 /** Create repeat pattern for preg
c313dd83 »
2010-10-19 Deferred operations by AJAX instead of JS (doesn't require sending to…
714 * @param string
4b405543 »
2011-07-27 Reorganize functions
715 * @param int
716 * @return string
c313dd83 »
2010-10-19 Deferred operations by AJAX instead of JS (doesn't require sending to…
717 */
4b405543 »
2011-07-27 Reorganize functions
718 function repeat_pattern($pattern, $length) {
719 // fix for Compilation failed: number too big in {} quantifier
720 return str_repeat("$pattern{0,65535}", $length / 65535) . "$pattern{0," . ($length % 65535) . "}"; // can create {0,0} which is OK
c313dd83 »
2010-10-19 Deferred operations by AJAX instead of JS (doesn't require sending to…
721 }
722
9a176b0c » jakubvrana
2009-08-31 Comments
723 /** Check whether the string is in UTF-8
724 * @param string
725 * @return bool
726 */
017ffb51 » jakubvrana
2009-06-11 Better UTF-8 detection
727 function is_utf8($val) {
64ba9242 » jakubvrana
2009-06-21 Comments
728 // don't print control chars except \t\r\n
017ffb51 » jakubvrana
2009-06-11 Better UTF-8 detection
729 return (preg_match('~~u', $val) && !preg_match('~[\\0-\\x8\\xB\\xC\\xE-\\x1F]~', $val));
730 }
731
9a176b0c » jakubvrana
2009-08-31 Comments
732 /** Shorten UTF-8 string
733 * @param string
734 * @param int
735 * @param string
736 * @return string escaped string with appended ...
737 */
cd075842 » jakubvrana
2009-06-15 Save SQL query to history
738 function shorten_utf8($string, $length = 80, $suffix = "") {
13155372 »
2011-01-10 No error for big text length (bug #264133)
739 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
740 preg_match("(^(" . repeat_pattern("[\t\r\n -~]", $length) . ")($)?)", $string, $match);
95764b01 » jakubvrana
2009-08-25 Shorten binary strings
741 }
687b3fdd »
2010-05-20 Replace <strong> by <b> and <em> by <i>
742 return h($match[1]) . $suffix . (isset($match[2]) ? "" : "<i>...</i>");
02a851c7 » jakubvrana
2007-08-24 Shorten texts in PHP
743 }
3920ebbe » jakubvrana
2008-10-03 Remove InnoDB table comment in Tables and views
744
4d491d70 »
2014-03-19 Translate numbers in ar, bn, fa
745 /** Format decimal number
746 * @param int
747 * @return string
748 */
749 function format_number($val) {
750 return strtr(number_format($val, 0, ".", lang(',')), preg_split('~~u', lang('0123456789'), -1, PREG_SPLIT_NO_EMPTY));
751 }
752
9a176b0c » jakubvrana
2009-08-31 Comments
753 /** Generate friendly URL
754 * @param string
755 * @return string
756 */
82518960 » jakubvrana
2009-06-11 Downloaded file name
757 function friendly_url($val) {
64ba9242 » jakubvrana
2009-06-21 Comments
758 // used for blobs and export
82518960 » jakubvrana
2009-06-11 Downloaded file name
759 return preg_replace('~[^a-z0-9_]~i', '-', $val);
760 }
761
9a176b0c » jakubvrana
2009-08-31 Comments
762 /** Print hidden fields
763 * @param array
764 * @param array
765 * @return null
766 */
820c8820 » jakubvrana
2008-10-09 Bulk clone
767 function hidden_fields($process, $ignore = array()) {
5d7071a5 » jakubvrana
2008-10-03 Choose language through option-list
768 while (list($key, $val) = each($process)) {
619b49c3 »
2014-03-21 Allow arrays to be ignored in hidden_fields()
769 if (!in_array($key, $ignore)) {
770 if (is_array($val)) {
771 foreach ($val as $k => $v) {
772 $process[$key . "[$k]"] = $v;
773 }
774 } else {
775 echo '<input type="hidden" name="' . h($key) . '" value="' . h($val) . '">';
5d7071a5 » jakubvrana
2008-10-03 Choose language through option-list
776 }
777 }
778 }
779 }
61640ee6 » jakubvrana
2009-07-03 Move common functions
780
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
781 /** Print hidden fields for GET forms
782 * @return null
783 */
784 function hidden_fields_get() {
f30fd121 »
2010-12-20 Work without session.use_cookies (bug #3138640)
785 echo (sid() ? '<input type="hidden" name="' . session_name() . '" value="' . h(session_id()) . '">' : '');
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
786 echo (SERVER !== null ? '<input type="hidden" name="' . DRIVER . '" value="' . h(SERVER) . '">' : "");
787 echo '<input type="hidden" name="username" value="' . h($_GET["username"]) . '">';
788 }
789
2e4a7121 »
2013-06-24 Fix title and links on invalid table pages
790 /** Get status of a single table and fall back to name on error
791 * @param string
792 * @param bool
793 * @return array
794 */
795 function table_status1($table, $fast = false) {
796 $return = table_status($table, $fast);
797 return ($return ? $return : array("Name" => $table));
798 }
799
9a176b0c » jakubvrana
2009-08-31 Comments
800 /** Find out foreign keys for each column
801 * @param string
802 * @return array array($col => array())
803 */
1e4d11e9 » jakubvrana
2009-07-23 Select from foreign keys in Editor
804 function column_foreign_keys($table) {
753909ec »
2010-10-29 Support for virtual foreign keys
805 global $adminer;
1e4d11e9 » jakubvrana
2009-07-23 Select from foreign keys in Editor
806 $return = array();
753909ec »
2010-10-29 Support for virtual foreign keys
807 foreach ($adminer->foreignKeys($table) as $foreign_key) {
1e4d11e9 » jakubvrana
2009-07-23 Select from foreign keys in Editor
808 foreach ($foreign_key["source"] as $val) {
809 $return[$val][] = $foreign_key;
810 }
811 }
812 return $return;
813 }
814
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
815 /** Print enum input field
816 * @param string "radio"|"checkbox"
817 * @param string
818 * @param array
819 * @param mixed int|string|array
684e70d0 »
2011-01-21 Search for empty enum
820 * @param string
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
821 * @return null
822 */
684e70d0 »
2011-01-21 Search for empty enum
823 function enum_input($type, $attrs, $field, $value, $empty = null) {
c0682062 »
2011-01-18 Use $adminer->editVal in enum_input function
824 global $adminer;
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
825 preg_match_all("~'((?:[^']|'')*)'~", $field["length"], $matches);
6591d485 »
2012-05-13 Replace isset($var) by $var !== null
826 $return = ($empty !== null ? "<label><input type='$type'$attrs value='$empty'" . ((is_array($value) ? in_array($empty, $value) : $value === 0) ? " checked" : "") . "><i>" . lang('empty') . "</i></label>" : "");
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
827 foreach ($matches[1] as $i => $val) {
828 $val = stripcslashes(str_replace("''", "'", $val));
829 $checked = (is_int($value) ? $value == $i+1 : (is_array($value) ? in_array($i+1, $value) : $value === $val));
c0682062 »
2011-01-18 Use $adminer->editVal in enum_input function
830 $return .= " <label><input type='$type'$attrs value='" . ($i+1) . "'" . ($checked ? ' checked' : '') . '>' . h($adminer->editVal($val, $field)) . '</label>';
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
831 }
fe069085 »
2010-10-23 Allow redefining editInput for enum (bug #3048711)
832 return $return;
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
833 }
834
9a176b0c » jakubvrana
2009-08-31 Comments
835 /** Print edit input field
836 * @param array one field from fields()
837 * @param mixed
838 * @param string
839 * @return null
840 */
1e4d11e9 » jakubvrana
2009-07-23 Select from foreign keys in Editor
841 function input($field, $value, $function) {
2b2d6987 »
2013-02-19 Compatibility with MySQL 5.6
842 global $connection, $types, $adminer, $jush;
689699aa » jakubvrana
2009-07-28 Shortcut for htmlspecialchars
843 $name = h(bracket_escape($field["field"]));
61640ee6 » jakubvrana
2009-07-03 Move common functions
844 echo "<td class='function'>";
6317c773 »
2013-08-09 NoSQL: Allow editing complex values
845 if (is_array($value) && !$function) {
846 $args = array($value);
847 if (version_compare(PHP_VERSION, 5.4) >= 0) {
848 $args[] = JSON_PRETTY_PRINT;
849 }
850 $value = call_user_func_array('json_encode', $args); //! requires PHP 5.2
851 $function = "json";
852 }
9f69d7ae »
2010-11-28 MS SQL: Do not update AI value
853 $reset = ($jush == "mssql" && $field["auto_increment"]);
854 if ($reset && !$_POST["save"]) {
855 $function = null;
856 }
857 $functions = (isset($_GET["select"]) || $reset ? array("orig" => lang('original')) : array()) + $adminer->editFunctions($field);
454c80f8 »
2010-09-09 Save bytes
858 $attrs = " name='fields[$name]'";
61640ee6 » jakubvrana
2009-07-03 Move common functions
859 if ($field["type"] == "enum") {
fe069085 »
2010-10-23 Allow redefining editInput for enum (bug #3048711)
860 echo nbsp($functions[""]) . "<td>" . $adminer->editInput($_GET["edit"], $field, $attrs, $value);
61640ee6 » jakubvrana
2009-07-03 Move common functions
861 } else {
99039467 » jakubvrana
2009-09-08 Use original values in Editor multi edit
862 $first = 0;
863 foreach ($functions as $key => $val) {
864 if ($key === "" || !$val) {
865 break;
866 }
867 $first++;
868 }
657191ee »
2013-08-09 Unselect original function on key up
869 $onchange = ($first ? " onchange=\"var f = this.form['function[" . h(js_escape(bracket_escape($field["field"]))) . "]']; if ($first > f.selectedIndex) f.selectedIndex = $first;\" onkeyup='keyupChange.call(this);'" : "");
454c80f8 »
2010-09-09 Save bytes
870 $attrs .= $onchange;
683f6eef »
2014-01-08 Don't use type=number if a SQL function is used
871 $has_function = (in_array($function, $functions) || isset($functions[$function]));
a338f9bf »
2013-07-19 Display help in tooltip
872 echo (count($functions) > 1
683f6eef »
2014-01-08 Don't use type=number if a SQL function is used
873 ? "<select name='function[$name]' onchange='functionChange(this);'" . on_help("getTarget(event).value.replace(/^SQL\$/, '')", 1) . ">" . optionlist($functions, $function === null || $has_function ? $function : "") . "</select>"
a338f9bf »
2013-07-19 Display help in tooltip
874 : nbsp(reset($functions))
875 ) . '<td>';
a5612526 » jakubvrana
2009-11-09 Use distinct name for blob inputs
876 $input = $adminer->editInput($_GET["edit"], $field, $attrs, $value); // usage in call is without a table
7352c281 » jakubvrana
2010-01-09 Replace strlen() by != ""
877 if ($input != "") {
e1abcda0 » jakubvrana
2009-07-28 Treat tinyint(1) as boolean
878 echo $input;
1e4d11e9 » jakubvrana
2009-07-23 Select from foreign keys in Editor
879 } elseif ($field["type"] == "set") { //! 64 bits
26c9d64b » jakubvrana
2009-08-19 Fix long SQL query crash (bug #2839231)
880 preg_match_all("~'((?:[^']|'')*)'~", $field["length"], $matches);
61640ee6 » jakubvrana
2009-07-03 Move common functions
881 foreach ($matches[1] as $i => $val) {
882 $val = stripcslashes(str_replace("''", "'", $val));
883 $checked = (is_int($value) ? ($value >> $i) & 1 : in_array($val, explode(",", $value), true));
045bce3c »
2011-02-10 Use editVal for set values
884 echo " <label><input type='checkbox' name='fields[$name][$i]' value='" . (1 << $i) . "'" . ($checked ? ' checked' : '') . "$onchange>" . h($adminer->editVal($val, $field)) . '</label>';
61640ee6 » jakubvrana
2009-07-03 Move common functions
885 }
e8c6ef04 »
2013-07-24 Replace ereg*() by preg_*()
886 } elseif (preg_match('~blob|bytea|raw|file~', $field["type"]) && ini_bool("file_uploads")) {
a5612526 » jakubvrana
2009-11-09 Use distinct name for blob inputs
887 echo "<input type='file' name='fields-$name'$onchange>";
e8c6ef04 »
2013-07-24 Replace ereg*() by preg_*()
888 } elseif (($text = preg_match('~text|lob~', $field["type"])) || preg_match("~\n~", $value)) {
f0d97a7c »
2012-08-11 Edit strings with \n in textarea
889 if ($text && $jush != "sqlite") {
890 $attrs .= " cols='50' rows='12'";
891 } else {
892 $rows = min(12, substr_count($value, "\n") + 1);
893 $attrs .= " cols='30' rows='$rows'" . ($rows == 1 ? " style='height: 1.2em;'" : ""); // 1.2em - line-height
894 }
895 echo "<textarea$attrs>" . h($value) . '</textarea>';
6317c773 »
2013-08-09 NoSQL: Allow editing complex values
896 } elseif ($function == "json") {
897 echo "<textarea$attrs cols='50' rows='12' class='jush-js'>" . h($value) . '</textarea>';
61640ee6 » jakubvrana
2009-07-03 Move common functions
898 } else {
899 // int(3) is only a display hint
e8c6ef04 »
2013-07-24 Replace ereg*() by preg_*()
900 $maxlength = (!preg_match('~int~', $field["type"]) && preg_match('~^(\\d+)(,(\\d+))?$~', $field["length"], $match) ? ((preg_match("~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));
901 if ($jush == 'sql' && $connection->server_info >= 5.6 && preg_match('~time~', $field["type"])) {
2b2d6987 »
2013-02-19 Compatibility with MySQL 5.6
902 $maxlength += 7; // microtime
903 }
2fb1ebc7 »
2012-12-05 HTML5 input fields
904 // type='date' and type='time' display localized value which may be confusing, type='datetime' uses 'T' as date and time separator
683f6eef »
2014-01-08 Don't use type=number if a SQL function is used
905 echo "<input"
906 . ((!$has_function || $function === "") && preg_match('~(?<!o)int~', $field["type"]) ? " type='number'" : "")
907 . " value='" . h($value) . "'" . ($maxlength ? " maxlength='$maxlength'" : "")
908 . (preg_match('~char|binary~', $field["type"]) && $maxlength > 20 ? " size='40'" : "")
909 . "$attrs>"
910 ;
61640ee6 » jakubvrana
2009-07-03 Move common functions
911 }
912 }
913 }
914
9a176b0c » jakubvrana
2009-08-31 Comments
915 /** Process edit input field
916 * @param one field from fields()
6317c773 »
2013-08-09 NoSQL: Allow editing complex values
917 * @return string or false to leave the original value
9a176b0c » jakubvrana
2009-08-31 Comments
918 */
a0def479 » jakubvrana
2009-07-27 Date localization
919 function process_input($field) {
7e644b43 »
2010-10-13 Save bytes ($connection->quote shortcut)
920 global $adminer;
a0def479 » jakubvrana
2009-07-27 Date localization
921 $idf = bracket_escape($field["field"]);
61640ee6 » jakubvrana
2009-07-03 Move common functions
922 $function = $_POST["function"][$idf];
923 $value = $_POST["fields"][$idf];
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
924 if ($field["type"] == "enum") {
925 if ($value == -1) {
926 return false;
927 }
928 if ($value == "") {
929 return "NULL";
930 }
04eccbad »
2010-10-22 Allow bigger numbers under 32 bits
931 return +$value;
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
932 }
933 if ($field["auto_increment"] && $value == "") {
934 return null;
935 }
936 if ($function == "orig") {
c0c21d4d »
2011-06-08 Preserve original timestamp value in multiple update (bug #3312614)
937 return ($field["on_update"] == "CURRENT_TIMESTAMP" ? idf_escape($field["field"]) : false);
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
938 }
939 if ($function == "NULL") {
ed130065 »
2014-01-11 MongoDB: Don't store unset values
940 $value = null;
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
941 }
942 if ($field["type"] == "set") {
4cead560 » jakubvrana
2009-08-21 Edit default values directly in table creation
943 return array_sum((array) $value);
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
944 }
6317c773 »
2013-08-09 NoSQL: Allow editing complex values
945 if ($function == "json") {
946 $function = "";
947 $value = json_decode($value, true);
948 if (!is_array($value)) {
949 return false; //! report errors
950 }
951 return $value;
952 }
e8c6ef04 »
2013-07-24 Replace ereg*() by preg_*()
953 if (preg_match('~blob|bytea|raw|file~', $field["type"]) && ini_bool("file_uploads")) {
a5612526 » jakubvrana
2009-11-09 Use distinct name for blob inputs
954 $file = get_file("fields-$idf");
61640ee6 » jakubvrana
2009-07-03 Move common functions
955 if (!is_string($file)) {
956 return false; //! report errors
957 }
7e644b43 »
2010-10-13 Save bytes ($connection->quote shortcut)
958 return q($file);
61640ee6 » jakubvrana
2009-07-03 Move common functions
959 }
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
960 return $adminer->processInput($field, $value, $function);
61640ee6 » jakubvrana
2009-07-03 Move common functions
961 }
962
2a9a5055 »
2014-01-11 MongoDB: insert
963 /** Compute fields() from $_POST edit data
964 * @return array
965 */
966 function fields_from_edit() {
9824540e »
2014-01-11 MongoDB: auto_increment
967 global $driver;
2a9a5055 »
2014-01-11 MongoDB: insert
968 $return = array();
969 foreach ((array) $_POST["field_keys"] as $key => $val) {
970 if ($val != "") {
971 $val = bracket_escape($val);
972 $_POST["function"][$val] = $_POST["field_funs"][$key];
973 $_POST["fields"][$val] = $_POST["field_vals"][$key];
974 }
975 }
976 foreach ((array) $_POST["fields"] as $key => $val) {
977 $name = bracket_escape($key, 1); // 1 - back
9824540e »
2014-01-11 MongoDB: auto_increment
978 $return[$name] = array(
979 "field" => $name,
980 "privileges" => array("insert" => 1, "update" => 1),
981 "null" => 1,
982 "auto_increment" => ($key == $driver->primary),
983 );
2a9a5055 »
2014-01-11 MongoDB: insert
984 }
985 return $return;
986 }
987
db07325e » jakubvrana
2010-02-16 Search in all tables
988 /** Print results of search in all tables
989 * @uses $_GET["where"][0]
990 * @uses $_POST["tables"]
991 * @return null
992 */
993 function search_tables() {
994 global $adminer, $connection;
dfa6cbf4 »
2010-10-07 Show tables overview in Editor
995 $_GET["where"][0]["op"] = "LIKE %%";
996 $_GET["where"][0]["val"] = $_POST["query"];
db07325e » jakubvrana
2010-02-16 Search in all tables
997 $found = false;
e24d1fcb »
2013-04-26 Optimize table_status()
998 foreach (table_status('', true) as $table => $table_status) {
db07325e » jakubvrana
2010-02-16 Search in all tables
999 $name = $adminer->tableName($table_status);
1000 if (isset($table_status["Engine"]) && $name != "" && (!$_POST["tables"] || in_array($table, $_POST["tables"]))) {
ee3e0459 »
2010-05-14 Separate $where in limit function
1001 $result = $connection->query("SELECT" . limit("1 FROM " . table($table), " WHERE " . implode(" AND ", $adminer->selectSearchProcess(fields($table), array())), 1));
312075d0 »
2012-09-09 Search non-ASCII data only in text columns (bug #3564415)
1002 if (!$result || $result->fetch_row()) {
db07325e » jakubvrana
2010-02-16 Search in all tables
1003 if (!$found) {
1004 echo "<ul>\n";
1005 $found = true;
1006 }
312075d0 »
2012-09-09 Search non-ASCII data only in text columns (bug #3564415)
1007 echo "<li>" . ($result
1008 ? "<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"
1009 : "$name: <span class='error'>" . error() . "</span>\n");
db07325e » jakubvrana
2010-02-16 Search in all tables
1010 }
1011 }
1012 }
1013 echo ($found ? "</ul>" : "<p class='message'>" . lang('No tables.')) . "\n";
1014 }
1015
8ab6fffe »
2011-02-17 Centralize dump_headers
1016 /** Send headers for export
1017 * @param string
1018 * @param bool
1019 * @return string extension
1020 */
1021 function dump_headers($identifier, $multi_table = false) {
1022 global $adminer;
1023 $return = $adminer->dumpHeaders($identifier, $multi_table);
b78b0cd1 »
2012-06-29 Plugin for including date in export filename
1024 $output = $_POST["output"];
1025 if ($output != "text") {
e8c6ef04 »
2013-07-24 Replace ereg*() by preg_*()
1026 header("Content-Disposition: attachment; filename=" . $adminer->dumpFilename($identifier) . ".$return" . ($output != "file" && !preg_match('~[^0-9a-z]~', $output) ? ".$output" : ""));
b78b0cd1 »
2012-06-29 Plugin for including date in export filename
1027 }
8ab6fffe »
2011-02-17 Centralize dump_headers
1028 session_write_close();
0e6003e8 »
2013-05-01 Send export headers sooner
1029 ob_flush();
1030 flush();
8ab6fffe »
2011-02-17 Centralize dump_headers
1031 return $return;
1032 }
1033
9a176b0c » jakubvrana
2009-08-31 Comments
1034 /** Print CSV row
1035 * @param array
1036 * @return null
1037 */
61640ee6 » jakubvrana
2009-07-03 Move common functions
1038 function dump_csv($row) {
1039 foreach ($row as $key => $val) {
e225d22f »
2010-10-29 TSV export and import (bug #3097657)
1040 if (preg_match("~[\"\n,;\t]~", $val) || $val === "") {
61640ee6 » jakubvrana
2009-07-03 Move common functions
1041 $row[$key] = '"' . str_replace('"', '""', $val) . '"';
1042 }
1043 }
02d6b908 »
2010-12-01 CR+LF in CSV export
1044 echo implode(($_POST["format"] == "csv" ? "," : ($_POST["format"] == "tsv" ? "\t" : ";")), $row) . "\r\n";
61640ee6 » jakubvrana
2009-07-03 Move common functions
1045 }
c16c57b1 » jakubvrana
2009-07-11 E-mail sending
1046
9a176b0c » jakubvrana
2009-08-31 Comments
1047 /** Apply SQL function
1048 * @param string
1049 * @param string escaped column identifier
1050 * @return string
1051 */
e3310672 » jakubvrana
2009-08-18 Move apply_sql_function
1052 function apply_sql_function($function, $column) {
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
1053 return ($function ? ($function == "unixepoch" ? "DATETIME($column, '$function')" : ($function == "count distinct" ? "COUNT(DISTINCT " : strtoupper("$function(")) . "$column)") : $column);
e3310672 » jakubvrana
2009-08-18 Move apply_sql_function
1054 }
1055
06f4346c »
2014-03-21 Prevent against brute force login attempts from the same IP address
1056 /** Get path of the temporary directory
1057 * @return string
4ba2d856 »
2010-05-06 Allow permanent login without customization
1058 */
06f4346c »
2014-03-21 Prevent against brute force login attempts from the same IP address
1059 function get_temp_dir() {
1060 $return = ini_get("upload_tmp_dir"); // session_save_path() may contain other storage path
1061 if (!$return) {
4ba2d856 »
2010-05-06 Allow permanent login without customization
1062 if (function_exists('sys_get_temp_dir')) {
06f4346c »
2014-03-21 Prevent against brute force login attempts from the same IP address
1063 $return = sys_get_temp_dir();
4ba2d856 »
2010-05-06 Allow permanent login without customization
1064 } else {
1065 $filename = @tempnam("", ""); // @ - temp directory can be disabled by open_basedir
1066 if (!$filename) {
1067 return false;
1068 }
06f4346c »
2014-03-21 Prevent against brute force login attempts from the same IP address
1069 $return = dirname($filename);
4ba2d856 »
2010-05-06 Allow permanent login without customization
1070 unlink($filename);
1071 }
1072 }
06f4346c »
2014-03-21 Prevent against brute force login attempts from the same IP address
1073 return $return;
1074 }
1075
1076 /** Read password from file adminer.key in temporary directory or create one
1077 * @param bool
1078 * @return string or false if the file can not be created
1079 */
1080 function password_file($create) {
1081 $filename = get_temp_dir() . "/adminer.key";
1082 $return = @file_get_contents($filename); // @ - may not exist
f29a7cb1 »
2013-06-11 Notify user about expired master password for permanent login
1083 if ($return || !$create) {
4ba2d856 »
2010-05-06 Allow permanent login without customization
1084 return $return;
1085 }
1086 $fp = @fopen($filename, "w"); // @ - can have insufficient rights //! is not atomic
1087 if ($fp) {
f80b296a »
2014-06-27 Make adminer.key group-readable
1088 chmod($filename, 0660);
61606040 »
2013-08-11 Encrypt passwords stored in session by a key stored in cookie (thanks…
1089 $return = rand_string();
4ba2d856 »
2010-05-06 Allow permanent login without customization
1090 fwrite($fp, $return);
1091 fclose($fp);
1092 }
1093 return $return;
1094 }
1095
61606040 »
2013-08-11 Encrypt passwords stored in session by a key stored in cookie (thanks…
1096 /** Get a random string
1097 * @return string 32 hexadecimal characters
1098 */
1099 function rand_string() {
1100 return md5(uniqid(mt_rand(), true));
1101 }
1102
9ffca2f6 »
2013-08-06 SimpleDB: Allow selecting arrays
1103 /** Format value to use in select
1104 * @param string
1105 * @param string
1106 * @param array
1107 * @param int
1108 * @return string HTML
1109 */
1110 function select_value($val, $link, $field, $text_length) {
1111 global $adminer, $HTTPS;
1112 if (is_array($val)) {
1113 $return = "";
1114 foreach ($val as $k => $v) {
1115 $return .= "<tr>"
1116 . ($val != array_values($val) ? "<th>" . h($k) : "")
1117 . "<td>" . select_value($v, $link, $field, $text_length)
1118 ;
1119 }
1120 return "<table cellspacing='0'>$return</table>";
1121 }
1122 if (!$link) {
1123 $link = $adminer->selectLink($val, $field);
1124 }
1125 if ($link === null) {
1126 if (is_mail($val)) {
1127 $link = "mailto:$val";
1128 }
1129 if ($protocol = is_url($val)) {
96c8e6fb »
2014-03-26 Use rel=noreferrer in WebKit
1130 $link = (($protocol == "http" && $HTTPS) || preg_match('~WebKit~i', $_SERVER["HTTP_USER_AGENT"]) // WebKit supports noreferrer since 2009
9ffca2f6 »
2013-08-06 SimpleDB: Allow selecting arrays
1131 ? $val // HTTP links from HTTPS pages don't receive Referer automatically
96c8e6fb »
2014-03-26 Use rel=noreferrer in WebKit
1132 : "$protocol://www.adminer.org/redirect/?url=" . urlencode($val) // intermediate page to hide Referer
9ffca2f6 »
2013-08-06 SimpleDB: Allow selecting arrays
1133 );
1134 }
1135 }
2ecff212 »
2014-01-10 Don't print query in edit
1136 $return = $adminer->editVal($val, $field);
1137 if ($return !== null) {
1138 if ($return === "") { // === - may be int
1139 $return = "&nbsp;";
1140 } elseif ($text_length != "" && is_shortable($field) && is_utf8($return)) {
1141 $return = shorten_utf8($return, max(0, +$text_length)); // usage of LEFT() would reduce traffic but complicate query - expected average speedup: .001 s VS .01 s on local network
9ffca2f6 »
2013-08-06 SimpleDB: Allow selecting arrays
1142 } else {
2ecff212 »
2014-01-10 Don't print query in edit
1143 $return = h($return);
9ffca2f6 »
2013-08-06 SimpleDB: Allow selecting arrays
1144 }
1145 }
2ecff212 »
2014-01-10 Don't print query in edit
1146 return $adminer->selectVal($return, $link, $field, $val);
9ffca2f6 »
2013-08-06 SimpleDB: Allow selecting arrays
1147 }
1148
9a176b0c » jakubvrana
2009-08-31 Comments
1149 /** Check whether the string is e-mail address
1150 * @param string
1151 * @return bool
1152 */
1cf374bf »
2010-10-16 Rename functions (avoid conflict with WordPress)
1153 function is_mail($email) {
c16c57b1 » jakubvrana
2009-07-11 E-mail sending
1154 $atom = '[-a-z0-9!#$%&\'*+/=?^_`{|}~]'; // characters of local-name
1155 $domain = '[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])'; // one domain component
28e1dd28 » jakubvrana
2010-04-21 Allow multiple e-mails
1156 $pattern = "$atom+(\\.$atom+)*@($domain?\\.)+$domain";
9ffca2f6 »
2013-08-06 SimpleDB: Allow selecting arrays
1157 return is_string($email) && preg_match("(^$pattern(,\\s*$pattern)*\$)i", $email);
c16c57b1 » jakubvrana
2009-07-11 E-mail sending
1158 }
dc667ea1 » jakubvrana
2009-09-11 Hide select export and import
1159
bc78866a » jakubvrana
2009-10-02 Link URLs in select
1160 /** Check whether the string is URL address
1161 * @param string
2cec7584 »
2010-05-25 Direct links from HTTPS to HTTP
1162 * @return string "http", "https" or ""
bc78866a » jakubvrana
2009-10-02 Link URLs in select
1163 */
1164 function is_url($string) {
2cec7584 »
2010-05-25 Direct links from HTTPS to HTTP
1165 $domain = '[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])'; // one domain component //! IDN
5002b89b »
2010-10-23 Big numbers without E
1166 return (preg_match("~^(https?)://($domain?\\.)+$domain(:\\d+)?(/.*)?(\\?.*)?(#.*)?\$~i", $string, $match) ? strtolower($match[1]) : ""); //! restrict path, query and fragment characters
bc78866a » jakubvrana
2009-10-02 Link URLs in select
1167 }
1f53cd33 »
2012-08-19 Time out long running database list
1168
b684569f »
2012-09-22 Shorten varchar fields
1169 /** Check if field should be shortened
1170 * @param array
1171 * @return bool
1172 */
1173 function is_shortable($field) {
e8c6ef04 »
2013-07-24 Replace ereg*() by preg_*()
1174 return preg_match('~char|text|lob|geometry|point|linestring|polygon|string~', $field["type"]);
b684569f »
2012-09-22 Shorten varchar fields
1175 }
1176
063ddcce »
2013-07-03 Fix counting rows with grouping outside MySQL
1177 /** Get query to compute number of found rows
1178 * @param string
1179 * @param array
1180 * @param bool
1181 * @param array
1182 * @return string
1183 */
1184 function count_rows($table, $where, $is_group, $group) {
1185 global $jush;
1186 $query = " FROM " . table($table) . ($where ? " WHERE " . implode(" AND ", $where) : "");
1187 return ($is_group && ($jush == "sql" || count($group) == 1)
1188 ? "SELECT COUNT(DISTINCT " . implode(", ", $group) . ")$query"
1189 : "SELECT COUNT(*)" . ($is_group ? " FROM (SELECT 1$query$group_by) x" : $query)
1190 );
1191 }
1192
ea6d6e0d »
2012-08-19 Simplify slow queries
1193 /** Run query which can be killed by AJAX call after timing out
1194 * @param string
bc2001a9 »
2013-07-03 Fix comment
1195 * @return array of strings
1f53cd33 »
2012-08-19 Time out long running database list
1196 */
ea6d6e0d »
2012-08-19 Simplify slow queries
1197 function slow_query($query) {
1f53cd33 »
2012-08-19 Time out long running database list
1198 global $adminer, $token;
ef0cb38a »
2012-08-19 Support Editor
1199 $db = $adminer->database();
d17b17e5 »
2013-07-06 Driver for SimpleDB
1200 $timeout = $adminer->queryTimeout();
ef0cb38a »
2012-08-19 Support Editor
1201 if (support("kill") && is_object($connection2 = connect()) && ($db == "" || $connection2->select_db($db))) {
ea6d6e0d »
2012-08-19 Simplify slow queries
1202 $kill = $connection2->result("SELECT CONNECTION_ID()"); // MySQL and MySQLi can use thread_id but it's not in PDO_MySQL
1203 ?>
1f53cd33 »
2012-08-19 Time out long running database list
1204 <script type="text/javascript">
1205 var timeout = setTimeout(function () {
1206 ajax('<?php echo js_escape(ME); ?>script=kill', function () {
1207 }, 'token=<?php echo $token; ?>&kill=<?php echo $kill; ?>');
d17b17e5 »
2013-07-06 Driver for SimpleDB
1208 }, <?php echo 1000 * $timeout; ?>);
1f53cd33 »
2012-08-19 Time out long running database list
1209 </script>
1210 <?php
ea6d6e0d »
2012-08-19 Simplify slow queries
1211 } else {
1212 $connection2 = null;
6a7021db »
2012-08-19 Time out long running select count
1213 }
1f53cd33 »
2012-08-19 Time out long running database list
1214 ob_flush();
1215 flush();
d17b17e5 »
2013-07-06 Driver for SimpleDB
1216 $return = @get_key_vals($query, $connection2, $timeout); // @ - may be killed
ea6d6e0d »
2012-08-19 Simplify slow queries
1217 if ($connection2) {
6a7021db »
2012-08-19 Time out long running select count
1218 echo "<script type='text/javascript'>clearTimeout(timeout);</script>\n";
1219 ob_flush();
1220 flush();
1f53cd33 »
2012-08-19 Time out long running database list
1221 }
ea6d6e0d »
2012-08-19 Simplify slow queries
1222 return array_keys($return);
1f53cd33 »
2012-08-19 Time out long running database list
1223 }
caa9f490 »
2012-09-06 Compress CSS
1224
a564bba2 »
2013-10-24 Protect CSRF token against BREACH
1225 /** Generate BREACH resistant CSRF token
1226 * @return string
1227 */
1228 function get_token() {
1229 $rand = rand(1, 1e6);
1230 return ($rand ^ $_SESSION["token"]) . ":$rand";
1231 }
1232
1233 /** Verify if supplied CSRF token is valid
1234 * @return bool
1235 */
1236 function verify_token() {
1237 list($token, $rand) = explode(":", $_POST["token"]);
1238 return ($rand ^ $_SESSION["token"]) == $token;
1239 }
1240
caa9f490 »
2012-09-06 Compress CSS
1241 // used in compiled version
1242 function lzw_decompress($binary) {
1243 // convert binary string to codes
1244 $dictionary_count = 256;
1245 $bits = 8; // ceil(log($dictionary_count, 2))
1246 $codes = array();
1247 $rest = 0;
1248 $rest_length = 0;
1249 for ($i=0; $i < strlen($binary); $i++) {
1250 $rest = ($rest << 8) + ord($binary[$i]);
1251 $rest_length += 8;
1252 if ($rest_length >= $bits) {
1253 $rest_length -= $bits;
1254 $codes[] = $rest >> $rest_length;
1255 $rest &= (1 << $rest_length) - 1;
1256 $dictionary_count++;
1257 if ($dictionary_count >> $bits) {
1258 $bits++;
1259 }
1260 }
1261 }
1262 // decompression
1263 $dictionary = range("\0", "\xFF");
1264 $return = "";
1265 foreach ($codes as $i => $code) {
1266 $element = $dictionary[$code];
1267 if (!isset($element)) {
1268 $element = $word . $word[0];
1269 }
1270 $return .= $element;
1271 if ($i) {
1272 $dictionary[] = $word . $element[0];
1273 }
1274 $word = $element;
1275 }
1276 return $return;
1277 }
a41ee665 »
2013-12-19 Move function used in Editor
1278
1279 /** Return events to display help on mouse over
1280 * @param string JS expression
1281 * @param bool JS expression
1282 * @return string
1283 */
1284 function on_help($command, $side = 0) {
1285 return " onmouseover='helpMouseover(this, event, " . h($command) . ", $side);' onmouseout='helpMouseout(this, event);'";
1286 }
9567e5cb »
2014-02-24 Prepare for using edit form in select
1287
1288 /** Print edit data form
7da82a12 »
2014-02-24 Display edit form after error in clone or multi-edit
1289 * @param string
9567e5cb »
2014-02-24 Prepare for using edit form in select
1290 * @param array
1291 * @param mixed
1292 * @param bool
1293 * @return null
1294 */
7da82a12 »
2014-02-24 Display edit form after error in clone or multi-edit
1295 function edit_form($TABLE, $fields, $row, $update) {
1296 global $adminer, $jush, $token, $error;
1297 $table_name = $adminer->tableName(table_status1($TABLE, true));
1298 page_header(
1299 ($update ? lang('Edit') : lang('Insert')),
1300 $error,
1301 array("select" => array($TABLE, $table_name)),
1302 $table_name
1303 );
1304 if ($row === false) {
1305 echo "<p class='error'>" . lang('No rows.') . "\n";
1306 }
9567e5cb »
2014-02-24 Prepare for using edit form in select
1307 ?>
7da82a12 »
2014-02-24 Display edit form after error in clone or multi-edit
1308 <div id="message"></div>
9567e5cb »
2014-02-24 Prepare for using edit form in select
1309 <form action="" method="post" enctype="multipart/form-data" id="form">
1310 <?php
1311 if (!$fields) {
1312 echo "<p class='error'>" . lang('You have no privileges to update this table.') . "\n";
1313 } else {
1314 echo "<table cellspacing='0' onkeydown='return editingKeydown(event);'>\n";
1315
1316 foreach ($fields as $name => $field) {
1317 echo "<tr><th>" . $adminer->fieldName($field);
1318 $default = $_GET["set"][bracket_escape($name)];
1319 if ($default === null) {
1320 $default = $field["default"];
1321 if ($field["type"] == "bit" && preg_match("~^b'([01]*)'\$~", $default, $regs)) {
1322 $default = $regs[1];
1323 }
1324 }
1325 $value = ($row !== null
1326 ? ($row[$name] != "" && $jush == "sql" && preg_match("~enum|set~", $field["type"])
1327 ? (is_array($row[$name]) ? array_sum($row[$name]) : +$row[$name])
1328 : $row[$name]
1329 )
1330 : (!$update && $field["auto_increment"]
1331 ? ""
1332 : (isset($_GET["select"]) ? false : $default)
1333 )
1334 );
1335 if (!$_POST["save"] && is_string($value)) {
1336 $value = $adminer->editVal($value, $field);
1337 }
1338 $function = ($_POST["save"]
1339 ? (string) $_POST["function"][$name]
1340 : ($update && $field["on_update"] == "CURRENT_TIMESTAMP"
1341 ? "now"
1342 : ($value === false ? null : ($value !== null ? '' : 'NULL'))
1343 )
1344 );
1345 if (preg_match("~time~", $field["type"]) && $value == "CURRENT_TIMESTAMP") {
1346 $value = "";
1347 $function = "now";
1348 }
1349 input($field, $value, $function);
1350 echo "\n";
1351 }
1352 if (!support("table")) {
1353 echo "<tr>"
1354 . "<th><input name='field_keys[]' onkeyup='keyupChange.call(this);' onchange='fieldChange(this);' value=''>" // needs empty value for keyupChange()
1355 . "<td class='function'>" . html_select("field_funs[]", $adminer->editFunctions(array("null" => isset($_GET["select"]))))
1356 . "<td><input name='field_vals[]'>"
1357 . "\n"
1358 ;
1359 }
1360 echo "</table>\n";
1361 }
1362 echo "<p>\n";
1363 if ($fields) {
1364 echo "<input type='submit' value='" . lang('Save') . "'>\n";
1365 if (!isset($_GET["select"])) {
1366 echo "<input type='submit' name='insert' value='" . ($update
1367 ? lang('Save and continue edit') . "' onclick='return !ajaxForm(this.form, \"" . lang('Saving') . '...", this)'
1368 : lang('Save and insert next')
1369 ) . "' title='Ctrl+Shift+Enter'>\n";
1370 }
1371 }
1372 echo ($update ? "<input type='submit' name='delete' value='" . lang('Delete') . "'" . confirm() . ">\n"
1373 : ($_POST || !$fields ? "" : "<script type='text/javascript'>focus(document.getElementById('form').getElementsByTagName('td')[1].firstChild);</script>\n")
1374 );
1375 if (isset($_GET["select"])) {
1376 hidden_fields(array("check" => (array) $_POST["check"], "clone" => $_POST["clone"], "all" => $_POST["all"]));
1377 }
1378 ?>
1379 <input type="hidden" name="referer" value="<?php echo h(isset($_POST["referer"]) ? $_POST["referer"] : $_SERVER["HTTP_REFERER"]); ?>">
1380 <input type="hidden" name="save" value="1">
1381 <input type="hidden" name="token" value="<?php echo $token; ?>">
1382 </form>
1383 <?php
1384 }
Something went wrong with that request. Please try again.