Skip to content

Commit

Permalink
Correct bugs for LinkController
Browse files Browse the repository at this point in the history
  • Loading branch information
cydrobolt committed Nov 7, 2015
1 parent f367910 commit 8c50b2f
Show file tree
Hide file tree
Showing 12 changed files with 424 additions and 40 deletions.
339 changes: 339 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions app/Helpers/LinkHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ static public function linkExists($link_ending) {
*/
$link = Link::where('short_url', $link_ending)
->first();

if ($link == null) {
return false;
}
else {
return $link->short_url;
return true;
}
}

Expand All @@ -56,6 +57,7 @@ static public function longLinkExists($long_url) {
* @return boolean
*/
$link = Link::where('long_url', $long_url)
->where('is_custom', 0)
->first();
if ($link == null) {
return false;
Expand All @@ -65,6 +67,11 @@ static public function longLinkExists($long_url) {
}
}

static public function validateEnding($link_ending) {
$is_alphanum = ctype_alnum($link_ending);

return $is_alphanum;
}

static public function findSuitableEnding() {
/**
Expand All @@ -74,12 +81,13 @@ static public function findSuitableEnding() {
*/
$base = env('POLR_BASE');

$link = Link::where('is_custom', '0')
$link = Link::where('is_custom', 0)
->orderBy('created_at', 'desc')
->first();

if ($link == null) {
$base10_val = 0;
$base_x_val = 0;
}
else {
$latest_link_ending = $link->short_url;
Expand Down
27 changes: 27 additions & 0 deletions app/Http/Controllers/AjaxController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Helpers\LinkHelper;

class AjaxController extends Controller {
/**
* Process non-admin AJAX requests.
*
* @return Response
*/
public function checkLinkAvailability(Request $request) {
$link_ending = $request->input('link_ending');
$ending_conforms = LinkHelper::validateEnding($link_ending);

if (!$ending_conforms) {
return "invalid";
}
else if (LinkHelper::linkExists($link_ending)) {
// if ending already exists
return "unavailable";
}
else {
return "available";
}
}
}
Empty file.
12 changes: 6 additions & 6 deletions app/Http/Controllers/LinkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ public function performShorten(Request $request) {

if ($custom_ending) {
// has custom ending
$is_alphanum = ctype_alnum($custom_ending);

if (!$is_alphanum) {
$ending_conforms = LinkHelper::validateEnding($custom_ending);
if (!$ending_conforms) {
return $this->renderError('Sorry, but custom endings
can only contain alphanumeric characters');
}
Expand All @@ -70,13 +69,11 @@ public function performShorten(Request $request) {
$link_ending = LinkHelper::findSuitableEnding();
}



$link = new Link;
$link->short_url = $link_ending;
$link->long_url = $long_url;
$link->ip = $request->ip();
$link->is_custom = isset($custom_ending);
$link->is_custom = $custom_ending != null;

if ($creator) {
// if user is logged in, save user as creator
Expand All @@ -89,6 +86,9 @@ public function performShorten(Request $request) {
$secret_key = bin2hex($rand_bytes);
$link->secret_key = $secret_key;
}
else {
$secret_key = false;
}

$link->save();

Expand Down
3 changes: 3 additions & 0 deletions app/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@

$app->post('/login', ['as' => 'plogin', 'uses' => 'UserController@performLogin']);
$app->post('/shorten', ['as' => 'shorten', 'uses' => 'LinkController@performShorten']);

/* API endpoints */
$app->post('/api/v2/link_avail_check', ['as' => 'link_check', 'uses' => 'AjaxController@checkLinkAvailability']);
6 changes: 4 additions & 2 deletions public/css/about.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
}

.license {
font-size: 70%;
padding-top: 40px;
font-size: 80%;
margin-top: 40px;
width: auto;
color: black;
}

.license-btn {
Expand Down
4 changes: 4 additions & 0 deletions public/css/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@
.check-btn {
margin-bottom: 30px;
}

#link-availability-status {
margin-bottom: 20px;
}
12 changes: 6 additions & 6 deletions public/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ $(function() {
}
});
$('#check-link-availability').click(function() {
var customlink = $('#custom').val();
var custom_link = $('.custom-url-field').val();
var request = $.ajax({
url: "/api/v2/linkcheck",
url: "/api/v2/link_avail_check",
type: "POST",
data: {
link: customlink
link_ending: custom_link
},
dataType: "html"
});
$('#link-availability-status').html('<span><i class="fa fa-spinner"></i> Loading</span>');
request.done(function(msg) {
if (msg == '0') {
if (msg == 'unavailable') {
$('#link-availability-status').html(' <span style="color:red"><i class="fa fa-ban"></i> Already in use</span>');
} else if (msg == '1') {
} else if (msg == 'available') {
$('#link-availability-status').html('<span style="color:green"><i class="fa fa-check"></i> Available</span>');
} else if (msg == '2') {
} else if (msg == 'invalid') {
$('#link-availability-status').html('<span style="color:orange"><i class="fa fa-exclamation-triangle"></i> Invalid Custom URL Ending</span>');
} else {
$('#link-availability-status').html(' <span style="color:red"><i class="fa fa-exclamation-circle"></i> An error occured. Try again</span>' + msg);
Expand Down
35 changes: 17 additions & 18 deletions resources/views/about.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,23 @@
</p>
</div>
<a href='#' class='btn btn-success license-btn'>More Information</a>
<div class="license" id="gpl-license">
The GNU General Public License v3
<br />
Copyright (C) 2013-2015 Chaoyi Zha, the Polr Project
<br />
This program is free software: you can redistribute it and/or modify<br />
it under the terms of the GNU General Public License as published by<br />
the Free Software Foundation, either version 3 of the License, or<br />
(at your option) any later version.<br />
<br />
This program is distributed in the hope that it will be useful,<br />
but WITHOUT ANY WARRANTY; without even the implied warranty of<br />
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br />
GNU General Public License for more details.<br />
<br />
You should have received a copy of the GNU General Public License<br />
along with this program. If not, see <a href='http://www.gnu.org/copyleft/gpl.html'>http://www.gnu.org/copyleft/gpl.html</a>.
</div>
<pre class="license" id="gpl-license">
Copyright (C) 2013-2015 Chaoyi Zha

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
</pre>

@endsection

Expand Down
2 changes: 1 addition & 1 deletion resources/views/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<p>Customize link: </p>
<div class='custom-link-text'>
<h2 class='site-url-field'>{{env('APP_ADDRESS')}}/</h2>
<input type='text' class='form-control custom-url-field' name='custom-ending' />
<input type='text' autocomplete="off" class='form-control custom-url-field' name='custom-ending' />
</div>
<div>
<a href='#' class='btn btn-success btn-xs check-btn' id='check-link-availability'>Check Availability</a>
Expand Down
12 changes: 7 additions & 5 deletions resources/views/layouts/base.blade.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
<!--
Polr, a minimalist URL shortening platform.
Copyright (C) 2013-2015 Chaoyi Zha
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-->

<!DOCTYPE html>
Expand Down

0 comments on commit 8c50b2f

Please sign in to comment.