<?php
/**
 * File to handle the verify mail page
 *
 * This file is used to check the mail using the password of the user.
 *
 * PHP version 5.0
 *
 * @category	Anova
 * @package		verifyMailHandler
 * @author 		selvaraj_47ag04
 * @copyright	Copyright (c) 2009 {@link http://www.agriya.com Agriya Infoway}
 * @license		http://www.agriya.com Agriya Infoway Licence
 * @version		SVN: $Id:
 * @since 		2008-12-19
 */
/**
 * File having common configuration variables required for the entire project
 */
require_once('./common/configs/config.inc.php');
$CFG['feature']['auto_hide_success_block'] = false;
$CFG['lang']['include_files'][] = 'languages/%s/root/verifyMail.php';
$CFG['html']['header'] = 'general/html_header.php';
$CFG['html']['footer'] = 'general/html_footer.php';

//compulsory
$CFG['mods']['is_include_only']['html_header'] = false;
$CFG['html']['is_use_header'] = false;
/**
 * File to include the header file, database access file, session management file, help file and necessary functions
 */
require($CFG['site']['project_path'].'common/application_top.inc.php');
//-------------- Class verifyMailHandler begins--------------->>>//
/**
 * This class is used to verify user mail ids
 */
class  verifyMailHandler extends FormHandler
	{
		public $user_details;

		/**
		 * verifyMailHandler::chkCorrectEmail()
		 *
		 * @param $err_tip
		 * @return
		 **/
		public function chkCorrectEmail($err_tip)
			{
				global  $smartyObj;
				$sql = 'SELECT user_id, name, display_name FROM '.$this->CFG['db']['tbl']['users'].
						' WHERE email=\''.addslashes($this->fields_arr['email']).'\'';
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);

				if (!$rs)
					    trigger_db_error($this->dbObj);
				$smartyObj->assign('email', $this->fields_arr['email']);
				if($row = $rs->FetchRow())
					{
						if($this->fields_arr['code']==substr(md5($row['user_id']), 0, 8))
							{
								$this->fields_arr['user_id'] = $row['user_id'];
								$this->fields_arr['name'] = $row['name'];
								$this->fields_arr['display_name'] = $row['display_name'];
								$smartyObj->assign('user_id', $row['user_id']);
								$smartyObj->assign('name', $row['name'] );
								$smartyObj->assign('display_name', $row['display_name']);
								return true;
							}
					}
				$this->fields_err_tip_arr['email'] = $err_tip;
				return false;
			}

		/**
		 * verifyMailHandler::updateNewPassword()
		 *
		 * @return
		 **/
		public function updateNewPassword()
			{
				$sql = 'UPDATE '.$this->CFG['db']['tbl']['users'].
						' SET  password = \''.md5($this->fields_arr['password']).'\'
						 WHERE user_id = \''.$this->fields_arr['user_id'].'\'';
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				if (!$rs)
					    trigger_db_error($this->dbObj);
			}

		/**
		 * verifyMailHandler::chkIsPasswordAndUserNameAreSame()
		 *
		 * @param string $err_tip
		 * @return
		 */
		public function chkIsPasswordAndUserNameAreSame($err_tip = '')
			{
				if($this->fields_arr['name'] and $this->fields_arr['password'])
					{
						if(strtolower($this->fields_arr['name']) == strtolower($this->fields_arr['password']))
							{
								$this->fields_err_tip_arr['password'] = $err_tip;
								return false;
							}
					}
				return true;
			}

		/**
		 * verifyMailHandler::chkIsSamePasswords()
		 *
		 * @param mixed $field_name1
		 * @param mixed $field_name2
		 * @param string $err_tip
		 * @return
		 */
		public function chkIsSamePasswords($field_name1, $field_name2, $err_tip='')
			{
				$is_ok = ($this->fields_arr[$field_name1]==$this->fields_arr[$field_name2]);
				if (!$is_ok)
						$this->fields_err_tip_arr[$field_name1] = $this->fields_err_tip_arr[$field_name2] = $err_tip;
				return $is_ok;
			}

		/**
		 * verifyMailHandler::chkIsValidSize()
		 *
		 * @param mixed $field_name
		 * @param mixed $min
		 * @param mixed $max
		 * @param string $err_tip
		 * @return
		 */
		public function chkIsValidSize($field_name, $min, $max, $err_tip = '')
			{
				if((strlen($this->fields_arr[$field_name])<$min) or (strlen($this->fields_arr[$field_name])>$max))
					{
						$this->fields_err_tip_arr[$field_name] = $err_tip;
						return false;
					}
				return true;
			}
	}
//<<<<<----------------- Class verifyMailHandler ends ------------//
//----------------------- Code begins -------------->>>>>//
$verifyfrm = new verifyMailHandler();
$verifyfrm->setPageBlockNames(array('block_form_verifymail', 'block_form_email'));
// Set the form fields
$verifyfrm->setFormField('code', '');
$verifyfrm->setFormField('user_id', '');
$verifyfrm->setFormField('password', '');
$verifyfrm->setFormField('confirm_password', '');
$verifyfrm->setFormField('email', '');
$verifyfrm->setFormField('name', '');
$verifyfrm->setFormField('display_name', '');
$verifyfrm->setAllPageBlocksHide();
$verifyfrm->setPageBlockShow('block_form_email');

$LANG['verifymail_password'] = str_ireplace('VAR_MIN_COUNT', $CFG['admin']['password_min_size'], $LANG['verifymail_password']);
$LANG['verifymail_password'] = str_ireplace('VAR_MAX_COUNT', $CFG['admin']['password_max_size'], $LANG['verifymail_password']);
$smartyObj->assign('verifymail_password', $LANG['verifymail_password']);

$title = $LANG['verifymail_title'];
$smartyObj->assign('title', $title);
$verifyfrm->sanitizeFormInputs($_REQUEST);
if ($verifyfrm->isFormGETed($_GET, 'code'))
	{
    	$verifyfrm->sanitizeFormInputs($_GET);
	}
if ($verifyfrm->isFormPOSTed($_POST, 'verifyEmail'))
	{
		$verifyfrm->chkIsNotEmpty('email',$LANG['err_tip_compulsory']) and
			$verifyfrm->chkIsValidEmail('email', $LANG['err_tip_invalid_email']);
		if($verifyfrm->isValidFormInputs())
			{
				$verifyfrm->chkCorrectEmail($LANG['err_tip_invalid_details']);
				if($verifyfrm->isValidFormInputs())
					{
						$verifyfrm->setAllPageBlocksHide();
						$verifyfrm->setPageBlockShow('block_form_verifymail');
						$title = $LANG['verifymail_password_title'];
						$smartyObj->assign('title', $title);
					}
				else
					{
						$verifyfrm->setCommonErrorMsg($LANG['common_msg_error_sorry']);
						$verifyfrm->setPageBlockShow('block_msg_form_error');
					}
			}
		else
			{
			 	$verifyfrm->setCommonErrorMsg($LANG['common_msg_error_sorry']);
				$verifyfrm->setPageBlockShow('block_msg_form_error');
			}
	}
if ($verifyfrm->isFormPOSTed($_POST, 'verifymail'))
	{
		$smartyObj->assign('user_id', $verifyfrm->getFormField('user_id'));
		$smartyObj->assign('name', $verifyfrm->getFormField('name') );
		$smartyObj->assign('display_name', $verifyfrm->getFormField('display_name'));
		$smartyObj->assign('email', $verifyfrm->getFormField('email'));
		$verifyfrm->chkIsNotEmpty('password', $LANG['err_tip_compulsory']) and
			$verifyfrm->chkIsValidSize('password', $CFG['admin']['password_min_size'], $CFG['admin']['password_max_size'], $LANG['err_tip_invalid_size']);
				$verifyfrm->chkIsNotEmpty('confirm_password', $LANG['err_tip_compulsory']) and
					$verifyfrm->chkIsSamePasswords('password', 'confirm_password', $LANG['common_err_tip_same_password']);
						$verifyfrm->chkIsPasswordAndUserNameAreSame($LANG['password_user_name']);
		$title = $LANG['verifymail_password_title'];
		$smartyObj->assign('title', $title);
		if($verifyfrm->isValidFormInputs())
			{
				$verifyfrm->updateNewPassword();
				$verifyfrm->setAllPageBlocksHide();
				$verifyfrm->setCommonSuccessMsg($LANG['verified_successfully']);
				$verifyfrm->setPageBlockShow('block_msg_form_success');
			}
		else
			{
				$verifyfrm->setFormField('password','');
				$verifyfrm->setFormField('confirm_password','');
				$verifyfrm->setAllPageBlocksHide();
				$verifyfrm->setPageBlockShow('block_form_verifymail');
				$verifyfrm->setCommonErrorMsg($LANG['common_msg_error_sorry']);
				$verifyfrm->setPageBlockShow('block_msg_form_error');
			}
	}
//<<<<<------------ Code ends --------------------------//
//------------ Page block templates begins ------->>>>>//
//include the header file
$verifyfrm->includeHeader();
//include the content of the page
setTemplateFolder('root/');
$smartyObj->display('verifyMail.tpl');
//includ the footer of the page
//<<<<<-------------------- Page block templates ends --------------------//
$verifyfrm->includeFooter();
?>