<?php

//-------------------------------------------
/**
* Purpose : Format the array into a better list
* @return string
*/
function format_post($data) {

    if ( is_array ($data) ) {
            $file_values = "";
            foreach($data as $key=>$val) {
                $file_values .= "$key = $val\r\n";
            }
            
            return $file_values;
    
    }
    else
    { 
        return false; 
    }
    
}
//-----------------------------------------
/**
* Purpose : posts transaction data using fsockopen.
* @return string
*/
function fsockpost($url, $data) { 

	//Parse url 
	$web = parse_url($url); 

	////build post string 
	//foreach($data as $i=>$v) { 
	//	$postdata.= $i . "=" . urlencode($v) . "&"; 
	//}

	//stripslashes added
	$postdata = "";
	foreach($data as $i=>$v) { 
		$postdata.= $i . "=" . urlencode(stripslashes($v)) . "&"; 
	}
	
	$postdata .= "cmd=_notify-validate";

	//Set the port number
	if ($web['scheme'] == "https") { $web['port']="443";  $ssl="ssl://"; } else { $web['port']="80"; }  

	//Create paypal connection
	$fp = @fsockopen($ssl . $web['host'], $web['port'], $errnum, $errstr, 30); 

	//Error checking
	if(!$fp) 
		{

		//		Debugging
		//		//echo "$errnum: $errstr"; 
		//		// HTTP error
		//		
		//		//Send email if there is an http error
		//		$address = 	getreceiveremail();
		//		mailmessage($address, "IPN - HTTP error in fsockopen - com_ipnfunctions.php", "$errnum : $errstr", "", $address, "", $testmode);
		
		} else { //Post Data
 
		fputs($fp, "POST ".$web['path']." HTTP/1.1\r\n"); 
		fputs($fp, "Host: ".$web['host']."\r\n"); 
		fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n"); 
		fputs($fp, "Content-length: ".strlen($postdata)."\r\n"); 
		fputs($fp, "Connection: close\r\n\r\n"); 
		fputs($fp, $postdata . "\r\n\r\n"); 

		//loop through the response from the server 
		while(!feof($fp)) { $info[]=@fgets($fp, 1024); } 

		//close fp - we are done with it 
		fclose($fp); 

		//break up results into a string
		$info = implode(",",$info); 

		}

	return $info; 

}
//------------------------------------------
/**
* Purpose :	Creates variables (single item) which are used in various places for further processing.
* @return array
*/
function create_local_variables() { 

	//PayPal Configuration 
	if ( isset($_POST['business']) ) { $array_name['business'] 				= $_POST['business']; }	
	
	//Your primary paypal email address	
	if ( isset($_POST['receiver_email']) ) { $array_name['receiver_email'] 	= $_POST['receiver_email']; }	
	if ( isset($_POST['receiver_id']) ) { $array_name['receiver_id'] 		= $_POST['receiver_id']; }
	
	//A brief description of the item	
	if ( isset($_POST['item_name']) ) { $array_name['item_name'] 			= $_POST['item_name']; }
	
	//The item identity which is used to define the item	
	if ( isset($_POST['item_number']) ) { $array_name['item_number'] 		= $_POST['item_number']; }
	
	//Currency code
	if ( isset($_POST['mc_currency']) ) { $array_name['mc_currency'] 		= $_POST['mc_currency']; }
	if ( isset($_POST['quantity']) ) { $array_name['quantity'] 				= $_POST['quantity']; }
	if ( isset($_POST['invoice']) ) { $array_name['invoice'] 				= $_POST['invoice']; }
	
	//pass through variable not seen by the purchaser
	if ( isset($_POST['custom']) ) { $array_name['custom'] 					= $_POST['custom']; }		
	if ( isset($_POST['memo']) ) { $array_name['memo'] 						= $_POST['memo']; }		
	if ( isset($_POST['tax']) ) { $array_name['tax'] 						= $_POST['tax']; }		
	if ( isset($_POST['option_name1']) ) { $array_name['option_name1'] 		= $_POST['option_name1']; }
	if ( isset($_POST['option_selection1']) ) { $array_name['option_selection1'] 	= $_POST['option_selection1']; }
	if ( isset($_POST['option_name2']) ) { $array_name['option_name2'] 				= $_POST['option_name2']; }	
	if ( isset($_POST['option_selection2']) ) { $array_name['option_selection2'] 	= $_POST['option_selection2']; } 
	if ( isset($_POST['num_cart_items']) ) { $array_name['num_cart_items'] 			= $_POST['num_cart_items']; }
	
	//The payment gross
	if ( isset($_POST['mc_gross']) ) { $array_name['mc_gross'] 				= $_POST['mc_gross']; }			
	if ( isset($_POST['mc_fee']) ) { $array_name['mc_fee'] 					= $_POST['mc_fee']; }				
	if ( isset($_POST['mc_currency']) ) { $array_name['mc_currency'] 		= $_POST['mc_currency']; }
	if ( isset($_POST['settle_amount']) ) { $array_name['settle_amount'] 	= $_POST['settle_amount']; }
	if ( isset($_POST['settle_currency']) ) { $array_name['settle_currency'] 	= $_POST['settle_currency']; }	
	if ( isset($_POST['exchange_rate']) ) { $array_name['exchange_rate'] 		= $_POST['exchange_rate']; }
	if ( isset($_POST['payment_fee']) ) { $array_name['payment_fee'] 		= $_POST['payment_fee']; }	
	if ( isset($_POST['payment_status']) ) { $array_name['payment_status'] 	= $_POST['payment_status']; }
	if ( isset($_POST['pending_reason']) ) { $array_name['pending_reason'] 	= $_POST['pending_reason']; }
	
	if ( isset($_POST['reason_code']) ) { $array_name['reason_code'] 		= $_POST['reason_code']; }			
	if ( isset($_POST['payment_date']) ) { $array_name['payment_date'] 		= $_POST['payment_date']; }	
	
	//The PayPal transaction id
	if ( isset($_POST['txn_id']) ) { $array_name['txn_id'] 					= $_POST['txn_id']; }				
	
	//The type of paypal transaction
	if ( isset($_POST['txn_type']) ) { $array_name['txn_type'] 				= $_POST['txn_type']; }	
	if ( isset($_POST['payment_type']) ) { $array_name['payment_type'] 		= $_POST['payment_type']; }
	if ( isset($_POST['for_auction']) ) { $array_name['for_auction'] 		= $_POST['for_auction']; }
	if ( isset($_POST['auction_buyer_id']) ) { $array_name['auction_buyer_id'] 		= $_POST['auction_buyer_id']; }		
	if ( isset($_POST['auction_closing_date']) ) { $array_name['auction_closing_date'] 		= $_POST['auction_closing_date']; }
	if ( isset($_POST['auction_multi_item']) ) { $array_name['auction_multi_item'] 	= $_POST['auction_multi_item']; }
	
	if ( isset($_POST['first_name']) ) { $array_name['first_name'] 			= $_POST['first_name']; }	
	if ( isset($_POST['last_name']) ) { $array_name['last_name'] 			= $_POST['last_name']; }
	
	if ( isset($_POST['payer_business_name']) ) { $array_name['payer_business_name']  = $_POST['payer_business_name']; }	
	

	if ( isset($_POST['address_name']) ) { $array_name['address_name'] 		= $_POST['address_name']; }	
	if ( isset($_POST['address_street']) ) { $array_name['address_street'] 	= $_POST['address_street']; }		
	if ( isset($_POST['address_city']) ) { $array_name['address_city'] 		= $_POST['address_city']; }
	if ( isset($_POST['address_state']) ) { $array_name['address_state'] 	= $_POST['address_state']; }
	if ( isset($_POST['address_zip']) ) { $array_name['address_zip'] 		= $_POST['address_zip']; }
	if ( isset($_POST['address_country']) ) { $array_name['address_country'] 	= $_POST['address_country']; }
	if ( isset($_POST['address_status']) ) { $array_name['address_status'] 	= $_POST['address_status']; }		

	
	//The email address of the person who is purchasing the product	
	if ( isset($_POST['payer_email']) ) { $array_name['payer_email'] 	= $_POST['payer_email']; }	
	if ( isset($_POST['payer_id']) ) { $array_name['payer_id'] 			= $_POST['payer_id']; }	

	
	//Will be COMPLETE if transaction is complete	
	if ( isset($_POST['payer_status']) ) { $array_name['payer_status'] 		= $_POST['payer_status']; }		
	if ( isset($_POST['notify_version']) ) { $array_name['notify_version'] 	= $_POST['notify_version']; }			
	if ( isset($_POST['verify_sign']) ) { $array_name['verify_sign'] 		= $_POST['verify_sign']; }			
 	
	return $array_name; 

}
//------------------------------------------
/**
* Purpose :	Creates variables (cart) which are used in various places for further processing.
* @return array
*/
function create_local_variables_cart() { 

	//PayPal Configuration 
	if ( isset($_POST['business']) ) { $array_name['business'] 				= $_POST['business']; }		
	
	//Your primary paypal email address		
	if ( isset($_POST['receiver_email']) ) { $array_name['receiver_email'] 	= $_POST['receiver_email']; }	
	if ( isset($_POST['receiver_id']) ) { $array_name['receiver_id'] 		= $_POST['receiver_id']; }
	
	//pass through variable not seen by the purchaser
	if ( isset($_POST['quantity']) ) { $array_name['quantity'] 					= $_POST['quantity']; }	
	if ( isset($_POST['invoice']) ) { $array_name['invoice'] 					= $_POST['invoice']; }
	if ( isset($_POST['custom']) ) { $array_name['custom'] 						= $_POST['custom']; }		
	if ( isset($_POST['memo']) ) { $array_name['memo'] 							= $_POST['memo']; }			
	if ( isset($_POST['tax']) ) { $array_name['tax'] 							= $_POST['tax']; }				
	if ( isset($_POST['option_name1']) ) { $array_name['option_name1'] 				= $_POST['option_name1']; }				
	if ( isset($_POST['option_selection1']) ) { $array_name['option_selection1'] 	= $_POST['option_selection1']; }
	if ( isset($_POST['option_name2']) ) { $array_name['option_name2'] 			= $_POST['option_name2']; }
	if ( isset($_POST['option_selection2']) ) { $array_name['option_selection2'] = $_POST['option_selection2']; }
	if ( isset($_POST['mc_fee']) ) { $array_name['mc_fee'] 						= $_POST['mc_fee']; }
	
	//Currency code
	if ( isset($_POST['mc_currency']) ) { $array_name['mc_currency'] 			= $_POST['mc_currency']; }	
	
	if ( isset($_POST['settle_amount']) ) { $array_name['settle_amount'] 		= $_POST['settle_amount']; }
	if ( isset($_POST['settle_currency']) ) { $array_name['settle_currency'] 	= $_POST['settle_currency']; }
	if ( isset($_POST['exchange_rate']) ) { $array_name['exchange_rate'] 	= $_POST['exchange_rate']; }
	
	//The payment gross
	if ( isset($_POST['mc_gross']) ) { $array_name['mc_gross'] 				= $_POST['mc_gross']; }
	if ( isset($_POST['payment_fee']) ) { $array_name['payment_fee'] 		= $_POST['payment_fee']; }	
	if ( isset($_POST['payment_status']) ) { $array_name['payment_status'] 	= $_POST['payment_status']; }
	if ( isset($_POST['pending_reason']) ) { $array_name['pending_reason'] 	= $_POST['pending_reason']; }			
	if ( isset($_POST['reason_code']) ) { $array_name['reason_code'] 		= $_POST['reason_code']; }			
	if ( isset($_POST['payment_date']) ) { $array_name['payment_date'] 		= $_POST['payment_date']; }
	
	//The PayPal transaction id
	if ( isset($_POST['txn_id']) ) { $array_name['txn_id'] 					= $_POST['txn_id']; }
	
	//The type of paypal transaction
	if ( isset($_POST['txn_type']) ) { $array_name['txn_type'] 				= $_POST['txn_type']; }
	if ( isset($_POST['payment_type']) ) { $array_name['payment_type'] 		= $_POST['payment_type']; }	
	if ( isset($_POST['for_auction']) ) { $array_name['for_auction'] 		= $_POST['for_auction']; }	
	if ( isset($_POST['auction_buyer_id']) ) { $array_name['auction_buyer_id'] 			= $_POST['auction_buyer_id']; }		
	if ( isset($_POST['auction_closing_date']) ) { $array_name['auction_closing_date'] 	= $_POST['auction_closing_date']; }		
	if ( isset($_POST['auction_multi_item']) ) { $array_name['auction_multi_item'] 		= $_POST['auction_multi_item']; }
	
	
	if ( isset($_POST['first_name']) ) { $array_name['first_name'] 						= $_POST['first_name']; }	
	if ( isset($_POST['last_name']) ) { $array_name['last_name'] 						= $_POST['last_name']; }	
	
	
	if ( isset($_POST['payer_business_name']) ) { $array_name['payer_business_name'] 	= $_POST['payer_business_name']; }	

	if ( isset($_POST['address_name']) ) { $array_name['address_name'] 		= $_POST['address_name']; }	
	if ( isset($_POST['address_street']) ) { $array_name['address_street'] 	= $_POST['address_street']; }	
	if ( isset($_POST['address_city']) ) { $array_name['address_city'] 		= $_POST['address_city']; }
	if ( isset($_POST['address_state']) ) { $array_name['address_state'] 	= $_POST['address_state']; }	
	if ( isset($_POST['address_zip']) ) { $array_name['address_zip'] 		= $_POST['address_zip']; }	
	if ( isset($_POST['address_country']) ) { $array_name['address_country'] = $_POST['address_country']; }	
	if ( isset($_POST['address_status']) ) { $array_name['address_status'] 	= $_POST['address_status']; }	
	
	//The email address of the person who is purchasing the product
	if ( isset($_POST['payer_email']) ) { $array_name['payer_email'] 		= $_POST['payer_email']; }		
	if ( isset($_POST['payer_id']) ) { $array_name['payer_id'] 				= $_POST['payer_id']; }			
	
	//Will be COMPLETE if transaction is complete
	if ( isset($_POST['payer_status']) ) { $array_name['payer_status'] 		= $_POST['payer_status']; }
	if ( isset($_POST['notify_version']) ) { $array_name['notify_version'] 	= $_POST['notify_version']; }
	if ( isset($_POST['verify_sign']) ) { $array_name['verify_sign'] 		= $_POST['verify_sign']; }

	//number of items in cart	
	if ( isset($_POST['num_cart_items']) ) { $array_name['num_cart_items'] 	= $_POST['num_cart_items']; }

	return $array_name; 

}
//-----------------------------------------------------------------
/**
* Purpose : Additional part required for cart items
* Input : testmode = true identifies if in local test mode
* Fill cart with details
* @return array
*/
function create_local_variables_cart_items() {

		
    $num_cart_items	=	$_POST['num_cart_items'];	//was undefined constant

    //Loop around these	
    for ($i=1; $i <= $num_cart_items; $i++)	{
    
        $item_name 		= "item_name$i";
        $item_number 	= "item_number$i";
        $mc_gross 		= "mc_gross_$i";
        $quantity		= "quantity$i";

        $array_name[$i]['item_name_' . $i]		=	$_POST[$item_name]; 	//A brief description of the item
        $array_name[$i]['item_number_' . $i]	=	$_POST[$item_number];	//The item identity which is used to define the item
        $array_name[$i]['mc_gross_' . $i]		=	$_POST[$mc_gross]; 		//gross value				
        $array_name[$i]['quantity_' . $i]		=	$_POST[$quantity];
    
    }
	return $array_name;
}
/*********************************************************** 
* Send the mail message
* Inputs : to address, subject, $body text, $format, $fromaddress, $bcc and testmode
* format is not used but might be "html" if html format is required which sets the header
* fromaddress is the from address
* bcc is not used but is the list of bcc addresses separated by comma (,)
* testmode is not used but might be used used in deugging.
*
* @param string $to
* @param string $subject
* @param string $body 
* @param string $format
* @param string $fromaddress
* @param string $bcc 
* @param boolean $testmode
* @return void
*/
function mailmessage($to, $subject, $body, $format, $fromaddress, $bcc, $testmode) {

	//Headers are separated by $sep 
	//(It may be necessary to use \n on some servers, but this not actually correct).
	$sep = "\r\n";		//separater for headers
	$headers = "";
	$headers .= "From:" . $fromaddress . $sep;
	
	$addparam = "-f".$fromaddress;
	mail($to, $subject, $body, $headers, $addparam);	//Send with header
	
}

?>