#!/usr/local/bin/perl #$root="/opt/clients/gazeta/cgi-bin"; #$htdocs="/opt/clients/gazeta/www"; $root="/bhome/part2/01/vh01002/vcgi"; $htdocs="/bhome/part2/01/vh01002/www"; #$root="/usr/local/www/cgi-bin/www.gazeta.ru"; #$htdocs="/usr/local/www/data/www.gazeta.ru"; #$root="/tmp"; #$htdocs="/tmp"; $datfile="opros1.dat"; $comber="combo1"; $tpldir="/bhome/part2/01/vh01002/vcgi/tpl"; $templ="opros1.html"; $ipbase="opros1.ip"; if( $ENV{'CONTENT_TYPE'}=~/multipart/ ) { binmode STDIN; read(STDIN, $query, $ENV{'CONTENT_LENGTH'}); $query=~/\A(.+?)(\015|\012)/; $boundary = $1; (@allkeys) = split/$boundary/,$query; foreach $mykey (@allkeys) { if($mykey=~/name="(.+?)"/) { $keyname=$1; $mykey=~s/\A(.+?)\015{0,1}\012\015{0,1}\012//s; $keyinfo=$1; if( $keyinfo=~/filename="(.+?)"/ ) { $filename = $1; } else { $filename=""; } $mykey=~s/\015{0,1}\012\Z//; $formdata{$keyname}=$mykey; $filenames{$keyname}=$filename; } } } else { if( $ENV{'REQUEST_METHOD'} eq 'POST' ) { read(STDIN, $query, $ENV{'CONTENT_LENGTH'}); @params=split(/&/, $query); foreach $keyvalueparam (@params) { ($keyparam, $valueparam) = split(/=/, $keyvalueparam); $valueparam=~tr/+/ /; $valueparam=~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg; $formdata{$keyparam}=$valueparam; } } else { $query = $ENV{'QUERY_STRING'}; @params=split(/&/, $query); foreach $keyvalueparam (@params) { ($keyparam, $valueparam) = split(/=/, $keyvalueparam); $valueparam=~tr/+/ /; $valueparam=~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg; $formdata{$keyparam}=$valueparam; } } } use CGI; use Fcntl qw(:flock); my $q=CGI->new(); saveVopros($q); ###################### sub saveVopros { my ($q,$cookie,@msglines,@strings,$cont); $q=shift; remove_expired_ips(); $cookie=user_can_vote($q); open(NUMZ,"$root/$datfile"); @msglines = ; map { s/\n//g; } @msglines; close(NUMZ); if( defined $cookie ) { $number = $formdata{"combo1"}; #$q->param($comber); if( defined $number ) { $msglines[$number]++; } open(OPR,">$root/$datfile"); flock(OPR,LOCK_EX); foreach (@msglines) { print OPR "$_\n"; } close(OPR); flock(OPR,LOCK_UN); chmod(0666,"$root/$datfile"); } if( defined $cookie ) { # print $q->header(-cookie=>$cookie); print "Content-type: text/html\n\n"; } else { print "Content-type: text/html\n\n"; } open(FILEE,"$tpldir/$templ"); @strings=; close(FILEE); $cont=&getCont; for (my $i=0; $i<$#strings+1;$i++) { $strings[$i] =~s|pgcombo0|$msglines[0]|g; $strings[$i] =~s|pgcombo1|$msglines[1]|g; $strings[$i] =~s|pgcombo2|$msglines[2]|g; $strings[$i] =~s|pgcombo3|$msglines[3]|g; $strings[$i] =~s|pgcombo4|$msglines[4]|g; $strings[$i] =~s|pgcombo5|$msglines[5]|g; $strings[$i] =~s|pgcombo6|$msglines[6]|g; $strings[$i] =~s|pgcombo7|$msglines[7]|g; $strings[$i] =~s|pgcombo8|$msglines[8]|g; $strings[$i] =~s|pgcombo9|$msglines[9]|g; $strings[$i] =~s|pgCONT|$cont|g; print "$strings[$i]"; } # print "Content-type: text/html\n\n"; # print "1 = $strings[0]
"; # print "2 = $strings[1]
"; # print "3 = $strings[2]
"; } #################################### sub getCont { my (@conts,$c); open(FILE,"$htdocs/cont.html"); @conts=; close(FILE); $c=join('',@conts); return $c; } sub create_cookie { my ($q)=shift; return $q->cookie(-NAME => "opros1", -VALUE => "1", -EXPIRES=>'+1h' ); } sub get_cookie_value { my ($q)=shift; return $q->cookie(-NAME=>"opros1z"); } sub load_ip_data { my (@ip); open(IP,"$root/$ipbase"); flock(IP,LOCK_EX); @ip=; close(IP); flock(IP,LOCK_UN); map { chomp } @ip; return @ip; } sub save_ip_data { my (@ip)=@_; open(IP,">$root/$ipbase"); flock(IP,LOCK_EX); foreach (@ip) { print IP "$_\n" } close(IP); flock(IP,LOCK_UN); } sub remove_expired_ips { my (@ip,@res,$now,$t); @ip=load_ip_data(); $now=time(); foreach (@ip) { (undef,$t)=split; if( ($now - $t) > 300 ) { next; } else { push(@res,$_); } } save_ip_data(@res); } sub add_new_ip { my ($new_ip,$ip)=@_; my $str; $str="$new_ip "; $str .= time; push(@{$ip},$str); } sub ip_presents { my ($ip,@ip_data)=@_; my ($cur_ip); foreach (@ip_data) { ($cur_ip,undef)=split; return 1 if $ip eq $cur_ip; } return undef; } sub user_can_vote { my ($q)=shift; my (@ip,$user_ip,$cookie); @ip = load_ip_data(); $user_ip = $q->remote_host(); $cookie = get_cookie_value($q); if( ! defined $cookie ) { if( ! ip_presents($user_ip,@ip) ) { add_new_ip($user_ip,\@ip); save_ip_data(@ip); return create_cookie($q); } elsif( $q->cookie(-name=>"politics") eq "yes" ) { return create_cookie($q); } else { return undef; } } else { return undef; } }