<?php

if (!defined('MODULE_LOADED')) {
  include(PATH . 'control/system/headers/403.php');
  exit;
}

if (!empty($pr_areas) && isset($_GET['tos'])) {
  define('P_POLICY', 1);
  $templateParams = [
    'TEXT' => [
      $msw_public_11_global
    ],
    'POLICY' => $SANTAGS->filter($SETTINGS->pr)
  ];
  // Global..
  include(PATH . 'control/system/global.php');
  $latte->render('privacy-policy.tpl', $templateParams);
  exit;
}

if (defined('LOGOUT_OPS')) {
  $SSN->set(array('aff-' . mswEnc($SETTINGS->email . SECRET_KEY) . '-key' => ''));
  $SSN->delete(array('aff-' . mswEnc($SETTINGS->email . SECRET_KEY) . '-key'));
  header("Location: " . $SETTINGS->ifolder);
  exit;
}

// If user is logged in, go to home screen..
if (isset($loggedInUser['id'])) {
  // Account deleted..
  if (isset($_GET['acc_del_confirm'])) {
    $SSN->set(array('aff-' . mswEnc($SETTINGS->email . SECRET_KEY) . '-key' => ''));
    $SSN->set(array('aff-' . mswEnc($SETTINGS->email . SECRET_KEY) . '-acc-deletion' => 'yes'));
    define('ACC_NUKED', 'yes');
  } else {
    header("Location: " . $SETTINGS->ifolder . '?p=home');
    exit;
  }
}

include(LANG_FLDR . 'footer.php');
include(LANG_FLDR . 'login.php');
include(PATH . 'control/system/modules/_header.php');

// For account deletion..
if (defined('ACC_NUKED')) {
  $msw_login[3] = $msw_login[7];
}

$captcha = '';
if ($SETTINGS->captcha == 'yes' && (!isset($api['ctalk']['enable']) || $api['ctalk']['enable'] == 'no')) {
  $captcha = $HTML->captcha($msw_public_11_captcha, 'cpt_login');
}

// Was account removed?
if (isset($_GET['acc_del_confirm'])) {
  define('DEL_ACTIONED', 1);
}

$templateParams = [
  'CAPTCHA' => $captcha,
  'TEXT' => $msw_login,
  'FTEXT' => $msw_footer,
  'ACCOUNT_DELETED' => (defined('DEL_ACTIONED') ? $msw_account_deleted : '')
];

// Global..
include(PATH . 'control/system/global.php');

$latte->render('login.tpl', $templateParams);

include(PATH . 'control/system/modules/_footer.php');

?>
