Welcome to mirror list, hosted at ThFree Co, Russian Federation.

tbl_replace.php - github.com/phpmyadmin/phpmyadmin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 402ceadef6c0af28bf73d8db3e539add3ba20e8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
/* $Id$ */
// vim: expandtab sw=4 ts=4 sts=4:


/**
 * Gets some core libraries
 */
require_once('./libraries/grab_globals.lib.php');
require_once('./libraries/common.lib.php');

// Check parameters
PMA_checkParameters(array('db','table','goto'));

/**
 * Initializes some variables
 */
// Defines the url to return in case of success of the query
if (isset($sql_query)) {
    $sql_query = urldecode($sql_query);
}
if (!isset($dontlimitchars)) {
    $dontlimitchars = 0;
}
$is_gotofile = FALSE;
if (isset($after_insert) && $after_insert == 'new_insert') {
    $goto = 'tbl_change.php?'
          . PMA_generate_common_url($db, $table, '&')
          . '&goto=' . urlencode($goto)
          . '&pos=' . $pos
          . '&session_max_rows=' . $session_max_rows
          . '&disp_direction=' . $disp_direction
          . '&repeat_cells=' . $repeat_cells
          . '&dontlimitchars=' . $dontlimitchars
          . (empty($sql_query) ? '' : '&sql_query=' . urlencode($sql_query));
} elseif (isset($after_insert) && $after_insert == 'same_insert') {
    $goto = 'tbl_change.php?'
          . PMA_generate_common_url($db, $table, '&')
          . '&goto=' . urlencode($goto)
          . '&pos=' . $pos
          . '&session_max_rows=' . $session_max_rows
          . '&disp_direction=' . $disp_direction
          . '&repeat_cells=' . $repeat_cells
          . '&dontlimitchars=' . $dontlimitchars
          . (empty($sql_query) ? '' : '&sql_query=' . urlencode($sql_query));
    if (isset($primary_key)) {
        foreach ($primary_key AS $pk) {
            $goto .= '&primary_key[]=' . $pk;
        }
    }
} else if ($goto == 'sql.php') {
    $goto = 'sql.php?'
          . PMA_generate_common_url($db, $table, '&')
          . '&pos=' . $pos
          . '&session_max_rows=' . $session_max_rows
          . '&disp_direction=' . $disp_direction
          . '&repeat_cells=' . $repeat_cells
          . '&dontlimitchars=' . $dontlimitchars
          . '&sql_query=' . urlencode($sql_query);
} else if (!empty($goto)) {
    // Security checkings
    $is_gotofile     = preg_replace('@^([^?]+).*$@', '\\1', $goto);
    if (!@file_exists('./' . $is_gotofile)) {
        $goto        = (empty($table)) ? 'db_details.php' : 'tbl_properties.php';
        $is_gotofile = TRUE;
    } else {
        $is_gotofile = ($is_gotofile == $goto);
    }
}

// Defines the url to return in case of failure of the query
if (isset($err_url)) {
    $err_url = urldecode($err_url);
} else {
    $err_url = str_replace('&', '&amp;', $goto)
             . (empty($primary_key) ? '' : '&amp;primary_key=' . (is_array($primary_key) ? $primary_key[0] : $primary_key));
}

// Resets tables defined in the configuration file
if (isset($funcs)) {
    reset($funcs);
}

// Misc
$seen_binary = FALSE;

/**
 * Prepares the update/insert of a row
 */
if (isset($primary_key)) {
    // we were editing something => use primary key
    $loop_array = (is_array($primary_key) ? $primary_key : array(0 => $primary_key));
    $using_key  = TRUE;
    $is_insert  = ($submit_type == $strInsertAsNewRow);
} else {
    // new row => use indexes
    $loop_array = array();
    for ($i = 0; $i < $cfg['InsertRows']; $i++) $loop_array[$i] = $i;
    $using_key  = FALSE;
    $is_insert  = TRUE;
}

PMA_DBI_select_db($db);
$query = array();
$message = '';

foreach ($loop_array AS $primary_key_index => $enc_primary_key) {
    // skip fields to be ignored
    if (!$using_key && isset($GLOBALS['insert_ignore_' . $enc_primary_key])) continue;
    
    // Restore the "primary key" to a convenient format
    $primary_key = urldecode($enc_primary_key);

    // Defines the SET part of the sql query
    $valuelist = '';
    $fieldlist = '';

    // Map multi-edit keys to single-level arrays, dependent on how we got the fields
    $me_fields      = isset($fields['multi_edit'])      && isset($fields['multi_edit'][$enc_primary_key])      ? $fields['multi_edit'][$enc_primary_key]      : null;
    $me_fields_prev = isset($fields_prev['multi_edit']) && isset($fields_prev['multi_edit'][$enc_primary_key]) ? $fields_prev['multi_edit'][$enc_primary_key] : null;
    $me_funcs       = isset($funcs['multi_edit'])       && isset($funcs['multi_edit'][$enc_primary_key])       ? $funcs['multi_edit'][$enc_primary_key]       : null;
    $me_fields_type = isset($fields_type['multi_edit']) && isset($fields_type['multi_edit'][$enc_primary_key]) ? $fields_type['multi_edit'][$enc_primary_key] : null;
    $me_fields_null = isset($fields_null['multi_edit']) && isset($fields_null['multi_edit'][$enc_primary_key]) ? $fields_null['multi_edit'][$enc_primary_key] : null;
  	   
    if ($using_key && isset($me_fields_type) && is_array($me_fields_type) && isset($primary_key)) { 	 
        $prot_result      = PMA_DBI_query('SELECT * FROM ' . PMA_backquote($table) . ' WHERE ' . $primary_key . ';'); 	 
        $prot_row         = PMA_DBI_fetch_assoc($prot_result); 	 
        PMA_DBI_free_result($prot_result);
        unset($prot_result);
    }

    foreach ($me_fields AS $encoded_key => $val) {
        $key         = urldecode($encoded_key);
        $fieldlist   .= PMA_backquote($key) . ', ';

        require('./tbl_replace_fields.php');

        if (empty($me_funcs[$encoded_key])) {
            $cur_value = $val . ', ';
        } else if (preg_match('@^(NOW|CURDATE|CURTIME|UNIX_TIMESTAMP|RAND|USER|LAST_INSERT_ID)$@', $me_funcs[$encoded_key])) {
            $cur_value = $me_funcs[$encoded_key] . '(), ';
        } else {
            $cur_value = $me_funcs[$encoded_key] . '(' . $val . '), ';
        }
        
        if ($is_insert) { 
            // insert, no need to add column
            $valuelist .= $cur_value;
        } else if (empty($me_funcs[$encoded_key])
            && isset($me_fields_prev) && isset($me_fields_prev[$encoded_key])
            && ("'" . PMA_sqlAddslashes(urldecode($me_fields_prev[$encoded_key])) . "'" == $val)) {
            // No change for this column and no MySQL function is used -> next column
            continue;
        }
        else if (!empty($val)) {
            $valuelist .= PMA_backquote($key) . ' = ' . $cur_value;
        }
    } // end while

    // get rid of last ,
    $valuelist    = preg_replace('@, $@', '', $valuelist);
    
    // Builds the sql query
    if ($is_insert) {
        if (empty($query)) {
            // first inserted row -> prepare template
            $fieldlist = preg_replace('@, $@', '', $fieldlist);
            $query = array('INSERT INTO ' . PMA_backquote($table) . ' (' . $fieldlist . ') VALUES ');
        }
        // append current values
        $query[0]  .= '(' . $valuelist . '), ';
        $message   = $strInsertedRows . '&nbsp;';
    } elseif (!empty($valuelist)) {
        // build update query
        $query[]   = 'UPDATE ' . PMA_backquote($table) . ' SET ' . $valuelist . ' WHERE' . $primary_key . ' LIMIT 1';

        $message  = $strAffectedRows . '&nbsp;';
    }
} // end for

// trim last , from insert query
if ($is_insert) {
    $query[0] = preg_replace('@, $@', '', $query[0]);
}

if (empty($valuelist) && empty($query)) {
    // No change -> move back to the calling script
    $message = $strNoModification;
    if ($is_gotofile) {
        $js_to_run = 'functions.js';
        require_once('./header.inc.php');
        require('./' . PMA_securePath($goto));
    } else {
        PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . $goto . '&disp_message=' . urlencode($message) . '&disp_query=');

    }
    exit();
}

/**
 * Executes the sql query and get the result, then move back to the calling
 * page
 */
$sql_query = implode(';', $query) . ';';
$total_affected_rows = 0;
$last_message = '';

foreach ($query AS $query_index => $single_query) {
    if ($cfg['IgnoreMultiSubmitErrors']) {
        $result = PMA_DBI_try_query($single_query);
    } else {
        $result = PMA_DBI_query($single_query);
    }
    if (!$result) {
        $message .= PMA_DBI_getError();
    } else {
        if (@PMA_DBI_affected_rows()) {
            $total_affected_rows += @PMA_DBI_affected_rows();
        }

        $insert_id = PMA_DBI_insert_id();
        if ($insert_id != 0) {
            $last_message .= '<br />'.$strInsertedRowId . '&nbsp;' . $insert_id;
        }
    } // end if
    PMA_DBI_free_result($result);
    unset($result);
}

if ($total_affected_rows != 0) {
    //$message .= '<br />' . $total_affected_rows;
    $message .= $total_affected_rows;
} else {
    $message .= $strModifications;
}

$message .= $last_message;

if ($is_gotofile) {
    if ($goto == 'db_details.php' && !empty($table)) {
        unset($table);
    }
    $js_to_run = 'functions.js';
    $active_page = $goto;
    require_once('./header.inc.php');
    require('./' . PMA_securePath($goto));
} else {
    // I don't understand this one:
    //$add_query = (strpos(' ' . $goto, 'tbl_change') ? '&disp_query=' . urlencode($sql_query) : '');

    // if we have seen binary,
    // we do not append the query to the Location so it won't be displayed
    // on the resulting page
    // Nijel: we also need to limit size of url...
    $add_query = (!$seen_binary && strlen($sql_query) < 1024 ? '&disp_query=' . urlencode($sql_query) : '');
    PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . $goto . '&disp_message=' . urlencode($message) . $add_query);
}
exit();
?>