diff --git a/index.php b/index.php index 9db73d8..ca3bb6c 100644 --- a/index.php +++ b/index.php @@ -6,115 +6,16 @@ if ( isset ( $request [ 0 ] ) && $request [ 0 ] != "" ) { switch ( $request [ 0 ] ) { case "go": - if ( isset ( $request [ 1 ] ) ) { - $link_id = $request [ 1 ]; - if ( ! ctype_digit ( $link_id ) ) { - die ( "You can't be forwarded to a non numerical URL link ID. If you think this is incorrect, please send an email to shorte@dev.bryanpedini.it with this URL: https://sh.bjphoster.com/?go=" . $link_id . " for more investigations" ); - } - $link_id = (int) $link_id; - $db_connection = db_connect ( ); - $statement = db_prepare ( $db_connection, "SELECT links.URL FROM links WHERE links.ID = ?" ); - $parameters = [ - [ "i" ], - [ &$link_id ], - ]; - db_bind ( $statement, $parameters ); - db_execute ( $statement ); - $result = $statement->get_result ( ); - $row = $result->fetch_assoc ( ); - if ( ! $row ) { - http_response_code ( 404 ); - include ( 'errors/404.html' ); - exit; - } - else { - include ( 'templates/redirect.html' ); - echo ( '' ); - } - } - else { - header ( "Location: " . substr ( $_SERVER [ 'SCRIPT_NAME' ], 0, -10 ) ); - } + include ( 'lib/php/go.php' ); break; case "login": - if ( isset ( $_SESSION [ 'user_id' ] ) ) { - header ( "Location: " . substr ( $_SERVER [ 'SCRIPT_NAME' ], 0, -10 ) . "/insert" ); - exit; - } - if ( isset ( $_POST [ 'username' ] ) && isset ( $_POST [ 'password' ] ) ) { - $username = $_POST [ 'username' ]; - $password = $_POST [ 'password' ]; - $db_connection = db_connect ( ); - $statement = db_prepare ( $db_connection, "SELECT users.id FROM users WHERE users.username = ? AND users.password = ?" ); - $parameters = [ - [ "ss" ], - [ &$username, &$password ], - ]; - db_bind ( $statement, $parameters ); - db_execute ( $statement ); - $result = $statement->get_result ( ); - $row = $result->fetch_assoc ( ); - if ( ! $row ) { - header ( 'Content-Type: application/json' ); - $response = [ - 'status' => 401, - 'error_message' => 'Username or password not correct.', - ]; - echo ( json_encode ( $response ) ); - } - else { - $_SESSION [ 'user_id' ] = $row [ 'id' ]; - header ( 'Content-Type: application/json' ); - $response = [ - 'status' => 200, - 'message' => 'Authentication succesfully executed.', - ]; - echo ( json_encode ( $response ) ); - } - } - else { - include ( 'lib/php/login.php' ); - } + include ( 'lib/php/login.php' ); break; case "logout": - $_SESSION = array ( ); - session_destroy ( ); - header ( "Location: " . substr ( $_SERVER [ 'SCRIPT_NAME' ], 0, -10 ) . "/" ); + include ( 'lib/php/logout.php' ); break; case "insert": - if ( ! isset ( $_SESSION [ 'user_id' ] ) ) { - header ( "Location: " . substr ( $_SERVER [ 'SCRIPT_NAME' ], 0, -10 ) . "/login" ); - } - if ( isset ( $_POST [ 'url' ] ) ) { - $url = $_POST [ 'url' ]; - $db_connection = db_connect ( ); - $statement = db_prepare ( $db_connection, "INSERT INTO `links` ( `ID`, `URL`, `created_by` ) VALUES ( NULL, ?, " . $_SESSION [ 'user_id' ] . " );" ); - $parameters = [ - [ "s" ], - [ &$url ], - ]; - db_bind ( $statement, $parameters ); - db_execute ( $statement ); - $statement = db_prepare ( $db_connection, "SELECT COUNT( links.ID ) as `count` FROM links;"); - db_execute ( $statement ); - $result = $statement->get_result ( ); - $row = $result->fetch_assoc ( ); - header ( 'Content-Type: application/json' ); - $response = [ - 'status' => 200, - 'message' => 'URL inserted correctly.', - 'new_id' => $row [ 'count' ], - ]; - echo ( json_encode ( $response ) ); - } - else { - $db_connection = db_connect ( ); - $statement = db_prepare ( $db_connection, "SELECT links.ID, links.URL FROM links WHERE links.created_by = " . $_SESSION [ 'user_id' ] ); - db_execute ( $statement ); - $result = $statement->get_result ( ); - $row = $result->fetch_assoc ( ); - include ( 'lib/php/insert.php' ); - } + include ( 'lib/php/insert.php' ); break; default: http_response_code ( 404 ); diff --git a/lib/php/go.php b/lib/php/go.php new file mode 100644 index 0000000..1d99b73 --- /dev/null +++ b/lib/php/go.php @@ -0,0 +1,31 @@ +get_result ( ); + $row = $result->fetch_assoc ( ); + if ( ! $row ) { + http_response_code ( 404 ); + include ( 'errors/404.html' ); + exit; + } + else { + include ( 'templates/redirect.html' ); + echo ( '' ); + } + } + else { + header ( "Location: " . substr ( $_SERVER [ 'SCRIPT_NAME' ], 0, -10 ) ); + } +?> \ No newline at end of file diff --git a/lib/php/insert.php b/lib/php/insert.php index c6f17a8..83e6789 100644 --- a/lib/php/insert.php +++ b/lib/php/insert.php @@ -1,3 +1,36 @@ +get_result ( ); + $row = $result->fetch_assoc ( ); + header ( 'Content-Type: application/json' ); + $response = [ + 'status' => 200, + 'message' => 'URL inserted correctly.', + 'new_id' => $row [ 'count' ], + ]; + echo ( json_encode ( $response ) ); + } + else { + $db_connection = db_connect ( ); + $statement = db_prepare ( $db_connection, "SELECT links.ID, links.URL FROM links WHERE links.created_by = " . $_SESSION [ 'user_id' ] ); + db_execute ( $statement ); + $result = $statement->get_result ( ); + $row = $result->fetch_assoc ( ); +?> @@ -23,4 +56,7 @@ - \ No newline at end of file + + \ No newline at end of file diff --git a/lib/php/login.php b/lib/php/login.php index ec6ccd2..cfdd54e 100644 --- a/lib/php/login.php +++ b/lib/php/login.php @@ -1,15 +1,116 @@ +get_result ( ); + $row = $result->fetch_assoc ( ); + if ( ! $row ) { + header ( 'Content-Type: application/json' ); + $response = [ + 'status' => 401, + 'error_message' => 'Username or password not correct.', + ]; + echo ( json_encode ( $response ) ); + } + else { + $_SESSION [ 'user_id' ] = $row [ 'id' ]; + header ( 'Content-Type: application/json' ); + $response = [ + 'status' => 200, + 'message' => 'Authentication succesfully executed.', + ]; + echo ( json_encode ( $response ) ); + } + } + else { +?> BJPHoster URL Shortener | Login + + -
-
-
+
+
+ +
+ + - \ No newline at end of file + + \ No newline at end of file diff --git a/lib/php/logout.php b/lib/php/logout.php new file mode 100644 index 0000000..deb0983 --- /dev/null +++ b/lib/php/logout.php @@ -0,0 +1,5 @@ + \ No newline at end of file diff --git a/lib/php/lostpassword.php b/lib/php/lostpassword.php new file mode 100644 index 0000000..9d330db --- /dev/null +++ b/lib/php/lostpassword.php @@ -0,0 +1,13 @@ + + + + + BJPHoster URL Shortener | Password Reset + + +
+

Reset your password here - Placeholder

+
+ + \ No newline at end of file