#!/usr/local/bin/perl
$mail_prog = '/usr/sbin/sendmail' ;
&GetFormInput;
$login = $field{'login'} ;	 
$adv = $field{'adv'} ;	 
$pass=$field{'pass'};

	 
	 sub GetFormInput {

	(*fval) = @_ if @_ ;

	local ($buf);
	if ($ENV{'REQUEST_METHOD'} eq 'POST') {
		read(STDIN,$buf,$ENV{'CONTENT_LENGTH'});
	}
	else {
		$buf=$ENV{'QUERY_STRING'};
	}
	if ($buf eq "") {
			return 0 ;
		}
	else {
 		@fval=split(/&/,$buf);
		foreach $i (0 .. $#fval){
			($name,$val)=split (/=/,$fval[$i],2);
			$val=~tr/+/ /;
			$val=~ s/%(..)/pack("c",hex($1))/ge;
			$name=~tr/+/ /;
			$name=~ s/%(..)/pack("c",hex($1))/ge;

			if (!defined($field{$name})) {
				$field{$name}=$val;
			}
			else {
				$field{$name} .= ",$val";
				
			}


		   }
		}
return 1;
}

#print FILE @m;
use Fcntl;
use NDBM_File;

	tie %hasht, "NDBM_File", "/web/012/wmclicks/public_html/members/logs/$login", O_RDWR, 0644;
	$pass = $hasht{'pass'};
	untie %hasht;
	
	open (FILE,"/web/012/wmclicks/public_html/members/backup/$login.dat");
	@f=<FILE>;
	close (FILE);
	
	chomp ($f[0]);
	
	$url="http://wmclicks.com/cgi-bin/links/begin.pl?login=$login&adv=$adv&pass=$pass";
	
	if ($ENV{'HTTP_REFERER'} eq $url and $f[0] eq $pass)
	{


$sit=1;
until ($sit==0)
{
open (FILE,"/web/012/wmclicks/public_html/members/work/$login.dat");
@f=<FILE>;
close (FILE);
chomp ($f[0]);
$sit=$f[0];
}

open (FILE,">/web/012/wmclicks/public_html/members/work/$login.dat");
flock (FILE,$LOCK_EX);
print FILE "1\n";
flock (FILE,$LOCK_UN);
close (FILE);


#Changing time of the last click 
open (FILE,"/web/012/wmclicks/public_html/members/tasks/$adv/$login.dat");
@mass=<FILE>;
close (FILE);
$ft=@mass[0];
$fn=time;
$r=$fn-$ft;

#Checking money

tie %hashg, "NDBM_File", "/web/012/wmclicks/public_html/adv/money/$adv", O_RDWR, 0644;
$summ = $hashg{summ};
untie %hashg;

	#Tunning adertiser
	tie %hasht, "NDBM_File", "/web/012/wmclicks/public_html/adv/tun/$adv", O_RDWR, 0644;
	$number=$hasht{number};
	$box = $hasht{box};
	$day = $hasht{day};
	untie %hasht;

	#Today clicks advertiser
	open (FILE,"/web/012/wmclicks/public_html/adv/today/$adv.dat");
	@gov=<FILE>;
	close (FILE);
	chomp ($gov[0]);
		
	if ($number == 0)
	{
	$number=10000;
	}


open (FILE,"/web/012/wmclicks/public_html/admin/ips/$adv.dat");
@sa=<FILE>;
close (FILE);

$iptest=0;
foreach $tr (@sa)
{
chomp ($tr);

	if ($tr eq $ENV{'REMOTE_ADDR'})
	{
	$iptest=1;
	}
}



	#Checking DATE
	open (FILE,"/web/012/wmclicks/public_html/admin/date.dat");
	@df=<FILE>;
	close (FILE);
	chomp ($df[0]);

	$dok=0;
	#Day
		@days=split (/,/,$day);
		foreach $k (@days)
		{
			if ($df[0]==$k)
			{
			$dok=1;
			} 
		}	
	

#Checking block-list from foreman
#if ($adv eq "foreman")
#}
open (FILE,"/web/012/wmclicks/public_html/adv/block/block.txt");
@gh=<FILE>;
close (FILE);

	$bl=0;
	foreach $fx (@gh)
	{
	chomp ($fx);
		if ($fx eq $ENV{'REMOTE_ADDR'})
		{
		$bl=1;
		} 
	}
#}


if ($r>86400 and $summ>0 and $number>$gov[0] and $iptest==0 and $dok==1 and $bl==0)
{
#Removing money from advertisers account
$summ=$summ-0.005;


unlink ("/web/012/wmclicks/public_html/adv/money/$adv.pag");
unlink ("/web/012/wmclicks/public_html/adv/money/$adv.dir");

tie %hash, "NDBM_File", "/web/012/wmclicks/public_html/adv/money/$adv", O_RDWR|O_CREAT|O_EXCL, 0644;
$hash{summ}=$summ;
untie %hash;


#Adding one click to the adv's ballance
open (FILE,"/web/012/wmclicks/public_html/adv/today/$adv.dat");
@gg=<FILE>;
close (FILE);

chomp ($gg[0]);
$gg[0]=$gg[0]+1;

open (FILE,">/web/012/wmclicks/public_html/adv/today/$adv.dat");
print FILE "$gg[0]\n";
close (FILE);


}


if ($r>86400 and $summ>0 and $number>$gov[0] and $iptest==0 and $dok==1 and $bl==0)
{
open (FILE,">/web/012/wmclicks/public_html/members/tasks/$adv/$login.dat");
print FILE time;
close (FILE);

open (FILE,"/web/012/wmclicks/public_html/members/tasks/$adv/$login.dat");
@list=<FILE>;
close (FILE);

#Adding money to users account
$usfile="/web/012/wmclicks/public_html/members/logs/$login.pag";
$usfiles="/web/012/wmclicks/public_html/members/logs/$login.dir";


tie %hashd, "NDBM_File", "/web/012/wmclicks/public_html/members/logs/$login", O_RDWR, 0644;
$pass=$hashd{pass};
$nick=$hashd{nick};
$mail=$hashd{mail};
$z=$hashd{z};
$csumm=$hashd{csumm};
$psumm=$hashd{psumm};
$refnum=$hashd{refnum};
$referer=$hashd{referer};
untie %hashd;

if ($adv eq "getmoney")
{
$csumm=$csumm+0.004;
}

else

{
$csumm=$csumm+0.003;
}

unlink ($usfile);
unlink ($usfiles);


tie %hash, "NDBM_File", "/web/012/wmclicks/public_html/members/logs/$login", O_RDWR|O_CREAT|O_EXCL, 0644;
$hash{pass} = $pass;
$hash{nick} = $nick;
$hash{mail} = $mail;
$hash{csumm} = $csumm;
$hash{psumm} = $psumm;
$hash{refnum} = $refnum;
$hash{z} = $z;
$hash{referer} = $referer;
untie %hash;


#Adding IP address to the base
open (FILE,">>/web/012/wmclicks/public_html/admin/ips/$adv.dat");
print FILE "$ENV{'REMOTE_ADDR'}\n";
close (FILE);
#Adding users logins to the base
open (FILE,">>/web/012/wmclicks/public_html/admin/logs/$adv.dat");
print FILE "$login\n";
close (FILE);


#Adding money to users referrer
$rfile="/web/012/wmclicks/public_html/members/logs/$referer.pag";
$rfiles="/web/012/wmclicks/public_html/members/logs/$referer.dir";



if (-e $rfile)
{
tie %hashd, "NDBM_File", "/web/012/wmclicks/public_html/members/logs/$referer", O_RDWR, 0644;
$pass=$hashd{pass};
$nick=$hashd{nick};
$mail=$hashd{mail};
$z=$hashd{z};
$csumm=$hashd{csumm};
$psumm=$hashd{psumm};
$refnum=$hashd{refnum};
$rf=$hashd{referer};
untie %hashd;

if ($adv eq "getmoney")
{
$csumm=$csumm+0.0004;
}

else

{
$csumm=$csumm+0.0003;
}


unlink ($rfile);
unlink ($rfiles);

tie %hashr, "NDBM_File", "/web/012/wmclicks/public_html/members/logs/$referer", O_RDWR|O_CREAT|O_EXCL, 0644;
$hashr{pass} = $pass;
$hashr{nick} = $nick;
$hashr{mail} = $mail;
$hashr{csumm} = $csumm;
$hashr{psumm} = $psumm;
$hashr{refnum} = $refnum;
$hashr{z} = $z;
$hashr{referer} = $rf;
untie %hashr;

}


open (FILE,">/web/012/wmclicks/public_html/members/work/$login.dat");
flock (FILE,$LOCK_EX);
print FILE "0\n";
flock (FILE,$LOCK_UN);
close (FILE);

#Redirecting user to adveriser's page
print "Content-Type: text/html\n\n";
print "<HTML><BODY BACKGROUND=http://wmclicks.com/bg.gif>";
print "<CENTER><FONT SIZE=4 FACE=Tahoma COLOR=#008000>Деньги зачислены на Ваш счет!</FONT></CENTER>";
print "<center>";

print "<!-- Russian LinkExchange code START (468x60)-->\n";
print "  <iframe src=http://www.linkexchange.ru/cgi-bin/erle.cgi?28805?44 \n";
print "  frameborder=0 vspace=0 hspace=0 width=468 height=60 \n";
print "  marginwidth=0 marginheight=0 scrolling=no>\n";
print "  <a href=http://www.linkexchange.ru/users/028805/goto.map target=_top> \n";
print "  <img src=http://www.linkexchange.ru/cgi-bin/rle.cgi?28805?44 \n";
print "  alt=\"RLE Banner Network\" border=0 height=60 width=468></a> \n";
print "  </iframe> \n";
print "<!-- Russian LinkExchange code END --> \n";

print "</center>";
print "</BODY></HTML>";




}

else

#Showing message if there are some errors
{
	open (FILE,">/web/012/wmclicks/public_html/members/work/$login.dat");
	flock (FILE,$LOCK_EX);
	print FILE "0\n";
	flock (FILE,$LOCK_UN);
	close (FILE);

	if ($iptest==1 or $bl==1)
	{
	open (FILE,">>/web/012/wmclicks/public_html/admin/secure.dat");
	print FILE "$login\n";
	close (FILE);
	
	print "Content-Type: text/html\n\n";
	print "<HTML><BODY BACKGROUND=http://wmclicks.com/bg.gif>";
	print "<CENTER><FONT SIZE=4 FACE=Tahoma COLOR=#008000>С Вашего IP адреса сегодня уже кликали по этой ссылке.</FONT></CENTER>";
print "<center>";

print "<!-- Russian LinkExchange code START (468x60)-->\n";
print "  <iframe src=http://www.linkexchange.ru/cgi-bin/erle.cgi?28805?44 \n";
print "  frameborder=0 vspace=0 hspace=0 width=468 height=60 \n";
print "  marginwidth=0 marginheight=0 scrolling=no>\n";
print "  <a href=http://www.linkexchange.ru/users/028805/goto.map target=_top> \n";
print "  <img src=http://www.linkexchange.ru/cgi-bin/rle.cgi?28805?44 \n";
print "  alt=\"RLE Banner Network\" border=0 height=60 width=468></a> \n";
print "  </iframe> \n";
print "<!-- Russian LinkExchange code END --> \n";

print "</center>";
	print "</BODY></HTML>";
	}
	else
	{
	print "Content-Type: text/html\n\n";
	print "<HTML><BODY BACKGROUND=http://wmclicks.com/bg.gif>";
	print "<CENTER><FONT SIZE=4 FACE=Tahoma COLOR=#008000>Деньги на Ваш счет не были зачислены, так как сегодня вы уже кликали по этой ссылке</FONT></CENTER>";
print "<center>";

print "<!-- Russian LinkExchange code START (468x60)-->\n";
print "  <iframe src=http://www.linkexchange.ru/cgi-bin/erle.cgi?28805?44 \n";
print "  frameborder=0 vspace=0 hspace=0 width=468 height=60 \n";
print "  marginwidth=0 marginheight=0 scrolling=no>\n";
print "  <a href=http://www.linkexchange.ru/users/028805/goto.map target=_top> \n";
print "  <img src=http://www.linkexchange.ru/cgi-bin/rle.cgi?28805?44 \n";
print "  alt=\"RLE Banner Network\" border=0 height=60 width=468></a> \n";
print "  </iframe> \n";
print "<!-- Russian LinkExchange code END --> \n";

print "</center>";
	print "</BODY></HTML>";
	}
}




	}
#URL check
	else
	{
	print "Content-Type: text/html\n\n";
	print "<HTML><BODY BACKGROUND=http://wmclicks.com/bg.gif>";
	print "<CENTER>$ENV{'HTTP_REFERER'}</CENTER>";
	print "</BODY></HTML>";
	}

	
