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.
This commit is contained in:
Julian Prieber 2022-03-31 22:43:01 +02:00
parent 518d82125e
commit 268c6557a1
1 changed files with 22 additions and 0 deletions

View File

@ -12,6 +12,22 @@ use App\Models\User;
use App\Models\Button; use App\Models\Button;
use App\Models\Link; 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 class UserController extends Controller
{ {
@ -82,7 +98,10 @@ class UserController extends Controller
'button' => 'required' 'button' => 'required'
]); ]);
if (stringStartsWith($request->link,'http://') == 'true' or stringStartsWith($request->link,'https://') == 'true' or stringStartsWith($request->link,'mailto:') == 'true')
$link = $request->link; $link = $request->link;
else
$link = 'https://' . $request->link;
if ($request->title == '') if ($request->title == '')
$title = $request->button; $title = $request->button;
else else
@ -186,7 +205,10 @@ class UserController extends Controller
'button' => 'required', 'button' => 'required',
]); ]);
if (stringStartsWith($request->link,'http://') == 'true' or stringStartsWith($request->link,'https://') == 'true' or stringStartsWith($request->link,'mailto:') == 'true')
$link = $request->link; $link = $request->link;
else
$link = 'https://' . $request->link;
$title = $request->title; $title = $request->title;
$order = $request->order; $order = $request->order;
$button = $request->button; $button = $request->button;