-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUriFactory.php
More file actions
182 lines (157 loc) · 5.82 KB
/
UriFactory.php
File metadata and controls
182 lines (157 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 5.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Http;
use Cake\Core\Configure;
use Laminas\Diactoros\Uri;
use Laminas\Diactoros\UriFactory as DiactorosUriFactory;
use Psr\Http\Message\UriFactoryInterface;
use Psr\Http\Message\UriInterface;
use function Laminas\Diactoros\marshalHeadersFromSapi;
/**
* Factory class for creating uri instances.
*/
class UriFactory implements UriFactoryInterface
{
/**
* Create a new URI.
*
* @param string $uri The URI to parse.
* @throws \InvalidArgumentException If the given URI cannot be parsed.
*/
public function createUri(string $uri = ''): UriInterface
{
return new Uri($uri);
}
/**
* Get a new Uri instance and base info from the provided server data.
*
* @param array|null $server Array of server data to build the Uri from.
* $_SERVER will be used if $server parameter is null.
* @return array
* @phpstan-return array{uri: \Psr\Http\Message\UriInterface, base: string, webroot: string}
*/
public static function marshalUriAndBaseFromSapi(?array $server = null): array
{
$server ??= $_SERVER;
$headers = marshalHeadersFromSapi($server);
$uri = DiactorosUriFactory::createFromSapi($server, $headers);
['base' => $base, 'webroot' => $webroot] = static::getBase($uri, $server);
$uri = static::updatePath($base, $uri);
if (!$uri->getHost()) {
$uri = $uri->withHost('localhost');
}
return ['uri' => $uri, 'base' => $base, 'webroot' => $webroot];
}
/**
* Updates the request URI to remove the base directory.
*
* @param string $base The base path to remove.
* @param \Psr\Http\Message\UriInterface $uri The uri to update.
* @return \Psr\Http\Message\UriInterface
*/
protected static function updatePath(string $base, UriInterface $uri): UriInterface
{
$path = $uri->getPath();
if ($base !== '' && str_starts_with($path, $base)) {
$path = substr($path, strlen($base));
}
// App.baseUrl is meant to be set only when URL rewriting is not used.
if (!Configure::read('App.baseUrl')) {
if ($path === '' || $path === '//') {
$path = '/';
}
return $uri->withPath($path);
}
if ($path === '/index.php' && $uri->getQuery()) {
$path = $uri->getQuery();
}
if (in_array($path, ['', '//', '/index.php'], true)) {
$path = '/';
}
// Check for $webroot/index.php at the start and end of the path.
$search = '';
if (str_starts_with($path, '/')) {
$search .= '/';
}
$search .= (Configure::read('App.webroot') ?: 'webroot') . '/index.php';
if (str_starts_with($path, $search)) {
$path = substr($path, strlen($search));
} elseif (str_ends_with($path, $search)) {
$path = '/';
}
if (!$path) {
$path = '/';
}
return $uri->withPath($path);
}
/**
* Calculate the base directory and webroot directory.
*
* @param \Psr\Http\Message\UriInterface $uri The Uri instance.
* @param array $server The SERVER data to use.
* @return array An array containing the base and webroot paths.
* @phpstan-return array{base: string, webroot: string}
*/
protected static function getBase(UriInterface $uri, array $server): array
{
$config = (array)Configure::read('App') + [
'base' => null,
'webroot' => null,
'baseUrl' => null,
];
$base = $config['base'];
$baseUrl = $config['baseUrl'];
$webroot = (string)$config['webroot'];
if ($base !== false && $base !== null) {
return ['base' => $base, 'webroot' => $base . '/'];
}
if (!$baseUrl) {
$phpSelf = $server['PHP_SELF'] ?? null;
if ($phpSelf === null) {
return ['base' => '', 'webroot' => '/'];
}
$base = dirname($server['PHP_SELF'] ?? DIRECTORY_SEPARATOR);
// Clean up additional / which cause following code to fail..
$base = (string)preg_replace('#/+#', '/', $base);
$indexPos = strpos($base, '/index.php');
if ($indexPos !== false) {
$base = substr($base, 0, $indexPos);
}
if ($webroot === basename($base)) {
$base = dirname($base);
}
if ($base === DIRECTORY_SEPARATOR || $base === '.') {
$base = '';
}
$base = implode('/', array_map('rawurlencode', explode('/', $base)));
return ['base' => $base, 'webroot' => $base . '/'];
}
$file = '/' . basename($baseUrl);
$base = dirname($baseUrl);
if ($base === DIRECTORY_SEPARATOR || $base === '.') {
$base = '';
}
$webrootDir = $base . '/';
$docRoot = $server['DOCUMENT_ROOT'] ?? '';
if (
($base || !str_contains($docRoot, $webroot))
&& !str_contains($webrootDir, '/' . $webroot . '/')
) {
$webrootDir .= $webroot . '/';
}
return ['base' => $base . $file, 'webroot' => $webrootDir];
}
}