#!/usr/bin/perl

use CGI qw/:standard/;
use CGI::Cookie;

sub notegs
{ 
 #Удаление символов пеpевода стpоки и тегов
 local($str)=@_; 
 $str=~s/\s+/ /g;
 $str=~s/^\s+//;
 $str=~s/&/&amp;/g;
 $str=~s/</&lt;/g;
 $str=~s/>/&gt;/g;
 return $str;
}

#Распознание и запись в пеpеменные паpаметpов вызова пpогpаммы
sub urldecode
{ 
 local($val)=@_; 
 $val=~s/\+/ /g;
 $val=~s/%([0-9A-H]{2})/pack('C',hex($1))/ge;
 return $val;
} 

if($ENV{'REQUEST_METHOD'} eq 'GET')
{ 
 $query=$ENV{'QUERY_STRING'};
}
elsif($ENV{'REQUEST_METHOD'} eq 'POST')
{
 sysread STDIN,$query,$ENV{'CONTENT_LENGTH'};
}

@formfields=split /&/,$query;       
foreach(@formfields)
{
 if(/^id=(.*)/){$id=notegs(urldecode($1));}
 if(/^name=(.*)/){$name=notegs(urldecode($1));}
 if(/^price=(.*)/){$price=notegs(urldecode($1));}
 if(/^mode=(.*)/){$mode=notegs(urldecode($1));}
}

while(($key,$val) = each(ENV))
{
 if ($key eq "REMOTE_ADDR"){$ip=$val;}
 if ($key eq "HTTP_REFERER"){$referer=$val;}
 if ($key eq "HTTP_USER_AGENT"){$browser=$val;}
}


%cookie = fetch CGI::Cookie;

if ($cookie{'custom_shop'} ne "")
{
 ($cart, $items)=$cookie{'custom_shop'}->value;

 @new_cart=();
 @cart=split /\n/, $items;

 #Удаление товара из корзины
 if ($mode eq "remove")
 {
  foreach $item (@cart)
  {
   ($item_id, $tmp)=split /#/, $item, 2;
   if ($item_id ne $id){push @new_cart, $item;}
  }

  $items=join "\n", @new_cart;

  $cookie = new CGI::Cookie(-name=>'custom_shop',-value=>{cart=>$items});
  print header(-cookie=>$cookie, -location=>$referer.'#cart');
 }

 #Пересчет корзины
 if ($mode eq "recalculate")
 {
  foreach $item (@cart)
  {
   ($id, $name, $dep, $price, $count, $sign, $img)=split /#/, $item;
   foreach(@formfields)
   {
    if(/^$id=(.*)/){$count=notegs(urldecode($1));}
   }
   push @new_cart, $id."#".$name."#".$name."#".$price."#".$count."#".$sign."#".$img;
  }

  $items=join "\n", @new_cart;

  $cookie = new CGI::Cookie(-name=>'custom_shop',-value=>{cart=>$items});
  print header(-cookie=>$cookie, -location=>$referer);
 }

 if ($mode eq "" && $items ne "")
 {
  $sum=0;
  $order="";
  $result="";
  $result.='<form id="cart_gform" action="/cgi-bin/cart_calculator_jsf.pl" method="post">';
  $result.='<table width="100%" cellspacing="1" cellpadding="5" border="0" style="border-top: solid #000 2px; border-collapse: collapse;">';
  $result.='<tr><td colspan="2">&nbsp;</td><td align="left" style="font-size: 16px;">Цена</td><td align="left" style="font-size: 16px;">Количество</td><td align="left" style="font-size: 16px;">Сумма</td><td align="center" style="font-size: 16px;">Очистить</td></tr>';

  foreach $item (@cart)
  {
   ($id, $name, $dep, $price, $count, $sign, $img)=split /#/, $item;
   $name=~s/\'//g;
   $sum+=$price*$count;
   $result.='<tr><td align="left"><img src="/thumb.php?file='.$img.'&sizex=180" border="0"></td><td align="left" style="font-size: 16px;">'.$name.'<br><em><span style="color: #646566; font-size: 14px;">'.$dep.'</span></em></td><td align="left"><span style="color: #f00; font-family: arial; font-size: 24px;"><b>'.$price.'</b></span> <span style="color: #696a6b; font-family: arial; font-size: 14px;">руб.</span></td><td align="left"><input type="text" name="'.$id.'" value="'.$count.'" size="3" class="shop_cart_count"></td><td align="left"><span style="color: #f00; font-family: arial; font-size: 24px;"><b>'.$price*$count.'</b></span> <span style="color: #696a6b; font-family: arial; font-size: 14px;">руб.</span></td><td align="center"><a href="/cgi-bin/cart_calculator_js.pl?mode=remove&id='.$id.'" class="shop_cart_link"><img src="/images/btn_del_good.jpg" border="0"></a></td></tr>';
   $order.="$name ($count шт.) - $price руб.<br>".'\n';
  }
  $result.='<tr><td align="left" style="border-top: solid #000 2px; font-size: 16px;" colspan="5">Итого: <span style="color: #f00; font-family: arial; font-size: 24px;"><b>'.$sum.' <span style="color: #696a6b; font-family: arial; font-size: 14px;">руб.</span></td><td style="border-top: solid #000 2px;" align="center"><!--<input type="submit" value="Пересчитать" class="shop_cart_button">--></td></tr>';
  $result.='<input type="hidden" name="mode" value="recalculate">';
  $result.='<input type="hidden" name="cart" id="cart" value="'.$order.'">';
  $result.='</table>';
  $result.='</form>';
 }
 else
 {
  $result="Ваша корзина пуста";
 }
}
else
{
 $result="Ваша корзина пуста";
}
print "Content-Type: text/html\n\n";
print "document.write (\'$result\');";

exit 0;

