Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.24% |
40 / 42 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Helper | |
95.24% |
40 / 42 |
|
50.00% |
2 / 4 |
10 | |
0.00% |
0 / 1 |
| proxySanitizeUri | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
| getFormatedDates | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| hashQueryParameters | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
3 | |||
| getContentForHash | |
94.12% |
16 / 17 |
|
0.00% |
0 / 1 |
4.00 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @copyright BerlinOnline Stadtportal GmbH & Co. KG |
| 5 | **/ |
| 6 | |
| 7 | namespace BO\Slim; |
| 8 | |
| 9 | class Helper |
| 10 | { |
| 11 | /** |
| 12 | * @param null|string|string[] $uri |
| 13 | * |
| 14 | * |
| 15 | * @return string|string[] |
| 16 | * |
| 17 | */ |
| 18 | public static function proxySanitizeUri(array|string|null $uri): array|string |
| 19 | { |
| 20 | if ($uri === null) { |
| 21 | return ''; |
| 22 | } |
| 23 | $uri = str_replace(':80/', '/', $uri); |
| 24 | return $uri; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * @param \DateTimeImmutable|int $timestamp |
| 29 | * |
| 30 | * @return false|string |
| 31 | */ |
| 32 | public static function getFormatedDates( |
| 33 | int|\DateTimeImmutable $timestamp, |
| 34 | string $pattern = 'MMMM', |
| 35 | string $locale = 'de_DE', |
| 36 | string $timezone = 'Europe/Berlin' |
| 37 | ): string|false { |
| 38 | $dateFormatter = new \IntlDateFormatter( |
| 39 | $locale, |
| 40 | \IntlDateFormatter::MEDIUM, |
| 41 | \IntlDateFormatter::MEDIUM, |
| 42 | $timezone, |
| 43 | \IntlDateFormatter::GREGORIAN, |
| 44 | $pattern |
| 45 | ); |
| 46 | return $dateFormatter->format($timestamp); |
| 47 | } |
| 48 | |
| 49 | |
| 50 | public static function hashQueryParameters( |
| 51 | string $section, |
| 52 | array $queryVariables, |
| 53 | array $parameters, |
| 54 | string $hashFunction = 'md5' |
| 55 | ): string { |
| 56 | $content = $section . self::getContentForHash($queryVariables, $parameters); |
| 57 | $hashString = $hashFunction($content . \App::$urlSignatureSecret); |
| 58 | $halfLength = (int) floor(strlen($hashString) / 2); |
| 59 | $firstHalf = substr($hashString, 0, $halfLength); |
| 60 | $secondHalf = substr($hashString, strlen($firstHalf)); |
| 61 | $alphabet = '0123456789' . implode(range('A', 'Z')) . implode(range('a', 'z')); |
| 62 | $rotation = 31; |
| 63 | // reducing the hash to half its length by combining first half and second half |
| 64 | for ($i = 0; $i < strlen($firstHalf); $i++) { |
| 65 | $position = strpos($alphabet, $firstHalf[$i]); |
| 66 | $rotation = (($position === false ? 0 : $position) + ord($secondHalf[$i]) + $rotation) % strlen($alphabet); |
| 67 | $firstHalf[$i] = $alphabet[$rotation]; |
| 68 | } |
| 69 | return $firstHalf; |
| 70 | } |
| 71 | |
| 72 | protected static function getContentForHash(array $queryVariables, array $parameters): string |
| 73 | { |
| 74 | $content = ''; |
| 75 | foreach ($parameters as $parameter) { |
| 76 | if (isset($queryVariables[$parameter])) { |
| 77 | if (is_array($queryVariables[$parameter])) { |
| 78 | $parameterArray = $queryVariables[$parameter]; |
| 79 | ksort($parameterArray); |
| 80 | $flat = []; |
| 81 | array_walk_recursive( |
| 82 | $parameterArray, |
| 83 | function (mixed $value) use (&$flat) { |
| 84 | $flat[] = strval($value); |
| 85 | } |
| 86 | ); |
| 87 | $content .= implode('', $flat); |
| 88 | } else { |
| 89 | $content .= (string) $queryVariables[$parameter]; |
| 90 | } |
| 91 | } else { |
| 92 | $content .= 'NULL'; |
| 93 | } |
| 94 | } |
| 95 | return $content; |
| 96 | } |
| 97 | } |