From 0f52d80ca67a49258b235f5831163dd72fbd54cf Mon Sep 17 00:00:00 2001 From: Frankie B Date: Tue, 11 Jun 2024 18:02:01 +0100 Subject: Merge MVC rewrite into master (#21) * Just commit it all * Require auth * crap * Update homepage * Block AI scrapers * Update cache update script * Add dummy file * Remove unnecessary lastfm config var * Use withQueryParameters for LastFM API * Fix embeds * Update example env * Smard --- app/Http/Controllers/AdminBookmarksController.php | 15 +++++ app/Http/Controllers/AdminGuestbookController.php | 34 +++++++++++ app/Http/Controllers/AdminImportController.php | 69 +++++++++++++++++++++++ app/Http/Controllers/BookmarksController.php | 15 +++++ app/Http/Controllers/CalculatorsController.php | 13 +++++ app/Http/Controllers/ComputersController.php | 13 +++++ app/Http/Controllers/GuestbookController.php | 49 +++++++++------- app/Http/Controllers/HomeController.php | 32 +++++++++++ app/Http/Controllers/MusicController.php | 69 +++++++++++++++++++++++ 9 files changed, 289 insertions(+), 20 deletions(-) create mode 100644 app/Http/Controllers/AdminBookmarksController.php create mode 100644 app/Http/Controllers/AdminGuestbookController.php create mode 100644 app/Http/Controllers/AdminImportController.php create mode 100644 app/Http/Controllers/BookmarksController.php create mode 100644 app/Http/Controllers/CalculatorsController.php create mode 100644 app/Http/Controllers/ComputersController.php create mode 100644 app/Http/Controllers/HomeController.php create mode 100644 app/Http/Controllers/MusicController.php (limited to 'app/Http/Controllers') diff --git a/app/Http/Controllers/AdminBookmarksController.php b/app/Http/Controllers/AdminBookmarksController.php new file mode 100644 index 0000000..c7d8afd --- /dev/null +++ b/app/Http/Controllers/AdminBookmarksController.php @@ -0,0 +1,15 @@ +get(); + return view('admin.bookmarks', compact('categories')); + } +} diff --git a/app/Http/Controllers/AdminGuestbookController.php b/app/Http/Controllers/AdminGuestbookController.php new file mode 100644 index 0000000..5ebf451 --- /dev/null +++ b/app/Http/Controllers/AdminGuestbookController.php @@ -0,0 +1,34 @@ +distinct()->count('ip'); + return $uniqueIpsCount; + } + + function getGuestbookEntriesCount(): int { + $entryCount = DB::table('guestbook__entries')->count(); + return $entryCount; + } + public function show() : View { + $guestbook_unique_addr = $this->getGuestbookUniqueAddr(); + $guestbook_entry_count = $this->getGuestbookEntriesCount(); + $entries = GuestbookEntry::selectEntries(); + $parser = Parser::create(); + + return view('admin.guestbook', [ + 'guestbook_unique_addr' => $guestbook_unique_addr, + 'guestbook_entry_count' => $guestbook_entry_count, + 'entries' => $entries, + 'parser' => $parser, + ]); + } +} diff --git a/app/Http/Controllers/AdminImportController.php b/app/Http/Controllers/AdminImportController.php new file mode 100644 index 0000000..dc32cec --- /dev/null +++ b/app/Http/Controllers/AdminImportController.php @@ -0,0 +1,69 @@ +validate([ + 'data_file' => 'required|mimes:json', + ]); + + $file = $request->file('data_file'); + $jsonContent = file_get_contents($file->getRealPath()); + $data = json_decode($jsonContent, true); + $tables = []; + foreach($data as $item) { + if ($item['type'] !== "table") continue; + $tables[$item['name']] = [ + 'data' => $item['data'], + 'count' => count($item['data']) + ]; + + if ($item['name'] === "guestbook__entries") { + GuestbookEntry::importGuestbookEntry($item['data']); + } + $this->import($item['data'], $item['name']); + } + return view('admin.import-success', ['tables' => $tables]); + } + + /** + * Imports the given data to the specified table + * + * @param array $data The data to import + * @param string $table_name The name of the table to import to + * @return void + * @throws Exception Invalid table specified, to be replaced with custom exception + */ + public function import(array $data, string $table_name): void { + switch ($table_name) { + case 'guestbook__entries': + GuestbookEntry::importGuestbookEntry($data); + break; + case 'bookmark__categories' : + BookmarkCategory::importBookmarkCategory($data); + break; + case 'bookmark__sites': + BookmarkSite::importBookmark($data); + break; + case 'guestbook__bans': + break; + default: + // TODO: Replace with custom exception + throw new Exception("Invalid table specified ($table_name)"); + } + } +} diff --git a/app/Http/Controllers/BookmarksController.php b/app/Http/Controllers/BookmarksController.php new file mode 100644 index 0000000..56aacc7 --- /dev/null +++ b/app/Http/Controllers/BookmarksController.php @@ -0,0 +1,15 @@ +get(); + return view('bookmarks', compact('categories')); + } +} diff --git a/app/Http/Controllers/CalculatorsController.php b/app/Http/Controllers/CalculatorsController.php new file mode 100644 index 0000000..38a7a41 --- /dev/null +++ b/app/Http/Controllers/CalculatorsController.php @@ -0,0 +1,13 @@ +with('entries', $entries) + ->with('parser', $parser); } - public function guestbookPost(Request $request) { + /** + * Creates a new guestbook entry + * + * @param Request $request + * @return RedirectResponse + * @throws ValidationException + */ + public function addEntry(Request $request): RedirectResponse { $this->validate($request, [ 'name' => 'required', 'message' => 'required' ]); - $matching_bans = DB::select('SELECT reason FROM guestbook__bans WHERE ip_address = ?', array($request->ip())); - - if (!empty($matching_bans)) { - return view('errors.guestbook-ipban')->with('reason', $matching_bans[0]->reason); - } - - DB::insert( - 'INSERT INTO guestbook__entries (name, timestamp, ip_address, agent, message) values (?, ?, ?, ?, ?)', - [ - htmlspecialchars($request->get('name')), - time(), - $request->ip(), - $request->userAgent(), - htmlspecialchars($request->get('message')) - ] - ); + GuestbookEntry::insertGuestbookEntry($request); return back()->with('success', 'Entry submitted successfully!'); } + + public function banIP(string $addr) { + // TODO: Add banning system + // $matching_bans = DB::select('SELECT reason FROM guestbook__bans WHERE ip_address = ?', array($request->ip())); + // if (!empty($matching_bans)) { + // return view('errors.guestbook-ipban')->with('reason', $matching_bans[0]->reason); + // } + } } diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php new file mode 100644 index 0000000..3fad094 --- /dev/null +++ b/app/Http/Controllers/HomeController.php @@ -0,0 +1,32 @@ +diff($currentDate); + return $age->y; + } + + /** + * Shows home page + * @return View + */ + public function show() : View { + return view('home', [ + 'age' => $this->returnAge() + ]); + } +} diff --git a/app/Http/Controllers/MusicController.php b/app/Http/Controllers/MusicController.php new file mode 100644 index 0000000..5e31d86 --- /dev/null +++ b/app/Http/Controllers/MusicController.php @@ -0,0 +1,69 @@ + 'user.getrecenttracks', + 'user' => Config::get('services.lastfm.user'), + 'format' => 'json', + 'nowplaying' => 'true', + 'api_key' => Config::get('services.lastfm.key') + ])->get('https://ws.audioscrobbler.com/2.0/'); + $data = $response->json(); + error_log($response->body()); + $track_data = $data["recenttracks"]["track"][0]; + $current_track = [ + 'title' => $track_data["name"], + 'artist' => $track_data["artist"]["#text"], + 'url' => $track_data["url"], + ]; + Cache::put('current_track', $current_track, now()->addSeconds(15)); + return $current_track; + } + + public function getTopTracks() { + // If it's already cached just return that + if (Cache::has('top_tracks')) { + return Cache::get('top_tracks'); + } + + $response = Http::withQueryParameters([ + 'method' => 'user.gettoptracks', + 'user' => Config::get('services.lastfm.user'), + 'format' => 'json', + 'period' => '1month', + 'limit' => 10, + 'api_key' => Config::get('services.lastfm.key') + ])->get('https://ws.audioscrobbler.com/2.0/'); + $data = $response->json(); + $topTracks = []; + foreach ($data["toptracks"]["track"] as $track) { + $topTracks[] = [ + 'title' => $track["name"], + 'artist' => $track["artist"]["name"], + 'url' => $track["url"], + 'plays' => $track["playcount"], + ]; + } + Cache::put('top_tracks', $topTracks, now()->addSeconds(15)); + return $topTracks; + } + public function show() : View { + return view('music') + ->with('current_track', $this->getCurrentTrack()) + ->with('top_tracks', $this->getTopTracks()); + } +} -- cgit v1.2.3-54-g00ecf