2.11.10 release
[phpmyadmin/crack.git] / db_search.php
blob2c7873bc62e6e7da1125d2bc64f9a20353b81f2a
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * searchs the entire database
6 * @todo make use of UNION when searching multiple tables
7 * @todo display executed query, optional?
8 * @uses $cfg['UseDbSearch']
9 * @uses $GLOBALS['db']
10 * @uses $GLOBALS['strAccessDenied']
11 * @uses $GLOBALS['strSearchOption1']
12 * @uses $GLOBALS['strSearchOption2']
13 * @uses $GLOBALS['strSearchOption3']
14 * @uses $GLOBALS['strSearchOption4']
15 * @uses $GLOBALS['strSearchResultsFor']
16 * @uses $GLOBALS['strNumSearchResultsInTable']
17 * @uses $GLOBALS['strBrowse']
18 * @uses $GLOBALS['strDelete']
19 * @uses $GLOBALS['strNumSearchResultsTotal']
20 * @uses $GLOBALS['strSearchFormTitle']
21 * @uses $GLOBALS['strSearchNeedle']
22 * @uses $GLOBALS['strSearchType']
23 * @uses $GLOBALS['strSplitWordsWithSpace']
24 * @uses $GLOBALS['strSearchInTables']
25 * @uses $GLOBALS['strUnselectAll']
26 * @uses $GLOBALS['strSelectAll']
27 * @uses PMA_DBI_get_tables()
28 * @uses PMA_sqlAddslashes()
29 * @uses PMA_getSearchSqls()
30 * @uses PMA_DBI_fetch_value()
31 * @uses PMA_linkOrButton()
32 * @uses PMA_generate_common_url()
33 * @uses PMA_generate_common_hidden_inputs()
34 * @uses PMA_showMySQLDocu()
35 * @uses $_REQUEST['search_str']
36 * @uses $_REQUEST['submit_search']
37 * @uses $_REQUEST['search_option']
38 * @uses $_REQUEST['table_select']
39 * @uses $_REQUEST['unselectall']
40 * @uses $_REQUEST['selectall']
41 * @uses is_string()
42 * @uses htmlspecialchars()
43 * @uses array_key_exists()
44 * @uses is_array()
45 * @uses array_intersect()
46 * @uses sprintf()
47 * @uses in_array()
48 * @version $Id$
49 * @author Thomas Chaumeny <chaume92 at aol.com>
52 /**
55 require_once './libraries/common.inc.php';
57 /**
58 * Gets some core libraries and send headers
60 require './libraries/db_common.inc.php';
62 /**
63 * init
65 // If config variable $GLOBALS['cfg']['Usedbsearch'] is on false : exit.
66 if (! $GLOBALS['cfg']['UseDbSearch']) {
67 PMA_mysqlDie($GLOBALS['strAccessDenied'], '', false, $err_url);
68 } // end if
69 $url_query .= '&amp;goto=db_search.php';
70 $url_params['goto'] = 'db_search.php';
72 /**
73 * @global array list of tables from the current database
74 * but do not clash with $tables coming from db_info.inc.php
76 $tables_names_only = PMA_DBI_get_tables($GLOBALS['db']);
78 $search_options = array(
79 '1' => $GLOBALS['strSearchOption1'],
80 '2' => $GLOBALS['strSearchOption2'],
81 '3' => $GLOBALS['strSearchOption3'],
82 '4' => $GLOBALS['strSearchOption4'],
85 if (empty($_REQUEST['search_option']) || ! is_string($_REQUEST['search_option'])
86 || ! array_key_exists($_REQUEST['search_option'], $search_options)) {
87 $search_option = 1;
88 unset($_REQUEST['submit_search']);
89 } else {
90 $search_option = (int) $_REQUEST['search_option'];
91 $option_str = $search_options[$_REQUEST['search_option']];
94 if (empty($_REQUEST['search_str']) || ! is_string($_REQUEST['search_str'])) {
95 unset($_REQUEST['submit_search']);
96 $searched = '';
97 } else {
98 $searched = htmlspecialchars($_REQUEST['search_str']);
99 // For "as regular expression" (search option 4), we should not treat
100 // this as an expression that contains a LIKE (second parameter of
101 // PMA_sqlAddslashes()).
103 // Usage example: If user is seaching for a literal $ in a regexp search,
104 // he should enter \$ as the value.
105 $search_str = PMA_sqlAddslashes($_REQUEST['search_str'], ($search_option == 4 ? false : true));
108 $tables_selected = array();
109 if (empty($_REQUEST['table_select']) || ! is_array($_REQUEST['table_select'])) {
110 unset($_REQUEST['submit_search']);
111 } elseif (! isset($_REQUEST['selectall']) && ! isset($_REQUEST['unselectall'])) {
112 $tables_selected = array_intersect($_REQUEST['table_select'], $tables_names_only);
115 if (isset($_REQUEST['selectall'])) {
116 $tables_selected = $tables_names_only;
117 } elseif (isset($_REQUEST['unselectall'])) {
118 $tables_selected = array();
122 * Displays top links
124 $sub_part = '';
125 require './libraries/db_info.inc.php';
129 * 1. Main search form has been submitted
131 if (isset($_REQUEST['submit_search'])) {
134 * Builds the SQL search query
136 * @todo can we make use of fulltextsearch IN BOOLEAN MODE for this?
137 * @uses PMA_DBI_query
138 * PMA_MYSQL_INT_VERSION
139 * PMA_backquote
140 * PMA_DBI_free_result
141 * PMA_DBI_fetch_assoc
142 * $GLOBALS['db']
143 * explode
144 * count
145 * strlen
146 * @param string the table name
147 * @param string the string to search
148 * @param integer type of search (1 -> 1 word at least, 2 -> all words,
149 * 3 -> exact string, 4 -> regexp)
151 * @return array 3 SQL querys (for count, display and delete results)
153 * @global string the url to return to in case of errors
154 * @global string charset connection
156 function PMA_getSearchSqls($table, $search_str, $search_option)
158 global $err_url, $charset_connection;
160 // Statement types
161 $sqlstr_select = 'SELECT';
162 $sqlstr_delete = 'DELETE';
164 // Fields to select
165 $res = PMA_DBI_query('SHOW ' . (PMA_MYSQL_INT_VERSION >= 40100 ? 'FULL ' : '') . 'FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($GLOBALS['db']) . ';');
166 while ($current = PMA_DBI_fetch_assoc($res)) {
167 if (PMA_MYSQL_INT_VERSION >= 40100) {
168 list($current['Charset']) = explode('_', $current['Collation']);
170 $current['Field'] = PMA_backquote($current['Field']);
171 $tblfields[] = $current;
172 } // while
173 PMA_DBI_free_result($res);
174 unset($current, $res);
176 // Table to use
177 $sqlstr_from = ' FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($table);
179 $search_words = (($search_option > 2) ? array($search_str) : explode(' ', $search_str));
180 $search_wds_cnt = count($search_words);
182 $like_or_regex = (($search_option == 4) ? 'REGEXP' : 'LIKE');
183 $automatic_wildcard = (($search_option <3) ? '%' : '');
185 $fieldslikevalues = array();
186 foreach ($search_words as $search_word) {
187 // Eliminates empty values
188 // In MySQL 4.1, if a field has no collation we get NULL in Charset
189 // but in MySQL 5.0.x we get ''
190 if (strlen($search_word) === 0) {
191 continue;
194 $thefieldlikevalue = array();
195 foreach ($tblfields as $tblfield) {
196 if (PMA_MYSQL_INT_VERSION >= 40100
197 && $tblfield['Charset'] != $charset_connection
198 && $tblfield['Charset'] != 'NULL'
199 && $tblfield['Charset'] != '') {
200 $prefix = 'CONVERT(_utf8 ';
201 $suffix = ' USING ' . $tblfield['Charset'] . ') COLLATE ' . $tblfield['Collation'];
202 } else {
203 $prefix = $suffix = '';
205 $thefieldlikevalue[] = $tblfield['Field']
206 . ' ' . $like_or_regex . ' '
207 . $prefix
208 . "'"
209 . $automatic_wildcard
210 . $search_word
211 . $automatic_wildcard . "'"
212 . $suffix;
213 } // end for
215 $fieldslikevalues[] = implode(' OR ', $thefieldlikevalue);
216 } // end for
218 $implode_str = ($search_option == 1 ? ' OR ' : ' AND ');
219 $sqlstr_where = ' WHERE (' . implode(') ' . $implode_str . ' (', $fieldslikevalues) . ')';
220 unset($fieldslikevalues);
222 // Builds complete queries
223 $sql['select_fields'] = $sqlstr_select . ' * ' . $sqlstr_from . $sqlstr_where;
224 // here, I think we need to still use the COUNT clause, even for
225 // VIEWs, anyway we have a WHERE clause that should limit results
226 $sql['select_count'] = $sqlstr_select . ' COUNT(*) AS `count`' . $sqlstr_from . $sqlstr_where;
227 $sql['delete'] = $sqlstr_delete . $sqlstr_from . $sqlstr_where;
228 return $sql;
229 } // end of the "PMA_getSearchSqls()" function
233 * Displays the results
235 $this_url_params = array(
236 'db' => $GLOBALS['db'],
237 'goto' => 'db_sql.php',
238 'pos' => 0,
239 'is_js_confirmed' => 0,
242 // Displays search string
243 echo '<br />' . "\n"
244 .'<table class="data">' . "\n"
245 .'<caption class="tblHeaders">' . "\n"
246 .sprintf($GLOBALS['strSearchResultsFor'],
247 $searched, $option_str) . "\n"
248 .'</caption>' . "\n";
250 $num_search_result_total = 0;
251 $odd_row = true;
253 foreach ($tables_selected as $each_table) {
254 // Gets the SQL statements
255 $newsearchsqls = PMA_getSearchSqls($each_table,
256 $search_str, $search_option);
258 // Executes the "COUNT" statement
259 $res_cnt = PMA_DBI_fetch_value($newsearchsqls['select_count']);
260 $num_search_result_total += $res_cnt;
262 $sql_query .= $newsearchsqls['select_count'];
264 echo '<tr class="' . ($odd_row ? 'odd' : 'even') . '">'
265 .'<td>' . sprintf($GLOBALS['strNumSearchResultsInTable'], $res_cnt,
266 htmlspecialchars($each_table)) . "</td>\n";
268 if ($res_cnt > 0) {
269 $this_url_params['sql_query'] = $newsearchsqls['select_fields'];
270 echo '<td>' . PMA_linkOrButton(
271 'sql.php' . PMA_generate_common_url($this_url_params),
272 $GLOBALS['strBrowse'], '') . "</td>\n";
274 $this_url_params['sql_query'] = $newsearchsqls['delete'];
275 echo '<td>' . PMA_linkOrButton(
276 'sql.php' . PMA_generate_common_url($this_url_params),
277 $GLOBALS['strDelete'], $newsearchsqls['delete']) . "</td>\n";
279 } else {
280 echo '<td>&nbsp;</td>' . "\n"
281 .'<td>&nbsp;</td>' . "\n";
282 }// end if else
283 $odd_row = ! $odd_row;
284 echo '</tr>' . "\n";
285 } // end for
287 echo '</table>' . "\n";
289 if (count($tables_selected) > 1) {
290 echo '<p>' . sprintf($GLOBALS['strNumSearchResultsTotal'],
291 $num_search_result_total) . '</p>' . "\n";
293 } // end 1.
297 * 2. Displays the main search form
300 <a name="db_search"></a>
301 <form method="post" action="db_search.php" name="db_search">
302 <?php echo PMA_generate_common_hidden_inputs($GLOBALS['db']); ?>
303 <fieldset>
304 <legend><?php echo $GLOBALS['strSearchFormTitle']; ?></legend>
306 <table class="formlayout">
307 <tr><td><?php echo $GLOBALS['strSearchNeedle']; ?></td>
308 <td><input type="text" name="search_str" size="60"
309 value="<?php echo $searched; ?>" /></td>
310 </tr>
311 <tr><td align="right" valign="top">
312 <?php echo $GLOBALS['strSearchType']; ?></td>
313 <td><input type="radio" id="search_option_1" name="search_option"
314 value="1"<?php if ($search_option == 1) echo ' checked="checked"'; ?> />
315 <label for="search_option_1">
316 <?php echo $GLOBALS['strSearchOption1']; ?></label><sup>1</sup><br />
317 <input type="radio" id="search_option_2" name="search_option"
318 value="2"<?php if ($search_option == 2) echo ' checked="checked"'; ?> />
319 <label for="search_option_2">
320 <?php echo $GLOBALS['strSearchOption2']; ?></label><sup>1</sup><br />
321 <input type="radio" id="search_option_3" name="search_option"
322 value="3"<?php if ($search_option == 3) echo ' checked="checked"'; ?> />
323 <label for="search_option_3">
324 <?php echo $GLOBALS['strSearchOption3']; ?></label><br />
325 <input type="radio" id="search_option_4" name="search_option"
326 value="4"<?php if ($search_option == 4) echo ' checked="checked"'; ?> />
327 <label for="search_option_4">
328 <?php echo $GLOBALS['strSearchOption4']; ?></label>
330 <?php echo PMA_showMySQLDocu('Regexp', 'Regexp'); ?><br />
331 <br />
332 <sup>1</sup><?php echo $GLOBALS['strSplitWordsWithSpace']; ?></td>
333 </tr>
334 <tr><td align="right" valign="top">
335 <?php echo $GLOBALS['strSearchInTables']; ?></td>
336 <td rowspan="2">
337 <?php
338 echo ' <select name="table_select[]" size="6" multiple="multiple">' . "\n";
339 foreach ($tables_names_only as $each_table) {
340 if (in_array($each_table, $tables_selected)) {
341 $is_selected = ' selected="selected"';
342 } else {
343 $is_selected = '';
346 echo ' <option value="' . htmlspecialchars($each_table) . '"'
347 . $is_selected . '>'
348 . str_replace(' ', '&nbsp;', htmlspecialchars($each_table)) . '</option>' . "\n";
349 } // end while
351 echo ' </select>' . "\n";
352 $alter_select =
353 '<a href="db_search.php' . PMA_generate_common_url(array_merge($url_params, array('selectall' => 1))) . '#db_search"'
354 . ' onclick="setSelectOptions(\'db_search\', \'table_select[]\', true); return false;">' . $GLOBALS['strSelectAll'] . '</a>'
355 . '&nbsp;/&nbsp;'
356 . '<a href="db_search.php' . PMA_generate_common_url(array_merge($url_params, array('unselectall' => 1))) . '#db_search"'
357 . ' onclick="setSelectOptions(\'db_search\', \'table_select[]\', false); return false;">' . $GLOBALS['strUnselectAll'] . '</a>';
359 </td>
360 </tr>
361 <tr><td align="right" valign="bottom">
362 <?php echo $alter_select; ?></td></tr>
363 </tr>
364 </table>
365 </fieldset>
366 <fieldset class="tblFooters">
367 <input type="submit" name="submit_search" value="<?php echo $GLOBALS['strGo']; ?>"
368 id="buttonGo" />
369 </fieldset>
370 </form>
372 <?php
374 * Displays the footer
376 require_once './libraries/footer.inc.php';