<?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 insert new URLs.'
];
echo ( json_encode ( $response ) );
exit;
}
if ( ! isset ( $_POST [ 'url' ] ) ) {
http_response_code ( 400 );
'status' => 400,
'error_message' => 'You either did not provide a URL or you provided an invalid one.'
$database = new Database ( $GLOBALS [ 'config' ] [ 'db' ] );
$database->connect ( );
$url = $_POST [ 'url' ];
$database->prepare ( "INSERT INTO `links` ( `URL`, `created_by` ) VALUES ( :url, " . $_SESSION [ 'user_id' ] . " );" );
$database->bind ( [ ':url' => $url ] );
$database->execute ( );
$database->prepare ( "SELECT MAX( links.ID ) as `last` FROM links;");
$result = $database->get_result ( );
$row = $result->fetchAll ( ) [ 0 ];
'status' => 200,
'message' => 'URL inserted correctly.',
'last_insert' => $row [ 'last' ],