Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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