<?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';

// Disable any caching by the browser
disable_browser_cache();



if (isset ($_REQUEST['id']))
{
   $id     = $_REQUEST['id'];
   $action = 'pay';
}
elseif (isset ($_REQUEST['ID']))
{
   $id     = $_REQUEST['ID'];
   $action = 'pay';
}
elseif ($_REQUEST['payed'])
{
	$id     = $_REQUEST['payed'];
	$action = 'payed';
}
elseif ($_REQUEST['canceled'])
{
	$id     = $_REQUEST['canceled'];
	$action = 'canceled';
}

$id = (isset ($id) ? trim ($id) : 0);
$id = preg_replace ('`(id[_]?)`', '', $id);
$id = (preg_match ('`^[\d]+$`', $id) ? intval ($id) : 0);

if (!empty ($id))
{
   if (!empty ($_REQUEST['mode']) && $_REQUEST['mode'] == 'review')
   {
      $data = $db->GetRow("SELECT * FROM `{$tables['link_review']['name']}` WHERE `LINK_ID` = ".$db->qstr($id));
      $data['ID'] = (!empty ($data['LINK_ID']) ? $data['LINK_ID'] : '');
      if (isset ($data['LINK_ID']))
         unset ($data['LINK_ID']);
   }
   else
      $data = $db->GetRow("SELECT * FROM `{$tables['link']['name']}` WHERE `ID` = ".$db->qstr($id));
}
//added 6-24-07
if($_SESSION['PRIVACY'] != '1')
         {
           $data['OWNER_EMAIL']= 'Hidden for Privacy Reasons.';
         }
// end add
if (empty ($data['ID']))
{
   echo $tpl->fetch('payment.tpl');
   exit();
}

$price = array ();

if ($data['LINK_TYPE'] == '4')
{
if (FTR_ENABLE == '1' && PAY_FEATURED > 0)
{
   $price[$link_type_int['featured']] = PAY_FEATURED;
}
 $ext = PAY_UMF;
  $sublength = PAY_UMF;
}

if ($data['LINK_TYPE'] == '2')
{
if (PAY_NORMAL > 0)
{
   $price[$link_type_int['normal']] = PAY_NORMAL;
   if (PAY_ENABLE_FREE){
      $price[$link_type_int['free']] = 0;
   }
 $ext = PAY_UMN;
 $sublength = PAY_UMN;
}
}

if ($data['LINK_TYPE'] == '3')
{
if (PAY_RECPR > 0)
{
   $price[$link_type_int['reciprocal']] = PAY_RECPR;
}
 $ext = PAY_UMR;
 $sublength = PAY_UMR;
}

$SubscriptionEnabled = 0;
 if (PAY_ENABLE_SUBSCRIPTION == 1 && $ext != 5)
{
   $SubscriptionEnabled = 1;
   $Subscription = array ();
   if ($ext == 2)
   {
      $Subscription['UM']     = _L('Month');
      $Subscription['PERIOD'] = 3;
	 $sublength = 2;
   }
   elseif ($ext == 3)
   {
      $Subscription['UM']     = _L('Month');
      $Subscription['PERIOD'] = 6;
	  $sublength = 3;
   }
   elseif ($ext == 4)
   {
      $Subscription['UM']     = _L('Year');
      $Subscription['PERIOD'] = 1;
	 $sublength = 4;
   }
   else
   {
      $Subscription['UM']     = _L('Month');
      $Subscription['PERIOD'] = 1;
	   $sublength = 1;
   }
   $tpl->assign('Subscription', $Subscription);
}

$tpl->assign('SubLength', $sublength);
if ($action=='pay')
{
	if (empty ($_REQUEST['submit']))
	{
		if (!empty ($_SERVER['HTTP_REFERER']))
			$_SESSION['return'] = $_SERVER['HTTP_REFERER'];

		SmartyValidate :: disconnect();
		SmartyValidate :: connect($tpl);
		SmartyValidate :: register_form('pay_link', true);
		SmartyValidate :: register_validator('v_quantity', 'quantity', 'isInt', false, false, 'trim', 'pay_link');
	}
   else
   {
   	
		SmartyValidate :: connect($tpl);
		if (SmartyValidate :: is_valid($_REQUEST, 'pay_link'))
		{
			$pay_data = array ();
			$pay_id   = $db->GenID($tables['payment']['name'].'_SEQ');
			$pay_data['ID']        = $pay_id;
			$pay_data['LINK_ID']   = $data['ID'];
			$pay_data['IPADDRESS'] = (!empty ($client_info['IP']) ? $client_info['IP'] : get_client_ip());
			$pay_data['QUANTITY']  = $_REQUEST['quantity'];
			$pay_data['AMOUNT']    = $price[$data['LINK_TYPE']];
			$pay_data['TOTAL']     = (int)$pay_data['QUANTITY'] * (float)$pay_data['AMOUNT'];
			$pay_data['UM']        = $sublength;
			$pay_data['PAY_DATE']  = gmdate ('Y-m-d H:i:s');
			$pay_data['CONFIRMED'] = -1;
         $pay_data['SUBSCRIBED'] = (isset ($_REQUEST['subscribe']) && $_REQUEST['subscribe'] == 1 && $SubscriptionEnabled == 1 ? 1 : 0);
			if (db_replace('payment', $pay_data, 'ID') > 0)
			{
				$action = 'paypal';
				$tpl->assign('PAYMENT', $pay_data);
			}
         else
				$tpl->assign('error', true);

         SmartyValidate :: disconnect();
		}
	}
	$quantity  = (!empty ($_REQUEST['quantity']) && preg_match ('`^[\d]+$`', $_REQUEST['quantity']) ? intval ($_REQUEST['quantity']) : 1);
   $subscribe = ($_REQUEST['subscribe'] == 1 ? 1 : 0);
}

$tpl->assign('quantity' , $quantity);
$tpl->assign('subscribe', $subscribe);
$tpl->assign('price'    , $price);
$tpl->assign('action'   , $action);

$path   = array ();
$path[] = array ('ID' => '0', 'TITLE' => _L(SITE_NAME)     , 'TITLE_URL' => DOC_ROOT, 'DESCRIPTION' => SITE_DESC);
$path[] = array ('ID' => '0', 'TITLE' => _L('Link Payment'), 'TITLE_URL' => ''      , 'DESCRIPTION' => _L('Submit a new link to the directory '));
$tpl->assign('path', $path);

$tpl->assign('SubscribeOptions', array(0 => _L('No'), 1 => _L('Yes')) );

$tpl->assign($data);
$tpl->assign('payment_um', $payment_um);
$tpl->assign('SubscriptionEnabled', $SubscriptionEnabled);
$tpl->assign('SubscriptionPeriod', $Subscription['PERIOD']);
$tpl->assign('SubscriptionUm', $Subscription['UM']);
//Clean whitespace
$tpl->load_filter('output', 'trimwhitespace');

//Make output
if ($action == 'paypal')
{
	echo $tpl->fetch('paypal.tpl');
	unset( $_SESSION['PRIVACY']);
}
else
   echo $tpl->fetch('payment.tpl');
?>