~ubuntu-branches/ubuntu/trusty/phpmyadmin/trusty

« back to all changes in this revision

Viewing changes to db_search.php

  • Committer: Bazaar Package Importer
  • Author(s): Thijs Kinkhorst
  • Date: 2009-02-16 17:58:28 UTC
  • mfrom: (13.1.4 jaunty)
  • Revision ID: james.westby@ubuntu.com-20090216175828-d2dllrukk9kecv4k
Tags: 4:3.1.2-2
* Upload to unstable.
* [INTL:es] Spanish debconf template update (Closes: #513690).

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
 * @uses    $_REQUEST['table_select']
39
39
 * @uses    $_REQUEST['unselectall']
40
40
 * @uses    $_REQUEST['selectall']
 
41
 * @uses    $_REQUEST['field_str']
41
42
 * @uses    is_string()
42
43
 * @uses    htmlspecialchars()
43
44
 * @uses    array_key_exists()
45
46
 * @uses    array_intersect()
46
47
 * @uses    sprintf()
47
48
 * @uses    in_array()
48
 
 * @version $Id: db_search.php 10958 2007-12-04 18:07:16Z lem9 $
 
49
 * @version $Id: db_search.php 11115 2008-02-10 13:04:35Z lem9 $
49
50
 * @author  Thomas Chaumeny <chaume92 at aol.com>
50
51
 */
51
52
 
99
100
    // For "as regular expression" (search option 4), we should not treat
100
101
    // this as an expression that contains a LIKE (second parameter of
101
102
    // PMA_sqlAddslashes()).
102
 
    // 
 
103
    //
103
104
    // Usage example: If user is seaching for a literal $ in a regexp search,
104
105
    // he should enter \$ as the value.
105
106
    $search_str = PMA_sqlAddslashes($_REQUEST['search_str'], ($search_option == 4 ? false : true));
118
119
    $tables_selected = array();
119
120
}
120
121
 
 
122
if (empty($_REQUEST['field_str']) || ! is_string($_REQUEST['field_str'])) {
 
123
    unset($field_str);
 
124
} else {
 
125
    $field_str = PMA_sqlAddslashes($_REQUEST['field_str'], true);
 
126
}
 
127
 
121
128
/**
122
129
 * Displays top links
123
130
 */
135
142
     *
136
143
     * @todo    can we make use of fulltextsearch IN BOOLEAN MODE for this?
137
144
     * @uses    PMA_DBI_query
138
 
     * PMA_MYSQL_INT_VERSION
139
145
     * PMA_backquote
140
146
     * PMA_DBI_free_result
141
147
     * PMA_DBI_fetch_assoc
144
150
     * count
145
151
     * strlen
146
152
     * @param   string   the table name
 
153
     * @param   string   restrict the search to this field
147
154
     * @param   string   the string to search
148
155
     * @param   integer  type of search (1 -> 1 word at least, 2 -> all words,
149
156
     *                                   3 -> exact string, 4 -> regexp)
153
160
     * @global  string   the url to return to in case of errors
154
161
     * @global  string   charset connection
155
162
     */
156
 
    function PMA_getSearchSqls($table, $search_str, $search_option)
 
163
    function PMA_getSearchSqls($table, $field, $search_str, $search_option)
157
164
    {
158
 
        global $err_url, $charset_connection;
 
165
        global $err_url;
159
166
 
160
167
        // Statement types
161
168
        $sqlstr_select = 'SELECT';
162
169
        $sqlstr_delete = 'DELETE';
163
170
 
164
171
        // 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']);
169
 
            }
170
 
            $current['Field'] = PMA_backquote($current['Field']);
171
 
            $tblfields[]      = $current;
172
 
        } // while
173
 
        PMA_DBI_free_result($res);
174
 
        unset($current, $res);
 
172
        $tblfields = PMA_DBI_fetch_result('SHOW FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($GLOBALS['db']),
 
173
            null, 'Field');
175
174
 
176
175
        // Table to use
177
176
        $sqlstr_from = ' FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($table);
180
179
        $search_wds_cnt  = count($search_words);
181
180
 
182
181
        $like_or_regex   = (($search_option == 4) ? 'REGEXP' : 'LIKE');
183
 
        $automatic_wildcard   = (($search_option <3) ? '%' : '');
 
182
        $automatic_wildcard   = (($search_option < 3) ? '%' : '');
184
183
 
185
184
        $fieldslikevalues = array();
186
185
        foreach ($search_words as $search_word) {
187
186
            // 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
187
            if (strlen($search_word) === 0) {
191
188
                continue;
192
189
            }
193
190
 
194
191
            $thefieldlikevalue = array();
195
192
            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 = '';
 
193
                if (! isset($field) || strlen($field) == 0 || $tblfield == $field) {
 
194
                    $thefieldlikevalue[] = PMA_backquote($tblfield)
 
195
                                         . ' ' . $like_or_regex . ' '
 
196
                                         . "'" . $automatic_wildcard
 
197
                                         . $search_word
 
198
                                         . $automatic_wildcard . "'";
204
199
                }
205
 
                $thefieldlikevalue[] = $tblfield['Field']
206
 
                                     . ' ' . $like_or_regex . ' '
207
 
                                     . $prefix
208
 
                                     . "'"
209
 
                                     . $automatic_wildcard
210
 
                                     . $search_word
211
 
                                     . $automatic_wildcard . "'"
212
 
                                     . $suffix;
213
200
            } // end for
214
201
 
215
 
            $fieldslikevalues[]      = implode(' OR ', $thefieldlikevalue);
 
202
            if (count($thefieldlikevalue) > 0) {
 
203
                $fieldslikevalues[]      = implode(' OR ', $thefieldlikevalue);
 
204
            }
216
205
        } // end for
217
206
 
218
207
        $implode_str  = ($search_option == 1 ? ' OR ' : ' AND ');
219
 
        $sqlstr_where = ' WHERE (' . implode(') ' . $implode_str . ' (', $fieldslikevalues) . ')';
 
208
        if ( empty($fieldslikevalues)) {
 
209
            // this could happen when the "inside field" does not exist
 
210
            // in any selected tables
 
211
            $sqlstr_where = ' WHERE FALSE';
 
212
        } else {
 
213
            $sqlstr_where = ' WHERE (' . implode(') ' . $implode_str . ' (', $fieldslikevalues) . ')';
 
214
        }
220
215
        unset($fieldslikevalues);
221
216
 
222
217
        // Builds complete queries
225
220
        // VIEWs, anyway we have a WHERE clause that should limit results
226
221
        $sql['select_count']  = $sqlstr_select . ' COUNT(*) AS `count`' . $sqlstr_from . $sqlstr_where;
227
222
        $sql['delete']        = $sqlstr_delete . $sqlstr_from . $sqlstr_where;
 
223
 
228
224
        return $sql;
229
225
    } // end of the "PMA_getSearchSqls()" function
230
226
 
252
248
 
253
249
    foreach ($tables_selected as $each_table) {
254
250
        // Gets the SQL statements
255
 
        $newsearchsqls = PMA_getSearchSqls($each_table,
256
 
            $search_str, $search_option);
 
251
        $newsearchsqls = PMA_getSearchSqls($each_table, (! empty($field_str) ? $field_str : ''), $search_str, $search_option);
257
252
 
258
253
        // Executes the "COUNT" statement
259
254
        $res_cnt = PMA_DBI_fetch_value($newsearchsqls['select_count']);
310
305
    </tr>
311
306
    <tr><td align="right" valign="top">
312
307
            <?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>
 
308
            <td><?php
329
309
 
330
 
            <?php echo PMA_showMySQLDocu('Regexp', 'Regexp'); ?><br />
331
 
            <br />
332
 
            <sup>1</sup><?php echo $GLOBALS['strSplitWordsWithSpace']; ?></td>
 
310
$choices = array(
 
311
    '1' => $GLOBALS['strSearchOption1'] . PMA_showHint($GLOBALS['strSplitWordsWithSpace']),
 
312
    '2' => $GLOBALS['strSearchOption2'] . PMA_showHint($GLOBALS['strSplitWordsWithSpace']),
 
313
    '3' => $GLOBALS['strSearchOption3'],
 
314
    '4' => $GLOBALS['strSearchOption4'] . ' ' . PMA_showMySQLDocu('Regexp', 'Regexp')
 
315
); 
 
316
// 4th parameter set to false to add line breaks
 
317
// 5th parameter set to false to avoid htmlspecialchars() escaping in the label
 
318
//  since we have some HTML in some labels
 
319
PMA_generate_html_radio('search_option', $choices, $search_option, true, false);
 
320
unset($choices);
 
321
            ?>
 
322
            </td>
333
323
    </tr>
334
324
    <tr><td align="right" valign="top">
335
325
            <?php echo $GLOBALS['strSearchInTables']; ?></td>
361
351
    <tr><td align="right" valign="bottom">
362
352
            <?php echo $alter_select; ?></td></tr>
363
353
    </tr>
 
354
    <tr><td align="right">
 
355
            <?php echo $GLOBALS['strSearchInField']; ?></td>
 
356
        <td><input type="text" name="field_str" size="60"
 
357
                value="<?php echo ! empty($field_str) ? $field_str : ''; ?>" /></td>
 
358
    </tr>
364
359
    </table>
365
360
</fieldset>
366
361
<fieldset class="tblFooters">