PageRenderTime 55ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/phpMyAdmin_old/db_search.php

https://bitbucket.org/steve_delbar/iepsm-projet-de-d-veloppement-internet-2013
PHP | 366 lines | 253 code | 52 blank | 61 comment | 58 complexity | 5f996f8e3b8f9a3baacaae5381b6a583 MD5 | raw file
  1. <?php
  2. /* $Id: db_search.php,v 2.11 2004/10/12 21:08:49 rabus Exp $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4. /**
  5. * Credits for this script goes to Thomas Chaumeny <chaume92 at aol.com>
  6. */
  7. /**
  8. * Gets some core libraries and send headers
  9. */
  10. require('./db_details_common.php');
  11. // If config variable $cfg['Usedbsearch'] is on FALSE : exit.
  12. if (!$cfg['UseDbSearch']) {
  13. PMA_mysqlDie($strAccessDenied, '', FALSE, $err_url);
  14. } // end if
  15. $url_query .= '&amp;goto=db_search.php';
  16. /**
  17. * Get the list of tables from the current database
  18. */
  19. $tables = PMA_DBI_get_tables($db);
  20. $num_tables = count($tables);
  21. /**
  22. * Displays top links
  23. */
  24. $sub_part = '';
  25. require('./db_details_links.php');
  26. /**
  27. * 1. Main search form has been submitted
  28. */
  29. if (isset($submit_search)) {
  30. /**
  31. * Builds the SQL search query
  32. *
  33. * @param string the table name
  34. * @param string the string to search
  35. * @param integer type of search (1 -> 1 word at least, 2 -> all words,
  36. * 3 -> exact string, 4 -> regexp)
  37. *
  38. * @return array 3 SQL querys (for count, display and delete results)
  39. *
  40. * @global string the url to retun to in case of errors
  41. */
  42. function PMA_getSearchSqls($table, $search_str, $search_option)
  43. {
  44. global $err_url, $charset_connection;
  45. // Statement types
  46. $sqlstr_select = 'SELECT';
  47. $sqlstr_delete = 'DELETE';
  48. // Fields to select
  49. $res = PMA_DBI_query('SHOW ' . (PMA_MYSQL_INT_VERSION >= 40100 ? 'FULL ' : '') . 'FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($GLOBALS['db']) . ';');
  50. while ($current = PMA_DBI_fetch_assoc($res)) {
  51. if (PMA_MYSQL_INT_VERSION >= 40100) {
  52. list($current['Charset']) = explode('_', $current['Collation']);
  53. }
  54. $current['Field'] = PMA_backquote($current['Field']);
  55. $tblfields[] = $current;
  56. } // while
  57. PMA_DBI_free_result($res);
  58. unset($current, $res);
  59. $tblfields_cnt = count($tblfields);
  60. // Table to use
  61. $sqlstr_from = ' FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($table);
  62. // Beginning of WHERE clause
  63. $sqlstr_where = ' WHERE';
  64. $search_words = (($search_option > 2) ? array($search_str) : explode(' ', $search_str));
  65. $search_wds_cnt = count($search_words);
  66. $like_or_regex = (($search_option == 4) ? 'REGEXP' : 'LIKE');
  67. $automatic_wildcard = (($search_option <3) ? '%' : '');
  68. for ($i = 0; $i < $search_wds_cnt; $i++) {
  69. // Eliminates empty values
  70. if (!empty($search_words[$i])) {
  71. for ($j = 0; $j < $tblfields_cnt; $j++) {
  72. $prefix = PMA_MYSQL_INT_VERSION >= 40100 && $tblfields[$j]['Charset'] != $charset_connection && $tblfields[$j]['Charset'] != 'NULL'
  73. ? 'CONVERT(_utf8 '
  74. : '';
  75. $suffix = PMA_MYSQL_INT_VERSION >= 40100 && $tblfields[$j]['Charset'] != $charset_connection && $tblfields[$j]['Charset'] != 'NULL'
  76. ? ' USING ' . $tblfields[$j]['Charset'] . ') COLLATE ' . $tblfields[$j]['Collation']
  77. : '';
  78. $thefieldlikevalue[] = $tblfields[$j]['Field']
  79. . ' ' . $like_or_regex . ' '
  80. . $prefix
  81. . '\''
  82. . $automatic_wildcard
  83. . $search_words[$i]
  84. . $automatic_wildcard . '\''
  85. . $suffix;
  86. } // end for
  87. $fieldslikevalues[] = ($search_wds_cnt > 1)
  88. ? '(' . implode(' OR ', $thefieldlikevalue) . ')'
  89. : implode(' OR ', $thefieldlikevalue);
  90. unset($thefieldlikevalue);
  91. } // end if
  92. } // end for
  93. $implode_str = ($search_option == 1 ? ' OR ' : ' AND ');
  94. $sqlstr_where .= ' ' . implode($implode_str, $fieldslikevalues);
  95. unset($fieldslikevalues);
  96. // Builds complete queries
  97. $sql['select_fields'] = $sqlstr_select . ' * ' . $sqlstr_from . $sqlstr_where;
  98. $sql['select_count'] = $sqlstr_select . ' COUNT(*) AS count' . $sqlstr_from . $sqlstr_where;
  99. $sql['delete'] = $sqlstr_delete . $sqlstr_from . $sqlstr_where;
  100. return $sql;
  101. } // end of the "PMA_getSearchSqls()" function
  102. /**
  103. * Displays the results
  104. */
  105. if (!empty($search_str) && !empty($search_option)) {
  106. $original_search_str = $search_str;
  107. $search_str = PMA_sqlAddslashes($search_str, TRUE);
  108. // Get the true string to display as option's comment
  109. switch ($search_option) {
  110. case 1:
  111. $option_str = ' (' . $strSearchOption1 . ')';
  112. break;
  113. case 2:
  114. $option_str = ' (' . $strSearchOption2 . ')';
  115. break;
  116. case 3:
  117. $option_str = ' (' . $strSearchOption3 . ')';
  118. break;
  119. case 4:
  120. $option_str = ' (' . $strSearchOption4 . ')';
  121. break;
  122. } // end switch
  123. // If $table is defined or if there is only one table in $table_select
  124. // set $onetable to the table's name (display is different if there is
  125. // only one table).
  126. //
  127. // Recall:
  128. // $tables is an array with all tables in database $db
  129. // $num_tables is the size of $tables
  130. if (isset($table)) {
  131. $onetable = $table;
  132. }
  133. else if (isset($table_select)) {
  134. $num_selectedtables = count($table_select);
  135. if ($num_selectedtables == 1) {
  136. $onetable = $table_select[0];
  137. }
  138. }
  139. else if ($num_tables == 1) {
  140. $onetable = $tables[0];
  141. }
  142. else {
  143. for ($i = 0; $i < $num_tables; $i++) {
  144. $table_select[] = $tables[$i];
  145. }
  146. $num_selectedtables = $num_tables;
  147. } // end if... else if... else
  148. ?>
  149. <br />
  150. <?php
  151. $url_sql_query = PMA_generate_common_url($db)
  152. . '&amp;goto=db_details.php'
  153. . '&amp;pos=0'
  154. . '&amp;is_js_confirmed=0';
  155. // Only one table defined in an variable $onetable
  156. if (isset($onetable)) {
  157. // Displays search string
  158. echo ' ' . sprintf($strSearchResultsFor, htmlspecialchars($original_search_str), $option_str) . "\n";
  159. echo ' <br />' . "\n";
  160. // Gets the SQL statements
  161. $newsearchsqls = PMA_getSearchSqls($onetable, $search_str, $search_option);
  162. // Executes the "COUNT" statement
  163. $res = PMA_DBI_query($newsearchsqls['select_count']);
  164. $res_cnt = PMA_DBI_fetch_assoc($res);
  165. $res_cnt = $res_cnt['count'];
  166. PMA_DBI_free_result($res);
  167. $num_search_result_total = $res_cnt;
  168. echo ' <!-- Search results in table ' . $onetable . ' (' . $res_cnt . ') -->' . "\n"
  169. . ' <br />' . "\n"
  170. . ' <table><tr><td>' . sprintf($strNumSearchResultsInTable, $res_cnt, htmlspecialchars($onetable)) . "</td>\n";
  171. if ($res_cnt > 0) {
  172. echo '<td>' . PMA_linkOrButton('sql.php?' . $url_sql_query
  173. . '&amp;sql_query=' .urlencode($newsearchsqls['select_fields']),
  174. $strBrowse, '') . "</td>\n";
  175. echo '<td>' . PMA_linkOrButton('sql.php?' . $url_sql_query
  176. . '&amp;sql_query=' .urlencode($newsearchsqls['delete']),
  177. $strDelete, $newsearchsqls['delete']) . "</td>\n";
  178. } // end if
  179. echo '</tr></table>' . "\n";
  180. } // end only one table
  181. // Several tables defined in the array $table_select
  182. else if (isset($table_select)) {
  183. // Displays search string
  184. echo ' ' . sprintf($strSearchResultsFor, htmlspecialchars($original_search_str), $option_str) . "\n";
  185. echo ' <ul>' . "\n";
  186. $num_search_result_total = 0;
  187. for ($i = 0; $i < $num_selectedtables; $i++) {
  188. // Gets the SQL statements
  189. $newsearchsqls = PMA_getSearchSqls($table_select[$i], $search_str, $search_option);
  190. // Executes the "COUNT" statement
  191. $res = PMA_DBI_query($newsearchsqls['select_count']);
  192. $res_cnt = PMA_DBI_fetch_assoc($res);
  193. $res_cnt = $res_cnt['count'];
  194. PMA_DBI_free_result($res);
  195. unset($res);
  196. $num_search_result_total += $res_cnt;
  197. echo ' <!-- Search results in table ' . $table_select[$i] . ' (' . $res_cnt . ') -->' . "\n"
  198. . ' <li>' . "\n"
  199. . ' <table><tr><td>' . sprintf($strNumSearchResultsInTable, $res_cnt, htmlspecialchars($table_select[$i])) . "</td>\n";
  200. if ($res_cnt > 0) {
  201. echo '<td>' . PMA_linkOrButton('sql.php?' . $url_sql_query
  202. . '&amp;sql_query=' .urlencode($newsearchsqls['select_fields']),
  203. $strBrowse, '') . "</td>\n";
  204. echo '<td>' . PMA_linkOrButton('sql.php?' . $url_sql_query
  205. . '&amp;sql_query=' .urlencode($newsearchsqls['delete']),
  206. $strDelete, $newsearchsqls['delete']) . "</td>\n";
  207. } // end if
  208. echo ' </tr></table></li>' . "\n";
  209. } // end for
  210. echo ' </ul>' . "\n";
  211. echo ' <p>' . sprintf($strNumSearchResultsTotal, $num_search_result_total) . '</p>' . "\n";
  212. } // end several tables
  213. echo "\n";
  214. ?>
  215. <hr width="100%">
  216. <?php
  217. } // end if (!empty($search_str) && !empty($search_option))
  218. } // end 1.
  219. /**
  220. * 2. Displays the main search form
  221. */
  222. echo "\n";
  223. $searched = (isset($original_search_str))
  224. ? htmlspecialchars($original_search_str)
  225. : '';
  226. if (empty($search_option)) {
  227. $search_option = 1;
  228. }
  229. ?>
  230. <!-- Display search form -->
  231. <a name="db_search"></a>
  232. <form method="post" action="db_search.php" name="db_search">
  233. <?php echo PMA_generate_common_hidden_inputs($db); ?>
  234. <table border="0" cellpadding="3" cellspacing="0">
  235. <tr>
  236. <th class="tblHeaders" align="center" colspan="2"><?php echo $strSearchFormTitle; ?></th>
  237. </tr>
  238. <tr><td colspan="2"></td></tr>
  239. <tr>
  240. <td bgcolor="<?php echo $cfg['BgcolorOne']; ?>">
  241. <?php echo $strSearchNeedle; ?>&nbsp;<br />
  242. </td>
  243. <td bgcolor="<?php echo $cfg['BgcolorOne']; ?>">
  244. <input type="text" name="search_str" size="60" value="<?php echo $searched; ?>" />
  245. </td>
  246. </tr>
  247. <tr><td colspan="2"></td></tr><tr>
  248. <td align="right" valign="top" bgcolor="<?php echo $cfg['BgcolorOne']; ?>">
  249. <?php echo $strSearchType; ?>&nbsp;
  250. </td>
  251. <td bgcolor="<?php echo $cfg['BgcolorOne']; ?>">
  252. <input type="radio" id="search_option_1" name="search_option" value="1"<?php if ($search_option == 1) echo ' checked="checked"'; ?> /><label for="search_option_1"><?php echo $strSearchOption1; ?></label>&nbsp;*<br />
  253. <input type="radio" id="search_option_2" name="search_option" value="2"<?php if ($search_option == 2) echo ' checked="checked"'; ?> /><label for="search_option_2"><?php echo $strSearchOption2; ?></label>&nbsp;*<br />
  254. <input type="radio" id="search_option_3" name="search_option" value="3"<?php if ($search_option == 3) echo ' checked="checked"'; ?> /><label for="search_option_3"><?php echo $strSearchOption3; ?></label><br />
  255. <input type="radio" id="search_option_4" name="search_option" value="4"<?php if ($search_option == 4) echo ' checked="checked"'; ?> /><label for="search_option_4"><?php echo $strSearchOption4; ?></label><?php echo PMA_showMySQLDocu('Regexp', 'Regexp'); ?><br />
  256. <br />
  257. *&nbsp;<?php echo $strSplitWordsWithSpace . "\n"; ?>
  258. </td>
  259. </tr>
  260. <tr><td colspan="2"></td></tr>
  261. <tr>
  262. <td align="right" valign="top" bgcolor="<?php echo $cfg['BgcolorOne']; ?>">
  263. <?php echo $strSearchInTables; ?>&nbsp;
  264. </td>
  265. <td rowspan="2" bgcolor="<?php echo $cfg['BgcolorOne']; ?>">
  266. <?php
  267. $strDoSelectAll='&nbsp;';
  268. if ($num_tables > 1) {
  269. $i = 0;
  270. echo ' <select name="table_select[]" size="6" multiple="multiple">' . "\n";
  271. while ($i < $num_tables) {
  272. if (!empty($unselectall)) {
  273. $is_selected = '';
  274. }
  275. else if ((isset($table_select) && PMA_isInto($tables[$i], $table_select) != -1)
  276. || (!empty($selectall))
  277. || (isset($onetable) && $onetable == $tables[$i])) {
  278. $is_selected = ' selected="selected"';
  279. }
  280. else {
  281. $is_selected = '';
  282. }
  283. echo ' <option value="' . htmlspecialchars($tables[$i]) . '"' . $is_selected . '>' . htmlspecialchars($tables[$i]) . '</option>' . "\n";
  284. $i++;
  285. } // end while
  286. echo ' </select>' . "\n";
  287. $strDoSelectAll = '<a href="db_search.php?' . $url_query . '&amp;selectall=1#db_search"'
  288. . ' onclick="setSelectOptions(\'db_search\', \'table_select[]\', true); return false;">' . $strSelectAll . '</a>'
  289. . '&nbsp;/&nbsp;'
  290. . '<a href="db_search.php?' . $url_query . '&amp;unselectall=1#db_search"'
  291. . ' onclick="setSelectOptions(\'db_search\', \'table_select[]\', false); return false;">' . $strUnselectAll . '</a>';
  292. }
  293. else {
  294. echo "\n";
  295. echo ' ' . htmlspecialchars($tables[0]) . "\n";
  296. echo ' <input type="hidden" name="table" value="' . htmlspecialchars($tables[0]) . '" />' . "\n";
  297. } // end if... else...
  298. echo"\n";
  299. ?>
  300. </td>
  301. </tr><tr><td align="right" valign="bottom" bgcolor="<?php echo $cfg['BgcolorOne']; ?>"><?php echo $strDoSelectAll; ?></td></tr>
  302. <tr><td colspan="2"></td>
  303. </tr><tr>
  304. <td colspan="2" align="right" class="tblHeaders"><input type="submit" name="submit_search" value="<?php echo $strGo; ?>" id="buttonGo" /></td>
  305. </tr>
  306. </table>
  307. </form>
  308. <?php
  309. /**
  310. * Displays the footer
  311. */
  312. echo "\n";
  313. require_once('./footer.inc.php');
  314. ?>