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

99 lines
2.7 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\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) {
$this->request->session()->flash('error', $message);
2015-11-06 00:16:55 +01:00
return redirect()->route('index');
2015-11-06 00:16:55 +01:00
}
2015-11-06 00:16:55 +01:00
public function performShorten(Request $request) {
$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');
2015-11-06 00:16:55 +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
}
if ($is_secret) {
// TODO if secret label as custom and don't return on lookup
}
if ($custom_ending) {
// has custom ending
$is_alphanum = ctype_alnum($custom_ending);
if (!$is_alphanum) {
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();
$link->is_custom = isset($custom_ending);
if ($creator) {
// if user is logged in, save user as creator
$link->creator = $creator;
}
2015-11-06 00:16:55 +01:00
$link->save();
2015-11-06 00:16:55 +01:00
$short_url = env('APP_PROTOCOL') . env('APP_ADDRESS') . "/" . $link_ending;
return view('shorten_result', ['short_url' => $short_url]);
}
public function performRedirect(Request $request, $short_url) {
$link = Link::where('short_url', $short_url)
->first();
if ($link == null) {
return abort(404);
}
if ($link['disabled'] == 1) {
2015-11-06 00:16:55 +01:00
return view('error', [
'message' => 'Sorry, but this link has been disabled by an administrator.'
2015-11-06 00:16:55 +01:00
]);
}
$long_url = $link->long_url;
return redirect()->to($long_url);
}
}