Skip to content

Commit

Permalink
Add expiry date for links
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian L committed Dec 28, 2021
1 parent 6e73538 commit 1e0562d
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 16 deletions.
15 changes: 8 additions & 7 deletions app/Factories/LinkFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private static function formatLink($link_ending, $secret_ending=false) {
return $short_url;
}

public static function createLink($long_url, $is_secret=false, $custom_ending=null, $link_ip='127.0.0.1', $creator=false, $return_object=false, $is_api=false) {
public static function createLink($long_url, $is_secret=false, $custom_ending=null, $link_ip='127.0.0.1', $creator=false, $expiry_date=null, $return_object=false, $is_api=false) {
/**
* Given parameters needed to create a link, generate appropriate ending and
* return formatted link.
Expand All @@ -36,6 +36,7 @@ public static function createLink($long_url, $is_secret=false, $custom_ending=nu
* @param string (optional) $custom_ending
* @param string $link_ip
* @param string $creator
* @param string $expiry_date
* @param bool $return_object
* @param bool $is_api
* @return string $formatted_link
Expand Down Expand Up @@ -89,12 +90,12 @@ public static function createLink($long_url, $is_secret=false, $custom_ending=nu
}

$link = new Link;
$link->short_url = $link_ending;
$link->long_url = $long_url;
$link->ip = $link_ip;
$link->is_custom = $custom_ending != null;

$link->is_api = $is_api;
$link->short_url = $link_ending;
$link->long_url = $long_url;
$link->ip = $link_ip;
$link->is_custom = $custom_ending != null;
$link->is_api = $is_api;
$link->expiry_date = $expiry_date;

if ($creator) {
$link->creator = $creator;
Expand Down
8 changes: 4 additions & 4 deletions app/Http/Controllers/AdminPaginationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,13 @@ public function paginateAdminUsers(Request $request) {
public function paginateAdminLinks(Request $request) {
self::ensureAdmin();

$admin_links = Link::select(['short_url', 'long_url', 'clicks', 'created_at', 'creator', 'is_disabled']);
$admin_links = Link::select(['short_url', 'long_url', 'clicks', 'created_at', 'expiry_date', 'creator', 'is_disabled']);
return Datatables::of($admin_links)
->addColumn('disable', [$this, 'renderToggleLinkActiveCell'])
->addColumn('delete', [$this, 'renderDeleteLinkCell'])
->editColumn('clicks', [$this, 'renderClicksCell'])
->editColumn('long_url', [$this, 'renderLongUrlCell'])
->escapeColumns(['short_url', 'creator'])
->escapeColumns(['short_url', 'creator', 'expiry_date'])
->make(true);
}

Expand All @@ -156,12 +156,12 @@ public function paginateUserLinks(Request $request) {

$username = session('username');
$user_links = Link::where('creator', $username)
->select(['id', 'short_url', 'long_url', 'clicks', 'created_at']);
->select(['id', 'short_url', 'long_url', 'clicks', 'created_at', 'expiry_date']);

return Datatables::of($user_links)
->editColumn('clicks', [$this, 'renderClicksCell'])
->editColumn('long_url', [$this, 'renderLongUrlCell'])
->escapeColumns(['short_url'])
->escapeColumns(['short_url', 'expiry_date'])
->make(true);
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Api/ApiLinkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ApiLinkController extends ApiController {
protected function getShortenedLink($long_url, $is_secret, $custom_ending, $link_ip, $username, $response_type) {
try {
$formatted_link = LinkFactory::createLink(
$long_url, $is_secret, $custom_ending, $link_ip, $username, false, true);
$long_url, $is_secret, $custom_ending, $link_ip, $username, null, false, true);
}
catch (\Exception $e) {
throw new ApiException('CREATION_ERROR', $e->getMessage(), 400, $response_type);
Expand Down
14 changes: 13 additions & 1 deletion app/Http/Controllers/LinkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Redirect;
use Carbon\Carbon;

use App\Models\Link;
use App\Factories\LinkFactory;
Expand Down Expand Up @@ -34,11 +35,12 @@ public function performShorten(Request $request) {
$long_url = $request->input('link-url');
$custom_ending = $request->input('custom-ending');
$is_secret = ($request->input('options') == "s" ? true : false);
$expiry_date = $request->has('expire_date');
$creator = session('username');
$link_ip = $request->ip();

try {
$short_url = LinkFactory::createLink($long_url, $is_secret, $custom_ending, $link_ip, $creator);
$short_url = LinkFactory::createLink($long_url, $is_secret, $custom_ending, $link_ip, $creator, $expiry_date, false, false);
}
catch (\Exception $e) {
return self::renderError($e->getMessage());
Expand All @@ -55,6 +57,16 @@ public function performRedirect(Request $request, $short_url, $secret_key=false)
if ($link == null) {
return abort(404);
}

// Return an error if link has expired.
if ($link->expiry_date && $link->expiry_date < Carbon::today()) {
if (env('SETTING_REDIRECT_404')) {
return abort(404);
}
return view('error', [
'message' => 'Sorry, but this link has been expired.'
]);
}

// Return an error if the link has been disabled
// or return a 404 if SETTING_REDIRECT_404 is set to true
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/SetupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public static function performSetup(Request $request) {
$st_anon_api = $request->input('setting:anon_api');
$st_anon_api_quota = $request->input('setting:anon_api_quota');
$st_pseudor_ending = $request->input('setting:pseudor_ending');
$st_expriy_date = $request->input('setting:expiry_date');
$st_adv_analytics = $request->input('setting:adv_analytics');

$mail_host = $request->input('app:smtp_server');
Expand Down Expand Up @@ -185,6 +186,7 @@ public static function performSetup(Request $request) {
'ST_ANON_API' => $st_anon_api,
'ST_ANON_API_QUOTA' => $st_anon_api_quota,
'ST_PSEUDOR_ENDING' => $st_pseudor_ending,
'ST_EXPIRY_DATE' => $st_expiry_date,
'ST_ADV_ANALYTICS' => $st_adv_analytics,

'TMP_SETUP_AUTH_KEY' => $setup_auth_key
Expand Down
33 changes: 33 additions & 0 deletions database/migrations/2021_12_27_102607_create_expiry_date_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateExpiryDateTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('links', function (Blueprint $table)
{
$table->date('expiry_date')->nullable();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('links', function (Blueprint $table)
{
$table->dropIndex('expiry_date');
});
}
}
3 changes: 3 additions & 0 deletions public/css/base.css
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ body {
.error-alert {
text-align: left;
}
.expiry-date input {
line-height: 1;
}

@media (min-width: 768px) {
.content-div-padding {
Expand Down
5 changes: 3 additions & 2 deletions public/js/AdminCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ polr.controller('AdminCtrl', function($scope, $compile, $timeout) {
{className: 'wrap-text', data: 'long_url', name: 'long_url'},
{data: 'clicks', name: 'clicks'},
{data: 'created_at', name: 'created_at'},
{data: 'expiry_date', name: 'expiry_date'},
{data: 'creator', name: 'creator'},

{data: 'disable', name: 'disable', orderable: false, searchable: false},
{data: 'delete', name: 'delete', orderable: false, searchable: false}

Expand All @@ -172,7 +172,8 @@ polr.controller('AdminCtrl', function($scope, $compile, $timeout) {
{className: 'wrap-text', data: 'short_url', name: 'short_url'},
{className: 'wrap-text', data: 'long_url', name: 'long_url'},
{data: 'clicks', name: 'clicks'},
{data: 'created_at', name: 'created_at'}
{data: 'created_at', name: 'created_at'},
{data: 'expiry_date', name: 'expiry_date'}
]
}, datatables_config));
};
Expand Down
3 changes: 3 additions & 0 deletions resources/views/env.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@
# A comma-separated list of permitted email domains
SETTING_ALLOWED_EMAIL_DOMAINS="{{$ST_ALLOWED_EMAIL_DOMAINS}}"

# Set to true to enable choosing date for expiring links
SETTING_EYPIRY_DATE="{{$ST_EYPIRY_DATE}}"

# reCAPTCHA site key
POLR_RECAPTCHA_SITE_KEY="{{$POLR_RECAPTCHA_SITE_KEY}}"

Expand Down
12 changes: 12 additions & 0 deletions resources/views/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ class='form-control long-link-input' placeholder='http://' name='link-url' />
<div id='link-availability-status'></div>
</div>
</div>

@if (!env('SETTING_EXPIRY_DATE'))
{{-- Show secret toggle only if using counter-based ending --}}
<div>
<div class='expiry-date'>
<label>Expiry date
<input type='date' class='form-control' name='expiry_date'">
</label>
</div>
</div>
@endif
</div>
<input type='submit' class='btn btn-info' id='shorten' value='Shorten' />
<a href='#' class='btn btn-warning' id='show-link-options'>Link Options</a>
Expand Down
9 changes: 9 additions & 0 deletions resources/views/setup.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@
<option value='true'>Use pseudorandom strings (longer but less predictable, e.g 6LxZ3j)</option>
</select>

<p>
Expiry Date:
<setup-tooltip content="If you choose to use pseudorandom strings, you will not have the option to use a counter-based ending."></setup-tooltip>
</p>
<select name='setting:expiry_date' class='form-control'>
<option value='false' selected='selected'>Show expiry date selection</option>
<option value='true'>No expiry date</option>
</select>

<p>
URL Ending Base:
<setup-tooltip content="This will have no effect if you choose to use pseudorandom endings."></setup-tooltip>
Expand Down
1 change: 1 addition & 0 deletions resources/views/snippets/link_table.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<th>Long Link</th>
<th>Clicks</th>
<th>Date</th>
<th>Expiry Date</th>
@if ($table_id == "admin_links_table")
{{-- Show action buttons only if admin view --}}
<th>Creator</th>
Expand Down
2 changes: 1 addition & 1 deletion tests/LinkHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testLinkHelperAlreadyShortened() {
}

public function testLinkExists() {
$link = LinkFactory::createLink('http://example.com/ci', true, null, '127.0.0.1', false, true);
$link = LinkFactory::createLink('http://example.com/ci', true, null, '127.0.0.1', '2021-27-12', false, true);
// assert that existent link ending returns true
$this->assertNotEquals(LinkHelper::linkExists($link->short_url), false);
// assert that nonexistent link ending returns false
Expand Down

0 comments on commit 1e0562d

Please sign in to comment.