<?php
error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE & ~E_DEPRECATED);
ob_start();
session_start();
if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest') {
    die();
}
require_once("../../backend/functions.php");
require_once("../../db/connection.php");
require_once("../../config/config.inc.php");
$id_map = (int)$_POST['id_map'];
$username = trim(strip_tags($_POST['username']));
$email = trim(strip_tags($_POST['email']));
$password = strip_tags($_POST['password']);
$repeat_password = strip_tags($_POST['repeat_password']);
$settings = sml_get_viewer_auth_settings($id_map);
if((int)$settings['viewer_auth_mode']!==2) {
    ob_end_clean();
    echo json_encode(array("status"=>"error","msg"=>_("Registration is not available.")));
    exit;
}
if(empty($username)) {
    ob_end_clean();
    echo json_encode(array("status"=>"error","msg"=>_("Username is required.")));
    exit;
}
if(empty($email) || !filter_var($email,FILTER_VALIDATE_EMAIL) || strlen($email)>191) {
    ob_end_clean();
    echo json_encode(array("status"=>"error","msg"=>_("Invalid e-mail")));
    exit;
}
if(empty($password)) {
    ob_end_clean();
    echo json_encode(array("status"=>"error","msg"=>_("Password is required.")));
    exit;
}
if($password!==$repeat_password) {
    ob_end_clean();
    echo json_encode(array("status"=>"error","msg"=>_("Passwords do not match.")));
    exit;
}
if(sml_username_exists($username)) {
    ob_end_clean();
    echo json_encode(array("status"=>"error","msg"=>_("Username already exists.")));
    exit;
}
if(sml_email_exists($email)) {
    ob_end_clean();
    echo json_encode(array("status"=>"error","msg"=>_("E-mail already exists.")));
    exit;
}
$global_settings = get_settings();
$validate_email = (!empty($global_settings['validate_email']) && !empty($global_settings['smtp_valid'])) ? 1 : 0;
$role = 'visitor';
$active = ($validate_email) ? 0 : 1;
$hash = ($validate_email) ? sml_generate_random_string(32) : null;
$key_enc = PASSWORDS_ENCRYPT_KEY;
$query = "INSERT INTO sml_users(username,email,password,role,active,`hash`) VALUES(?,?,AES_ENCRYPT(?, ?),?,?,?);";
if($smt = $mysqli->prepare($query)) {
    $smt->bind_param('sssssis',$username,$email,$password,$key_enc,$role,$active,$hash);
    if($smt->execute()) {
        $new_id_user = (int)$mysqli->insert_id;
        if($validate_email) {
            if(sml_send_activation_email($new_id_user,$id_map,$hash)) {
                ob_end_clean();
                echo json_encode(array("status"=>"activation_required"));
                exit;
            } else {
                $query_delete = "DELETE FROM sml_users WHERE id=? LIMIT 1;";
                if($smt_delete = $mysqli->prepare($query_delete)) {
                    $smt_delete->bind_param('i',$new_id_user);
                    $smt_delete->execute();
                }
                ob_end_clean();
                echo json_encode(array("status"=>"error","msg"=>_("Unable to send activation e-mail.")));
                exit;
            }
        }
        session_regenerate_id(true);
        $_SESSION['viewer_id_user'] = $new_id_user;
        ob_end_clean();
        echo json_encode(array("status"=>"ok","user"=>array("id"=>$new_id_user,"username"=>$username,"email"=>$email,"role"=>$role,"markers_total"=>0,"markers_pending"=>0)));
        exit;
    }
}
ob_end_clean();
echo json_encode(array("status"=>"error","msg"=>_("Unable to create account.")));
