@@ -137,7 +137,9 @@ public function switchAction(ServerRequestInterface $requestInterface, ResponseI
137
137
case "edit_record " ;
138
138
139
139
$ isNew = false ;
140
- if (isSet ($ record_is_new ) && $ record_is_new == "true " ) $ isNew = true ;
140
+ if (isSet ($ httpVars ['record_is_new ' ]) && $ httpVars ['record_is_new ' ] == "true " ) {
141
+ $ isNew = true ;
142
+ }
141
143
$ tableName = $ httpVars ["table_name " ];
142
144
$ pkName = $ httpVars ["pk_name " ];
143
145
$ arrValues = array ();
@@ -147,16 +149,19 @@ public function switchAction(ServerRequestInterface $requestInterface, ResponseI
147
149
$ arrValues [$ newKey ] = $ value ;
148
150
}
149
151
}
152
+ $ autoKey = $ this ->findTableAutoIncrementKey ($ ctx , $ tableName );
150
153
if ($ isNew ) {
151
- $ string = "" ;
154
+ $ values = [] ;
152
155
$ index = 0 ;
153
156
foreach ($ arrValues as $ k =>$ v ) {
154
- // CHECK IF AUTO KEY!!!
155
- $ string .= "' " .addslashes ($ v )."' " ;
156
- if ($ index < count ($ arrValues )-1 ) $ string .=", " ;
157
+ if ($ autoKey !== false && $ k === $ autoKey ){
158
+ $ values [] = 'NULL ' ;
159
+ }else {
160
+ $ values []= "' " .addslashes ($ v )."' " ;
161
+ }
157
162
$ index ++;
158
163
}
159
- $ query = "INSERT INTO $ tableName VALUES ( $ string ) " ;
164
+ $ query = "INSERT INTO ` $ tableName` VALUES (" . implode ( " , " , $ values ). " ) " ;
160
165
} else {
161
166
$ string = "" ;
162
167
$ index = 0 ;
@@ -170,7 +175,7 @@ public function switchAction(ServerRequestInterface $requestInterface, ResponseI
170
175
$ index ++;
171
176
}
172
177
if (!isSet ($ pkValue )) throw new PydioException ("Cannot find PK Value " );
173
- $ query = "UPDATE $ tableName SET $ string WHERE $ pkName=' $ pkValue' " ;
178
+ $ query = "UPDATE ` $ tableName` SET $ string WHERE $ pkName=' $ pkValue' " ;
174
179
}
175
180
$ this ->execQuery ($ ctx , $ query );
176
181
$ logMessage = $ query ;
@@ -183,6 +188,7 @@ public function switchAction(ServerRequestInterface $requestInterface, ResponseI
183
188
//------------------------------------
184
189
case "edit_table " :
185
190
if (isSet ($ httpVars ["current_table " ])) {
191
+ $ current_table = InputFilter::sanitize ($ httpVars ["current_table " ], InputFilter::SANITIZE_ALPHANUM );
186
192
if (isSet ($ httpVars ["delete_column " ])) {
187
193
$ query = "ALTER TABLE " .$ httpVars ["current_table " ]." DROP COLUMN " .$ httpVars ["delete_column " ];
188
194
$ this ->execQuery ($ ctx , $ query );
@@ -192,7 +198,7 @@ public function switchAction(ServerRequestInterface $requestInterface, ResponseI
192
198
}
193
199
if (isSet ($ httpVars ["add_column " ])) {
194
200
$ defString = $ this ->makeColumnDef ($ httpVars , "add_field_ " );
195
- $ query = "ALTER TABLE " .$ httpVars [ " current_table " ]. " ADD COLUMN ( $ defString) " ;
201
+ $ query = "ALTER TABLE ` " .$ current_table. " ` ADD COLUMN ($ defString) " ;
196
202
if (isSet ($ httpVars ["add_field_pk " ]) && $ httpVars ["add_field_pk " ]=="1 " ) {
197
203
$ query .= ", ADD PRIMARY KEY ( " .$ httpVars ["add_field_name " ].") " ;
198
204
}
@@ -232,7 +238,8 @@ public function switchAction(ServerRequestInterface $requestInterface, ResponseI
232
238
$ reload_file_list = true ;
233
239
}
234
240
$ logMessage = $ qMessage ;
235
- } else if (isSet ($ new_table )) {
241
+ } else if (isSet ($ httpVars ["new_table " ])) {
242
+ $ new_table = InputFilter::sanitize ($ httpVars ["new_table " ], InputFilter::SANITIZE_ALPHANUM );
236
243
$ fieldsDef = array ();
237
244
$ pks = array ();
238
245
$ indexes = array ();
@@ -261,6 +268,7 @@ public function switchAction(ServerRequestInterface $requestInterface, ResponseI
261
268
$ reload_file_list = true ;
262
269
$ reload_current_node = true ;
263
270
}
271
+
264
272
break ;
265
273
266
274
//------------------------------------
@@ -593,6 +601,25 @@ public function listTables(ContextInterface $ctx)
593
601
return $ allTables ;
594
602
}
595
603
604
+ /**
605
+ * Find autoincrement key
606
+ * @param ContextInterface $ctx
607
+ * @param $tablename
608
+ * @return bool
609
+ * @throws PydioException
610
+ */
611
+ public function findTableAutoIncrementKey (ContextInterface $ ctx , $ tablename ){
612
+
613
+ $ result = $ this ->execQuery ($ ctx , "SELECT * from ` $ tablename` LIMIT 0,1 " );
614
+ $ fields = mysqli_fetch_fields ($ result );
615
+ foreach ($ fields as $ field ){
616
+ if ($ field ->flags & MYSQLI_AUTO_INCREMENT_FLAG ){
617
+ return $ field ->name ;
618
+ }
619
+ }
620
+ return false ;
621
+ }
622
+
596
623
/**
597
624
* @param ContextInterface $ctx
598
625
* @param $query
@@ -673,7 +700,7 @@ public function showRecords(ContextInterface $ctx, $query, $tablename, $currentP
673
700
}
674
701
675
702
// MAKE ROWS RESULT
676
- for ($ s =0 ; $ s < $ rpp ; $ s ++) {
703
+ for ($ s =0 ; $ s < min ( $ rpp, mysqli_num_rows ( $ result )) ; $ s ++) {
677
704
$ row =mysqli_fetch_array ($ result );
678
705
if (!isset ($ pk )) {
679
706
$ pk =' ' ;
0 commit comments