<?php
error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE & ~E_DEPRECATED);
session_start();
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
require_once("../backend/functions.php");
require_once("../db/connection.php");
$email = isset($_GET['email']) ? trim(strip_tags($_GET['email'])) : '';
$hash = isset($_GET['hash']) ? trim(strip_tags($_GET['hash'])) : '';
$code = isset($_GET['code']) ? trim(strip_tags($_GET['code'])) : '';
$redirect = "index.php";
if($code!='') {
    $redirect .= "?code=".urlencode($code);
}
if($email!='' && $hash!='') {
    $query = "SELECT id FROM sml_users WHERE email=? AND `hash`=? LIMIT 1;";
    if($smt = $mysqli->prepare($query)) {
        $smt->bind_param('ss',$email,$hash);
        if($smt->execute()) {
            $result = get_result($smt);
            if(count($result)==1) {
                $row = array_shift($result);
                $id = (int)$row['id'];
                $query_update = "UPDATE sml_users SET active=1,`hash`=NULL WHERE id=?;";
                if($smt_update = $mysqli->prepare($query_update)) {
                    $smt_update->bind_param('i',$id);
                    if($smt_update->execute()) {
                        $_SESSION['viewer_id_user'] = $id;
                    }
                }
            }
        }
    }
}
header("Location: ".$redirect);
exit;
