3.1.3.2 release
[phpmyadmin/crack.git] / tbl_replace.php
blob4448d3db7d7a7ab7552fc3cbce4f90819236a9b1
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * manipulation of table data like inserting, replacing and updating
6 * usally called as form action from tbl_change.php to insert or update table rows
8 * @version $Id$
10 * @todo 'edit_next' tends to not work as expected if used ... at least there is no order by
11 * it needs the original query and the row number and than replace the LIMIT clause
12 * @uses PMA_checkParameters()
13 * @uses PMA_DBI_select_db()
14 * @uses PMA_DBI_query()
15 * @uses PMA_DBI_fetch_row()
16 * @uses PMA_DBI_get_fields_meta()
17 * @uses PMA_DBI_free_result()
18 * @uses PMA_DBI_try_query()
19 * @uses PMA_DBI_getError()
20 * @uses PMA_DBI_affected_rows()
21 * @uses PMA_DBI_insert_id()
22 * @uses PMA_backquote()
23 * @uses PMA_getUniqueCondition()
24 * @uses PMA_sqlAddslashes()
25 * @uses PMA_securePath()
26 * @uses PMA_sendHeaderLocation()
27 * @uses str_replace()
28 * @uses count()
29 * @uses file_exists()
30 * @uses strlen()
31 * @uses str_replace()
32 * @uses preg_replace()
33 * @uses is_array()
34 * @uses $GLOBALS['db']
35 * @uses $GLOBALS['table']
36 * @uses $GLOBALS['goto']
37 * @uses $GLOBALS['sql_query']
38 * @uses PMA_File::getRecentBLOBReference()
39 * @package phpMyAdmin
42 /**
43 * do not import request variable into global scope
45 * cannot be used as long as it could happen that the $goto file that is included
46 * at the end of this script is not updated to work without imported request variables
48 * @todo uncomment this if all possible included files to rely on import request variables
49 if (! defined('PMA_NO_VARIABLES_IMPORT')) {
50 define('PMA_NO_VARIABLES_IMPORT', true);
53 /**
54 * Gets some core libraries
56 require_once './libraries/common.inc.php';
58 // Check parameters
59 PMA_checkParameters(array('db', 'table', 'goto'));
61 PMA_DBI_select_db($GLOBALS['db']);
63 /**
64 * Initializes some variables
66 $goto_include = false;
68 if (isset($_REQUEST['insert_rows']) && is_numeric($_REQUEST['insert_rows']) && $_REQUEST['insert_rows'] != $cfg['InsertRows']) {
69 $cfg['InsertRows'] = $_REQUEST['insert_rows'];
70 $GLOBALS['js_include'][] = 'tbl_change.js';
71 require_once './libraries/header.inc.php';
72 require './tbl_change.php';
73 exit;
76 if (isset($_REQUEST['after_insert'])
77 && in_array($_REQUEST['after_insert'], array('new_insert', 'same_insert', 'edit_next'))) {
78 $url_params['after_insert'] = $_REQUEST['after_insert'];
79 //$GLOBALS['goto'] = 'tbl_change.php';
80 $goto_include = 'tbl_change.php';
82 if (isset($_REQUEST['primary_key'])) {
83 if ($_REQUEST['after_insert'] == 'same_insert') {
84 foreach ($_REQUEST['primary_key'] as $pk) {
85 $url_params['primary_key'][] = $pk;
87 } elseif ($_REQUEST['after_insert'] == 'edit_next') {
88 foreach ($_REQUEST['primary_key'] as $pk) {
89 $local_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table'])
90 . ' WHERE ' . str_replace('` =', '` >', $pk)
91 . ' LIMIT 1;';
92 $res = PMA_DBI_query($local_query);
93 $row = PMA_DBI_fetch_row($res);
94 $meta = PMA_DBI_get_fields_meta($res);
95 // must find a unique condition based on unique key,
96 // not a combination of all fields
97 if ($tmp = PMA_getUniqueCondition($res, count($meta), $meta, $row, true)) {
98 $_SESSION['edit_next'] = $tmp;
100 unset($tmp);
104 } elseif (! empty($GLOBALS['goto'])) {
105 if (! preg_match('@^[a-z_]+\.php$@', $GLOBALS['goto'])) {
106 // this should NOT happen
107 //$GLOBALS['goto'] = false;
108 $goto_include = false;
109 } else {
110 $goto_include = $GLOBALS['goto'];
112 if ($GLOBALS['goto'] == 'db_sql.php' && strlen($GLOBALS['table'])) {
113 $GLOBALS['table'] = '';
117 if (! $goto_include) {
118 if (! strlen($GLOBALS['table'])) {
119 $goto_include = 'db_sql.php';
120 } else {
121 $goto_include = 'tbl_sql.php';
125 // Defines the url to return in case of failure of the query
126 if (isset($_REQUEST['err_url'])) {
127 $err_url = $_REQUEST['err_url'];
128 } else {
129 $err_url = 'tbl_change.php' . PMA_generate_common_url($url_params);
133 * Prepares the update/insert of a row
135 if (isset($_REQUEST['primary_key'])) {
136 // we were editing something => use primary key
137 $loop_array = (is_array($_REQUEST['primary_key']) ? $_REQUEST['primary_key'] : array($_REQUEST['primary_key']));
138 $using_key = true;
139 $is_insert = ($_REQUEST['submit_type'] == $GLOBALS['strInsertAsNewRow']);
140 } else {
141 // new row => use indexes
142 $loop_array = array();
143 foreach ($_REQUEST['fields']['multi_edit'] as $key => $dummy) {
144 $loop_array[] = $key;
146 $using_key = false;
147 $is_insert = true;
150 $query = array();
151 $value_sets = array();
152 $func_no_param = array(
153 'NOW',
154 'CURDATE',
155 'CURTIME',
156 'UTC_DATE',
157 'UTC_TIME',
158 'UTC_TIMESTAMP',
159 'UNIX_TIMESTAMP',
160 'RAND',
161 'USER',
162 'LAST_INSERT_ID',
163 'UUID',
164 'CURRENT_USER',
167 foreach ($loop_array as $rowcount => $primary_key) {
168 // skip fields to be ignored
169 if (! $using_key && isset($_REQUEST['insert_ignore_' . $primary_key])) {
170 continue;
173 // Defines the SET part of the sql query
174 $query_values = array();
176 // Map multi-edit keys to single-level arrays, dependent on how we got the fields
177 $me_fields =
178 isset($_REQUEST['fields']['multi_edit'][$rowcount])
179 ? $_REQUEST['fields']['multi_edit'][$rowcount]
180 : array();
181 $me_fields_name =
182 isset($_REQUEST['fields_name']['multi_edit'][$rowcount])
183 ? $_REQUEST['fields_name']['multi_edit'][$rowcount]
184 : null;
185 $me_fields_prev =
186 isset($_REQUEST['fields_prev']['multi_edit'][$rowcount])
187 ? $_REQUEST['fields_prev']['multi_edit'][$rowcount]
188 : null;
189 $me_funcs =
190 isset($_REQUEST['funcs']['multi_edit'][$rowcount])
191 ? $_REQUEST['funcs']['multi_edit'][$rowcount]
192 : null;
193 $me_fields_type =
194 isset($_REQUEST['fields_type']['multi_edit'][$rowcount])
195 ? $_REQUEST['fields_type']['multi_edit'][$rowcount]
196 : null;
197 $me_fields_null =
198 isset($_REQUEST['fields_null']['multi_edit'][$rowcount])
199 ? $_REQUEST['fields_null']['multi_edit'][$rowcount]
200 : null;
201 $me_fields_null_prev =
202 isset($_REQUEST['fields_null_prev']['multi_edit'][$rowcount])
203 ? $_REQUEST['fields_null_prev']['multi_edit'][$rowcount]
204 : null;
205 $me_auto_increment =
206 isset($_REQUEST['auto_increment']['multi_edit'][$rowcount])
207 ? $_REQUEST['auto_increment']['multi_edit'][$rowcount]
208 : null;
210 $primary_field = PMA_BS_GetPrimaryField($GLOBALS['db'], $GLOBALS['table']);
212 // Fetch the current values of a row to use in case we have a protected field
213 // @todo possibly move to ./libraries/tbl_replace_fields.inc.php
214 if ($is_insert && $using_key && isset($me_fields_type) &&
215 is_array($me_fields_type) && isset($primary_key)) {
216 $prot_result = PMA_DBI_query('SELECT * FROM ' .
217 PMA_backquote($table) . ' WHERE ' . $primary_key . ';');
218 $prot_row = PMA_DBI_fetch_assoc($prot_result);
219 PMA_DBI_free_result($prot_result);
220 unset($prot_result);
222 foreach ($me_fields as $key => $val) {
224 // Note: $key is an md5 of the fieldname. The actual fieldname is available in $me_fields_name[$key]
226 require './libraries/tbl_replace_fields.inc.php';
228 // rajk - for blobstreaming
229 if (NULL != $primary_field || strlen($primary_field) > 0)
231 $remove_blob_repo = isset($_REQUEST['remove_blob_repo_' . $key]) ? $_REQUEST['remove_blob_repo_' . $key] : NULL;
232 $upload_blob_repo = isset($_REQUEST['upload_blob_repo_' . $key]) ? $_REQUEST['upload_blob_repo_' . $key] : NULL;
234 // checks if an existing blob repository reference should be removed
235 if (isset($remove_blob_repo) && !isset($upload_blob_repo))
237 $remove_blob_reference = $_REQUEST['remove_blob_ref_' . $key];
239 if (isset($remove_blob_reference))
240 $val = "''";
243 // checks if this field requires a bs reference attached to it
244 $requires_bs_reference = isset($upload_blob_repo);
246 if ($requires_bs_reference)
248 // get the most recent BLOB reference
249 $bs_reference = PMA_File::getRecentBLOBReference();
251 // if the most recent BLOB reference exists, set it as a field value
252 if (!is_null($bs_reference))
253 $val = "'" . PMA_sqlAddslashes($bs_reference) . "'";
257 if (empty($me_funcs[$key])) {
258 $cur_value = $val;
259 } elseif ('UNIX_TIMESTAMP' === $me_funcs[$key] && $val != "''") {
260 $cur_value = $me_funcs[$key] . '(' . $val . ')';
261 } elseif (in_array($me_funcs[$key], $func_no_param)) {
262 $cur_value = $me_funcs[$key] . '()';
263 } else {
264 $cur_value = $me_funcs[$key] . '(' . $val . ')';
267 // i n s e r t
268 if ($is_insert) {
269 // no need to add column into the valuelist
270 if (strlen($cur_value)) {
271 $query_values[] = $cur_value;
272 // first inserted row so prepare the list of fields
273 if (empty($value_sets)) {
274 $query_fields[] = PMA_backquote($me_fields_name[$key]);
278 // u p d a t e
279 } elseif (!empty($me_fields_null_prev[$key])
280 && !isset($me_fields_null[$key])) {
281 // field had the null checkbox before the update
282 // field no longer has the null checkbox
283 $query_values[] = PMA_backquote($me_fields_name[$key]) . ' = ' . $cur_value;
284 } elseif (empty($me_funcs[$key])
285 && isset($me_fields_prev[$key])
286 && ("'" . PMA_sqlAddslashes($me_fields_prev[$key]) . "'" == $val)) {
287 // No change for this column and no MySQL function is used -> next column
288 continue;
289 } elseif (! empty($val)) {
290 // avoid setting a field to NULL when it's already NULL
291 // (field had the null checkbox before the update
292 // field still has the null checkbox)
293 if (!(! empty($me_fields_null_prev[$key])
294 && isset($me_fields_null[$key]))) {
295 $query_values[] = PMA_backquote($me_fields_name[$key]) . ' = ' . $cur_value;
298 } // end foreach ($me_fields as $key => $val)
300 if (count($query_values) > 0) {
301 if ($is_insert) {
302 $value_sets[] = implode(', ', $query_values);
303 } else {
304 // build update query
305 $query[] = 'UPDATE ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table'])
306 . ' SET ' . implode(', ', $query_values) . ' WHERE ' . str_replace('&#93;', ']', $primary_key) . ' LIMIT 1';
310 } // end foreach ($loop_array as $primary_key)
311 unset($me_fields_name, $me_fields_prev, $me_funcs, $me_fields_type, $me_fields_null, $me_fields_null_prev,
312 $me_auto_increment, $cur_value, $key, $val, $loop_array, $primary_key, $using_key,
313 $func_no_param);
316 // Builds the sql query
317 if ($is_insert && count($value_sets) > 0) {
318 $query[] = 'INSERT INTO ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table'])
319 . ' (' . implode(', ', $query_fields) . ') VALUES (' . implode('), (', $value_sets) . ')';
321 unset($query_fields, $value_sets);
323 $message = PMA_Message::success('strRowsInserted');
324 } elseif (! empty($query)) {
325 $message = PMA_Message::success('strRowsAffected');
326 } else {
327 // No change -> move back to the calling script
328 $message = PMA_Message::success('strNoModification');
329 $GLOBALS['js_include'][] = 'functions.js';
330 $active_page = $goto_include;
331 require_once './libraries/header.inc.php';
332 require './' . PMA_securePath($goto_include);
333 exit;
335 unset($me_fields, $is_insert);
338 * Executes the sql query and get the result, then move back to the calling
339 * page
341 if (! empty($GLOBALS['sql_query'])) {
342 $url_params['sql_query'] = $GLOBALS['sql_query'];
343 $return_to_sql_query = $GLOBALS['sql_query'];
345 $GLOBALS['sql_query'] = implode('; ', $query) . ';';
346 $total_affected_rows = 0;
347 $last_messages = array();
348 $warning_messages = array();
349 $error_messages = array();
351 foreach ($query as $single_query) {
352 if ($GLOBALS['cfg']['IgnoreMultiSubmitErrors']) {
353 $result = PMA_DBI_try_query($single_query);
354 } else {
355 $result = PMA_DBI_query($single_query);
358 if (! $result) {
359 $error_messages[] = PMA_DBI_getError();
360 } else {
361 if (@PMA_DBI_affected_rows()) {
362 $total_affected_rows += @PMA_DBI_affected_rows();
365 $insert_id = PMA_DBI_insert_id();
366 if ($insert_id != 0) {
367 // insert_id is id of FIRST record inserted in one insert, so if we
368 // inserted multiple rows, we had to increment this
370 if ($total_affected_rows > 0) {
371 $insert_id = $insert_id + $total_affected_rows - 1;
373 $last_message = PMA_Message::notice('strInsertedRowId');
374 $last_message->addParam($insert_id);
375 $last_messages[] = $last_message;
377 PMA_DBI_free_result($result);
378 } // end if
380 foreach (PMA_DBI_get_warnings() as $warning) {
381 $warning_messages[] = $warning['Level'] . ': #' . $warning['Code']
382 . ' ' . $warning['Message'];
385 unset($result);
387 unset($single_query, $query);
389 $message->addParam($total_affected_rows);
390 $message->addMessages($last_messages, '<br />');
392 if (! empty($warning_messages)) {
394 * @todo use a <div class="warning"> in PMA_showMessage() for this part of
395 * the message
397 $message->addMessages($warning_messages, '<br />');
398 $message->isWarning(true);
400 if (! empty($error_messages)) {
401 $message->addMessages($error_messages);
402 $message->isError(true);
404 unset($error_messages, $warning_messages, $total_affected_rows, $last_messages, $last_message);
406 if (isset($return_to_sql_query)) {
407 $disp_query = $GLOBALS['sql_query'];
408 $disp_message = $message;
409 unset($message);
410 $GLOBALS['sql_query'] = $return_to_sql_query;
413 $GLOBALS['js_include'][] = 'tbl_change.js';
414 $GLOBALS['js_include'][] = 'functions.js';
415 // in case we call sql.php which needs those:
416 $GLOBALS['js_include'][] = 'mootools.js';
418 $active_page = $goto_include;
420 * Load header.
422 require_once './libraries/header.inc.php';
424 * Load target page.
426 require './' . PMA_securePath($goto_include);
427 exit;