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