Issues (1358)

modules/Psr7/Request.php (2 issues)

1
<?php
2
/**
3
 * @package    Psr7
4
 * @category   modules
5
 * @author     Nazar Mokrynskyi <[email protected]>
6
 * @license    0BSD
7
 */
8
namespace cs\modules\Psr7;
9
use
10
	cs\Request as System_request;
11
12
class Request {
13
	/**
14
	 * Initialize request from PSR-7 request object
15
	 *
16
	 * @param \Psr\Http\Message\ServerRequestInterface $Psr7_request
0 ignored issues
show
The type Psr\Http\Message\ServerRequestInterface 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...
17
	 *
18
	 * @throws \cs\ExitException
19
	 */
20
	public static function init_from_psr7 ($Psr7_request) {
21
		++System_request::$id;
22
		$System_request = System_request::instance();
23
		self::from_psr7_server($System_request, $Psr7_request);
24
		self::from_psr7_query($System_request, $Psr7_request);
25
		self::from_psr7_data_and_files($System_request, $Psr7_request);
26
		$System_request->init_route();
27
		$System_request->started = microtime(true);
28
	}
29
	/**
30
	 * @param System_request                           $System_request
31
	 * @param \Psr\Http\Message\ServerRequestInterface $Psr7_request
32
	 */
33
	protected static function from_psr7_server ($System_request, $Psr7_request) {
34
		$uri                    = $Psr7_request->getUri();
35
		$System_request->method = $Psr7_request->getMethod();
36
		$System_request->host   = $uri->getHost();
37
		$System_request->scheme = $uri->getScheme();
38
		$System_request->secure = $System_request->scheme == 'https';
39
		if (
40
			(!$System_request->secure && $uri->getPort() != 80) ||
41
			($System_request->secure && $uri->getPort() != 443)
42
		) {
43
			$System_request->host .= ':'.$uri->getPort();
44
		}
45
		$System_request->protocol     = 'HTTP/'.$Psr7_request->getProtocolVersion();
46
		$System_request->path         = $uri->getPath();
47
		$System_request->query_string = $uri->getQuery();
48
		/** @noinspection NestedTernaryOperatorInspection */
49
		$System_request->uri         = $System_request->path.($System_request->query_string ? "?$System_request->query_string" : '') ?: '/';
50
		$System_request->remote_addr = $Psr7_request->getServerParams()['REMOTE_ADDR'] ?? '127.0.0.1';
51
		$System_request->ip          = self::ip($System_request, $Psr7_request);
52
	}
53
	/**
54
	 * The best guessed IP of client (based on all known headers), `127.0.0.1` by default
55
	 *
56
	 * @param System_request                           $System_request
57
	 * @param \Psr\Http\Message\ServerRequestInterface $Psr7_request
58
	 *
59
	 * @return string
60
	 */
61
	protected static function ip ($System_request, $Psr7_request) {
62
		$potential_addresses = [
63
			$Psr7_request->getHeaderLine('x-forwarded-for'),
64
			$Psr7_request->getHeaderLine('client-ip'),
65
			$Psr7_request->getHeaderLine('x-forwarded'),
66
			$Psr7_request->getHeaderLine('x-cluster-client-ip'),
67
			$Psr7_request->getHeaderLine('forwarded-for'),
68
			$Psr7_request->getHeaderLine('forwarded')
69
		];
70
		foreach ($potential_addresses as $ip) {
71
			$ip = trim(explode(',', $ip)[0]);
72
			if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
73
				return $ip;
74
			}
75
		}
76
		return $System_request->remote_addr;
77
	}
78
	/**
79
	 * @param System_request                           $System_request
80
	 * @param \Psr\Http\Message\ServerRequestInterface $Psr7_request
81
	 */
82
	protected static function from_psr7_query ($System_request, $Psr7_request) {
83
		$System_request->query = $Psr7_request->getQueryParams();
84
	}
85
	/**
86
	 * @param System_request                           $System_request
87
	 * @param \Psr\Http\Message\ServerRequestInterface $Psr7_request
88
	 *
89
	 * @throws \cs\ExitException
90
	 */
91
	protected static function from_psr7_data_and_files ($System_request, $Psr7_request) {
92
		Psr7_data_stream::$stream = $Psr7_request->getBody();
93
		$System_request->init_data_and_files([], [], fopen('request-psr7-data://', 'r'));
0 ignored issues
show
It seems like fopen('request-psr7-data://', 'r') can also be of type false; however, parameter $data_stream of cs\Request::init_data_and_files() does only seem to accept resource|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

93
		$System_request->init_data_and_files([], [], /** @scrutinizer ignore-type */ fopen('request-psr7-data://', 'r'));
Loading history...
94
	}
95
}
96