Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 261 lines (240 sloc) 8.459 kb
667bfec Decomposition
jakubvrana authored
1 <?php
2 function idf_escape($idf) {
3 return "`" . str_replace("`", "``", $idf) . "`";
4 }
5
6 function idf_unescape($idf) {
7 return str_replace("``", "`", $idf);
8 }
9
10 function bracket_escape($idf, $back = false) {
64ba924 Comments
jakubvrana authored
11 // escape brackets inside name="x[]"
73e8631 Move stuff to functions.inc.php
jakubvrana authored
12 static $trans = array(':' => ':1', ']' => ':2', '[' => ':3');
667bfec Decomposition
jakubvrana authored
13 return strtr($idf, ($back ? array_flip($trans) : $trans));
14 }
15
78639a9 Function results in edit
jakubvrana authored
16 function optionlist($options, $selected = null) {
667bfec Decomposition
jakubvrana authored
17 $return = "";
18 foreach ($options as $k => $v) {
19 if (is_array($v)) {
20 $return .= '<optgroup label="' . htmlspecialchars($k) . '">';
21 }
a9eab4c Space in type (bug 1896054)
jakubvrana authored
22 foreach ((is_array($v) ? $v : array($v)) as $val) {
78639a9 Function results in edit
jakubvrana authored
23 $return .= '<option' . ($val === $selected ? ' selected="selected"' : '') . '>' . htmlspecialchars($val) . '</option>';
667bfec Decomposition
jakubvrana authored
24 }
25 if (is_array($v)) {
26 $return .= '</optgroup>';
27 }
28 }
29 return $return;
30 }
31
0bd864e Allow specifying column in get_vals
jakubvrana authored
32 function get_vals($query, $column = 0) {
5f16c75 Prepare for version 2
jakubvrana authored
33 global $dbh;
7769f2c Separate get_vals
jakubvrana authored
34 $return = array();
5f16c75 Prepare for version 2
jakubvrana authored
35 $result = $dbh->query($query);
49f7886 SHOW DATABASES can be revoked
jakubvrana authored
36 if ($result) {
37 while ($row = $result->fetch_row()) {
0bd864e Allow specifying column in get_vals
jakubvrana authored
38 $return[] = $row[$column];
49f7886 SHOW DATABASES can be revoked
jakubvrana authored
39 }
40 $result->free();
7769f2c Separate get_vals
jakubvrana authored
41 }
42 return $return;
43 }
44
667bfec Decomposition
jakubvrana authored
45 function unique_idf($row, $indexes) {
720f5fc Restructure indexes
jakubvrana authored
46 foreach ($indexes as $index) {
47 if ($index["type"] == "PRIMARY" || $index["type"] == "UNIQUE") {
48 $return = array();
49 foreach ($index["columns"] as $key) {
64ba924 Comments
jakubvrana authored
50 if (!isset($row[$key])) { // NULL is ambiguous
720f5fc Restructure indexes
jakubvrana authored
51 continue 2;
667bfec Decomposition
jakubvrana authored
52 }
430034c Cottage homework
jakubvrana authored
53 $return[] = urlencode("where[" . bracket_escape($key) . "]") . "=" . urlencode($row[$key]);
667bfec Decomposition
jakubvrana authored
54 }
720f5fc Restructure indexes
jakubvrana authored
55 return $return;
667bfec Decomposition
jakubvrana authored
56 }
57 }
58 $return = array();
59 foreach ($row as $key => $val) {
430034c Cottage homework
jakubvrana authored
60 $return[] = (isset($val) ? urlencode("where[" . bracket_escape($key) . "]") . "=" . urlencode($val) : "null%5B%5D=" . urlencode($key));
61 }
62 return $return;
63 }
64
9ea3112 Mandatory $where in where()
jakubvrana authored
65 function where($where) {
5f16c75 Prepare for version 2
jakubvrana authored
66 global $dbh;
430034c Cottage homework
jakubvrana authored
67 $return = array();
116faf2 Mass delete
jakubvrana authored
68 foreach ((array) $where["where"] as $key => $val) {
581bd35 Respect function results in where()
jakubvrana authored
69 $key = bracket_escape($key, "back");
5f16c75 Prepare for version 2
jakubvrana authored
70 $return[] = (preg_match('~^[A-Z0-9_]+\\(`(?:[^`]+|``)+`\\)$~', $key) ? $key : idf_escape($key)) . " = BINARY '" . $dbh->escape_string($val) . "'"; //! enum and set, columns looking like functions
430034c Cottage homework
jakubvrana authored
71 }
116faf2 Mass delete
jakubvrana authored
72 foreach ((array) $where["null"] as $key) {
581bd35 Respect function results in where()
jakubvrana authored
73 $key = bracket_escape($key, "back");
74 $return[] = (preg_match('~^[A-Z0-9_]+\\(`(?:[^`]+|``)+`\\)$~', $key) ? $key : idf_escape($key)) . " IS NULL";
667bfec Decomposition
jakubvrana authored
75 }
76 return $return;
77 }
78
0cd2afb Remove single item clone
jakubvrana authored
79 function where_check($val) {
80 parse_str($val, $check);
81 return where($check);
82 }
83
7aaec14 Separate process_length()
jakubvrana authored
84 function process_length($length) {
85 global $enum_length;
baac444 Allow +- in unquoted IN (bug #2784541)
jakubvrana authored
86 return (preg_match("~^\\s*(?:$enum_length)(?:\\s*,\\s*(?:$enum_length))*\\s*\$~", $length) && preg_match_all("~$enum_length~", $length, $matches) ? implode(",", $matches[0]) : preg_replace('~[^0-9,+-]~', '', $length));
7aaec14 Separate process_length()
jakubvrana authored
87 }
88
430034c Cottage homework
jakubvrana authored
89 function redirect($location, $message = null) {
90 if (isset($message)) {
ff74bf0 Allow multiple messages
jakubvrana authored
91 $_SESSION["messages"][] = $message;
430034c Cottage homework
jakubvrana authored
92 }
93 if (strlen(SID)) {
64ba924 Comments
jakubvrana authored
94 // append SID if session cookies are disabled
430034c Cottage homework
jakubvrana authored
95 $location .= (strpos($location, "?") === false ? "?" : "&") . SID;
96 }
97 header("Location: " . (strlen($location) ? $location : "."));
98 exit;
99 }
100
748ee83 Print SQL command with multiple queries
jakubvrana authored
101 function query_redirect($query, $location, $message, $redirect = true, $execute = true, $failed = false) {
5f16c75 Prepare for version 2
jakubvrana authored
102 global $dbh, $error, $SELF;
06faef2 Print SQL query by messages
jakubvrana authored
103 $id = "sql-" . count($_SESSION["messages"]);
10bdbed Save queries to history
jakubvrana authored
104 $sql = "";
105 if ($query) {
e162856 Wrap lines of SQL commands in IE
jakubvrana authored
106 $sql = " <a href='#$id' onclick=\"return !toggle('$id');\">" . lang('SQL command') . "</a><span id='$id' class='hidden'><pre class='jush-sql'>" . htmlspecialchars($query) . '</pre><a href="' . htmlspecialchars($SELF . 'sql=&history=' . count($_SESSION["history"][$_GET["server"]][$_GET["db"]])) . '">' . lang('Edit') . '</a></span>';
66a04f3 Separate history for server and database
jakubvrana authored
107 $_SESSION["history"][$_GET["server"]][$_GET["db"]][] = $query;
10bdbed Save queries to history
jakubvrana authored
108 }
748ee83 Print SQL command with multiple queries
jakubvrana authored
109 if ($execute) {
5f16c75 Prepare for version 2
jakubvrana authored
110 $failed = !$dbh->query($query);
748ee83 Print SQL command with multiple queries
jakubvrana authored
111 }
112 if ($failed) {
5f16c75 Prepare for version 2
jakubvrana authored
113 $error = htmlspecialchars($dbh->error) . $sql;
c48a017 Print SQL query by error
jakubvrana authored
114 return false;
f91b2c4 Print SQL query by message
jakubvrana authored
115 }
c48a017 Print SQL query by error
jakubvrana authored
116 if ($redirect) {
117 redirect($location, $message . $sql);
118 }
119 return true;
f91b2c4 Print SQL query by message
jakubvrana authored
120 }
121
748ee83 Print SQL command with multiple queries
jakubvrana authored
122 function queries($query = null) {
5f16c75 Prepare for version 2
jakubvrana authored
123 global $dbh;
748ee83 Print SQL command with multiple queries
jakubvrana authored
124 static $queries = array();
125 if (!isset($query)) {
64ba924 Comments
jakubvrana authored
126 // return executed queries without parameter
36950eb Tables and views
jakubvrana authored
127 return implode(";\n", $queries);
748ee83 Print SQL command with multiple queries
jakubvrana authored
128 }
129 $queries[] = $query;
5f16c75 Prepare for version 2
jakubvrana authored
130 return $dbh->query($query);
748ee83 Print SQL command with multiple queries
jakubvrana authored
131 }
132
d41d446 remove_from_uri()
jakubvrana authored
133 function remove_from_uri($param = "") {
6272cae Unconditionally remove session_name() in remove_from_uri()
jakubvrana authored
134 $param = "($param|" . session_name() . ")";
d41d446 remove_from_uri()
jakubvrana authored
135 return preg_replace("~\\?$param=[^&]*&~", '?', preg_replace("~\\?$param=[^&]*\$|&$param=[^&]*~", '', $_SERVER["REQUEST_URI"]));
136 }
137
3bda945 Rename confusing print_page
jakubvrana authored
138 function print_pagination($page) {
1456283 Pages on last page
jakubvrana authored
139 echo " " . ($page == $_GET["page"] ? $page + 1 : '<a href="' . htmlspecialchars(remove_from_uri("page") . ($page ? "&page=$page" : "")) . '">' . ($page + 1) . "</a>");
140 }
141
430034c Cottage homework
jakubvrana authored
142 function get_file($key) {
64ba924 Comments
jakubvrana authored
143 // returns int for error, string otherwise
430034c Cottage homework
jakubvrana authored
144 if (isset($_POST["files"][$key])) {
64ba924 Comments
jakubvrana authored
145 // get the file from hidden field if the user was logged out
430034c Cottage homework
jakubvrana authored
146 $length = strlen($_POST["files"][$key]);
4d38c7d Protection against big POST data
jakubvrana authored
147 return ($length && $length < 4 ? intval($_POST["files"][$key]) : base64_decode($_POST["files"][$key]));
430034c Cottage homework
jakubvrana authored
148 }
149 return (!$_FILES[$key] || $_FILES[$key]["error"] ? $_FILES[$key]["error"] : file_get_contents($_FILES[$key]["tmp_name"]));
150 }
151
4e5b126 Highlight odd and hover rows
jakubvrana authored
152 function odd($s = ' class="odd"') {
153 static $i = 0;
154 if (!$s) { // reset counter
1b97f48 Reset odd() for each result
jakubvrana authored
155 $i = -1;
4e5b126 Highlight odd and hover rows
jakubvrana authored
156 }
1b97f48 Reset odd() for each result
jakubvrana authored
157 return ($i++ % 2 ? $s : '');
4e5b126 Highlight odd and hover rows
jakubvrana authored
158 }
159
fbebb08 Use separate connection for exploring indexes
jakubvrana authored
160 function select($result, $dbh2 = null) {
a01944e Primary key links
jakubvrana authored
161 global $SELF;
ae372aa Database abstraction
jakubvrana authored
162 if (!$result->num_rows) {
430034c Cottage homework
jakubvrana authored
163 echo "<p class='message'>" . lang('No rows.') . "</p>\n";
164 } else {
be8f88e Don't wrap select results
jakubvrana authored
165 echo "<table cellspacing='0' class='nowrap'>\n";
64ba924 Comments
jakubvrana authored
166 $links = array(); // colno => orgtable - create links from these columns
167 $indexes = array(); // orgtable => array(column => colno) - primary keys
168 $columns = array(); // orgtable => array(column => ) - not selected columns in primary key
169 $blobs = array(); // colno => bool - display bytes for blobs
170 $types = array(); // colno => type - display char in <code>
171 odd(''); // reset odd for each result
ae372aa Database abstraction
jakubvrana authored
172 for ($i=0; $row = $result->fetch_row(); $i++) {
430034c Cottage homework
jakubvrana authored
173 if (!$i) {
174 echo "<thead><tr>";
175 for ($j=0; $j < count($row); $j++) {
ae372aa Database abstraction
jakubvrana authored
176 $field = $result->fetch_field();
038e688 Primary key flag is not always set
jakubvrana authored
177 if (strlen($field->orgtable)) {
ae372aa Database abstraction
jakubvrana authored
178 if (!isset($indexes[$field->orgtable])) {
64ba924 Comments
jakubvrana authored
179 // find primary key in each table
ae372aa Database abstraction
jakubvrana authored
180 $indexes[$field->orgtable] = array();
fbebb08 Use separate connection for exploring indexes
jakubvrana authored
181 foreach (indexes($field->orgtable, $dbh2) as $index) {
430034c Cottage homework
jakubvrana authored
182 if ($index["type"] == "PRIMARY") {
ae372aa Database abstraction
jakubvrana authored
183 $indexes[$field->orgtable] = array_flip($index["columns"]);
430034c Cottage homework
jakubvrana authored
184 break;
185 }
186 }
ae372aa Database abstraction
jakubvrana authored
187 $columns[$field->orgtable] = $indexes[$field->orgtable];
430034c Cottage homework
jakubvrana authored
188 }
a01944e Primary key links
jakubvrana authored
189 if (isset($columns[$field->orgtable][$field->orgname])) {
190 unset($columns[$field->orgtable][$field->orgname]);
191 $indexes[$field->orgtable][$field->orgname] = $j;
192 $links[$j] = $field->orgtable;
193 }
430034c Cottage homework
jakubvrana authored
194 }
ae372aa Database abstraction
jakubvrana authored
195 if ($field->charsetnr == 63) {
430034c Cottage homework
jakubvrana authored
196 $blobs[$j] = true;
197 }
ae6892d Display char type in <code>
jakubvrana authored
198 $types[$j] = $field->type;
430034c Cottage homework
jakubvrana authored
199 echo "<th>" . htmlspecialchars($field->name) . "</th>";
200 }
201 echo "</tr></thead>\n";
202 }
4e5b126 Highlight odd and hover rows
jakubvrana authored
203 echo "<tr" . odd() . ">";
430034c Cottage homework
jakubvrana authored
204 foreach ($row as $key => $val) {
205 if (!isset($val)) {
206 $val = "<i>NULL</i>";
207 } else {
017ffb5 Better UTF-8 detection
jakubvrana authored
208 if ($blobs[$key] && !is_utf8($val)) {
8251896 Downloaded file name
jakubvrana authored
209 $val = "<i>" . lang('%d byte(s)', strlen($val)) . "</i>"; //! link to download
4c75d84 Use &nbsp;
jakubvrana authored
210 } elseif (!strlen(trim($val))) {
64ba924 Comments
jakubvrana authored
211 $val = "&nbsp;"; // some content to print a border
e9ee9b8 Reformat
jakubvrana authored
212 } else {
fbebb08 Use separate connection for exploring indexes
jakubvrana authored
213 $val = nl2br(htmlspecialchars($val));
ae6892d Display char type in <code>
jakubvrana authored
214 if ($types[$key] == 254) {
215 $val = "<code>$val</code>";
216 }
e9ee9b8 Reformat
jakubvrana authored
217 }
430034c Cottage homework
jakubvrana authored
218 if (isset($links[$key]) && !$columns[$links[$key]]) {
219 $link = "edit=" . urlencode($links[$key]);
220 foreach ($indexes[$links[$key]] as $col => $j) {
221 $link .= "&amp;where" . urlencode("[" . bracket_escape($col) . "]") . "=" . urlencode($row[$j]);
222 }
223 $val = '<a href="' . htmlspecialchars($SELF) . $link . '">' . $val . '</a>';
224 }
225 }
226 echo "<td>$val</td>";
227 }
228 echo "</tr>\n";
229 }
230 echo "</table>\n";
231 }
ae372aa Database abstraction
jakubvrana authored
232 $result->free();
73e8631 Move stuff to functions.inc.php
jakubvrana authored
233 }
234
017ffb5 Better UTF-8 detection
jakubvrana authored
235 function is_utf8($val) {
64ba924 Comments
jakubvrana authored
236 // don't print control chars except \t\r\n
017ffb5 Better UTF-8 detection
jakubvrana authored
237 return (preg_match('~~u', $val) && !preg_match('~[\\0-\\x8\\xB\\xC\\xE-\\x1F]~', $val));
238 }
239
cd07584 Save SQL query to history
jakubvrana authored
240 function shorten_utf8($string, $length = 80, $suffix = "") {
d044ae0 Simplify shorten_utf8
jakubvrana authored
241 preg_match("~^(.{0,$length})(.?)~su", $string, $match);
cd07584 Save SQL query to history
jakubvrana authored
242 return htmlspecialchars($match[1]) . $suffix . ($match[2] ? "<em>...</em>" : "");
02a851c Shorten texts in PHP
jakubvrana authored
243 }
3920ebb Remove InnoDB table comment in Tables and views
jakubvrana authored
244
8251896 Downloaded file name
jakubvrana authored
245 function friendly_url($val) {
64ba924 Comments
jakubvrana authored
246 // used for blobs and export
8251896 Downloaded file name
jakubvrana authored
247 return preg_replace('~[^a-z0-9_]~i', '-', $val);
248 }
249
820c882 Bulk clone
jakubvrana authored
250 function hidden_fields($process, $ignore = array()) {
5d7071a Choose language through option-list
jakubvrana authored
251 while (list($key, $val) = each($process)) {
252 if (is_array($val)) {
253 foreach ($val as $k => $v) {
254 $process[$key . "[$k]"] = $v;
255 }
256 } elseif (!in_array($key, $ignore)) {
257 echo '<input type="hidden" name="' . htmlspecialchars($key) . '" value="' . htmlspecialchars($val) . '" />';
258 }
259 }
260 }
Something went wrong with that request. Please try again.