<?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		###activateMailAccount###
 * @author
 * @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
 */

//-------------- Class verifyMailHandler begins--------------->>>//
class  verifyMailHandler extends FormHandler
	{

		/**
		 * verifyMailHandler::chkIsValidCode()
		 *
		 * @param mixed $code
		 * @return
		 */
		public function chkIsValidCode($code)
			{
				$sql = 'SELECT user_id, name, display_name, usr_status, password, email FROM ' .$this->CFG['db']['tbl']['users'].
					  ' WHERE substring(md5(name), 1, 8) = '.$this->dbObj->Param('field_name').' AND usr_status!=\'Deleted\'';

				$stmt = $this->dbObj->Prepare($sql);
                $rs = $this->dbObj->Execute($stmt, array($code));
                if (!$rs)
            	    trigger_db_error($this->dbObj);
				if ($row = $rs->FetchRow())
					{
						return $row['user_id'];
					}
				else
					{
						$this->setCommonErrorMsg($this->LANG['common_msg_error_sorry']);
						return false;
					}
			}

		/**
		 * verifyMailHandler::chkIsNotDuplicateEmail()
		 *
		 * @param mixed $table_name
		 * @param mixed $field_name
		 * @param string $err_tip
		 * @return
		 */
		public function chkIsNotDuplicateEmail($table_name, $field_name, $err_tip='')
			{
				$sql = 'SELECT '.$this->getUserTableField('user_id').' FROM ' . $table_name .
				 	   ' WHERE '.$this->getUserTableField('email').' ='.$this->dbObj->Param($field_name);
				// prepare query
				$stmt = $this->dbObj->Prepare($sql);
				// execute query
				$rs = $this->dbObj->Execute($stmt, array($this->fields_arr[$field_name]));
				//raise user error... fatal
				if (!$rs)
					trigger_db_error($this->dbObj);
				// counts number of rows
				$numrows = $rs->PO_RecordCount();
				// finds row exists
				$is_ok = ($numrows == 0 ) ;
				// set error tips
				if (!$is_ok)
					$this->fields_err_tip_arr[$field_name] =  $err_tip;
				return $is_ok;
			}

		/**
		 * To send email
		 *
		 * @param 		string $to_email to email id
		 * @param 		string $subject subject
		 * @param		string $body mail body
		 * @param 		string $sender_name sender name
		 * @param 		string $sender_email sender email
		 * @return 		void
		 * @access 		private
		 */
		private function _sendMail($to_email, $subject, $body, $sender_name, $sender_email)
			{
			    $this->buildEmailTemplate($subject, $body, false, true);
				$EasySwift = new EasySwift($this->getSwiftConnection());
				$EasySwift->flush();
				$EasySwift->addPart($this->getEmailContent(), "text/html");
				$from_address = $sender_name.'<'.$sender_email.'>';
				return $EasySwift->send($to_email, $from_address, $this->getEmailSubject());
			}

		/**
		 * To update user log information
		 *
		 * @param 		string $table_name table name
		 * @param 		string $ip ip address
		 * @param 		string $session_id session id
		 * @return 		void
		 * @access 		public
		 */
		public function updateMail()
			{
				$mail = $this->fields_arr['new_mail'];
				$id = $this->fields_arr['user_id'];

				$sql = 'UPDATE '.$this->CFG['db']['tbl']['users'].
  					   ' SET '.$this->getUserTableField('email').'=\''.$mail.'\' WHERE '.$this->getUserTableField('user_id').'='.$id;

				$stmt = $this->dbObj->Prepare($sql);
				$result = $this->dbObj->Execute($stmt);
				if (!$result)
					trigger_db_error($this->dbObj);
			}


	}
//<<<<<----------------- Class verifyMailHandler ends ------------//
//----------------------- Code begins -------------->>>>>//
#print_r($_POST);
$verifyfrm = new verifyMailHandler();
$verifyfrm->setPageBlockNames(array('form_verifymail'));
// Set the form fields
$verifyfrm->setFormField('code', '');
$verifyfrm->setFormField('mail_code', '');
$verifyfrm->setFormField('new_mail', '');
//Corresponding User id for this code
$verifyfrm->setFormField('user_id', '');
$verifyfrm->setAllPageBlocksHide();
$verifyfrm->setPageBlockShow('form_verifymail');
$verifyfrm->sanitizeFormInputs($_REQUEST);
$activated_account = false;
if ($verifyfrm->isFormPOSTed($_POST, 'new_mail'))
	{
		$verifyfrm->chkIsNotEmpty('new_mail', $LANG['err_tip_compulsory']) and
			$verifyfrm->chkIsValidEmail('new_mail', $LANG['err_tip_invalid_email']) and
				($verifyfrm->chkIsNotDuplicateEmail($CFG['db']['tbl']['users'], 'new_mail', $LANG['err_tip_email_already_exists']) or
					$verifyfrm->setCommonErrorMsg($LANG['common_msg_error_sorry']));
					if ($verifyfrm->isValidFormInputs())
						{
							$newMail = substr(md5($verifyfrm->getFormField('new_mail')),0,8);
							$mailCode = $verifyfrm->getFormField('mail_code');
							if($newMail == $mailCode)
								 {
									 $verifyfrm->updateMail();
									 $verifyfrm->setFormField('new_mail', '');
									 $verifyfrm->setAllPageBlocksHide();
									 $verifyfrm->setCommonSuccessMsg($LANG['email_successfully']);
									 $verifyfrm->setPageBlockShow('block_msg_form_success');
								 }
							 else
								 {
									$verifyfrm->setAllPageBlocksHide();
									$verifyfrm->setCommonErrorMsg($LANG['common_msg_error_sorry']);
									$verifyfrm->setFormFieldErrorTip('new_mail', $LANG['err_tip_invalid_email']);
									$verifyfrm->setPageBlockShow('block_msg_form_error');
									$verifyfrm->setIndirectFormField('new_mail', '');
									$verifyfrm->setPageBlockShow('form_verifymail');
								 }
						 }
					else
						{
							$verifyfrm->setAllPageBlocksHide();
							$verifyfrm->setCommonErrorMsg($LANG['common_msg_error_sorry']);
							$verifyfrm->setPageBlockShow('block_msg_form_error');
							$verifyfrm->setIndirectFormField('new_mail', '');
							$verifyfrm->setPageBlockShow('form_verifymail');
						}


	}
elseif ($verifyfrm->isFormGETed($_GET, 'code'))
	{

		$code = substr($verifyfrm->getFormField('code'), 0,8);
		$mailcode = substr($verifyfrm->getFormField('code'), 8,8);
		$verifyfrm->setFormField('mail_code', $mailcode);

		if($verifyfrm->chkIsValidCode($code) !== false)
			{
				$verifyfrm->setFormField('user_id', $verifyfrm->chkIsValidCode($code));
				$verifyfrm->setAllPageBlocksHide();
				$verifyfrm->setPageBlockShow('form_verifymail');
			}
		else
			{
				$verifyfrm->setAllPageBlocksHide();
				$verifyfrm->setCommonErrorMsg($verifyfrm->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('general/');
$smartyObj->display('activateMailAccount.tpl');
//includ the footer of the page
//<<<<<--------------Page block templates ends----------------//
$verifyfrm->includeFooter();
?>
