<?php
/*-------------------------------------------------------+
| PHPFusion Content Management System
| Copyright (C) PHP Fusion Inc
| https://phpfusion.com/
+--------------------------------------------------------+
| Filename: reactivate.php
| Author: Paul Beuk (muscapaul)
| Co Author: Hans Krisitan Flaatten (Starefossen)
+--------------------------------------------------------+
| This program is released as free software under the
| Affero GPL license. You can redistribute it and/or
| modify it under the terms of this license which you
| can read by viewing the included agpl.txt or online
| at www.gnu.org/licenses/agpl.html. Removal of this
| copyright header is strictly prohibited without
| written permission from the original author(s).
+--------------------------------------------------------*/
require_once __DIR__."/maincore.php";
require_once THEMES."templates/header.php";
require_once INCLUDES."suspend_include.php";
include LOCALE.LOCALESET."reactivate.php";

if (iMEMBER) {
    redirect("index.php");
}

if (isset($_GET['user_id']) && isnum($_GET['user_id']) && isset($_GET['code']) && preg_check("/^[0-9a-z]{32}$/", $_GET['code'])) {
    $result = dbquery("SELECT user_name, user_email, user_actiontime, user_password FROM ".DB_USERS." WHERE user_id='".$_GET['user_id']."' AND user_actiontime>'0' AND user_status='7'");
    if (dbrows($result)) {
        $data = dbarray($result);
        $code = md5($data['user_actiontime'].$data['user_password']);
        if ($_GET['code'] == $code) {
            if ($data['user_actiontime'] > time()) {
                $result = dbquery("UPDATE ".DB_USERS." SET user_status='0', user_actiontime='0', user_lastvisit='".time()."' WHERE user_id='".$_GET['user_id']."'");
                unsuspend_log($_GET['user_id'], 7, $locale['506'], TRUE);
                $message = str_replace(
                    ["[USER_NAME]", '[SITENAME]', '[SITEUSERNAME]'],
                    [$data['user_name'], $settings['sitename'], $settings['siteusername']],
                    $locale['505']
                );
                require_once INCLUDES."sendmail_include.php";
                sendemail($data['user_name'], $data['user_email'], $settings['siteusername'], $settings['siteemail'], $locale['504'], $message);
                redirect(BASEDIR."login.php");
            } else {
                redirect(FUSION_SELF."?error=1");
            }
        } else {
            redirect(FUSION_SELF."?error=2&user_id=".$data['user_id']."&code=".$_GET['code']);
        }
    } else {
        redirect(FUSION_SELF."?error=3");
    }
} else if (isset($_GET['error']) && isnum($_GET['error'])) {
    opentable($locale['500']);
    switch ($_GET['error']) {
        case 1:
            echo str_replace('[SITEEMAIL]', "<a href='mailto:".$settings['siteemail']."'>".$settings['siteemail']."</a>", $locale['501']);
            break;
        case 2:
            echo str_replace('[SITEEMAIL]', "<a href='mailto:".$settings['siteemail']."'>".$settings['siteemail']."</a>", $locale['502']);
            break;
        case 3:
            echo str_replace(['[LINK]', '[/LINK]', '[SITEEMAIL]'],
                ["<a href='".$settings['siteurl']."login.php'>", "</a>", "<a href='mailto:".$settings['siteemail']."'>".$settings['siteemail']."</a>"],
                $locale['503']
            );
            break;
        default:
            redirect(BASEDIR."index.php");
    }
    closetable();
} else {
    redirect(BASEDIR."index.php");
}

require_once THEMES."templates/footer.php";
