#!/usr/bin/perl
use strict;
##########################################################
# CSVwrite 28/Okt/2006
# © 1999-2003 EZscripting.com
# Script by Alexandre Golovkine & Babelnotes.be
##########################################################
my $base_file                = 'database.txt';
my $HTML_thankyou            = 'thankyou.htm';
my $line_break               = '<br>';
my $ID_autoincrement         = 1; 
my $ID_autoincrement_name    = "ID";
##########################################################
# EZscripting.com © 1999 - 2006
# The scripts are available for private and commercial use
# Once purchased this script can be used in any website you build personally
# You may not sell the script in any format to anybody
# The scripts may only be distributed by EZscripting.com
# Do not post or email all or part of the this code in any form whatsoever
# The redistribution of modified versions of the scripts is prohibited
# EZscripting.com accepts no responsibility or liability
# whatsoever for any damages however caused when using our services or scripts
# By downloading and using this script you agree to the terms and conditions
##########################################################
my $r = shift;
$r=undef if ref($r)!~/^Apache/;
 
use Fcntl qw(:DEFAULT :flock);
my @field;
my %field;
my %FORM = parse_cgi();
create_base()   unless -f $base_file;
 
open(F, $base_file) || error("Can't open file $base_file $!!");
flock(F, LOCK_EX);
my @data = <F>;
close F;
 my @data_fields=split('\|', $data[0]);
 error("You have bad file!") if !@data_fields;
 chomp $data_fields[@data_fields-1];
 my $maxID;
 if($ID_autoincrement_name && $ID_autoincrement){
  my $positionID =-1; my $p=0;
  foreach(@data_fields){
   $positionID=$p if $_ eq $ID_autoincrement_name;
   $p++;
  }
  if($positionID>=0){
   #seach max ID
   for(1..@data-1){
    my @line=split('\|', $data[$_]);
    $maxID=$line[$positionID] if $maxID < $line[$positionID];
   }
   $maxID||=999999;
   $maxID++;
  }
  else{error("Can't find ID-field in your db!");}
 }
 
 
my $line;
foreach(@data_fields){
 if ($ID_autoincrement && $_ eq $ID_autoincrement_name){
  $line .="$maxID|";
 }
 else{
  $line .="$FORM{$_}|"; 
 }
 $field{$_}=1;
} 
chop $line;
foreach(@field){error("Field $_ not exist!") if !$field{$_};} 
 
 open(F, ">>$base_file") || error("Can't open file $base_file!");
 flock(F, LOCK_EX);
 print F "$line\n";
 close F;
if($r){
 $r->headers_out->{'Location'} = $HTML_thankyou;
 $r->status(302);
}
else{print "Location: $HTML_thankyou\n\n";}
 
exit;
 

##############################################################
sub create_base{
 my $field = join('|', @field);
 $field = $ID_autoincrement_name."|".$field if $ID_autoincrement;
 open(F, ">$base_file");
 flock(F, LOCK_EX);
 print F "$field\n";
 close F;
}
 
sub error{
 if($r){$r->content_type('text/html');}
 else{print "Content-type: text/html\n\n";}
 print "<html><head><title>Error</title></head><body><br><br><br><font color=red><h3>$_[0]</h3></font></body></html>";
 exit;
}  
 
sub parse_cgi{
 my(%FORM,$name,$value,$content);
 my $a=0;
 read(STDIN, my $buffer, $ENV{'CONTENT_LENGTH'});
 my @pairs = split(/&/, $buffer);
 foreach my $pair (@pairs) {
    my ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $value =~ s/"/&#34;/g;
    $value =~ s/'/&#39;/g;
    $value =~ s/<!--(.|\n)*-->//g;
    $value =~ s/<([^>]|\n)*>//g;
           $value =~ s/\n/$line_break/g;  # added to strip line breaks
           $value =~ s/\r//g;  
    $value =~ s/\|/I/g;
    if($FORM{$name}){$FORM{$name} = ", ".$value;}
           else{$FORM{$name} = $value;}
           $field[$a++]=$name;
 }
 $a=0;
 @pairs=split(/&/,$ENV{'QUERY_STRING'});
 foreach my $pair (@pairs) {
  ($name, $value)= split (/=/,$pair,2);
  $value =~ tr/+/ /;
  $value =~ s/%(..)/pack("c",hex($1))/ge;
  $value =~ s/\|/I/g;
  $value =~ s/"/&#34;/g;
  $value =~ s/'/&#39;/g;
  $value =~ s/<!--(.|\n)*-->//g;
  $value =~ s/<([^>]|\n)*>//g;
  $value =~ s/\n/$line_break/g;  # added to strip line breaks
  $value =~ s/\r//g;
  if($FORM{$name}){$FORM{$name} .= ", ".$value;}
  else{$FORM{$name} = $value;}
  $field[$a++]=$name;
  }
 return %FORM;
}
 
##########################################################
# EZscripting.com © 1999 - 2006
# The scripts are available for private and commercial use
# Once purchased this script can be used in any website you build personally
# You may not sell the script in any format to anybody
# The scripts may only be distributed by EZscripting.com
# Do not post or email all or part of the this code in any form whatsoever
# The redistribution of modified versions of the scripts is prohibited
# EZscripting.com accepts no responsibility or liability
# whatsoever for any damages however caused when using our services or scripts
# By downloading and using this script you agree to the terms and conditions
##########################################################
