2007-07-02 05:51:26 +00:00
< ? php
2007-07-09 06:12:22 +00:00
$where = where ();
$fields = array ();
foreach ( fields ( $_GET [ " edit " ]) as $name => $field ) {
if ( isset ( $_GET [ " default " ]) ? ! $field [ " auto_increment " ] : isset ( $field [ " privileges " ][ $where ? " update " : " insert " ])) {
$fields [ $name ] = $field ;
}
2007-07-05 10:41:53 +00:00
}
2007-07-09 06:12:22 +00:00
if ( $_POST && ! $error ) {
2007-07-02 05:51:26 +00:00
if ( isset ( $_POST [ " delete " ])) {
2007-07-09 06:12:22 +00:00
$set = true ;
2007-07-02 05:51:26 +00:00
$query = " DELETE FROM " . idf_escape ( $_GET [ " edit " ]) . " WHERE " . implode ( " AND " , $where ) . " LIMIT 1 " ;
$message = lang ( 'Item has been deleted.' );
} else {
$set = array ();
2007-07-09 06:12:22 +00:00
foreach ( $fields as $name => $field ) {
$key = bracket_escape ( $name );
$val = $_POST [ " fields " ][ $key ];
if ( preg_match ( '~char|text|set|binary|blob~' , $field [ " type " ]) ? $_POST [ " null " ][ $key ] : ! strlen ( $val )) {
2007-07-06 13:48:25 +00:00
$val = " NULL " ;
2007-07-02 05:51:26 +00:00
} elseif ( $field [ " type " ] == " enum " ) {
2007-07-09 06:12:22 +00:00
$val = ( isset ( $_GET [ " default " ]) && preg_match_all ( " ~'((?:[^']*|'')+)'~ " , $field [ " length " ], $matches ) ? " ' " . $matches [ 1 ][ $val - 1 ] . " ' " : intval ( $val ));
2007-07-02 05:51:26 +00:00
} elseif ( $field [ " type " ] == " set " ) {
2007-07-09 06:12:22 +00:00
if ( ! isset ( $_GET [ " default " ])) {
$val = array_sum (( array ) $val );
} else {
preg_match_all ( " ~'((?:[^']*|'')+)'~ " , $field [ " length " ], $matches );
$value = array ();
foreach (( array ) $val as $key => $v ) {
$value [] = $matches [ 1 ][ $key ];
}
$val = " ' " . implode ( " , " , $value ) . " ' " ;
}
} elseif ( preg_match ( '~binary|blob~' , $field [ " type " ])) {
$file = get_file ( $key );
if ( ! is_string ( $file ) && ! $field [ " null " ]) {
continue ; //! report errors, also empty $_POST - not only because of file upload
}
$val = " _binary' " . ( is_string ( $file ) ? mysql_real_escape_string ( $file ) : " " ) . " ' " ;
2007-07-02 05:51:26 +00:00
} else {
2007-07-06 13:48:25 +00:00
$val = " ' " . mysql_real_escape_string ( $val ) . " ' " ;
2007-07-02 05:51:26 +00:00
}
2007-07-09 06:12:22 +00:00
$set [] = idf_escape ( $name ) . ( isset ( $_GET [ " default " ]) ? ( $val == " NULL " ? " DROP DEFAULT " : " SET DEFAULT $val " ) : " = $val " );
2007-07-02 05:51:26 +00:00
}
2007-07-09 06:12:22 +00:00
if ( isset ( $_GET [ " default " ])) {
$query = " ALTER TABLE " . idf_escape ( $_GET [ " edit " ]) . " ALTER " . implode ( " , ALTER " , $set );
$message = lang ( 'Default values has been set.' );
} elseif ( $where ) {
2007-07-02 05:51:26 +00:00
$query = " UPDATE " . idf_escape ( $_GET [ " edit " ]) . " SET " . implode ( " , " , $set ) . " WHERE " . implode ( " AND " , $where ) . " LIMIT 1 " ;
$message = lang ( 'Item has been updated.' );
} else {
$query = " INSERT INTO " . idf_escape ( $_GET [ " edit " ]) . " SET " . implode ( " , " , $set );
$message = lang ( 'Item has been inserted.' );
}
}
2007-07-09 06:12:22 +00:00
if ( ! $set || mysql_query ( $query )) {
redirect ( $SELF . ( isset ( $_GET [ " default " ]) ? " table= " : ( $_POST [ " insert " ] ? " edit= " : " select= " )) . urlencode ( $_GET [ " edit " ]), ( $set ? $message : null ));
2007-07-02 05:51:26 +00:00
}
2007-07-02 16:42:42 +00:00
$error = mysql_error ();
2007-07-02 05:51:26 +00:00
}
2007-07-09 06:12:22 +00:00
page_header (( isset ( $_GET [ " default " ]) ? lang ( 'Default values' ) : ( $_GET [ " where " ] ? lang ( 'Edit' ) : lang ( 'Insert' ))) . " : " . htmlspecialchars ( $_GET [ " edit " ]));
2007-07-02 05:51:26 +00:00
if ( $_POST ) {
2007-07-02 16:42:42 +00:00
echo " <p class='error'> " . lang ( 'Error during saving' ) . " : " . htmlspecialchars ( $error ) . " </p> \n " ;
2007-07-09 06:12:22 +00:00
$data = ( array ) $_POST [ " fields " ];
foreach (( array ) $_POST [ " null " ] as $key => $val ) {
2007-07-02 05:51:26 +00:00
$data [ $key ] = null ;
}
} elseif ( $where ) {
2007-07-06 13:48:25 +00:00
$select = array ();
2007-07-02 05:51:26 +00:00
foreach ( $fields as $name => $field ) {
2007-07-09 06:12:22 +00:00
if ( isset ( $field [ " privileges " ][ " select " ]) && ! preg_match ( '~binary|blob~' , $field [ " type " ])) {
2007-07-06 13:48:25 +00:00
$select [] = ( $field [ " type " ] == " enum " || $field [ " type " ] == " set " ? " 1* " . idf_escape ( $name ) . " AS " : " " ) . idf_escape ( $name );
2007-07-02 05:51:26 +00:00
}
}
2007-07-06 13:48:25 +00:00
$data = ( $select ? mysql_fetch_assoc ( mysql_query ( " SELECT " . implode ( " , " , $select ) . " FROM " . idf_escape ( $_GET [ " edit " ]) . " WHERE " . implode ( " AND " , $where ) . " LIMIT 1 " )) : array ());
2007-07-02 05:51:26 +00:00
} else {
2007-07-09 06:12:22 +00:00
unset ( $data );
2007-07-02 05:51:26 +00:00
}
?>
2007-07-09 06:12:22 +00:00
< form action = " " method = " post " enctype = " multipart/form-data " >
2007-07-06 13:48:25 +00:00
< table border = " 0 " cellspacing = " 0 " cellpadding = " 2 " >
2007-07-02 05:51:26 +00:00
< ? php
2007-07-04 14:37:54 +00:00
$types = types ();
2007-07-09 06:12:22 +00:00
$save_possible = false ;
2007-07-02 05:51:26 +00:00
foreach ( $fields as $name => $field ) {
2007-07-09 06:12:22 +00:00
$save_possible = true ;
echo " <tr><th> " . htmlspecialchars ( $name ) . " </th><td> " ;
$value = ( isset ( $data ) ? $data [ $name ] : $field [ " default " ]);
$name = htmlspecialchars ( $_POST ? $name : bracket_escape ( $name ));
if ( $field [ " type " ] == " enum " ) {
if ( ! isset ( $_GET [ " default " ])) {
2007-07-06 13:48:25 +00:00
echo '<input type="radio" name="fields[' . $name . ']" value="0"' . ( $value == " 0 " ? ' checked="checked"' : '' ) . ' />' ;
2007-07-02 05:51:26 +00:00
}
2007-07-09 06:12:22 +00:00
preg_match_all ( " ~'((?:[^']*|'')+)'~ " , $field [ " length " ], $matches );
foreach ( $matches [ 1 ] as $i => $val ) {
$id = " field- $name - " . ( $i + 1 );
$checked = ( isset ( $data ) ? $value == $i + 1 : $val === $field [ " default " ]);
echo ' <input type="radio" name="fields[' . $name . ']" id="' . $id . '" value="' . ( $i + 1 ) . '"' . ( $checked ? ' checked="checked"' : '' ) . ' /><label for="' . $id . '">' . htmlspecialchars ( str_replace ( " '' " , " ' " , $val )) . '</label>' ;
}
if ( $field [ " null " ]) {
$id = " field- $name - " ;
echo '<input type="radio" name="fields[' . $name . ']" id="' . $id . '" value=""' . ( strlen ( $value ) ? '' : ' checked="checked"' ) . ' /><label for="' . $id . '">' . lang ( 'NULL' ) . '</label> ' ;
2007-07-02 05:51:26 +00:00
}
2007-07-09 06:12:22 +00:00
} elseif ( $field [ " type " ] == " set " ) { //! 64 bits
preg_match_all ( " ~'((?:[^']*|'')+)'~ " , $field [ " length " ], $matches );
foreach ( $matches [ 1 ] as $i => $val ) {
$id = " $name - " . ( $i + 1 );
$checked = ( isset ( $data ) ? ( $value >> $i ) & 1 : in_array ( str_replace ( " '' " , " ' " , $val ), explode ( " , " , $field [ " default " ]), true ));
echo ' <input type="checkbox" name="fields[' . $name . '][' . $i . ']" id="' . $id . '" value="' . ( 1 << $i ) . '"' . ( $checked ? ' checked="checked"' : '' ) . ' /><label for="' . $id . '">' . htmlspecialchars ( str_replace ( " '' " , " ' " , $val )) . '</label>' ;
}
} elseif ( strpos ( $field [ " type " ], " text " ) !== false ) {
echo '<textarea name="fields[' . $name . ']" cols="50" rows="12">' . htmlspecialchars ( $value ) . '</textarea>' ;
} elseif ( preg_match ( '~binary|blob~' , $field [ " type " ])) {
echo ( ini_get ( " file_uploads " ) ? '<input type="file" name="' . $name . '" />' : lang ( 'File uploads are disabled.' ) . ' ' );
} else { //! binary
echo '<input name="fields[' . $name . ']" value="' . htmlspecialchars ( $value ) . '"' . ( strlen ( $field [ " length " ]) ? " maxlength=' $field[length] ' " : ( $types [ $field [ " type " ]] ? " maxlength=' " . $types [ $field [ " type " ]] . " ' " : '' )) . ' />' ;
}
if ( $field [ " null " ] && preg_match ( '~char|text|set|binary|blob~' , $field [ " type " ])) {
echo '<input type="checkbox" name="null[' . $name . ']" value="1" id="null-' . $name . '"' . ( isset ( $value ) ? '' : ' checked="checked"' ) . ' /><label for="null-' . $name . '">' . lang ( 'NULL' ) . '</label>' ;
2007-07-02 05:51:26 +00:00
}
2007-07-09 06:12:22 +00:00
echo " </td></tr> \n " ;
2007-07-02 05:51:26 +00:00
}
?>
</ table >
2007-07-09 06:12:22 +00:00
< p >
< input type = " hidden " name = " token " value = " <?php echo $token ; ?> " />
< ? php if ( $save_possible ) { ?>
< input type = " submit " value = " <?php echo lang('Save'); ?> " />
< ? php if ( ! isset ( $_GET [ " default " ])) { ?> <input type="submit" name="insert" value="<?php echo lang('Save and insert'); ?>" /><?php } ?>
< ? php } ?>
< ? php if ( $where ) { ?> <input type="submit" name="delete" value="<?php echo lang('Delete'); ?>" /><?php } ?>
</ p >
2007-07-02 05:51:26 +00:00
</ form >