<?php
    use App\MyUser;
    if (!isset($_SESSION['login'])){
        header('Location: signin');
        exit();
    }

    if (!isset($_POST['passwd'], $_POST['passwdconf'])) {
        header('Location: signin');
        exit();
    }
    if (empty($_POST['passwd']) || empty($_POST['passwdconf'])) {
        header('Location: signin');
        exit();
    }
    
    $passwd = htmlspecialchars($_POST['passwd']);
    $passwdconf = htmlspecialchars($_POST['passwdconf']);
    
    if ($passwd != $passwdconf) {
        header('Location: signin');
        exit();
    }
    
    try {
        $user = new MyUser($_SESSION['login'], $passwd);
        $user->changePassword();
        header('Location: welcome');
        exit();
    }
    catch (Exception $e) {
        $_SESSION['message'] = $e->getMessage();
        header('Location: signin');
        exit();
    }
