<?php
/***************************************************************************
 *                                                                          *
 * Copyright (c) 2005 ISPG Technologies (I) Pvt Ltd. All rights reserved.   *
 *                                                                          *
 * This  is  commercial  software,  only  users  who have purchased a valid *
 * license  and  accept  to the terms of the  License Agreement can install *
 * and use this program.                                                    *
 *                                                                          *
 ****************************************************************************
 * PLEASE READ THE FULL TEXT  OF THE SOFTWARE  LICENSE   AGREEMENT  IN  THE *
 * "copyright.txt" FILE PROVIDED WITH THIS DISTRIBUTION PACKAGE.            *
 ****************************************************************************/
//
//$Id: ForgotpasswordController.php, v 1.0 2011/06/14 20:52:11
//
@session_start();
class ForgotpasswordController extends Controller {
    //FUNCTION TO SEND PASSWORD 
    public function indexAction() {
        $ObjEmailtemplate = new EmailtemplateModel();
        $languageIDs      = $this->ObjView->languageIDs;
        $enableCaptcha    = $this->getSettingValue('security_settings', 'enable_captcha');
        $this->ObjView->assign('enableCaptcha', $enableCaptcha);
        $businessID = $this->ObjSession->getSession("BUSINESS_USER_ID");
        /*
        if ($businessID) {
            $this->setRedirect('/index/');
        }*/
        $ObjBusiness      = new BusinessModel();
        $ObjEmailtemplate = new EmailtemplateModel();
        if ($this->ObjForm->isValid('formForgotPassword', 'SendPassword')) {
            $adminSecurityCode = $_REQUEST['adminSecurityCode'];
            $ses_code          = $_SESSION['captcha_val'];            
            $adminSecurityCode = isset($adminSecurityCode) ? $adminSecurityCode : '';
            if (($adminSecurityCode == $ses_code && $enableCaptcha!='') || $enableCaptcha == '') {
                $businessLogin      = $_POST['businessLogin'];
                $resbusinessDetails = $ObjBusiness->getBusinessIDforLogin($businessLogin);
                if ($resbusinessDetails && $resbusinessDetails['businessStatus'] == 'Active' && $resetPassword != '0') {
                    $resetPassword          = $ObjBusiness->changeBusinessPassword($businessLogin);
                    // sent mail to user
                    $languageIDs['Current'] = $resbusinessDetails['businessDefaultLanguageID'];
                    $emailbody              = $ObjEmailtemplate->getEmailTemlateDetails($languageIDs, '', 'product-assistance-password');
                    $from                   = array(
                        $emailbody['emailFromEmail'],
                        $emailbody['emailFromName']
                    );
                    $subject                = $emailbody['emailSubject'];
                    $siteName               = $this->getSettingValue('site_settings', 'site_name');
                    $siteUrl                = $this->getSettingValue('site_settings', 'global_http_url');
                    $companyLogo            = $this->getSettingValue('company_information', 'company_logo');
                    $userName               = $resbusinessDetails['businessCFirstName'] . " " . $resbusinessDetails['businessCLastName'];
                    $this->ObjView->assign('companyLogo', $companyLogo);
                    //email body
                    $newpassword = $resetPassword;
                    $bodytag     = str_replace('{$siteName}', $siteName, $emailbody['emailHtml']);
                    $bodytag     = str_replace('{$password}', $newpassword, $bodytag);
                    $bodytag     = str_replace('{$siteName}', $siteName, $bodytag);
                    $bodytag     = str_replace('{$userName}', $userName, $bodytag);
                    $bodytag     = str_replace('{$companyLogo}', $companyLogo, $bodytag);
                    $this->ObjView->assign('emailbody', $bodytag);
                    $this->ObjView->assign('siteUrl', $siteUrl);
                    $this->ObjView->assign('siteName', $siteName);
                    $this->ObjView->assign('userName', $userName);
                    $this->ObjView->assign('companyLogo', $companyLogo);
                    $emailID                        = $resbusinessDetails['businessEmail'];
                    $body                           = $this->ObjCommon->fetch($this->ObjView, "email_templates/Layout.tpl", $languageIDs);
                    $mail_settings                  = $this->getSettingValue('mail_settings');
                    $mail_settings['smtp_password'] = $this->encryptDecrypt("decrypt", $mail_settings['smtp_password']);
                    $ObjMail                        = new Mail($mail_settings);
                    $ObjMail->IsHTML();
                    $ObjMail->sendMail($from, $emailID, $subject, $body);
                    if ($ObjMail->Send()) {
                        $this->setMessage("language.lblOldForgotPasswordSent", "message_success");
                        $this->setRedirect();
                    }
                } else if ($resbusinessDetails && $resbusinessDetails['businessStatus'] == 'Inactive') {
                    $this->setMessage("language.lblForgotPasswordMailFailed", "message_error");
                    $this->setRedirect();
                } else if ($resbusinessDetails && $resbusinessDetails['businessStatus'] == 'Locked') {
                    $this->setMessage("language.msgAccountBlocked", "message_error");
                    $this->setRedirect();
                } else {
                    $this->setMessage("language.msgUsernameNotFound", "message_error");
                    $this->setRedirect();
                }
            } else {
                $this->setMessage("language.errInvalidSecurityCode", "message_error");
                $this->setRedirect();
            }
        }
        $resEmail = $ObjEmailtemplate->getEmailTemplateBasic('product-assistance-password', 'emailFromEmail');
        $this->ObjView->assign('fromEmail', $resEmail['emailFromEmail']);
        $this->ObjView->display('ForgotPassword.tpl');
    }
    //END OF FUNCTION
    //TO DISPALY SECURITY CAPTCHA
    public function imageCaptchaAction() {
        $ObjAdminuser = new AdminuserModel();
        $style        = $this->getSettingValue('security_settings', 'captcha');
        $captcha      = $ObjAdminuser->CaptchaSecurityImages($this->ObjSession, '120', '40', '6', $style);
    }
    //END OF FUNCTION
}