Documentation TYPO3 par Ameos |
00001 <?php 00002 /* $Id: tbl_replace.php,v 2.8 2003/12/15 21:40:22 garvinhicking 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 00018 // Defines the url to return in case of success of the query 00019 if (isset($sql_query)) { 00020 $sql_query = urldecode($sql_query); 00021 } 00022 if (!isset($dontlimitchars)) { 00023 $dontlimitchars = 0; 00024 } 00025 $is_gotofile = FALSE; 00026 if (isset($after_insert) && $after_insert == 'new_insert') { 00027 $goto = 'tbl_change.php?' 00028 . PMA_generate_common_url($db, $table, '&') 00029 . '&goto=' . urlencode($goto) 00030 . '&pos=' . $pos 00031 . '&session_max_rows=' . $session_max_rows 00032 . '&disp_direction=' . $disp_direction 00033 . '&repeat_cells=' . $repeat_cells 00034 . '&dontlimitchars=' . $dontlimitchars 00035 . (empty($sql_query) ? '' : '&sql_query=' . urlencode($sql_query)); 00036 } else if ($goto == 'sql.php') { 00037 $goto = 'sql.php?' 00038 . PMA_generate_common_url($db, $table, '&') 00039 . '&pos=' . $pos 00040 . '&session_max_rows=' . $session_max_rows 00041 . '&disp_direction=' . $disp_direction 00042 . '&repeat_cells=' . $repeat_cells 00043 . '&dontlimitchars=' . $dontlimitchars 00044 . '&sql_query=' . urlencode($sql_query); 00045 } else if (!empty($goto)) { 00046 // Security checkings 00047 $is_gotofile = preg_replace('@^([^?]+).*$@', '\\1', $goto); 00048 if (!@file_exists('./' . $is_gotofile)) { 00049 $goto = (empty($table)) ? 'db_details.php' : 'tbl_properties.php'; 00050 $is_gotofile = TRUE; 00051 } else { 00052 $is_gotofile = ($is_gotofile == $goto); 00053 } 00054 } 00055 00056 // Defines the url to return in case of failure of the query 00057 if (isset($err_url)) { 00058 $err_url = urldecode($err_url); 00059 } else { 00060 $err_url = str_replace('&', '&', $goto) 00061 . (empty($primary_key) ? '' : '&primary_key=' . (is_array($primary_key) ? $primary_key[0] : $primary_key)); 00062 } 00063 00064 // Resets tables defined in the configuration file 00065 if (isset($funcs)) { 00066 reset($funcs); 00067 } 00068 00069 // Misc 00070 $seen_binary = FALSE; 00071 00075 if (isset($primary_key) && ($submit_type != $strInsertAsNewRow)) { 00076 $loop_array = (is_array($primary_key) ? $primary_key : array(0 => $primary_key)); 00077 PMA_mysql_select_db($db); 00078 $query = array(); 00079 $message = ''; 00080 00081 foreach($loop_array AS $primary_key_index => $enc_primary_key) { 00082 // Restore the "primary key" to a convenient format 00083 $primary_key = urldecode($enc_primary_key); 00084 00085 // Defines the SET part of the sql query 00086 $valuelist = ''; 00087 00088 // Map multi-edit keys to single-level arrays, dependent on how we got the fields 00089 $me_fields = (isset($fields['multi_edit']) && isset($fields['multi_edit'][$enc_primary_key]) ? $fields['multi_edit'][$enc_primary_key] : (isset($fields) ? $fields : null)); 00090 $me_fields_prev = (isset($fields_prev['multi_edit']) && isset($fields_prev['multi_edit'][$enc_primary_key]) ? $fields_prev['multi_edit'][$enc_primary_key] : (isset($fields_prev) ? $fields_prev : null)); 00091 $me_funcs = (isset($funcs['multi_edit']) && isset($funcs['multi_edit'][$enc_primary_key]) ? $funcs['multi_edit'][$enc_primary_key] : (isset($funcs) ? $funcs : null)); 00092 $me_fields_type = (isset($fields_type['multi_edit']) && isset($fields_type['multi_edit'][$enc_primary_key]) ? $fields_type['multi_edit'][$enc_primary_key] : (isset($fields_type) ? $fields_type : null)); 00093 $me_fields_null = (isset($fields_null['multi_edit']) && isset($fields_null['multi_edit'][$enc_primary_key]) ? $fields_null['multi_edit'][$enc_primary_key] : (isset($fields_null) ? $fields_null : null)); 00094 00095 foreach($me_fields AS $key => $val) { 00096 $encoded_key = $key; 00097 $key = urldecode($key); 00098 00099 require('./tbl_replace_fields.php'); 00100 00101 // No change for this column and no MySQL function is used -> next column 00102 if (empty($me_funcs[$encoded_key]) 00103 && isset($me_fields_prev) && isset($me_fields_prev[$encoded_key]) 00104 && ("'" . PMA_sqlAddslashes(urldecode($me_fields_prev[$encoded_key])) . "'" == $val)) { 00105 continue; 00106 } 00107 else if (!empty($val)) { 00108 if (empty($me_funcs[$encoded_key])) { 00109 $valuelist .= PMA_backquote($key) . ' = ' . $val . ', '; 00110 } else if ($val == '\'\'' 00111 && (preg_match('@^(NOW|CURDATE|CURTIME|UNIX_TIMESTAMP|RAND|USER|LAST_INSERT_ID)$@', $me_funcs[$encoded_key]))) { 00112 $valuelist .= PMA_backquote($key) . ' = ' . $me_funcs[$encoded_key] . '(), '; 00113 } else { 00114 $valuelist .= PMA_backquote($key) . ' = ' . $me_funcs[$encoded_key] . "($val), "; 00115 } 00116 } 00117 } // end while 00118 00119 // Builds the sql update query 00120 $valuelist = preg_replace('@, $@', '', $valuelist); 00121 if (!empty($valuelist)) { 00122 $query[] = 'UPDATE ' . PMA_backquote($table) . ' SET ' . $valuelist . ' WHERE' . $primary_key 00123 . ' LIMIT 1'; 00124 00125 // lem9: why a line break here? 00126 //$message = $strAffectedRows . ' <br />'; 00127 $message = $strAffectedRows . ' '; 00128 } 00129 } 00130 00131 if (empty($valuelist)) { 00132 // No change -> move back to the calling script 00133 $message = $strNoModification; 00134 if ($is_gotofile) { 00135 $js_to_run = 'functions.js'; 00136 require_once('./header.inc.php'); 00137 require('./' . preg_replace('@\.\.*@', '.', $goto)); 00138 } else { 00139 header('Location: ' . $cfg['PmaAbsoluteUri'] . $goto . '&disp_message=' . urlencode($message) . '&disp_query='); 00140 } 00141 exit(); 00142 } 00143 } // end row update 00144 00145 00149 else { 00150 $loop_array = (isset($primary_key) && is_array($primary_key) ? $primary_key : array(0 => (isset($primary_key) ? $primary_key : null))); 00151 $query = array(); 00152 $message = ''; 00153 PMA_mysql_select_db($db); 00154 00155 foreach($loop_array AS $primary_key_index => $enc_primary_key) { 00156 $fieldlist = ''; 00157 $valuelist = ''; 00158 00159 $me_fields = (isset($fields['multi_edit']) && isset($fields['multi_edit'][$enc_primary_key]) ? $fields['multi_edit'][$enc_primary_key] : (isset($fields) ? $fields : null)); 00160 $me_fields_prev = (isset($fields_prev['multi_edit']) && isset($fields_prev['multi_edit'][$enc_primary_key]) ? $fields_prev['multi_edit'][$enc_primary_key] : (isset($fields_prev) ? $fields_prev : null)); 00161 $me_funcs = (isset($funcs['multi_edit']) && isset($funcs['multi_edit'][$enc_primary_key]) ? $funcs['multi_edit'][$enc_primary_key] : (isset($funcs) ? $funcs : null)); 00162 $me_fields_type = (isset($fields_type['multi_edit']) && isset($fields_type['multi_edit'][$enc_primary_key]) ? $fields_type['multi_edit'][$enc_primary_key] : (isset($fields_type) ? $fields_type : null)); 00163 $me_fields_null = (isset($fields_null['multi_edit']) && isset($fields_null['multi_edit'][$enc_primary_key]) ? $fields_null['multi_edit'][$enc_primary_key] : (isset($fields_null) ? $fields_null : null)); 00164 00165 // garvin: Get, if sent, any protected fields to insert them here: 00166 if (isset($me_fields_type) && is_array($me_fields_type) && isset($enc_primary_key)) { 00167 $prot_local_query = 'SELECT * FROM ' . PMA_backquote($table) . ' WHERE ' . urldecode($enc_primary_key); 00168 $prot_result = PMA_mysql_query($prot_local_query) or PMA_mysqlDie('', $prot_local_query, '', $err_url); 00169 $prot_row = PMA_mysql_fetch_array($prot_result); 00170 } 00171 00172 foreach($me_fields AS $key => $val) { 00173 $encoded_key = $key; 00174 $key = urldecode($key); 00175 $fieldlist .= PMA_backquote($key) . ', '; 00176 00177 require('./tbl_replace_fields.php'); 00178 00179 if (empty($me_funcs[$encoded_key])) { 00180 $valuelist .= $val . ', '; 00181 } else if (($val == '\'\'' 00182 && preg_match('@^(UNIX_TIMESTAMP|RAND|LAST_INSERT_ID)$@', $me_funcs[$encoded_key])) 00183 || preg_match('@^(NOW|CURDATE|CURTIME|USER)$@', $me_funcs[$encoded_key])) { 00184 $valuelist .= $me_funcs[$encoded_key] . '(), '; 00185 } else { 00186 $valuelist .= $me_funcs[$encoded_key] . '(' . $val . '), '; 00187 } 00188 } // end while 00189 00190 // Builds the sql insert query 00191 $fieldlist = preg_replace('@, $@', '', $fieldlist); 00192 $valuelist = preg_replace('@, $@', '', $valuelist); 00193 $query[] = 'INSERT INTO ' . PMA_backquote($table) . ' (' . $fieldlist . ') VALUES (' . $valuelist . ')'; 00194 $message = $strInsertedRows . ' '; 00195 } 00196 } // end row insertion 00197 00198 00203 $sql_query = implode(';', $query) . ';'; 00204 $total_affected_rows = 0; 00205 $last_message = ''; 00206 00207 foreach($query AS $query_index => $single_query) { 00208 $result = PMA_mysql_query($single_query); 00209 if (!$result) { 00210 if ($cfg['IgnoreMultiSubmitErrors']) { 00211 $message .= PMA_mysql_error(); 00212 } else { 00213 $error = PMA_mysql_error(); 00214 require_once('./header.inc.php'); 00215 PMA_mysqlDie($error, '', '', $err_url); 00216 } 00217 } else { 00218 if (@mysql_affected_rows()) { 00219 $total_affected_rows += @mysql_affected_rows(); 00220 } 00221 00222 $insert_id = mysql_insert_id(); 00223 if ($insert_id != 0) { 00224 $last_message .= '<br />'.$strInsertedRowId . ' ' . $insert_id; 00225 } 00226 } // end if 00227 } 00228 00229 if ($total_affected_rows != 0) { 00230 //$message .= '<br />' . $total_affected_rows; 00231 $message .= $total_affected_rows; 00232 } else { 00233 $message .= $strModifications; 00234 } 00235 00236 $message .= $last_message; 00237 00238 if ($is_gotofile) { 00239 if ($goto == 'db_details.php' && !empty($table)) { 00240 unset($table); 00241 } 00242 $js_to_run = 'functions.js'; 00243 $active_page = $goto; 00244 require_once('./header.inc.php'); 00245 require('./' . preg_replace('@\.\.*@', '.', $goto)); 00246 } else { 00247 // I don't understand this one: 00248 //$add_query = (strpos(' ' . $goto, 'tbl_change') ? '&disp_query=' . urlencode($sql_query) : ''); 00249 00250 // if we have seen binary, 00251 // we do not append the query to the Location so it won't be displayed 00252 // on the resulting page 00253 // Nijel: we also need to limit size of url... 00254 $add_query = (!$seen_binary && strlen($sql_query) < 1024 ? '&disp_query=' . urlencode($sql_query) : ''); 00255 header('Location: ' . $cfg['PmaAbsoluteUri'] . $goto . '&disp_message=' . urlencode($message) . $add_query); 00256 } 00257 exit(); 00258 ?>