1
0
mirror of https://github.com/cydrobolt/polr.git synced 2024-11-10 04:02:28 +01:00

Merge pull request #1 from ntoenk1186/handle_short_url_not_exist

Handle short url not exist to redirect to some URL
This commit is contained in:
Untung Ginardi 2016-09-07 14:06:32 +07:00 committed by GitHub
commit ebe69b042f
6 changed files with 60 additions and 17 deletions

View File

@ -49,10 +49,23 @@ 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', [
@ -60,21 +73,6 @@ class LinkController extends Controller {
]);
}
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);
}
}
}

View File

@ -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,

View File

@ -24,5 +24,7 @@
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="SETTING_REDIRECT_URL_NOT_EXIST" value="true"/>
<env name="SETTING_NOT_EXIST_REDIRECT" value="http://redirect.polr.me/"/>
</php>
</phpunit>

View File

@ -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}}

View File

@ -78,6 +78,20 @@ Setup
page.
</p>
<p>
Short URL not exist handle type :
</p>
<select name='setting:url_not_exist_handle' class='form-control'>
<option value='false' selected='selected'>Show error message (Default)</option>
<option value='true'>Redirect to URL</option>
</select>
<p>
If short URL not exist, redirect to :
<button data-content="This will be used if you handle not exist URL as redirect to URL" type="button" class="btn btn-xs btn-default setup-qmark" data-toggle="popover">?</button>
</p>
<input type='text' class='form-control' name='setting:url_not_exist_redirect' placeholder='http://your-main-site.com'>
<p>
Default URL Ending Type:
<button data-content="If you choose to use pseudorandom strings, you will not have the option to use a counter-based ending." type="button" class="btn btn-xs btn-default setup-qmark" data-toggle="popover">?</button>

View File

@ -14,4 +14,10 @@ class IndexTest extends TestCase
->see('>Sign In</a>') // 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_NOT_EXIST_REDIRECT'));
}
}