#!/usr/bin/perl -w # this is meant to be run as an Apache::Registry script use RSI::Funcs; use RSI::SQL; use RSCT::Programs; use RSCT::Session; use RSCT::DBAccount; #use Geo::IP; use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); use DBI (); use Apache::Cookie; use vars qw($MAX_TRIES $ID_LENGTH $DO_DISCONNECT); use strict; my $q = new CGI; BEGIN { $main::debug = 0; $DO_DISCONNECT = 0; # disconnect when done? $MAX_TRIES = 10; # max tries to create a session_id $ID_LENGTH = 8; # length of the session ID } { my %siteurl; my %urlsite; my %firstpage; my $error = ""; my $fatal = 0; # if we don't already have the siteurl info loaded, then if (!%siteurl) { my ($sth) = RSI::SQL::exec("select site,url,prefix,firstpage from sites"); while (my ($site,$url,$prefix,$firstpage) = $sth->fetchrow_array) { $firstpage ||= "index.html"; $firstpage{$site} = $firstpage; $siteurl{$site} = $url; $siteurl{$site} =~ /^www/ or $siteurl{$site} = "$prefix.$url"; $siteurl{$site} = "http://$siteurl{$site}"; $url =~ s|http://||; $url !~ /^www/ and $url = "$prefix.$url"; $urlsite{$url} = $site; } } # my $gi = Geo::IP->new(GEOIP_STANDARD); # my $country = $gi->country_code_by_addr($ENV{REMOTE_ADDR}); # if ($main::config{sitename} eq "adultcash") { # if ($country eq "GB") { # print "Location: http://www.erosconnect.com/bnr/3050857320\n\n"; # print STDERR "uk redirect\n"; # exit; # } # } my $r = Apache->request; my $html_page = $q->param('j'); $html_page ||= $q->param('landpage'); if ($html_page eq "tour/index.php" or $html_page eq 'join.php') { $html_page = ""; } my $account = $q->param("account"); $account ||= $q->param("a"); $account ||= $main::config{default_account}; # DB20060112 $account ||= ""; my $db_account = RSCT::DBAccount->load($account); if (defined $db_account and $db_account->status eq "disabled") { print "Content-Type: text/html\n\n"; print "This account ($account) is disabled. Sorry!\n"; exit; } my $orig_account = $account; $account =~ s/amp;//; my $site = param("site"); $site ||= param("s"); $site ||= ""; $site =~ s/amp;//; if ($site !~ /^\d+$/) { ($site) = RSI::SQL::one_row("select site from sites where site_abbr=?", $site); } else { $site = $site+0; } if ($main::config{sitename} eq "mpb") { if ($site eq "6") { $site = "171"; } elsif ($site eq "129") { $site = "172"; } elsif ($site eq "148") { $site = "171"; } elsif ($site eq "150") { $site = "172"; } elsif ($site eq "47") { $site = "171"; } elsif ($site eq "68") { $site = "172"; } elsif ($site eq "50") { $site = "20"; } elsif ($site eq "75") { $site = "119"; } } if ($main::config{sitename} eq "adultcash") { if ($site eq "5") { $site = "14"; } elsif ($site eq "10") { $site = "14"; } elsif ($site eq "11") { $site = "14"; } elsif ($site eq "12") { $site = "14"; } elsif ($site eq "42") { $site = "14"; } elsif ($site eq "84") { $site = "81"; } elsif ($site eq "94") { $site = "81"; } elsif ($site eq "95") { $site = "81"; } elsif ($site eq "96") { $site = "81"; } elsif ($site eq "93") { $site = "81"; } elsif ($site eq "140") { $site = "137"; } elsif ($site eq "150") { $site = "137"; } elsif ($site eq "151") { $site = "137"; } elsif ($site eq "152") { $site = "137"; } elsif ($site eq "149") { $site = "137"; } } if (!$site) { my $server_name = $ENV{SERVER_NAME} || ""; if ($urlsite{$server_name}) { $site = $urlsite{$server_name}; } } $site ||= ""; my $program = param("program"); $program ||= param("p"); $program ||= $main::config{default_program}; $program =~ s/amp;//; if ($program !~ /^\d+$/) { # this is for adultcash migration. $program eq "s" and $program = "persignup"; $program eq "u" and $program = "perunique"; $program eq "p" and $program = "perunique"; ($program) = RSI::SQL::one_row("select program from programs where program_abbr=?", $program); } # check if the program and site combo are valid. my ($priv_allowed) = RSI::SQL::one_row("select status from privs where programID=? and siteID=?",$program,$site); # DB20070919: check if the program and account combo are valid. my ($wm_priv_allowed) = RSI::SQL::one_row("select status from webmaster_privs where programID = ? and accountID in (?,0) order by accountID desc limit 1", $program, $account); # if not then assign default program if (not ($priv_allowed and $wm_priv_allowed)) { $program = $main::config{default_program}; } my $banner = param("banner"); $banner ||= param("b"); $banner ||= ""; $banner =~ s/amp;//; # DB20070418: Check if this landing page is allowed my $db_site = RSCT::DBSite->load($site); my @pages = $db_site->landing_pages($account); my $allowed_landing_page = 0; foreach my $page (@pages) { my $path = $page->page; $path =~ s/\*/\.\*/g; if ($html_page =~ /$path/) { $allowed_landing_page = 1; last; } } if (not $allowed_landing_page) { $html_page = ''; } # DB20051109: get the campaign code. We assume it's valid and don't # otherwise check, because we'll accept the click regardless. my $campaign = $q->param("campaign") || $q->param("c") || ""; $campaign =~ s/amp;//; printf STDERR "DB20051209: Campaign received by click is: %s\n", $campaign; my $env_http_referer = $ENV{HTTP_REFERER} || ""; my $env_server_name = $ENV{SERVER_NAME} || ""; my $env_request_uri = $ENV{REQUEST_URI} || ""; $r->header_in("Referer",""); # this is weird, but it seems to stick around sometimes when it shouldn't, so i clear it $ENV{HTTP_REFERER} = ""; if ($account && $account !~ /^[0-9]{6,6}$/) { my $sth = $main::dbh->prepare("select account from accounts where username=?"); $sth->execute($account); ($account) = $sth->fetchrow_array; $sth->finish; } ($account =~ /\d+/) and $account = sprintf("%08d", $account); ($account !~ /^[0-9]{6,8}$/) and $account = sprintf("%08d", $account); if ($account !~ /^[0-9]{6,8}$/) { print STDERR "impossible account:\n"; printf STDERR "\$account %s\n",$account; printf STDERR "found %s\n",$orig_account; printf STDERR "account %s\n",param("account") || 'no account'; printf STDERR "a %s\n",param("a") || 'a'; printf STDERR "uri %s\n",$env_request_uri; printf STDERR "query %s\n",$ENV{QUERY_STRING}; printf STDERR "redirect %s\n",$ENV{REDIRECT_QUERY_STRING}||""; #printf STDERR "mod_perl %s\n",Apache->request->args; printf STDERR "method %s\n",$ENV{REQUEST_METHOD}; printf STDERR "referer %s\n",$env_http_referer; $error = "impossible account (account=$account,referer=$env_http_referer,uri=http://$env_server_name/$env_request_uri)"; $fatal = 0; } elsif (length $banner>60) { $error = "banner tracking code was greater than 60 characters (length: ".length($banner). ") (account=$account,referer=$env_http_referer,uri=http://$env_server_name$env_request_uri)"; $fatal = 0; } elsif (!exists($siteurl{$site})) { $error = "site doesn't exist (site=$site,account=$account,referer=$env_http_referer,uri=http://$env_server_name$env_request_uri)"; $fatal = 1; } #elsif (! RSCT::Accounts::account_exists($account)) { # $error = "account doesn't exists (account=$account,referer=$env_http_referer,uri=http://$env_server_name$env_request_uri)"; # $fatal = 0; #} if ( $error && $fatal) { print < Error An error occurred. Check your linking code and try again.

$error

Account = $account
Site = $site
Program = $program
Banner = $banner
EOM RSI::Funcs::log("click","Invalid click was sent fatal, $error"); exit; } elsif ($error) { RSI::Funcs::log("click","Invalid click was sent, $error"); my $siteurl = $siteurl{$site} || ""; $siteurl =~ s|^http://www2\.|http://www\.|; $siteurl =~ s|^http://www3\.|http://www\.|; my $firstpage = ""; $firstpage{$site} and $firstpage = $firstpage{$site}; $firstpage ||= "index.html"; print redirect_url("$siteurl/$firstpage"); $DO_DISCONNECT and $main::dbh->disconnect; exit(0); } else { my $session = new RSCT::Session; print STDERR "DB20051209: click is creating new session object. Campaign is $campaign.\n"; $session->create({ account => $account, site => $site, program => $program, banner => $banner, referrer => $env_http_referer, ip => $ENV{REMOTE_ADDR}, campaignID => $campaign, }); my $siteurl = $siteurl{$site}; # Default URL to send them to # Megapornbuck specific changes if (!$html_page) { if (my $temp = $q->param('t')) { my $secure_url = $main::config{siteurl}; $secure_url =~ s/http\:\/\///gis; printf STDERR "DB20060119: Redirecting to $secure_url/cgi/jp.cgi, with session %d\n", $session->id; print redirect_url(sprintf "https://$secure_url/cgi/jp.cgi?s=%s&t=$temp",$session->id); return 0; } } $html_page ||= $firstpage{$site}; my $redir_url = sprintf "$siteurl/%s/$html_page",$session->id; print STDERR "DB20060119: Will redirect to $redir_url\n"; # Mainstreamdollars redirects if ($main::config{sitename} eq "mainstreamdollar") { die "We are here for some reason"; my $orig_url = $redir_url; my $session_id = $session->id; # JM: processing for non-hosted sites on mainstreamdollars. We're passing these # through into another person's affiliate system and getting notification on sales. if ($site == 5) { $redir_url = "http://rdr.primegra.com/p/10000510830?sourceid=$session_id"; } elsif ($site == 6) { $redir_url = "http://int.dvd.freecooloffers.com/p/10000515355?sourceid=$session_id"; } elsif ($site == 7) { $redir_url = "http://lnk.freecooloffers.com/p/10000515356?sourceid=$session_id"; } elsif ($site == 8) { $redir_url = "http://rdr.moneypluscard3.com/p/10000510828?sourceid=$session_id"; } elsif ($site == 9) { $redir_url = "http://rdr.usaplatinumplus.com/p/10000510829?sourceid=$session_id"; } elsif ($site == 10) { $redir_url = "http://rdr.usaplatinumcard.com/p/10000510831?sourceid=$session_id"; } elsif ($site == 11) { $redir_url = "http://rdr.freecardsearch.com/p/10000510833?sourceid=$session_id"; } elsif ($site == 12) { $redir_url = "http://rdr.cupidjunctionapp.com/p/10000510832?sourceid=$session_id"; } # JM: normally we record clicks in Session.pm, but these sites aren't mirrored if ($redir_url ne $orig_url) { my $sth = $main::dbh->prepare("select raw from clicks where session_id='$session_id'"); my $rv = $sth->execute; $sth->finish; #print "rv=$rv
\n"; if ($rv == 1) { $main::dbh->do("update clicks set raw=raw+1,second=1 where session_id='$session_id'"); } else { my $referer = $ENV{HTTP_REFERER} || ""; $rv = $main::dbh->do("insert into clicks set account='$account',real_referer='$referer',referer='$referer',ip='$ENV{REMOTE_ADDR}',site='$site',program='$program',datetime=now(),date=now(),raw=1,second=1,banner='$banner',session_id='$session_id',processed='no',campaignID='$campaign'"); } } } # JM: for karups # DB20060724: Only save a cookie if associated with an affiliate. if ($account) { #print STDERR "DB20060724: click, from account $account, write cookie.\n"; my $shorturl = $siteurl; $shorturl =~ s/http:\/\///; my $cookie_account = $account; length($cookie_account) > 6 and $cookie_account =~ s/^00//; my $cookie_value = $session->id."-$cookie_account-$program"; my $cookie = Apache::Cookie->new($r, -domain => $shorturl, -name => "karups-rsct-$site", -value => $cookie_value, -expires => '+6M', -path => '/'); print STDERR "click: setting cookie: '",$cookie->as_string,"'\n"; $cookie->bake(); } print redirect_url($redir_url); return 0; } } $SIG{__WARN__} = sub{ }; sub redirect_url { my ($location) = @_; return "Location: $location\n\n"; }