You've already forked OpenShorte.old
							
							
		
			
				
	
	
		
			44 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			44 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?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' ] ) ) {
 | 
						|
        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 ( $GLOBALS [ 'config' ] [ 'db' ] );
 | 
						|
    $database->connect ( );
 | 
						|
    $url = $_POST [ 'url' ];
 | 
						|
    $database->prepare ( "INSERT INTO `links` ( `URL`, `created_by` ) VALUES ( ?, " . $_SESSION [ 'user_id' ] . " );" );
 | 
						|
    $parameters = [
 | 
						|
        [ "s" ],
 | 
						|
        [ &$url ],
 | 
						|
    ];
 | 
						|
    $database->bind ( $parameters );
 | 
						|
    $database->execute ( );
 | 
						|
    $database->prepare ( "SELECT MAX( links.ID ) as `last` FROM links;");
 | 
						|
    $database->execute ( );
 | 
						|
    $result = $database->get_result ( );
 | 
						|
    $row = $result->fetch_assoc ( );
 | 
						|
    header ( 'Content-Type: application/json' );
 | 
						|
    $response = [
 | 
						|
        'status' => 200,
 | 
						|
        'message' => 'URL inserted correctly.',
 | 
						|
        'last_insert' => $row [ 'last' ],
 | 
						|
    ];
 | 
						|
    echo ( json_encode ( $response ) );
 | 
						|
    exit;
 |