Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 540 lines (503 sloc) 16.65 kb
667bfec4 » jakubvrana
2007-07-02 Decomposition
1 <?php
9a176b0c » jakubvrana
2009-08-31 Comments
2 /** Get database connection
3 * @return Min_DB
4 */
64d616c0 » jakubvrana
2009-09-22 Rename get_dbh to connection
5 function connection() {
6 // can be used in customization, $connection is minified
7 global $connection;
8 return $connection;
c64c4fd1 » jakubvrana
2009-07-27 Adminer class
9 }
10
9a176b0c » jakubvrana
2009-08-31 Comments
11 /** Escape database identifier
12 * @param string
13 * @return string
14 */
667bfec4 » jakubvrana
2007-07-02 Decomposition
15 function idf_escape($idf) {
16 return "`" . str_replace("`", "``", $idf) . "`";
17 }
18
9a176b0c » jakubvrana
2009-08-31 Comments
19 /** Unescape database identifier
20 * @param string text inside ``
21 * @return string
22 */
667bfec4 » jakubvrana
2007-07-02 Decomposition
23 function idf_unescape($idf) {
24 return str_replace("``", "`", $idf);
25 }
26
5e01a627 » jakubvrana
2009-10-16 Prepare for SQLite
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
9a176b0c » jakubvrana
2009-08-31 Comments
36 /** Escape or unescape string to use inside form []
37 * @param string
38 * @param bool
39 * @return string
40 */
667bfec4 » jakubvrana
2007-07-02 Decomposition
41 function bracket_escape($idf, $back = false) {
64ba9242 » jakubvrana
2009-06-21 Comments
42 // escape brackets inside name="x[]"
73e8631b » jakubvrana
2007-07-09 Move stuff to functions.inc.php
43 static $trans = array(':' => ':1', ']' => ':2', '[' => ':3');
667bfec4 » jakubvrana
2007-07-02 Decomposition
44 return strtr($idf, ($back ? array_flip($trans) : $trans));
45 }
46
9a176b0c » jakubvrana
2009-08-31 Comments
47 /** Escape for HTML
48 * @param string
49 * @return string
50 */
689699aa » jakubvrana
2009-07-28 Shortcut for htmlspecialchars
51 function h($string) {
52 return htmlspecialchars($string, ENT_QUOTES);
53 }
54
9a176b0c » jakubvrana
2009-08-31 Comments
55 /** Escape for TD
56 * @param string
57 * @return string
58 */
c1969850 » jakubvrana
2009-08-25 Function nbsp
59 function nbsp($string) {
60 return (strlen(trim($string)) ? h($string) : "&nbsp;");
61 }
62
6b30cfa7 » jakubvrana
2009-09-25 Separate checkbox
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 = "") {
c4fe12a1 » jakubvrana
2009-09-26 Clickable labels in IE6
72 static $id = 0;
73 $id++;
74 $return = "<input type='checkbox' name='$name' value='" . h($value) . "'" . ($checked ? " checked" : "") . ($onclick ? " onclick=\"$onclick\"" : "") . " id='checkbox-$id'>";
75 return (strlen($label) ? "<label for='checkbox-$id'>$return" . h($label) . "</label>" : $return);
6b30cfa7 » jakubvrana
2009-09-25 Separate checkbox
76 }
77
e40612a4 » jakubvrana
2009-10-02 Use radio in export
78 /** Generate HTML radio list
79 * @param string
80 * @param array
81 * @param string
06984091 » jakubvrana
2009-10-02 Utilize html_select
82 * @param string true for no onchange, false for radio
e40612a4 » jakubvrana
2009-10-02 Use radio in export
83 * @return string
84 */
06984091 » jakubvrana
2009-10-02 Utilize html_select
85 function html_select($name, $options, $value = "", $onchange = true) {
86 if ($onchange) {
87 return "<select name='" . h($name) . "'" . (is_string($onchange) ? " onchange=\"$onchange\"" : "") . ">" . optionlist($options, $value) . "</select>";
e40612a4 » jakubvrana
2009-10-02 Use radio in export
88 }
89 $return = "";
90 foreach ($options as $key => $val) {
91 $return .= "<label><input type='radio' name='" . h($name) . "' value='" . h($key) . "'" . ($key == $value ? " checked" : "") . ">" . h($val) . "</label>";
92 }
93 return $return;
94 }
95
9a176b0c » jakubvrana
2009-08-31 Comments
96 /** Generate list of HTML options
97 * @param array array of strings or arrays (creates optgroup)
98 * @param mixed
99 * @param bool always use array keys for value="", otherwise only string keys are used
100 * @return string
101 */
1e4d11e9 » jakubvrana
2009-07-23 Select from foreign keys in Editor
102 function optionlist($options, $selected = null, $use_keys = false) {
667bfec4 » jakubvrana
2007-07-02 Decomposition
103 $return = "";
104 foreach ($options as $k => $v) {
105 if (is_array($v)) {
689699aa » jakubvrana
2009-07-28 Shortcut for htmlspecialchars
106 $return .= '<optgroup label="' . h($k) . '">';
667bfec4 » jakubvrana
2007-07-02 Decomposition
107 }
ced9de9d » jakubvrana
2009-07-02 Create single column foreign key in table structure
108 foreach ((is_array($v) ? $v : array($k => $v)) as $key => $val) {
cb6d36c0 » jakubvrana
2009-07-28 HTML instead of XHTML
109 $return .= '<option' . ($use_keys || is_string($key) ? ' value="' . h($key) . '"' : '') . (($use_keys || is_string($key) ? (string) $key : $val) === $selected ? ' selected' : '') . '>' . h($val);
667bfec4 » jakubvrana
2007-07-02 Decomposition
110 }
111 if (is_array($v)) {
112 $return .= '</optgroup>';
113 }
114 }
115 return $return;
116 }
117
9a176b0c » jakubvrana
2009-08-31 Comments
118 /** Get list of values from database
119 * @param string
120 * @param mixed
121 * @return array
122 */
0bd864ea » jakubvrana
2009-05-21 Allow specifying column in get_vals
123 function get_vals($query, $column = 0) {
64d616c0 » jakubvrana
2009-09-22 Rename get_dbh to connection
124 global $connection;
7769f2c6 » jakubvrana
2007-07-11 Separate get_vals
125 $return = array();
64d616c0 » jakubvrana
2009-09-22 Rename get_dbh to connection
126 $result = $connection->query($query);
49f7886f » jakubvrana
2008-09-30 SHOW DATABASES can be revoked
127 if ($result) {
128 while ($row = $result->fetch_row()) {
0bd864ea » jakubvrana
2009-05-21 Allow specifying column in get_vals
129 $return[] = $row[$column];
49f7886f » jakubvrana
2008-09-30 SHOW DATABASES can be revoked
130 }
7769f2c6 » jakubvrana
2007-07-11 Separate get_vals
131 }
132 return $return;
133 }
134
9a176b0c » jakubvrana
2009-08-31 Comments
135 /** Find unique identifier of a row
136 * @param array
137 * @param array result of indexes()
138 * @return string query string
139 */
667bfec4 » jakubvrana
2007-07-02 Decomposition
140 function unique_idf($row, $indexes) {
720f5fc8 » jakubvrana
2007-07-05 Restructure indexes
141 foreach ($indexes as $index) {
4b3d569a » jakubvrana
2009-10-07 Save bytes
142 if (ereg("PRIMARY|UNIQUE", $index["type"])) {
720f5fc8 » jakubvrana
2007-07-05 Restructure indexes
143 $return = array();
144 foreach ($index["columns"] as $key) {
64ba9242 » jakubvrana
2009-06-21 Comments
145 if (!isset($row[$key])) { // NULL is ambiguous
720f5fc8 » jakubvrana
2007-07-05 Restructure indexes
146 continue 2;
667bfec4 » jakubvrana
2007-07-02 Decomposition
147 }
430034c3 » jakubvrana
2007-07-09 Cottage homework
148 $return[] = urlencode("where[" . bracket_escape($key) . "]") . "=" . urlencode($row[$key]);
667bfec4 » jakubvrana
2007-07-02 Decomposition
149 }
720f5fc8 » jakubvrana
2007-07-05 Restructure indexes
150 return $return;
667bfec4 » jakubvrana
2007-07-02 Decomposition
151 }
152 }
153 $return = array();
154 foreach ($row as $key => $val) {
26c9d64b » jakubvrana
2009-08-19 Fix long SQL query crash (bug #2839231)
155 if (!preg_match('~^(COUNT\\((\\*|(DISTINCT )?`(?:[^`]|``)+`)\\)|(AVG|GROUP_CONCAT|MAX|MIN|SUM)\\(`(?:[^`]|``)+`\\))$~', $key)) { //! columns looking like functions
988a29fc » jakubvrana
2009-07-28 Don't use aggregation functions in unique_idf
156 $return[] = (isset($val) ? urlencode("where[" . bracket_escape($key) . "]") . "=" . urlencode($val) : "null%5B%5D=" . urlencode($key));
157 }
430034c3 » jakubvrana
2007-07-09 Cottage homework
158 }
159 return $return;
160 }
161
9a176b0c » jakubvrana
2009-08-31 Comments
162 /** Create SQL condition from parsed query string
163 * @param array parsed query string
164 * @return string
165 */
9ea31125 » jakubvrana
2008-08-08 Mandatory $where in where()
166 function where($where) {
430034c3 » jakubvrana
2007-07-09 Cottage homework
167 $return = array();
116faf2e » jakubvrana
2008-01-08 Mass delete
168 foreach ((array) $where["where"] as $key => $val) {
581bd350 » jakubvrana
2008-08-08 Respect function results in where()
169 $key = bracket_escape($key, "back");
ffc103d8 » jakubvrana
2009-10-26 Compare floats as strings (bug #2886518)
170 $return[] = (preg_match('~^[A-Z0-9_]+\\(`(?:[^`]|``)+`\\)$~', $key) ? $key : idf_escape($key)) . " LIKE " . exact_value(addcslashes($val, "%_")); // LIKE because of floats //! enum and set, columns looking like functions
430034c3 » jakubvrana
2007-07-09 Cottage homework
171 }
116faf2e » jakubvrana
2008-01-08 Mass delete
172 foreach ((array) $where["null"] as $key) {
581bd350 » jakubvrana
2008-08-08 Respect function results in where()
173 $key = bracket_escape($key, "back");
26c9d64b » jakubvrana
2009-08-19 Fix long SQL query crash (bug #2839231)
174 $return[] = (preg_match('~^[A-Z0-9_]+\\(`(?:[^`]|``)+`\\)$~', $key) ? $key : idf_escape($key)) . " IS NULL";
667bfec4 » jakubvrana
2007-07-02 Decomposition
175 }
c16c57b1 » jakubvrana
2009-07-11 E-mail sending
176 return implode(" AND ", $return);
667bfec4 » jakubvrana
2007-07-02 Decomposition
177 }
178
9a176b0c » jakubvrana
2009-08-31 Comments
179 /** Create SQL condition from query string
180 * @param string
181 * @return string
182 */
0cd2afbb » jakubvrana
2009-06-07 Remove single item clone
183 function where_check($val) {
184 parse_str($val, $check);
185 return where($check);
186 }
187
9a176b0c » jakubvrana
2009-08-31 Comments
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 */
c42c46a2 » jakubvrana
2009-07-15 Table relations
194 function where_link($i, $column, $value) {
db46fc24 » jakubvrana
2009-08-03 Maintain insert prefill value
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);
c42c46a2 » jakubvrana
2009-07-15 Table relations
196 }
197
319cba4b » jakubvrana
2009-09-09 Save e-mail from to cookie
198 /** Set cookie valid for 1 month
199 * @param string
200 * @param string
201 * @return bool
202 */
203 function cookie($name, $value) {
3675fe6f » jakubvrana
2009-09-25 Simplify cookie expiration
204 return setcookie($name, $value, time() + 2592000, preg_replace('~\\?.*~', '', $_SERVER["REQUEST_URI"])); // 2592000 = 30 * 24 * 60 * 60
319cba4b » jakubvrana
2009-09-09 Save e-mail from to cookie
205 }
206
f9a20232 » jakubvrana
2009-11-02 Improve concurrency
207 /** Restart stopped session
208 * @return null
209 */
210 function restart_session() {
211 if (!ini_get("session.use_cookies")) {
212 session_start();
213 }
214 }
215
9a176b0c » jakubvrana
2009-08-31 Comments
216 /** Send Location header and exit
217 * @param string
218 * @param string
219 * @return null
220 */
430034c3 » jakubvrana
2007-07-09 Cottage homework
221 function redirect($location, $message = null) {
222 if (isset($message)) {
be49e08b » jakubvrana
2009-11-03 Improve session restarting
223 restart_session();
ff74bf07 » jakubvrana
2008-04-18 Allow multiple messages
224 $_SESSION["messages"][] = $message;
430034c3 » jakubvrana
2007-07-09 Cottage homework
225 }
226 header("Location: " . (strlen($location) ? $location : "."));
227 exit;
228 }
229
9a176b0c » jakubvrana
2009-08-31 Comments
230 /** Execute query and redirect if successful
231 * @param string
232 * @param string
233 * @param string
234 * @param bool
235 * @param bool
236 * @param bool
237 * @return bool
238 */
748ee836 » jakubvrana
2008-09-03 Print SQL command with multiple queries
239 function query_redirect($query, $location, $message, $redirect = true, $execute = true, $failed = false) {
64d616c0 » jakubvrana
2009-09-22 Rename get_dbh to connection
240 global $connection, $error, $adminer;
f9a20232 » jakubvrana
2009-11-02 Improve concurrency
241 if ($execute) {
242 $failed = !$connection->query($query);
243 }
10bdbed1 » jakubvrana
2009-06-04 Save queries to history
244 $sql = "";
245 if ($query) {
c64c4fd1 » jakubvrana
2009-07-27 Adminer class
246 $sql = $adminer->messageQuery($query);
10bdbed1 » jakubvrana
2009-06-04 Save queries to history
247 }
748ee836 » jakubvrana
2008-09-03 Print SQL command with multiple queries
248 if ($failed) {
64d616c0 » jakubvrana
2009-09-22 Rename get_dbh to connection
249 $error = h($connection->error) . $sql;
c48a017e » jakubvrana
2008-09-02 Print SQL query by error
250 return false;
f91b2c4b » jakubvrana
2008-09-02 Print SQL query by message
251 }
c48a017e » jakubvrana
2008-09-02 Print SQL query by error
252 if ($redirect) {
253 redirect($location, $message . $sql);
254 }
255 return true;
f91b2c4b » jakubvrana
2008-09-02 Print SQL query by message
256 }
257
9a176b0c » jakubvrana
2009-08-31 Comments
258 /** Execute and remember query
259 * @param string null to return remembered queries
260 * @return Min_Result
261 */
748ee836 » jakubvrana
2008-09-03 Print SQL command with multiple queries
262 function queries($query = null) {
64d616c0 » jakubvrana
2009-09-22 Rename get_dbh to connection
263 global $connection;
748ee836 » jakubvrana
2008-09-03 Print SQL command with multiple queries
264 static $queries = array();
265 if (!isset($query)) {
64ba9242 » jakubvrana
2009-06-21 Comments
266 // return executed queries without parameter
36950ebe » jakubvrana
2008-09-25 Tables and views
267 return implode(";\n", $queries);
748ee836 » jakubvrana
2008-09-03 Print SQL command with multiple queries
268 }
269 $queries[] = $query;
64d616c0 » jakubvrana
2009-09-22 Rename get_dbh to connection
270 return $connection->query($query);
748ee836 » jakubvrana
2008-09-03 Print SQL command with multiple queries
271 }
272
dbdd40a1 » jakubvrana
2009-10-13 Introduce queries_redirect function
273 /** Redirect by remembered queries
274 * @param string
275 * @param string
276 * @param bool
277 */
278 function queries_redirect($location, $message, $redirect) {
279 return query_redirect(queries(), $location, $message, $redirect, false, !$redirect);
280 }
281
9a176b0c » jakubvrana
2009-08-31 Comments
282 /** Remove parameter from query string
283 * @param string
284 * @return string
285 */
d41d4460 » jakubvrana
2007-07-27 remove_from_uri()
286 function remove_from_uri($param = "") {
6272cae9 » jakubvrana
2007-07-28 Unconditionally remove session_name() in remove_from_uri()
287 $param = "($param|" . session_name() . ")";
5f836192 » jakubvrana
2009-10-06 Session management
288 return substr(preg_replace("~([?&])$param=[^&]*&~", '\\1', "$_SERVER[REQUEST_URI]&"), 0, -1);
d41d4460 » jakubvrana
2007-07-27 remove_from_uri()
289 }
290
9a176b0c » jakubvrana
2009-08-31 Comments
291 /** Generate page number for pagination
292 * @param int
293 * @return string
294 */
c565c545 » jakubvrana
2009-07-15 Let pagination function return
295 function pagination($page) {
689699aa » jakubvrana
2009-07-28 Shortcut for htmlspecialchars
296 return " " . ($page == $_GET["page"] ? $page + 1 : '<a href="' . h(remove_from_uri("page") . ($page ? "&page=$page" : "")) . '">' . ($page + 1) . "</a>");
1456283e » jakubvrana
2008-08-07 Pages on last page
297 }
298
9a176b0c » jakubvrana
2009-08-31 Comments
299 /** Get file contents from $_FILES or $_POST["files"]
300 * @param string
301 * @param bool
302 * @return string
303 */
25f01d35 » jakubvrana
2009-08-28 Compress export and import
304 function get_file($key, $decompress = false) {
64ba9242 » jakubvrana
2009-06-21 Comments
305 // returns int for error, string otherwise
25f01d35 » jakubvrana
2009-08-28 Compress export and import
306 $file = $_POST["files"][$key];
307 if (isset($file)) {
64ba9242 » jakubvrana
2009-06-21 Comments
308 // get the file from hidden field if the user was logged out
25f01d35 » jakubvrana
2009-08-28 Compress export and import
309 $length = strlen($file);
310 if ($length && $length < 4) {
311 return intval($file);
312 }
313 return base64_decode($file);
314 }
315 $file = $_FILES[$key];
316 if (!$file || $file["error"]) {
317 return $file["error"];
430034c3 » jakubvrana
2007-07-09 Cottage homework
318 }
0a2d81d7 » jakubvrana
2009-08-29 Bzip2 import
319 return file_get_contents($decompress && ereg('\\.gz$', $file["name"]) ? "compress.zlib://$file[tmp_name]"
320 : ($decompress && ereg('\\.bz2$', $file["name"]) ? "compress.bzip2://$file[tmp_name]"
321 : $file["tmp_name"]
322 )); //! may not be reachable because of open_basedir
430034c3 » jakubvrana
2007-07-09 Cottage homework
323 }
324
9a176b0c » jakubvrana
2009-08-31 Comments
325 /** Determine upload error
326 * @param int
327 * @return string
328 */
e2a03c08 » jakubvrana
2009-07-16 Show upload_max_filesize
329 function upload_error($error) {
330 $max_size = ($error == UPLOAD_ERR_INI_SIZE ? ini_get("upload_max_filesize") : null); // post_max_size is checked in index.php
b2a7c7ac » jakubvrana
2009-08-25 Execute SQL file stored on server disk
331 return ($error ? lang('Unable to upload a file.') . ($max_size ? " " . lang('Maximum allowed file size is %sB.', $max_size) : "") : lang('File does not exist.'));
e2a03c08 » jakubvrana
2009-07-16 Show upload_max_filesize
332 }
333
9a176b0c » jakubvrana
2009-08-31 Comments
334 /** Generate class for odd rows
335 * @param string return this for odd rows, empty to reset counter
336 * @return string
337 */
338 function odd($return = ' class="odd"') {
4e5b1262 » jakubvrana
2009-04-30 Highlight odd and hover rows
339 static $i = 0;
9a176b0c » jakubvrana
2009-08-31 Comments
340 if (!$return) { // reset counter
1b97f48e » jakubvrana
2009-05-18 Reset odd() for each result
341 $i = -1;
4e5b1262 » jakubvrana
2009-04-30 Highlight odd and hover rows
342 }
9a176b0c » jakubvrana
2009-08-31 Comments
343 return ($i++ % 2 ? $return : '');
4e5b1262 » jakubvrana
2009-04-30 Highlight odd and hover rows
344 }
345
9a176b0c » jakubvrana
2009-08-31 Comments
346 /** Check whether the string is in UTF-8
347 * @param string
348 * @return bool
349 */
017ffb51 » jakubvrana
2009-06-11 Better UTF-8 detection
350 function is_utf8($val) {
64ba9242 » jakubvrana
2009-06-21 Comments
351 // don't print control chars except \t\r\n
017ffb51 » jakubvrana
2009-06-11 Better UTF-8 detection
352 return (preg_match('~~u', $val) && !preg_match('~[\\0-\\x8\\xB\\xC\\xE-\\x1F]~', $val));
353 }
354
9a176b0c » jakubvrana
2009-08-31 Comments
355 /** Shorten UTF-8 string
356 * @param string
357 * @param int
358 * @param string
359 * @return string escaped string with appended ...
360 */
cd075842 » jakubvrana
2009-06-15 Save SQL query to history
361 function shorten_utf8($string, $length = 80, $suffix = "") {
11982f17 » jakubvrana
2009-09-24 Shorten printed SQL query
362 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
363 preg_match("(^([\t\r\n -~]{0,$length})($)?)", $string, $match);
95764b01 » jakubvrana
2009-08-25 Shorten binary strings
364 }
11982f17 » jakubvrana
2009-09-24 Shorten printed SQL query
365 return h($match[1]) . $suffix . (isset($match[2]) ? "" : "<em>...</em>");
02a851c7 » jakubvrana
2007-08-24 Shorten texts in PHP
366 }
3920ebbe » jakubvrana
2008-10-03 Remove InnoDB table comment in Tables and views
367
9a176b0c » jakubvrana
2009-08-31 Comments
368 /** Generate friendly URL
369 * @param string
370 * @return string
371 */
82518960 » jakubvrana
2009-06-11 Downloaded file name
372 function friendly_url($val) {
64ba9242 » jakubvrana
2009-06-21 Comments
373 // used for blobs and export
82518960 » jakubvrana
2009-06-11 Downloaded file name
374 return preg_replace('~[^a-z0-9_]~i', '-', $val);
375 }
376
9a176b0c » jakubvrana
2009-08-31 Comments
377 /** Print hidden fields
378 * @param array
379 * @param array
380 * @return null
381 */
820c8820 » jakubvrana
2008-10-09 Bulk clone
382 function hidden_fields($process, $ignore = array()) {
5d7071a5 » jakubvrana
2008-10-03 Choose language through option-list
383 while (list($key, $val) = each($process)) {
384 if (is_array($val)) {
385 foreach ($val as $k => $v) {
386 $process[$key . "[$k]"] = $v;
387 }
388 } elseif (!in_array($key, $ignore)) {
689699aa » jakubvrana
2009-07-28 Shortcut for htmlspecialchars
389 echo '<input type="hidden" name="' . h($key) . '" value="' . h($val) . '">';
5d7071a5 » jakubvrana
2008-10-03 Choose language through option-list
390 }
391 }
392 }
61640ee6 » jakubvrana
2009-07-03 Move common functions
393
9a176b0c » jakubvrana
2009-08-31 Comments
394 /** Find out foreign keys for each column
395 * @param string
396 * @return array array($col => array())
397 */
1e4d11e9 » jakubvrana
2009-07-23 Select from foreign keys in Editor
398 function column_foreign_keys($table) {
399 $return = array();
400 foreach (foreign_keys($table) as $foreign_key) {
401 foreach ($foreign_key["source"] as $val) {
402 $return[$val][] = $foreign_key;
403 }
404 }
405 return $return;
406 }
407
9a176b0c » jakubvrana
2009-08-31 Comments
408 /** Print edit input field
409 * @param array one field from fields()
410 * @param mixed
411 * @param string
412 * @return null
413 */
1e4d11e9 » jakubvrana
2009-07-23 Select from foreign keys in Editor
414 function input($field, $value, $function) {
c64c4fd1 » jakubvrana
2009-07-27 Adminer class
415 global $types, $adminer;
689699aa » jakubvrana
2009-07-28 Shortcut for htmlspecialchars
416 $name = h(bracket_escape($field["field"]));
61640ee6 » jakubvrana
2009-07-03 Move common functions
417 echo "<td class='function'>";
67584771 » jakubvrana
2009-10-09 Hide NULL in Editor enum
418 $functions = (isset($_GET["select"]) ? array("orig" => lang('original')) : array()) + $adminer->editFunctions($field);
61640ee6 » jakubvrana
2009-07-03 Move common functions
419 if ($field["type"] == "enum") {
67584771 » jakubvrana
2009-10-09 Hide NULL in Editor enum
420 echo "&nbsp;<td>" . ($functions["orig"] ? "<label><input type='radio' name='fields[$name]' value='-1' checked><em>$functions[orig]</em></label> " : "");
421 if (in_array("NULL", $functions)) {
422 echo "<label><input type='radio' name='fields[$name]' value=''" . (isset($value) || $functions["orig"] ? '' : ' checked') . "><em>NULL</em></label> ";
61640ee6 » jakubvrana
2009-07-03 Move common functions
423 }
4cead560 » jakubvrana
2009-08-21 Edit default values directly in table creation
424 echo "<input type='radio' name='fields[$name]' value='0'" . ($value === 0 ? ' checked' : '') . '>';
26c9d64b » jakubvrana
2009-08-19 Fix long SQL query crash (bug #2839231)
425 preg_match_all("~'((?:[^']|'')*)'~", $field["length"], $matches);
61640ee6 » jakubvrana
2009-07-03 Move common functions
426 foreach ($matches[1] as $i => $val) {
427 $val = stripcslashes(str_replace("''", "'", $val));
428 $checked = (is_int($value) ? $value == $i+1 : $value === $val);
4cead560 » jakubvrana
2009-08-21 Edit default values directly in table creation
429 echo " <label><input type='radio' name='fields[$name]' value='" . ($i+1) . "'" . ($checked ? ' checked' : '') . '>' . h($val) . '</label>';
61640ee6 » jakubvrana
2009-07-03 Move common functions
430 }
431 } else {
99039467 » jakubvrana
2009-09-08 Use original values in Editor multi edit
432 $first = 0;
433 foreach ($functions as $key => $val) {
434 if ($key === "" || !$val) {
435 break;
436 }
437 $first++;
438 }
689699aa » jakubvrana
2009-07-28 Shortcut for htmlspecialchars
439 $onchange = ($first ? " onchange=\"var f = this.form['function[" . addcslashes($name, "\r\n'\\") . "]']; if ($first > f.selectedIndex) f.selectedIndex = $first;\"" : "");
06984091 » jakubvrana
2009-10-02 Utilize html_select
440 echo (count($functions) > 1 ? html_select("function[$name]", $functions, !isset($function) || in_array($function, $functions) ? $function : "") : nbsp(reset($functions))) . '<td>';
689699aa » jakubvrana
2009-07-28 Shortcut for htmlspecialchars
441 $input = $adminer->editInput($_GET["edit"], $field, " name='fields[$name]'$onchange", $value); // usage in call is without a table
e1abcda0 » jakubvrana
2009-07-28 Treat tinyint(1) as boolean
442 if (strlen($input)) {
443 echo $input;
1e4d11e9 » jakubvrana
2009-07-23 Select from foreign keys in Editor
444 } elseif ($field["type"] == "set") { //! 64 bits
26c9d64b » jakubvrana
2009-08-19 Fix long SQL query crash (bug #2839231)
445 preg_match_all("~'((?:[^']|'')*)'~", $field["length"], $matches);
61640ee6 » jakubvrana
2009-07-03 Move common functions
446 foreach ($matches[1] as $i => $val) {
447 $val = stripcslashes(str_replace("''", "'", $val));
448 $checked = (is_int($value) ? ($value >> $i) & 1 : in_array($val, explode(",", $value), true));
4cead560 » jakubvrana
2009-08-21 Edit default values directly in table creation
449 echo " <label><input type='checkbox' name='fields[$name][$i]' value='" . (1 << $i) . "'" . ($checked ? ' checked' : '') . "$onchange>" . h($val) . '</label>';
61640ee6 » jakubvrana
2009-07-03 Move common functions
450 }
2204cefd » jakubvrana
2009-11-09 Use textarea for blob with disabled file uploads
451 } elseif (ereg('binary|blob', $field["type"]) && ini_get("file_uploads")) {
452 echo "<input type='file' name='$name'$onchange>";
453 } elseif (ereg('text|blob', $field["type"])) {
689699aa » jakubvrana
2009-07-28 Shortcut for htmlspecialchars
454 echo "<textarea name='fields[$name]' cols='50' rows='12'$onchange>" . h($value) . '</textarea>';
61640ee6 » jakubvrana
2009-07-03 Move common functions
455 } else {
456 // int(3) is only a display hint
457 $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));
ad237d30 » jakubvrana
2009-10-06 Enlarge fields for long strings
458 echo "<input name='fields[$name]' value='" . h($value) . "'" . ($maxlength ? " maxlength='$maxlength'" : "") . (ereg('char', $field["type"]) && $field["length"] > 20 ? " size='40'" : "") . "$onchange>";
61640ee6 » jakubvrana
2009-07-03 Move common functions
459 }
460 }
461 }
462
9a176b0c » jakubvrana
2009-08-31 Comments
463 /** Process edit input field
464 * @param one field from fields()
465 * @return string
466 */
a0def479 » jakubvrana
2009-07-27 Date localization
467 function process_input($field) {
64d616c0 » jakubvrana
2009-09-22 Rename get_dbh to connection
468 global $connection, $adminer;
a0def479 » jakubvrana
2009-07-27 Date localization
469 $idf = bracket_escape($field["field"]);
61640ee6 » jakubvrana
2009-07-03 Move common functions
470 $function = $_POST["function"][$idf];
471 $value = $_POST["fields"][$idf];
472 if ($field["type"] == "enum" ? $value == -1 : $function == "orig") {
473 return false;
474 } elseif ($field["type"] == "enum" || $field["auto_increment"] ? !strlen($value) : $function == "NULL") {
475 return "NULL";
476 } elseif ($field["type"] == "enum") {
4cead560 » jakubvrana
2009-08-21 Edit default values directly in table creation
477 return intval($value);
61640ee6 » jakubvrana
2009-07-03 Move common functions
478 } elseif ($field["type"] == "set") {
4cead560 » jakubvrana
2009-08-21 Edit default values directly in table creation
479 return array_sum((array) $value);
2204cefd » jakubvrana
2009-11-09 Use textarea for blob with disabled file uploads
480 } elseif (ereg('binary|blob', $field["type"]) && ini_get("file_uploads")) {
61640ee6 » jakubvrana
2009-07-03 Move common functions
481 $file = get_file($idf);
482 if (!is_string($file)) {
483 return false; //! report errors
484 }
91cb95ab » jakubvrana
2009-11-09 Remove unnecessary _binary
485 return $connection->quote($file);
61640ee6 » jakubvrana
2009-07-03 Move common functions
486 } else {
a0def479 » jakubvrana
2009-07-27 Date localization
487 return $adminer->processInput($field, $value, $function);
61640ee6 » jakubvrana
2009-07-03 Move common functions
488 }
489 }
490
9a176b0c » jakubvrana
2009-08-31 Comments
491 /** Print CSV row
492 * @param array
493 * @return null
494 */
61640ee6 » jakubvrana
2009-07-03 Move common functions
495 function dump_csv($row) {
496 foreach ($row as $key => $val) {
497 if (preg_match("~[\"\n,]~", $val) || (isset($val) && !strlen($val))) {
498 $row[$key] = '"' . str_replace('"', '""', $val) . '"';
499 }
500 }
89a60e77 » jakubvrana
2009-10-02 Use output buffering for dump compression
501 echo implode(",", $row) . "\n";
61640ee6 » jakubvrana
2009-07-03 Move common functions
502 }
c16c57b1 » jakubvrana
2009-07-11 E-mail sending
503
9a176b0c » jakubvrana
2009-08-31 Comments
504 /** Apply SQL function
505 * @param string
506 * @param string escaped column identifier
507 * @return string
508 */
e3310672 » jakubvrana
2009-08-18 Move apply_sql_function
509 function apply_sql_function($function, $column) {
510 return ($function ? ($function == "count distinct" ? "COUNT(DISTINCT " : strtoupper("$function(")) . "$column)" : $column);
511 }
512
9a176b0c » jakubvrana
2009-08-31 Comments
513 /** Check whether the string is e-mail address
514 * @param string
515 * @return bool
516 */
c16c57b1 » jakubvrana
2009-07-11 E-mail sending
517 function is_email($email) {
518 $atom = '[-a-z0-9!#$%&\'*+/=?^_`{|}~]'; // characters of local-name
519 $domain = '[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])'; // one domain component
fb141ccf » jakubvrana
2009-10-05 Speedup
520 return preg_match("(^$atom+(\\.$atom+)*@($domain?\\.)+$domain\$)i", $email);
c16c57b1 » jakubvrana
2009-07-11 E-mail sending
521 }
dc667ea1 » jakubvrana
2009-09-11 Hide select export and import
522
bc78866a » jakubvrana
2009-10-02 Link URLs in select
523 /** Check whether the string is URL address
524 * @param string
525 * @return bool
526 */
527 function is_url($string) {
528 $domain = '[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])'; // one domain component
529 return preg_match("~^https?://($domain?\\.)+$domain(:[0-9]+)?(/.*)?(\\?.*)?(#.*)?\$~i", $string); //! restrict path, query and fragment characters
530 }
531
dc667ea1 » jakubvrana
2009-09-11 Hide select export and import
532 /** Print header for hidden fieldset (close by </div></fieldset>)
533 * @param string
534 * @param string
535 * @param bool
536 * @return null
537 */
538 function print_fieldset($id, $legend, $visible = false) {
539 echo "<fieldset><legend><a href='#fieldset-$id' onclick=\"return !toggle('fieldset-$id');\">$legend</a></legend><div id='fieldset-$id'" . ($visible ? "" : " class='hidden'") . ">\n";
540 }
Something went wrong with that request. Please try again.