<?php

if (!defined('MODULE_LOADED') || $SETTINGS->createaff == 'no') {
  include(PATH . 'control/system/headers/403.php');
  exit;
}

include(LANG_FLDR . 'footer.php');
include(LANG_FLDR . 'create.php');

// Verification..
if (isset($_GET['ac']) && $SETTINGS->approveaff == 'no') {

  $accnt = $ACC->account([
    '`approved` = \'no\'',
    '`sys2` = \'' . $DB->db_escape(preg_replace('/[^0-9a-zA-Z]/', '', $_GET['ac'])) . '\''
  ]);

  // Did we find something?
  if (isset($accnt->id) && $accnt->approved == 'no') {
    // Activate account..
    $update = $ACC->update([
      '`en` = \'yes\'',
      '`approved` = \'yes\'',
      '`sys2` = \'\'',
      '`sys3` = \'passchange\''
    ], '`id` = \'' . $accnt->id . '\'');
    // Send email to webmaster..
    if ($update > 0) {
      include(PATH . 'control/mail-init.php');
      $msg = mswTmp(MSW_EM_LANG . 'affiliate-active.txt', 'yes');
      $sbj = str_replace('{website}', $SETTINGS->website, $msw_emails6);
      $MAILR->addTag('{NAME}', $accnt->fname . ' ' . $accnt->lname);
      $MAILR->addTag('{FNAME}', $accnt->fname);
      $MAILR->addTag('{LNAME}', $accnt->lname);
      $MAILR->addTag('{EMAIL}', $accnt->email);
      $MAILR->sendMail([
        'from_email' => ($SETTINGS->smtp_email ? $SETTINGS->smtp_email : $SETTINGS->email),
        'from_name' => ($SETTINGS->smtp_from ? $SETTINGS->smtp_from : $SETTINGS->website),
        'to_email' => $SETTINGS->email,
        'to_name' => $SETTINGS->website,
        'subject' => $sbj,
        'replyto' => [
          'name' => $SETTINGS->email,
          'email' => $SETTINGS->website
        ],
        'template' => $msg,
        'language' => $SETTINGS->language
      ]);
      $MAILR->smtpClose();
    }
    $msg = str_replace('{name}', mswSH($accnt->fname), $msw_create[6]);
  } else {
    $msg = $msw_create_err3;
  }

  $sysTitle = $msw_common19 . ': ' . mswCD($SETTINGS->website);

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

  $templateParams = [
    'MSG' => $msg
  ];

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

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

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

// If user is logged in, go to home screen..
if (isset($loggedInUser['id'])) {
  header("Location: " . $SETTINGS->ifolder . '?p=home');
  exit;
}

$sysTitle = $msw_footer[4] . ': ' . mswCD($SETTINGS->website);
$loadJS[] = 'ibox';

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

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

$templateParams = [
  'CAPTCHA' => $captcha,
  'TEXT' => $msw_create,
  'PTEXT' => [
    $msw_public_11_global
  ],
  'FTEXT' => $msw_footer,
  'PP' => (in_array('log', $pr_areas) ? 'yes' : 'no')
];

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

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

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

?>
