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