lang
[phpmyadmin/crack.git] / db_search.php3
blob3e96cbcc4640f7c280117dc862b7b5a1e62a4c76
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
4 /**
5 * Credits for this script goes to Thomas Chaumeny <chaume92 at aol.com>
6 */
9 /**
10 * Gets some core libraries and send headers
12 require('./db_details_common.php3');
13 // If config variable $cfg['Usedbsearch'] is on FALSE : exit.
14 if (!$cfg['UseDbSearch']) {
15 PMA_mysqlDie($strAccessDenied, '', FALSE, $err_url);
16 } // end if
17 $url_query .= '&amp;goto=db_search.php3';
20 /**
21 * Get the list of tables from the current database
23 $list_tables = PMA_mysql_list_tables($db);
24 $num_tables = ($list_tables ? mysql_num_rows($list_tables) : 0);
25 for ($i = 0; $i < $num_tables; $i++) {
26 $tables[] = PMA_mysql_tablename($list_tables, $i);
28 if ($num_tables) {
29 mysql_free_result($list_tables);
33 /**
34 * Displays top links
36 $sub_part = '';
37 require('./db_details_links.php3');
40 /**
41 * 1. Main search form has been submitted
43 if (isset($submit_search)) {
45 /**
46 * Builds the SQL search query
48 * @param string the table name
49 * @param string the string to search
50 * @param integer type of search (1 -> 1 word at least, 2 -> all words,
51 * 3 -> exact string, 4 -> regexp)
53 * @return array 3 SQL querys (for count, display and delete results)
55 * @global string the url to retun to in case of errors
57 function PMA_getSearchSqls($table, $search_str, $search_option)
59 global $err_url;
61 // Statement types
62 $sqlstr_select = 'SELECT';
63 $sqlstr_delete = 'DELETE';
65 // Fields to select
66 $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($GLOBALS['db']);
67 $res = @PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, FALSE, $err_url);
68 $res_cnt = ($res ? mysql_num_rows($res) : 0);
69 for ($i = 0; $i < $res_cnt; $i++) {
70 $tblfields[] = PMA_backquote(PMA_mysql_result($res, $i, 'field'));
71 } // end if
72 $sqlstr_fieldstoselect = ' ' . implode(', ', $tblfields);
73 $tblfields_cnt = count($tblfields);
74 if ($res) {
75 mysql_free_result($res);
78 // Table to use
79 $sqlstr_from = ' FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($table);
81 // Beginning of WHERE clause
82 $sqlstr_where = ' WHERE';
84 $search_words = (($search_option > 2) ? array($search_str) : explode(' ', $search_str));
85 $search_wds_cnt = count($search_words);
87 $like_or_regex = (($search_option == 4) ? 'REGEXP' : 'LIKE');
88 $automatic_wildcard = (($search_option <3) ? '%' : '');
90 for ($i = 0; $i < $search_wds_cnt; $i++) {
91 // Elimines empty values
92 if (!empty($search_words[$i])) {
93 for ($j = 0; $j < $tblfields_cnt; $j++) {
94 $thefieldlikevalue[] = $tblfields[$j]
95 . ' ' . $like_or_regex
96 . ' \''
97 . $automatic_wildcard
98 . $search_words[$i]
99 . $automatic_wildcard . '\'';
100 } // end for
102 $fieldslikevalues[] = ($search_wds_cnt > 1)
103 ? '(' . implode(' OR ', $thefieldlikevalue) . ')'
104 : implode(' OR ', $thefieldlikevalue);
105 unset($thefieldlikevalue);
106 } // end if
107 } // end for
109 $implode_str = ($search_option == 1 ? ' OR ' : ' AND ');
110 $sqlstr_where .= ' ' . implode($implode_str, $fieldslikevalues);
111 unset($fieldslikevalues);
113 // Builds complete queries
114 $sql['select_fields'] = $sqlstr_select . $sqlstr_fieldstoselect . $sqlstr_from . $sqlstr_where;
115 $sql['select_count'] = $sqlstr_select . ' COUNT(*) AS count' . $sqlstr_from . $sqlstr_where;
116 $sql['delete'] = $sqlstr_delete . $sqlstr_from . $sqlstr_where;
118 return $sql;
119 } // end of the "PMA_getSearchSqls()" function
123 * Displays the results
125 if (!empty($search_str) && !empty($search_option)) {
127 $original_search_str = $search_str;
128 $search_str = PMA_sqlAddslashes($search_str, TRUE);
130 // Get the true string to display as option's comment
131 switch ($search_option) {
132 case 1:
133 $option_str = ' (' . $strSearchOption1 . ')';
134 break;
135 case 2:
136 $option_str = ' (' . $strSearchOption2 . ')';
137 break;
138 case 3:
139 $option_str = ' (' . $strSearchOption3 . ')';
140 break;
141 case 4:
142 $option_str = ' (' . $strSearchOption4 . ')';
143 break;
144 } // end switch
146 // If $table is defined or if there is only one table in $table_select
147 // set $onetable to the table's name (display is different if there is
148 // only one table).
150 // Recall:
151 // $tables is an array with all tables in database $db
152 // $num_tables is the size of $tables
153 if (isset($table)) {
154 $onetable = $table;
156 else if (isset($table_select)) {
157 $num_selectedtables = count($table_select);
158 if ($num_selectedtables == 1) {
159 $onetable = $table_select[0];
162 else if ($num_tables == 1) {
163 $onetable = $tables[0];
165 else {
166 for ($i = 0; $i < $num_tables; $i++) {
167 $table_select[] = $tables[$i];
169 $num_selectedtables = $num_tables;
170 } // end if... else if... else
172 <br />
174 <?php
175 $url_sql_query = PMA_generate_common_url($db)
176 . '&amp;goto=db_details.php3'
177 . '&amp;pos=0'
178 . '&amp;is_js_confirmed=0';
180 // Only one table defined in an variable $onetable
181 if (isset($onetable)) {
182 // Displays search string
183 echo ' ' . sprintf($strSearchResultsFor, htmlspecialchars($original_search_str), $option_str) . "\n";
184 echo ' <br />' . "\n";
186 // Gets the SQL statements
187 $newsearchsqls = PMA_getSearchSqls($onetable, $search_str, $search_option);
189 // Executes the "COUNT" statement
190 $local_query = $newsearchsqls['select_count'];
191 $res = @PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, FALSE, $err_url);
192 if ($res) {
193 $res_cnt = PMA_mysql_result($res, 0, 'count');
194 mysql_free_result($res);
195 } else {
196 $res_cnt = 0;
197 } // end if... else ...
198 $num_search_result_total = $res_cnt;
200 echo ' <!-- Search results in table ' . $onetable . ' (' . $res_cnt . ') -->' . "\n"
201 . ' <br />' . "\n"
202 . ' <table><tr><td>' . sprintf($strNumSearchResultsInTable, $res_cnt, htmlspecialchars($onetable)) . "</td>\n";
204 if ($res_cnt > 0) {
205 echo '<td>' . PMA_linkOrButton('sql.php3?' . $url_sql_query
206 . '&amp;sql_query=' .urlencode($newsearchsqls['select_fields']),
207 $strBrowse, '') . "</td>\n";
209 echo '<td>' . PMA_linkOrButton('sql.php3?' . $url_sql_query
210 . '&amp;sql_query=' .urlencode($newsearchsqls['delete']),
211 $strDelete, $newsearchsqls['delete']) . "</td>\n";
213 } // end if
214 echo '</tr></table>' . "\n";
215 } // end only one table
217 // Several tables defined in the array $table_select
218 else if (isset($table_select)) {
219 // Displays search string
220 echo ' ' . sprintf($strSearchResultsFor, htmlspecialchars($original_search_str), $option_str) . "\n";
221 echo ' <ul>' . "\n";
223 $num_search_result_total = 0;
224 for ($i = 0; $i < $num_selectedtables; $i++) {
225 // Gets the SQL statements
226 $newsearchsqls = PMA_getSearchSqls($table_select[$i], $search_str, $search_option);
228 // Executes the "COUNT" statement
229 $local_query = $newsearchsqls['select_count'];
230 $res = @PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, FALSE, $err_url);
231 if ($res) {
232 $res_cnt = PMA_mysql_result($res, 0, 'count');
233 mysql_free_result($res);
234 } else {
235 $res_cnt = 0;
236 } // end if... else ...
237 $num_search_result_total += $res_cnt;
239 echo ' <!-- Search results in table ' . $table_select[$i] . ' (' . $res_cnt . ') -->' . "\n"
240 . ' <li>' . "\n"
241 . ' <table><tr><td>' . sprintf($strNumSearchResultsInTable, $res_cnt, htmlspecialchars($table_select[$i])) . "</td>\n";
243 if ($res_cnt > 0) {
244 echo '<td>' . PMA_linkOrButton('sql.php3?' . $url_sql_query
245 . '&amp;sql_query=' .urlencode($newsearchsqls['select_fields']),
246 $strBrowse, '') . "</td>\n";
248 echo '<td>' . PMA_linkOrButton('sql.php3?' . $url_sql_query
249 . '&amp;sql_query=' .urlencode($newsearchsqls['delete']),
250 $strDelete, $newsearchsqls['delete']) . "</td>\n";
252 } // end if
254 echo ' </tr></table></li>' . "\n";
255 } // end for
257 echo ' </ul>' . "\n";
258 echo ' <p>' . sprintf($strNumSearchResultsTotal, $num_search_result_total) . '</p>' . "\n";
259 } // end several tables
261 echo "\n";
263 <hr width="100%">
264 <?php
265 } // end if (!empty($search_str) && !empty($search_option))
267 } // end 1.
271 * 2. Displays the main search form
273 echo "\n";
274 $searched = (isset($original_search_str))
275 ? htmlspecialchars($original_search_str)
276 : '';
277 if (empty($search_option)) {
278 $search_option = 1;
281 <!-- Display search form -->
282 <p align="center">
283 <b><?php echo $strSearchFormTitle; ?></b>
284 </p>
286 <a name="db_search"></a>
287 <form method="post" action="db_search.php3" name="db_search">
288 <?php echo PMA_generate_common_hidden_inputs($db); ?>
290 <table>
291 <tr>
292 <td>
293 <?php echo $strSearchNeedle; ?>&nbsp;
294 </td>
295 <td>
296 <input type="text" name="search_str" size="30" value="<?php echo $searched; ?>" />
297 </td>
298 </tr>
299 <tr>
300 <td colspan="2">&nbsp;</td>
301 </tr>
302 <tr>
303 <td valign="top">
304 <?php echo $strSearchType; ?>&nbsp;
305 </td>
306 <td>
307 <input type="radio" id="search_option_1" name="search_option" value="1"<?php if ($search_option == 1) echo ' checked="checked"'; ?> />
308 <label for="search_option_1"><?php echo $strSearchOption1; ?></label>&nbsp;*<br />
309 <input type="radio" id="search_option_2" name="search_option" value="2"<?php if ($search_option == 2) echo ' checked="checked"'; ?> />
310 <label for="search_option_2"><?php echo $strSearchOption2; ?></label>&nbsp;*<br />
311 <input type="radio" id="search_option_3" name="search_option" value="3"<?php if ($search_option == 3) echo ' checked="checked"'; ?> />
312 <label for="search_option_3"><?php echo $strSearchOption3; ?></label><br />
313 <input type="radio" id="search_option_4" name="search_option" value="4"<?php if ($search_option == 4) echo ' checked="checked"'; ?> />
314 <label for="search_option_4"><?php echo $strSearchOption4 . '</label> ' . PMA_showMySQLDocu('Regexp', 'Regexp'); ?><br />
315 <br />
316 *&nbsp;<?php echo $strSplitWordsWithSpace . "\n"; ?>
317 </td>
318 </tr>
319 <tr>
320 <td colspan="2">&nbsp;</td>
321 </tr>
322 <tr>
323 <td valign="top">
324 <?php echo $strSearchInTables; ?>&nbsp;
325 </td>
326 <td>
327 <?php
328 if ($num_tables > 1) {
329 $i = 0;
331 echo ' <select name="table_select[]" size="6" multiple="multiple">' . "\n";
332 while ($i < $num_tables) {
333 if (!empty($unselectall)) {
334 $is_selected = '';
336 else if ((isset($table_select) && PMA_isInto($tables[$i], $table_select) != -1)
337 || (!empty($selectall))
338 || (isset($onetable) && $onetable == $tables[$i])) {
339 $is_selected = ' selected="selected"';
341 else {
342 $is_selected = '';
345 echo ' <option value="' . htmlspecialchars($tables[$i]) . '"' . $is_selected . '>' . htmlspecialchars($tables[$i]) . '</option>' . "\n";
346 $i++;
347 } // end while
348 echo ' </select>' . "\n";
350 <br />
351 <a href="db_search.php3?<?php echo $url_query; ?>&amp;selectall=1#db_search" onclick="setSelectOptions('db_search', 'table_select[]', true); return false;"><?php echo $strSelectAll; ?></a>
352 &nbsp;/&nbsp;
353 <a href="db_search.php3?<?php echo $url_query; ?>&amp;unselectall=1#db_search" onclick="setSelectOptions('db_search', 'table_select[]', false); return false;"><?php echo $strUnselectAll; ?></a>
354 <?php
356 else {
357 echo "\n";
358 echo ' ' . htmlspecialchars($tables[0]) . "\n";
359 echo ' <input type="hidden" name="table" value="' . htmlspecialchars($tables[0]) . '" />' . "\n";
360 } // end if... else...
362 echo"\n";
364 </td>
365 </tr>
367 <tr>
368 <td colspan="2">&nbsp;</td>
369 </tr>
370 <tr>
371 <td colspan="2"><input type="submit" name="submit_search" value="<?php echo $strGo; ?>" /></td>
372 </tr>
373 </table>
374 </form>
377 <?php
379 * Displays the footer
381 echo "\n";
382 require('./footer.inc.php3');