Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
41.46% covered (danger)
41.46%
34 / 82
42.86% covered (danger)
42.86%
3 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
HttpUtils
41.46% covered (danger)
41.46%
34 / 82
42.86% covered (danger)
42.86%
3 / 7
173.22
0.00% covered (danger)
0.00%
0 / 1
 getBotRegexes
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
1
 isBot
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 countRequest
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
30
 stripParams
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
42
 dieWithHttpError
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 redirect
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
1
 validateGetParams
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
42
 sendHttpResponseCode
n/a
0 / 0
n/a
0 / 0
1
 sendHeader
n/a
0 / 0
n/a
0 / 0
1
 sendHttpBody
n/a
0 / 0
n/a
0 / 0
1
 exitExecution
n/a
0 / 0
n/a
0 / 0
1
1<?php
2
3namespace Olz\Utils;
4
5use Olz\Components\Error\OlzErrorPage\OlzErrorPage;
6use Olz\Components\Page\OlzFooter\OlzFooter;
7use Olz\Components\Page\OlzHeaderWithoutRouting\OlzHeaderWithoutRouting;
8use Olz\Entity\Counter;
9use PhpTypeScriptApi\PhpStan\PhpStanUtils;
10use PhpTypeScriptApi\PhpStan\ValidateVisitor;
11use Symfony\Component\HttpFoundation\Request;
12
13class HttpUtils {
14    use WithUtilsTrait;
15
16    /** @return array<string> */
17    public function getBotRegexes(): array {
18        return [
19            '/bingbot/i',
20            '/googlebot/i',
21            '/google/i',
22            '/facebookexternalhit/i',
23            '/applebot/i',
24            '/yandexbot/i',
25            '/ecosia/i',
26            '/phpservermon/i',
27            '/OlzSystemTest\//i',
28            '/bot\//i',
29            '/crawler\//i',
30        ];
31    }
32
33    public function isBot(string $user_agent): bool {
34        foreach ($this->getBotRegexes() as $regex) {
35            if (preg_match($regex, $user_agent)) {
36                return true;
37            }
38        }
39        return false;
40    }
41
42    /** @param array<string> $get_params */
43    public function countRequest(Request $request, array $get_params = []): void {
44        $user_agent = $this->server()['HTTP_USER_AGENT'] ?? '';
45        if ($this->isBot($user_agent)) {
46            $this->log()->debug("Counter: user agent is bot: {$user_agent}");
47            return;
48        }
49        $path = "{$request->getBasePath()}{$request->getPathInfo()}";
50        $query = [];
51        foreach ($get_params as $key) {
52            $value = $request->query->get($key);
53            if ($value !== null) {
54                $query[] = "{$key}={$value}";
55            }
56        }
57        $pretty_query = empty($query) ? '' : '?'.implode('&', $query);
58        $counter_repo = $this->entityManager()->getRepository(Counter::class);
59        $counter_repo->record("{$path}{$pretty_query}");
60        $this->log()->debug("Counter: Counted {$path}{$pretty_query} (user agent: {$user_agent})");
61    }
62
63    /** @param array<string> $get_params */
64    public function stripParams(Request $request, array $get_params = []): void {
65        $should_strip = false;
66        $query = [];
67        foreach ($request->query->all() as $key => $value) {
68            if (in_array($key, $get_params)) {
69                $should_strip = true;
70            } elseif (is_string($value)) {
71                $query[] = "{$key}={$value}";
72            }
73        }
74        if (!$should_strip) {
75            return;
76        }
77        $path = "{$request->getBasePath()}{$request->getPathInfo()}";
78        $pretty_query = empty($query) ? '' : '?'.implode('&', $query);
79        $this->redirect("{$path}{$pretty_query}", 308);
80    }
81
82    public function dieWithHttpError(int $http_status_code): void {
83        $this->sendHttpResponseCode($http_status_code);
84
85        $out = OlzErrorPage::render([
86            'http_status_code' => $http_status_code,
87        ]);
88
89        $this->sendHttpBody($out);
90        $this->exitExecution();
91    }
92
93    public function redirect(string $redirect_url, int $http_status_code = 301): void {
94        $this->sendHeader("Location: {$redirect_url}");
95        $this->sendHttpResponseCode($http_status_code);
96
97        $out = "";
98        $out .= OlzHeaderWithoutRouting::render([
99            'title' => "Weiterleitung...",
100        ]);
101
102        $enc_redirect_url = json_encode($redirect_url);
103        $out .= <<<ZZZZZZZZZZ
104            <div class='content-full'>
105                <h2>Automatische Weiterleitung...</h2>
106                <p>Falls die automatische Weiterleitung nicht funktionieren sollte, kannst du auch diesen Link anklicken:</p>
107                <p><b><a href='{$redirect_url}' class='linkint' id='redirect-link'>{$redirect_url}</a></b></p>
108                <script type='text/javascript'>
109                    window.setTimeout(function () {
110                        window.location.href = {$enc_redirect_url};
111                    }, 1000);
112                </script>
113            </div>
114            ZZZZZZZZZZ;
115
116        $out .= OlzFooter::render();
117        $this->sendHttpBody($out);
118        $this->exitExecution();
119    }
120
121    /**
122     * @template T of array
123     *
124     * @param class-string<HttpParams<T>>             $params_class
125     * @param ?array<string, ?(string|array<string>)> $get_params
126     * @param array{just_log?: bool}                  $options
127     *
128     * @return T
129     */
130    public function validateGetParams(string $params_class, ?array $get_params = null, array $options = []): array {
131        if ($get_params === null) {
132            $get_params = $this->getParams();
133        }
134        $class_info = new \ReflectionClass($params_class);
135        $utils = new PhpStanUtils();
136        $php_doc_node = $utils->parseDocComment(
137            $class_info->getDocComment(),
138            $class_info->getFileName() ?: null,
139        );
140        $type = $php_doc_node?->getExtendsTagValues()[0]->type->genericTypes[0];
141        $aliases = $utils->getAliases($php_doc_node);
142        if (!$type) {
143            $this->dieWithHttpError(400);
144            throw new \Exception('should already have failed');
145        }
146        $result = ValidateVisitor::validateDeserialize($utils, $get_params, $type, $aliases);
147        if (!$result->isValid() && ($options['just_log'] ?? false) === false) {
148            $this->dieWithHttpError(400);
149            throw new \Exception('should already have failed');
150        }
151        return $result->getValue();
152    }
153
154    // @codeCoverageIgnoreStart
155    // Reason: Mock functions for tests.
156
157    protected function sendHttpResponseCode(int $http_response_code): void {
158        http_response_code($http_response_code);
159    }
160
161    protected function sendHeader(string $http_header_line): void {
162        header($http_header_line);
163    }
164
165    protected function sendHttpBody(string $http_body): void {
166        echo $http_body;
167    }
168
169    protected function exitExecution(): void {
170        exit('');
171    }
172
173    // @codeCoverageIgnoreEnd
174}