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