<?php 
/*#################################################################*\
|# Licence Number 0DT7-0122-0NW1-0210
|# -------------------------------------------------------------   #|
|# Copyright (c)2010 PHP Link Directory.                           #|
|# http://www.phplinkdirectory.com                                 #|
\*#################################################################*/
	
/**
# ################################################################################
# Project:   PHP Link Directory
#
# **********************************************************************
# Copyright (C) 2004-2009 NetCreated, Inc. (http://www.netcreated.com/)
#
# This software is for use only to those who have purchased a license.
# A license must be purchased for EACH installation of the software.
#
# By using the software you agree to the terms:
#
#    - You may not redistribute, sell or otherwise share this software
#      in whole or in part without the consent of the the ownership
#      of PHP Link Directory. Please contact david@david-duval.com
#      if you need more information.
#
#    - You agree to retain a link back to http://www.phplinkdirectory.com/
#      on all pages of your directory in you purchased the $25 version
#      of the software.
#
# License holders are entitled to upgrades to the 3.4 branch of the software
# as they are made available at http://www.phplinkdirectory.com/
#
# In some cases, license holders may be required to agree to changes
# in the software license before receiving updates to the software.
# **********************************************************************
#
# For questions, help, comments, discussion, etc., please join the
# PHP Link Directory Forum http://www.phplinkdirectory.com/forum/
#
# @link           http://www.phplinkdirectory.com/
# @copyright      2004-2009 NetCreated, Inc. (http://www.netcreated.com/)
# @projectManager David DuVal <david@david-duval.com>
# @package        PHPLinkDirectory
# @version        4.0.0
# ################################################################################
*/

require_once 'init.php';
$paypal_host = 'www.paypal.com';
$paypal_path = '/cgi-bin/webscr';
$ipn_data = array ();

$pid = $_GET['pid'];

$data = array ();

$post_string = '';
foreach ($_POST as $field => $value)
{
        $ipn_data[$field] = $value;
        $post_string     .= $field.'='.urlencode ($value).'&';
}
$post_string .= "cmd=_notify-validate"; // append ipn command

// Open the connection to paypal
$fp = @ fsockopen ($paypal_host, '80', $err_num, $err_str, 30);
if (!$fp)
{
        // Could not open the connection.
   // If loggin is on, the error message will be in the log.
        $last_error = "fsockopen error no. {$errnum}: {$errstr}";
        log_ipn_results(false);
        exit();

}
else
{
        // Post the data back to paypal
        @ fputs ($fp, "POST {$paypal_path} HTTP/1.1\n");
        @ fputs ($fp, "Host: {$paypal_host}\n");
        @ fputs ($fp, "Content-type: application/x-www-form-urlencoded\n");
        @ fputs ($fp, "Content-length: ".strlen ($post_string)."\n");
        @ fputs ($fp, "Connection: close\n\n");
        @ fputs ($fp, $post_string."\n\n");

        // Loop through the response from the server and append to variable
        while (!feof ($fp))
                $ipn_response .= @ fgets ($fp, 1024);

   // Close connection
   @ fclose ($fp);
}

if (eregi ("VERIFIED", $ipn_response))
{
        // Valid IPN transaction.
        log_ipn_results(true);
        exit();
}
else
{
        // Invalid IPN transaction.  Check the log for details.
        $last_error = 'IPN Validation Failed.';
        log_ipn_results(false);
        exit();
}

function log_ipn_results($success)
{
        global $ipn_data, $last_error, $ipn_response;

   // Timestamp
   $text = '['.date ('m/d/Y g:i A').'] - ';

   // Success or failure being logged?
   if ($success){

      $text .= "SUCCESS!\n";
          $success = 2;
   }
   else
           {
      $text .= 'FAIL: '.$last_error."\n";
   $success= -1;
           }
   // Log the POST variables
   $text .= "IPN POST Vars from Paypal:\n";
   foreach ($ipn_data as $key=>$value)
   {
      $text .= "{$key}={$value}, ";
      $raw /= "{$key}\t{$value}\n";
   }
   $data['email']    = $ipn_data['payer_email'];
   $data['name']     = $ipn_data['last_name'].' '.$ipn_data['first_name'];
   $data['article_id']  = $ipn_data['item_number'];
   $data['quantity'] = $ipn_data['quantity'];#mc_gross
   $data['total']    = $ipn_data['mc_gross'];
   $data['payment_type']    = $ipn_data['payment_type'];#if echeck or instant
   $data['payment_status']    = $ipn_data['payment_status']; #check for status as echeck does not clear
   $txn_type = $ipn_data['txn_type'];
    if($txn_type != "subscr_signup") {
   update_article_payment($_GET['pid'], $data, $success, $raw);
        }
   // Log the response from the paypal server
   $text .= "\nIPN Response from Paypal Server:\n ".$ipn_response;

   // Write to log
   $fp = @ fopen (INSTALL_PATH.'temp/ipn_log.txt','a');
   @ fwrite ($fp, $text . "\n\n");
   @ fclose ($fp);  // close file
}
?>
