<?php

require_once('config.php');

require_once('classes/user.php');

$user_class = new User();

if(empty($_SESSION['recover_user_id'])){
    header('Location: '.baseUrl('login.php'));
    die();
}

$error = '';

if(isset($_POST['submit']) && $_POST['submit'] == 'change'){
    $password   = empty($_POST['password']) ? '' : $_POST['password'];
    $rpassword  = empty($_POST['repassword']) ? '' : $_POST['repassword'];

    if($password == ''){
        $error = "Password can not be empty.";
    }elseif($password != $rpassword){
        $error = "Passwords mismatch.";
    }else{
        $data['password']   = sha1($password);
        $data['token']      = '';
        $user_class->update_user_data($data, (int)$_SESSION['recover_user_id']);
        unset($_SESSION['recover_user_id']);
        header('Location: '.baseUrl('login.php'));
        die();
    }
}

include('templates/change_password.php');
