<?php
    session_start();
    require_once('models/User.php');
    if (!isset($_SESSION['login'])){
        header('Location: signin.php');
        exit();
    }
    if ($_SERVER['REQUEST_METHOD'] != 'POST') {
        header('Location: signin.php');
        exit();
    }
    if (!isset($_POST['passwd'], $_POST['passwdconf'])) {
        header('Location: signin.php');
        exit();
    }
    if (empty($_POST['passwd']) || empty($_POST['passwdconf'])) {
        header('Location: signin.php');
        exit();
    }
    
    $passwd = htmlspecialchars($_POST['passwd']);
    $passwdconf = htmlspecialchars($_POST['passwdconf']);
    
    if ($passwd != $passwdconf) {
        header('Location: signin.php');
        exit();
    }
    
    try {
        $user = new User($_SESSION['login'], $passwd);
        $user->changePassword();
        header('Location: welcome.php');
        exit();
    }
    catch (Exception $e) {
        $_SESSION['message'] = $e->getMessage();
        header('Location: signin.php');
        exit();
    }
