<?php
///////////////////////////////////////////////////////////////////////////////////
//////////////// DO NOT MODIFY ////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////

// pdthtml.php -> DOWNLOADMANAGER.php
error_reporting(0); //disable error reportings

ob_start(); //important else header is set !
include '../settings.php';
ob_end_clean(); //important else header is set !

$TitlePage = $SettingsDownload['TitlePage'];
$auth_token_real = $SettingsPayPal['auth_token_real'];
$sandbox_mode = $SettingsPayPal['sandbox_mode'];
$auth_token_sandbox = $SettingsPayPal['auth_token_sandbox'];
$completely_failed_message = $SettingsPayPal['completely_failed_message'];

////////////////////////////////////////////////////////////////////
//////////////////////CODE FROM PAYPAL//////////////////////////
///MODIFIDED BY TRUSTFM FOR SELLING DIGITAL PRODUCTS///
///////////////////////////////////////////////////////////////////

// read the post from PayPal system and add 'cmd'

$req = 'cmd=_notify-synch';

$tx_token = $_GET['tx'];

if ($sandbox_mode==True){
  $req .= "&tx=$tx_token&at=$auth_token_sandbox";
  $HeaderHost = "Host: www.sandbox.paypal.com:80\r\n";
}
else{
  $req .= "&tx=$tx_token&at=$auth_token_real";
  $HeaderHost = "Host: www.paypal.com:80\r\n";
}

// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= $HeaderHost; //line added !  NOT!! THIS LINE IS NOT IN THE ipn.php supplied by PAYPAL
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";

if ($sandbox_mode==True){
  $fp = fsockopen ('www.sandbox.paypal.com', 80, $errno, $errstr, 30);
}
else{
  $fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);
}

// If possible, securely post back to paypal using HTTPS
// Your PHP server will need to be SSL enabled
// $fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);

if (!$fp) {
  // HTTP ERROR
  } else {
  fputs ($fp, $header . $req);
  // read the body data
  $res = '';
  $headerdone = false;
  while (!feof($fp)) {
    $line = fgets ($fp, 1024);
    if (strcmp($line, "\r\n") == 0) {
    // read the header
      $headerdone = true;
    }
    else if ($headerdone)
    {
    // header has been read. now read the contents
    $res .= $line;
    }
  }
  
  // parse the data
  $lines = explode("\n", $res);
  $keyarray = array();
  
  //Create the page now
  echo "<html>\n";
  echo "<head>\n";
  echo "<title>" . $TitlePage . "</title>\n";
  echo '<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">';
  echo "\n";
  echo "</head>\n";
  echo "<body>\n";
  
  if (strcmp ($lines[0], "SUCCESS") == 0) {
  
 
    for ($i=1; $i<count($lines);$i++){
      list($key,$val) = explode("=", $lines[$i]);
      $keyarray[urldecode($key)] = urldecode($val);
    }
    
    $firstname = $keyarray['first_name'];
    $lastname = $keyarray['last_name'];
	$id = $keyarray['item_number'];
	$itemname = $keyarray['item_name'];
	$amount = $keyarray['mc_gross'];
	$currency = $keyarray['mc_currency'];
	
	include '../../Products/' . $id . '.php';
	
	if ($Product['id']==$id && $Product['Name']==$itemname && $Product['Price']==$amount && $Product['Currency']==$currency ) {
	  //Here is the Download Link
	  echo "<center>";
	  echo '<H1>' . $Product['Name'] . '<br><br>';
	  echo '<a href="DownloadManager.php?f=' . $Download['file_path'] . $Product['Filename'] . '&tx=' . $tx_token  .'"> ' . $Product['DownloadMessage'] . ' </a>';
	  echo "</H1><br><br>";
	  echo  $Product['ThanksMessage'];
      echo ("<br><br><b>Payment Details</b><br>\n");
      echo ("<li>Name: $firstname $lastname</li>\n");
	  echo ("<li>Item: $id</li>\n");
      echo ("<li>Item: $itemname</li>\n");
      echo ("<li>Amount: $amount $currency</li>\n");
	  echo "<br><br>";
	  echo  $Product['ReturnToHomepage'];
	  echo "</center><br><br>";
	} else {
	  echo "<center>";
	  echo  $Product['ErrorMessage'];
      echo "<br><br>";	  
      echo  $Product['ReturnToHomepage'];
      echo "</center>";	  
	}
   
    
  }
  else if (strcmp ($lines[0], "FAIL") == 0) {
    // log for manual investigation
    
	echo $completely_failed_message;

    }

  } 

fclose ($fp);


echo "\n";
echo "</body>\n";
echo "</html>";

?>