From dd1a9286a25cc7c61eb0cc7c5e0b4974316a74e2 Mon Sep 17 00:00:00 2001 From: Deon George Date: Thu, 13 Oct 2022 22:41:38 +1100 Subject: [PATCH] Enabled redirection --- .editorconfig | 4 +- app/Http/Controllers/RequestController.php | 73 ++++++++++++ app/Models/Site.php | 29 +++++ app/Models/SiteUri.php | 44 +++++++ app/Providers/AppServiceProvider.php | 6 +- app/Traits/SingleOrFail.php | 45 +++++++ .../migrations/2022_10_12_103229_sites.php | 55 +++++++++ resources/views/welcome.blade.php | 111 ++---------------- routes/web.php | 7 +- 9 files changed, 269 insertions(+), 105 deletions(-) create mode 100644 app/Http/Controllers/RequestController.php create mode 100644 app/Models/Site.php create mode 100644 app/Models/SiteUri.php create mode 100644 app/Traits/SingleOrFail.php create mode 100644 database/migrations/2022_10_12_103229_sites.php diff --git a/.editorconfig b/.editorconfig index 1671c9b..811eca2 100644 --- a/.editorconfig +++ b/.editorconfig @@ -3,8 +3,8 @@ root = true [*] charset = utf-8 end_of_line = lf -insert_final_newline = true -indent_style = space +insert_final_newline = false +indent_style = tab indent_size = 4 trim_trailing_whitespace = true diff --git a/app/Http/Controllers/RequestController.php b/app/Http/Controllers/RequestController.php new file mode 100644 index 0000000..fcba462 --- /dev/null +++ b/app/Http/Controllers/RequestController.php @@ -0,0 +1,73 @@ +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); + } + } + } +} \ No newline at end of file diff --git a/app/Models/Site.php b/app/Models/Site.php new file mode 100644 index 0000000..f7a8e11 --- /dev/null +++ b/app/Models/Site.php @@ -0,0 +1,29 @@ +hasMany(SiteUri::class); + } + + /* ATTRIBUTES */ + + public function getActiveAttribute(bool $value): bool + { + return $value && (strlen($this->redirect) > 0); + } +} \ No newline at end of file diff --git a/app/Models/SiteUri.php b/app/Models/SiteUri.php new file mode 100644 index 0000000..459ad16 --- /dev/null +++ b/app/Models/SiteUri.php @@ -0,0 +1,44 @@ +belongsTo(Site::class); + } + + /* ATTRIBUTES */ + + public function getActiveAttribute(bool $value): bool + { + return $value && (strlen($this->redirect) > 0); + } + + public function getDelayAttribute(int $value=NULL): int + { + return $this->active && $value ? $value : $this->site->delay; + } + + public function getMessageAttribute(string $value=NULL): string + { + return $this->active && $value ? $value : $this->site->message; + } + + public function getRedirectAttribute(string $value=NULL): string + { + return ($x=($this->getRawOriginal('active') && $value ? $value : $this->site->redirect)) ? $x.(request()->getRequestUri() != '/' ? request()->getRequestUri() : '') : ''; + } +} \ No newline at end of file diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index ee8ca5b..1d80324 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -4,6 +4,8 @@ use Illuminate\Support\ServiceProvider; +use App\Traits\SingleOrFail; + class AppServiceProvider extends ServiceProvider { /** @@ -23,6 +25,6 @@ public function register() */ public function boot() { - // + SingleOrFail::bootSingleOrfail(); } -} +} \ No newline at end of file diff --git a/app/Traits/SingleOrFail.php b/app/Traits/SingleOrFail.php new file mode 100644 index 0000000..2a73630 --- /dev/null +++ b/app/Traits/SingleOrFail.php @@ -0,0 +1,45 @@ +get(); + + if (($x=$result->count()) == 1) + return $result->first(); + + throw new ModelNotFoundException(sprintf('Query brings back %d record(s) called for singleOrFail()',$x)); + }); + + // When a query should return 1 object, or NULL if it doesnt + Builder::macro('single',function () { + $result = $this->get(); + + if ($result->count() == 1) + return $result->first(); + + return NULL; + }); + + // When a query should return 1 object, or setup to create a new object + Builder::macro('singleOrNew',function (array $args=[]) { + $result = $this->where($args)->get(); + + if ($result->count() == 1) + return $result->first(); + + return $this->newModelInstance($args); + }); + } +} diff --git a/database/migrations/2022_10_12_103229_sites.php b/database/migrations/2022_10_12_103229_sites.php new file mode 100644 index 0000000..8ecfbbb --- /dev/null +++ b/database/migrations/2022_10_12_103229_sites.php @@ -0,0 +1,55 @@ +id(); + $table->timestamps(); + $table->boolean('active')->default('FALSE'); + $table->string('site')->unique(); + $table->string('redirect')->nullable(); + $table->integer('delay')->nullable(); + $table->text('message')->nullable(); + $table->boolean('permanent')->default(FALSE); + }); + + Schema::create('site_uris', function (Blueprint $table) { + $table->id(); + $table->timestamps(); + $table->boolean('active')->default('FALSE'); + $table->string('uri'); + $table->bigInteger('hits'); + $table->string('redirect')->nullable(); + $table->integer('delay')->nullable(); + $table->text('message')->nullable(); + $table->boolean('permanent')->default(FALSE); + + $table->integer('site_id'); + $table->foreign('site_id')->references('id')->on('sites'); + + $table->unique(['site_id','uri']); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('site_uris'); + Schema::dropIfExists('sites'); + } +}; diff --git a/resources/views/welcome.blade.php b/resources/views/welcome.blade.php index de23392..f70db53 100644 --- a/resources/views/welcome.blade.php +++ b/resources/views/welcome.blade.php @@ -19,114 +19,29 @@ font-family: 'Nunito', sans-serif; } + + @if($uri->site->delay) + + @endif
- @if (Route::has('login')) - - @endif -
-
- - - - - +
+

This site has moved.

-
-
- - -
-
- Laravel has wonderful, thorough documentation covering every aspect of the framework. Whether you are new to the framework or have previous experience with Laravel, we recommend reading all of the documentation from beginning to end. -
-
+
+
+ {{ $uri->message }}
- -
-
- - -
- -
-
- Laracasts offers thousands of video tutorials on Laravel, PHP, and JavaScript development. Check them out, see for yourself, and massively level up your development skills in the process. -
-
-
- -
-
- - -
- -
-
- Laravel News is a community driven portal and newsletter aggregating all of the latest and most important news in the Laravel ecosystem, including new package releases and tutorials. -
-
-
- -
-
- -
Vibrant Ecosystem
-
- -
-
- Laravel's robust library of first-party tools and libraries, such as Forge, Vapor, Nova, and Envoyer help you take your projects to the next level. Pair them with powerful open source libraries like Cashier, Dusk, Echo, Horizon, Sanctum, Telescope, and more. -
-
-
-
-
- -
-
-
- - - - - - Shop - - - - - - - - Sponsor - -
-
- -
- Laravel v{{ Illuminate\Foundation\Application::VERSION }} (PHP v{{ PHP_VERSION }}) +
+ Redirecting to {{ $uri->redirect }} in {{ $uri->delay }} seconds... +
- + \ No newline at end of file diff --git a/routes/web.php b/routes/web.php index b130397..4f02e0e 100644 --- a/routes/web.php +++ b/routes/web.php @@ -2,6 +2,8 @@ use Illuminate\Support\Facades\Route; +use App\Http\Controllers\RequestController; + /* |-------------------------------------------------------------------------- | Web Routes @@ -13,6 +15,5 @@ | */ -Route::get('/', function () { - return view('welcome'); -}); +Route::any('{uri?}',[RequestController::class,'main']) + ->where('uri','.*');