Skip to content

Commit

Permalink
Add option to redirect 404s and disabled URLs; update setup picture f…
Browse files Browse the repository at this point in the history
…or 2.0.0 stable
  • Loading branch information
cydrobolt committed Sep 11, 2016
1 parent 0ef7f85 commit ab95465
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 76 deletions.
22 changes: 0 additions & 22 deletions .env.setup

This file was deleted.

11 changes: 8 additions & 3 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,15 @@ public function render($request, Exception $e)
{
if (env('APP_DEBUG') != true) {
// Render nice error pages if debug is off
if ($e instanceof NotFoundHttpException){
if ($e instanceof NotFoundHttpException) {
if (env('SETTING_REDIRECT_404')) {
// Redirect 404s to SETTING_INDEX_REDIRECT
return redirect()->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();

Expand All @@ -54,7 +59,7 @@ public function render($request, Exception $e)
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);
}
}
Expand Down
39 changes: 18 additions & 21 deletions app/Http/Controllers/LinkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,24 @@ public function performRedirect(Request $request, $short_url, $secret_key=false)
$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) {
Expand All @@ -67,36 +81,19 @@ public function performRedirect(Request $request, $short_url, $secret_key=false)
}
}

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);
}
}


}
5 changes: 2 additions & 3 deletions app/Http/Controllers/SetupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ public static function performSetup(Request $request) {
// 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');
Expand Down Expand Up @@ -145,7 +144,7 @@ public static function performSetup(Request $request) {
'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,
Expand Down
Binary file modified public/img/setup.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 4 additions & 6 deletions resources/views/env.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,13 @@
# 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}}
Expand Down
26 changes: 12 additions & 14 deletions resources/views/setup.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<p>Application protocol:</p>
<input type='text' class='form-control' name='app:protocol' value='http://'>

<p>Application URL (path to Polr, no http://, or trailing slash):</p>
<p>Application URL (path to Polr; do not include http:// or trailing slash):</p>
<input type='text' class='form-control' name='app:external_url' value='yoursite.com'>

<p>Shortening Permissions:</p>
Expand All @@ -61,31 +61,29 @@
<option value='true'>Only logged in users may shorten URLs</option>
</select>

<p>Show Public Interface:</p>
<p>Public Interface:</p>
<select name='setting:public_interface' class='form-control'>
<option value='true' selected='selected'>Show public interface (default)</option>
<option value='false'>Hide public interface (for private shorteners)</option>
<option value='false'>Redirect index page to redirect URL</option>
</select>

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

<p>404s and Disabled Short Links:</p>
<select name='setting:redirect_404' class='form-control'>
<option value='false' selected='selected'>Show an error message (default)</option>
<option value='true'>Redirect to redirect URL</option>
</select>

<p>
If public interface is hidden or short URL not exist handle type set to redirect to URL, redirect index page to:
<button data-content="Required if public interface is hidden or if you set handle not exist URL as redirect to URL. To use Polr, login by directly heading to yoursite.com/login first." type="button" class="btn btn-xs btn-default setup-qmark" data-toggle="popover">?</button>
Redirect URL:
<button data-content="Required if you wish to redirect the index page or 404s to a different website. To use Polr, login by directly heading to yoursite.com/login first." type="button" class="btn btn-xs btn-default setup-qmark" data-toggle="popover">?</button>
</p>
<input type='text' class='form-control' name='setting:index_redirect' placeholder='http://your-main-site.com'>
<p class='text-muted'>
If a redirect is enabled, you will need to go to
http://yoursite.com/login before you can access the index
page.
</p>

<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>
Expand Down Expand Up @@ -189,7 +187,7 @@
<option value='//maxcdn.bootstrapcdn.com/bootswatch/3.3.6/simplex/bootstrap.min.css'>Crisp White</option>
<option value='//maxcdn.bootstrapcdn.com/bootswatch/3.3.6/darkly/bootstrap.min.css'>Cloudy Night</option>
<option value='//maxcdn.bootstrapcdn.com/bootswatch/3.3.6/cerulean/bootstrap.min.css'>Calm Skies</option>
<option value='//maxcdn.bootstrapcdn.com/bootswatch/3.3.6/paper/bootstrap.min.css'>Android Material Design</option>
<option value='//maxcdn.bootstrapcdn.com/bootswatch/3.3.6/paper/bootstrap.min.css'>Google Material Design</option>
<option value='//maxcdn.bootstrapcdn.com/bootswatch/3.3.6/superhero/bootstrap.min.css'>Blue Metro</option>
<option value='//maxcdn.bootstrapcdn.com/bootswatch/3.3.6/sandstone/bootstrap.min.css'>Sandstone</option>
<option value='//maxcdn.bootstrapcdn.com/bootswatch/3.3.6/cyborg/bootstrap.min.css'>Jet Black</option>
Expand Down
7 changes: 0 additions & 7 deletions tests/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,4 @@ public function testIndex() {
->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_INDEX_REDIRECT'));
}
}

15 changes: 15 additions & 0 deletions tests/LinkControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

class LinkControllerTest extends TestCase
{
/**
* Test LinkController
*
* @return void
*/
public function testRequestGetNotExistShortUrl() {
$response = $this->call('GET', '/notexist');
$this->assertTrue($response->isRedirection());
$this->assertRedirectedTo(env('SETTING_INDEX_REDIRECT'));
}
}

0 comments on commit ab95465

Please sign in to comment.