2015-11-05 23:34:43 +01:00
|
|
|
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
2015-11-06 23:19:39 +01:00
|
|
|
use Illuminate\Http\Redirect;
|
|
|
|
|
2015-11-05 23:34:43 +01:00
|
|
|
use App\Models\Link;
|
|
|
|
|
2015-11-07 03:44:50 +01:00
|
|
|
use App\Helpers\CryptoHelper;
|
2015-11-06 23:19:39 +01:00
|
|
|
use App\Helpers\LinkHelper;
|
|
|
|
|
2015-11-05 23:34:43 +01:00
|
|
|
class LinkController extends Controller {
|
|
|
|
/**
|
|
|
|
* Show the admin panel, and process admin AJAX requests.
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
2015-11-06 00:16:55 +01:00
|
|
|
|
2015-11-06 23:19:39 +01:00
|
|
|
private function renderError($message) {
|
|
|
|
$this->request->session()->flash('error', $message);
|
2015-11-06 00:16:55 +01:00
|
|
|
|
2015-11-06 23:19:39 +01:00
|
|
|
return redirect()->route('index');
|
2015-11-06 00:16:55 +01:00
|
|
|
}
|
|
|
|
|
2015-11-07 00:47:57 +01:00
|
|
|
private function formatAndRender($link_ending, $secret_ending=False) {
|
|
|
|
$short_url = env('APP_PROTOCOL') . env('APP_ADDRESS') . '/' . $link_ending;
|
|
|
|
if ($secret_ending) {
|
|
|
|
$short_url .= '/' . $secret_ending;
|
|
|
|
}
|
|
|
|
return view('shorten_result', ['short_url' => $short_url]);
|
|
|
|
}
|
|
|
|
|
2015-11-06 23:19:39 +01:00
|
|
|
|
2015-11-06 00:16:55 +01:00
|
|
|
public function performShorten(Request $request) {
|
2015-11-06 23:19:39 +01:00
|
|
|
$this->request = $request;
|
|
|
|
|
2015-11-06 00:16:55 +01:00
|
|
|
$long_url = $request->input('link-url');
|
|
|
|
$custom_ending = $request->input('custom-ending');
|
|
|
|
$is_secret = ($request->input('options') == "s" ? true : false);
|
2015-11-06 23:19:39 +01:00
|
|
|
$creator = session('username');
|
2015-11-06 00:16:55 +01:00
|
|
|
|
2015-11-06 23:19:39 +01:00
|
|
|
$is_already_short = LinkHelper::checkIfAlreadyShortened($long_url);
|
|
|
|
if ($is_already_short) {
|
|
|
|
return $this->renderError('Sorry, but your link already
|
|
|
|
looks like a shortened URL.');
|
2015-11-06 00:16:55 +01:00
|
|
|
}
|
|
|
|
|
2015-11-07 00:47:57 +01:00
|
|
|
if (!$is_secret && $existing_link = LinkHelper::longLinkExists($long_url)) {
|
|
|
|
// if link is not specified as secret, is non-custom, and
|
|
|
|
// already exists in Polr, lookup the value and return
|
|
|
|
return $this->formatAndRender($existing_link);
|
2015-11-06 00:16:55 +01:00
|
|
|
}
|
|
|
|
|
2015-11-06 23:19:39 +01:00
|
|
|
if ($custom_ending) {
|
|
|
|
// has custom ending
|
2015-11-07 02:25:48 +01:00
|
|
|
$ending_conforms = LinkHelper::validateEnding($custom_ending);
|
|
|
|
if (!$ending_conforms) {
|
2015-11-06 23:19:39 +01:00
|
|
|
return $this->renderError('Sorry, but custom endings
|
|
|
|
can only contain alphanumeric characters');
|
|
|
|
}
|
|
|
|
|
|
|
|
$ending_in_use = LinkHelper::linkExists($custom_ending);
|
|
|
|
if ($ending_in_use) {
|
|
|
|
return $this->renderError('Sorry, but this URL ending is already in use.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$link_ending = $custom_ending;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// no custom ending
|
|
|
|
$link_ending = LinkHelper::findSuitableEnding();
|
|
|
|
}
|
|
|
|
|
|
|
|
$link = new Link;
|
|
|
|
$link->short_url = $link_ending;
|
|
|
|
$link->long_url = $long_url;
|
|
|
|
$link->ip = $request->ip();
|
2015-11-07 02:25:48 +01:00
|
|
|
$link->is_custom = $custom_ending != null;
|
2015-11-06 23:19:39 +01:00
|
|
|
|
|
|
|
if ($creator) {
|
|
|
|
// if user is logged in, save user as creator
|
|
|
|
$link->creator = $creator;
|
|
|
|
}
|
2015-11-06 00:16:55 +01:00
|
|
|
|
2015-11-07 00:47:57 +01:00
|
|
|
if ($is_secret) {
|
|
|
|
$rand_bytes_num = intval(env('POLR_SECRET_BYTES'));
|
2015-11-07 03:44:50 +01:00
|
|
|
$secret_key = CryptoHelper::generateRandomHex($rand_bytes_num);
|
2015-11-07 00:47:57 +01:00
|
|
|
$link->secret_key = $secret_key;
|
|
|
|
}
|
2015-11-07 02:25:48 +01:00
|
|
|
else {
|
|
|
|
$secret_key = false;
|
|
|
|
}
|
2015-11-07 00:47:57 +01:00
|
|
|
|
2015-11-06 23:19:39 +01:00
|
|
|
$link->save();
|
2015-11-06 00:16:55 +01:00
|
|
|
|
2015-11-07 00:47:57 +01:00
|
|
|
return $this->formatAndRender($link_ending, $secret_key);
|
2015-11-06 00:16:55 +01:00
|
|
|
}
|
|
|
|
|
2015-11-07 00:47:57 +01:00
|
|
|
public function performRedirect(Request $request, $short_url, $secret_key=false) {
|
2015-11-05 23:34:43 +01:00
|
|
|
$link = Link::where('short_url', $short_url)
|
|
|
|
->first();
|
|
|
|
|
|
|
|
if ($link == null) {
|
|
|
|
return abort(404);
|
|
|
|
}
|
|
|
|
|
2015-11-07 00:47:57 +01:00
|
|
|
$link_secret_key = $link->secret_key;
|
|
|
|
|
|
|
|
if ($link->disabled == 1) {
|
2015-11-06 00:16:55 +01:00
|
|
|
return view('error', [
|
2015-11-06 23:19:39 +01:00
|
|
|
'message' => 'Sorry, but this link has been disabled by an administrator.'
|
2015-11-06 00:16:55 +01:00
|
|
|
]);
|
2015-11-05 23:34:43 +01:00
|
|
|
}
|
|
|
|
|
2015-11-14 05:51:01 +01:00
|
|
|
|
|
|
|
|
2015-11-07 00:47:57 +01:00
|
|
|
if ($link_secret_key) {
|
|
|
|
if (!$secret_key) {
|
|
|
|
// if we do not receieve a secret key
|
|
|
|
// when we are expecting one, return a 404
|
|
|
|
return abort(404);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ($link_secret_key != $secret_key) {
|
|
|
|
// a secret key is provided, but it is incorrect
|
|
|
|
return abort(404);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-11-06 23:19:39 +01:00
|
|
|
$long_url = $link->long_url;
|
2015-11-14 05:51:01 +01:00
|
|
|
|
|
|
|
if (is_int($link->clicks)) {
|
|
|
|
$link->clicks += 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$link->clicks = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
$link->save();
|
|
|
|
|
2015-11-06 23:19:39 +01:00
|
|
|
return redirect()->to($long_url);
|
2015-11-05 23:34:43 +01:00
|
|
|
}
|
|
|
|
}
|