Documentation TYPO3 par Ameos |
00001 <?php 00002 /* $Id: tbl_replace.php,v 2.27 2004/12/28 16:34:44 nijel Exp $ */ 00003 // vim: expandtab sw=4 ts=4 sts=4: 00004 00005 00009 require_once('./libraries/grab_globals.lib.php'); 00010 require_once('./libraries/common.lib.php'); 00011 00012 // Check parameters 00013 PMA_checkParameters(array('db','table','goto')); 00014 00015 PMA_DBI_select_db($db); 00016 00020 // Defines the url to return in case of success of the query 00021 if (isset($sql_query)) { 00022 $sql_query = urldecode($sql_query); 00023 } 00024 if (!isset($dontlimitchars)) { 00025 $dontlimitchars = 0; 00026 } 00027 $is_gotofile = FALSE; 00028 if (isset($after_insert) && $after_insert == 'new_insert') { 00029 $goto = 'tbl_change.php?' 00030 . PMA_generate_common_url($db, $table, '&') 00031 . '&goto=' . urlencode($goto) 00032 . '&pos=' . $pos 00033 . '&session_max_rows=' . $session_max_rows 00034 . '&disp_direction=' . $disp_direction 00035 . '&repeat_cells=' . $repeat_cells 00036 . '&dontlimitchars=' . $dontlimitchars 00037 . (empty($sql_query) ? '' : '&sql_query=' . urlencode($sql_query)); 00038 } elseif (isset($after_insert) && $after_insert == 'same_insert') { 00039 $goto = 'tbl_change.php?' 00040 . PMA_generate_common_url($db, $table, '&') 00041 . '&goto=' . urlencode($goto) 00042 . '&pos=' . $pos 00043 . '&session_max_rows=' . $session_max_rows 00044 . '&disp_direction=' . $disp_direction 00045 . '&repeat_cells=' . $repeat_cells 00046 . '&dontlimitchars=' . $dontlimitchars 00047 . (empty($sql_query) ? '' : '&sql_query=' . urlencode($sql_query)); 00048 if (isset($primary_key)) { 00049 foreach ($primary_key AS $pk) { 00050 $goto .= '&primary_key[]=' . $pk; 00051 } 00052 } 00053 } elseif (isset($after_insert) && $after_insert == 'edit_next') { 00054 $goto = 'tbl_change.php?' 00055 . PMA_generate_common_url($db, $table, '&') 00056 . '&goto=' . urlencode($goto) 00057 . '&pos=' . $pos 00058 . '&session_max_rows=' . $session_max_rows 00059 . '&disp_direction=' . $disp_direction 00060 . '&repeat_cells=' . $repeat_cells 00061 . '&dontlimitchars=' . $dontlimitchars 00062 . (empty($sql_query) ? '' : '&sql_query=' . urlencode($sql_query)); 00063 if (isset($primary_key)) { 00064 foreach ($primary_key AS $pk) { 00065 $local_query = 'SELECT * FROM ' . PMA_backquote($table) . ' WHERE ' . str_replace('` =', '` >', urldecode($pk)) . ' LIMIT 1;'; 00066 $res = PMA_DBI_query($local_query); 00067 $row = PMA_DBI_fetch_row($res); 00068 $meta = PMA_DBI_get_fields_meta($res); 00069 $goto .= '&primary_key[]=' . urlencode(PMA_getUvaCondition($res, count($row), $meta, $row)); 00070 } 00071 } 00072 } else if ($goto == 'sql.php') { 00073 $goto = 'sql.php?' 00074 . PMA_generate_common_url($db, $table, '&') 00075 . '&pos=' . $pos 00076 . '&session_max_rows=' . $session_max_rows 00077 . '&disp_direction=' . $disp_direction 00078 . '&repeat_cells=' . $repeat_cells 00079 . '&dontlimitchars=' . $dontlimitchars 00080 . '&sql_query=' . urlencode($sql_query); 00081 } else if (!empty($goto)) { 00082 // Security checkings 00083 $is_gotofile = preg_replace('@^([^?]+).*$@', '\\1', $goto); 00084 if (!@file_exists('./' . $is_gotofile)) { 00085 $goto = (empty($table)) ? 'db_details.php' : 'tbl_properties.php'; 00086 $is_gotofile = TRUE; 00087 } else { 00088 $is_gotofile = ($is_gotofile == $goto); 00089 } 00090 } 00091 00092 // Defines the url to return in case of failure of the query 00093 if (isset($err_url)) { 00094 $err_url = urldecode($err_url); 00095 } else { 00096 $err_url = str_replace('&', '&', $goto) 00097 . (empty($primary_key) ? '' : '&primary_key=' . (is_array($primary_key) ? $primary_key[0] : $primary_key)); 00098 } 00099 00100 // Misc 00101 $seen_binary = FALSE; 00102 00106 if (isset($primary_key)) { 00107 // we were editing something => use primary key 00108 $loop_array = (is_array($primary_key) ? $primary_key : array(0 => $primary_key)); 00109 $using_key = TRUE; 00110 $is_insert = ($submit_type == $strInsertAsNewRow); 00111 } else { 00112 // new row => use indexes 00113 $loop_array = array(); 00114 for ($i = 0; $i < $cfg['InsertRows']; $i++) $loop_array[$i] = $i; 00115 $using_key = FALSE; 00116 $is_insert = TRUE; 00117 } 00118 00119 $query = array(); 00120 $message = ''; 00121 00122 foreach ($loop_array AS $primary_key_index => $enc_primary_key) { 00123 // skip fields to be ignored 00124 if (!$using_key && isset($GLOBALS['insert_ignore_' . $enc_primary_key])) continue; 00125 00126 // Restore the "primary key" to a convenient format 00127 $primary_key = urldecode($enc_primary_key); 00128 00129 // Defines the SET part of the sql query 00130 $valuelist = ''; 00131 $fieldlist = ''; 00132 00133 // Map multi-edit keys to single-level arrays, dependent on how we got the fields 00134 $me_fields = isset($fields['multi_edit']) && isset($fields['multi_edit'][$enc_primary_key]) ? $fields['multi_edit'][$enc_primary_key] : null; 00135 $me_fields_prev = isset($fields_prev['multi_edit']) && isset($fields_prev['multi_edit'][$enc_primary_key]) ? $fields_prev['multi_edit'][$enc_primary_key] : null; 00136 $me_funcs = isset($funcs['multi_edit']) && isset($funcs['multi_edit'][$enc_primary_key]) ? $funcs['multi_edit'][$enc_primary_key] : null; 00137 $me_fields_type = isset($fields_type['multi_edit']) && isset($fields_type['multi_edit'][$enc_primary_key]) ? $fields_type['multi_edit'][$enc_primary_key] : null; 00138 $me_fields_null = isset($fields_null['multi_edit']) && isset($fields_null['multi_edit'][$enc_primary_key]) ? $fields_null['multi_edit'][$enc_primary_key] : null; 00139 00140 if ($using_key && isset($me_fields_type) && is_array($me_fields_type) && isset($primary_key)) { 00141 $prot_result = PMA_DBI_query('SELECT * FROM ' . PMA_backquote($table) . ' WHERE ' . $primary_key . ';'); 00142 $prot_row = PMA_DBI_fetch_assoc($prot_result); 00143 PMA_DBI_free_result($prot_result); 00144 unset($prot_result); 00145 } 00146 00147 foreach ($me_fields AS $encoded_key => $val) { 00148 $key = urldecode($encoded_key); 00149 $fieldlist .= PMA_backquote($key) . ', '; 00150 00151 require('./tbl_replace_fields.php'); 00152 00153 if (empty($me_funcs[$encoded_key])) { 00154 $cur_value = $val . ', '; 00155 } else if (preg_match('@^(UNIX_TIMESTAMP)$@', $me_funcs[$encoded_key]) && $val != '\'\'') { 00156 $cur_value = $me_funcs[$encoded_key] . '(' . $val . '), '; 00157 } else if (preg_match('@^(NOW|CURDATE|CURTIME|UNIX_TIMESTAMP|RAND|USER|LAST_INSERT_ID)$@', $me_funcs[$encoded_key])) { 00158 $cur_value = $me_funcs[$encoded_key] . '(), '; 00159 } else { 00160 $cur_value = $me_funcs[$encoded_key] . '(' . $val . '), '; 00161 } 00162 00163 if ($is_insert) { 00164 // insert, no need to add column 00165 $valuelist .= $cur_value; 00166 } else if (empty($me_funcs[$encoded_key]) 00167 && isset($me_fields_prev) && isset($me_fields_prev[$encoded_key]) 00168 && ("'" . PMA_sqlAddslashes(urldecode($me_fields_prev[$encoded_key])) . "'" == $val)) { 00169 // No change for this column and no MySQL function is used -> next column 00170 continue; 00171 } 00172 else if (!empty($val)) { 00173 $valuelist .= PMA_backquote($key) . ' = ' . $cur_value; 00174 } 00175 } // end while 00176 00177 // get rid of last , 00178 $valuelist = preg_replace('@, $@', '', $valuelist); 00179 00180 // Builds the sql query 00181 if ($is_insert) { 00182 if (empty($query)) { 00183 // first inserted row -> prepare template 00184 $fieldlist = preg_replace('@, $@', '', $fieldlist); 00185 $query = array('INSERT INTO ' . PMA_backquote($table) . ' (' . $fieldlist . ') VALUES '); 00186 } 00187 // append current values 00188 $query[0] .= '(' . $valuelist . '), '; 00189 $message = $strInsertedRows . ' '; 00190 } elseif (!empty($valuelist)) { 00191 // build update query 00192 $query[] = 'UPDATE ' . PMA_backquote($table) . ' SET ' . $valuelist . ' WHERE' . $primary_key . ' LIMIT 1'; 00193 00194 $message = $strAffectedRows . ' '; 00195 } 00196 } // end for 00197 00198 // trim last , from insert query 00199 if ($is_insert) { 00200 $query[0] = preg_replace('@, $@', '', $query[0]); 00201 } 00202 00203 if (empty($valuelist) && empty($query)) { 00204 // No change -> move back to the calling script 00205 $message = $strNoModification; 00206 if ($is_gotofile) { 00207 $js_to_run = 'functions.js'; 00208 require_once('./header.inc.php'); 00209 require('./' . PMA_securePath($goto)); 00210 } else { 00211 PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . $goto . '&disp_message=' . urlencode($message) . '&disp_query='); 00212 00213 } 00214 exit(); 00215 } 00216 00221 $sql_query = implode(';', $query) . ';'; 00222 $total_affected_rows = 0; 00223 $last_message = ''; 00224 00225 foreach ($query AS $query_index => $single_query) { 00226 if ($cfg['IgnoreMultiSubmitErrors']) { 00227 $result = PMA_DBI_try_query($single_query); 00228 } else { 00229 $result = PMA_DBI_query($single_query); 00230 } 00231 if (!$result) { 00232 $message .= PMA_DBI_getError(); 00233 } else { 00234 if (@PMA_DBI_affected_rows()) { 00235 $total_affected_rows += @PMA_DBI_affected_rows(); 00236 } 00237 00238 $insert_id = PMA_DBI_insert_id(); 00239 if ($insert_id != 0) { 00240 $last_message .= '[br]'.$strInsertedRowId . ' ' . $insert_id; 00241 } 00242 } // end if 00243 PMA_DBI_free_result($result); 00244 unset($result); 00245 } 00246 00247 if ($total_affected_rows != 0) { 00248 $message .= $total_affected_rows; 00249 } else { 00250 $message .= $strModifications; 00251 } 00252 00253 $message .= $last_message; 00254 00255 if ($is_gotofile) { 00256 if ($goto == 'db_details.php' && !empty($table)) { 00257 unset($table); 00258 } 00259 $js_to_run = 'functions.js'; 00260 $active_page = $goto; 00261 require_once('./header.inc.php'); 00262 require('./' . PMA_securePath($goto)); 00263 } else { 00264 // I don't understand this one: 00265 //$add_query = (strpos(' ' . $goto, 'tbl_change') ? '&disp_query=' . urlencode($sql_query) : ''); 00266 00267 // if we have seen binary, 00268 // we do not append the query to the Location so it won't be displayed 00269 // on the resulting page 00270 // Nijel: we also need to limit size of url... 00271 $add_query = (!$seen_binary && strlen($sql_query) < 1024 ? '&disp_query=' . urlencode($sql_query) : ''); 00272 PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . $goto . '&disp_message=' . urlencode($message) . $add_query); 00273 } 00274 exit(); 00275 ?>