Skip to content

Commit 1e0562d

Browse files
author
Sebastian L
committed
Add expiry date for links
1 parent 6e73538 commit 1e0562d

13 files changed

Lines changed: 93 additions & 16 deletions

File tree

app/Factories/LinkFactory.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ private static function formatLink($link_ending, $secret_ending=false) {
2626
return $short_url;
2727
}
2828

29-
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) {
29+
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) {
3030
/**
3131
* Given parameters needed to create a link, generate appropriate ending and
3232
* return formatted link.
@@ -36,6 +36,7 @@ public static function createLink($long_url, $is_secret=false, $custom_ending=nu
3636
* @param string (optional) $custom_ending
3737
* @param string $link_ip
3838
* @param string $creator
39+
* @param string $expiry_date
3940
* @param bool $return_object
4041
* @param bool $is_api
4142
* @return string $formatted_link
@@ -89,12 +90,12 @@ public static function createLink($long_url, $is_secret=false, $custom_ending=nu
8990
}
9091

9192
$link = new Link;
92-
$link->short_url = $link_ending;
93-
$link->long_url = $long_url;
94-
$link->ip = $link_ip;
95-
$link->is_custom = $custom_ending != null;
96-
97-
$link->is_api = $is_api;
93+
$link->short_url = $link_ending;
94+
$link->long_url = $long_url;
95+
$link->ip = $link_ip;
96+
$link->is_custom = $custom_ending != null;
97+
$link->is_api = $is_api;
98+
$link->expiry_date = $expiry_date;
9899

99100
if ($creator) {
100101
$link->creator = $creator;

app/Http/Controllers/AdminPaginationController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,13 @@ public function paginateAdminUsers(Request $request) {
141141
public function paginateAdminLinks(Request $request) {
142142
self::ensureAdmin();
143143

144-
$admin_links = Link::select(['short_url', 'long_url', 'clicks', 'created_at', 'creator', 'is_disabled']);
144+
$admin_links = Link::select(['short_url', 'long_url', 'clicks', 'created_at', 'expiry_date', 'creator', 'is_disabled']);
145145
return Datatables::of($admin_links)
146146
->addColumn('disable', [$this, 'renderToggleLinkActiveCell'])
147147
->addColumn('delete', [$this, 'renderDeleteLinkCell'])
148148
->editColumn('clicks', [$this, 'renderClicksCell'])
149149
->editColumn('long_url', [$this, 'renderLongUrlCell'])
150-
->escapeColumns(['short_url', 'creator'])
150+
->escapeColumns(['short_url', 'creator', 'expiry_date'])
151151
->make(true);
152152
}
153153

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

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

161161
return Datatables::of($user_links)
162162
->editColumn('clicks', [$this, 'renderClicksCell'])
163163
->editColumn('long_url', [$this, 'renderLongUrlCell'])
164-
->escapeColumns(['short_url'])
164+
->escapeColumns(['short_url', 'expiry_date'])
165165
->make(true);
166166
}
167167
}

app/Http/Controllers/Api/ApiLinkController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class ApiLinkController extends ApiController {
1010
protected function getShortenedLink($long_url, $is_secret, $custom_ending, $link_ip, $username, $response_type) {
1111
try {
1212
$formatted_link = LinkFactory::createLink(
13-
$long_url, $is_secret, $custom_ending, $link_ip, $username, false, true);
13+
$long_url, $is_secret, $custom_ending, $link_ip, $username, null, false, true);
1414
}
1515
catch (\Exception $e) {
1616
throw new ApiException('CREATION_ERROR', $e->getMessage(), 400, $response_type);

app/Http/Controllers/LinkController.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace App\Http\Controllers;
33
use Illuminate\Http\Request;
44
use Illuminate\Http\Redirect;
5+
use Carbon\Carbon;
56

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

4042
try {
41-
$short_url = LinkFactory::createLink($long_url, $is_secret, $custom_ending, $link_ip, $creator);
43+
$short_url = LinkFactory::createLink($long_url, $is_secret, $custom_ending, $link_ip, $creator, $expiry_date, false, false);
4244
}
4345
catch (\Exception $e) {
4446
return self::renderError($e->getMessage());
@@ -55,6 +57,16 @@ public function performRedirect(Request $request, $short_url, $secret_key=false)
5557
if ($link == null) {
5658
return abort(404);
5759
}
60+
61+
// Return an error if link has expired.
62+
if ($link->expiry_date && $link->expiry_date < Carbon::today()) {
63+
if (env('SETTING_REDIRECT_404')) {
64+
return abort(404);
65+
}
66+
return view('error', [
67+
'message' => 'Sorry, but this link has been expired.'
68+
]);
69+
}
5870

5971
// Return an error if the link has been disabled
6072
// or return a 404 if SETTING_REDIRECT_404 is set to true

app/Http/Controllers/SetupController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ public static function performSetup(Request $request) {
127127
$st_anon_api = $request->input('setting:anon_api');
128128
$st_anon_api_quota = $request->input('setting:anon_api_quota');
129129
$st_pseudor_ending = $request->input('setting:pseudor_ending');
130+
$st_expriy_date = $request->input('setting:expiry_date');
130131
$st_adv_analytics = $request->input('setting:adv_analytics');
131132

132133
$mail_host = $request->input('app:smtp_server');
@@ -185,6 +186,7 @@ public static function performSetup(Request $request) {
185186
'ST_ANON_API' => $st_anon_api,
186187
'ST_ANON_API_QUOTA' => $st_anon_api_quota,
187188
'ST_PSEUDOR_ENDING' => $st_pseudor_ending,
189+
'ST_EXPIRY_DATE' => $st_expiry_date,
188190
'ST_ADV_ANALYTICS' => $st_adv_analytics,
189191

190192
'TMP_SETUP_AUTH_KEY' => $setup_auth_key
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class CreateExpiryDateTable extends Migration
7+
{
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up()
14+
{
15+
Schema::table('links', function (Blueprint $table)
16+
{
17+
$table->date('expiry_date')->nullable();
18+
});
19+
}
20+
21+
/**
22+
* Reverse the migrations.
23+
*
24+
* @return void
25+
*/
26+
public function down()
27+
{
28+
Schema::table('links', function (Blueprint $table)
29+
{
30+
$table->dropIndex('expiry_date');
31+
});
32+
}
33+
}

public/css/base.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ body {
3737
.error-alert {
3838
text-align: left;
3939
}
40+
.expiry-date input {
41+
line-height: 1;
42+
}
4043

4144
@media (min-width: 768px) {
4245
.content-div-padding {

public/js/AdminCtrl.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ polr.controller('AdminCtrl', function($scope, $compile, $timeout) {
156156
{className: 'wrap-text', data: 'long_url', name: 'long_url'},
157157
{data: 'clicks', name: 'clicks'},
158158
{data: 'created_at', name: 'created_at'},
159+
{data: 'expiry_date', name: 'expiry_date'},
159160
{data: 'creator', name: 'creator'},
160-
161161
{data: 'disable', name: 'disable', orderable: false, searchable: false},
162162
{data: 'delete', name: 'delete', orderable: false, searchable: false}
163163

@@ -172,7 +172,8 @@ polr.controller('AdminCtrl', function($scope, $compile, $timeout) {
172172
{className: 'wrap-text', data: 'short_url', name: 'short_url'},
173173
{className: 'wrap-text', data: 'long_url', name: 'long_url'},
174174
{data: 'clicks', name: 'clicks'},
175-
{data: 'created_at', name: 'created_at'}
175+
{data: 'created_at', name: 'created_at'},
176+
{data: 'expiry_date', name: 'expiry_date'}
176177
]
177178
}, datatables_config));
178179
};

resources/views/env.blade.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@
9494
# A comma-separated list of permitted email domains
9595
SETTING_ALLOWED_EMAIL_DOMAINS="{{$ST_ALLOWED_EMAIL_DOMAINS}}"
9696

97+
# Set to true to enable choosing date for expiring links
98+
SETTING_EYPIRY_DATE="{{$ST_EYPIRY_DATE}}"
99+
97100
# reCAPTCHA site key
98101
POLR_RECAPTCHA_SITE_KEY="{{$POLR_RECAPTCHA_SITE_KEY}}"
99102

resources/views/index.blade.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ class='form-control long-link-input' placeholder='http://' name='link-url' />
3636
<div id='link-availability-status'></div>
3737
</div>
3838
</div>
39+
40+
@if (!env('SETTING_EXPIRY_DATE'))
41+
{{-- Show secret toggle only if using counter-based ending --}}
42+
<div>
43+
<div class='expiry-date'>
44+
<label>Expiry date
45+
<input type='date' class='form-control' name='expiry_date'">
46+
</label>
47+
</div>
48+
</div>
49+
@endif
50+
3951
</div>
4052
<input type='submit' class='btn btn-info' id='shorten' value='Shorten' />
4153
<a href='#' class='btn btn-warning' id='show-link-options'>Link Options</a>

0 commit comments

Comments
 (0)