From 268c6557a1174d24e1014e00d0c03d28fe6bae59 Mon Sep 17 00:00:00 2001 From: Julian Prieber <60265788+JulianPrieber@users.noreply.github.com> Date: Thu, 31 Mar 2022 22:43:01 +0200 Subject: [PATCH] Added check, if added links begin with "https" Added check added, or edited links begin with https. Previously if links were saved without this formatting applied an error exception would be thrown. For this I added two functions, one can check if a string ends with a certain string, the other checks if it ends with a certain string. The first function is used for the newly added check, the other one is planned to be used in a similar check in the future. --- app/Http/Controllers/UserController.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index bb3107a..53fac06 100755 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -12,6 +12,22 @@ use App\Models\User; use App\Models\Button; use App\Models\Link; + //Function tests if string starts with certain string (used to test for illegal strings) +function stringStartsWith($haystack,$needle,$case=true) { + if ($case){ + return strpos($haystack, $needle, 0) === 0; + } + return stripos($haystack, $needle, 0) === 0; +} + + //Function tests if string ends with certain string (used to test for illegal strings) +function stringEndsWith($haystack,$needle,$case=true) { + $expectedPosition = strlen($haystack) - strlen($needle); + if ($case){ + return strrpos($haystack, $needle, 0) === $expectedPosition; + } + return strripos($haystack, $needle, 0) === $expectedPosition; +} class UserController extends Controller { @@ -82,7 +98,10 @@ class UserController extends Controller 'button' => 'required' ]); + if (stringStartsWith($request->link,'http://') == 'true' or stringStartsWith($request->link,'https://') == 'true' or stringStartsWith($request->link,'mailto:') == 'true') $link = $request->link; + else + $link = 'https://' . $request->link; if ($request->title == '') $title = $request->button; else @@ -186,7 +205,10 @@ class UserController extends Controller 'button' => 'required', ]); + if (stringStartsWith($request->link,'http://') == 'true' or stringStartsWith($request->link,'https://') == 'true' or stringStartsWith($request->link,'mailto:') == 'true') $link = $request->link; + else + $link = 'https://' . $request->link; $title = $request->title; $order = $request->order; $button = $request->button;