protect binary contents in cookies
[phpmyadmin/crack.git] / tbl_addfield.php3
blob08d6fa432b1bc86f22e9c5e575d4f6c20f34c40e
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
6 /**
7 * Get some core libraries
8 */
9 require('./libraries/grab_globals.lib.php3');
10 $js_to_run = 'functions.js';
11 require('./header.inc.php3');
14 /**
15 * Defines the url to return to in case of error in a sql statement
17 $err_url = 'tbl_properties.php3?' . PMA_generate_common_url($db, $table);
20 /**
21 * The form used to define the field to add has been submitted
23 $abort = false;
24 if (isset($submit)) {
25 $query = '';
27 // Transforms the radio button field_key into 3 arrays
28 $field_cnt = count($field_name);
29 for ($i = 0; $i < $field_cnt; ++$i) {
30 if (isset(${'field_key_' . $i})) {
31 if (${'field_key_' . $i} == 'primary_' . $i) {
32 $field_primary[] = $i;
34 if (${'field_key_' . $i} == 'index_' . $i) {
35 $field_index[] = $i;
37 if (${'field_key_' . $i} == 'unique_' . $i) {
38 $field_unique[] = $i;
40 } // end if
41 } // end for
42 // Builds the field creation statement and alters the table
43 for ($i = 0; $i < $field_cnt; ++$i) {
44 if (empty($field_name[$i])) {
45 continue;
47 if (PMA_MYSQL_INT_VERSION < 32306) {
48 PMA_checkReservedWords($field_name[$i], $err_url);
51 $query .= PMA_backquote($field_name[$i]) . ' ' . $field_type[$i];
52 if ($field_length[$i] != ''
53 && !eregi('^(DATE|DATETIME|TIME|TINYBLOB|TINYTEXT|BLOB|TEXT|MEDIUMBLOB|MEDIUMTEXT|LONGBLOB|LONGTEXT)$', $field_type[$i])) {
54 $query .= '(' . $field_length[$i] . ')';
56 if ($field_attribute[$i] != '') {
57 $query .= ' ' . $field_attribute[$i];
58 } else if (PMA_MYSQL_INT_VERSION >= 40100 && $field_charset[$i] != '') {
59 $query .= ' CHARACTER SET ' . $field_charset[$i];
61 if ($field_default[$i] != '') {
62 if (strtoupper($field_default[$i]) == 'NULL') {
63 $query .= ' DEFAULT NULL';
64 } else {
65 $query .= ' DEFAULT \'' . PMA_sqlAddslashes($field_default[$i]) . '\'';
68 if ($field_null[$i] != '') {
69 $query .= ' ' . $field_null[$i];
71 if ($field_extra[$i] != '') {
72 $query .= ' ' . $field_extra[$i];
73 // An auto_increment field must be use as a primary key
74 if ($field_extra[$i] == 'AUTO_INCREMENT' && isset($field_primary)) {
75 $primary_cnt = count($field_primary);
76 for ($j = 0; $j < $primary_cnt && $field_primary[$j] != $i; $j++) {
77 // void
78 } // end for
79 if ($field_primary[$j] == $i) {
80 $query .= ' PRIMARY KEY';
81 unset($field_primary[$j]);
82 } // end if
83 } // end if (auto_increment)
86 if ($after_field != '--end--') {
87 // Only the first field can be added somewhere else than at the end
88 if ($i == 0) {
89 if ($after_field == '--first--') {
90 $query .= ' FIRST';
91 } else {
92 $query .= ' AFTER ' . PMA_backquote(urldecode($after_field));
94 } else {
95 $query .= ' AFTER ' . PMA_backquote($field_name[$i-1]);
98 $query .= ', ADD ';
99 } // end for
100 $query = ereg_replace(', ADD $', '', $query);
102 // To allow replication, we first select the db to use and then run queries
103 // on this db.
104 $sql_query = 'USE ' . PMA_backquote($db);
105 $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url);
106 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD ' . $query;
107 $error_create = false;
108 $result = PMA_mysql_query($sql_query) or $error_create = true;
110 if ($error_create == false) {
112 $sql_query_cpy = $sql_query . ';';
114 // Builds the primary keys statements and updates the table
115 $primary = '';
116 if (isset($field_primary)) {
117 $primary_cnt = count($field_primary);
118 for ($i = 0; $i < $primary_cnt; $i++) {
119 $j = $field_primary[$i];
120 if (!empty($field_name[$j])) {
121 $primary .= PMA_backquote($field_name[$j]) . ', ';
123 } // end for
124 $primary = ereg_replace(', $', '', $primary);
125 if (!empty($primary)) {
126 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD PRIMARY KEY (' . $primary . ')';
127 $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url);
128 $sql_query_cpy .= "\n" . $sql_query . ';';
130 } // end if
132 // Builds the indexes statements and updates the table
133 $index = '';
134 if (isset($field_index)) {
135 $index_cnt = count($field_index);
136 for ($i = 0; $i < $index_cnt; $i++) {
137 $j = $field_index[$i];
138 if (!empty($field_name[$j])) {
139 $index .= PMA_backquote($field_name[$j]) . ', ';
141 } // end for
142 $index = ereg_replace(', $', '', $index);
143 if (!empty($index)) {
144 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD INDEX (' . $index . ')';
145 $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url);
146 $sql_query_cpy .= "\n" . $sql_query . ';';
148 } // end if
150 // Builds the uniques statements and updates the table
151 $unique = '';
152 if (isset($field_unique)) {
153 $unique_cnt = count($field_unique);
154 for ($i = 0; $i < $unique_cnt; $i++) {
155 $j = $field_unique[$i];
156 if (!empty($field_name[$j])) {
157 $unique .= PMA_backquote($field_name[$j]) . ', ';
159 } // end for
160 $unique = ereg_replace(', $', '', $unique);
161 if (!empty($unique)) {
162 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD UNIQUE (' . $unique . ')';
163 $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url);
164 $sql_query_cpy .= "\n" . $sql_query . ';';
166 } // end if
169 // Builds the fulltext statements and updates the table
170 $fulltext = '';
171 if (PMA_MYSQL_INT_VERSION >= 32323 && isset($field_fulltext)) {
172 $fulltext_cnt = count($field_fulltext);
173 for ($i = 0; $i < $fulltext_cnt; $i++) {
174 $j = $field_fulltext[$i];
175 $fulltext .= PMA_backquote($field_name[$j]) . ', ';
176 } // end for
177 $fulltext = ereg_replace(', $', '', $fulltext);
178 if (!empty($fulltext)) {
179 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD FULLTEXT (' . $fulltext . ')';
180 $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url);
181 $sql_query_cpy .= "\n" . $sql_query . ';';
183 } // end if
185 // garvin: If comments were sent, enable relation stuff
186 require('./libraries/relation.lib.php3');
187 require('./libraries/transformations.lib.php3');
189 $cfgRelation = PMA_getRelationsParam();
191 // garvin: Update comment table, if a comment was set.
192 if (isset($field_comments) && is_array($field_comments) && $cfgRelation['commwork']) {
193 @reset($field_comments);
194 while(list($fieldindex, $fieldcomment) = each($field_comments)) {
195 PMA_setComment($db, $table, $field_name[$fieldindex], $fieldcomment);
199 // garvin: Update comment table for mime types [MIME]
200 if (isset($field_mimetype) && is_array($field_mimetype) && $cfgRelation['commwork'] && $cfgRelation['mimework'] && $cfg['BrowseMIME']) {
201 @reset($field_mimetype);
202 while(list($fieldindex, $mimetype) = each($field_mimetype)) {
203 PMA_setMIME($db, $table, $field_name[$fieldindex], $mimetype, $field_transformation[$fieldindex], $field_transformation_options[$fieldindex]);
207 // Go back to the structure sub-page
208 $sql_query = $sql_query_cpy;
209 unset($sql_query_cpy);
210 $message = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenAltered;
211 include('./tbl_properties_structure.php3');
212 exit();
213 } else {
214 PMA_mysqlDie('', '', '', $err_url, FALSE);
215 // garvin: An error happened while inserting/updating a table definition.
216 // to prevent total loss of that data, we embed the form once again.
217 // The variable $regenerate will be used to restore data in tbl_properties.inc.php3
218 $num_fields = $orig_num_fields;
219 if (isset($orig_after_field)) {
220 $after_field = $orig_after_field;
222 $regenerate = true;
224 } // end do alter table
227 * Displays the form used to define the new field
229 if ($abort == FALSE) {
230 $action = 'tbl_addfield.php3';
231 include('./tbl_properties.inc.php3');
233 // Diplays the footer
234 echo "\n";
235 include('./footer.inc.php3');