Passed
Push — master ( ebf522...44a309 )
by Mihail
04:57
created

Request::processBalancerProxies()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 5
nc 3
nop 0
dl 0
loc 8
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace Ffcms\Core\Network;
4
5
use Ffcms\Core\App;
6
use Ffcms\Core\Helper\Type\Str;
7
use Ffcms\Templex\Url\UrlRepository;
0 ignored issues
show
Bug introduced by
The type Ffcms\Templex\Url\UrlRepository was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Symfony\Component\HttpFoundation\Request as FoundationRequest;
9
10
/**
11
 * Class Request. Classic implementation of httpfoundation.request with smooth additions and changes which allow
12
 * working as well as in ffcms.
13
 * @package Ffcms\Core\Network
14
 */
15
class Request extends FoundationRequest
16
{
17
    use Request\MvcFeatures, Request\MultiLanguageFeatures, Request\RouteMapFeatures;
18
19
    /**
20
     * Request constructor.
21
     * @inheritdoc
22
     * @param array $query
23
     * @param array $request
24
     * @param array $attributes
25
     * @param array $cookies
26
     * @param array $files
27
     * @param array $server
28
     * @param null $content
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $content is correct as it would always require null to be passed?
Loading history...
29
     */
30
    public function __construct(array $query = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], $content = null)
31
    {
32
        parent::__construct($query, $request, $attributes, $cookies, $files, $server, $content);
33
        $this->searchRedirect();
34
        $this->runMultiLanguage();
35
        $this->processBalancerProxies();
36
        $this->loadTrustedProxies();
37
        $this->runRouteBinding();
38
        $this->setTemplexFeatures();
39
    }
40
41
    /**
42
     * Sets the parameters for this request.
43
     *
44
     * This method also re-initializes all properties.
45
     *
46
     * @param array $query The GET parameters
47
     * @param array $request The POST parameters
48
     * @param array $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
49
     * @param array $cookies The COOKIE parameters
50
     * @param array $files The FILES parameters
51
     * @param array $server The SERVER parameters
52
     * @param string $content The raw body data
53
     *
54
     * @api
55
     */
56
    public function initialize(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null)
57
    {
58
        parent::initialize($query, $request, $attributes, $cookies, $files, $server, $content);
59
60
        $basePath = trim(App::$Properties->get('basePath'), '/');
0 ignored issues
show
Bug introduced by
It seems like Ffcms\Core\App::Properties->get('basePath') can also be of type false; however, parameter $str of trim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
        $basePath = trim(/** @scrutinizer ignore-type */ App::$Properties->get('basePath'), '/');
Loading history...
61
        if ($basePath !== null && Str::length($basePath) > 0) {
62
            $basePath = '/' . $basePath;
63
        }
64
65
        if (!defined('env_no_uri') || env_no_uri === false) {
0 ignored issues
show
Bug introduced by
The constant Ffcms\Core\Network\env_no_uri was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
66
            $basePath .= '/' . strtolower(env_name);
0 ignored issues
show
Bug introduced by
The constant Ffcms\Core\Network\env_name was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
67
        }
68
69
        // we never try to use path's without friendly url's
70
        $this->basePath = $this->baseUrl = $basePath;
71
    }
72
73
    private function processBalancerProxies()
74
    {
75
        // fix cloudflare forwarding
76
        $protos = $this->headers->get('cf-visitor', false);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type null|string expected by parameter $default of Symfony\Component\HttpFoundation\HeaderBag::get(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
        $protos = $this->headers->get('cf-visitor', /** @scrutinizer ignore-type */ false);
Loading history...
77
        if ($protos) {
78
            $data = json_decode($protos);
79
            if ($data && $data->scheme && (string)$data->scheme === 'https') {
80
                $this->server->set('HTTPS', 'on');
81
            }
82
        }
83
    }
84
85
    /**
86
     * Set trusted proxies from configs
87
     */
88
    private function loadTrustedProxies()
89
    {
90
        $proxies = App::$Properties->get('trustedProxy');
91
        if (!$proxies || Str::likeEmpty($proxies)) {
92
            return;
93
        }
94
95
        $pList = explode(',', $proxies);
96
        $resultList = [];
97
        foreach ($pList as $proxy) {
98
            $resultList[] = trim($proxy);
99
        }
100
        self::setTrustedProxies($resultList, self::HEADER_X_FORWARDED_ALL);
101
    }
102
103
    /**
104
     * Get pathway as string
105
     * @return string
106
     */
107
    public function getPathInfo()
108
    {
109
        $route = $this->languageInPath ? Str::sub(parent::getPathInfo(), Str::length($this->language) + 1) : parent::getPathInfo();
110
        if (!Str::startsWith('/', $route)) {
111
            $route = '/' . $route;
112
        }
113
        return $route;
114
    }
115
116
    /**
117
     * Get pathway without current controller/action path
118
     * @return string
119
     */
120
    public function getPathWithoutControllerAction(): ?string
121
    {
122
        $path = trim($this->getPathInfo(), '/');
123
        if ($this->aliasPathTarget !== null) {
124
            $path = trim($this->aliasPathTarget, '/');
125
        }
126
127
        $pathArray = explode('/', $path);
128
        if ($pathArray[0] === Str::lowerCase($this->getController())) {
129
            // unset controller
130
            array_shift($pathArray);
131
            if ($pathArray[0] === Str::lowerCase($this->getAction())) {
132
                // unset action
133
                array_shift($pathArray);
134
            }
135
        }
136
        return trim(implode('/', $pathArray), '/');
137
    }
138
139
    /**
140
     * Get current full request URI
141
     * @return string
142
     */
143
    public function getFullUrl(): string
144
    {
145
        return $this->getSchemeAndHttpHost() . $this->getRequestUri();
146
    }
147
148
    /**
149
     * Get base path from current environment without basePath of subdirectories
150
     * @return string
151
     */
152
    public function getInterfaceSlug(): string
153
    {
154
        $path = $this->getBasePath();
155
        $subDir = App::$Properties->get('basePath');
156
        if ($subDir !== '/') {
157
            $offset = (int)Str::length($subDir);
0 ignored issues
show
Bug introduced by
It seems like $subDir can also be of type false; however, parameter $string of Ffcms\Core\Helper\Type\Str::length() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

157
            $offset = (int)Str::length(/** @scrutinizer ignore-type */ $subDir);
Loading history...
158
            $path = Str::sub($path, --$offset);
159
        }
160
        return $path;
161
    }
162
163
    /**
164
     * Set templex template engine URL features
165
     * @return void
166
     */
167
    private function setTemplexFeatures(): void
168
    {
169
        $url = $this->getSchemeAndHttpHost();
170
        $sub = null;
171
        if ($this->getInterfaceSlug() && Str::length($this->getInterfaceSlug()) > 0) {
172
            $sub = $this->getInterfaceSlug() . '/';
173
        }
174
175
        if ($this->languageInPath()) {
176
            $sub .= $this->getLanguage();
177
        }
178
179
        UrlRepository::factory()->setUrlAndSubdir($url, $sub);
180
    }
181
}
182