From 5edaf4134690df2733de1b6fcccf2add03a1fa3d Mon Sep 17 00:00:00 2001
From: Untung Ginardi
Date: Wed, 7 Sep 2016 10:25:12 +0700
Subject: [PATCH] Handle when short URL not exist
- Add setup config for define handle type when short URL not exist
- Add setup config for define default URL if handle short URL not exist
with redirect
---
app/Http/Controllers/LinkController.php | 43 ++++++++++++++----------
app/Http/Controllers/SetupController.php | 4 +++
resources/views/env.blade.php | 8 +++++
resources/views/setup.blade.php | 14 ++++++++
4 files changed, 52 insertions(+), 17 deletions(-)
diff --git a/app/Http/Controllers/LinkController.php b/app/Http/Controllers/LinkController.php
index 7d42aab..93dbc95 100644
--- a/app/Http/Controllers/LinkController.php
+++ b/app/Http/Controllers/LinkController.php
@@ -49,32 +49,30 @@ class LinkController extends Controller {
->first();
if ($link == null) {
- return abort(404);
+ return $this->handleShortUrlNotExist();
}
$link_secret_key = $link->secret_key;
+ if ($link_secret_key) {
+ if (!$secret_key) {
+ // if we do not receieve a secret key
+ // when we are expecting one, return a 403
+ return abort(403);
+ }
+ else {
+ if ($link_secret_key != $secret_key) {
+ // a secret key is provided, but it is incorrect
+ return abort(403);
+ }
+ }
+ }
if ($link->is_disabled == 1) {
return view('error', [
'message' => 'Sorry, but this link has been disabled by an administrator.'
]);
}
-
- if ($link_secret_key) {
- if (!$secret_key) {
- // if we do not receieve a secret key
- // when we are expecting one, return a 403
- return abort(403);
- }
- else {
- if ($link_secret_key != $secret_key) {
- // a secret key is provided, but it is incorrect
- return abort(403);
- }
- }
-
- }
-
+
$long_url = $link->long_url;
$clicks = intval($link->clicks);
@@ -90,4 +88,15 @@ class LinkController extends Controller {
return redirect()->to($long_url);
}
+
+ private function handleShortUrlNotExist() {
+ $urlNotExistHandleType = env('SETTING_REDIRECT_URL_NOT_EXIST');
+ $urlNotExistRedirect = env('SETTING_NOT_EXIST_REDIRECT');
+ if (($urlNotExistHandleType == true) && ($urlNotExistRedirect)) {
+ return redirect()->to($urlNotExistRedirect);
+ } else {
+ return abort(404);
+ }
+ }
+
}
diff --git a/app/Http/Controllers/SetupController.php b/app/Http/Controllers/SetupController.php
index a920548..8de1569 100644
--- a/app/Http/Controllers/SetupController.php
+++ b/app/Http/Controllers/SetupController.php
@@ -102,6 +102,8 @@ 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_password_recov = $request->input('setting:password_recovery');
$st_base = $request->input('setting:base');
@@ -143,6 +145,8 @@ 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_URL_NOT_EXIST_REDIRECT' => $st_url_not_found_redirect,
'ST_PASSWORD_RECOV' => $st_password_recov,
'MAIL_ENABLED' => $mail_enabled,
diff --git a/resources/views/env.blade.php b/resources/views/env.blade.php
index 0abc943..bd54481 100644
--- a/resources/views/env.blade.php
+++ b/resources/views/env.blade.php
@@ -58,6 +58,14 @@ SETTING_SHORTEN_PERMISSION={{$ST_SHORTEN_PERMISSION}}
# 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
+SETTING_REDIRECT_URL_NOT_EXIST={{$ST_URL_NOT_EXIST_HANDLE_TYPE}}
+
+# You must set SETTING_NOT_EXIST_REDIRECT if SETTING_REDIRECT_URL_NOT_EXIST is true
+# Polr will redirect to this URL if requested URL not exist
+SETTING_NOT_EXIST_REDIRECT={{$ST_URL_NOT_EXIST_REDIRECT}}
+
# 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 f77b43c..c09c0b7 100644
--- a/resources/views/setup.blade.php
+++ b/resources/views/setup.blade.php
@@ -77,6 +77,20 @@ Setup
http://yoursite.com/login before you can access the index
page.