bug #3024344 [setup] Setup forces numeric MemoryLimit
[phpmyadmin/crack.git] / tbl_replace.php
bloba7da0bc56c2ca645169f39dc22e36297773b1f3f
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';
57 $blob_streaming_active = $_SESSION['PMA_Config']->get('BLOBSTREAMING_PLUGINS_EXIST');
59 // Check parameters
60 PMA_checkParameters(array('db', 'table', 'goto'));
62 PMA_DBI_select_db($GLOBALS['db']);
64 /**
65 * Initializes some variables
67 $goto_include = false;
69 if (isset($_REQUEST['insert_rows']) && is_numeric($_REQUEST['insert_rows']) && $_REQUEST['insert_rows'] != $cfg['InsertRows']) {
70 $cfg['InsertRows'] = $_REQUEST['insert_rows'];
71 $GLOBALS['js_include'][] = 'tbl_change.js';
72 require_once './libraries/header.inc.php';
73 require './tbl_change.php';
74 exit;
77 if (isset($_REQUEST['after_insert'])
78 && in_array($_REQUEST['after_insert'], array('new_insert', 'same_insert', 'edit_next'))) {
79 $url_params['after_insert'] = $_REQUEST['after_insert'];
80 //$GLOBALS['goto'] = 'tbl_change.php';
81 $goto_include = 'tbl_change.php';
83 if (isset($_REQUEST['where_clause'])) {
84 if ($_REQUEST['after_insert'] == 'same_insert') {
85 foreach ($_REQUEST['where_clause'] as $one_where_clause) {
86 $url_params['where_clause'][] = $one_where_clause;
88 } elseif ($_REQUEST['after_insert'] == 'edit_next') {
89 foreach ($_REQUEST['where_clause'] as $one_where_clause) {
90 $local_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table'])
91 . ' WHERE ' . str_replace('` =', '` >', $one_where_clause)
92 . ' LIMIT 1;';
93 $res = PMA_DBI_query($local_query);
94 $row = PMA_DBI_fetch_row($res);
95 $meta = PMA_DBI_get_fields_meta($res);
96 // must find a unique condition based on unique key,
97 // not a combination of all fields
98 list($unique_condition, $clause_is_unique) = PMA_getUniqueCondition($res, count($meta), $meta, $row, true);
99 if (! empty($unique_condition)) {
100 $_SESSION['edit_next'] = $unique_condition;
102 unset($unique_condition, $clause_is_unique);
106 } elseif (! empty($GLOBALS['goto'])) {
107 if (! preg_match('@^[a-z_]+\.php$@', $GLOBALS['goto'])) {
108 // this should NOT happen
109 //$GLOBALS['goto'] = false;
110 $goto_include = false;
111 } else {
112 $goto_include = $GLOBALS['goto'];
114 if ($GLOBALS['goto'] == 'db_sql.php' && strlen($GLOBALS['table'])) {
115 $GLOBALS['table'] = '';
119 if (! $goto_include) {
120 if (! strlen($GLOBALS['table'])) {
121 $goto_include = 'db_sql.php';
122 } else {
123 $goto_include = 'tbl_sql.php';
127 // Defines the url to return in case of failure of the query
128 if (isset($_REQUEST['err_url'])) {
129 $err_url = $_REQUEST['err_url'];
130 } else {
131 $err_url = 'tbl_change.php' . PMA_generate_common_url($url_params);
135 * Prepares the update/insert of a row
137 if (isset($_REQUEST['where_clause'])) {
138 // we were editing something => use the WHERE clause
139 $loop_array = (is_array($_REQUEST['where_clause']) ? $_REQUEST['where_clause'] : array($_REQUEST['where_clause']));
140 $using_key = true;
141 $is_insert = ($_REQUEST['submit_type'] == $GLOBALS['strInsertAsNewRow']);
142 } else {
143 // new row => use indexes
144 $loop_array = array();
145 foreach ($_REQUEST['fields']['multi_edit'] as $key => $dummy) {
146 $loop_array[] = $key;
148 $using_key = false;
149 $is_insert = true;
152 $query = array();
153 $value_sets = array();
154 $func_no_param = array(
155 'NOW',
156 'CURDATE',
157 'CURTIME',
158 'UTC_DATE',
159 'UTC_TIME',
160 'UTC_TIMESTAMP',
161 'UNIX_TIMESTAMP',
162 'RAND',
163 'USER',
164 'LAST_INSERT_ID',
165 'UUID',
166 'CURRENT_USER',
169 foreach ($loop_array as $rowcount => $where_clause) {
170 // skip fields to be ignored
171 if (! $using_key && isset($_REQUEST['insert_ignore_' . $where_clause])) {
172 continue;
175 // Defines the SET part of the sql query
176 $query_values = array();
178 // Map multi-edit keys to single-level arrays, dependent on how we got the fields
179 $me_fields =
180 isset($_REQUEST['fields']['multi_edit'][$rowcount])
181 ? $_REQUEST['fields']['multi_edit'][$rowcount]
182 : array();
183 $me_fields_name =
184 isset($_REQUEST['fields_name']['multi_edit'][$rowcount])
185 ? $_REQUEST['fields_name']['multi_edit'][$rowcount]
186 : null;
187 $me_fields_prev =
188 isset($_REQUEST['fields_prev']['multi_edit'][$rowcount])
189 ? $_REQUEST['fields_prev']['multi_edit'][$rowcount]
190 : null;
191 $me_funcs =
192 isset($_REQUEST['funcs']['multi_edit'][$rowcount])
193 ? $_REQUEST['funcs']['multi_edit'][$rowcount]
194 : null;
195 $me_fields_type =
196 isset($_REQUEST['fields_type']['multi_edit'][$rowcount])
197 ? $_REQUEST['fields_type']['multi_edit'][$rowcount]
198 : null;
199 $me_fields_null =
200 isset($_REQUEST['fields_null']['multi_edit'][$rowcount])
201 ? $_REQUEST['fields_null']['multi_edit'][$rowcount]
202 : null;
203 $me_fields_null_prev =
204 isset($_REQUEST['fields_null_prev']['multi_edit'][$rowcount])
205 ? $_REQUEST['fields_null_prev']['multi_edit'][$rowcount]
206 : null;
207 $me_auto_increment =
208 isset($_REQUEST['auto_increment']['multi_edit'][$rowcount])
209 ? $_REQUEST['auto_increment']['multi_edit'][$rowcount]
210 : null;
212 if ($blob_streaming_active) {
213 $primary_field = PMA_BS_GetPrimaryField($GLOBALS['db'], $GLOBALS['table']);
216 // Fetch the current values of a row to use in case we have a protected field
217 // @todo possibly move to ./libraries/tbl_replace_fields.inc.php
218 if ($is_insert && $using_key && isset($me_fields_type) && is_array($me_fields_type) && isset($where_clause)) {
219 $prot_row = PMA_DBI_fetch_single_row('SELECT * FROM ' . PMA_backquote($table) . ' WHERE ' . $where_clause . ';');
221 foreach ($me_fields as $key => $val) {
223 // Note: $key is an md5 of the fieldname. The actual fieldname is available in $me_fields_name[$key]
225 require './libraries/tbl_replace_fields.inc.php';
227 // rajk - for blobstreaming
228 if ($blob_streaming_active && (NULL != $primary_field || strlen($primary_field) > 0)) {
229 $remove_blob_repo = isset($_REQUEST['remove_blob_repo_' . $key]) ? $_REQUEST['remove_blob_repo_' . $key] : NULL;
230 $upload_blob_repo = isset($_REQUEST['upload_blob_repo_' . $key]) ? $_REQUEST['upload_blob_repo_' . $key] : NULL;
232 // checks if an existing blob repository reference should be removed
233 if (isset($remove_blob_repo) && !isset($upload_blob_repo)) {
234 $remove_blob_reference = $_REQUEST['remove_blob_ref_' . $key];
235 if (isset($remove_blob_reference)) {
236 $val = "''";
239 // checks if this field requires a bs reference attached to it
240 if (isset($upload_blob_repo)) {
241 // get the most recent BLOB reference
242 $bs_reference = PMA_File::getRecentBLOBReference();
244 // if the most recent BLOB reference exists, set it as a field value
245 if (!is_null($bs_reference)) {
246 $val = "'" . PMA_sqlAddslashes($bs_reference) . "'";
252 if (empty($me_funcs[$key])) {
253 $cur_value = $val;
254 } elseif ('UNIX_TIMESTAMP' === $me_funcs[$key] && $val != "''") {
255 $cur_value = $me_funcs[$key] . '(' . $val . ')';
256 } elseif (in_array($me_funcs[$key], $func_no_param)) {
257 $cur_value = $me_funcs[$key] . '()';
258 } else {
259 $cur_value = $me_funcs[$key] . '(' . $val . ')';
262 // i n s e r t
263 if ($is_insert) {
264 // no need to add column into the valuelist
265 if (strlen($cur_value)) {
266 $query_values[] = $cur_value;
267 // first inserted row so prepare the list of fields
268 if (empty($value_sets)) {
269 $query_fields[] = PMA_backquote($me_fields_name[$key]);
273 // u p d a t e
274 } elseif (!empty($me_fields_null_prev[$key])
275 && !isset($me_fields_null[$key])) {
276 // field had the null checkbox before the update
277 // field no longer has the null checkbox
278 $query_values[] = PMA_backquote($me_fields_name[$key]) . ' = ' . $cur_value;
279 } elseif (empty($me_funcs[$key])
280 && isset($me_fields_prev[$key])
281 && ("'" . PMA_sqlAddslashes($me_fields_prev[$key]) . "'" == $val)) {
282 // No change for this column and no MySQL function is used -> next column
283 continue;
284 } elseif (! empty($val)) {
285 // avoid setting a field to NULL when it's already NULL
286 // (field had the null checkbox before the update
287 // field still has the null checkbox)
288 if (!(! empty($me_fields_null_prev[$key])
289 && isset($me_fields_null[$key]))) {
290 $query_values[] = PMA_backquote($me_fields_name[$key]) . ' = ' . $cur_value;
293 } // end foreach ($me_fields as $key => $val)
295 if (count($query_values) > 0) {
296 if ($is_insert) {
297 $value_sets[] = implode(', ', $query_values);
298 } else {
299 // build update query
300 $query[] = 'UPDATE ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table'])
301 . ' SET ' . implode(', ', $query_values) . ' WHERE ' . $where_clause . ($_REQUEST['clause_is_unique'] ? '' : ' LIMIT 1');
305 } // end foreach ($loop_array as $where_clause)
306 unset($me_fields_name, $me_fields_prev, $me_funcs, $me_fields_type, $me_fields_null, $me_fields_null_prev,
307 $me_auto_increment, $cur_value, $key, $val, $loop_array, $where_clause, $using_key,
308 $func_no_param);
311 // Builds the sql query
312 if ($is_insert && count($value_sets) > 0) {
313 $query[] = 'INSERT INTO ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table'])
314 . ' (' . implode(', ', $query_fields) . ') VALUES (' . implode('), (', $value_sets) . ')';
316 unset($query_fields, $value_sets);
318 $message = PMA_Message::success('strRowsInserted');
319 } elseif (! empty($query)) {
320 $message = PMA_Message::success('strRowsAffected');
321 } else {
322 // No change -> move back to the calling script
323 $message = PMA_Message::success('strNoModification');
324 $GLOBALS['js_include'][] = 'functions.js';
325 $active_page = $goto_include;
326 require_once './libraries/header.inc.php';
327 require './' . PMA_securePath($goto_include);
328 exit;
330 unset($me_fields, $is_insert);
333 * Executes the sql query and get the result, then move back to the calling
334 * page
336 if (! empty($GLOBALS['sql_query'])) {
337 $url_params['sql_query'] = $GLOBALS['sql_query'];
338 $return_to_sql_query = $GLOBALS['sql_query'];
340 $GLOBALS['sql_query'] = implode('; ', $query) . ';';
341 // to ensure that the query is displayed in case of
342 // "insert as new row" and then "insert another new row"
343 $GLOBALS['display_query'] = $GLOBALS['sql_query'];
345 $total_affected_rows = 0;
346 $last_messages = array();
347 $warning_messages = array();
348 $error_messages = array();
350 foreach ($query as $single_query) {
351 if ($GLOBALS['cfg']['IgnoreMultiSubmitErrors']) {
352 $result = PMA_DBI_try_query($single_query);
353 } else {
354 $result = PMA_DBI_query($single_query);
357 if (! $result) {
358 $error_messages[] = PMA_DBI_getError();
359 } else {
360 // the following is a real assignment:
361 if ($tmp = @PMA_DBI_affected_rows()) {
362 $total_affected_rows += $tmp;
364 unset($tmp);
366 $insert_id = PMA_DBI_insert_id();
367 if ($insert_id != 0) {
368 // insert_id is id of FIRST record inserted in one insert, so if we
369 // inserted multiple rows, we had to increment this
371 if ($total_affected_rows > 0) {
372 $insert_id = $insert_id + $total_affected_rows - 1;
374 $last_message = PMA_Message::notice('strInsertedRowId');
375 $last_message->addParam($insert_id);
376 $last_messages[] = $last_message;
378 PMA_DBI_free_result($result);
379 } // end if
381 foreach (PMA_DBI_get_warnings() as $warning) {
382 $warning_messages[] = $warning['Level'] . ': #' . $warning['Code']
383 . ' ' . $warning['Message'];
386 unset($result);
388 unset($single_query, $query);
390 $message->addParam($total_affected_rows);
391 $message->addMessages($last_messages, '<br />');
393 if (! empty($warning_messages)) {
395 * @todo use a <div class="warning"> in PMA_showMessage() for this part of
396 * the message
398 $message->addMessages($warning_messages, '<br />');
399 $message->isWarning(true);
401 if (! empty($error_messages)) {
402 $message->addMessages($error_messages);
403 $message->isError(true);
405 unset($error_messages, $warning_messages, $total_affected_rows, $last_messages, $last_message);
407 if (isset($return_to_sql_query)) {
408 $disp_query = $GLOBALS['sql_query'];
409 $disp_message = $message;
410 unset($message);
411 $GLOBALS['sql_query'] = $return_to_sql_query;
414 $GLOBALS['js_include'][] = 'tbl_change.js';
415 $GLOBALS['js_include'][] = 'functions.js';
416 // in case we call sql.php which needs those:
417 $GLOBALS['js_include'][] = 'mootools.js';
419 $active_page = $goto_include;
422 * If user asked for "and then Insert another new row" we have to remove
423 * WHERE clause information so that tbl_change.php does not go back
424 * to the current record
426 if (isset($_REQUEST['after_insert']) && 'new_insert' == $_REQUEST['after_insert']) {
427 unset($_REQUEST['where_clause']);
431 * Load header.
433 require_once './libraries/header.inc.php';
435 * Load target page.
437 require './' . PMA_securePath($goto_include);
438 exit;