1
0
mirror of https://github.com/cydrobolt/polr.git synced 2024-11-14 22:22:32 +01:00
polr/app/Http/Controllers/LinkController.php

100 lines
2.8 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Redirect;
use App\Models\Link;
use App\Factories\LinkFactory;
use App\Helpers\CryptoHelper;
use App\Helpers\LinkHelper;
class LinkController extends Controller {
/**
* Show the admin panel, and process admin AJAX requests.
*
* @return Response
*/
2015-11-06 00:16:55 +01:00
private function renderError($message) {
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.');
}
$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);
$creator = session('username');
$link_ip = $request->ip();
2015-11-06 00:16:55 +01:00
try {
$short_url = LinkFactory::createLink($long_url, $is_secret, $custom_ending, $link_ip, $creator);
2015-11-07 00:47:57 +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
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) {
$link = Link::where('short_url', $short_url)
->first();
// Return 404 if link not found
if ($link == null) {
return abort(404);
}
// 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;
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
// Increment click count
$long_url = $link->long_url;
$clicks = intval($link->clicks);
if (is_int($clicks)) {
$clicks += 1;
}
$link->clicks = $clicks;
$link->save();
// Redirect to final destination
LinkHelper::processPostClick($link);
return redirect()->to($long_url);
}
}