bug [calendar] js error "window.parent.addEvent is not a function"
[phpmyadmin/crack.git] / tbl_replace.php
blobdad5ce0287ca8f164281badc8a5aa4e3e5b949bd
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()
41 /**
42 * do not import request variable into global scope
44 * cannot be used as long as it could happen that the $goto file that is included
45 * at the end of this script is not updated to work without imported request variables
47 * @todo uncomment this if all possible included files to rely on import request variables
48 if (! defined('PMA_NO_VARIABLES_IMPORT')) {
49 define('PMA_NO_VARIABLES_IMPORT', true);
52 /**
53 * Gets some core libraries
55 require_once './libraries/common.inc.php';
57 // Check parameters
58 PMA_checkParameters(array('db', 'table', 'goto'));
60 PMA_DBI_select_db($GLOBALS['db']);
62 /**
63 * Initializes some variables
65 $goto_include = false;
67 if (isset($_REQUEST['insert_rows']) && is_numeric($_REQUEST['insert_rows']) && $_REQUEST['insert_rows'] != $cfg['InsertRows']) {
68 $cfg['InsertRows'] = $_REQUEST['insert_rows'];
69 $GLOBALS['js_include'][] = 'tbl_change.js';
70 require_once './libraries/header.inc.php';
71 require './tbl_change.php';
72 exit;
75 if (isset($_REQUEST['after_insert'])
76 && in_array($_REQUEST['after_insert'], array('new_insert', 'same_insert', 'edit_next'))) {
77 $url_params['after_insert'] = $_REQUEST['after_insert'];
78 //$GLOBALS['goto'] = 'tbl_change.php';
79 $goto_include = 'tbl_change.php';
81 if (isset($_REQUEST['primary_key'])) {
82 if ($_REQUEST['after_insert'] == 'same_insert') {
83 foreach ($_REQUEST['primary_key'] as $pk) {
84 $url_params['primary_key'][] = $pk;
86 } elseif ($_REQUEST['after_insert'] == 'edit_next') {
87 foreach ($_REQUEST['primary_key'] as $pk) {
88 $local_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table'])
89 . ' WHERE ' . str_replace('` =', '` >', $pk)
90 . ' LIMIT 1;';
91 $res = PMA_DBI_query($local_query);
92 $row = PMA_DBI_fetch_row($res);
93 $meta = PMA_DBI_get_fields_meta($res);
94 // must find a unique condition based on unique key,
95 // not a combination of all fields
96 if ($tmp = PMA_getUniqueCondition($res, count($meta), $meta, $row, true)) {
97 $_SESSION['edit_next'] = $tmp;
99 unset($tmp);
103 } elseif (! empty($GLOBALS['goto'])) {
104 if (! preg_match('@^[a-z_]+\.php$@', $GLOBALS['goto'])) {
105 // this should NOT happen
106 //$GLOBALS['goto'] = false;
107 $goto_include = false;
108 } else {
109 $goto_include = $GLOBALS['goto'];
111 if ($GLOBALS['goto'] == 'db_sql.php' && strlen($GLOBALS['table'])) {
112 $GLOBALS['table'] = '';
116 if (! $goto_include) {
117 if (! strlen($GLOBALS['table'])) {
118 $goto_include = 'db_sql.php';
119 } else {
120 $goto_include = 'tbl_sql.php';
124 // Defines the url to return in case of failure of the query
125 if (isset($_REQUEST['err_url'])) {
126 $err_url = $_REQUEST['err_url'];
127 } else {
128 $err_url = 'tbl_change.php' . PMA_generate_common_url($url_params);
132 * Prepares the update/insert of a row
134 if (isset($_REQUEST['primary_key'])) {
135 // we were editing something => use primary key
136 $loop_array = (is_array($_REQUEST['primary_key']) ? $_REQUEST['primary_key'] : array($_REQUEST['primary_key']));
137 $using_key = true;
138 $is_insert = ($_REQUEST['submit_type'] == $GLOBALS['strInsertAsNewRow']);
139 } else {
140 // new row => use indexes
141 $loop_array = array();
142 foreach ($_REQUEST['fields']['multi_edit'] as $key => $dummy) {
143 $loop_array[] = $key;
145 $using_key = false;
146 $is_insert = true;
149 $query = array();
150 $value_sets = array();
151 $func_no_param = array(
152 'NOW',
153 'CURDATE',
154 'CURTIME',
155 'UTC_DATE',
156 'UTC_TIME',
157 'UTC_TIMESTAMP',
158 'UNIX_TIMESTAMP',
159 'RAND',
160 'USER',
161 'LAST_INSERT_ID',
162 'UUID',
163 'CURRENT_USER',
166 foreach ($loop_array as $rowcount => $primary_key) {
167 // skip fields to be ignored
168 if (! $using_key && isset($_REQUEST['insert_ignore_' . $primary_key])) {
169 continue;
172 // Defines the SET part of the sql query
173 $query_values = array();
175 // Map multi-edit keys to single-level arrays, dependent on how we got the fields
176 $me_fields =
177 isset($_REQUEST['fields']['multi_edit'][$rowcount])
178 ? $_REQUEST['fields']['multi_edit'][$rowcount]
179 : array();
180 $me_fields_prev =
181 isset($_REQUEST['fields_prev']['multi_edit'][$rowcount])
182 ? $_REQUEST['fields_prev']['multi_edit'][$rowcount]
183 : null;
184 $me_funcs =
185 isset($_REQUEST['funcs']['multi_edit'][$rowcount])
186 ? $_REQUEST['funcs']['multi_edit'][$rowcount]
187 : null;
188 $me_fields_type =
189 isset($_REQUEST['fields_type']['multi_edit'][$rowcount])
190 ? $_REQUEST['fields_type']['multi_edit'][$rowcount]
191 : null;
192 $me_fields_null =
193 isset($_REQUEST['fields_null']['multi_edit'][$rowcount])
194 ? $_REQUEST['fields_null']['multi_edit'][$rowcount]
195 : null;
196 $me_fields_null_prev =
197 isset($_REQUEST['fields_null_prev']['multi_edit'][$rowcount])
198 ? $_REQUEST['fields_null_prev']['multi_edit'][$rowcount]
199 : null;
200 $me_auto_increment =
201 isset($_REQUEST['auto_increment']['multi_edit'][$rowcount])
202 ? $_REQUEST['auto_increment']['multi_edit'][$rowcount]
203 : null;
205 $primary_field = PMA_BS_GetPrimaryField($GLOBALS['db'], $GLOBALS['table']);
207 // Fetch the current values of a row to use in case we have a protected field
208 // @todo possibly move to ./libraries/tbl_replace_fields.inc.php
209 if ($is_insert && $using_key && isset($me_fields_type) &&
210 is_array($me_fields_type) && isset($primary_key)) {
211 $prot_result = PMA_DBI_query('SELECT * FROM ' .
212 PMA_backquote($table) . ' WHERE ' . $primary_key . ';');
213 $prot_row = PMA_DBI_fetch_assoc($prot_result);
214 PMA_DBI_free_result($prot_result);
215 unset($prot_result);
218 foreach ($me_fields as $key => $val) {
220 require './libraries/tbl_replace_fields.inc.php';
222 // rajk - for blobstreaming
223 if (NULL != $primary_field || strlen($primary_field) > 0)
225 $remove_blob_repo = isset($_REQUEST['remove_blob_repo_' . $key]) ? $_REQUEST['remove_blob_repo_' . $key] : NULL;
226 $upload_blob_repo = isset($_REQUEST['upload_blob_repo_' . $key]) ? $_REQUEST['upload_blob_repo_' . $key] : NULL;
228 // checks if an existing blob repository reference should be removed
229 if (isset($remove_blob_repo) && !isset($upload_blob_repo))
231 $remove_blob_reference = $_REQUEST['remove_blob_ref_' . $key];
233 if (isset($remove_blob_reference))
234 $val = "''";
237 // checks if this field requires a bs reference attached to it
238 $requires_bs_reference = isset($upload_blob_repo);
240 if ($requires_bs_reference)
242 // get the most recent BLOB reference
243 $bs_reference = PMA_File::getRecentBLOBReference();
245 // if the most recent BLOB reference exists, set it as a field value
246 if (!is_null($bs_reference))
247 $val = "'" . PMA_sqlAddslashes($bs_reference) . "'";
251 if (empty($me_funcs[$key])) {
252 $cur_value = $val;
253 } elseif ('UNIX_TIMESTAMP' === $me_funcs[$key] && $val != "''") {
254 $cur_value = $me_funcs[$key] . '(' . $val . ')';
255 } elseif (in_array($me_funcs[$key], $func_no_param)) {
256 $cur_value = $me_funcs[$key] . '()';
257 } else {
258 $cur_value = $me_funcs[$key] . '(' . $val . ')';
261 // i n s e r t
262 if ($is_insert) {
263 // no need to add column into the valuelist
264 if (strlen($cur_value)) {
265 $query_values[] = $cur_value;
266 // first inserted row so prepare the list of fields
267 if (empty($value_sets)) {
268 $query_fields[] = PMA_backquote($key);
272 // u p d a t e
273 } elseif (!empty($me_fields_null_prev[$key])
274 && !isset($me_fields_null[$key])) {
275 // field had the null checkbox before the update
276 // field no longer has the null checkbox
277 $query_values[] = PMA_backquote($key) . ' = ' . $cur_value;
278 } elseif (empty($me_funcs[$key])
279 && isset($me_fields_prev[$key])
280 && ("'" . PMA_sqlAddslashes($me_fields_prev[$key]) . "'" == $val)) {
281 // No change for this column and no MySQL function is used -> next column
282 continue;
283 } elseif (! empty($val)) {
284 // avoid setting a field to NULL when it's already NULL
285 // (field had the null checkbox before the update
286 // field still has the null checkbox)
287 if (!(! empty($me_fields_null_prev[$key])
288 && isset($me_fields_null[$key]))) {
289 $query_values[] = PMA_backquote($key) . ' = ' . $cur_value;
292 } // end foreach ($me_fields as $key => $val)
294 if (count($query_values) > 0) {
295 if ($is_insert) {
296 $value_sets[] = implode(', ', $query_values);
297 } else {
298 // build update query
299 $query[] = 'UPDATE ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table'])
300 . ' SET ' . implode(', ', $query_values) . ' WHERE ' . str_replace('&#93;', ']', $primary_key) . ' LIMIT 1';
304 } // end foreach ($loop_array as $primary_key)
305 unset($me_fields_prev, $me_funcs, $me_fields_type, $me_fields_null, $me_fields_null_prev,
306 $me_auto_increment, $cur_value, $key, $val, $loop_array, $primary_key, $using_key,
307 $func_no_param);
310 // Builds the sql query
311 if ($is_insert && count($value_sets) > 0) {
312 $query[] = 'INSERT INTO ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table'])
313 . ' (' . implode(', ', $query_fields) . ') VALUES (' . implode('), (', $value_sets) . ')';
315 unset($query_fields, $value_sets);
317 $message = PMA_Message::success('strRowsInserted');
318 } elseif (! empty($query)) {
319 $message = PMA_Message::success('strRowsAffected');
320 } else {
321 // No change -> move back to the calling script
322 $message = PMA_Message::success('strNoModification');
323 $GLOBALS['js_include'][] = 'functions.js';
324 $active_page = $goto_include;
325 require_once './libraries/header.inc.php';
326 require './' . PMA_securePath($goto_include);
327 exit;
329 unset($me_fields, $is_insert);
332 * Executes the sql query and get the result, then move back to the calling
333 * page
335 if (! empty($GLOBALS['sql_query'])) {
336 $url_params['sql_query'] = $GLOBALS['sql_query'];
337 $return_to_sql_query = $GLOBALS['sql_query'];
339 $GLOBALS['sql_query'] = implode('; ', $query) . ';';
340 $total_affected_rows = 0;
341 $last_messages = array();
342 $warning_messages = array();
343 $error_messages = array();
345 foreach ($query as $single_query) {
346 if ($GLOBALS['cfg']['IgnoreMultiSubmitErrors']) {
347 $result = PMA_DBI_try_query($single_query);
348 } else {
349 $result = PMA_DBI_query($single_query);
352 if (! $result) {
353 $error_messages[] = PMA_DBI_getError();
354 } else {
355 if (@PMA_DBI_affected_rows()) {
356 $total_affected_rows += @PMA_DBI_affected_rows();
359 $insert_id = PMA_DBI_insert_id();
360 if ($insert_id != 0) {
361 // insert_id is id of FIRST record inserted in one insert, so if we
362 // inserted multiple rows, we had to increment this
364 if ($total_affected_rows > 0) {
365 $insert_id = $insert_id + $total_affected_rows - 1;
367 $last_message = PMA_Message::notice('strInsertedRowId');
368 $last_message->addParam($insert_id);
369 $last_messages[] = $last_message;
371 PMA_DBI_free_result($result);
372 } // end if
374 foreach (PMA_DBI_get_warnings() as $warning) {
375 $warning_messages[] = $warning['Level'] . ': #' . $warning['Code']
376 . ' ' . $warning['Message'];
379 unset($result);
381 unset($single_query, $query);
383 $message->addParam($total_affected_rows);
384 $message->addMessages($last_messages, '<br />');
386 if (! empty($warning_messages)) {
388 * @todo use a <div class="warning"> in PMA_showMessage() for this part of
389 * the message
391 $message->addMessages($warning_messages, '<br />');
392 $message->isWarning(true);
394 if (! empty($error_messages)) {
395 $message->addMessages($error_messages);
396 $message->isError(true);
398 unset($error_messages, $warning_messages, $total_affected_rows, $last_messages, $last_message);
400 if (isset($return_to_sql_query)) {
401 $disp_query = $GLOBALS['sql_query'];
402 $disp_message = $message;
403 unset($message);
404 $GLOBALS['sql_query'] = $return_to_sql_query;
407 $GLOBALS['js_include'][] = 'tbl_change.js';
408 $GLOBALS['js_include'][] = 'functions.js';
409 // in case we call sql.php which needs those:
410 $GLOBALS['js_include'][] = 'mootools.js';
412 $active_page = $goto_include;
413 require_once './libraries/header.inc.php';
414 require './' . PMA_securePath($goto_include);
415 exit;