<?php
    if ( ! isset ( $_SESSION [ 'user_id' ] ) ) {
        header ( 'Content-Type: application/json' );
        http_response_code ( 401 );
        $response = [
            'status' => 401,
            'error_message' => 'You either are not logged in or you do not have permissions to edit URLs.'
        ];
        echo ( json_encode ( $response ) );
        exit;
    }
    if ( ! isset ( $_POST [ 'url' ] ) ) {
        header ( 'Content-Type: application/json' );
        http_response_code ( 400 );
        $response = [
            'status' => 400,
            'error_message' => 'You either did not provide a URL or you provided an invalid one.'
        ];
        echo ( json_encode ( $response ) );
        exit;
    }
    $database = new Database ( $config [ 'db' ] );
    $database->connect ( );
    $database->prepare ( "UPDATE `links` SET `URL`=:url WHERE `ID`=:url_id;" );
    $database->bind ( [ ':url' => $_POST [ 'url' ], 'url_id' => $_POST [ 'url_id' ] ] );
    $database->execute ( );
    header ( 'Content-Type: application/json' );
    $response = [
        'status' => 200,
        'message' => 'URL edited correctly.'
    ];
    echo ( json_encode ( $response ) );
    exit;