#!/usr/bin/perl

########################################################
#                                                      #
# Gestdb v1.0 by Michela Fontana                       #
# Website : http://www.calzatura.com                   #
# Created : 01/01/2003                                 #
#                                                      #
########################################################

############   ADJUST THESE VARIABLES   ################

        use CGI;
        use DBI;

# Get the form variables

	if ($ENV{'REQUEST_METHOD'} eq 'GET') {
        	$buffer = $ENV{'QUERY_STRING'};
	}	
	else {
        	read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
	}

# Break em up into a format the script can read

	@pairs = split(/&/, $buffer);
	foreach $pair (@pairs) {
        	($name, $value) = split(/=/, $pair);
        	$value =~ tr/+/ /;
        	$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
        	$FORM{$name} = $value;
	}

# Get the heading information

  $headfile = '/italy/head/head_motore.txt';

   unless (open (DATA,"$headfile")) {die (&error);}
		if ($uselock eq '1') {
			flock DATA, 2;
			seek DATA, 0, 0;
		}
		@headinfo = <DATA>;
		if ($uselock eq '1') {
			flock DATA, 8;
		}
	close (DATA);
	foreach $headline (@headinfo){
		$heading = $heading.$headline;
	}

# Get the footer information

  $footfile = '/italy/head/foot.txt';

   unless (open (DATA,"$footfile")) {die (&error);}
		if ($uselock eq '1') {
			flock DATA, 2;
			seek DATA, 0, 0;
		}
		@footinfo = <DATA>;
		if ($uselock eq '1') {
			flock DATA, 8;
		}
	close (DATA);
	foreach $footline (@footinfo){
		$footing = $footing.$footline;
	}

# Get the Dbase information

$mysql1  = 'calzatura_com_calzatura';
$mysql2  = 'mysql.register.it';
$mysql3  = 'GS1219_CALZATURA';
$mysql4  = 'michela';

$id       = $FORM{'id'};
$miamail  = $FORM{'miamail'};
$password = $FORM{'password'};

print "Content-type: text/html\n\n";

##################################################################################

# Open DB connection
$dbh = DBI->connect("DBI:mysql:$mysql1:$mysql2", $mysql3, $mysql4);
if (!dbh) { print "Errore!"; }

my $stat = $dbh->prepare("SELECT ID, TIPO, CATEGORIA, OGGETTO, DESCRIZIONE, PREZZO,
                                 VALUTA, FOTO, UTENTE, PASSWORD,
                                 DATAINSERIMENTO, DATAVALIDITA, PAGAMENTO, GIORNI, RESA,
                                 OGGETTOENG, OGGETTOSPA, OGGETTOPORT,
                                 DESCRIZIONEENG, DESCRIZIONESPA, DESCRIZIONEPORT,
                                 UM, NAZIONE
                          FROM Market
                          WHERE ID='$id'");

my $return = $stat->execute;

	unless (open (DATA,"/easysearch/password.txt")) {die (&error);}
		if ($uselock eq '1') {
			flock DATA, 2;
			seek DATA, 0, 0;
		}
		@input = <DATA>;
		if ($uselock eq '1') {
			flock DATA, 8;
		}
                $currentline = @input;
	close (DATA);

($id, $tipo, $categoria, $oggetto, $descrizione, $prezzo, $valuta, $foto,
        $mkutente, $mkpassword, $datainserimento, $validita, $pagamento, $giorni,
        $resa, $oggettoeng, $oggettospa, $oggettoport, $descrizioneeng, $descrizionespa,
        $descrizioneport, $um, $mknazione) = $stat->fetchrow_array;

$i = $currentline;
$trovato = 'no';
$ident = '';

until (($i < 0) || ($trovato eq 'si')) {
      @data = split(/::/,$input[$i]);
      $uutente = "\U$data[0]";
      $upassword = "\U$data[1]";
      $uutente2 = "\U$utente";
      $upassword2 = "\U$password";
      if (($uutente eq $uutente2) && ($upassword eq $upassword2)) {
         $trovato = 'si';
         $ident = $data[2];
         $nome = $data[6];
         $nome2 = $data[5];
         $nome2 =~ s/́/<br>/g;
         $azienda = $data[5];
         $email = $data[4];
         }
      else
         {
         --$i;
      }
}

($azienda,$citta,$provincia,$naz,$mail,$nomecontatto,$cognomecontatto) = $dbh->selectrow_array("SELECT RAGIONESOCIALE, CITTA, PROVINCIA, NAZIONE, MAIL, NOMECONTATTO, COGNOMECONTATTO FROM Base WHERE MAIL='$mkutente'");
($nazioneestesa, $imgnazione) = $dbh->selectrow_array("SELECT NAZIONE, IMMAGINE FROM Nazioni WHERE ID='$naz'");

$fotourl='http://www.calzatura.com/fotomarket/'.$foto;

print "$heading";
print "<body topmargin=0 bgcolor=#E8E8E8 leftmargin=2 link=#305790 vlink=#4375C0 alink=#305790 text=#305790>
<table border=0 cellpadding=0 cellspacing=3 width=100%>
  <tr>
    <td width=100% bgcolor=#808080><p align=center><font face=Verdana size=2 color=#FFFFFF><strong>MARKET PLACE - N° $id</strong></font></td>
  </tr>
</table>

<table border=0 cellpadding=0 cellspacing=3 width=100%>
  <tr>
    <td width=15% bgcolor=#FF8000><font face=Verdana color=#FFFFFF><small><small><strong>A</strong>zienda:</small></small></font></td>
    <td width=5% bgcolor=#EAEAEA align=center valign=center><small><font face=Verdana><small><img src=$imgnazione width=20 height=12 alt='$nazioneestesa'></small></font></td>
    <td width=30% bgcolor=#EAEAEA><font face=Verdana><strong><small><small>$azienda<strong></small></small></font></td>
    <td width=10% bgcolor=#FF8000><font face=Verdana color=#FFFFFF><small><small><strong>C</strong>ontatto:</small></small></font></td>
    <td width=25% bgcolor=#EAEAEA><font face=Verdana><strong><small><small>$nomecontatto $cognomecontatto</small></small></font></td>
    <td width=15%><form method=POST action=contatta.cgi>
      <input type=hidden name=id             value='$id'>
      <input type=hidden name=miamail        value=$miamail>
      <input type=hidden name=password       value=$password>
      <div align=center><center><p>
      <input type=submit value=Contatta name=B1 style='background-color: rgb(255,255,255); border: thin solid rgb(255,128,0)'></p></center></div></td>
    </form>
  </tr>
</table>
<div align=center>

<table border=0 cellpadding=0 cellspacing=3 width=100%>
  <tr>
    <td width=15% bgcolor=#FF8000><font face=Verdana color=#FFFFFF><small><small><strong>O</strong>ggetto:</small></small></font></td>
    <td width=85%><font face=Verdana><small><small>";

if ($oggetto ne '') { print "$oggetto"; }
else {
    if ($oggettoeng ne '') { print "$oggettoeng"; }
       else {
          if ($oggettospa ne '') { print "$oggettospa"; }
             else {
               if ($oggettoport ne '') { print "$oggettoport"; }
} } }

print "</small></small></font></td>
  </tr>
  <tr>
    <td width=15% bgcolor=#FF8000><font face=Verdana color=#FFFFFF><small><small><strong>D</strong>escrizione:</small></small></font></td>
    <td width=85%><font face=Verdana><small><small>";

if ($descrizione ne '') { print "$descrizione"; }
else {
    if ($descrizioneeng ne '') { print "$descrizioneeng"; }
       else {
          if ($descrizionespa ne '') { print "$descrizionespa"; }
             else {
               if ($descrizioneport ne '') { print "$descrizioneport"; }
} } }

print "</small></small></font></td>
  </tr>";
if ($foto ne '') {
  $fotourl='http://www.calzatura.com/fotomarket/'.$foto;
  print "<tr>
    <td width=15%><font face=Verdana color=#FFFFFF><small><small></small></small></font></td>
    <td width=85%><img src=$fotourl></td>
  </tr>";
}

if ($um ne '') {
print "<tr>
    <td width=15% bgcolor=#FF8000><small><small><font face=Verdana color=#FFFFFF><strong>U.</strong> Misura:</font></small></small></td>
    <td width=85%><font face=Verdana><small><small>$um</small></small></font></td>
  </tr>";
}

if ($prezzo ne '0.00') {
print "<tr>
    <td width=15% bgcolor=#FF8000><small><small><font face=Verdana color=#FFFFFF><strong>P</strong>rezzo:</font></small></small></td>
    <td width=85%><font face=Verdana><small><small>$prezzo $valuta</small></small></font></td>
  </tr>";
}

if ($datavalidita ne '') {
print"  <tr>
    <td width=15% bgcolor=#FF8000><font face=Verdana color=#FFFFFF><small><small><strong>V</strong>alidità fino al:</small></small></font></td>
    <td width=85%><font face=Verdana><small><small>$validita</small></small></font></td>
  </tr>";
}

if ($pagamento ne '') {
   print "  <tr>
    <td width=15% bgcolor=#FF8000><font face=Verdana color=#FFFFFF><small><small><strong>P</strong>agamento:</small></small></font></td>
    <td width=85%><font face=Verdana><small><small>$pagamento $giorni gg</small></small></font></td>
  </tr>";
}
if ($resa ne '') {
   print "  <tr>
    <td width=15% bgcolor=#FF8000><font face=Verdana color=#FFFFFF><small><small><strong>R</strong>esa:</small></small></font></td>
    <td width=85%><font face=Verdana><small><small>$resa</small></small></font></td>
  </tr>";
}


print "</table>
</body>";
print "$footing";

$dbh->disconnect;

exit;

##################################################################################


sub error {    
	$noshow = 1;
	print "<CENTER><FONT $font><h2>File Access Error</h2><P><B>You have an error in your PATH configuration variables in the $ENV{'SCRIPT_NAME'} file.</B><P>Your server reports that your BASE path is : $ENV{'DOCUMENT_ROOT'}<BR>Note that this is reported as your BASE path, not the FULL path to your files.<P>If you require help installing this script please consider purchasing the professional version of this script. Your purchase includes full tech support and installation.<P><B>Get it at : <A HREF=http://www.getperl.com/easysearch/>http://www.getperl.com/easysearch/</A></B></FONT></CENTER><P>\n";
	&footer;
}
