rudimentary "spam filter"
This commit is contained in:
parent
0a8db68e42
commit
3e97458ee2
6 changed files with 52 additions and 15 deletions
|
@ -27,21 +27,7 @@ public function show(): View {
|
|||
* @throws ValidationException
|
||||
*/
|
||||
public function addEntry(Request $request): RedirectResponse {
|
||||
$this->validate($request, [
|
||||
'name' => 'required',
|
||||
'message' => 'required'
|
||||
]);
|
||||
|
||||
|
||||
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);
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -47,6 +47,7 @@ class Kernel extends HttpKernel
|
|||
|
||||
protected $routeMiddleware = [
|
||||
'rate_limit' => \App\Http\Middleware\RateLimiter::class,
|
||||
'validator' => \App\Http\Middleware\GuestbookValidate::class,
|
||||
];
|
||||
|
||||
|
||||
|
|
37
app/Http/Middleware/GuestbookValidate.php
Normal file
37
app/Http/Middleware/GuestbookValidate.php
Normal file
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class GuestbookValidate
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
|
||||
*/
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
if (
|
||||
!$request->validate([
|
||||
'name' => 'required',
|
||||
'message' => 'required'
|
||||
]) ||
|
||||
$this->containsUrl($request->get('message')) ||
|
||||
$this->containsUrl($request->get('name'))
|
||||
) {
|
||||
return response()->view('errors.guestbook-invalid', [], 400);
|
||||
}
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
public function containsUrl($str) {
|
||||
$matches = [];
|
||||
$pattern = '/\b(?:https?|ftp|www)(:\/\/)*[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i';
|
||||
preg_match_all($pattern, $str, $matches);
|
||||
return count($matches[0]);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue