<?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
//
class ForgotpasswordController extends Controller {
    //LOGIN
    public function indexAction() {
        $affiliateID = $this->ObjSession->getSession("AFFILIATE_USER_ID");
        if ($affiliateID) {
            $this->setRedirect('/index/');
        }
        $ObjAffiliate     = new AffiliateModel();
        $ObjEmailtemplate = new EmailtemplateModel();
        $languageIDs      = $this->ObjView->languageIDs;
        $enableCaptcha    = $this->getSettingValue('security_settings', 'enable_captcha');
        $emailbody        = $ObjEmailtemplate->getEmailTemplateBasic('product-assistance-password', 'emailFromEmail');
        if ($this->ObjForm->isValid('formAffiliateForgotPassword', 'SendPassword')) {
            $affSecurityCode = $_REQUEST['affSecurityCode'];
            $ses_code        = $this->ObjSession->getSession("ses_code");
            $affSecurityCode = isset($affSecurityCode) ? $affSecurityCode : '';
            if ($affSecurityCode == $ses_code || $enableCaptcha == '') {
                $affiliateUsername   = $_POST['affiliateLogin'];
                $resaffiliateDetails = $ObjAffiliate->getAffiliateIDforLogin($affiliateUsername);
                $resetPassword       = $ObjAffiliate->changeAffiliatePassword($affiliateUsername);
                if ($resaffiliateDetails && $resetPassword != '0') {
                    if ($resaffiliateDetails['preferredLanguage'] > 0) {
                        $languageIDs['Current'] = $resaffiliateDetails['preferredLanguage'];
                    }
                    $emailbody = $ObjEmailtemplate->getEmailTemlateDetails($languageIDs, '', 'product-assistance-password');
                    // sent mail to user
                    $from      = array(
                        $emailbody['emailFromEmail'],
                        $emailbody['emailFromName']
                    );
                    $subject   = $emailbody['emailSubject'];
                    $siteName  = $this->getSettingValue('site_settings', 'site_name');
                    $userName  = $resaffiliateDetails['affiliateName'];
                    //email body
                    $bodytag   = str_replace('{$siteName}', $siteName, $emailbody['emailHtml']);
                    $bodytag   = str_replace('{$password}', $resetPassword, $bodytag);
                    $bodytag   = str_replace('{$userName}', $userName, $bodytag);
                    $this->ObjView->assign('emailbody', $bodytag);
                    $emailID                        = $resaffiliateDetails['affiliateEmail'];
                    $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->setMessage("language.lblOldForgotPasswordSent", 'message_success', 'Forgotpassword', 'forgot_password_msg');
                        $this->setRedirect('/login/');
                    }
                } else if ($resaffiliateDetails && $resaffiliateDetails['affiliateStatus'] == 'Inactive') {
                    $this->setMessage("language.lblForgotPasswordMailFailed", "message_error");
                    $this->setRedirect();
                } else {
                    $this->setMessage("language.msgUsernameInvalid", "message_error");
                    $this->setRedirect();
                }
            } else {
                $this->setMessage("language.errSecurityCode", "message_error");
                $this->setRedirect();
            }
        }
        $this->ObjView->assign('enableCaptcha', $enableCaptcha);
        $this->ObjView->assign('fromEmail', $emailbody['emailFromEmail']);
        $this->ObjView->display('ForgotPassword.tpl');
    }
    //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
}