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;
|
2016-01-29 20:58:49 +01:00
|
|
|
use App\Factories\LinkFactory;
|
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) {
|
2015-12-28 23:54:53 +01:00
|
|
|
return redirect(route('index'))->with('error', $message);
|
2015-11-06 00:16:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function performShorten(Request $request) {
|
2016-02-13 20:42:43 +01:00
|
|
|
if (env('SETTING_SHORTEN_PERMISSION') && !self::isLoggedIn()) {
|
|
|
|
return redirect(route('index'))->with('error', 'You must be logged in to shorten links.');
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2016-01-29 20:58:49 +01:00
|
|
|
$creator = session('username');
|
2015-11-06 23:19:39 +01:00
|
|
|
|
2016-01-29 20:58:49 +01:00
|
|
|
$link_ip = $request->ip();
|
2015-11-06 00:16:55 +01:00
|
|
|
|
2016-01-29 20:58:49 +01:00
|
|
|
try {
|
|
|
|
$short_url = LinkFactory::createLink($long_url, $is_secret, $custom_ending, $link_ip, $creator);
|
2015-11-07 00:47:57 +01:00
|
|
|
}
|
2016-01-29 20:58:49 +01:00
|
|
|
catch (\Exception $e) {
|
|
|
|
return self::renderError($e->getMessage());
|
2015-11-07 02:25:48 +01:00
|
|
|
}
|
2015-11-07 00:47:57 +01:00
|
|
|
|
2016-01-29 20:58:49 +01:00
|
|
|
return view('shorten_result', ['short_url' => $short_url]);
|
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();
|
|
|
|
|
2016-09-11 22:01:34 +02:00
|
|
|
// Return 404 if link not found
|
2015-11-05 23:34:43 +01:00
|
|
|
if ($link == null) {
|
2016-09-11 22:01:34 +02:00
|
|
|
return abort(404);
|
2015-11-05 23:34:43 +01:00
|
|
|
}
|
|
|
|
|
2016-09-11 22:01:34 +02:00
|
|
|
// Return an error if the link has been disabled
|
|
|
|
// or return a 404 if SETTING_REDIRECT_404 is set to true
|
|
|
|
if ($link->is_disabled == 1) {
|
|
|
|
if (env('SETTING_REDIRECT_404')) {
|
|
|
|
return abort(404);
|
|
|
|
}
|
|
|
|
|
|
|
|
return view('error', [
|
|
|
|
'message' => 'Sorry, but this link has been disabled by an administrator.'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return a 403 if the secret key is incorrect
|
2015-11-07 00:47:57 +01:00
|
|
|
$link_secret_key = $link->secret_key;
|
2016-09-07 05:25:12 +02:00
|
|
|
if ($link_secret_key) {
|
|
|
|
if (!$secret_key) {
|
|
|
|
// if we do not receieve a secret key
|
|
|
|
// when we are expecting one, return a 403
|
|
|
|
return abort(403);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ($link_secret_key != $secret_key) {
|
|
|
|
// a secret key is provided, but it is incorrect
|
|
|
|
return abort(403);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-07 00:47:57 +01:00
|
|
|
|
2016-09-11 22:01:34 +02:00
|
|
|
// Increment click count
|
2015-11-06 23:19:39 +01:00
|
|
|
$long_url = $link->long_url;
|
2016-02-12 17:20:33 +01:00
|
|
|
$clicks = intval($link->clicks);
|
2015-11-14 05:51:01 +01:00
|
|
|
|
2016-02-12 17:20:33 +01:00
|
|
|
if (is_int($clicks)) {
|
|
|
|
$clicks += 1;
|
2015-11-14 05:51:01 +01:00
|
|
|
}
|
2016-02-12 17:20:33 +01:00
|
|
|
$link->clicks = $clicks;
|
2015-11-14 05:51:01 +01:00
|
|
|
$link->save();
|
|
|
|
|
2016-09-11 22:01:34 +02:00
|
|
|
// Redirect to final destination
|
2016-01-29 20:58:49 +01:00
|
|
|
LinkHelper::processPostClick($link);
|
2015-11-06 23:19:39 +01:00
|
|
|
return redirect()->to($long_url);
|
2015-11-05 23:34:43 +01:00
|
|
|
}
|
2016-09-11 22:01:34 +02:00
|
|
|
|
2015-11-05 23:34:43 +01:00
|
|
|
}
|