#!/usr/bin/perl use DBI; require "logon.lib"; require "vars.lib"; require "site.lib"; require "countries.lib"; %keys=parse_form(); %cookies=parse_cookie(); $dbh=login(); if (defined $keys{loging}) { $cookies{login} = $keys{login}; $cookies{password} = crypt($keys{password}, $keys{login}); print "Set-Cookie: public=$keys{public}; path=/\n"; } $q_login = int($cookies{login}) - 10000; $query = "select id, password from users where id = $q_login"; $sth = $dbh->prepare($query); $sth->execute(); while ($row = $sth->fetchrow_arrayref) { $user_id = $row->[0]; $pass = $row->[1]; print "Set-Cookie: public_comp=$keys{'public'}; path=/\n"; } if ($cookies{'password'} ne crypt($pass, $cookies{'login'}) || $cookies{'password'} eq '' || $pass eq '' ) { print "Content-type: text/html\n\n"; $maintpl = open_tpl('login.tpl'); if ($keys{login} || $keys{password}) { $maintpl =~ s/<\?ERROR>/Invalid username or password/gs; } else { $maintpl =~ s/<\!\-\-ERROR\-\->.*?<\!\-\-ERROR\-\->//gs; } $maintpl =~ s/<\?LOGIN>/$cookies{login}/gs; print_tpl($maintpl,'Affiliate Program - Viaden Inc - Login'); exit; } if (!$keys{public}) { $time = time() + 9999999; $expires = ('Sunday','Monday','Tuesday','Wednesday','Thurthday','Friday','Saturday')[(gmtime($time))[6]].','. (gmtime($time))[3].'-'. ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Des')[(gmtime($time))[4]].'-'. ((gmtime($time))[5]+1900).' '. (gmtime($time))[2].':'. (gmtime($time))[1].':'. (gmtime($time))[0].' GMT'; print "Set-Cookie: login=$cookies{'login'}; expires=$expires; path=/\n"; } else { print "Set-Cookie: login=$cookies{'login'}; path=/\n"; } print "Set-Cookie: password=$cookies{'password'}; path=/\n"; #$user_id=$ENV{REMOTE_USER}-10000; ############################################################################### # STATS ############################################################################### if($keys{'mode'} eq ''){ print "Content-type: text/html\n\n"; $maintpl=open_tpl('stats.tpl'); $trtpl=open_tpl('stats_tr.tpl'); if($keys{'fromd'}){ $flag1=$flag2=0; $query="select to_days('$keys{'fromy'}-$keys{'fromm'}-$keys{'fromd'}')"; $sth=$dbh->prepare($query); $sth->execute() or print "Error:".$dbh->errstr."$query"; while($row=$sth->fetchrow_arrayref){ $flag1=$row->[0]; } $query="select to_days('$keys{'toy'}-$keys{'tom'}-$keys{'tod'}')"; $sth=$dbh->prepare($query); $sth->execute() or print "Error:".$dbh->errstr."$query"; while($row=$sth->fetchrow_arrayref){ $flag2=$row->[0]; } if(!$flag1 || !$flag2){ $keys{'fromd'}=(localtime())[3]; $keys{'fromm'}=(localtime())[4]+1; $keys{'fromy'}=(localtime())[5]+1900; $keys{'tod'}=(localtime())[3]; $keys{'tom'}=(localtime())[4]+1; $keys{'toy'}=(localtime())[5]+1900; } $datefrom=($keys{'fromy'}).'-'.sprintf("%02d",($keys{'fromm'})).'-'.sprintf("%02d",$keys{'fromd'}); $dateto=($keys{'toy'}).'-'.sprintf("%02d",($keys{'tom'})).'-'.sprintf("%02d",$keys{'tod'}); }else{ if($keys{'period'} eq 'y'){ $query="select dayofmonth(DATE_SUB(now(), INTERVAL 1 DAY)),month(DATE_SUB(now(), INTERVAL 1 DAY)),year(DATE_SUB(now(), INTERVAL 1 DAY))"; $sth=$dbh->prepare($query); $sth->execute() or print "Error:".$dbh->errstr."$query"; while($row=$sth->fetchrow_arrayref){ $keys{'fromd'}=$row->[0]; $keys{'fromm'}=$row->[1]; $keys{'fromy'}=$row->[2]; $keys{'toy'}=$row->[2]; $keys{'tom'}=$row->[1]; $keys{'tod'}=$row->[0]; } $datefrom=($keys{'fromy'}).'-'.($keys{'fromm'}).'-'.$keys{'fromd'}; $dateto=($keys{'toy'}).'-'.($keys{'tom'}).'-'.$keys{'tod'}; }elsif($keys{'period'} eq 'w'){ $query="select dayofmonth(DATE_SUB(now(), INTERVAL 7 DAY)),month(DATE_SUB(now(), INTERVAL 7 DAY)),year(DATE_SUB(now(), INTERVAL 7 DAY)),year(now()), month(now()), dayofmonth(now())"; $sth=$dbh->prepare($query); $sth->execute() or print "Error:".$dbh->errstr."$query"; while($row=$sth->fetchrow_arrayref){ $keys{'fromd'}=$row->[0]; $keys{'fromm'}=$row->[1]; $keys{'fromy'}=$row->[2]; $keys{'toy'}=$row->[3]; $keys{'tom'}=$row->[4]; $keys{'tod'}=$row->[5]; } $datefrom=($keys{'fromy'}).'-'.($keys{'fromm'}).'-'.$keys{'fromd'}; $dateto=($keys{'toy'}).'-'.($keys{'tom'}).'-'.$keys{'tod'}; }elsif($keys{'period'} eq 'm'){ $query="select dayofmonth(DATE_SUB(now(), INTERVAL 30 DAY)),month(DATE_SUB(now(), INTERVAL 30 DAY)),year(DATE_SUB(now(), INTERVAL 30 DAY)),year(now()), month(now()), dayofmonth(now())"; $sth=$dbh->prepare($query); $sth->execute() or print "Error:".$dbh->errstr."$query"; while($row=$sth->fetchrow_arrayref){ $keys{'fromd'}=$row->[0]; $keys{'fromm'}=$row->[1]; $keys{'fromy'}=$row->[2]; $keys{'toy'}=$row->[3]; $keys{'tom'}=$row->[4]; $keys{'tod'}=$row->[5]; } $datefrom=($keys{'fromy'}).'-'.($keys{'fromm'}).'-'.$keys{'fromd'}; $dateto=($keys{'toy'}).'-'.($keys{'tom'}).'-'.$keys{'tod'}; }elsif($keys{'period'} eq 'a'){ $query="select year(now()), month(now()), dayofmonth(now())"; $sth=$dbh->prepare($query); $sth->execute() or print "Error:".$dbh->errstr."$query"; while($row=$sth->fetchrow_arrayref){ $keys{'toy'}=$row->[0]; $keys{'tom'}=$row->[1]; $keys{'tod'}=$row->[2]; } $datefrom='2000-1-1'; $dateto=($keys{'toy'}).'-'.($keys{'tom'}).'-'.$keys{'tod'}; }else{ $keys{'fromd'}=(localtime())[3]; $keys{'fromm'}=(localtime())[4]+1; $keys{'fromy'}=(localtime())[5]+1900; $keys{'tod'}=(localtime())[3]; $keys{'tom'}=(localtime())[4]+1; $keys{'toy'}=(localtime())[5]+1900; $datefrom=($keys{'fromy'}).'-'.($keys{'fromm'}).'-'.$keys{'fromd'}; $dateto=($keys{'toy'}).'-'.($keys{'tom'}).'-'.$keys{'tod'}; } } $query="select date,raw,uniq from users_stat where user_id=$user_id+10000 and date>='$datefrom' and date<='$dateto' "; $sth=$dbh->prepare($query); $sth->execute() or print "Error:".$dbh->errstr."$query"; $table='';$i=0; while($row=$sth->fetchrow_hashref){ $stat2{$row->{'date'}}->{'raw'}=$row->{'raw'}; $stat2{$row->{'date'}}->{'uniq'}=$row->{'uniq'}; $stat{$row->{'date'}."|-"}->{'services'}=0; $stat{$row->{'date'}."|-"}->{'contacts'}=0; $stat{$row->{'date'}."|-"}->{'earned'}=0; $stat{$row->{'date'}."|-"}->{'cost'}=0; $stat{$row->{'date'}."|-"}->{'uniq'}=$row->{'raw'}; $stat{$row->{'date'}."|-"}->{'raw'}=$row->{'uniq'}; } $query="select date_format(date,'%Y-%m-%d') as udate, sum(order_type = 0) as contacts, sum(order_type = 1) as services, sum(ref_earned) as earned, sum(cost) as cost, product from orders where referer_id=$user_id+10000 and to_days(date)>=to_days('$datefrom') and to_days(date)<=to_days('$dateto') group by date_format(date,'%m/%d/%Y'), product "; $sth=$dbh->prepare($query); $sth->execute() or print "Error:".$dbh->errstr."$query"; $table='';$i=0; while($row=$sth->fetchrow_hashref){ $stat{$row->{'udate'}."|".$row->{'product'}}->{'services'}=$row->{'services'}; $stat{$row->{'udate'}."|".$row->{'product'}}->{'contacts'}=$row->{'contacts'}; $stat{$row->{'udate'}."|".$row->{'product'}}->{'earned'}=$row->{'earned'}; $stat{$row->{'udate'}."|".$row->{'product'}}->{'cost'}=$row->{'cost'}; $stat{$row->{'udate'}."|".$row->{'product'}}->{'uniq'}=$stat2{$row->{'udate'}}->{'uniq'}; $stat{$row->{'udate'}."|".$row->{'product'}}->{'raw'}=$stat2{$row->{'udate'}}->{'raw'}; } $tcost = 0; $tearn = 0; foreach $k (sort {$a cmp $b} keys %stat){ $tr=$trtpl; $class=(($i++)%2==0)?'tline1':'tline2'; $tr=~s/<\?CLASS>/$class/gs; ($dt,$pr) = split /\|/, $k; @date=split /-/,$dt; $tr=~s/<\?DATE>/$date[2]\/$date[1]\/$date[0]/gs; $traffic=sprintf("%d",$stat{$k}->{'uniq'}).'('.sprintf("%d",$stat{$k}->{'raw'}).')'; $tr=~s/<\?TRAFFIC>/$traffic/gs; $contacts=sprintf("%d",$stat{$k}->{'contacts'}); $tr=~s/<\?CONTACTS>/$contacts/gs; $services=sprintf("%d",$stat{$k}->{'services'}); $tr=~s/<\?SERVICES>/$services/gs; $tearn += $stat{$k}->{'earned'}; $earned=sprintf("%.02f",$stat{$k}->{'earned'}); $tr=~s/<\?EARNED>/$earned/gs; $tcost += $stat{$k}->{'cost'}; $cost=sprintf("%.02f",$stat{$k}->{'cost'}); $tr=~s/<\?COST>/$cost/gs; $tr=~s/<\?PRODUCT>/$pr/gs; $table.=$tr; } if($table eq ''){$table="