<?php
//dv at mydomain.com


/*
FIRST, CREATE A FORM LIKE THIS IN ANOTHER PAGE:

<form method="POST" action="awstats.php">
<input type="text" name="_domain">
<input type="text" name="_user">
<input type="password" name="_pass">
<input type="submit">
</form>

*/

//NEXT, CHANGE THIS TO ANY UNIQUE STRING OF ALPHANUMERIC CHARACTERS
$unique 'Q42Taw7f0dd4';

/* NO NEED TO CHANGE ANYTHING BELOW HERE */

session_start();

$k base64_encode(strrev(str_rot13($unique.session_id())));
$s base64_encode($k);

//first access of page from form
if(isset($_POST['_user']) && isset($_POST['_pass']) && isset($_POST['_domain']) && !isset($_SESSION[$k]) && !isset($_SESSION[$s])){
    
$u trim(strip_tags($_POST['_user']));
  
$p trim(strip_tags($_POST['_pass']));
  
$d preg_replace('/^(www\.|http:\/\/www\.)/i','',trim(strip_tags($_POST['_domain'])));
    
$data "$u~~$p~~$d";
    
$_SESSION[$k] = md5(uniqid(mt_rand(), true));
    
$_SESSION[$s] = encrypt($data$_SESSION[$k]);
}

if(isset(
$_SESSION[$s]) && isset($_SESSION[$k])){
    
$data explode('~~',decrypt($_SESSION[$s], $_SESSION[$k]));
    
$user $data[0];
    
$pass $data[1];
    
$domain $data[2];
}
else {
    echo 
'Could not validate';
    exit;
}


function 
encrypt($string,$key) {
   
srand((double) microtime() * 1000000); //for sake of MCRYPT_RAND
  
$td mcrypt_module_open('des''','cfb''');
  
$key substr($key0mcrypt_enc_get_key_size($td));
  
$iv_size mcrypt_enc_get_iv_size($td);
  
$iv mcrypt_create_iv($iv_sizeMCRYPT_RAND);
  
/* Initialize encryption handle */
   
if (mcrypt_generic_init($td$key$iv) != -1) {

     
/* Encrypt data */
     
$c_t mcrypt_generic($td$string);
     
mcrypt_generic_deinit($td);
     
mcrypt_module_close($td);
       
$c_t $iv.$c_t;
       return 
$c_t;
   } 
//end if
}

function 
decrypt($string,$key) {
  
/* Open module, and create IV */
  
$td mcrypt_module_open('des''','cfb''');
  
$key substr($key0mcrypt_enc_get_key_size($td));
  
$iv_size mcrypt_enc_get_iv_size($td);
  
$iv substr($string,0,$iv_size);
  
$string substr($string,$iv_size);
  
/* Initialize encryption handle */
   
if (mcrypt_generic_init($td$key$iv) != -1) {

     
/* Encrypt data */
     
$c_t mdecrypt_generic($td$string);
     
mcrypt_generic_deinit($td);
     
mcrypt_module_close($td);
       return 
$c_t;
   } 
//end if
}

//retrieves the file, either .pl or .png
function getFile($fileQuery){
  global 
$user$pass$domain;
  return 
file_get_contents("http://$user:$pass@$domain:2082/".$fileQuery);
}

//it's a .png file...
if(strpos($_SERVER['QUERY_STRING'],'.png')!==false) {
  
$fileQuery $_SERVER['QUERY_STRING'];
}
//probably first time to access page...
elseif(empty($_SERVER['QUERY_STRING'])){
  
$fileQuery "awstats.pl?config=$domain";
}
//otherwise, all other accesses
else {
  
$fileQuery 'awstats.pl?'.$_SERVER['QUERY_STRING'];
}

//now get the file
$file getFile($fileQuery);

//check again to see if it was a .png file
//if it's not, replace the links
if(strpos($_SERVER['QUERY_STRING'],'.png')===false) {
  
$file str_replace('awstats.pl'basename($_SERVER['PHP_SELF']), $file);
  
$file str_replace('="/images','="'.basename($_SERVER['PHP_SELF']).'?images',$file);

}
//if it is a png, output appropriate header
else {
  
header("Content-type: image/png");
}

//output the file
echo $file;

?>