Compare commits

...

12 Commits

3 changed files with 80 additions and 28 deletions

View File

@ -163,21 +163,19 @@ final class MD_STD {
*/ */
public static function startsWithAny(string $haystack, array $needles):bool { public static function startsWithAny(string $haystack, array $needles):bool {
$output = false;
foreach ($needles as $needle) { foreach ($needles as $needle) {
$output = \str_starts_with($haystack, $needle); if (\str_starts_with($haystack, $needle) === true) {
if ($output == true) { return true;
return $output;
} }
} }
return $output; return false;
} }
/** /**
* Function checking if a string contains another in a case-insensitive way. * Function checking if a string contains another in a case-insensitive way.
* *
* @param non-empty-string $haystack String to check. * @param string $haystack String to check.
* @param non-empty-string $needle Potential start of $haystack. * @param non-empty-string $needle Potential start of $haystack.
* *
* @return boolean * @return boolean
@ -234,7 +232,7 @@ final class MD_STD {
* *
* @param array<mixed> $value The value being encoded. Can be any type except a resource. * @param array<mixed> $value The value being encoded. Can be any type except a resource.
* @param integer $options Bitmask consisting of JSON_FORCE_OBJECT, JSON_HEX_QUOT ... * @param integer $options Bitmask consisting of JSON_FORCE_OBJECT, JSON_HEX_QUOT ...
* @param integer $depth Depth of coding. * @param positive-int $depth Depth of coding.
* *
* @return string * @return string
*/ */
@ -255,7 +253,7 @@ final class MD_STD {
* *
* @param object $value The value being encoded. Can be any type except a resource. * @param object $value The value being encoded. Can be any type except a resource.
* @param integer $options Bitmask consisting of JSON_FORCE_OBJECT, JSON_HEX_QUOT ... * @param integer $options Bitmask consisting of JSON_FORCE_OBJECT, JSON_HEX_QUOT ...
* @param integer $depth Depth of coding. * @param positive-int $depth Depth of coding.
* *
* @return string * @return string
*/ */
@ -298,14 +296,18 @@ final class MD_STD {
$curl = \curl_init(); $curl = \curl_init();
\curl_setopt($curl, CURLOPT_URL, $url); \curl_setopt_array($curl, [
\curl_setopt($curl, CURLOPT_HEADER, false); CURLOPT_URL => $url,
\curl_setopt($curl, CURLOPT_CONNECTTIMEOUT_MS, $timeout); //timeout in seconds CURLOPT_HEADER => false,
\curl_setopt($curl, CURLOPT_TIMEOUT_MS, $timeout); //timeout in seconds CURLOPT_CONNECTTIMEOUT_MS => $timeout, //timeout in seconds
\curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); CURLOPT_TIMEOUT_MS => $timeout, //timeout in seconds
// \curl_setopt($curl, CURLOPT_COOKIESESSION, true); CURLOPT_FOLLOWLOCATION => true,
\curl_setopt($curl, CURLOPT_AUTOREFERER, true); // CURLOPT_COOKIESESSION => true,
\curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2'); CURLOPT_AUTOREFERER => true,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2',
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2_0,
CURLOPT_TCP_FASTOPEN => true,
]);
/* /*
if (!file_exists(__DIR__ . '/../../curled.txt')) { if (!file_exists(__DIR__ . '/../../curled.txt')) {
@ -773,7 +775,7 @@ final class MD_STD {
* @param array<int, int> $input Input array. * @param array<int, int> $input Input array.
* @param positive-int $size Size of each part of the return set. * @param positive-int $size Size of each part of the return set.
* *
* @return array<non-empty-array<integer>> * @return array<non-empty-array<int, integer>>
*/ */
public static function split_int_array_into_sized_parts(array $input, int $size):array { public static function split_int_array_into_sized_parts(array $input, int $size):array {
@ -788,7 +790,36 @@ final class MD_STD {
while ($offset < $max) { while ($offset < $max) {
$cur = array_slice($input, $offset, $size - 1); $cur = array_slice($input, $offset, $size - 1);
if (!empty($cur)) $output[] = $cur; if (!empty($cur)) $output[] = $cur;
$offset += $size; $offset += $size - 1;
}
return $output;
}
/**
* Splits an lists of strings into parts of a predefined size and returns those
* as sub-lists of a superordinate list.
*
* @param array<int, string> $input Input array.
* @param positive-int $size Size of each part of the return set.
*
* @return array<non-empty-array<int, string>>
*/
public static function split_string_array_into_sized_parts(array $input, int $size):array {
/*
if ($size < 1) throw new Exception("Size of the target array must be a positive integer");
*/
$output = [];
$max = count($input);
$offset = 0;
while ($offset < $max) {
$cur = array_slice($input, $offset, $size - 1);
if (!empty($cur)) $output[] = $cur;
$offset += $size - 1;
} }
return $output; return $output;

View File

@ -72,7 +72,7 @@ final class MD_STD_CACHE {
ob_start(); ob_start();
if (($redisResult = $redis->get($redisKey))) { if ($redisResult = $redis->get($redisKey)) {
if (strlen($redisResult) > 3 and strpos($redisResult, ' id="errorPage"') === false) { if (strlen($redisResult) > 3 and strpos($redisResult, ' id="errorPage"') === false) {
$redis->close(); $redis->close();

View File

@ -73,13 +73,12 @@ final class MD_STD_IN {
*/ */
public static function sanitize_text(mixed $input):string { public static function sanitize_text(mixed $input):string {
$output = \filter_var($input, $output = \filter_var($input, FILTER_UNSAFE_RAW);
FILTER_SANITIZE_STRING,
FILTER_FLAG_NO_ENCODE_QUOTES);
if ($output === false) { if ($output === false) {
return ""; return "";
} }
$output = strip_tags($output);
while (strpos($output, " ") !== false) { while (strpos($output, " ") !== false) {
$output = str_replace(" ", " ", $output); $output = str_replace(" ", " ", $output);
} }
@ -97,9 +96,7 @@ final class MD_STD_IN {
*/ */
public static function sanitize_rgb_color(mixed $input):string { public static function sanitize_rgb_color(mixed $input):string {
$output = \filter_var($input, $output = \filter_var($input, FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH);
FILTER_SANITIZE_STRING,
FILTER_FLAG_NO_ENCODE_QUOTES);
if ($output === false if ($output === false
|| (preg_match('/^[a-zA-Z0-9]{3}$/', $output) === false && preg_match('/^[a-zA-Z0-9]{6}$/', $output) === false) || (preg_match('/^[a-zA-Z0-9]{3}$/', $output) === false && preg_match('/^[a-zA-Z0-9]{6}$/', $output) === false)
@ -231,6 +228,30 @@ final class MD_STD_IN {
} }
/**
* Validates a password (minimum requirements: 8 characters, including
* one number and one special char) and returns a list of errors,
* if there are any.
*
* @param string $input Input string.
*
* @return array<string>
*/
public static function validate_password(string $input):array {
$errors = [];
if (mb_strlen($input) < 8) {
$errors[] = 'password_too_short';
}
if (!(\preg_match('@[0-9]@', $input)) && !(\preg_match('@[^\w]@', $input))) {
$errors[] = 'password_has_no_number_no_special_char';
}
return $errors;
}
/** /**
* Sanitizes and validates a phone number. An empty string passes. * Sanitizes and validates a phone number. An empty string passes.
* *
@ -295,7 +316,7 @@ final class MD_STD_IN {
} }
// ISBN 13 // ISBN 13
if (\mb_strlen($input) === 10) { if (\mb_strlen($input) === 13) {
if (\preg_match('/\d{13}/i', $input)) { if (\preg_match('/\d{13}/i', $input)) {
return $input; return $input;