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