#!/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="No any traffic for this period"} $maintpl=~s/<\?STATS>/$table/gs; $tcost = sprintf("%.02f",$tcost); $maintpl=~s/<\?TCOST>/$tcost/gs; $tearn = sprintf("%.02f",$tearn); $maintpl=~s/<\?TEARN>/$tearn/gs; for($i=1;$i<32;$i++){ if($i==$keys{'fromd'}){$sel='selected';}else{$sel='';} $date.=""; } $maintpl=~s/<\?FROMD>/$date/gs; $date=''; for($i=1;$i<32;$i++){ if($i==$keys{'tod'}){$sel='selected';}else{$sel='';} $date.=""; } $maintpl=~s/<\?TOD>/$date/gs; $date=''; for($i=1;$i<13;$i++){ if($i==$keys{'fromm'}){$sel='selected';}else{$sel='';} $date.=""; } $maintpl=~s/<\?FROMM>/$date/gs; $date=''; for($i=1;$i<13;$i++){ if($i==$keys{'tom'}){$sel='selected';}else{$sel='';} $date.=""; } $maintpl=~s/<\?TOM>/$date/gs; $date=''; for($i=2001;$i<(localtime())[5]+1901;$i++){ if($i==$keys{'fromy'}){$sel='selected';}else{$sel='';} $date.=""; } $maintpl=~s/<\?FROMY>/$date/gs; $date=''; for($i=2001;$i<(localtime())[5]+1901;$i++){ if($i==$keys{'toy'}){$sel='selected';}else{$sel='';} $date.=""; } $maintpl=~s/<\?TOY>/$date/gs; print_tpl($maintpl, 'Affiliate Program - Viaden Inc - Stats'); } ############################################################################### # EDIT PROFILE ############################################################################### elsif($keys{'mode'} eq 'edit_profile'){ print "Content-type: text/html\n\n"; $maintpl=open_tpl('edit_profile.tpl'); $query="select * from users where id=$user_id"; $sth=$dbh->prepare($query); $sth->execute() or print "Error:".$dbh->errstr."$query"; while($row=$sth->fetchrow_hashref){ $keys{'name'}=$row->{'name'}; $keys{'address'}=$row->{'address'}; $keys{'city'}=$row->{'city'}; $keys{'state'}=$row->{'state'}; $keys{'zip'}=$row->{'zip'}; $keys{'email'}=$row->{'email'}; $keys{'phone'}=$row->{'phone'}; $keys{'website'}=$row->{'web_site'}; $keys{'bankname'}=$row->{'bank_name'}; $keys{'bankaddress'}=$row->{'bank_address'}; $keys{'bankswift'}=$row->{'bank_swift'}; $keys{'bankaccount'}=$row->{'bank_account'}; $keys{'corbankname'}=$row->{'cor_bank_name'}; $keys{'corbankaddress'}=$row->{'cor_bank_address'}; $keys{'corbankswift'}=$row->{'cor_bank_swift'}; $keys{'corbankaccount'}=$row->{'cor_bank_account'}; $keys{'contactname'}=$row->{'contact_name'}; $keys{'country'}=$row->{'country'}; } $select=''; for $k (@countries){ if($keys{'country'} eq $k){$sel='selected';}else{$sel='';} $select.=""; } $maintpl=~s/<\?COUNTRIES>/$select/gs; print_tpl($maintpl, 'Affiliate Program - Viaden Inc - Edit Profile'); } ############################################################################### # MAKE EDIT PROFILE ############################################################################### elsif($keys{'mode'} eq 'make_edit_profile'){ $flag=0; # print "Content-type: text/html\n\n"; if(!$keys{'name'}){$err.='Please enter your name';$flag=1;} if(!$keys{'address'}){$err.='Please enter your Street Address';$flag=1;} if(!$keys{'city'}){$err.='Please enter your city';$flag=1;} if(!$keys{'zip'}){$err.='Please enter your postal code';$flag=1;} if($keys{'country'} eq ''){$err.='Please choose your country';$flag=1;} if(($keys{'email'} !~ /^(\S+)\@(\S+)\.(\S+)$/) || (($1.$2.$3) =~ /@/)){$err.='Please enter your email correctly';$flag=1;} if(!$keys{'phone'}){$err.='Please enter your phone';$flag=1;} if($flag){ print "Content-type: text/html\n\n"; $maintpl=open_tpl('edit_profile.tpl'); $maintpl=~s/<\?ERROR>/$err/gs; foreach $k (@countries){ if($keys{'country'} eq $k){$sel='selected';}else{$sel='';} $select.=""; } $maintpl=~s/<\?COUNTRIES>/$select/gs; print_tpl($maintpl, 'Affiliate Program - Viaden Inc - Edit Profile'); exit; } $query='update users set name="'.prepare_to_db($keys{'name'}).'", address="'.prepare_to_db($keys{'address'}).'", city="'.prepare_to_db($keys{'city'}).'", state="'.prepare_to_db($keys{'state'}).'", zip="'. prepare_to_db($keys{'zip'}).'", country='. $dbh->quote($keys{'country'}).', email="'. prepare_to_db($keys{'email'}).'", phone="'. prepare_to_db($keys{'phone'}).'", web_site="'. prepare_to_db($keys{'website'}).'", bank_name="'.prepare_to_db($keys{'bankname'}).'", bank_address="'. prepare_to_db($keys{'bankaddress'}).'", bank_swift="'. prepare_to_db($keys{'bankswift'}).'", bank_account="'. prepare_to_db($keys{'bankaccount'}).'", cor_bank_name="'. prepare_to_db($keys{'corbankname'}).'", cor_bank_address="'. prepare_to_db($keys{'corbankaddress'}).'", cor_bank_swift="'. prepare_to_db($keys{'corbankswift'}).'", cor_bank_account="'. prepare_to_db($keys{'corbankaccount'}).'", contact_name="'. prepare_to_db($keys{'contactname'}).'", date=date'. " where id=$user_id"; $sth=$dbh->prepare($query); $sth->execute() or print "Error:".$dbh->errstr."$query"; print "Location: http://$our_site/cgi-bin/index.cgi?mode=edit_profile\n\n"; } ############################################################################### # ACCOUNT ############################################################################### elsif($keys{'mode'} eq 'account'){ print "Content-type: text/html\n\n"; $maintpl=open_tpl('account.tpl'); $trtpl=open_tpl('account_tr.tpl'); $query="select account as account from users where id=$user_id"; $sth=$dbh->prepare($query); $sth->execute() or print "Error:".$dbh->errstr."$query"; while($row=$sth->fetchrow_hashref){ $account_now=$row->{'account'}; } $query="select * from account where user_id=$user_id"; $sth=$dbh->prepare($query); $sth->execute() or print "Error:".$dbh->errstr."$query"; $i=0; while($row=$sth->fetchrow_hashref){ $class=(($i++)%2==0)?'tline1':'tline2'; $tr=$trtpl; $tr=~s/<\?CLASS>/$class/gs; $tr=~s/<\?DATE>/$row->{'date'}/gs; $ammount=sprintf("%.02f",$row->{'amount'}); $tr=~s/<\?AMOUNT>/$ammount/gs; $table.=$tr; } if($table eq ''){$table="No any payments yet"} if($account_now){ $table.='Now on your account:$'.sprintf("%.02f",$account_now).''; } $maintpl=~s/<\?STATS>/$table/gs; print_tpl($maintpl, 'Affiliate Program - Viaden Inc - Payouts'); } ############################################################################### # VIEW REFERERS ############################################################################### elsif($keys{'mode'} eq 'view_referers'){ print "Content-type: text/html\n\n"; $maintpl=open_tpl('referers.tpl'); $trtpl=open_tpl('referers_tr.tpl'); $query="select * from referers where user_id=$user_id+10000 order by col desc"; $sth=$dbh->prepare($query); $sth->execute() or print "Error:".$dbh->errstr."$query"; $table='';$i=0; while($row=$sth->fetchrow_hashref){ $class=(($i++)%2==0)?'tline1':'tline2'; $tr=$trtpl; $tr=~s/<\?CLASS>/$class/gs; $page=substr($row->{'page'},0,100); $tr=~s/<\?PAGE>/$page/gs; $tr=~s/<\?COL>/$row->{'col'}/gs; $table.=$tr; } if($table eq ''){$table="No referers pages yet"} $maintpl=~s/<\?STATS>/$table/gs; $maintpl=~s/<\?base_site_url>/$our_site\//gs; print $maintpl; } ############################################################################### # GET CODE ############################################################################### elsif($keys{'mode'} eq 'get_code'){ print "Content-type: text/html\n\n"; $maintpl=open_tpl('banners.tpl'); $user_id+=10000; $maintpl=~s~<\?UID>~$user_id~gs; print_tpl($maintpl, 'Affiliate Program - Viaden Inc - Banner Farm'); } else{ print "Location: http://$our_site/\n\n"; } exit;