@@ -205,22 +205,62 @@ public static function getPathInfo(?string $requestUri = null, ?string $scriptNa
205
205
return '/ ' . ltrim ($ pathInfo , '/ ' );
206
206
}
207
207
208
+ private static function buildStringFromParts (array $ uriParts ): string
209
+ {
210
+ $ join [] = $ scheme = $ uriParts ['scheme ' ] ?? 'http ' ;
211
+ $ join [] = ':// ' ;
212
+ if (isset ($ uriParts ['user ' ])) {
213
+ $ join [] = $ uriParts ['user ' ];
214
+ if (isset ($ uriParts ['pass ' ])) {
215
+ $ join [] = ": {$ uriParts ['pass ' ]}" ;
216
+ }
217
+ $ join [] = '@ ' ;
218
+ }
219
+ $ join [] = $ uriParts ['host ' ] ?? 'localhost ' ;
220
+ if (isset ($ uriParts ['port ' ])) {
221
+ $ port = $ uriParts ['port ' ];
222
+ if (
223
+ $ scheme == 'http ' && $ port != 80 ||
224
+ $ scheme == 'https ' && $ port != 443
225
+ ) {
226
+ $ join [] = ": {$ port }" ;
227
+ }
228
+ }
229
+ $ join [] = '/ ' ;
230
+ if (isset ($ uriParts ['path ' ])) {
231
+ $ join [] = ltrim ($ uriParts ['path ' ], '/ ' );
232
+ }
233
+ if (isset ($ uriParts ['query ' ])) {
234
+ $ join [] = "? {$ uriParts ['query ' ]}" ;
235
+ }
236
+ if (isset ($ uriParts ['fragment ' ])) {
237
+ $ join [] .= "# {$ uriParts ['fragment ' ]}" ;
238
+ }
239
+ return join ('' , $ join );
240
+ }
241
+
208
242
public static function getSiteUrl (?string $ path = null , array $ sapiVars = [], bool $ cached = false )
209
243
{
210
244
$ sapiVars = array_merge ($ _SERVER , $ sapiVars );
211
245
$ sapiVars ['REQUEST_URI ' ] = $ sapiVars ['SCRIPT_NAME ' ] ?? '' ;
212
246
$ uri = static ::getCurrentString ($ sapiVars , $ cached );
213
- if (isset ($ path )) {
214
- $ uri = rtrim ($ uri , '/ ' ) . '/ ' . ltrim ($ path , '/ ' );
247
+ $ uriParts = parse_url ($ uri );
248
+ if (isset ($ sapiVars ['X_FORWARDED_PREFIX ' ])) {
249
+ $ uriParts ['path ' ] =
250
+ '/ ' . ltrim ($ sapiVars ['X_FORWARDED_PREFIX ' ], '/ ' ) .
251
+ $ uriParts ['path ' ];
215
252
}
216
- return $ uri ;
253
+ if (!empty ($ path )) {
254
+ $ uriParts ['path ' ] = ($ uriParts ['path ' ] ?? '' ) . '/ ' . ltrim ($ path , '/ ' );
255
+ }
256
+ return static ::buildStringFromParts ($ uriParts );
217
257
}
218
258
219
259
public static function getBaseUrl (?string $ path = null , array $ sapiVars = [], bool $ cached = false )
220
260
{
221
261
$ sapiVars = array_merge ($ _SERVER , $ sapiVars );
222
262
$ scriptName = $ sapiVars ['SCRIPT_NAME ' ] ?? '' ;
223
- $ sapiVars ['SCRIPT_NAME ' ] = substr_replace ( $ scriptName , '' , strrpos ( $ scriptName , '/ ' ) );
263
+ $ sapiVars ['SCRIPT_NAME ' ] = ' / ' . ltrim ( strtr ( dirname ( $ scriptName) , '\\ ' , ' / ' ) , '/ ' );
224
264
return static ::getSiteUrl ($ path , $ sapiVars , $ cached );
225
265
}
226
266
@@ -233,25 +273,18 @@ public static function getCurrentString(array $sapiVars = [], bool $cached = fal
233
273
234
274
$ sapiVars = array_merge ($ _SERVER , $ sapiVars );
235
275
236
- $ scheme = 'http ' ;
237
- $ defaultPort = 80 ;
238
- if (! empty ($ sapiVars ['HTTPS ' ]) && $ sapiVars ['HTTPS ' ] != 'off ' ) {
239
- $ scheme = 'https ' ;
240
- $ defaultPort = 443 ;
276
+ $ uriParts [ ' scheme ' ] = 'http ' ;
277
+ $ uriParts [ ' port ' ] = 80 ;
278
+ if (isset ($ sapiVars ['HTTPS ' ]) && $ sapiVars ['HTTPS ' ] != 'off ' ) {
279
+ $ uriParts [ ' scheme ' ] = 'https ' ;
280
+ $ uriParts [ ' port ' ] = 443 ;
241
281
}
242
-
243
- $ host = $ sapiVars ['HTTP_HOST ' ] ?? null ;
244
- if (!isset ($ host )) {
245
- $ host = $ sapiVars ['SERVER_NAME ' ];
246
- // Detecting port and comparing with default
247
- if (isset ($ sapiVars ['SERVER_PORT ' ]) && ($ port = $ sapiVars ['SERVER_PORT ' ]) !== $ defaultPort ) {
248
- $ host .= ": {$ port }" ;
249
- }
282
+ if (isset ($ sapiVars ['SERVER_PORT ' ])) {
283
+ $ uriParts ['port ' ] = $ sapiVars ['SERVER_PORT ' ];
250
284
}
251
-
252
- $ uri = "{$ scheme }:// {$ host }" ;
253
- $ uri = rtrim ($ uri , '/ ' ) . '/ ' . ltrim ($ sapiVars ['REQUEST_URI ' ], '/ ' );
254
- return $ uri ;
285
+ $ uriParts ['host ' ] = $ sapiVars ['HTTP_HOST ' ] ?? $ sapiVars ['SERVER_NAME ' ] ?? null ;
286
+ $ uriParts ['path ' ] = '/ ' . ltrim ($ sapiVars ['REQUEST_URI ' ], '/ ' );
287
+ return static ::buildStringFromParts ($ uriParts );
255
288
}
256
289
257
290
public static function getCurrent (UriFactoryInterface $ uriFactory ): UriInterface
@@ -315,7 +348,7 @@ public static function getUser(UriInterface $uri): string
315
348
return $ explode [0 ];
316
349
}
317
350
318
- public static function getPassword (UriInterface $ uri ): string
351
+ public static function getPassword (UriInterface $ uri ): ? string
319
352
{
320
353
$ explode = explode (': ' , $ uri ->getUserInfo ());
321
354
if (isset ($ explode [1 ])) {
0 commit comments