path disclosure
[phpmyadmin/crack.git] / tbl_addfield.php3
blobd0772b92176aeb05d40e4a40ad29685fad043e78
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');
13 // Check parameters
14 PMA_checkParameters(array('db', 'table'));
17 /**
18 * Defines the url to return to in case of error in a sql statement
20 $err_url = 'tbl_properties.php3?' . PMA_generate_common_url($db, $table);
23 /**
24 * The form used to define the field to add has been submitted
26 $abort = false;
27 if (isset($submit)) {
28 $query = '';
30 // Transforms the radio button field_key into 3 arrays
31 $field_cnt = count($field_name);
32 for ($i = 0; $i < $field_cnt; ++$i) {
33 if (isset(${'field_key_' . $i})) {
34 if (${'field_key_' . $i} == 'primary_' . $i) {
35 $field_primary[] = $i;
37 if (${'field_key_' . $i} == 'index_' . $i) {
38 $field_index[] = $i;
40 if (${'field_key_' . $i} == 'unique_' . $i) {
41 $field_unique[] = $i;
43 } // end if
44 } // end for
45 // Builds the field creation statement and alters the table
46 for ($i = 0; $i < $field_cnt; ++$i) {
47 if (empty($field_name[$i])) {
48 continue;
50 if (PMA_MYSQL_INT_VERSION < 32306) {
51 PMA_checkReservedWords($field_name[$i], $err_url);
54 $query .= PMA_backquote($field_name[$i]) . ' ' . $field_type[$i];
55 if ($field_length[$i] != ''
56 && !eregi('^(DATE|DATETIME|TIME|TINYBLOB|TINYTEXT|BLOB|TEXT|MEDIUMBLOB|MEDIUMTEXT|LONGBLOB|LONGTEXT)$', $field_type[$i])) {
57 $query .= '(' . $field_length[$i] . ')';
59 if ($field_attribute[$i] != '') {
60 $query .= ' ' . $field_attribute[$i];
61 } else if (PMA_MYSQL_INT_VERSION >= 40100 && $field_charset[$i] != '') {
62 $query .= ' CHARACTER SET ' . $field_charset[$i];
64 if ($field_default[$i] != '') {
65 if (strtoupper($field_default[$i]) == 'NULL') {
66 $query .= ' DEFAULT NULL';
67 } else {
68 $query .= ' DEFAULT \'' . PMA_sqlAddslashes($field_default[$i]) . '\'';
71 if ($field_null[$i] != '') {
72 $query .= ' ' . $field_null[$i];
74 if ($field_extra[$i] != '') {
75 $query .= ' ' . $field_extra[$i];
76 // An auto_increment field must be use as a primary key
77 if ($field_extra[$i] == 'AUTO_INCREMENT' && isset($field_primary)) {
78 $primary_cnt = count($field_primary);
79 for ($j = 0; $j < $primary_cnt && $field_primary[$j] != $i; $j++) {
80 // void
81 } // end for
82 if ($field_primary[$j] == $i) {
83 $query .= ' PRIMARY KEY';
84 unset($field_primary[$j]);
85 } // end if
86 } // end if (auto_increment)
89 if ($after_field != '--end--') {
90 // Only the first field can be added somewhere else than at the end
91 if ($i == 0) {
92 if ($after_field == '--first--') {
93 $query .= ' FIRST';
94 } else {
95 $query .= ' AFTER ' . PMA_backquote(urldecode($after_field));
97 } else {
98 $query .= ' AFTER ' . PMA_backquote($field_name[$i-1]);
101 $query .= ', ADD ';
102 } // end for
103 $query = ereg_replace(', ADD $', '', $query);
105 // To allow replication, we first select the db to use and then run queries
106 // on this db.
107 $sql_query = 'USE ' . PMA_backquote($db);
108 $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url);
109 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD ' . $query;
110 $error_create = false;
111 $result = PMA_mysql_query($sql_query) or $error_create = true;
113 if ($error_create == false) {
115 $sql_query_cpy = $sql_query . ';';
117 // Builds the primary keys statements and updates the table
118 $primary = '';
119 if (isset($field_primary)) {
120 $primary_cnt = count($field_primary);
121 for ($i = 0; $i < $primary_cnt; $i++) {
122 $j = $field_primary[$i];
123 if (!empty($field_name[$j])) {
124 $primary .= PMA_backquote($field_name[$j]) . ', ';
126 } // end for
127 $primary = ereg_replace(', $', '', $primary);
128 if (!empty($primary)) {
129 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD PRIMARY KEY (' . $primary . ')';
130 $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url);
131 $sql_query_cpy .= "\n" . $sql_query . ';';
133 } // end if
135 // Builds the indexes statements and updates the table
136 $index = '';
137 if (isset($field_index)) {
138 $index_cnt = count($field_index);
139 for ($i = 0; $i < $index_cnt; $i++) {
140 $j = $field_index[$i];
141 if (!empty($field_name[$j])) {
142 $index .= PMA_backquote($field_name[$j]) . ', ';
144 } // end for
145 $index = ereg_replace(', $', '', $index);
146 if (!empty($index)) {
147 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD INDEX (' . $index . ')';
148 $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url);
149 $sql_query_cpy .= "\n" . $sql_query . ';';
151 } // end if
153 // Builds the uniques statements and updates the table
154 $unique = '';
155 if (isset($field_unique)) {
156 $unique_cnt = count($field_unique);
157 for ($i = 0; $i < $unique_cnt; $i++) {
158 $j = $field_unique[$i];
159 if (!empty($field_name[$j])) {
160 $unique .= PMA_backquote($field_name[$j]) . ', ';
162 } // end for
163 $unique = ereg_replace(', $', '', $unique);
164 if (!empty($unique)) {
165 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD UNIQUE (' . $unique . ')';
166 $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url);
167 $sql_query_cpy .= "\n" . $sql_query . ';';
169 } // end if
172 // Builds the fulltext statements and updates the table
173 $fulltext = '';
174 if (PMA_MYSQL_INT_VERSION >= 32323 && isset($field_fulltext)) {
175 $fulltext_cnt = count($field_fulltext);
176 for ($i = 0; $i < $fulltext_cnt; $i++) {
177 $j = $field_fulltext[$i];
178 $fulltext .= PMA_backquote($field_name[$j]) . ', ';
179 } // end for
180 $fulltext = ereg_replace(', $', '', $fulltext);
181 if (!empty($fulltext)) {
182 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD FULLTEXT (' . $fulltext . ')';
183 $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url);
184 $sql_query_cpy .= "\n" . $sql_query . ';';
186 } // end if
188 // garvin: If comments were sent, enable relation stuff
189 require('./libraries/relation.lib.php3');
190 require('./libraries/transformations.lib.php3');
192 $cfgRelation = PMA_getRelationsParam();
194 // garvin: Update comment table, if a comment was set.
195 if (isset($field_comments) && is_array($field_comments) && $cfgRelation['commwork']) {
196 @reset($field_comments);
197 while(list($fieldindex, $fieldcomment) = each($field_comments)) {
198 PMA_setComment($db, $table, $field_name[$fieldindex], $fieldcomment);
202 // garvin: Update comment table for mime types [MIME]
203 if (isset($field_mimetype) && is_array($field_mimetype) && $cfgRelation['commwork'] && $cfgRelation['mimework'] && $cfg['BrowseMIME']) {
204 @reset($field_mimetype);
205 while(list($fieldindex, $mimetype) = each($field_mimetype)) {
206 PMA_setMIME($db, $table, $field_name[$fieldindex], $mimetype, $field_transformation[$fieldindex], $field_transformation_options[$fieldindex]);
210 // Go back to the structure sub-page
211 $sql_query = $sql_query_cpy;
212 unset($sql_query_cpy);
213 $message = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenAltered;
214 include('./tbl_properties_structure.php3');
215 exit();
216 } else {
217 PMA_mysqlDie('', '', '', $err_url, FALSE);
218 // garvin: An error happened while inserting/updating a table definition.
219 // to prevent total loss of that data, we embed the form once again.
220 // The variable $regenerate will be used to restore data in tbl_properties.inc.php3
221 $num_fields = $orig_num_fields;
222 if (isset($orig_after_field)) {
223 $after_field = $orig_after_field;
225 $regenerate = true;
227 } // end do alter table
230 * Displays the form used to define the new field
232 if ($abort == FALSE) {
233 $action = 'tbl_addfield.php3';
234 include('./tbl_properties.inc.php3');
236 // Diplays the footer
237 echo "\n";
238 include('./footer.inc.php3');