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
This commit is contained in:
parent
2fbf6cdc39
commit
c9299b5410
88 changed files with 1982 additions and 1661 deletions
15
app/Http/Controllers/AdminBookmarksController.php
Normal file
15
app/Http/Controllers/AdminBookmarksController.php
Normal file
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\BookmarkCategory;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class AdminBookmarksController extends Controller
|
||||
{
|
||||
public function show() : View {
|
||||
$categories = BookmarkCategory::with('sites')->get();
|
||||
return view('admin.bookmarks', compact('categories'));
|
||||
}
|
||||
}
|
34
app/Http/Controllers/AdminGuestbookController.php
Normal file
34
app/Http/Controllers/AdminGuestbookController.php
Normal file
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\GuestbookEntry;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\View\View;
|
||||
use UAParser\Parser;
|
||||
|
||||
class AdminGuestbookController extends Controller
|
||||
{
|
||||
function getGuestbookUniqueAddr(): int {
|
||||
$uniqueIpsCount = DB::table('guestbook__entries')->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,
|
||||
]);
|
||||
}
|
||||
}
|
69
app/Http/Controllers/AdminImportController.php
Normal file
69
app/Http/Controllers/AdminImportController.php
Normal file
|
@ -0,0 +1,69 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\BookmarkCategory;
|
||||
use App\Models\BookmarkSite;
|
||||
use App\Models\GuestbookEntry;
|
||||
use Exception;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class AdminImportController extends Controller
|
||||
{
|
||||
public function show() : View {
|
||||
return view('admin.import');
|
||||
}
|
||||
|
||||
public function submit(Request $request)
|
||||
{
|
||||
$request->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)");
|
||||
}
|
||||
}
|
||||
}
|
15
app/Http/Controllers/BookmarksController.php
Normal file
15
app/Http/Controllers/BookmarksController.php
Normal file
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\BookmarkSite;
|
||||
use App\Models\BookmarkCategory;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class BookmarksController extends Controller
|
||||
{
|
||||
public function show() : View {
|
||||
$categories = BookmarkCategory::with('sites')->get();
|
||||
return view('bookmarks', compact('categories'));
|
||||
}
|
||||
}
|
13
app/Http/Controllers/CalculatorsController.php
Normal file
13
app/Http/Controllers/CalculatorsController.php
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class CalculatorsController extends Controller
|
||||
{
|
||||
public function show() : View {
|
||||
return view('calculators');
|
||||
}
|
||||
}
|
13
app/Http/Controllers/ComputersController.php
Normal file
13
app/Http/Controllers/ComputersController.php
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class ComputersController extends Controller
|
||||
{
|
||||
public function show() : View {
|
||||
return view('computers');
|
||||
}
|
||||
}
|
|
@ -2,37 +2,46 @@
|
|||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\GuestbookEntry;
|
||||
use Illuminate\Http\Request;
|
||||
use DB;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use UAParser\Parser;
|
||||
|
||||
class GuestbookController extends Controller {
|
||||
public function guestbook() {
|
||||
return view('pages.guestbook');
|
||||
public function show(): View {
|
||||
$entries = GuestbookEntry::selectEntries();
|
||||
$parser = Parser::create();
|
||||
|
||||
return view('guestbook')
|
||||
->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);
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
|
32
app/Http/Controllers/HomeController.php
Normal file
32
app/Http/Controllers/HomeController.php
Normal file
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\View\View;
|
||||
use DateTime;
|
||||
|
||||
class HomeController extends Controller
|
||||
{
|
||||
/**
|
||||
* Returns age based on birthday date and current date (GMT)
|
||||
* @return int
|
||||
*/
|
||||
function returnAge(): int
|
||||
{
|
||||
date_default_timezone_set('Europe/London');
|
||||
$birthday = new DateTime("2005-06-07");
|
||||
$currentDate = DateTime::createFromFormat("Y-m-d", date("Y-m-d"));
|
||||
$age = $birthday->diff($currentDate);
|
||||
return $age->y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows home page
|
||||
* @return View
|
||||
*/
|
||||
public function show() : View {
|
||||
return view('home', [
|
||||
'age' => $this->returnAge()
|
||||
]);
|
||||
}
|
||||
}
|
69
app/Http/Controllers/MusicController.php
Normal file
69
app/Http/Controllers/MusicController.php
Normal file
|
@ -0,0 +1,69 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class MusicController extends Controller
|
||||
{
|
||||
public function getCurrentTrack() {
|
||||
// If it's already cached just return that
|
||||
if (Cache::has('current_track')) {
|
||||
return Cache::get('current_track');
|
||||
}
|
||||
|
||||
$response = Http::withQueryParameters([
|
||||
'method' => '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());
|
||||
}
|
||||
}
|
|
@ -16,6 +16,9 @@ class RateLimiter
|
|||
*/
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
if (auth()->check()) {
|
||||
return $next($request);
|
||||
}
|
||||
$ipAddress = $request->ip();
|
||||
$cacheKey = 'rate_limit_'.$ipAddress;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue