This has been fixed in PHP 5.1.0, we support 5.2+.
[phpmyadmin/crack.git] / tbl_replace.php
blobea66322fdeb51641143e29a515fc2943b757cb50
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']
40 /**
41 * do not import request variable into global scope
43 * cannot be used as long as it could happen that the $goto file that is included
44 * at the end of this script is not updated to work without imported request variables
46 * @todo uncomment this if all possible included files to rely on import request variables
47 if (! defined('PMA_NO_VARIABLES_IMPORT')) {
48 define('PMA_NO_VARIABLES_IMPORT', true);
51 /**
52 * Gets some core libraries
54 require_once './libraries/common.inc.php';
56 // Check parameters
57 PMA_checkParameters(array('db', 'table', 'goto'));
59 PMA_DBI_select_db($GLOBALS['db']);
61 /**
62 * Initializes some variables
64 $goto_include = false;
66 if (isset($_REQUEST['insert_rows']) && is_numeric($_REQUEST['insert_rows']) && $_REQUEST['insert_rows'] != $cfg['InsertRows']) {
67 $cfg['InsertRows'] = $_REQUEST['insert_rows'];
68 $GLOBALS['js_include'][] = 'tbl_change.js';
69 require_once './libraries/header.inc.php';
70 require './tbl_change.php';
71 exit;
74 if (isset($_REQUEST['after_insert'])
75 && in_array($_REQUEST['after_insert'], array('new_insert', 'same_insert', 'edit_next'))) {
76 $url_params['after_insert'] = $_REQUEST['after_insert'];
77 //$GLOBALS['goto'] = 'tbl_change.php';
78 $goto_include = 'tbl_change.php';
80 if (isset($_REQUEST['primary_key'])) {
81 if ($_REQUEST['after_insert'] == 'same_insert') {
82 foreach ($_REQUEST['primary_key'] as $pk) {
83 $url_params['primary_key'][] = $pk;
85 } elseif ($_REQUEST['after_insert'] == 'edit_next') {
86 foreach ($_REQUEST['primary_key'] as $pk) {
87 $local_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table'])
88 . ' WHERE ' . str_replace('` =', '` >', $pk)
89 . ' LIMIT 1;';
90 $res = PMA_DBI_query($local_query);
91 $row = PMA_DBI_fetch_row($res);
92 $meta = PMA_DBI_get_fields_meta($res);
93 // must find a unique condition based on unique key,
94 // not a combination of all fields
95 if ($tmp = PMA_getUniqueCondition($res, count($meta), $meta, $row, true)) {
96 $_SESSION['edit_next'] = $tmp;
98 unset($tmp);
102 } elseif (! empty($GLOBALS['goto'])) {
103 if (! preg_match('@^[a-z_]+\.php$@', $GLOBALS['goto'])) {
104 // this should NOT happen
105 //$GLOBALS['goto'] = false;
106 $goto_include = false;
107 } else {
108 $goto_include = $GLOBALS['goto'];
110 if ($GLOBALS['goto'] == 'db_sql.php' && strlen($GLOBALS['table'])) {
111 $GLOBALS['table'] = '';
115 if (! $goto_include) {
116 if (! strlen($GLOBALS['table'])) {
117 $goto_include = 'db_sql.php';
118 } else {
119 $goto_include = 'tbl_sql.php';
123 // Defines the url to return in case of failure of the query
124 if (isset($_REQUEST['err_url'])) {
125 $err_url = $_REQUEST['err_url'];
126 } else {
127 $err_url = 'tbl_change.php' . PMA_generate_common_url($url_params);
131 * Prepares the update/insert of a row
133 if (isset($_REQUEST['primary_key'])) {
134 // we were editing something => use primary key
135 $loop_array = (is_array($_REQUEST['primary_key']) ? $_REQUEST['primary_key'] : array($_REQUEST['primary_key']));
136 $using_key = true;
137 $is_insert = ($_REQUEST['submit_type'] == $GLOBALS['strInsertAsNewRow']);
138 } else {
139 // new row => use indexes
140 $loop_array = array();
141 foreach ($_REQUEST['fields']['multi_edit'] as $key => $dummy) {
142 $loop_array[] = $key;
144 $using_key = false;
145 $is_insert = true;
148 $query = array();
149 $value_sets = array();
150 $func_no_param = array(
151 'NOW',
152 'CURDATE',
153 'CURTIME',
154 'UTC_DATE',
155 'UTC_TIME',
156 'UTC_TIMESTAMP',
157 'UNIX_TIMESTAMP',
158 'RAND',
159 'USER',
160 'LAST_INSERT_ID',
163 foreach ($loop_array as $rowcount => $primary_key) {
164 // skip fields to be ignored
165 if (! $using_key && isset($_REQUEST['insert_ignore_' . $primary_key])) {
166 continue;
169 // Defines the SET part of the sql query
170 $query_values = array();
172 // Map multi-edit keys to single-level arrays, dependent on how we got the fields
173 $me_fields =
174 isset($_REQUEST['fields']['multi_edit'][$rowcount])
175 ? $_REQUEST['fields']['multi_edit'][$rowcount]
176 : array();
177 $me_fields_prev =
178 isset($_REQUEST['fields_prev']['multi_edit'][$rowcount])
179 ? $_REQUEST['fields_prev']['multi_edit'][$rowcount]
180 : null;
181 $me_funcs =
182 isset($_REQUEST['funcs']['multi_edit'][$rowcount])
183 ? $_REQUEST['funcs']['multi_edit'][$rowcount]
184 : null;
185 $me_fields_type =
186 isset($_REQUEST['fields_type']['multi_edit'][$rowcount])
187 ? $_REQUEST['fields_type']['multi_edit'][$rowcount]
188 : null;
189 $me_fields_null =
190 isset($_REQUEST['fields_null']['multi_edit'][$rowcount])
191 ? $_REQUEST['fields_null']['multi_edit'][$rowcount]
192 : null;
193 $me_fields_null_prev =
194 isset($_REQUEST['fields_null_prev']['multi_edit'][$rowcount])
195 ? $_REQUEST['fields_null_prev']['multi_edit'][$rowcount]
196 : null;
197 $me_auto_increment =
198 isset($_REQUEST['auto_increment']['multi_edit'][$rowcount])
199 ? $_REQUEST['auto_increment']['multi_edit'][$rowcount]
200 : null;
202 foreach ($me_fields as $key => $val) {
204 require './libraries/tbl_replace_fields.inc.php';
206 if (empty($me_funcs[$key])) {
207 $cur_value = $val;
208 } elseif ('UNIX_TIMESTAMP' === $me_funcs[$key] && $val != "''") {
209 $cur_value = $me_funcs[$key] . '(' . $val . ')';
210 } elseif (in_array($me_funcs[$key], $func_no_param)) {
211 $cur_value = $me_funcs[$key] . '()';
212 } else {
213 $cur_value = $me_funcs[$key] . '(' . $val . ')';
216 // i n s e r t
217 if ($is_insert) {
218 // no need to add column into the valuelist
219 if (strlen($cur_value)) {
220 $query_values[] = $cur_value;
221 // first inserted row so prepare the list of fields
222 if (empty($value_sets)) {
223 $query_fields[] = PMA_backquote($key);
227 // u p d a t e
228 } elseif (!empty($me_fields_null_prev[$key])
229 && !isset($me_fields_null[$key])) {
230 // field had the null checkbox before the update
231 // field no longer has the null checkbox
232 $query_values[] = PMA_backquote($key) . ' = ' . $cur_value;
233 } elseif (empty($me_funcs[$key])
234 && isset($me_fields_prev[$key])
235 && ("'" . PMA_sqlAddslashes($me_fields_prev[$key]) . "'" == $val)) {
236 // No change for this column and no MySQL function is used -> next column
237 continue;
238 } elseif (! empty($val)) {
239 // avoid setting a field to NULL when it's already NULL
240 // (field had the null checkbox before the update
241 // field still has the null checkbox)
242 if (!(! empty($me_fields_null_prev[$key])
243 && isset($me_fields_null[$key]))) {
244 $query_values[] = PMA_backquote($key) . ' = ' . $cur_value;
247 } // end foreach ($me_fields as $key => $val)
249 if (count($query_values) > 0) {
250 if ($is_insert) {
251 $value_sets[] = implode(', ', $query_values);
252 } else {
253 // build update query
254 $query[] = 'UPDATE ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table'])
255 . ' SET ' . implode(', ', $query_values) . ' WHERE ' . $primary_key . ' LIMIT 1';
259 } // end foreach ($loop_array as $primary_key)
260 unset($me_fields_prev, $me_funcs, $me_fields_type, $me_fields_null, $me_fields_null_prev,
261 $me_auto_increment, $cur_value, $key, $val, $loop_array, $primary_key, $using_key,
262 $func_no_param);
265 // Builds the sql query
266 if ($is_insert && count($value_sets) > 0) {
267 $query[] = 'INSERT INTO ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table'])
268 . ' (' . implode(', ', $query_fields) . ') VALUES (' . implode('), (', $value_sets) . ')';
270 unset($query_fields, $value_sets);
272 $message = PMA_Message::success('strRowsInserted');
273 } elseif (! empty($query)) {
274 $message = PMA_Message::success('strRowsAffected');
275 } else {
276 // No change -> move back to the calling script
277 $message = PMA_Message::success('strNoModification');
278 $GLOBALS['js_include'][] = 'functions.js';
279 $active_page = $goto_include;
280 require_once './libraries/header.inc.php';
281 require './' . PMA_securePath($goto_include);
282 exit;
284 unset($me_fields, $is_insert);
287 * Executes the sql query and get the result, then move back to the calling
288 * page
290 if (! empty($GLOBALS['sql_query'])) {
291 $url_params['sql_query'] = $GLOBALS['sql_query'];
292 $return_to_sql_query = $GLOBALS['sql_query'];
294 $GLOBALS['sql_query'] = implode('; ', $query) . ';';
295 $total_affected_rows = 0;
296 $last_messages = array();
297 $warning_messages = array();
298 $error_messages = array();
300 foreach ($query as $single_query) {
301 if ($GLOBALS['cfg']['IgnoreMultiSubmitErrors']) {
302 $result = PMA_DBI_try_query($single_query);
303 } else {
304 $result = PMA_DBI_query($single_query);
307 if (! $result) {
308 $error_messages[] = PMA_DBI_getError();
309 } else {
310 if (@PMA_DBI_affected_rows()) {
311 $total_affected_rows += @PMA_DBI_affected_rows();
314 $insert_id = PMA_DBI_insert_id();
315 if ($insert_id != 0) {
316 // insert_id is id of FIRST record inserted in one insert, so if we
317 // inserted multiple rows, we had to increment this
319 if ($total_affected_rows > 0) {
320 $insert_id = $insert_id + $total_affected_rows - 1;
322 $last_message = PMA_Message::notice('strInsertedRowId');
323 $last_message->addParam($insert_id);
324 $last_messages[] = $last_message;
326 PMA_DBI_free_result($result);
327 } // end if
329 foreach (PMA_DBI_get_warnings() as $warning) {
330 $warning_messages[] = $warning['Level'] . ': #' . $warning['Code']
331 . ' ' . $warning['Message'];
334 unset($result);
336 unset($single_query, $query);
338 $message->addParam($total_affected_rows);
339 $message->addMessages($last_messages, '<br />');
341 if (! empty($warning_messages)) {
343 * @todo use a <div class="warning"> in PMA_showMessage() for this part of
344 * the message
346 $message->addMessages($warning_messages, '<br />');
347 $message->isWarning(true);
349 if (! empty($error_messages)) {
350 $message->addMessages($error_messages);
351 $message->isError(true);
353 unset($error_messages, $warning_messages, $total_affected_rows, $last_messages, $last_message);
355 if (isset($return_to_sql_query)) {
356 $disp_query = $GLOBALS['sql_query'];
357 $disp_message = $message;
358 unset($message);
359 $GLOBALS['sql_query'] = $return_to_sql_query;
362 $GLOBALS['js_include'][] = 'tbl_change.js';
363 $GLOBALS['js_include'][] = 'functions.js';
364 // in case we call sql.php which needs those:
365 $GLOBALS['js_include'][] = 'mootools.js';
366 $GLOBALS['js_include'][] = 'mootools-domready.js';
368 $active_page = $goto_include;
369 require_once './libraries/header.inc.php';
370 require './' . PMA_securePath($goto_include);
371 exit;