<?php

/******************************************************************************
 *
 *	PROJECT: Flynax Classifieds Software
 *	VERSION: 4.0
 *	LISENSE: http://www.flynax.com/license-agreement.html
 *	PRODUCT: General Classifieds
 *	
 *	FILE: POST_2CO.GATEWAY.PHP
 *
 *	This script is a commercial software and any kind of using it must be 
 *	coordinate with Flynax Owners Team and be agree to Flynax License Agreement
 *
 *	This block may not be removed from this file or any other files with out 
 *	permission of Flynax respective owners.
 *
 *	Copyrights Flynax Classifieds Software | 2012
 *	http://www.flynax.com/
 *
 ******************************************************************************/

if ($_SERVER['REQUEST_METHOD'] == 'POST' || $_SERVER['REQUEST_METHOD'] == 'post')
{
	require_once('../../../includes/config.inc.php');
	
	/* system controller */
	require_once( RL_INC . 'control.inc.php' );
	
	/* load system configurations */
	$config = $rlConfig -> allConfig();
	$errors = false;

	if (!empty($_POST['item_number']))
	{
		$items = explode('|', base64_decode(urldecode($_POST['item_number'])));
		$plan_id = $items[0];
		$item_id = $items[1];
		$account_id = $items[2];
		$crypted_sum = $items[3];
		$callback_class = $items[4];
		$callback_method = $items[5];
		$cancel_url = $items[6];
		$success_url = $items[7];
		$lang_code = $items[8];
		
		define( 'RL_LANG_CODE', $lang_code );
		define( 'RL_DATE_FORMAT', $rlDb -> getOne('Date_format', "`Code` = '{$config['lang']}'", 'languages') );
		
		$seo_base = RL_URL_HOME;
		$seo_base .= $lang_code == $config['lang'] ? '' : $lang_code;
		
		$lang = $rlLang -> getLangBySide( 'frontEnd', RL_LANG_CODE );
		$GLOBALS['lang'] = $lang;

		$total = $_POST['total'];

		// Check crypted sum
		if ( strcmp($crypted_sum, crypt(sprintf("%.2f", $total), str_replace('http://', '', RL_URL_HOME))) != 0 )
		{
			// Exit since crypted sum is invalid
			$errors = true;
		}
		
		if ( empty($item_id) || empty($plan_id) || empty($total) )
		{
			$errors = true;
		}

		if ( !$errors )
		{
			// If IPN processing script gets to this point it means
			// everything went smoothly and we can update listing status
			$txn_id = $_POST['cart_id'];
	
			$reefless -> loadClass(str_replace('rl', '', $callback_class));
			$$callback_class -> $callback_method( $item_id, $plan_id, $account_id, $txn_id, 'paypal', $total );
			
			$reefless -> redirect(null, $success_url);
		}
		else
		{
			$reefless -> redirect(null, $cancel_url);
		}
	}
}