<?php
/**
 * File to handle the forget password
 *
 * Using this file if the user forget his password detail, by entering some
 * required information the user can get his password back to his mail.
 *
 * PHP version 5.0
 *
 * @category	Anova
 * @package		ForgotpasswordFormHandler
 * @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: forgotPassword.php 736 2008-05-26 06:34:50Z selvaraj_47ag04 $
 * @since 		2008-12-22
 */
/**
 * 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/forgotPassword.php';
$CFG['html']['header'] = 'general/html_header.php';
$CFG['html']['footer'] = 'general/html_footer.php';
$CFG['lang']['include_files'][] = 'languages/%s/common/email_notify.inc.php';
if ($_POST)
    $CFG['mods']['include_files'][] = 'common/classes/swiftmailer/lib/EasySwift.php';

//compulsory
$CFG['mods']['is_include_only']['html_header'] = false;
$CFG['html']['is_use_header'] = false;
$CFG['auth']['is_authenticate'] = 'root';
/**
 * To include application top file
 */
require($CFG['site']['project_path'].'common/application_top.inc.php');
//-------------- Class ForgotpasswordFormHandler begins ---------------->>>>>//
/**
 * This class handles the user forget password options
 */
class ForgotpasswordFormHandler extends FormHandler
	{
	   /**
          * To get the password details
          *
          * @param 		string $table_name  table name
          * @param 		string $email email of the users
          * @access 	public
          * @return 	boolean
          **/
		public function chkIsValidUser($email)
			{
				$sql = 'SELECT user_id, name, display_name FROM '.$this->CFG['db']['tbl']['users'].
						' WHERE email = '.$this->dbObj->Param($this->fields_arr[$email]);
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($this->fields_arr[$email]));
				if (!$rs)
					    trigger_db_error($this->dbObj);
				if ($rs->PO_RecordCount())
					{
					    $row = $rs->FetchRow();
					    return $row;
					}
				return false;
			}

		/**
		 * To send the activation code
		 *
		 * @param 		string $field_user_id user id
		 * @param 		string $field_email email
		 * @param 		string $activation_subj mail subject
		 * @param 		string $activation_msg mail message content
		 * @param 		string $return_url return url
		 * @param 		string $sender_name sender name
		 * @param 		string $sender_email sender email
		 * @return 		boolean
		 * @access 		public
		 */
		public function sendActivationCode($field_user_id, $field_email, $activation_subj, $activation_msg, $return_url='', $sender_name, $sender_email)
			{
				$act_code = substr(md5($this->fields_arr[$field_user_id]), 0, 8);
				if($return_url)
					{
						$activation_link =  URL($return_url.urlencode($act_code));
					}
				else
					{
						$activation_link = '';
					}
				$activation_msg = str_ireplace('ACTIVATION_LINK', $activation_link, $activation_msg);
				$EasySwift = new EasySwift($this->getSwiftConnection());
				$EasySwift->flush();
				if($EasySwift->send($this->fields_arr[$field_email], $sender_email, $activation_subj, $activation_msg))
					$is_ok = true;
				else
					$is_ok = false;
				return $is_ok;
			}

		public function IsOpenidUser($user_id)
			{
				$sql = ' SELECT * FROM '.$this->CFG['db']['tbl']['user_identity'].''.
					   ' WHERE user_id = '.$this->dbObj->Param('user_id');
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($this->fields_arr['user_id']));
				$numrows = $rs->PO_RecordCount();
				if($numrows >0)
					return false;
				else
					return true;

			}
		public function IsTwitterUser($user_id)
			{
				$sql = ' SELECT * FROM '.$this->CFG['db']['tbl']['twitter_identity'].''.
					   ' WHERE user_id = '.$this->dbObj->Param('user_id');
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($this->fields_arr['user_id']));
				$numrows = $rs->PO_RecordCount();
				if($numrows >0)
					return false;
				else
					return true;

			}
	}
//<<<<<--------------- Class ForgotpasswordFormHandler ends -------------//
//-------------------- Code begins -------------->>>>>//
$forgotpasswordfrm = new ForgotpasswordFormHandler();
$forgotpasswordfrm->setPageBlockNames(array('block_Forgotpassword'));
// Set the form fields
$forgotpasswordfrm->setFormField('email', '');
$forgotpasswordfrm->setFormField('user_id', '');

$forgotpasswordfrm->setAllPageBlocksHide();
// default page block. show it. All others hidden
$forgotpasswordfrm->setPageBlockShow('block_Forgotpassword');
if ($forgotpasswordfrm->isFormPOSTed($_POST, 'forgot_submit'))
	{
		$forgotpasswordfrm->sanitizeFormInputs($_POST);
		$forgotpasswordfrm->setDBObject($db);
		$forgotpasswordfrm->chkIsNotEmpty('email', $LANG['forgot_err_tip_compulsory']) and
			$forgotpasswordfrm->chkIsValidEmail('email', $LANG['forgot_err_tip_invalid_email']);
		if ($forgotpasswordfrm->isValidFormInputs())
			{
				//Add Coding
				if ($user_details = $forgotpasswordfrm->chkIsValidUser('email'))
					{
				    		$forgotpasswordfrm->setIndirectFormField('user_id', $user_details['user_id']);

				    		if(chkAllowedModule(array('openid')) AND $forgotpasswordfrm->IsOpenidUser($forgotpasswordfrm->getFormField('user_id')))
								{
									$message = str_ireplace('VAR_USERNAME', $user_details['display_name'], $LANG['forgot_message']);
									if ($forgotpasswordfrm->sendActivationCode('user_id', 'email',
																$subject,
																$message,
																getUrl('verify', '?code=', '?code=' , 'root'),
																$CFG['site']['noreply_name'],
																$CFG['site']['noreply_email']))
										$forgotpasswordfrm->setCommonSuccessMsg($LANG['forgot_activation_code_sent']);
									else
										$forgotpasswordfrm->setCommonSuccessMsg($LANG['common_email_failure']);
								}
							else if(chkAllowedModule(array('facebook')) AND $forgotpasswordfrm->isFacebookUser($forgotpasswordfrm->getFormField('user_id')))
								{
									$message = str_ireplace('VAR_USERNAME', $user_details['display_name'], $LANG['invalid_fb_forgot_message']);
									if ($forgotpasswordfrm->sendActivationCode('user_id', 'email',
																$LANG['forgot_subject'],
																$message,
																'',
																$CFG['site']['noreply_name'],
																$CFG['site']['noreply_email']))
										$forgotpasswordfrm->setCommonSuccessMsg($LANG['forgot_activation_code_sent']);
									else
										$forgotpasswordfrm->setCommonSuccessMsg($LANG['common_email_failure']);
								}
							else if(chkAllowedModule(array('ning')) AND $forgotpasswordfrm->chkIsNingUser($forgotpasswordfrm->getFormField('user_id')))
								{
									$message = str_ireplace('VAR_USERNAME', $user_details['display_name'], $LANG['invalid_ning_forgot_message']);
									if ($forgotpasswordfrm->sendActivationCode('user_id', 'email',
																$LANG['forgot_subject'],
																$message,
																'',
																$CFG['site']['noreply_name'],
																$CFG['site']['noreply_email']))
										$forgotpasswordfrm->setCommonSuccessMsg($LANG['forgot_activation_code_sent']);
									else
										$forgotpasswordfrm->setCommonSuccessMsg($LANG['common_email_failure']);
								}
							else if(chkAllowedModule(array('twitter')) AND $forgotpasswordfrm->IsTwitterUser($forgotpasswordfrm->getFormField('user_id')))
								{
									$message = str_ireplace('VAR_USERNAME', $user_details['display_name'], $LANG['forgot_message']);
									if ($forgotpasswordfrm->sendActivationCode('user_id', 'email',
																$subject,
																$message,
																getUrl('verify', '?code=', '?code=' , 'root'),
																$CFG['site']['noreply_name'],
																$CFG['site']['noreply_email']))
										$forgotpasswordfrm->setCommonSuccessMsg($LANG['forgot_activation_code_sent']);
									else
										$forgotpasswordfrm->setCommonSuccessMsg($LANG['common_email_failure']);

									//everything fine. show success
									$forgotpasswordfrm->setAllPageBlocksHide();
									$forgotpasswordfrm->setPageBlockShow('block_msg_form_success');
									$forgotpasswordfrm->setFormField('email', '');
									//$forgotpasswordfrm->setPageBlockShow('block_Forgotpassword');
								}
							else
								{
									$message = str_ireplace('VAR_USERNAME', $user_details['display_name'], $LANG['invalid_forgot_message']);
									$message = str_ireplace('VAR_SITENAME', $CFG['site']['noreply_name'], $message);
									//$message = html_entity_decode(nl2br($message),ENT_QUOTES);
									if ($forgotpasswordfrm->sendActivationCode('user_id', 'email',
																$LANG['forgot_subject'],
																$message,
																'',
																$CFG['site']['noreply_name'],
																$CFG['site']['noreply_email']))
										$forgotpasswordfrm->setCommonSuccessMsg($LANG['forgot_activation_code_sent']);
									else
										{
											$forgotpasswordfrm->setCommonSuccessMsg($LANG['forgot_activation_code_not_sent']);
										}
								}

						//everything fine. show success
						$forgotpasswordfrm->setAllPageBlocksHide();
						$forgotpasswordfrm->setPageBlockShow('block_msg_form_success');
					}
				else
					{
						$forgotpasswordfrm->setAllPageBlocksHide();
						$forgotpasswordfrm->setPageBlockShow('block_msg_form_error');
						$forgotpasswordfrm->setCommonErrorMsg($LANG['forgot_err_tip_data_not_exists']);
						$forgotpasswordfrm->setPageBlockShow('block_Forgotpassword');
					}
			}
		else //error in form inputs
			{
				$forgotpasswordfrm->setAllPageBlocksHide();
				$forgotpasswordfrm->setCommonErrorMsg($LANG['common_msg_error_sorry']);
				$forgotpasswordfrm->setPageBlockShow('block_msg_form_error');
				$forgotpasswordfrm->setPageBlockShow('block_Forgotpassword');
			}
	}
//<<<<<------------------- Code ends ----------------------//

//--------------------Page block templates begins-------------------->>>>>//
//include the header file
$forgotpasswordfrm->includeHeader();
//include the content of the page
setTemplateFolder('root/');
$smartyObj->display('forgotPassword.tpl');
//includ the footer of the page
//<<<<<<--------------------Page block templates Ends--------------------//
$forgotpasswordfrm->includeFooter();
?>