<?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: LoginController.php, v 1.0 2011/06/14 20:52:11
//
class LoginController extends Controller {
    //LOGIN
    public function indexAction() {
        if ($this->ObjSession->getSession("AFFILIATE_USER_ID") != '') {
            $this->setRedirect("index/");
            exit;
        }
        $ObjAffiliate                          = new AffiliateModel();
        $ObjContent                            = new ContentModel();
        $ObjUrlrewrite                         = new UrlrewriteModel();
        $languageIDs                           = $this->ObjView->languageIDs;
        $ObjUser                               = new UserModel();
        $enableAccountBlocking                 = $this->getSettingValue('security_settings', 'security_account_blocking');
        $blockingPeriod                        = $this->getSettingValue('security_settings', 'blocking_period');
        $settingsSecurityAdminBlockingAttempts = $this->getSettingValue('security_settings', 'security_account_blocking_attempts');
        if ($this->ObjForm->isValid('formAffiliateLogin', 'submitLogin')) {
            $affiliateLogin          = $this->escape($_POST['affiliateLogin']);
            $affiliatePassword       = trim($_POST['affiliatePassword']);
            $resAffiliateDetails     = $ObjAffiliate->getAffiliateIDforLogin($affiliateLogin);
            $hashedPassword          = $resAffiliateDetails['affiliatePassword'];
            $pwd_exists              = $ObjUser->verify($affiliatePassword, $hashedPassword);
            $msgAccountIs            = $this->getLanguageValue("msgAccountIs");
            $msgFor                  = $this->getLanguageValue("msgFor");
            $msgBlockedFor           = $this->getLanguageValue("msgBlockedFor");
            $msgMinutes              = $this->getLanguageValue("msgMinutes");
            $msgContactAdmin         = $this->getLanguageValue("msgContactAdmin");
            $affiliateLastAccessDate = $resAffiliateDetails['affiliateLastAccesed'];
            if (!empty($resAffiliateDetails) && ($pwd_exists)) {
                $updateLastAccess          = $ObjAffiliate->updateAffiliateLastAccess($resAffiliateDetails['affiliateUserID']);
                $affiliateStatus           = $resAffiliateDetails['affiliateStatus'];
                $affiliateUserID           = $resAffiliateDetails['affiliateUserID'];
                $resCheckLoginAttemptCount = $ObjAffiliate->updateInvalidLoginAttemptCount($affiliateLogin, $settingsSecurityAdminBlockingAttempts);
                if ($affiliateStatus == 'Locked' && $resCheckLoginAttemptCount != 99) {
                    $lockedTime  = strtotime($resAffiliateDetails['loginLockedDate']);
                    $currentTime = strtotime(date("Y-m-d H:i:s"));
                    $diff        = abs($currentTime - $lockedTime);
                    $years       = floor($diff / (365 * 60 * 60 * 24));
                    $months      = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
                    $days        = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
                    $hours       = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24) / (60 * 60));
                    $diff_minuts = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24 - $hours * 60 * 60) / 60);
                    $diff_minuts = $diff / 60;
                    if ($diff_minuts > $blockingPeriod) {
                        $resActivateUserAccount = $ObjAffiliate->activateAffiliateAccount($affiliateUserID);
                        $resAffiliateDetails    = $ObjAffiliate->getAffiliateIDforLogin($affiliateLogin);
                        $affiliateStatus        = $resAffiliateDetails['affiliateStatus'];
                    } else {
                        $msgAccountIsStatusContactAdmin = $msgAccountIs . $affiliateStatus . $msgFor . $blockingPeriod . $msgMinutes;
                        $this->setMessage($msgAccountIsStatusContactAdmin, "message_error");
                        $this->setRedirect();
                    }
                }
                if ($affiliateStatus != "Deleted" && $affiliateStatus != "Inactive" && $affiliateStatus != "Locked") {
                    $resActivateUserAccount  = $ObjAffiliate->activateAffiliateAccount($affiliateUserID);
                    $affiliateUserID         = $resAffiliateDetails['affiliateUserID'];
                    $affiliateName           = $resAffiliateDetails['affiliateName'];
                    $affiliateIPAddress      = $this->getIpAddress();
                    $affiliateAccessDateTime = date("l, F d, Y- h:m A");
                    $affiliateLastAccessDate = ($affiliateLastAccessDate == '' || $affiliateLastAccessDate == '0000-00-00 00:00:00') ? $affiliateAccessDateTime : $affiliateLastAccessDate;
                    $this->ObjSession->setSession("AFFILIATE_USER_ID", $affiliateUserID);
                    $this->ObjSession->setSession("AFFILIATE_NAME", $affiliateName);
                    $this->ObjSession->setSession("AFFILIATE_USER_IP", $affiliateIPAddress);
                    $this->ObjSession->setSession("AFFILIATE_USER_LAST_ACCESS", $affiliateLastAccessDate);
                    //Aff language Setings
                    $languageID = ($resAffiliateDetails['preferredLanguage'] != '') ? $resAffiliateDetails['preferredLanguage'] : $languageIDs['Current'];
                    $this->ObjSession->setSession("AFFILIATE_LANG_ID", $languageID);
                    //Timeout
                    $timeout = trim($this->getSettingValue('security_settings', 'security_user_timeout'));
                    if ($timeout != '') {
                        $this->ObjSession->setSession("TIMEOUT", time());
                    }
                    $this->ObjCookie->setCookie($this->ObjView->_section . '_CurrentLanguageID', $languageID);
                    $this->setRedirect("/index/");
                    exit;
                } else if ($affiliateStatus == 'Inactive') {
                    $this->setMessage("language.msgAccountInActive", "message_error");
                    $this->setRedirect("/login/");
                    exit;
                } else {
                    if ($resCheckLoginAttemptCount == 99) {
                        $msgAccountIs                   = $this->getLanguageValue("msgAccountIs");
                        $msgContactAdmin                = $this->getLanguageValue("msgContactAdmin");
                        $msgAccountIsStatusContactAdmin = $msgAccountIs . " blocked " . $msgContactAdmin;
                        $this->setMessage($msgAccountIsStatusContactAdmin, "message_error");
                        $this->setRedirect();
                    } else {
                        $this->setMessage("language.msgAccountBlocked", "message_error");
                        $this->setRedirect();
                    }
                }
            } else {
                $resAffiliateDetails = $ObjAffiliate->getAffiliateIDforLogin($affiliateLogin);
                $affiliateStatus     = $resAffiliateDetails['affiliateStatus'];
                $affiliateUserID     = $resAffiliateDetails['affiliateUserID'];
                if ($affiliateStatus == "Inactive") {
                    $this->setMessage("language.msgAccountInActive", "message_error");
                    $this->setRedirect();
                } else {
                    if ($enableAccountBlocking == 'Yes' && !empty($resAffiliateDetails)) {
                        if ($affiliateStatus == 'Locked' && $resAffiliateDetails['loginInvalidAccessCount'] != 99) {
                            $lockedTime  = strtotime($resAffiliateDetails['loginLockedDate']);
                            $currentTime = strtotime(date("Y-m-d H:i:s"));
                            $diff        = abs($currentTime - $lockedTime);
                            $years       = floor($diff / (365 * 60 * 60 * 24));
                            $months      = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
                            $days        = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
                            $hours       = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24) / (60 * 60));
                            $diff_minuts = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24 - $hours * 60 * 60) / 60);
                            $diff_minuts = $diff / 60;
                            if ($diff_minuts > $blockingPeriod) {
                                $resActivateUserAccount    = $ObjAffiliate->activateAffiliateAccount($affiliateUserID);
                                $resCheckLoginAttemptCount = $ObjAffiliate->updateInvalidLoginAttemptCount($affiliateLogin, $settingsSecurityAdminBlockingAttempts);
                                $this->setMessage("language.msgPasswordInvalid", "message_error");
                                $this->setRedirect();
                            } elseif ($resCheckLoginAttemptCount == $settingsSecurityAdminBlockingAttempts) {
                                $msgAccountIsStatusContactAdmin = $msgAccountIs . $msgBlockedFor . $blockingPeriod . $msgMinutes;
                                $this->setMessage($msgAccountIsStatusContactAdmin, "message_error");
                                $this->setRedirect();
                            } elseif ($resCheckLoginAttemptCount > $settingsSecurityAdminBlockingAttempts) {
                                $this->setMessage("language.msgAccountBlocked", "message_error");
                                $this->setRedirect();
                            }
                        }
                        $resCheckLoginAttemptCount = $ObjAffiliate->updateInvalidLoginAttemptCount($affiliateLogin, $settingsSecurityAdminBlockingAttempts);
                        if ($resCheckLoginAttemptCount == 99) {
                            $msgAccountIs                   = $this->getLanguageValue("msgAccountIs");
                            $msgContactAdmin                = $this->getLanguageValue("msgContactAdmin");
                            $msgAccountIsStatusContactAdmin = $msgAccountIs . " blocked " . $msgContactAdmin;
                            $this->setMessage($msgAccountIsStatusContactAdmin, "message_error");
                            $this->setRedirect();
                        } elseif ($resCheckLoginAttemptCount == $settingsSecurityAdminBlockingAttempts) {
                            $msgAccountIsStatusContactAdmin = $msgAccountIs . $msgBlockedFor . $blockingPeriod . $msgMinutes;
                            $this->setMessage($msgAccountIsStatusContactAdmin, "message_error");
                            $this->setRedirect();
                        } elseif ($resCheckLoginAttemptCount > $settingsSecurityAdminBlockingAttempts) {
                            $this->setMessage("language.msgAccountBlocked", "message_error");
                            $this->setRedirect();
                        } else {
                            $this->setMessage("language.msgPasswordInvalid", "message_error");
                            $this->setRedirect();
                        }
                    } else {
                        $this->setMessage("language.msgPasswordInvalid", "message_error");
                        $this->setRedirect();
                    }
                }
            }
        }
        //Get Affiliate CMS PAGE Link
        //Get Affiliate CMS PAGE Link
        $contentID      = $ObjContent->getContentID('affiliates');
        $affRequestPage = $ObjUrlrewrite->getRequestPathByIdPath($languageIDs['Current'], 'page/' . $contentID . '/');
        $global_url     = rtrim($this->getSettingValue('site_settings', 'global_http_url'), "/") . "/";
        $this->ObjView->assign('becomeAffLink', $global_url . $affRequestPage['requestPath']);
        $this->ObjView->display('Login.tpl');
    }
}