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

$error = 0;
$errorMsg = '';

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

//Generate unique imagehash for visual confirmation
if (VISUAL_CONFIRM == 1)
{
   require_once 'include/functions_imgverif.php';
   $imagehash = fetch_captcha_hash();
   $tpl->assign('imagehash', $imagehash);
   unset ($imagehash);
}

$path   = array ();
$path[] = array ('ID' => '0', 'TITLE' => _L(SITE_NAME)    , 'TITLE_URL' => DOC_ROOT, 'DESCRIPTION' => SITE_DESC);
$path[] = array ('ID' => '0', 'TITLE' => _L('Tell a Friend') , 'TITLE_URL' => ''      , 'DESCRIPTION' => _L('Tell a Friend'));
$tpl->assign('path', $path);

if (empty ($_POST['submit']))
{
   if (!empty ($_SERVER['HTTP_REFERER']))
      $_SESSION['return'] = $_SERVER['HTTP_REFERER'];

   // Clear some varialbles
   if (isset($_SESSION['SmartyPaginate']))
      unset ($_SESSION['SmartyPaginate']);
   if (isset($_SESSION['SmartyValidate']))
      unset ($_SESSION['SmartyValidate']);
   if (isset($_SESSION['values']))
      unset ($_SESSION['values']);

   SmartyValidate :: disconnect();
   SmartyValidate :: connect($tpl, true);
   
    //for login

    if (!SmartyValidate :: is_registered_form('login')) {
    
    SmartyValidate :: register_form('login', true);
    SmartyValidate :: register_validator('v_user', 'user', 'notEmpty', false, false, 'trim', 'login');
    SmartyValidate :: register_validator('v_pass', 'pass', 'notEmpty', false, false, 'trim', 'login');
    }
    //end for login

   SmartyValidate :: register_form('contact', true);
   SmartyValidate :: register_criteria('isCaptchaValid' , 'validate_captcha'        , 'contact');

  
   SmartyValidate :: register_validator('v_EMAIL'       , 'EMAIL'   , 'isEmail'  , false, false, 'trim', 'contact');
   SmartyValidate :: register_validator('v_FRIEND_EMAIL'       , 'FRIEND_EMAIL'   , 'isEmail'  , false, false, 'trim', 'contact');
   SmartyValidate :: register_validator('v_MESSAGE'     , 'MESSAGE' , 'notEmpty' , false, false, 'trim', 'contact');


   if (VISUAL_CONFIRM == 1)
   {
      SmartyValidate :: register_validator('v_CAPTCHA' , 'CAPTCHA:IMAGEHASH', 'isCaptchaValid', false, false, null, 'contact');
   }
}
else
{
   SmartyValidate :: connect($tpl);

   $data = array ();
   $data['EMAIL']   = (!empty ($_REQUEST['EMAIL'])   ? strip_white_space ($_REQUEST['EMAIL'])   : '');
   $data['FRIEND_EMAIL']   = (!empty ($_REQUEST['FRIEND_EMAIL'])   ? strip_white_space ($_REQUEST['FRIEND_EMAIL'])   : '');
   $data['MESSAGE'] = (!empty ($_REQUEST['MESSAGE']) ? strip_white_space ($_REQUEST['MESSAGE']) : '');

   //Take care of white-space chars
   //$data = filter_white_space($data);

   if (VISUAL_CONFIRM == 1 && !empty ($_POST['CAPTCHA']))
   {
      $data = array_merge ($data, array ('CAPTCHA' => $_POST['CAPTCHA'], 'IMAGEHASH' => $_POST['IMAGEHASH']));
   }

   if (SmartyValidate :: is_valid($data, 'contact'))
   {
      //Initialize emailer
      require_once 'libs/phpmailer/class.phpmailer.php';
      $mail            = new PHPMailer();
      $mail->PluginDir = 'libs/phpmailer/';

      $subject = "Link Coupon Recommendation";
      
      if (mail($data['FRIEND_EMAIL'],$subject,$data['MESSAGE'], "From: ".$data['EMAIL'])) {
          unset ($data);
         $tpl->assign('contactSent', 1);
      } else {
          $error++;
         $errorMsg = "Message could not be sent. Please try again.";
         $tpl->assign('contactSent', 0);
      }
   }
   else
      $tpl->assign($_POST);
}

if (VISUAL_CONFIRM == 1)
   $tpl->assign('captcha_length', CAPTCHA_PHRASE_LENGTH);

$tpl->assign($data);
$tpl->assign('error'   , $error);
$tpl->assign('errorMsg', $errorMsg);

//Clean whitespace
$tpl->load_filter('output', 'trimwhitespace');

//Make output
echo $tpl->fetch('tell_friend.tpl');
?>