Verified Commit fbc79c2b authored by Luc Didry's avatar Luc Didry

Fix #51 — Automagically slugify custom url

parent b5f02193
......@@ -3,6 +3,8 @@ Revision history for Perl application Lstu
0.22-0 ????-??-??
- Disable URLs instead of removing them (prevents spammers to reuse a
deleted shortened URL)
- Slugify custom URLs and add a suffix (-2, -3…) if the custom URL is
already taken
0.21-4 2018-12-17
- Add a lockfile to GSB database update to prevent concurrent updates
......
......@@ -6,7 +6,7 @@ use Lstu::DB::Ban;
use Data::Validate::URI qw(is_http_uri is_https_uri);
use Mojo::JSON qw(to_json decode_json);
use Mojo::URL;
use Mojo::Util qw(b64_encode);
use Mojo::Util qw(b64_encode slugify);
use Image::PNG::QRCode 'qrpng';
sub add {
......@@ -54,13 +54,9 @@ sub add {
my ($msg, $short);
if (defined($custom_url) &&
(
$custom_url =~ m#^(a|d|cookie|stats|fullstats|login|logout|api)$# ||
$custom_url =~ m/\.json$/ || $custom_url !~ m/^[-a-zA-Z0-9_]+$/
)) {
$msg = $c->l('The shortened text can contain only numbers, letters and the - and _ character, can\'t be "a", "api", "d" or "stats" or end with ".json". Your URL to shorten: %1', $url);
} elsif (defined($custom_url) && Lstu::DB::URL->new(app => $c)->exist($custom_url) > 0) {
$msg = $c->l('The shortened text (%1) is already used. Please choose another one.', $custom_url);
($custom_url =~ m#^(a|d|cookie|stats|fullstats|login|logout|api)$# || $custom_url =~ m/\.json$/)
) {
$msg = $c->l('The shortened text can\'t be "a", "api", "d", "cookie", "stats", "fullstats", "login" or "logout" or end with ".json". Your URL to shorten: %1', $url);
} elsif (is_http_uri($url->to_string) || is_https_uri($url->to_string) || (defined($url->host) && $url->host =~ m/\.onion$/)) {
my $res = ($url->host =~ m/\.onion$/) ? {} : $c->is_spam($url, 0);
......@@ -85,6 +81,13 @@ sub add {
$short = $db_url->short;
} else {
if (defined($custom_url)) {
$custom_url = slugify $custom_url;
my $suffix = 2;
my $original_custom_url = $custom_url;
while (Lstu::DB::URL->new(app => $c)->exist($custom_url) > 0) {
$custom_url = $original_custom_url.'-'.$suffix;
$suffix++;
}
Lstu::DB::URL->new(
app => $c,
short => $custom_url,
......
......@@ -16,7 +16,7 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n"
#. ($url)
#: lib/Lstu/Controller/URL.pm:115
#: lib/Lstu/Controller/URL.pm:118
msgid "%1 is not a valid URL."
msgstr ""
......@@ -154,7 +154,7 @@ msgid "Next"
msgstr ""
#. ($c->config('contact')
#: lib/Lstu/Controller/URL.pm:109
#: lib/Lstu/Controller/URL.pm:112
msgid "No shortened URL available. Please retry or contact the administrator at %1. Your URL to shorten: %2"
msgstr ""
......@@ -235,14 +235,9 @@ msgstr ""
msgid "The shortened URL %1 doesn't exist."
msgstr ""
#. ($custom_url)
#: lib/Lstu/Controller/URL.pm:63
msgid "The shortened text (%1) is already used. Please choose another one."
msgstr ""
#. ($url)
#: lib/Lstu/Controller/URL.pm:61
msgid "The shortened text can contain only numbers, letters and the - and _ character, can't be \"a\", \"api\", \"d\" or \"stats\" or end with \".json\". Your URL to shorten: %1"
#: lib/Lstu/Controller/URL.pm:59
msgid "The shortened text can't be \"a\", \"api\", \"d\", \"cookie\", \"stats\", \"fullstats\", \"login\" or \"logout\" or end with \".json\". Your URL to shorten: %1"
msgstr ""
#: themes/default/templates/api.html.ep:113
......
......@@ -17,7 +17,7 @@
<img alt="QRCode" src="data:image/png;base64,<%= stash('qrcode') %>">
<div class="form-group">
<label><%= link_to stash('url') => begin %><%= stash('url') %><%= end %></a></label>
<br> =&gt; <input id="input-short" value="<%== stash('short') %>" class="form-control">
<br> =&gt; <input id="input-short" value="<%== stash('short') %>" class="form-control" size="<%= length(stash('short')) %>">
<span id="js" class="hidden"><a href="#" id="clipboard" class="btn btn-default" title="<%= l('Copy to clipboard') %>" data-short="<%== stash('short') %>"><span class="icon icon-clipboard"></span></a><span>
</div>
</h3>
......
......@@ -14,7 +14,7 @@
<fieldset class="result">
<img class="qrcode-result" alt="QRCode" src="data:image/png;base64,<%= stash('qrcode') %>">
<label><%= link_to stash('url') => begin %><h4><%= stash('url') %></h4><%= end %></label>
<input id="input-short" value="<%== stash('short') %>" class="form-control">
<input id="input-short" value="<%== stash('short') %>" class="form-control" size="<%= length(stash('short')) %>">
<span id="js" class="hidden"><a href="#" id="clipboard" class="button button-clear" title="<%= l('Copy to clipboard') %>" data-short="<%== stash('short') %>"><h5><span class="icon icon-clipboard"></span></h5></a><span>
</fieldset>
% }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment