All pastes #966409 Raw Edit

Untitled

public text v1 · immutable
·published 2008-04-01 18:41 UTC
rendered paste body
<?php

//CONFIG
require_once('/mypublisher/reports/php_common/MyPubConfig.php');
require_once('/mypublisher/reports/php_common/db.php');
$squid = new MyPubConfig("ks_squid.conf"); //$q


//GLOBALS
$dir_assoc = "/data/sites/www/BookMaker/binaries";
$ipNumber = 2; //this is the cutoff number of downloads per IP


//get date to run
$opts = getopt('d:');
        if(isset($opts['d'])){
                $today = $opts['d'];
                $today_array = explode("-",$today);
                $tomorrow_st = mktime(0,0,0,$today_array[1],$today_array[2],$today_array[0]);
                $tomorrow = date("Y-m-d", ($tomorrow_st + (60*60*24*1)));
        }
        else{
                $today = date("Y-m-d", time());
                $tomorrow = date("Y-m-d", time()+(60*60*24*1));
        }
//==================================

//grab all possible download filenames
$handle = opendir($dir_assoc);
        while (false !== ($file = readdir($handle))){
                        if ($file != "." && $file != ".." && $file != ".DAV" && $file != "latest"){
                                $all_files[] = $file;
                        }
                }
closedir($handle);
sort($all_files);

//start database objects
$q3 = new DB($squid);

//loop through all download associations
foreach($all_files as $download_file){
        //echo("$assoc\n");

                $dl206Count = "SELECT count(remote_host) as num_206s, remote_host FROM `access_log` WHERE request_uri
                = '/BookMaker/binaries/$download_file' and time_stamp > '$today' and time_stamp < '$tomorrow' group by remote_host";
                $q3->query($dl206Count);

                while($q3->next_record()){
                        $num_206s = $q3->f("num_206s");
                        $ipAddress = $q3->f("remote_host");
                        //clear vars
                        $org_name = "";
                        $org_id = "";
                        $country = "";

                        //get all IPs for this assoc with 2 or more
                        if($num_206s >= $ipNumber){
                                //grab IP information from whois
                                $this_whois = shell_exec("whois $ipAddress");

                                $parse_whois = explode("\n", $this_whois);
                                foreach($parse_whois as $single_line){
                                        //echo("$single_line\n\n\n");
                                        if(stristr($single_line, "OrgName") || stristr($single_line, "org:")){
                                                $org_name_array = explode(":", $single_line);
                                                $org_name = trim($org_name_array[1]);
                                        }
                                        elseif(stristr($single_line, "OrgID") || stristr($single_line, "netname")){
                                                $org_id_array = explode(":",$single_line);
                                                $org_id = trim($org_id_array[1]);
                                        }
                                        elseif(stristr($single_line, "Country")){
                                                $country_array = explode(":",$single_line);
                                                $country = trim($country_array[1]);
                                        }
                                }
                                //check for update or insert
                                $q4 = new DB($squid);
                                $query="select download_id from downloads where date = '$today' and ip_address = '$ipAddress' and download_name ='$download_file'";
                                $q4->query($query);
                                if($q4->next_record()){
                                        $download_id = $q4->f("download_id");
                                        $query2 = "update downloads
                                        set num_requests = $num_206s,
                                        org_name = '$org_name'
                                        org_id = '$org_id',
                                        country = '$country'
                                        where download_id = $download_id";
                                }
                                else{
                                        $query2 = "insert into downloads (date,ip_address,download_name,num_requests,org_name,org_id,country) values ('$today','$ipAddress','$download_file',$num_206s,'$org_name','$org_id','$country'";
                                }

                                $q5 = new DB($squid);
                                //$q5->query($query2);
                                echo("$today, $ipAddress, $download_file, $num_206s, $org_name, $org_id, $country\n");
                        }
                }
}

?>