1
0
mirror of https://github.com/cydrobolt/polr.git synced 2024-09-19 15:11:40 +02:00

Add expiry date for links

This commit is contained in:
Sebastian L 2022-02-03 15:29:00 +01:00
parent 6e73538257
commit 4a46f33903
13 changed files with 96 additions and 17 deletions

View File

@ -26,7 +26,7 @@ class LinkFactory {
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.
@ -36,6 +36,7 @@ class LinkFactory {
* @param string (optional) $custom_ending
* @param string $link_ip
* @param string $creator
* @param string (optional) $expiry_date
* @param bool $return_object
* @param bool $is_api
* @return string $formatted_link
@ -89,12 +90,12 @@ class LinkFactory {
}
$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;

View File

@ -141,13 +141,13 @@ class AdminPaginationController extends Controller {
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);
}
@ -156,12 +156,12 @@ class AdminPaginationController extends Controller {
$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);
}
}

View File

@ -7,10 +7,10 @@ use App\Helpers\LinkHelper;
use App\Exceptions\Api\ApiException;
class ApiLinkController extends ApiController {
protected function getShortenedLink($long_url, $is_secret, $custom_ending, $link_ip, $username, $response_type) {
protected function getShortenedLink($long_url, $is_secret, $custom_ending, $link_ip, $username, $expiry_date $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, $expiry_date, false, true);
}
catch (\Exception $e) {
throw new ApiException('CREATION_ERROR', $e->getMessage(), 400, $response_type);
@ -39,6 +39,7 @@ class ApiLinkController extends ApiController {
$request->input('custom_ending'),
$request->ip(),
$user->username,
$user->input('expiry_date'),
$response_type
);
@ -84,6 +85,7 @@ class ApiLinkController extends ApiController {
array_get($link, 'custom_ending'),
$link_ip,
$username,
array_get($link, 'expiry_date'),
$response_type
);

View File

@ -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;
@ -34,11 +35,12 @@ class LinkController extends Controller {
$long_url = $request->input('link-url');
$custom_ending = $request->input('custom-ending');
$is_secret = ($request->input('options') == "s" ? true : false);
$expiry_date = $request->input('expiry_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);
}
catch (\Exception $e) {
return self::renderError($e->getMessage());
@ -55,6 +57,16 @@ class LinkController extends Controller {
if ($link == null) {
return abort(404);
}
// Return an error if link has expired.
if ($link->expiry_date !== '0000-00-00' && $link->expiry_date !== null && $link->expiry_date < Carbon::today()->toDateString()) {
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

View File

@ -127,6 +127,7 @@ class SetupController extends Controller {
$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');
@ -185,6 +186,7 @@ class SetupController extends Controller {
'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

View File

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

View File

@ -37,6 +37,9 @@ body {
.error-alert {
text-align: left;
}
.expiry-date input {
line-height: 1;
}
@media (min-width: 768px) {
.content-div-padding {

View File

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

View File

@ -94,6 +94,9 @@ SETTING_RESTRICT_EMAIL_DOMAIN={{$ST_RESTRICT_EMAIL_DOMAIN}}
# 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}}"

View File

@ -36,6 +36,18 @@
<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>

View File

@ -113,6 +113,15 @@ Setup
<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>

View File

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

View File

@ -36,7 +36,7 @@ class LinkHelperTest extends TestCase
}
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