<?php
/*
script: notify.php
purpose: handles paypal ipn notifications when someone upgrades their account.
				Accounts are automatically upgraded and payment info is logged in the database.
copyright: copyright 2004 Php Image Host. All Rights Reserved. You may modify this file for use on your
					site running a licenced copy of the Php Image Host scripts.
					You must not distribute this file or derivations of it.
Nullified By CrAcKlOvE

*/

$dump = false;

define ("FATAL", E_USER_ERROR);
define ("ERROR", E_USER_WARNING);
define ("WARNING", E_USER_NOTICE);

// set the error reporting level for this script
error_reporting (FATAL | ERROR | WARNING);


require_once(dirname(__FILE__).'/includes/config.inc.php');

require_once(dirname(__FILE__).'/classes/paypal_ipn.class.php');
$config->testing_mode = 0;
$ipn = new paypal_ipn($config->paypal_email, 0);

require_once(dirname(__FILE__).'/classes/paypalpayments.class.php');
$payments =& new paypalpayments($ace);

require_once(dirname(__FILE__).'/classes/users.class.php');
$users =& new users($ace);

ob_start();

// 1) Check it is a valid paypal ipn post...


if( $ipn->process() == true ){

	$dt = '';
	foreach( $ipn->data as $n=>$v ){
		$dt .= "$n = $v\n";
	}

	// 2) check that the payment has been made to us

	if( $ipn->data['receiver_email'] == $config->paypal_email || $ipn->data['receiver_email'] == $config->paypal_account_email){
		echo "Business checked ok...\n";

		// 3) check that payment made by a valid user (and get that user)

		$username = $ipn->data['item_number'];
		$user = $users->getuser(array('username'=>$username));

		if( $user != 0 ){

			echo "Got User<br />\n";

			// 4a) Handle subscription signup notifications...

			if( $ipn->data['txn_type'] == 'subscr_signup' ){

				echo "Signup notification<br />\n";

				// i) if user doesn't already have this subscription id, check that the months and price is valid and update user...
				if( $ipn->data['subscr_id'] != $user->sub_id ){
					$months = 0;
					if( preg_match('/^([0-9]+).*?m/i', $ipn->data['period3'],$match ) )$months = (int)$match[1];
					$amount = (float)$ipn->data['amount3'];
					if( in_array($months, array(1,3,6,12) )){
						$avar = 'price_'.$months;
						$realprice = $config->$avar;
						if( $realprice == $amount ){
							$ups = array('sub_id'=>$ipn->data['subscr_id'], 'sub_months'=>$months, 'sub_amount'=>$amount);
							$users->updateusers(array($user->user_id),$ups);
						}
					}
				}
				echo "Signup notification processed...<br />";

			// 4b) Handle subscription payments...

			}elseif ( $ipn->data['txn_type'] == 'subscr_payment'){

				echo "Subscription payment<br />";

				// i) check that the payment is complete

				if( $ipn->isComplete() ){

					echo "Status is complete<br />\n";

					// ii) check that the currency is correct...

					if( $ipn->data['mc_currency'] == 'USD' ){

					echo "currency ok<br />";

					// iii) check that the price / duration is valid - first see if the user has these values set...

						$valid = false;
						$amount = (float)$ipn->data['mc_gross'];
						$months = 0;
						if( $user->sub_id != '' && $user->sub_id == $ipn->data['subscr_id'] ){
							if( $user->sub_amount != 0 && $user->sub_amount == $amount ){
								$months = $user->sub_months;
								$valid = true;
							}
						}

						// couldn't validate payment against user, so check our plans instead...

						if( !$valid ){
							if( $config->price_1 == $amount ){
								$months = 1;
							}elseif( $config->price_3 == $amount ){
								$months = 3;
							}elseif( $config->price_6 == $amount ){
								$months = 6;
							}elseif( $config->price_12 == $amount ){
								$months = 12;
							}
							if( $months != 0 ) $valid = true;
						}

						// if everything so far is valid, continue					

						if( $valid ){

							echo "Valid Payment<br />";

							// confirm this isn't a duplicate payment...

							if( $payments->getpayments(array('count'=>true,'txnid'=>$ipn->data['txn_id'])) == 0 ){

								echo "Not a duplicate<br />";

								// add this payment to the database

								$pid = $payments->addpayment($ipn->data['txn_id'], $ipn->data['subscr_id'], $user->user_id,$ipn->data['payer_email'], $ipn->data['mc_gross'], 1, $ipn->data);

								if( $pid ){

									echo "Payment added<br />\n";

									// set the user to "paid account" and extend their days of paid 

									$users->upgradeuser($user,$months,$ipn);
	
									// if we are sending upgrade confirmation emails, send 1 :)

									echo "Payment added OK!";
								} // end payment inserted ok
							}else{ // end no duplicate payment
								echo "Duplicate payment\n";
							}
						}else{ // end everything is valid
							echo "Everything is not valid\n";
						}
					}else{ // end currency ok
						echo "Wrong currency\n";
					}
				}else{ // end complete
					echo "Payment is  not complete.";
				}
			}else{ // end payment type check
				echo "Payment type is wrong\n";
			}
		}else{ // end user check
			echo "User doesn't exist.\n";
		}
	}else{ // end business check
		echo "Receiver email (".$ipn->data['receiver_email']." != {$config->paypal_email}\n";
	}
	print_r($ipn->data);

}else{
	echo "No bloody ipn.";
	switch( $ipn->error_code ){
		case ERR_INVALID: break;
		case ERR_SOCKETS: break;
	}
}

$result = ob_get_contents();
ob_end_flush();
if( $dump ) @mail('sam@samyapp.com', 'paypal ipn dump', $result);

?>
