redirect/app/Http/Controllers/RequestController.php

73 lines
1.9 KiB
PHP
Raw Normal View History

2022-10-13 11:41:38 +00:00
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use App\Models\{Site,SiteUri};
class RequestController extends Controller
{
public function __construct()
{
if (gethostname() == request()->getHost())
$this->middleware('auth');
}
public function main()
{
// If the url is the same as the host, then present a site view
if (gethostname() == request()->getHost()) {
//$this->authorize('create', Site::class);
dd('Show site information',Auth::id());
// Otherwise process the request
} else {
// Find the site
$s = Site::singleOrNew(['site'=>request()->getHost()]);
if (! $s->exists) {
Log::info(sprintf('New site: %s (auto not active)',$s->site));
$s->site = request()->getHost();
$s->active = FALSE;
$s->save();
$u = SiteUri::firstOrNew(['uri'=>request()->getRequestUri()]);
$u->hits = 1;
$s->uris()->save($u);
return response('',444);
}
// Find the URI
$u = SiteUri::where('site_id',$s->id)->firstOrNew(['uri'=>request()->getRequestUri()]);
$u->hits++;
$s->uris()->save($u);
// If the site is not active, return
if (! $s->getRawOriginal('active')) {
Log::info(sprintf('Site: %s, with redirect: (%s) (NOT ACTIVE)',$s->site,$u->uri));
return response('',444);
}
Log::info(sprintf('Site: %s, URI: %s has redirect (%s) - hits (%d)',$u->site->site,$u->uri,$u->redirect,$u->hits));
// Do we have a redirect?
if (! $u->active) {
Log::info(sprintf(' - %s%s (%s) (%s)',$u->site->site,$u->uri,$u->redirect ?: '-NO REDIRECT-',$u->redirect ? 'URI NOT ACTIVE' : ''));
return response('',444);
}
if (! is_null($s->delay)) {
return view('welcome')
->with('uri',$u);
} else {
Log::info(sprintf(' - %s%s redirect to (%s) (%s)',$u->site->site,$u->uri,$u->redirect,$u->permanent ? 'PERM' : 'TEMP'));
return redirect($u->redirect,$u->permanent ? 301 : 302);
}
}
}
}