#!/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"; #/usr/local/www/cgi-bin/www.gazeta.ru"; #/usr/local/www/data/www.gazeta.ru"; #$root="/tmp"; #$htdocs="/tmp"; $datfile="opros.dat"; $comber="combo"; $templ="opros.html"; $ipbase="opros.ip"; 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");flock(NUMZ,LOCK_EX); @msglines = ; map { s/\n//g; } @msglines; close(NUMZ);flock(NUMZ,LOCK_UN); #if( defined $cookie ) { $number = $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(0644,"$root/$datfile"); # } # if( defined $cookie ) { # print $q->header(-cookie=>$cookie); # } else { print "Content-type: text/html\n\n"; # } open(FILEE,"$htdocs/$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]"; } } #################################### 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 => "opros", -VALUE => "1", -EXPIRES=>'+1h' ); } sub get_cookie_value { my ($q)=shift; return $q->cookie(-NAME=>"opros"); } 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) > 3600 ) { 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; } }