diff --git a/.env.setup b/.env.setup deleted file mode 100644 index 5dc4aa9..0000000 --- a/.env.setup +++ /dev/null @@ -1,22 +0,0 @@ -APP_ENV=local -APP_DEBUG=true -APP_KEY=:_to(env('SETTING_INDEX_REDIRECT')); + } + // Otherwise, show a nice error page return view('errors.404'); } - if ($e instanceof HttpException){ + if ($e instanceof HttpException) { $status_code = $e->getStatusCode(); $status_message = $e->getMessage(); @@ -54,7 +59,7 @@ class Handler extends ExceptionHandler { return view('errors.500'); } else { - // If not 500, then render generic page + // If not 500, render generic page return response(view('errors.generic', ['status_code' => $status_code, 'status_message' => $status_message]), $status_code); } } diff --git a/app/Http/Controllers/LinkController.php b/app/Http/Controllers/LinkController.php index 9a12165..4aea9a0 100644 --- a/app/Http/Controllers/LinkController.php +++ b/app/Http/Controllers/LinkController.php @@ -48,10 +48,24 @@ class LinkController extends Controller { $link = Link::where('short_url', $short_url) ->first(); + // Return 404 if link not found if ($link == null) { - return $this->handleShortUrlNotExist(); + return abort(404); } + // Return an error if the link has been disabled + // or return a 404 if SETTING_REDIRECT_404 is set to true + if ($link->is_disabled == 1) { + if (env('SETTING_REDIRECT_404')) { + return abort(404); + } + + return view('error', [ + 'message' => 'Sorry, but this link has been disabled by an administrator.' + ]); + } + + // Return a 403 if the secret key is incorrect $link_secret_key = $link->secret_key; if ($link_secret_key) { if (!$secret_key) { @@ -67,36 +81,19 @@ class LinkController extends Controller { } } - if ($link->is_disabled == 1) { - return view('error', [ - 'message' => 'Sorry, but this link has been disabled by an administrator.' - ]); - } - + // Increment click count $long_url = $link->long_url; $clicks = intval($link->clicks); if (is_int($clicks)) { $clicks += 1; } - $link->clicks = $clicks; - $link->save(); + // Redirect to final destination LinkHelper::processPostClick($link); - return redirect()->to($long_url); } - - private function handleShortUrlNotExist() { - $urlNotExistHandleType = env('SETTING_REDIRECT_URL_NOT_EXIST'); - $urlRedirect = env('SETTING_INDEX_REDIRECT'); - if (($urlNotExistHandleType == true) && ($urlRedirect)) { - return redirect()->to($urlRedirect); - } else { - return abort(404); - } - } - + } diff --git a/app/Http/Controllers/SetupController.php b/app/Http/Controllers/SetupController.php index 1479a4b..7655205 100644 --- a/app/Http/Controllers/SetupController.php +++ b/app/Http/Controllers/SetupController.php @@ -102,8 +102,7 @@ class SetupController extends Controller { // if true, only logged in users can shorten $st_shorten_permission = $request->input('setting:shorten_permission'); $st_index_redirect = $request->input('setting:index_redirect'); - $st_url_not_exist_handle_type = $request->input('setting:url_not_exist_handle'); - $st_url_not_found_redirect = $request->input('setting:url_not_exist_redirect'); + $st_redirect_404 = $request->input('setting:redirect_404'); $st_password_recov = $request->input('setting:password_recovery'); $st_base = $request->input('setting:base'); @@ -145,7 +144,7 @@ class SetupController extends Controller { 'POLR_ACCT_ACTIVATION' => $polr_acct_activation, 'ST_SHORTEN_PERMISSION' => $st_shorten_permission, 'ST_INDEX_REDIRECT' => $st_index_redirect, - 'ST_URL_NOT_EXIST_HANDLE_TYPE' => $st_url_not_exist_handle_type, + 'ST_REDIRECT_404' => $st_redirect_404, 'ST_PASSWORD_RECOV' => $st_password_recov, 'MAIL_ENABLED' => $mail_enabled, diff --git a/public/img/setup.jpg b/public/img/setup.jpg index fab9907..1f0c076 100644 Binary files a/public/img/setup.jpg and b/public/img/setup.jpg differ diff --git a/resources/views/env.blade.php b/resources/views/env.blade.php index 0952fce..09d5fc5 100644 --- a/resources/views/env.blade.php +++ b/resources/views/env.blade.php @@ -54,15 +54,13 @@ POLR_ACCT_ACTIVATION={{$POLR_ACCT_ACTIVATION}} # Set to true to require users to be logged in before shortening URLs SETTING_SHORTEN_PERMISSION={{$ST_SHORTEN_PERMISSION}} -# You must set SETTING_INDEX_REDIRETC if SETTING_PUBLIC_INTERFACE is false +# You must set SETTING_INDEX_REDIRECT if SETTING_PUBLIC_INTERFACE is false # Polr will redirect logged off users to this URL SETTING_INDEX_REDIRECT={{$ST_INDEX_REDIRECT}} -# Set value to true if you want to redirect to specified URL -# when requested short URL not exist -# The SETTING_INDEX_REDIRECT will be used as redirect URL, -# if it not set it will show as error message -SETTING_REDIRECT_URL_NOT_EXIST={{$ST_URL_NOT_EXIST_HANDLE_TYPE}} +# Set to true if you wish to redirect 404s to SETTING_INDEX_REDIRECT +# Otherwise, an error message will be shown +SETTING_REDIRECT_404={{$ST_REDIRECT_404}} # Set to true to enable password recovery SETTING_PASSWORD_RECOV={{$ST_PASSWORD_RECOV}} diff --git a/resources/views/setup.blade.php b/resources/views/setup.blade.php index 3a03830..92fc543 100644 --- a/resources/views/setup.blade.php +++ b/resources/views/setup.blade.php @@ -52,7 +52,7 @@ Setup

Application protocol:

-

Application URL (path to Polr, no http://, or trailing slash):

+

Application URL (path to Polr; do not include http:// or trailing slash):

Shortening Permissions:

@@ -61,23 +61,21 @@ Setup -

Show Public Interface:

+

Public Interface:

- -

- Short URL not exist handle type : -

- + +

- If public interface is hidden or short URL not exist handle type set to redirect to URL, redirect index page to: - + Redirect URL: +

@@ -85,7 +83,7 @@ Setup http://yoursite.com/login before you can access the index page.

- +

Default URL Ending Type: @@ -189,7 +187,7 @@ Setup - + diff --git a/tests/IndexTest.php b/tests/IndexTest.php index 6ff66d3..af40015 100644 --- a/tests/IndexTest.php +++ b/tests/IndexTest.php @@ -14,11 +14,4 @@ class IndexTest extends TestCase ->see('>Sign In') // Ensure log in buttons are shown when user is logged out ->dontSee('SQLSTATE'); // Ensure database connection is correct } - - public function testRequestGetNotExistShortUrl() { - $response = $this->call('GET', '/notexist'); - $this->assertTrue($response->isRedirection()); - $this->assertRedirectedTo(env('SETTING_INDEX_REDIRECT')); - } } - \ No newline at end of file diff --git a/tests/LinkControllerTest.php b/tests/LinkControllerTest.php new file mode 100644 index 0000000..d04c66d --- /dev/null +++ b/tests/LinkControllerTest.php @@ -0,0 +1,15 @@ +call('GET', '/notexist'); + $this->assertTrue($response->isRedirection()); + $this->assertRedirectedTo(env('SETTING_INDEX_REDIRECT')); + } +}