diff --git a/libraries/insert_edit.lib.php b/libraries/insert_edit.lib.php new file mode 100644 index 0000000000..10d2e60e87 --- /dev/null +++ b/libraries/insert_edit.lib.php @@ -0,0 +1,1737 @@ + $db, + 'table' => $table, + 'goto' => $GLOBALS['goto'], + 'err_url' => $err_url, + 'sql_query' => $_REQUEST['sql_query'], + ); + if (isset($where_clauses)) { + foreach ($where_clause_array as $key_id => $where_clause) { + $_form_params['where_clause[' . $key_id . ']'] = trim($where_clause); + } + } + if (isset($_REQUEST['clause_is_unique'])) { + $_form_params['clause_is_unique'] = $_REQUEST['clause_is_unique']; + } + return $_form_params; +} + +/** + * Retrieve the values for pma edit mode + * + * @param type $where_clause where clauses + * @param type $table name of the table + * @param type $db name of the database + * + * @return type containing insert_mode,whereClauses, result array + * where_clauses_array and found_unique_key boolean value + */ +function PMA_getValuesForEditMode($where_clause, $table, $db) +{ + $found_unique_key = false; + if (isset($where_clause)) { + $where_clause_array = PMA_getWhereClauseArray($where_clause); + list($whereClauses, $resultArray, $rowsArray, $found_unique_key) + = PMA_analyzeWhereClauses($where_clause_array, $table, $db, $found_unique_key); + return array(false, $whereClauses, $resultArray, $rowsArray, $where_clause_array, $found_unique_key); + } else { + list($results, $row) = PMA_loadFirstRowInEditMode($table, $db); + return array(true, null, $results, $row, null, $found_unique_key); + } +} + +/** + * + * @return whereClauseArray array of where clauses + */ +function PMA_getWhereClauseArray($where_clause) +{ + if(isset ($where_clause)) { + if (is_array($where_clause)) { + return $where_clause; + } else { + return array(0 => $where_clause); + } + } +} + +/** + * Analysing where cluases array + * + * @param array $where_clause_array array of where clauses + * @param string $table name of the table + * @param string $db name of the database + * @param boolean $found_unique_key boolean variable for unique key + * + * @return array $where_clauses, $result, $rows + */ +function PMA_analyzeWhereClauses($where_clause_array, $table, $db, $found_unique_key) +{ + $rows = array(); + $result = array(); + $where_clauses = array(); + foreach ($where_clause_array as $key_id => $where_clause) { + $local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) + . ' WHERE ' . $where_clause . ';'; + $result[$key_id] = PMA_DBI_query($local_query, null, PMA_DBI_QUERY_STORE); + $rows[$key_id] = PMA_DBI_fetch_assoc($result[$key_id]); + $where_clauses[$key_id] = str_replace('\\', '\\\\', $where_clause); + $found_unique_key = PMA_showEmptyResultMessageOrSetUniqueCondition($rows, $key_id, + $where_clause_array, $local_query, $result, $found_unique_key); + } + return array($where_clauses, $result, $rows, $found_unique_key); +} + +/** + * Show message for empty reult or set the unique_condition + * + * @param array $rows + * @param string $key_id + * @param array $where_clause_array + * @param string $local_query + * @param array $result + * @param boolean $found_unique_key + * + * @return boolean $found_unique_key + */ +function PMA_showEmptyResultMessageOrSetUniqueCondition($rows, $key_id, + $where_clause_array, $local_query, $result, $found_unique_key +) { + // No row returned + if (! $rows[$key_id]) { + unset($rows[$key_id], $where_clause_array[$key_id]); + PMA_showMessage(__('MySQL returned an empty result set (i.e. zero rows).'), $local_query); + echo "\n"; + include 'libraries/footer.inc.php'; + } else {// end if (no row returned) + $meta = PMA_DBI_get_fields_meta($result[$key_id]); + list($unique_condition, $tmp_clause_is_unique) + = PMA_getUniqueCondition($result[$key_id], count($meta), $meta, $rows[$key_id], true); + if (! empty($unique_condition)) { + $found_unique_key = true; + } + unset($unique_condition, $tmp_clause_is_unique); + } + return $found_unique_key; +} + +/** + * No primary key given, just load first row + * + * @param string $table name of the table + * @param string $db name of the database + * + * @return array containing $result and $rows arrays + */ +function PMA_loadFirstRowInEditMode($table, $db) +{ + $result = PMA_DBI_query( + 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) . ' LIMIT 1;', + null, + PMA_DBI_QUERY_STORE + ); + $rows = array_fill(0, $GLOBALS['cfg']['InsertRows'], false); + return array($result, $rows); +} + +/** + * Add some url parameters + * + * @param array $url_params containing $db and $table as url parameters + * + * @return array Add some url parameters to $url_params array + * and return it + */ +function PMA_urlParamsInEditMode($url_params) +{ + if (isset($_REQUEST['where_clause'])) { + $url_params['where_clause'] = trim($_REQUEST['where_clause']); + } + if (! empty($_REQUEST['sql_query'])) { + $url_params['sql_query'] = $_REQUEST['sql_query']; + } + return $url_params; +} + +/** + * Show function fields in data edit view in pma + * + * @param array $url_params containing url parameters + * + * @return string an html snippet + */ +function PMA_showFunctionFieldsInEditMode($url_params, $showFuncFields) +{ + $params = array(); + if(! $showFuncFields) { + $params['ShowFunctionFields'] = 1; + } else { + $params['ShowFunctionFields'] = 0; + } + $params['ShowFieldTypesInDataEditView'] = $GLOBALS['cfg']['ShowFieldTypesInDataEditView']; + $params['goto'] = 'sql.php'; + $this_url_params = array_merge($url_params, $params); + if(! $showFuncFields) { + return ' : ' . __('Function') . '' . "\n"; + } + return ' ' . __('Function') . '' . "\n"; +} + +/** + * Show field types in data edit view in pma + * + * @param array $url_params containing url parameters + * + * @return stirng an html snippet + */ +function PMA_showColumnTypesInDataEditView($url_params, $showColumnType ) +{ + $params = array(); + if(! $showColumnType) { + $params['ShowFieldTypesInDataEditView'] = 1; + } else { + $params['ShowFieldTypesInDataEditView'] = 0; + } + $params['ShowFunctionFields'] = $GLOBALS['cfg']['ShowFunctionFields']; + $params['goto'] = 'sql.php'; + $this_other_url_params = array_merge($url_params, $params); + if(! $showColumnType) { + return ' : ' . __('Type') . '' . "\n"; + } + return ' ' . __('Type') . '' . "\n"; + +} + +/** + * Retrieve the default for datetime data type + * + * @param array $column containing column type, Default and null + */ +function PMA_getDefaultForDatetime($column) +{ + // d a t e t i m e + // + // Current date should not be set as default if the field is NULL + // for the current row, but do not put here the current datetime + // if there is a default value (the real default value will be set + // in the Default value logic below) + + // Note: (tested in MySQL 4.0.16): when lang is some UTF-8, + // $column['Default'] is not set if it contains NULL: + // Array ([Field] => d [Type] => datetime [Null] => YES [Key] => [Extra] => [True_Type] => datetime) + // but, look what we get if we switch to iso: (Default is NULL) + // Array ([Field] => d [Type] => datetime [Null] => YES [Key] => [Default] => [Extra] => [True_Type] => datetime) + // so I force a NULL into it (I don't think it's possible + // to have an empty default value for DATETIME) + // then, the "if" after this one will work + if ($column['Type'] == 'datetime' + && ! isset($column['Default']) + && isset($column['Null']) + && $column['Null'] == 'YES' + ) { + $column['Default'] = null; + } +} + + /** + * Analyze the table column array + * + * @param array $column description of column in given table + * @param array $comments_map comments for every column that has a comment + * @param integer $timestamp_seen 0 interger + * + * @return array description of column in given table + */ +function PMA_analyzeTableColumnsArray($column, $comments_map, $timestamp_seen) +{ + $column['Field_html'] = htmlspecialchars($column['Field']); + $column['Field_md5'] = md5($column['Field']); + // True_Type contains only the type (stops at first bracket) + $column['True_Type'] = preg_replace('@\(.*@s', '', $column['Type']); + PMA_getDefaultForDatetime($column); + $column['len'] = preg_match('@float|double@', $column['Type']) ? 100 : -1; + $column['Field_title'] = PMA_getColumnTitle($column, $comments_map); + $column['is_binary'] = PMA_isColumnBinary($column); + $column['is_blob'] = PMA_isColumnBlob($column); + $column['is_char'] = PMA_isColumnChar($column); + list($column['pma_type'], $column['wrap'], $column['first_timestamp']) = + PMA_getEnumSetAndTimestampColumns($column, $timestamp_seen); + + return $column; +} + + /** + * Retrieve the column title + * + * @param array $column description of column in given table + * @param array $comments_map comments for every column that has a comment + * + * @return string column title + */ +function PMA_getColumnTitle($column, $comments_map) +{ + if (isset($comments_map[$column['Field']])) { + return '' + . $column['Field_html'] . ''; + } else { + return $column['Field_html']; + } +} + + /** + * check whether the column is a bainary + * + * @param array $column description of column in given table + * + * @return boolean If check to ensure types such as "enum('one','two','binary',..)" or + * "enum('one','two','varbinary',..)" are not categorized as binary. + */ +function PMA_isColumnBinary($column) +{ + // The type column. + // Fix for bug #3152931 'ENUM and SET cannot have "Binary" option' + if (stripos($column['Type'], 'binary') === 0 + || stripos($column['Type'], 'varbinary') === 0 + ) { + return stristr($column['Type'], 'binary'); + } else { + return false; + } + +} + + /** + * check whether the column is a blob + * + * @param array $column description of column in given table + * + * @return boolean If check to ensure types such as "enum('one','two','blob',..)" or + * "enum('one','two','tinyblob',..)" etc. are not categorized as blob. + */ +function PMA_isColumnBlob($column) +{ + if (stripos($column['Type'], 'blob') === 0 + || stripos($column['Type'], 'tinyblob') === 0 + || stripos($column['Type'], 'mediumblob') === 0 + || stripos($column['Type'], 'longblob') === 0 + ) { + return stristr($column['Type'], 'blob'); + } else { + return false; + } +} + +/** + * check is table column char + * + * @param array $column description of column in given table + * + * @return boolean If check to ensure types such as "enum('one','two','char',..)" or + * "enum('one','two','varchar',..)" are not categorized as char. + */ +function PMA_isColumnChar($column) +{ + if (stripos($column['Type'], 'char') === 0 + || stripos($column['Type'], 'varchar') === 0 + ) { + return stristr($column['Type'], 'char'); + } else { + return false; + } +} +/** + * Retieve set, enum, timestamp table columns + * + * @param array $column description of column in given table + * @param int $timestamp_seen 0 interger + * + * return array $column['pma_type'], $column['wrap'], $column['first_timestamp'] + */ +function PMA_getEnumSetAndTimestampColumns($column, $timestamp_seen) +{ + $column['first_timestamp'] = false; + switch ($column['True_Type']) { + case 'set': + $column['pma_type'] = 'set'; + $column['wrap'] = ''; + break; + case 'enum': + $column['pma_type'] = 'enum'; + $column['wrap'] = ''; + break; + case 'timestamp': + if (!$timestamp_seen) { // can only occur once per table + $timestamp_seen = 1; + $column['first_timestamp'] = true; + } + $column['pma_type'] = $column['Type']; + $column['wrap'] = ' nowrap'; + break; + + default: + $column['pma_type'] = $column['Type']; + $column['wrap'] = ' nowrap'; + break; + } + return array($column['pma_type'], $column['wrap'], $column['first_timestamp']); +} + +/** + * The function column + * We don't want binary data to be destroyed + * Note: from the MySQL manual: "BINARY doesn't affect how the column is + * stored or retrieved" so it does not mean that the contents is binary + * + * @param array $column description of column in given table + * @param boolean $is_upload upload or no + * @param string $column_name_appendix the name atttibute + * @param string $unnullify_trigger validation string + * @param array $no_support_types list of datatypes that are not (yet) handled by PMA + * @param integer $tabindex_for_function +3000 + * @param integer $tabindex tab index + * @param integer $idindex id index + * @param boolean $insert_mode insert mode or edit mode + * + * @return string an html sippet + */ +function PMA_getFunctionColumn($column, $is_upload, $column_name_appendix, + $unnullify_trigger, $no_support_types, $tabindex_for_function, + $tabindex, $idindex, $insert_mode +) { + $html_output = ''; + if (($GLOBALS['cfg']['ProtectBinary'] && $column['is_blob'] && !$is_upload) + || ($GLOBALS['cfg']['ProtectBinary'] == 'all' && $column['is_binary']) + || ($GLOBALS['cfg']['ProtectBinary'] == 'noblob' && !$column['is_blob']) + ) { + $html_output .= ' ' . __('Binary') . '' . "\n"; + } elseif (strstr($column['True_Type'], 'enum') + || strstr($column['True_Type'], 'set') + || in_array($column['pma_type'], $no_support_types) + ) { + $html_output .= ' --' . "\n"; + } else { + $html_output .= '' . "\n"; + $html_output .= '' . "\n"; + $html_output .= '' . "\n"; + } + return $html_output; +} + +/** + * The null column + * + * @param array $column description of column in given table + * @param string $column_name_appendix the name atttibute + * @param array $real_null_value is column value null or not null + * @param integer $tabindex tab index + * @param integer $tabindex_for_null +6000 + * @param integer $idindex id index + * @param array $vkey [multi_edit]['row_id'] + * @param array $foreigners keys into foreign fields + * @param array $foreignData data about the foreign keys + * + * @return string an html snippet + */ +function PMA_getNullColumn($column, $column_name_appendix, $real_null_value, + $tabindex, $tabindex_for_null, $idindex, $vkey, $foreigners, $foreignData +) { + $html_output = ''; + $html_output .= ' ' . "\n"; + if ($column['Null'] == 'YES') { + $html_output .= ' '; + + // nullify_code is needed by the js nullify() function + $nullify_code = PMA_getNullifyCodeForNullColumn($column, $foreigners, $foreignData); + // to be able to generate calls to nullify() in jQuery + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + } + $html_output .= ' ' . "\n"; + + return $html_output; +} + +/** + * Retrieve the nullify code for the null column + * + * @param array $column description of column in given table + * @param array $foreigners keys into foreign fields + * @param array $foreignData data about the foreign keys + * + * @return integer $nullify_code + */ +function PMA_getNullifyCodeForNullColumn($column, $foreigners, $foreignData) +{ + if (strstr($column['True_Type'], 'enum')) { + if (strlen($column['Type']) > 20) { + $nullify_code = '1'; + } else { + $nullify_code = '2'; + } + } elseif (strstr($column['True_Type'], 'set')) { + $nullify_code = '3'; + } elseif ($foreigners && isset($foreigners[$column['Field']]) && $foreignData['foreign_link'] == false) { + // foreign key in a drop-down + $nullify_code = '4'; + } elseif ($foreigners && isset($foreigners[$column['Field']]) && $foreignData['foreign_link'] == true) { + // foreign key with a browsing icon + $nullify_code = '6'; + } else { + $nullify_code = '5'; + } + return $nullify_code; +} + +/** + * Get the HTML elements for value column in inert form + * + * @param array $column description of column in given table + * @param string $backup_field hidden input field + * @param string $column_name_appendix the name atttibute + * @param string $unnullify_trigger validation string + * @param integer $tabindex tab index + * @param integer $tabindex_for_value offset for the values tabindex + * @param integer $idindex id index + * @param array $data description of the column field + * @param array $special_chars special characters + * @param array $foreignData data about the foreign keys + * @param array $paramTableDbArray array containing $db and $table + * @param array $rownumber_param &rownumber=row_id + * @param array $titles An HTML IMG tag for a particular icon from a theme, + * which may be an actual file or an icon from a sprite + * @param array $text_dir + * @param string $special_chars_encoded replaced char if the string starts + * with a \r\n pair (0x0d0a) add an extra \n + * @param integer $biggest_max_file_size 0 intger + * @param string $default_char_editing default char editing mode which is stroe + * in the config.inc.php script + * @param array $no_support_types list of datatypes that are not (yet) handled by PMA + * @param array $gis_data_types list of GIS data types + * + * @return string an html snippet + */ +function PMA_getValueColumn($column, $backup_field, $column_name_appendix, + $unnullify_trigger,$tabindex, $tabindex_for_value, $idindex, $data, + $special_chars, $foreignData, $odd_row, $paramTableDbArray,$rownumber_param, + $titles, $text_dir, $special_chars_encoded, $vkey,$is_upload,$biggest_max_file_size, + $default_char_editing, $no_support_types, $gis_data_types, $extracted_columnspec +) { + $html_output = ''; + + if ($foreignData['foreign_link'] == true) { + $html_output .= PMA_getForeignLink($column, $backup_field, $column_name_appendix, + $unnullify_trigger, $tabindex, $tabindex_for_value, $idindex, $data, + $paramTableDbArray, $rownumber_param, $titles + ); + + } elseif (is_array($foreignData['disp_row'])) { + $html_output .= PMA_dispRowForeignData($backup_field, $column_name_appendix, + $unnullify_trigger, $tabindex, $tabindex_for_value, $idindex, $data, $foreignData + ); + + } elseif ($GLOBALS['cfg']['LongtextDoubleTextarea'] && strstr($column['pma_type'], 'longtext')) { + $html_output = ' '; + $html_output .= ''; + $html_output .= '' + . ''; + $html_output .= PMA_getTextarea($column,$backup_field, $column_name_appendix, $unnullify_trigger, + $tabindex, $tabindex_for_value, $idindex, $text_dir, $special_chars_encoded + ); + + } elseif (strstr($column['pma_type'], 'text')) { + $html_output .= PMA_getTextarea($column,$backup_field, $column_name_appendix, $unnullify_trigger, + $tabindex, $tabindex_for_value, $idindex, $text_dir, $special_chars_encoded + ); + $html_output .= "\n"; + if (strlen($special_chars) > 32000) { + $html_output .= " \n"; + $html_output .= ' ' . __('Because of its length,
this column might not be editable'); + } + + } elseif ($column['pma_type'] == 'enum') { + $html_output .= PMA_getPmaTypeEnum($paramsArrayForColumns, $column,$extracted_columnspec); + + } elseif ($column['pma_type'] == 'set') { + $html_output .= PMA_getPmaTypeSet($column,$extracted_columnspec, $backup_field, + $column_name_appendix, $unnullify_trigger, $tabindex, $tabindex_for_value, $idindex); + + } elseif ($column['is_binary'] || $column['is_blob']) { + $html_output .= PMA_getBinaryAndBlobColumn($column, $data, $special_chars,$biggest_max_file_size, + $backup_field,$column_name_appendix, $unnullify_trigger, $tabindex, $tabindex_for_value, + $idindex, $text_dir, $special_chars_encoded, $vkey, $is_upload); + + } elseif (! in_array($column['pma_type'], $no_support_types)) { + $html_output .= PMA_getNoSupportTypes($column, $default_char_editing,$backup_field, + $column_name_appendix, $unnullify_trigger,$tabindex,$special_chars, + $tabindex_for_value, $idindex, $text_dir, $special_chars_encoded, $data, $extracted_columnspec); + } + + if (in_array($column['pma_type'], $gis_data_types)) { + $html_output .= PMA_getHTMLforGisDataTypes($current_row, $column); + } + + return $html_output; +} + +/** + * Get HTML for foreign link in insert form + * + * @param array $column description of column in given table + * @param string $backup_field hidden input field + * @param string $column_name_appendix the name atttibute + * @param string $unnullify_trigger validation string + * @param integer $tabindex tab index + * @param integer $tabindex_for_value offset for the values tabindex + * @param integer $idindex id index + * @param array $data + * @param array $paramTableDbArray array containing $db and $table + * @param array $rownumber_param &rownumber=row_id + * @param array $titles An HTML IMG tag for a particular icon from a theme, + * which may be an actual file or an icon from a sprite + * + * @return string an html snippet + */ +function PMA_getForeignLink($column, $backup_field, $column_name_appendix, + $unnullify_trigger, $tabindex, $tabindex_for_value, $idindex, $data, + $paramTableDbArray, $rownumber_param, $titles +) { + list($db, $table) = $paramTableDbArray; + $html_output = ''; + $html_output .= $backup_field . "\n"; + $html_output .= ''; + $html_output .= '' + . '' + . str_replace("'", "\'", $titles['Browse']) . ''; + return $html_output; +} + +/** + * Get HTML to display foreign data + * + * @param string $backup_field hidden input field + * @param string $column_name_appendix the name atttibute + * @param string $unnullify_trigger validation string + * @param integer $tabindex tab index + * @param integer $tabindex_for_value offset for the values tabindex + * @param integer $idindex id index + * @param array $data + * @param array $foreignData data about the foreign keys + * + * @return string an html snippet + */ +function PMA_dispRowForeignData($backup_field, $column_name_appendix, + $unnullify_trigger, $tabindex, $tabindex_for_value, $idindex, $data, $foreignData +) { + $html_output = ''; + $html_output .= $backup_field . "\n"; + $html_output .= '' + . ''; + + return $html_output; +} + +/** + * Get HTML for enum type + * + * @param array $column description of column in given table + * @param string $backup_field hidden input field + * @param string $column_name_appendix the name atttibute + * @param array $extracted_columnspec associative array containing type, spec_in_brackets + * and possibly enum_set_values (another array) + * + * @return string an html snippet + */ +function PMA_getPmaTypeEnum($column, $backup_field, $column_name_appendix, $extracted_columnspec) +{ + $html_output = ''; + if (! isset($column['values'])) { + $column['values'] = PMA_getColumnEnumValues($column, $extracted_columnspec); + } + $column_enum_values = $column['values']; + $html_output .= ''; + $html_output .= ''; + $html_output .= "\n" . ' ' . $backup_field . "\n"; + if (strlen($column['Type']) > 20) { + $html_output .= PMA_getDropDownDependingOnLength($column, $column_name_appendix, $unnullify_trigger, + $tabindex, $tabindex_for_value, $idindex, $data, $column_enum_values + ); + } else { + $html_output .= PMA_getRadioButtonDependingOnLength($column_name_appendix, $unnullify_trigger, + $tabindex, $column, $tabindex_for_value, $idindex, $data, $column_enum_values + ); + } + return $html_output; +} + +/** + * Get column values + * + * @param array $column description of column in given table + * @param array $extracted_columnspec associative array containing type, spec_in_brackets + * and possibly enum_set_values (another array) + * + * @return array column values as an associative array + */ +function PMA_getColumnEnumValues($column, $extracted_columnspec) +{ + $column['values'] = array(); + foreach ($extracted_columnspec['enum_set_values'] as $val) { + // Removes automatic MySQL escape format + $val = str_replace('\'\'', '\'', str_replace('\\\\', '\\', $val)); + $column['values'][] = array( + 'plain' => $val, + 'html' => htmlspecialchars($val), + ); + } + return $column['values']; +} + +/** + * Get HTML drop down for more than 20 string length + * + * @param array $column description of column in given table + * @param string $column_name_appendix the name atttibute + * @param string $unnullify_trigger validation string + * @param integer $tabindex tab index + * @param integer $tabindex_for_value offset for the values tabindex + * @param integer $idindex id index + * @param array $data + * @param array $column_enum_values $column['values'] + * + * @return string an html snippet + */ +function PMA_getDropDownDependingOnLength($column, $column_name_appendix, $unnullify_trigger, + $tabindex, $tabindex_for_value, $idindex, $data, $column_enum_values +) { + $html_output = ''; + return $html_output; +} + +/** + * Get HTML radio button for less than 20 string length + * + * @param string $column_name_appendix the name atttibute + * @param string $unnullify_trigger validation string + * @param integer $tabindex tab index + * @param array $column description of column in given table + * @param integer $tabindex_for_value offset for the values tabindex + * @param integer $idindex id index + * @param array $data + * @param array $column_enum_values $column['values'] + * + * @return string an html snippet + */ +function PMA_getRadioButtonDependingOnLength($column_name_appendix, $unnullify_trigger, + $tabindex, $column, $tabindex_for_value, $idindex, $data, $column_enum_values +) { + $j = 0; + $html_output = ''; + foreach ($column_enum_values as $enum_value) { + $html_output .= ' ' + . ''; + $html_output .= '' . "\n"; + $j++; + } + return $html_output; +} + +/** + * Get the HTML for 'set' pma type + * + * @param array $column description of column in given table + * @param array $extracted_columnspec associative array containing type, spec_in_brackets + * and possibly enum_set_values (another array) + * @param string $backup_field hidden input field + * @param string $column_name_appendix the name atttibute + * @param string $unnullify_trigger validation string + * @param integer $tabindex tab index + * @param integer $tabindex_for_value offset for the values tabindex + * @param integer $idindex id index + * + * @return string an html snippet + */ +function PMA_getPmaTypeSet($column,$extracted_columnspec, $backup_field, + $column_name_appendix, $unnullify_trigger, $tabindex, $tabindex_for_value, $idindex +) { + list($column_set_values, $select_size) = PMA_getColumnSetValueAndSelectSize($column, $extracted_columnspec); + $vset = array_flip(explode(',', $data)); + $html_output = $backup_field . "\n"; + $html_output .= ''; + $html_output .= ''; + return $html_output; +} + +/** + * Retrieve column 'set' value and select size + * + * @param array $column description of column in given table + * @param array $extracted_columnspec associative array containing type, spec_in_brackets + * and possibly enum_set_values (another array) + * + * @return array $column['values'], $column['select_size'] + */ +function PMA_getColumnSetValueAndSelectSize($column, $extracted_columnspec) +{ + if (! isset($column['values'])) { + $column['values'] = array(); + foreach ($extracted_columnspec['enum_set_values'] as $val) { + $column['values'][] = array( + 'plain' => $val, + 'html' => htmlspecialchars($val), + ); + } + $column['select_size'] = min(4, count($column['values'])); + } + return array($column['values'], $column['select_size']); +} + +/** + * Get HTML for binary and blob column + * + * @param array $column description of column in given table + * @param array $data + * @param array $special_chars special characters + * @param integer $biggest_max_file_size biggest max file size for uploading + * @param string $backup_field hidden input field + * @param string $column_name_appendix the name atttibute + * @param string $unnullify_trigger validation string + * @param integer $tabindex tab index + * @param integer $tabindex_for_value offset for the values tabindex + * @param integer $idindex id index + * @param string $text_dir + * @param string $special_chars_encoded replaced char if the string starts + * with a \r\n pair (0x0d0a) add an extra \n + * @param string $vkey [multi_edit]['row_id'] + * @param boolean $is_upload is upload or not + * + * @return string an html snippet + */ +function PMA_getBinaryAndBlobColumn($column, $data, $special_chars,$biggest_max_file_size, + $backup_field, $column_name_appendix, $unnullify_trigger, $tabindex, + $tabindex_for_value, $idindex, $text_dir, $special_chars_encoded,$vkey, $is_upload +) { + $html_output = ''; + if (($GLOBALS['cfg']['ProtectBinary'] && $column['is_blob']) + || ($GLOBALS['cfg']['ProtectBinary'] == 'all' && $column['is_binary']) + || ($GLOBALS['cfg']['ProtectBinary'] == 'noblob' && !$column['is_blob']) + ) { + $html_output .= __('Binary - do not edit'); + if (isset($data)) { + $data_size = PMA_formatByteDown(strlen(stripslashes($data)), 3, 1); + $html_output .= ' ('. $data_size [0] . ' ' . $data_size[1] . ')'; + unset($data_size); + } + + $html_output .= '' + . ''; + } elseif ($column['is_blob']) { + $html_output .= "\n" + . PMA_getTextarea($column,$backup_field, $column_name_appendix, $unnullify_trigger, + $tabindex, $tabindex_for_value, $idindex, $text_dir, $special_chars_encoded); + } else { + // field size should be at least 4 and max $GLOBALS['cfg']['LimitChars'] + $fieldsize = min(max($column['len'], 4), $GLOBALS['cfg']['LimitChars']); + $html_output .= "\n" + . $backup_field . "\n" + . PMA_getHTMLinput($column, $column_name_appendix, $special_chars, $fieldsize, + $unnullify_trigger, $tabindex, $tabindex_for_value, $idindex); + } + + if ($is_upload && $column['is_blob']) { + $html_output .= '
' + . ' '; + list($html_out, $biggest_max_file_size) = PMA_getMaxUploadSize($column,$biggest_max_file_size); + $html_output .= $html_out; + } + + if (!empty($GLOBALS['cfg']['UploadDir'])) { + $html_output .= PMA_getSelectOptionForUpload($vkey, $column); + } + + return $html_output; +} + +/** + * Get HTML input type + * + * @param array $column description of column in given table + * @param string $column_name_appendix the name atttibute + * @param array $special_chars special characters + * @param integer $fieldsize html field size + * @param string $unnullify_trigger validation string + * @param integer $tabindex tab index + * @param integer $tabindex_for_value offset for the values tabindex + * @param integer $idindex id index + * + * @return string an html snippet + */ +function PMA_getHTMLinput($column, $column_name_appendix, $special_chars, + $fieldsize, $unnullify_trigger, $tabindex, $tabindex_for_value, $idindex +) { + $the_class = 'textfield'; + if ($column['pma_type'] == 'date') { + $the_class .= ' datefield'; + } elseif ($column['pma_type'] == 'datetime' + || substr($column['pma_type'], 0, 9) == 'timestamp' + ) { + $the_class .= ' datetimefield'; + } + return ''; +} + +/** + * Get HTML select option for upload + * + * @param string $vkey [multi_edit]['row_id'] + * @param array $column description of column in given table + * + * @return string an html snippet + */ +function PMA_getSelectOptionForUpload($vkey, $column) +{ + $files = PMA_getFileSelectOptions(PMA_userDir($GLOBALS['cfg']['UploadDir'])); + if ($files === false) { + return ' ' . __('Error') . '
' . "\n" + . ' ' . __('The directory you set for upload work cannot be reached') . "\n"; + } elseif (!empty($files)) { + return "
\n" + . ' ' . __('Or') . '' . ' ' . __('web server upload directory') . ':
' . "\n" + . ' ' . "\n"; + } +} + +/** + * Retrieve the maximum upload file size + * + * @param array $column description of column in given table + * @param integer $biggest_max_file_size biggest max file size for uploading + * + * @return array an html snippet and $biggest_max_file_size + */ +function PMA_getMaxUploadSize($column, $biggest_max_file_size) +{ + // find maximum upload size, based on field type + /** + * @todo with functions this is not so easy, as you can basically + * process any data with function like MD5 + */ + global $max_upload_size; + $max_field_sizes = array( + 'tinyblob' => '256', + 'blob' => '65536', + 'mediumblob' => '16777216', + 'longblob' => '4294967296'); // yeah, really + + $this_field_max_size = $max_upload_size; // from PHP max + if ($this_field_max_size > $max_field_sizes[$column['pma_type']]) { + $this_field_max_size = $max_field_sizes[$column['pma_type']]; + } + $html_output = PMA_getFormattedMaximumUploadSize($this_field_max_size) . "\n"; + // do not generate here the MAX_FILE_SIZE, because we should + // put only one in the form to accommodate the biggest field + if ($this_field_max_size > $biggest_max_file_size) { + $biggest_max_file_size = $this_field_max_size; + } + return array($html_output, $biggest_max_file_size); +} + +/** + * Get HTML for pma no support types + * + * @param array $column description of column in given table + * @param string $default_char_editing default char editing mode which is stroe + * in the config.inc.php script + * @param string $backup_field hidden input field + * @param string $column_name_appendix the name atttibute + * @param string $unnullify_trigger validation string + * @param integer $tabindex tab index + * @param array $special_chars apecial characters + * @param integer $tabindex_for_value offset for the values tabindex + * @param integer $idindex id index + * @param string $text_dir + * @param array $special_chars_encoded replaced char if the string starts + * with a \r\n pair (0x0d0a) add an extra \n + * + * @return string an html snippet + */ +function PMA_getNoSupportTypes($column, $default_char_editing,$backup_field, + $column_name_appendix, $unnullify_trigger,$tabindex,$special_chars, + $tabindex_for_value, $idindex, $text_dir, $special_chars_encoded,$data, $extracted_columnspec +) { + $fieldsize = PMA_getColumnSize($column, $extracted_columnspec); + $html_output = $backup_field . "\n"; + if ($column['is_char'] + && ($GLOBALS['cfg']['CharEditing'] == 'textarea' + || strpos($data, "\n") !== false) + ) { + $html_output .= "\n"; + $GLOBALS['cfg']['CharEditing'] = $default_char_editing; + $html_output .= PMA_getTextarea($column, $backup_field, $column_name_appendix, $unnullify_trigger, + $tabindex, $tabindex_for_value, $idindex, $text_dir, $special_chars_encoded); + } else { + $html_output .= PMA_getHTMLinput($column, $column_name_appendix, $special_chars, + $fieldsize, $unnullify_trigger, $tabindex, $tabindex_for_value, $idindex); + + if ($column['Extra'] == 'auto_increment') { + $html_output .= ''; + + } + if (substr($column['pma_type'], 0, 9) == 'timestamp') { + $html_output .= ''; + + } + if (substr($column['pma_type'], 0, 8) == 'datetime') { + $html_output .= ''; + + } + if ($column['True_Type'] == 'bit') { + $html_output .= ''; + + } + if ($column['pma_type'] == 'date' + || $column['pma_type'] == 'datetime' + || substr($column['pma_type'], 0, 9) == 'timestamp' + ) { + // the _3 suffix points to the date field + // the _2 suffix points to the corresponding NULL checkbox + // in dateFormat, 'yy' means the year with 4 digits + } + } + return $html_output; +} + +/** + * Get the field size + * + * @param array $column description of column in given table + * + * @return integer field size + */ +function PMA_getColumnSize($column, $extracted_columnspec) +{ + if ($column['is_char']) { + $fieldsize = $extracted_columnspec['spec_in_brackets']; + if ($fieldsize > $GLOBALS['cfg']['MaxSizeForInputField']) { + /** + * This case happens for CHAR or VARCHAR columns which have + * a size larger than the maximum size for input field. + */ + $GLOBALS['cfg']['CharEditing'] = 'textarea'; + } + } else { + /** + * This case happens for example for INT or DATE columns; + * in these situations, the value returned in $column['len'] + * seems appropriate. + */ + $fieldsize = $column['len']; + } + return min(max($fieldsize, $GLOBALS['cfg']['MinSizeForInputField']), $GLOBALS['cfg']['MaxSizeForInputField']); +} + +/** + * Get HTML for gis data types + * + * @param string $current_row row description + * @param array $column description of column in given table + * + * @return string an html snippet + */ +function PMA_getHTMLforGisDataTypes($current_row, $column) +{ + $data_val = isset($current_row[$column['Field']]) ? $current_row[$column['Field']] : ''; + $_url_params = array( + 'field' => $column['Field_title'], + 'value' => $data_val, + ); + if ($column['pma_type'] != 'geometry') { + $_url_params = $_url_params + array('gis_data[gis_type]' => strtoupper($column['pma_type'])); + } + $edit_str = PMA_getIcon('b_edit.png', __('Edit/Insert')); + return '' + . PMA_linkOrButton('#', $edit_str, array(), false, false, '_blank') + . ''; +} + +/** + * get html for continue insertion form + * + * @param string $table name of the table + * @param string $db name of the database + * @param array $where_clause_array array of where clauses + * @param string $err_url error url + * + * @return string an html snippet + */ +function PMA_getContinueInsertionForm($table, $db, $where_clause_array, $err_url) +{ + $html_output = '
' + . PMA_generate_common_hidden_inputs($db, $table) + . '' + . '' + . ''; + + if (isset($_REQUEST['where_clause'])) { + foreach ($where_clause_array as $key_id => $where_clause) { + $html_output .= ''. "\n"; + } + } + $tmp = '' . "\n"; + $html_output .= "\n" . sprintf(__('Continue insertion with %s rows'), $tmp); + unset($tmp); + $html_output .= '
' . "\n"; + return $html_output; +} + +/** + * Get action panel + * + * @param integer $tabindex tab index + * @param integer $tabindex_for_value offset for the values tabindex + * @param boolean $found_unique_key boolean variable for unique key + * + * @return string an html snippet + */ +function PMA_getActionsPanel($where_clause, $after_insert, $tabindex, + $tabindex_for_value, $found_unique_key +) { + $html_output = '
' + . '' + . '' + . '' + . '' + . '' + . ''; + $html_output .='' + . PMA_getSumbitAndResetButtonForActionsPanel($tabindex, $tabindex_for_value) + . '' + . '
' + . PMA_getSubmitTypeDropDown($where_clause, $tabindex, $tabindex_for_value) + . "\n"; + + $html_output .= '' + . '   ' . __('and then') . '   ' + . '' + . PMA_getAfterInsertDropDown($where_clause, $after_insert, $found_unique_key) + . '
' + . '
'; + return $html_output; +} + +/** + * Get a HTML drop down for submit types + * + * @param array $where_clause where clause + * @param integer $tabindex tab index + * @param integer $tabindex_for_value offset for the values tabindex + * + * @return string an html snippet + */ +function PMA_getSubmitTypeDropDown($where_clause, $tabindex, $tabindex_for_value) +{ + $html_output = ''; + return $html_output; +} + +/** + * Get HTML drop down for after insert + * + * @param array $where_clause where clause + * @param string $after_insert insert mode, e.g. new_insert, same_insert + * @param string $after_insert a request parameter it can be 'edit_text', 'back' + * @param boolean $found_unique_key boolean variable for unique key + * + * @return string an html snippet + */ +function PMA_getAfterInsertDropDown($where_clause, $after_insert, $found_unique_key) +{ + $html_output = ''; + return $html_output; + +} + +/** + * get Submit button and Reset button for action panel + * + * @param integer $tabindex tab index + * @param integer $tabindex_for_value offset for the values tabindex + * + * @return string an html snippet + */ +function PMA_getSumbitAndResetButtonForActionsPanel($tabindex, $tabindex_for_value) +{ + return '' + . PMA_showHint(__('Use TAB key to move from value to value, or CTRL+arrows to move anywhere')) + . '' + . '' + . '' + . '' + . ''; +} + +/** + * Get table head and table foot for insert row table + * + * @param array $url_params url parameters + * + * @return string an html snippet + */ +function PMA_getHeadAndFootOfInsertRowTable($url_params) +{ + $html_output = '' + . '' + . '' + . ''; + + if ($GLOBALS['cfg']['ShowFieldTypesInDataEditView']) { + $html_output .= PMA_showColumnTypesInDataEditView($url_params, true); + } + if ($GLOBALS['cfg']['ShowFunctionFields']) { + $html_output .= PMA_showFunctionFieldsInEditMode($url_params, true); + } + + $html_output .= '' + . '' + . '' + . '' + . ' ' + . '' + . '' + . '' + . ''; + return $html_output; +} + +/** + * Prepares the field value and retrieve special chars, backup field and data array + * + * @param array $current_row a row of the table + * @param array $column description of column in given table + * @param array $extracted_columnspec associative array containing type, spec_in_brackets + * and possibly enum_set_values (another array) + * @param boolean $real_null_value whether column value null or not null + * + * @return array $real_null_value, $data, $special_chars, $backup_field, $special_chars_encoded + */ +function PMA_getSpecialCharsAndBackupFieldForExistingRow($current_row, $column, $extracted_columnspec, + $real_null_value, $gis_data_types, $column_name_appendix) +{ + $special_chars_encoded = ''; + // (we are editing) + if (is_null($current_row[$column['Field']])) { + $real_null_value = true; + $current_row[$column['Field']] = ''; + $special_chars = ''; + $data = $current_row[$column['Field']]; + } elseif ($column['True_Type'] == 'bit') { + $special_chars = PMA_printable_bit_value( + $current_row[$column['Field']], $extracted_columnspec['spec_in_brackets'] + ); + } elseif (in_array($column['True_Type'], $gis_data_types)) { + // Convert gis data to Well Know Text format + $current_row[$column['Field']] = PMA_asWKT($current_row[$column['Field']], true); + $special_chars = htmlspecialchars($current_row[$column['Field']]); + } else { + // special binary "characters" + if ($column['is_binary'] || ($column['is_blob'] && ! $GLOBALS['cfg']['ProtectBinary'])) { + if ($_SESSION['tmp_user_values']['display_binary_as_hex'] && $GLOBALS['cfg']['ShowFunctionFields']) { + $current_row[$column['Field']] = bin2hex($current_row[$column['Field']]); + $column['display_binary_as_hex'] = true; + } else { + $current_row[$column['Field']] = PMA_replaceBinaryContents($current_row[$column['Field']]); + } + } // end if + $special_chars = htmlspecialchars($current_row[$column['Field']]); + + //We need to duplicate the first \n or otherwise we will lose + //the first newline entered in a VARCHAR or TEXT column + $special_chars_encoded = PMA_duplicateFirstNewline($special_chars); + + $data = $current_row[$column['Field']]; + } // end if... else... + + //when copying row, it is useful to empty auto-increment column to prevent duplicate key error + if (isset($_REQUEST['default_action']) && $_REQUEST['default_action'] === 'insert') { + if ($column['Key'] === 'PRI' && strpos($column['Extra'], 'auto_increment') !== false) { + $data = $special_chars_encoded = $special_chars = null; + } + } + // If a timestamp field value is not included in an update + // statement MySQL auto-update it to the current timestamp; + // however, things have changed since MySQL 4.1, so + // it's better to set a fields_prev in this situation + $backup_field = ''; + + return array($real_null_value, $special_chars_encoded, $special_chars, $data, $backup_field); +} + +/** + * display default values + * + * @param type $column description of column in given table + * @param boolean $real_null_value whether column value null or not null + * + * @return array $real_null_value, $data, $special_chars, $backup_field, $special_chars_encoded + */ +function PMA_getSpecialCharsAndBackupFieldForInsertingMode($column, $real_null_value) +{ + if (! isset($column['Default'])) { + $column['Default'] = ''; + $real_null_value = true; + $data = ''; + } else { + $data = $column['Default']; + } + + if ($column['True_Type'] == 'bit') { + $special_chars = PMA_convert_bit_default_value($column['Default']); + } else { + $special_chars = htmlspecialchars($column['Default']); + } + $backup_field = ''; + $special_chars_encoded = PMA_duplicateFirstNewline($special_chars); + // this will select the UNHEX function while inserting + if (($column['is_binary'] || ($column['is_blob'] && ! $GLOBALS['cfg']['ProtectBinary'])) + && (isset($_SESSION['tmp_user_values']['display_binary_as_hex']) + && $_SESSION['tmp_user_values']['display_binary_as_hex']) + && $GLOBALS['cfg']['ShowFunctionFields'] + ) { + $column['display_binary_as_hex'] = true; + } + return array($real_null_value, $data, $special_chars, $backup_field, $special_chars_encoded); +} + +/** + * Prepares the update/insert of a row + * + * @return array $loop_array, $using_key, $is_insert, $is_insertignore + */ +function PMA_getParamsForUpdateOrInsert() +{ + if (isset($_REQUEST['where_clause'])) { + // we were editing something => use the WHERE clause + $loop_array = (is_array($_REQUEST['where_clause']) ? $_REQUEST['where_clause'] : array($_REQUEST['where_clause'])); + $using_key = true; + $is_insert = $_REQUEST['submit_type'] == 'insert' + || $_REQUEST['submit_type'] == 'showinsert' + || $_REQUEST['submit_type'] == 'insertignore'; + $is_insertignore = $_REQUEST['submit_type'] == 'insertignore'; + } else { + // new row => use indexes + $loop_array = array(); + foreach ($_REQUEST['fields']['multi_edit'] as $key => $dummy) { + $loop_array[] = $key; + } + $using_key = false; + $is_insert = true; + $is_insertignore = false; + } + return array($loop_array, $using_key, $is_insert, $is_insertignore); +} + +/** + * check wether insert row mode and if so include tbl_changen script and set global variables. + * + * @return void + */ +function PMA_isInsertRow() +{ + if (isset($_REQUEST['insert_rows']) + && is_numeric($_REQUEST['insert_rows']) + && $_REQUEST['insert_rows'] != $GLOBALS['cfg']['InsertRows'] + ) { + $GLOBALS['cfg']['InsertRows'] = $_REQUEST['insert_rows']; + $GLOBALS['js_include'][] = 'tbl_change.js'; + include_once 'libraries/header.inc.php'; + include 'tbl_change.php'; + exit; + } +} + +/** + * set $_SESSION for edit_next + * + * @param string $one_where_clause one where clause from where clauses array + * + * @return void + */ +function PMA_setSessionForEditNext($one_where_clause) +{ + $local_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table']) + . ' WHERE ' . str_replace('` =', '` >', $one_where_clause) + . ' LIMIT 1;'; + $res = PMA_DBI_query($local_query); + $row = PMA_DBI_fetch_row($res); + $meta = PMA_DBI_get_fields_meta($res); + // must find a unique condition based on unique key, + // not a combination of all fields + list($unique_condition, $clause_is_unique) + = PMA_getUniqueCondition($res, count($meta), $meta, $row, true); + if (! empty($unique_condition)) { + $_SESSION['edit_next'] = $unique_condition; + } + unset($unique_condition, $clause_is_unique); +} + +/** + * set $goto_include varible for different cases and retrieve like, + * if $GLOBALS['goto'] empty, if $goto_include previously not defined + * and new_insert, same_insert, same_insert + * + * @param string $goto_include store some script for include, otherwise it is boolean false + * @return string $goto_include + */ +function PMA_getGotoInclude($goto_include) +{ + if (isset($_REQUEST['after_insert']) + && in_array($_REQUEST['after_insert'], array('new_insert', 'same_insert', 'same_insert')) + ) { + $goto_include = 'tbl_change.php'; + } elseif (! empty($GLOBALS['goto'])) { + if (! preg_match('@^[a-z_]+\.php$@', $GLOBALS['goto'])) { + // this should NOT happen + //$GLOBALS['goto'] = false; + $goto_include = false; + } else { + $goto_include = $GLOBALS['goto']; + } + if ($GLOBALS['goto'] == 'db_sql.php' && strlen($GLOBALS['table'])) { + $GLOBALS['table'] = ''; + } + } + if (! $goto_include) { + if (! strlen($GLOBALS['table'])) { + $goto_include = 'db_sql.php'; + } else { + $goto_include = 'tbl_sql.php'; + } + } + return $goto_include; +} + +/** + * Defines the url to return in case of failure of the query + * + * @param array $url_params url parameters + * @return string error url for query failure + */ +function PMA_getErrorUrl($url_params) +{ + if (isset($_REQUEST['err_url'])) { + return $_REQUEST['err_url']; + } else { + return 'tbl_change.php' . PMA_generate_common_url($url_params); + } +} + +/** + * Builds the sql query + * + * @param boolean $is_insertignore $_REQUEST['submit_type'] == 'insertignore' + * @param array $query_fields column names array + * @param array $value_sets array of query values + * @return string a query + */ +function PMA_buildSqlQuery($is_insertignore, $query_fields, $value_sets) +{ + if ($is_insertignore) { + $insert_command = 'INSERT IGNORE '; + } else { + $insert_command = 'INSERT '; + } + $query[] = $insert_command . 'INTO ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table']) + . ' (' . implode(', ', $query_fields) . ') VALUES (' . implode('), (', $value_sets) . ')'; + unset($insert_command, $query_fields); + return $query; +} + +/** + * Executes the sql query and get the result, then move back to the calling page + * + * @param array $url_params url paramters array + * @param string $query built query from PMA_buildSqlQuery() + * @return array $url_params, $total_affected_rows, $last_messages + * $warning_messages, $error_messages + */ +function PMA_executeSqlQuery($url_params, $query) +{ + if (! empty($GLOBALS['sql_query'])) { + $url_params['sql_query'] = $GLOBALS['sql_query']; + $return_to_sql_query = $GLOBALS['sql_query']; + } + $GLOBALS['sql_query'] = implode('; ', $query) . ';'; + // to ensure that the query is displayed in case of + // "insert as new row" and then "insert another new row" + $GLOBALS['display_query'] = $GLOBALS['sql_query']; + + $total_affected_rows = 0; + $last_messages = array(); + $warning_messages = array(); + $error_messages = array(); + + foreach ($query as $single_query) { + if ($_REQUEST['submit_type'] == 'showinsert') { + $last_messages[] = PMA_Message::notice(__('Showing SQL query')); + continue; + } + if ($GLOBALS['cfg']['IgnoreMultiSubmitErrors']) { + $result = PMA_DBI_try_query($single_query); + } else { + $result = PMA_DBI_query($single_query); + } + if (! $result) { + $error_messages[] = PMA_Message::sanitize(PMA_DBI_getError()); + } else { + // The next line contains a real assignment, it's not a typo + if ($tmp = @PMA_DBI_affected_rows()) { + $total_affected_rows += $tmp; + } + unset($tmp); + + $insert_id = PMA_DBI_insert_id(); + if ($insert_id != 0) { + // insert_id is id of FIRST record inserted in one insert, so if we + // inserted multiple rows, we had to increment this + + if ($total_affected_rows > 0) { + $insert_id = $insert_id + $total_affected_rows - 1; + } + $last_message = PMA_Message::notice(__('Inserted row id: %1$d')); + $last_message->addParam($insert_id); + $last_messages[] = $last_message; + } + PMA_DBI_free_result($result); + } + $warning_messages = PMA_getWarningMessages(); + } + unset($result, $single_query, $last_message, $query); + return array($url_params, $total_affected_rows, $last_messages, $warning_messages, $error_messages); +} + +/** + * get the warning messages array + * + * @return array $warning_essages + */ +function PMA_getWarningMessages() +{ + $warning_essages = array(); + foreach (PMA_DBI_get_warnings() as $warning) { + $warning_essages[] + = PMA_Message::sanitize( + $warning['Level'] . ': #' . $warning['Code'] . ' ' . $warning['Message'] + ); + } + return $warning_essages; +} +?> diff --git a/libraries/tbl_replace_fields.inc.php b/libraries/tbl_replace_fields.inc.php index 5549c33456..e5a33d4654 100644 --- a/libraries/tbl_replace_fields.inc.php +++ b/libraries/tbl_replace_fields.inc.php @@ -42,8 +42,8 @@ if (false !== $possibly_uploaded_val) { // f i e l d v a l u e i n t h e f o r m - if (isset($me_fields_type[$key])) { - $type = $me_fields_type[$key]; + if (isset($multi_edit_columns_type[$key])) { + $type = $multi_edit_columns_type[$key]; } else { $type = ''; } @@ -51,7 +51,7 @@ if (false !== $possibly_uploaded_val) { // $key contains the md5() of the fieldname if ($type != 'protected' && $type != 'set' && 0 === strlen($val)) { // best way to avoid problems in strict mode (works also in non-strict mode) - if (isset($me_auto_increment) && isset($me_auto_increment[$key])) { + if (isset($multi_edit_auto_increment) && isset($multi_edit_auto_increment[$key])) { $val = 'NULL'; } else { $val = "''"; @@ -72,8 +72,8 @@ if (false !== $possibly_uploaded_val) { // when in UPDATE mode, do not alter field's contents. When in INSERT // mode, insert empty field because no values were submitted. If protected // blobs where set, insert original fields content. - if (! empty($prot_row[$me_fields_name[$key]])) { - $val = '0x' . bin2hex($prot_row[$me_fields_name[$key]]); + if (! empty($prot_row[$multi_edit_columns_name[$key]])) { + $val = '0x' . bin2hex($prot_row[$multi_edit_columns_name[$key]]); } else { $val = ''; } @@ -89,14 +89,14 @@ if (false !== $possibly_uploaded_val) { // Was the Null checkbox checked for this field? // (if there is a value, we ignore the Null checkbox: this could // be possible if Javascript is disabled in the browser) - if (! empty($me_fields_null[$key]) && ($val == "''" || $val == '')) { + if (! empty($multi_edit_columns_null[$key]) && ($val == "''" || $val == '')) { $val = 'NULL'; } // The Null checkbox was unchecked for this field if (empty($val) - && ! empty($me_fields_null_prev[$key]) - && ! isset($me_fields_null[$key]) + && ! empty($multi_edit_columns_null_prev[$key]) + && ! isset($multi_edit_columns_null[$key]) ) { $val = "''"; } diff --git a/tbl_change.php b/tbl_change.php index 5b42085dea..59addef9ad 100644 --- a/tbl_change.php +++ b/tbl_change.php @@ -19,38 +19,34 @@ require_once 'libraries/common.lib.php'; */ require_once 'libraries/db_table_exists.lib.php'; +/** + * functions implementation for this script + */ +require_once 'libraries/insert_edit.lib.php'; + /** * Sets global variables. * Here it's better to use a if, instead of the '?' operator * to avoid setting a variable to '' when it's not present in $_REQUEST */ + if (isset($_REQUEST['where_clause'])) { $where_clause = $_REQUEST['where_clause']; } -if (isset($_REQUEST['clause_is_unique'])) { - $clause_is_unique = $_REQUEST['clause_is_unique']; -} if (isset($_SESSION['edit_next'])) { $where_clause = $_SESSION['edit_next']; unset($_SESSION['edit_next']); $after_insert = 'edit_next'; } -if (isset($_REQUEST['sql_query'])) { - $sql_query = $_REQUEST['sql_query']; -} if (isset($_REQUEST['ShowFunctionFields'])) { $cfg['ShowFunctionFields'] = $_REQUEST['ShowFunctionFields']; } if (isset($_REQUEST['ShowFieldTypesInDataEditView'])) { $cfg['ShowFieldTypesInDataEditView'] = $_REQUEST['ShowFieldTypesInDataEditView']; } -if (isset($_REQUEST['default_action'])) { - $default_action = $_REQUEST['default_action']; -} if (isset($_REQUEST['after_insert'])) { $after_insert = $_REQUEST['after_insert']; } - /** * file listing */ @@ -73,8 +69,8 @@ if (empty($GLOBALS['goto'])) { * @todo check if we could replace by "db_|tbl_" - please clarify!? */ $_url_params = array( - 'db' => $db, - 'sql_query' => $sql_query + 'db' => $db, + 'sql_query' => $_REQUEST['sql_query'] ); if (preg_match('@^tbl_@', $GLOBALS['goto'])) { @@ -133,7 +129,7 @@ if (! empty($disp_message)) { if (! isset($disp_query)) { $disp_query = null; } - echo PMA_getMessage($disp_message, $disp_query); + PMA_showMessage($disp_message, $disp_query); } /** @@ -152,63 +148,27 @@ unset($show_create_table); */ PMA_DBI_select_db($db); $table_fields = array_values(PMA_DBI_get_columns($db, $table)); -$rows = array(); -if (isset($where_clause)) { - // when in edit mode load all selected rows from table - $insert_mode = false; - if (is_array($where_clause)) { - $where_clause_array = $where_clause; - } else { - $where_clause_array = array(0 => $where_clause); - } - $result = array(); - $found_unique_key = false; - $where_clauses = array(); +$paramTableDbArray = array($table, $db); - foreach ($where_clause_array as $key_id => $where_clause) { - $local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) - . ' WHERE ' . $where_clause . ';'; - $result[$key_id] = PMA_DBI_query($local_query, null, PMA_DBI_QUERY_STORE); - $rows[$key_id] = PMA_DBI_fetch_assoc($result[$key_id]); - $where_clauses[$key_id] = str_replace('\\', '\\\\', $where_clause); - - // No row returned - if (! $rows[$key_id]) { - unset($rows[$key_id], $where_clause_array[$key_id]); - echo PMA_getMessage(__('MySQL returned an empty result set (i.e. zero rows).'), $local_query); - echo "\n"; - include 'libraries/footer.inc.php'; - } else { // end if (no row returned) - $meta = PMA_DBI_get_fields_meta($result[$key_id]); - list($unique_condition, $tmp_clause_is_unique) - = PMA_getUniqueCondition($result[$key_id], count($meta), $meta, $rows[$key_id], true); - if (! empty($unique_condition)) { - $found_unique_key = true; - } - unset($unique_condition, $tmp_clause_is_unique); - } - - } -} else { - // no primary key given, just load first row - but what happens if table is empty? - $insert_mode = true; - $result = PMA_DBI_query( - 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) . ' LIMIT 1;', - null, - PMA_DBI_QUERY_STORE - ); - $rows = array_fill(0, $cfg['InsertRows'], false); +if (! isset($where_clause)) { + $where_clause = null; } +//Retrieve values for data edit view +list($insert_mode, $where_clauses, $result, $rows, $where_clause_array, $found_unique_key) + = PMA_getValuesForEditMode($where_clause, $table, $db); // Copying a row - fetched data will be inserted as a new row, therefore the where clause is needless. -if (isset($default_action) && $default_action === 'insert') { +if (isset($_REQUEST['default_action']) && $_REQUEST['default_action'] === 'insert') { unset($where_clause, $where_clauses); } // retrieve keys into foreign fields, if any -$foreigners = PMA_getForeigners($db, $table); +$foreigners = PMA_getForeigners($db, $table); +// Retrieve form parameters for insert/edit form +$_form_params = PMA_getFormParametersForInsertForm( + $db, $table, $where_clauses, $where_clause_array, $err_url); /** * Displays the form @@ -216,53 +176,21 @@ $foreigners = PMA_getForeigners($db, $table); // autocomplete feature of IE kills the "onchange" event handler and it // must be replaced by the "onpropertychange" one in this case $chg_evt_handler = (PMA_USR_BROWSER_AGENT == 'IE' && PMA_USR_BROWSER_VER >= 5 && PMA_USR_BROWSER_VER < 7) - ? 'onpropertychange' - : 'onchange'; + ? 'onpropertychange' + : 'onchange'; // Had to put the URI because when hosted on an https server, // some browsers send wrongly this form to the http server. - ?> - - - - $db, - 'table' => $table, - 'goto' => $GLOBALS['goto'], - 'err_url' => $err_url, - 'sql_query' => $sql_query, -); -if (isset($where_clauses)) { - foreach ($where_clause_array as $key_id => $where_clause) { - $_form_params['where_clause[' . $key_id . ']'] = trim($where_clause); - } -} -if (isset($clause_is_unique)) { - $_form_params['clause_is_unique'] = $clause_is_unique; -} - -?> - - -> -' + . ''; // Set if we passed the first timestamp field $timestamp_seen = 0; -$fields_cnt = count($table_fields); +$columns_cnt = count($table_fields); $tabindex = 0; $tabindex_for_function = +3000; @@ -271,246 +199,104 @@ $tabindex_for_value = 0; $o_rows = 0; $biggest_max_file_size = 0; -// user can toggle the display of Function column -// (currently does not work for multi-edits) $url_params['db'] = $db; $url_params['table'] = $table; -if (isset($where_clause)) { - $url_params['where_clause'] = trim($where_clause); -} -if (! empty($sql_query)) { - $url_params['sql_query'] = $sql_query; -} +$url_params = PMA_urlParamsInEditMode($url_params); +//Insert/Edit form +$html_output .= ' 1, - 'ShowFieldTypesInDataEditView' => $cfg['ShowFieldTypesInDataEditView'], - 'goto' => 'sql.php' - ) - ); - echo ' : ' . __('Function') . '' . "\n"; -} -if (! $cfg['ShowFieldTypesInDataEditView']) { - $this_other_url_params = array_merge( - $url_params, - array( - 'ShowFieldTypesInDataEditView' => 1, - 'ShowFunctionFields' => $cfg['ShowFunctionFields'], - 'goto' => 'sql.php' - ) - ); - echo ' : ' . __('Type') . '' . "\n"; + $html_output .= __('Show'); } -foreach ($rows as $row_id => $vrow) { - if ($vrow === false) { - unset($vrow); +if (! $cfg['ShowFunctionFields']) { + $html_output .= PMA_showFunctionFieldsInEditMode($url_params, false); +} + +if (! $cfg['ShowFieldTypesInDataEditView']) { + $html_output .= PMA_showColumnTypesInDataEditView($url_params, false); +} + +foreach ($rows as $row_id => $current_row) { + if ($current_row === false) { + unset($current_row); } $jsvkey = $row_id; $rownumber_param = '&rownumber=' . $row_id; $vkey = '[multi_edit][' . $jsvkey . ']'; - $vresult = (isset($result) && is_array($result) && isset($result[$row_id]) ? $result[$row_id] : $result); + $current_result = (isset($result) && is_array($result) && isset($result[$row_id]) + ? $result[$row_id] + : $result); if ($insert_mode && $row_id > 0) { - echo ''; - echo '
' . "\n"; + $html_output .= '' + .'
' . "\n"; } -?> -
' . __('Column') . ''. __('Null') . '' . __('Value') . '
' + . '' + . '
- - - + + $html_output .= PMA_getHeadAndFootOfInsertRowTable($url_params) + . ''; - 0, - 'ShowFunctionFields' => $cfg['ShowFunctionFields'], - 'goto' => 'sql.php' - ) - ); - echo ' ' . "\n"; - } - - if ($cfg['ShowFunctionFields']) { - $this_url_params = array_merge( - $url_params, - array( - 'ShowFunctionFields' => 0, - 'ShowFieldTypesInDataEditView' => $cfg['ShowFieldTypesInDataEditView'], - 'goto' => 'sql.php' - ) - ); - echo ' ' . "\n"; - } -?> - - - - - - - - - - - d [Type] => datetime [Null] => YES [Key] => [Extra] => [True_Type] => datetime) - // but, look what we get if we switch to iso: (Default is NULL) - // Array ([Field] => d [Type] => datetime [Null] => YES [Key] => [Default] => [Extra] => [True_Type] => datetime) - // so I force a NULL into it (I don't think it's possible - // to have an empty default value for DATETIME) - // then, the "if" after this one will work - if ($table_fields[$i]['Type'] == 'datetime' - && ! isset($table_fields[$i]['Default']) - && isset($table_fields[$i]['Null']) - && $table_fields[$i]['Null'] == 'YES' - ) { - $table_fields[$i]['Default'] = null; - } - - $table_fields[$i]['len'] - = preg_match('@float|double@', $table_fields[$i]['Type']) ? 100 : -1; - - - if (isset($comments_map[$table_fields[$i]['Field']])) { - $table_fields[$i]['Field_title'] = '' - . $table_fields[$i]['Field_html'] . ''; - } else { - $table_fields[$i]['Field_title'] = $table_fields[$i]['Field_html']; - } - - // The type column. - // Fix for bug #3152931 'ENUM and SET cannot have "Binary" option' - // If check to ensure types such as "enum('one','two','binary',..)" or - // "enum('one','two','varbinary',..)" are not categorized as binary. - if (stripos($table_fields[$i]['Type'], 'binary') === 0 - || stripos($table_fields[$i]['Type'], 'varbinary') === 0 - ) { - $table_fields[$i]['is_binary'] = stristr($table_fields[$i]['Type'], 'binary'); - } else { - $table_fields[$i]['is_binary'] = false; - } - - // If check to ensure types such as "enum('one','two','blob',..)" or - // "enum('one','two','tinyblob',..)" etc. are not categorized as blob. - if (stripos($table_fields[$i]['Type'], 'blob') === 0 - || stripos($table_fields[$i]['Type'], 'tinyblob') === 0 - || stripos($table_fields[$i]['Type'], 'mediumblob') === 0 - || stripos($table_fields[$i]['Type'], 'longblob') === 0 - ) { - $table_fields[$i]['is_blob'] = stristr($table_fields[$i]['Type'], 'blob'); - } else { - $table_fields[$i]['is_blob'] = false; - } - - // If check to ensure types such as "enum('one','two','char',..)" or - // "enum('one','two','varchar',..)" are not categorized as char. - if (stripos($table_fields[$i]['Type'], 'char') === 0 - || stripos($table_fields[$i]['Type'], 'varchar') === 0 - ) { - $table_fields[$i]['is_char'] = stristr($table_fields[$i]['Type'], 'char'); - } else { - $table_fields[$i]['is_char'] = false; - } - - $table_fields[$i]['first_timestamp'] = false; - switch ($table_fields[$i]['True_Type']) { - case 'set': - $table_fields[$i]['pma_type'] = 'set'; - $table_fields[$i]['wrap'] = ''; - break; - case 'enum': - $table_fields[$i]['pma_type'] = 'enum'; - $table_fields[$i]['wrap'] = ''; - break; - case 'timestamp': - if (!$timestamp_seen) { // can only occur once per table - $timestamp_seen = 1; - $table_fields[$i]['first_timestamp'] = true; - } - $table_fields[$i]['pma_type'] = $table_fields[$i]['Type']; - $table_fields[$i]['wrap'] = ' nowrap'; - break; - - default: - $table_fields[$i]['pma_type'] = $table_fields[$i]['Type']; - $table_fields[$i]['wrap'] = ' nowrap'; - break; - } + $column = $table_fields[$i]; + $column = PMA_analyzeTableColumnsArray($column, $comments_map, $timestamp_seen); } - $field = $table_fields[$i]; - $extracted_columnspec = PMA_extractColumnSpec($field['Type']); + + $extracted_columnspec = PMA_extractColumnSpec($column['Type']); - if (-1 === $field['len']) { - $field['len'] = PMA_DBI_field_len($vresult, $i); + if (-1 === $column['len']) { + $column['len'] = PMA_DBI_field_len($current_result, $i); // length is unknown for geometry fields, make enough space to edit very simple WKTs - if (-1 === $field['len']) { - $field['len'] = 30; + if (-1 === $column['len']) { + $column['len'] = 30; } } //Call validation when the form submited... $unnullify_trigger = $chg_evt_handler . "=\"return verificationsAfterFieldChange('" - . PMA_escapeJsString($field['Field_md5']) . "', '" - . PMA_escapeJsString($jsvkey) . "','".$field['pma_type']."')\""; + . PMA_escapeJsString($column['Field_md5']) . "', '" + . PMA_escapeJsString($jsvkey) . "','".$column['pma_type'] . "')\""; // Use an MD5 as an array index to avoid having special characters in the name atttibute (see bug #1746964 ) - $field_name_appendix = $vkey . '[' . $field['Field_md5'] . ']'; + $column_name_appendix = $vkey . '[' . $column['Field_md5'] . ']'; - if ($field['Type'] == 'datetime' - && ! isset($field['Default']) - && ! is_null($field['Default']) - && ($insert_mode || ! isset($vrow[$field['Field']])) + if ($column['Type'] == 'datetime' + && ! isset($column['Default']) + && ! is_null($column['Default']) + && ($insert_mode || ! isset($current_row[$column['Field']])) ) { // INSERT case or // UPDATE case with an NULL value - $vrow[$field['Field']] = date('Y-m-d H:i:s', time()); + $current_row[$column['Field']] = date('Y-m-d H:i:s', time()); } - ?> - - - - - - ' + . ''; + if ($cfg['ShowFieldTypesInDataEditView']) { + $html_output .= ''; + } //End if // Get a list of GIS data types. $gis_data_types = PMA_getGISDatatypes(); @@ -518,81 +304,19 @@ foreach ($rows as $row_id => $vrow) { // Prepares the field value $real_null_value = false; $special_chars_encoded = ''; - if (isset($vrow)) { + if (isset($current_row)) { // (we are editing) - if (is_null($vrow[$field['Field']])) { - $real_null_value = true; - $vrow[$field['Field']] = ''; - $special_chars = ''; - $data = $vrow[$field['Field']]; - } elseif ($field['True_Type'] == 'bit') { - $special_chars = PMA_printableBitValue( - $vrow[$field['Field']], $extracted_columnspec['spec_in_brackets'] - ); - } elseif (in_array($field['True_Type'], $gis_data_types)) { - // Convert gis data to Well Know Text format - $vrow[$field['Field']] = PMA_asWKT($vrow[$field['Field']], true); - $special_chars = htmlspecialchars($vrow[$field['Field']]); - } else { - // special binary "characters" - if ($field['is_binary'] || ($field['is_blob'] && ! $cfg['ProtectBinary'])) { - if ($_SESSION['tmp_user_values']['display_binary_as_hex'] && $cfg['ShowFunctionFields']) { - $vrow[$field['Field']] = bin2hex($vrow[$field['Field']]); - $field['display_binary_as_hex'] = true; - } else { - $vrow[$field['Field']] = PMA_replaceBinaryContents($vrow[$field['Field']]); - } - } // end if - $special_chars = htmlspecialchars($vrow[$field['Field']]); - - //We need to duplicate the first \n or otherwise we will lose - //the first newline entered in a VARCHAR or TEXT column - $special_chars_encoded = PMA_duplicateFirstNewline($special_chars); - - $data = $vrow[$field['Field']]; - } // end if... else... - - //when copying row, it is useful to empty auto-increment column to prevent duplicate key error - if (isset($default_action) && $default_action === 'insert') { - if ($field['Key'] === 'PRI' && strpos($field['Extra'], 'auto_increment') !== false) { - $data = $special_chars_encoded = $special_chars = null; - } - } - // If a timestamp field value is not included in an update - // statement MySQL auto-update it to the current timestamp; - // however, things have changed since MySQL 4.1, so - // it's better to set a fields_prev in this situation - $backup_field = ''; + list($real_null_value, $special_chars_encoded, $special_chars, $data, $backup_field) + = PMA_getSpecialCharsAndBackupFieldForExistingRow($current_row, $column, $extracted_columnspec, + $real_null_value, $gis_data_types, $column_name_appendix); } else { // (we are inserting) // display default values - if (! isset($field['Default'])) { - $field['Default'] = ''; - $real_null_value = true; - $data = ''; - } else { - $data = $field['Default']; - } - if ($field['True_Type'] == 'bit') { - $special_chars = PMA_convertBitDefaultValue($field['Default']); - } else { - $special_chars = htmlspecialchars($field['Default']); - } - $backup_field = ''; - $special_chars_encoded = PMA_duplicateFirstNewline($special_chars); - // this will select the UNHEX function while inserting - if (($field['is_binary'] || ($field['is_blob'] && ! $cfg['ProtectBinary'])) - && (isset($_SESSION['tmp_user_values']['display_binary_as_hex']) - && $_SESSION['tmp_user_values']['display_binary_as_hex']) - && $cfg['ShowFunctionFields'] - ) { - $field['display_binary_as_hex'] = true; - } + list($real_null_value, $data, $special_chars, $backup_field, $special_chars_encoded) + = PMA_getSpecialCharsAndBackupFieldForInsertingMode($column, $real_null_value); } - $idindex = ($o_rows * $fields_cnt) + $i + 1; + $idindex = ($o_rows * $columns_cnt) + $i + 1; $tabindex = $idindex; // Get a list of data types that are not yet supported. @@ -600,554 +324,70 @@ foreach ($rows as $row_id => $vrow) { // The function column // ------------------- - // We don't want binary data to be destroyed - // Note: from the MySQL manual: "BINARY doesn't affect how the column is - // stored or retrieved" so it does not mean that the contents is - // binary if ($cfg['ShowFunctionFields']) { - if (($cfg['ProtectBinary'] && $field['is_blob'] && !$is_upload) - || ($cfg['ProtectBinary'] == 'all' && $field['is_binary']) - || ($cfg['ProtectBinary'] == 'noblob' && !$field['is_blob']) - ) { - echo ' ' . "\n"; - } elseif (strstr($field['True_Type'], 'enum') - || strstr($field['True_Type'], 'set') - || in_array($field['pma_type'], $no_support_types) - ) { - echo ' ' . "\n"; - } else { - ?> - - ' . "\n"; - if ($field['Null'] == 'YES') { - echo ' ' . "\n"; - - echo ' '; - - // nullify_code is needed by the js nullify() function - if (strstr($field['True_Type'], 'enum')) { - if (strlen($field['Type']) > 20) { - $nullify_code = '1'; - } else { - $nullify_code = '2'; - } - } elseif (strstr($field['True_Type'], 'set')) { - $nullify_code = '3'; - } elseif ($foreigners && isset($foreigners[$field['Field']]) && $foreignData['foreign_link'] == false) { - // foreign key in a drop-down - $nullify_code = '4'; - } elseif ($foreigners && isset($foreigners[$field['Field']]) && $foreignData['foreign_link'] == true) { - // foreign key with a browsing icon - $nullify_code = '6'; - } else { - $nullify_code = '5'; - } - // to be able to generate calls to nullify() in jQuery - echo ''; - echo ''; - echo ''; - } - echo ' ' . "\n"; + $foreignData = PMA_getForeignData($foreigners, $column['Field'], false, '', ''); + $html_output .= PMA_getNullColumn($column, $column_name_appendix, $real_null_value, + $tabindex, $tabindex_for_null, $idindex, $vkey, $foreigners, $foreignData); // The value column (depends on type) // ---------------- // See bug #1667887 for the reason why we don't use the maxlength // HTML attribute - - echo ' - - - \n"; - echo ' ' + . ''; - // show dropdown or radio depend on length - if (strlen($field['Type']) > 20) { - ?> - - '; - echo '' . "\n"; - $j++; - } // end for - } // end else - } elseif ($field['pma_type'] == 'set') { - if (! isset($table_fields[$i]['values'])) { - $table_fields[$i]['values'] = array(); - foreach ($extracted_columnspec['enum_set_values'] as $val) { - $table_fields[$i]['values'][] = array( - 'plain' => $val, - 'html' => htmlspecialchars($val), - ); - } - $table_fields[$i]['select_size'] = min(4, count($table_fields[$i]['values'])); - } - $field_set_values = $table_fields[$i]['values']; - $select_size = $table_fields[$i]['select_size']; - - $vset = array_flip(explode(',', $data)); - echo $backup_field . "\n"; - ?> - - - - - - - - - - tabindex="" - id="field__3" /> - '; - echo ' '; - - // find maximum upload size, based on field type - /** - * @todo with functions this is not so easy, as you can basically - * process any data with function like MD5 - */ - $max_field_sizes = array( - 'tinyblob' => '256', - 'blob' => '65536', - 'mediumblob' => '16777216', - 'longblob' => '4294967296'); // yeah, really - - $this_field_max_size = $max_upload_size; // from PHP max - if ($this_field_max_size > $max_field_sizes[$field['pma_type']]) { - $this_field_max_size = $max_field_sizes[$field['pma_type']]; - } - echo PMA_getFormattedMaximumUploadSize($this_field_max_size) . "\n"; - // do not generate here the MAX_FILE_SIZE, because we should - // put only one in the form to accommodate the biggest field - if ($this_field_max_size > $biggest_max_file_size) { - $biggest_max_file_size = $this_field_max_size; - } - } - - if (!empty($cfg['UploadDir'])) { - $files = PMA_getFileSelectOptions(PMA_userDir($cfg['UploadDir'])); - if ($files === false) { - echo ' ' . __('Error') . '
' . "\n"; - echo ' ' . __('The directory you set for upload work cannot be reached') . "\n"; - } elseif (!empty($files)) { - echo "
\n"; - echo ' ' . __('Or') . '' . ' ' . __('web server upload directory') . ':
' . "\n"; - echo ' ' . "\n"; - } - } // end if (web-server upload directory) - // end elseif (binary or blob) - } elseif (! in_array($field['pma_type'], $no_support_types)) { - // ignore this column to avoid changing it - if ($field['is_char']) { - $fieldsize = $extracted_columnspec['spec_in_brackets']; - if ($fieldsize > $cfg['MaxSizeForInputField']) { - /** - * This case happens for CHAR or VARCHAR columns which have - * a size larger than the maximum size for input field. - */ - $cfg['CharEditing'] = 'textarea'; - } - } else { - /** - * This case happens for example for INT or DATE columns; - * in these situations, the value returned in $field['len'] - * seems appropriate. - */ - $fieldsize = $field['len']; - } - $fieldsize = min(max($fieldsize, $cfg['MinSizeForInputField']), $cfg['MaxSizeForInputField']); - echo $backup_field . "\n"; - if ($field['is_char'] - && ($cfg['CharEditing'] == 'textarea' - || strpos($data, "\n") !== false) - ) { - echo "\n"; - $cfg['CharEditing'] = $default_char_editing; - ?> - - - - tabindex="" - id="field__3" /> - - - - - - - - - $field['Field_title'], - 'value' => $data_val, - ); - if ($field['pma_type'] != 'geometry') { - $_url_params = $_url_params + array('gis_data[gis_type]' => strtoupper($field['pma_type'])); - } - $edit_str = PMA_getIcon('b_edit.png', __('Edit/Insert')); - echo ''; - echo PMA_linkOrButton('#', $edit_str, array(), false, false, '_blank'); - echo ''; - } - ?> - - -
' . __('Type') . '' . __('Function') . '
- -
class="center"> - - - - ' + . $column['Field_title'] + . '' + . '' + . '' . $column['pma_type'] . '' + . '' . __('Binary') . '-- - - ' . "\n"; + $html_output .= ' ' . "\n"; // Will be used by js/tbl_change.js to set the default value // for the "Continue insertion" feature - echo '' . $special_chars . ''; - if ($foreignData['foreign_link'] == true) { - echo $backup_field . "\n"; - ?> - - - tabindex="" - id="field__3" - value="" /> - - - - - -  
- - - - - 32000) { - echo " ' . __('Because of its length,
this column might not be editable'); - } - } elseif ($field['pma_type'] == 'enum') { - if (! isset($table_fields[$i]['values'])) { - $table_fields[$i]['values'] = array(); - foreach ($extracted_columnspec['enum_set_values'] as $val) { - // Removes automatic MySQL escape format - $val = str_replace('\'\'', '\'', str_replace('\\\\', '\\', $val)); - $table_fields[$i]['values'][] = array( - 'plain' => $val, - 'html' => htmlspecialchars($val), - ); - } - } - $field_enum_values = $table_fields[$i]['values']; - ?> - - - ' . $special_chars . ''; + + $html_output .= PMA_getValueColumn( + $column, $backup_field, $column_name_appendix, $unnullify_trigger,$tabindex, + $tabindex_for_value, $idindex, $data,$special_chars, $foreignData, $odd_row, + $paramTableDbArray,$rownumber_param, $titles, $text_dir, $special_chars_encoded, + $vkey,$is_upload, $biggest_max_file_size, $default_char_editing, + $no_support_types, $gis_data_types, $extracted_columnspec); + + $html_output .= '

'; + $html_output .= ' ' + . '
'; } // end foreach on multi-edit -?> -
-
-
- - - - - - - - - - -
- - ' + . '' + . '
'; if (! isset($after_insert)) { $after_insert = 'back'; } -?> -
-        - - -
- - - - -
-
- 0) { - echo ' ' . PMA_generateHiddenMaxFileSize($biggest_max_file_size) . "\n"; - } ?> - - 0) { + $html_output .= ' ' . PMA_generateHiddenMaxFileSize($biggest_max_file_size) . "\n"; +} +$html_output .= ''; +// end Insert/Edit form + if ($insert_mode) { -?> - -
- - - - - $where_clause) { - echo ''. "\n"; - } - } - $tmp = '' . "\n"; - echo "\n" . sprintf(__('Continue insertion with %s rows'), $tmp); - unset($tmp); - echo '
' . "\n"; + //Continue insertion form + $html_output .= PMA_getContinueInsertionForm($table, $db, $where_clause_array, $err_url); } - +echo $html_output; /** * Displays the footer */ require 'libraries/footer.inc.php'; + ?> diff --git a/tbl_relation.php b/tbl_relation.php index b990df4d14..06df554797 100644 --- a/tbl_relation.php +++ b/tbl_relation.php @@ -130,7 +130,7 @@ if ($cfgRelation['displaywork']) { } // will be used in the logic for internal relations and foreign keys: -$me_fields_name = isset($_REQUEST['fields_name']) +$multi_edit_columns_name = isset($_REQUEST['fields_name']) ? $_REQUEST['fields_name'] : null; @@ -141,7 +141,7 @@ if (isset($destination) && $cfgRelation['relwork']) { $upd_query = false; // Map the fieldname's md5 back to its real name - $master_field = $me_fields_name[$master_field_md5]; + $master_field = $multi_edit_columns_name[$master_field_md5]; if (! empty($foreign_string)) { $foreign_string = trim($foreign_string, '`'); @@ -189,7 +189,7 @@ if (isset($_REQUEST['destination_foreign'])) { foreach ($_REQUEST['destination_foreign'] as $master_field_md5 => $foreign_string) { // Map the fieldname's md5 back to it's real name - $master_field = $me_fields_name[$master_field_md5]; + $master_field = $multi_edit_columns_name[$master_field_md5]; if (! empty($foreign_string)) { list($foreign_db, $foreign_table, $foreign_field) = PMA_backquote_split($foreign_string); diff --git a/tbl_replace.php b/tbl_replace.php index e24eeeddcc..87760ee1b9 100644 --- a/tbl_replace.php +++ b/tbl_replace.php @@ -16,6 +16,11 @@ */ require_once 'libraries/common.inc.php'; +/** + * functions implementation for this script + */ +require_once 'libraries/insert_edit.lib.php'; + // Check parameters PMA_checkParameters(array('db', 'table', 'goto')); @@ -30,96 +35,34 @@ $GLOBALS['js_include'][] = 'makegrid.js'; // Needed for generation of Inline Edit anchors $GLOBALS['js_include'][] = 'sql.js'; -if (isset($_REQUEST['insert_rows']) - && is_numeric($_REQUEST['insert_rows']) - && $_REQUEST['insert_rows'] != $cfg['InsertRows'] -) { - $cfg['InsertRows'] = $_REQUEST['insert_rows']; - $GLOBALS['js_include'][] = 'tbl_change.js'; - include_once 'libraries/header.inc.php'; - include 'tbl_change.php'; - exit; -} +// check whether insert row moode, if so include tbl_change.php +PMA_isInsertRow(); if (isset($_REQUEST['after_insert']) && in_array($_REQUEST['after_insert'], array('new_insert', 'same_insert', 'edit_next')) ) { $url_params['after_insert'] = $_REQUEST['after_insert']; - //$GLOBALS['goto'] = 'tbl_change.php'; - $goto_include = 'tbl_change.php'; - if (isset($_REQUEST['where_clause'])) { - if ($_REQUEST['after_insert'] == 'same_insert') { - foreach ($_REQUEST['where_clause'] as $one_where_clause) { + foreach ($_REQUEST['where_clause'] as $one_where_clause) { + if ($_REQUEST['after_insert'] == 'same_insert') { $url_params['where_clause'][] = $one_where_clause; - } - } elseif ($_REQUEST['after_insert'] == 'edit_next') { - foreach ($_REQUEST['where_clause'] as $one_where_clause) { - $local_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table']) - . ' WHERE ' . str_replace('` =', '` >', $one_where_clause) - . ' LIMIT 1;'; - $res = PMA_DBI_query($local_query); - $row = PMA_DBI_fetch_row($res); - $meta = PMA_DBI_get_fields_meta($res); - // must find a unique condition based on unique key, - // not a combination of all fields - list($unique_condition, $clause_is_unique) = PMA_getUniqueCondition($res, count($meta), $meta, $row, true); - if (! empty($unique_condition)) { - $_SESSION['edit_next'] = $unique_condition; - } - unset($unique_condition, $clause_is_unique); + } elseif ($_REQUEST['after_insert'] == 'edit_next') { + PMA_setSessionForEditNext($one_where_clause); } } } -} elseif (! empty($GLOBALS['goto'])) { - if (! preg_match('@^[a-z_]+\.php$@', $GLOBALS['goto'])) { - // this should NOT happen - //$GLOBALS['goto'] = false; - $goto_include = false; - } else { - $goto_include = $GLOBALS['goto']; - } - if ($GLOBALS['goto'] == 'db_sql.php' && strlen($GLOBALS['table'])) { - $GLOBALS['table'] = ''; - } -} - -if (! $goto_include) { - if (! strlen($GLOBALS['table'])) { - $goto_include = 'db_sql.php'; - } else { - $goto_include = 'tbl_sql.php'; - } } +//get $goto_include for different cases +$goto_include = PMA_getGotoInclude($goto_include); // Defines the url to return in case of failure of the query -if (isset($_REQUEST['err_url'])) { - $err_url = $_REQUEST['err_url']; -} else { - $err_url = 'tbl_change.php' . PMA_generate_common_url($url_params); -} +$err_url = PMA_getErrorUrl($url_params); /** * Prepares the update/insert of a row */ -if (isset($_REQUEST['where_clause'])) { - // we were editing something => use the WHERE clause - $loop_array = (is_array($_REQUEST['where_clause']) ? $_REQUEST['where_clause'] : array($_REQUEST['where_clause'])); - $using_key = true; - $is_insert = $_REQUEST['submit_type'] == 'insert' - || $_REQUEST['submit_type'] == 'showinsert' - || $_REQUEST['submit_type'] == 'insertignore'; - $is_insertignore = $_REQUEST['submit_type'] == 'insertignore'; -} else { - // new row => use indexes - $loop_array = array(); - foreach ($_REQUEST['fields']['multi_edit'] as $key => $dummy) { - $loop_array[] = $key; - } - $using_key = false; - $is_insert = true; - $is_insertignore = false; -} +list($loop_array, $using_key, $is_insert, $is_insertignore) + = PMA_getParamsForUpdateOrInsert(); $query = array(); $value_sets = array(); @@ -179,35 +122,35 @@ foreach ($loop_array as $rownumber => $where_clause) { $query_values = array(); // Map multi-edit keys to single-level arrays, dependent on how we got the fields - $me_fields + $multi_edit_colummns = isset($_REQUEST['fields']['multi_edit'][$rownumber]) ? $_REQUEST['fields']['multi_edit'][$rownumber] : array(); - $me_fields_name + $multi_edit_columns_name = isset($_REQUEST['fields_name']['multi_edit'][$rownumber]) ? $_REQUEST['fields_name']['multi_edit'][$rownumber] : null; - $me_fields_prev + $multi_edit_columns_prev = isset($_REQUEST['fields_prev']['multi_edit'][$rownumber]) ? $_REQUEST['fields_prev']['multi_edit'][$rownumber] : null; - $me_funcs + $multi_edit_funcs = isset($_REQUEST['funcs']['multi_edit'][$rownumber]) ? $_REQUEST['funcs']['multi_edit'][$rownumber] : null; - $me_fields_type + $multi_edit_columns_type = isset($_REQUEST['fields_type']['multi_edit'][$rownumber]) ? $_REQUEST['fields_type']['multi_edit'][$rownumber] : null; - $me_fields_null + $multi_edit_columns_null = isset($_REQUEST['fields_null']['multi_edit'][$rownumber]) ? $_REQUEST['fields_null']['multi_edit'][$rownumber] : null; - $me_fields_null_prev + $multi_edit_columns_null_prev = isset($_REQUEST['fields_null_prev']['multi_edit'][$rownumber]) ? $_REQUEST['fields_null_prev']['multi_edit'][$rownumber] : null; - $me_auto_increment + $multi_edit_auto_increment = isset($_REQUEST['auto_increment']['multi_edit'][$rownumber]) ? $_REQUEST['auto_increment']['multi_edit'][$rownumber] : null; @@ -215,49 +158,49 @@ foreach ($loop_array as $rownumber => $where_clause) { // Fetch the current values of a row to use in case we have a protected field // @todo possibly move to ./libraries/tbl_replace_fields.inc.php if ($is_insert - && $using_key && isset($me_fields_type) - && is_array($me_fields_type) && isset($where_clause) + && $using_key && isset($multi_edit_columns_type) + && is_array($multi_edit_columns_type) && isset($where_clause) ) { $prot_row = PMA_DBI_fetch_single_row('SELECT * FROM ' . PMA_backquote($table) . ' WHERE ' . $where_clause . ';'); } // When a select field is nullified, it's not present in $_REQUEST - // so initialize it; this way, the foreach($me_fields) will process it - foreach ($me_fields_name as $key => $val) { - if (! isset($me_fields[$key])) { - $me_fields[$key] = ''; + // so initialize it; this way, the foreach($multi_edit_colummns) will process it + foreach ($multi_edit_columns_name as $key => $val) { + if (! isset($multi_edit_colummns[$key])) { + $multi_edit_colummns[$key] = ''; } } - // Iterate in the order of $me_fields_name, not $me_fields, to avoid problems + // Iterate in the order of $multi_edit_columns_name, not $multi_edit_colummns, to avoid problems // when inserting multiple entries - foreach ($me_fields_name as $key => $field_name) { - $val = $me_fields[$key]; + foreach ($multi_edit_columns_name as $key => $colummn_name) { + $val = $multi_edit_colummns[$key]; - // Note: $key is an md5 of the fieldname. The actual fieldname is available in $me_fields_name[$key] + // Note: $key is an md5 of the fieldname. The actual fieldname is available in $multi_edit_columns_name[$key] include 'libraries/tbl_replace_fields.inc.php'; - if (empty($me_funcs[$key])) { + if (empty($multi_edit_funcs[$key])) { $cur_value = $val; - } elseif ('UUID' === $me_funcs[$key]) { + } elseif ('UUID' === $multi_edit_funcs[$key]) { /* This way user will know what UUID new row has */ $uuid = PMA_DBI_fetch_value('SELECT UUID()'); $cur_value = "'" . $uuid . "'"; - } elseif ((in_array($me_funcs[$key], $gis_from_text_functions) + } elseif ((in_array($multi_edit_funcs[$key], $gis_from_text_functions) && substr($val, 0, 3) == "'''") - || in_array($me_funcs[$key], $gis_from_wkb_functions) + || in_array($multi_edit_funcs[$key], $gis_from_wkb_functions) ) { // Remove enclosing apostrophes $val = substr($val, 1, strlen($val) - 2); // Remove escaping apostrophes $val = str_replace("''", "'", $val); - $cur_value = $me_funcs[$key] . '(' . $val . ')'; - } elseif (! in_array($me_funcs[$key], $func_no_param) - || ($val != "''" && in_array($me_funcs[$key], $func_optional_param))) { - $cur_value = $me_funcs[$key] . '(' . $val . ')'; + $cur_value = $multi_edit_funcs[$key] . '(' . $val . ')'; + } elseif (! in_array($multi_edit_funcs[$key], $func_no_param) + || ($val != "''" && in_array($multi_edit_funcs[$key], $func_optional_param))) { + $cur_value = $multi_edit_funcs[$key] . '(' . $val . ')'; } else { - $cur_value = $me_funcs[$key] . '()'; + $cur_value = $multi_edit_funcs[$key] . '()'; } // i n s e r t @@ -267,32 +210,32 @@ foreach ($loop_array as $rownumber => $where_clause) { $query_values[] = $cur_value; // first inserted row so prepare the list of fields if (empty($value_sets)) { - $query_fields[] = PMA_backquote($me_fields_name[$key]); + $query_fields[] = PMA_backquote($multi_edit_columns_name[$key]); } } // u p d a t e - } elseif (!empty($me_fields_null_prev[$key]) - && ! isset($me_fields_null[$key])) { + } elseif (!empty($multi_edit_columns_null_prev[$key]) + && ! isset($multi_edit_columns_null[$key])) { // field had the null checkbox before the update // field no longer has the null checkbox - $query_values[] = PMA_backquote($me_fields_name[$key]) . ' = ' . $cur_value; - } elseif (empty($me_funcs[$key]) - && isset($me_fields_prev[$key]) - && ("'" . PMA_sqlAddSlashes($me_fields_prev[$key]) . "'" == $val)) { + $query_values[] = PMA_backquote($multi_edit_columns_name[$key]) . ' = ' . $cur_value; + } elseif (empty($multi_edit_funcs[$key]) + && isset($multi_edit_columns_prev[$key]) + && ("'" . PMA_sqlAddSlashes($multi_edit_columns_prev[$key]) . "'" == $val)) { // No change for this column and no MySQL function is used -> next column continue; } elseif (! empty($val)) { // avoid setting a field to NULL when it's already NULL // (field had the null checkbox before the update // field still has the null checkbox) - if (empty($me_fields_null_prev[$key]) - || empty($me_fields_null[$key]) + if (empty($multi_edit_columns_null_prev[$key]) + || empty($multi_edit_columns_null[$key]) ) { - $query_values[] = PMA_backquote($me_fields_name[$key]) . ' = ' . $cur_value; + $query_values[] = PMA_backquote($multi_edit_columns_name[$key]) . ' = ' . $cur_value; } } - } // end foreach ($me_fields as $key => $val) + } // end foreach ($multi_edit_colummns as $key => $val) if (count($query_values) > 0) { if ($is_insert) { @@ -306,23 +249,15 @@ foreach ($loop_array as $rownumber => $where_clause) { } } } // end foreach ($loop_array as $where_clause) -unset($me_fields_name, $me_fields_prev, $me_funcs, $me_fields_type, $me_fields_null, $me_fields_null_prev, - $me_auto_increment, $cur_value, $key, $val, $loop_array, $where_clause, $using_key, - $func_no_param); +unset($multi_edit_columns_name, $multi_edit_columns_prev, $multi_edit_funcs, + $multi_edit_columns_type, $multi_edit_columns_null, $func_no_param, + $multi_edit_auto_increment, $cur_value, $key, $val, $loop_array, $where_clause, + $using_key, $multi_edit_columns_null_prev); // Builds the sql query if ($is_insert && count($value_sets) > 0) { - if ($is_insertignore) { - $insert_command = 'INSERT IGNORE '; - } else { - $insert_command = 'INSERT '; - } - $query[] = $insert_command . 'INTO ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table']) - . ' (' . implode(', ', $query_fields) . ') VALUES (' . implode('), (', $value_sets) . ')'; - unset($insert_command); - - unset($query_fields); + $query = PMA_buildSqlQuery($is_insertignore, $query_fields, $value_sets); } elseif (empty($query)) { // No change -> move back to the calling script // @@ -335,71 +270,14 @@ if ($is_insert && count($value_sets) > 0) { include '' . PMA_securePath($goto_include); exit; } -unset($me_fields, $is_insertignore); +unset($multi_edit_colummns, $is_insertignore); /** * Executes the sql query and get the result, then move back to the calling * page */ -if (! empty($GLOBALS['sql_query'])) { - $url_params['sql_query'] = $GLOBALS['sql_query']; - $return_to_sql_query = $GLOBALS['sql_query']; -} -$GLOBALS['sql_query'] = implode('; ', $query) . ';'; -// to ensure that the query is displayed in case of -// "insert as new row" and then "insert another new row" -$GLOBALS['display_query'] = $GLOBALS['sql_query']; - -$total_affected_rows = 0; -$last_messages = array(); -$warning_messages = array(); -$error_messages = array(); - -foreach ($query as $single_query) { - if ($_REQUEST['submit_type'] == 'showinsert') { - $last_messages[] = PMA_Message::notice(__('Showing SQL query')); - continue; - } - if ($GLOBALS['cfg']['IgnoreMultiSubmitErrors']) { - $result = PMA_DBI_try_query($single_query); - } else { - $result = PMA_DBI_query($single_query); - } - - if (! $result) { - $error_messages[] = PMA_Message::sanitize(PMA_DBI_getError()); - } else { - // The next line contains a real assignment, it's not a typo - if ($tmp = @PMA_DBI_affected_rows()) { - $total_affected_rows += $tmp; - } - unset($tmp); - - $insert_id = PMA_DBI_insert_id(); - if ($insert_id != 0) { - // insert_id is id of FIRST record inserted in one insert, so if we - // inserted multiple rows, we had to increment this - - if ($total_affected_rows > 0) { - $insert_id = $insert_id + $total_affected_rows - 1; - } - $last_message = PMA_Message::notice(__('Inserted row id: %1$d')); - $last_message->addParam($insert_id); - $last_messages[] = $last_message; - } - PMA_DBI_free_result($result); - } // end if - - foreach (PMA_DBI_get_warnings() as $warning) { - $warning_messages[] - = PMA_Message::sanitize( - $warning['Level'] . ': #' . $warning['Code'] . ' ' . $warning['Message'] - ); - } - - unset($result); -} -unset($single_query, $query); +list ($url_params, $total_affected_rows, $last_messages, $warning_messages, + $error_messages) = PMA_executeSqlQuery($url_params, $query); if ($is_insert && count($value_sets) > 0) { $message = PMA_Message::inserted_rows($total_affected_rows);