1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Middleware; |
6
|
|
|
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
9
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
10
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
11
|
|
|
use Yiisoft\Aliases\Aliases; |
12
|
|
|
use Yiisoft\Router\UrlGeneratorInterface; |
13
|
|
|
|
14
|
|
|
use function basename; |
15
|
|
|
use function dirname; |
16
|
|
|
use function rtrim; |
17
|
|
|
use function str_replace; |
18
|
|
|
use function str_starts_with; |
19
|
|
|
use function strlen; |
20
|
|
|
use function strpos; |
21
|
|
|
use function substr; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* This middleware supports routing when webroot is not the same folder as public. |
25
|
|
|
*/ |
26
|
|
|
final class SubFolder implements MiddlewareInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @param UrlGeneratorInterface $uriGenerator The URI generator instance. |
30
|
|
|
* @param Aliases $aliases The aliases instance. |
31
|
12 |
|
* @param string $baseUrlAlias The base url alias {@see Aliases::get()}. Default "@baseUrl". |
32
|
|
|
*/ |
33
|
|
|
public function __construct( |
34
|
|
|
private UrlGeneratorInterface $uriGenerator, |
35
|
|
|
private Aliases $aliases, |
36
|
|
|
private ?string $prefix = null, |
37
|
12 |
|
private ?string $baseUrlAlias = '@baseUrl', |
38
|
|
|
) { |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritDoc |
43
|
|
|
*/ |
44
|
12 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
45
|
|
|
{ |
46
|
12 |
|
$uri = $request->getUri(); |
47
|
12 |
|
$path = $uri->getPath(); |
48
|
12 |
|
$baseUrl = $this->prefix ?? $this->getBaseUrl($request); |
49
|
12 |
|
$length = strlen($baseUrl); |
50
|
|
|
|
51
|
12 |
|
if ($length > 0 && str_starts_with($path, $baseUrl)) { |
52
|
|
|
$newPath = substr($path, $length); |
53
|
12 |
|
|
54
|
|
|
if ($newPath === '') { |
55
|
|
|
$newPath = '/'; |
56
|
7 |
|
} |
57
|
|
|
if ($newPath[0] === '/') { |
58
|
7 |
|
$request = $request->withUri($uri->withPath($newPath)); |
59
|
7 |
|
} |
60
|
7 |
|
} |
61
|
|
|
|
62
|
7 |
|
if ($length !== 0) { |
63
|
6 |
|
$this->uriGenerator->setUriPrefix($baseUrl); |
64
|
7 |
|
if ($this->baseUrlAlias !== null) { |
65
|
|
|
$this->aliases->set($this->baseUrlAlias, $baseUrl); |
66
|
|
|
} |
67
|
5 |
|
} |
68
|
|
|
|
69
|
5 |
|
return $handler->handle($request); |
70
|
1 |
|
} |
71
|
|
|
|
72
|
|
|
public function getBaseUrl(ServerRequestInterface $request): string |
73
|
4 |
|
{ |
74
|
2 |
|
$serverParams = $request->getServerParams(); |
75
|
|
|
$scriptUrl = $serverParams['SCRIPT_FILENAME']; |
76
|
|
|
$scriptName = basename($scriptUrl); |
77
|
|
|
|
78
|
9 |
|
if (isset($serverParams['PHP_SELF']) && basename($serverParams['PHP_SELF']) === $scriptName) { |
79
|
7 |
|
$scriptUrl = $serverParams['PHP_SELF']; |
80
|
|
|
} elseif ( |
81
|
7 |
|
isset($serverParams['ORIG_SCRIPT_NAME']) && |
82
|
1 |
|
basename($serverParams['ORIG_SCRIPT_NAME']) === $scriptName |
83
|
|
|
) { |
84
|
|
|
$scriptUrl = $serverParams['ORIG_SCRIPT_NAME']; |
85
|
7 |
|
} elseif ( |
86
|
2 |
|
isset($serverParams['PHP_SELF']) && |
87
|
2 |
|
($pos = strpos($serverParams['PHP_SELF'], '/' . $scriptName)) !== false |
88
|
|
|
) { |
89
|
|
|
$scriptUrl = substr($serverParams['PHP_SELF'], 0, $pos) . '/' . $scriptName; |
90
|
5 |
|
} elseif ( |
91
|
|
|
!empty($serverParams['DOCUMENT_ROOT']) && |
92
|
5 |
|
str_starts_with($scriptUrl, $serverParams['DOCUMENT_ROOT']) |
93
|
|
|
) { |
94
|
5 |
|
$scriptUrl = str_replace([$serverParams['DOCUMENT_ROOT'], '\\'], ['', '/'], $scriptUrl); |
95
|
5 |
|
} |
96
|
|
|
|
97
|
|
|
return rtrim(dirname($scriptUrl), '\\/'); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|