<?php

session_start();
include_once 'enterprise.php';
$body = file_get_contents('php://input');
if (isset($body)) {
    $json = json_decode($body);
    if (isset($json) && changeEnterprisePassword($json->id, $json->newPassword)) {
        $message = array('success' => 'Password was successfully changed');
    } else {
        $message = array('error' => 'Password was not changed');
    }
    $message = $json;
}
$JSONresponse = json_encode($message);
echo $JSONresponse;
$body = file_get_contents('php://input');
if (isset($body)) {
    $json = json_decode($body);
    if (isset($json)) {
        $result = checkCodeValidity($json->token);
        if (!empty($result)) {
            if ($result['studentid'] != NULL) {
                if (changeStudentPassword($result['studentid'], $json->password)) {
                    removeRecovery($json->token);
                    $message = array('success' => 'Password changed successfully');
                } else {
                    $message = array('error' => 'Unable to change password');
                }
            } else {
                if ($result['enterpriseid'] != NULL) {
                    if (changeEnterprisePassword($result['studentid'], $json->password)) {
                        removeRecovery($json->token);
                        $message = array('success' => 'Password changed successfully');
                    } else {
                        $message = array('error' => 'Unable to change password');
                    }
                } else {
                    $message = array('error' => 'Code provided is not valid');
                }
            }
        } else {
            $message = array('error' => 'Code used is not valid');
        }
    } else {
        $message = array('error' => 'Error occurred: json data not set');
    }