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