<?php

require_once(DIR_FS_INC . 'xtc_validate_email.inc.php');
require_once(DIR_FS_INC . 'xtc_write_user_info.inc.php');
require_once(DIR_FS_INC . 'xtc_validate_password.inc.php');


/**
 * check the group permission
 */
$writeCheck = $voting->getGroupAccess($voting->customer_group_write, (int)$_SESSION['customers_status']['customers_status_id']);
$captchaCheck = $voting->getGroupAccess($voting->customer_group_captcha, (int)$_SESSION['customers_status']['customers_status_id']);
$pagepart = $voting->pagepart();


if ($voting->voting_module_active == 0) {
    xtc_redirect(xtc_href_link(FILENAME_DEFAULT, '', 'NONSSL'));
} elseif ($writeCheck != 1) {

    $emailAddress = xtc_db_prepare_input($_POST['email_address']);
    $password = xtc_db_prepare_input($_POST['password']);
    $customersArray = $voting->getLoginInfos($emailAddress);
    $checkPassword = $customersArray['customers_password'];

    $smarty->assign('WRITECHECK', 'no');
    $smarty->assign('FORM_LOGIN_LINK', xtc_href_link(FILENAME_WEB4DESIGN_DE_REVIEW_WRITE));

    if (empty($customersArray) && sizeof($_POST) > 0) {
        $smarty->assign('MAIL_ERROR_CLASS', 1);
        $smarty->assign('MAIL_ERROR_TEXT', NOT_REGISTERED);
    } else {

        if (is_null($checkPassword) || is_null($_POST['email_address'])) {
            $smarty->assign('PASSWORD_ERROR_CLASS', 1);
            $smarty->assign('PASSWORD_ERROR_TEXT', WRONG_PASSWORD);
        }
        elseif (!xtc_validate_password($password, $checkPassword, $customersArray['customers_id'])) {
            if (!empty($_POST)) {
                $smarty->assign('PASSWORD_ERROR_CLASS', 1);
                $smarty->assign('PASSWORD_ERROR_TEXT', WRONG_PASSWORD);
            }

        } else {

            if (SESSION_RECREATE == 'True') {
                xtc_session_recreate();
            }

            $voting->setSessionInfos($customersArray);

            $date_now = date('Ymd');

            xtc_db_query("update " . TABLE_CUSTOMERS_INFO . " SET customers_info_date_of_last_logon = now(), customers_info_number_of_logons = customers_info_number_of_logons+1 WHERE customers_info_id = '" . (int)$_SESSION['customer_id'] . "'");
            xtc_write_user_info((int)$_SESSION['customer_id']);
            // restore cart contents
            $_SESSION['cart']->restore_contents();

            if (is_object($econda)) $econda->_loginUser();

            xtc_redirect(xtc_href_link(FILENAME_WEB4DESIGN_DE_REVIEW_WRITE), 'SSL');

        }
    }
}
