<br> after "Page number" only in navi, thanks to Juergen Wind
[phpmyadmin/crack.git] / db_search.php
blobba8f8b9684028ec2bfc07b35dd3bf18783ae5356
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_str']) || ! is_string($_REQUEST['search_str'])) {
86 unset($_REQUEST['submit_search']);
87 $searched = '';
88 } else {
89 $searched = htmlspecialchars($_REQUEST['search_str']);
90 $search_str = PMA_sqlAddslashes($_REQUEST['search_str'], true);
93 if (empty($_REQUEST['search_option']) || ! is_string($_REQUEST['search_option'])
94 || ! array_key_exists($_REQUEST['search_option'], $search_options)) {
95 $search_option = 1;
96 unset($_REQUEST['submit_search']);
97 } else {
98 $search_option = (int) $_REQUEST['search_option'];
99 $option_str = $search_options[$_REQUEST['search_option']];
102 $tables_selected = array();
103 if (empty($_REQUEST['table_select']) || ! is_array($_REQUEST['table_select'])) {
104 unset($_REQUEST['submit_search']);
105 } elseif (! isset($_REQUEST['selectall']) && ! isset($_REQUEST['unselectall'])) {
106 $tables_selected = array_intersect($_REQUEST['table_select'], $tables_names_only);
109 if (isset($_REQUEST['selectall'])) {
110 $tables_selected = $tables_names_only;
111 } elseif (isset($_REQUEST['unselectall'])) {
112 $tables_selected = array();
116 * Displays top links
118 $sub_part = '';
119 require './libraries/db_info.inc.php';
123 * 1. Main search form has been submitted
125 if (isset($_REQUEST['submit_search'])) {
128 * Builds the SQL search query
130 * @todo can we make use of fulltextsearch IN BOOLEAN MODE for this?
131 * @uses PMA_DBI_query
132 * PMA_MYSQL_INT_VERSION
133 * PMA_backquote
134 * PMA_DBI_free_result
135 * PMA_DBI_fetch_assoc
136 * $GLOBALS['db']
137 * explode
138 * count
139 * strlen
140 * @param string the table name
141 * @param string the string to search
142 * @param integer type of search (1 -> 1 word at least, 2 -> all words,
143 * 3 -> exact string, 4 -> regexp)
145 * @return array 3 SQL querys (for count, display and delete results)
147 * @global string the url to return to in case of errors
148 * @global string charset connection
150 function PMA_getSearchSqls($table, $search_str, $search_option)
152 global $err_url, $charset_connection;
154 // Statement types
155 $sqlstr_select = 'SELECT';
156 $sqlstr_delete = 'DELETE';
158 // Fields to select
159 $res = PMA_DBI_query('SHOW ' . (PMA_MYSQL_INT_VERSION >= 40100 ? 'FULL ' : '') . 'FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($GLOBALS['db']) . ';');
160 while ($current = PMA_DBI_fetch_assoc($res)) {
161 if (PMA_MYSQL_INT_VERSION >= 40100) {
162 list($current['Charset']) = explode('_', $current['Collation']);
164 $current['Field'] = PMA_backquote($current['Field']);
165 $tblfields[] = $current;
166 } // while
167 PMA_DBI_free_result($res);
168 unset($current, $res);
170 // Table to use
171 $sqlstr_from = ' FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($table);
173 $search_words = (($search_option > 2) ? array($search_str) : explode(' ', $search_str));
174 $search_wds_cnt = count($search_words);
176 $like_or_regex = (($search_option == 4) ? 'REGEXP' : 'LIKE');
177 $automatic_wildcard = (($search_option <3) ? '%' : '');
179 $fieldslikevalues = array();
180 foreach ($search_words as $search_word) {
181 // Eliminates empty values
182 // In MySQL 4.1, if a field has no collation we get NULL in Charset
183 // but in MySQL 5.0.x we get ''
184 if (strlen($search_word) === 0) {
185 continue;
188 $thefieldlikevalue = array();
189 foreach ($tblfields as $tblfield) {
190 if (PMA_MYSQL_INT_VERSION >= 40100
191 && $tblfield['Charset'] != $charset_connection
192 && $tblfield['Charset'] != 'NULL'
193 && $tblfield['Charset'] != '') {
194 $prefix = 'CONVERT(_utf8 ';
195 $suffix = ' USING ' . $tblfield['Charset'] . ') COLLATE ' . $tblfield['Collation'];
196 } else {
197 $prefix = $suffix = '';
199 $thefieldlikevalue[] = $tblfield['Field']
200 . ' ' . $like_or_regex . ' '
201 . $prefix
202 . "'"
203 . $automatic_wildcard
204 . $search_word
205 . $automatic_wildcard . "'"
206 . $suffix;
207 } // end for
209 $fieldslikevalues[] = implode(' OR ', $thefieldlikevalue);
210 } // end for
212 $implode_str = ($search_option == 1 ? ' OR ' : ' AND ');
213 $sqlstr_where = ' WHERE (' . implode(') ' . $implode_str . ' (', $fieldslikevalues) . ')';
214 unset($fieldslikevalues);
216 // Builds complete queries
217 $sql['select_fields'] = $sqlstr_select . ' * ' . $sqlstr_from . $sqlstr_where;
218 // here, I think we need to still use the COUNT clause, even for
219 // VIEWs, anyway we have a WHERE clause that should limit results
220 $sql['select_count'] = $sqlstr_select . ' COUNT(*) AS `count`' . $sqlstr_from . $sqlstr_where;
221 $sql['delete'] = $sqlstr_delete . $sqlstr_from . $sqlstr_where;
223 return $sql;
224 } // end of the "PMA_getSearchSqls()" function
228 * Displays the results
230 $this_url_params = array(
231 'db' => $GLOBALS['db'],
232 'goto' => 'db_sql.php',
233 'pos' => 0,
234 'is_js_confirmed' => 0,
237 // Displays search string
238 echo '<br />' . "\n"
239 .'<table class="data">' . "\n"
240 .'<caption class="tblHeaders">' . "\n"
241 .sprintf($GLOBALS['strSearchResultsFor'],
242 $searched, $option_str) . "\n"
243 .'</caption>' . "\n";
245 $num_search_result_total = 0;
246 $odd_row = true;
248 foreach ($tables_selected as $each_table) {
249 // Gets the SQL statements
250 $newsearchsqls = PMA_getSearchSqls($each_table,
251 $search_str, $search_option);
253 // Executes the "COUNT" statement
254 $res_cnt = PMA_DBI_fetch_value($newsearchsqls['select_count']);
255 $num_search_result_total += $res_cnt;
257 $sql_query .= $newsearchsqls['select_count'];
259 echo '<tr class="' . ($odd_row ? 'odd' : 'even') . '">'
260 .'<td>' . sprintf($GLOBALS['strNumSearchResultsInTable'], $res_cnt,
261 htmlspecialchars($each_table)) . "</td>\n";
263 if ($res_cnt > 0) {
264 $this_url_params['sql_query'] = $newsearchsqls['select_fields'];
265 echo '<td>' . PMA_linkOrButton(
266 'sql.php' . PMA_generate_common_url($this_url_params),
267 $GLOBALS['strBrowse'], '') . "</td>\n";
269 $this_url_params['sql_query'] = $newsearchsqls['delete'];
270 echo '<td>' . PMA_linkOrButton(
271 'sql.php' . PMA_generate_common_url($this_url_params),
272 $GLOBALS['strDelete'], $newsearchsqls['delete']) . "</td>\n";
274 } else {
275 echo '<td>&nbsp;</td>' . "\n"
276 .'<td>&nbsp;</td>' . "\n";
277 }// end if else
278 $odd_row = ! $odd_row;
279 echo '</tr>' . "\n";
280 } // end for
282 echo '</table>' . "\n";
284 if (count($tables_selected) > 1) {
285 echo '<p>' . sprintf($GLOBALS['strNumSearchResultsTotal'],
286 $num_search_result_total) . '</p>' . "\n";
288 } // end 1.
292 * 2. Displays the main search form
295 <a name="db_search"></a>
296 <form method="post" action="db_search.php" name="db_search">
297 <?php echo PMA_generate_common_hidden_inputs($GLOBALS['db']); ?>
298 <fieldset>
299 <legend><?php echo $GLOBALS['strSearchFormTitle']; ?></legend>
301 <table class="formlayout">
302 <tr><td><?php echo $GLOBALS['strSearchNeedle']; ?></td>
303 <td><input type="text" name="search_str" size="60"
304 value="<?php echo $searched; ?>" /></td>
305 </tr>
306 <tr><td align="right" valign="top">
307 <?php echo $GLOBALS['strSearchType']; ?></td>
308 <td><input type="radio" id="search_option_1" name="search_option"
309 value="1"<?php if ($search_option == 1) echo ' checked="checked"'; ?> />
310 <label for="search_option_1">
311 <?php echo $GLOBALS['strSearchOption1']; ?></label><sup>1</sup><br />
312 <input type="radio" id="search_option_2" name="search_option"
313 value="2"<?php if ($search_option == 2) echo ' checked="checked"'; ?> />
314 <label for="search_option_2">
315 <?php echo $GLOBALS['strSearchOption2']; ?></label><sup>1</sup><br />
316 <input type="radio" id="search_option_3" name="search_option"
317 value="3"<?php if ($search_option == 3) echo ' checked="checked"'; ?> />
318 <label for="search_option_3">
319 <?php echo $GLOBALS['strSearchOption3']; ?></label><br />
320 <input type="radio" id="search_option_4" name="search_option"
321 value="4"<?php if ($search_option == 4) echo ' checked="checked"'; ?> />
322 <label for="search_option_4">
323 <?php echo $GLOBALS['strSearchOption4']; ?></label>
325 <?php echo PMA_showMySQLDocu('Regexp', 'Regexp'); ?><br />
326 <br />
327 <sup>1</sup><?php echo $GLOBALS['strSplitWordsWithSpace']; ?></td>
328 </tr>
329 <tr><td align="right" valign="top">
330 <?php echo $GLOBALS['strSearchInTables']; ?></td>
331 <td rowspan="2">
332 <?php
333 echo ' <select name="table_select[]" size="6" multiple="multiple">' . "\n";
334 foreach ($tables_names_only as $each_table) {
335 if (in_array($each_table, $tables_selected)) {
336 $is_selected = ' selected="selected"';
337 } else {
338 $is_selected = '';
341 echo ' <option value="' . htmlspecialchars($each_table) . '"'
342 . $is_selected . '>'
343 . str_replace(' ', '&nbsp;', htmlspecialchars($each_table)) . '</option>' . "\n";
344 } // end while
346 echo ' </select>' . "\n";
347 $alter_select =
348 '<a href="db_search.php' . PMA_generate_common_url(array_merge($url_params, array('selectall' => 1))) . '#db_search"'
349 . ' onclick="setSelectOptions(\'db_search\', \'table_select[]\', true); return false;">' . $GLOBALS['strSelectAll'] . '</a>'
350 . '&nbsp;/&nbsp;'
351 . '<a href="db_search.php' . PMA_generate_common_url(array_merge($url_params, array('unselectall' => 1))) . '#db_search"'
352 . ' onclick="setSelectOptions(\'db_search\', \'table_select[]\', false); return false;">' . $GLOBALS['strUnselectAll'] . '</a>';
354 </td>
355 </tr>
356 <tr><td align="right" valign="bottom">
357 <?php echo $alter_select; ?></td></tr>
358 </tr>
359 </table>
360 </fieldset>
361 <fieldset class="tblFooters">
362 <input type="submit" name="submit_search" value="<?php echo $GLOBALS['strGo']; ?>"
363 id="buttonGo" />
364 </fieldset>
365 </form>
367 <?php
369 * Displays the footer
371 require_once './libraries/footer.inc.php';