Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Internal/Utilitie...
File: URL.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Automattic\WooCommerce\Internal\Utilities;
[2] Fix | Delete
[3] Fix | Delete
/**
[4] Fix | Delete
* Provides an easy method of assessing URLs, including filepaths (which will be silently
[5] Fix | Delete
* converted to a file:// URL if provided).
[6] Fix | Delete
*/
[7] Fix | Delete
class URL {
[8] Fix | Delete
/**
[9] Fix | Delete
* Components of the URL being assessed.
[10] Fix | Delete
*
[11] Fix | Delete
* The keys match those potentially returned by the parse_url() function, except
[12] Fix | Delete
* that they are always defined and 'drive' (Windows drive letter) has been added.
[13] Fix | Delete
*
[14] Fix | Delete
* @var string|null[]
[15] Fix | Delete
*/
[16] Fix | Delete
private $components = array(
[17] Fix | Delete
'drive' => null,
[18] Fix | Delete
'fragment' => null,
[19] Fix | Delete
'host' => null,
[20] Fix | Delete
'pass' => null,
[21] Fix | Delete
'path' => null,
[22] Fix | Delete
'port' => null,
[23] Fix | Delete
'query' => null,
[24] Fix | Delete
'scheme' => null,
[25] Fix | Delete
'user' => null,
[26] Fix | Delete
);
[27] Fix | Delete
[28] Fix | Delete
/**
[29] Fix | Delete
* If the URL (or filepath) is absolute.
[30] Fix | Delete
*
[31] Fix | Delete
* @var bool
[32] Fix | Delete
*/
[33] Fix | Delete
private $is_absolute;
[34] Fix | Delete
[35] Fix | Delete
/**
[36] Fix | Delete
* If the URL (or filepath) represents a directory other than the root directory.
[37] Fix | Delete
*
[38] Fix | Delete
* This is useful at different points in the process, when deciding whether to re-apply
[39] Fix | Delete
* a trailing slash at the end of processing or when we need to calculate how many
[40] Fix | Delete
* directory traversals are needed to form a (grand-)parent URL.
[41] Fix | Delete
*
[42] Fix | Delete
* @var bool
[43] Fix | Delete
*/
[44] Fix | Delete
private $is_non_root_directory;
[45] Fix | Delete
[46] Fix | Delete
/**
[47] Fix | Delete
* The components of the URL's path.
[48] Fix | Delete
*
[49] Fix | Delete
* For instance, in the case of "file:///srv/www/wp.site" (noting that a file URL has
[50] Fix | Delete
* no host component) this would contain:
[51] Fix | Delete
*
[52] Fix | Delete
* [ "srv", "www", "wp.site" ]
[53] Fix | Delete
*
[54] Fix | Delete
* In the case of a non-file URL such as "https://example.com/foo/bar/baz" (noting the
[55] Fix | Delete
* host is not part of the path) it would contain:
[56] Fix | Delete
*
[57] Fix | Delete
* [ "foo", "bar", "baz" ]
[58] Fix | Delete
*
[59] Fix | Delete
* @var array
[60] Fix | Delete
*/
[61] Fix | Delete
private $path_parts = array();
[62] Fix | Delete
[63] Fix | Delete
/**
[64] Fix | Delete
* The URL.
[65] Fix | Delete
*
[66] Fix | Delete
* @var string
[67] Fix | Delete
*/
[68] Fix | Delete
private $url;
[69] Fix | Delete
[70] Fix | Delete
/**
[71] Fix | Delete
* Creates and processes the provided URL (or filepath).
[72] Fix | Delete
*
[73] Fix | Delete
* @throws URLException If the URL (or filepath) is seriously malformed.
[74] Fix | Delete
*
[75] Fix | Delete
* @param string $url The URL (or filepath).
[76] Fix | Delete
*/
[77] Fix | Delete
public function __construct( string $url ) {
[78] Fix | Delete
$this->url = $url;
[79] Fix | Delete
$this->preprocess();
[80] Fix | Delete
$this->process_path();
[81] Fix | Delete
}
[82] Fix | Delete
[83] Fix | Delete
/**
[84] Fix | Delete
* Makes all slashes forward slashes, converts filepaths to file:// URLs, and
[85] Fix | Delete
* other processing to help with comprehension of filepaths.
[86] Fix | Delete
*
[87] Fix | Delete
* @throws URLException If the URL is seriously malformed.
[88] Fix | Delete
*/
[89] Fix | Delete
private function preprocess() {
[90] Fix | Delete
// For consistency, all slashes should be forward slashes.
[91] Fix | Delete
$this->url = str_replace( '\\', '/', $this->url );
[92] Fix | Delete
[93] Fix | Delete
// Windows: capture the drive letter if provided.
[94] Fix | Delete
if ( preg_match( '#^(file://)?([a-z]):/(?!/).*#i', $this->url, $matches ) ) {
[95] Fix | Delete
$this->components['drive'] = $matches[2];
[96] Fix | Delete
}
[97] Fix | Delete
[98] Fix | Delete
/*
[99] Fix | Delete
* If there is no scheme, assume and prepend "file://". An exception is made for cases where the URL simply
[100] Fix | Delete
* starts with exactly two forward slashes, which indicates 'any scheme' (most commonly, that is used when
[101] Fix | Delete
* there is freedom to switch between 'http' and 'https').
[102] Fix | Delete
*/
[103] Fix | Delete
if ( ! preg_match( '#^[a-z]+://#i', $this->url ) && ! preg_match( '#^//(?!/)#', $this->url ) ) {
[104] Fix | Delete
$this->url = 'file://' . $this->url;
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
$parsed_components = wp_parse_url( $this->url );
[108] Fix | Delete
[109] Fix | Delete
// If we received a really badly formed URL, let's go no further.
[110] Fix | Delete
if ( false === $parsed_components ) {
[111] Fix | Delete
throw new URLException(
[112] Fix | Delete
sprintf(
[113] Fix | Delete
/* translators: %s is the URL. */
[114] Fix | Delete
__( '%s is not a valid URL.', 'woocommerce' ),
[115] Fix | Delete
$this->url
[116] Fix | Delete
)
[117] Fix | Delete
);
[118] Fix | Delete
}
[119] Fix | Delete
[120] Fix | Delete
$this->components = array_merge( $this->components, $parsed_components );
[121] Fix | Delete
[122] Fix | Delete
// File URLs cannot have a host. However, the initial path segment *or* the Windows drive letter
[123] Fix | Delete
// (if present) may be incorrectly be interpreted as the host name.
[124] Fix | Delete
if ( 'file' === $this->components['scheme'] && ! empty( $this->components['host'] ) ) {
[125] Fix | Delete
// If we do not have a drive letter, then simply merge the host and the path together.
[126] Fix | Delete
if ( null === $this->components['drive'] ) {
[127] Fix | Delete
$this->components['path'] = $this->components['host'] . ( $this->components['path'] ?? '' );
[128] Fix | Delete
}
[129] Fix | Delete
[130] Fix | Delete
// Restore the host to null in this situation.
[131] Fix | Delete
$this->components['host'] = null;
[132] Fix | Delete
}
[133] Fix | Delete
}
[134] Fix | Delete
[135] Fix | Delete
/**
[136] Fix | Delete
* Simplifies the path if possible, by resolving directory traversals to the extent possible
[137] Fix | Delete
* without touching the filesystem.
[138] Fix | Delete
*/
[139] Fix | Delete
private function process_path() {
[140] Fix | Delete
$segments = explode( '/', $this->components['path'] );
[141] Fix | Delete
$this->is_absolute = substr( $this->components['path'], 0, 1 ) === '/' || ! empty( $this->components['host'] );
[142] Fix | Delete
$this->is_non_root_directory = substr( $this->components['path'], -1, 1 ) === '/' && strlen( $this->components['path'] ) > 1;
[143] Fix | Delete
$resolve_traversals = 'file' !== $this->components['scheme'] || $this->is_absolute;
[144] Fix | Delete
$retain_traversals = false;
[145] Fix | Delete
[146] Fix | Delete
// Clean the path.
[147] Fix | Delete
foreach ( $segments as $part ) {
[148] Fix | Delete
// Drop empty segments.
[149] Fix | Delete
if ( strlen( $part ) === 0 || '.' === $part ) {
[150] Fix | Delete
continue;
[151] Fix | Delete
}
[152] Fix | Delete
[153] Fix | Delete
// Directory traversals created with percent-encoding syntax should also be detected.
[154] Fix | Delete
$is_traversal = str_ireplace( '%2e', '.', $part ) === '..';
[155] Fix | Delete
[156] Fix | Delete
// Resolve directory traversals (if allowed: see further comment relating to this).
[157] Fix | Delete
if ( $resolve_traversals && $is_traversal ) {
[158] Fix | Delete
if ( count( $this->path_parts ) > 0 && ! $retain_traversals ) {
[159] Fix | Delete
$this->path_parts = array_slice( $this->path_parts, 0, count( $this->path_parts ) - 1 );
[160] Fix | Delete
continue;
[161] Fix | Delete
} elseif ( $this->is_absolute ) {
[162] Fix | Delete
continue;
[163] Fix | Delete
}
[164] Fix | Delete
}
[165] Fix | Delete
[166] Fix | Delete
/*
[167] Fix | Delete
* Consider allowing directory traversals to be resolved (ie, the process that converts 'foo/bar/../baz' to
[168] Fix | Delete
* 'foo/baz').
[169] Fix | Delete
*
[170] Fix | Delete
* 1. For this decision point, we are only concerned with relative filepaths (in all other cases,
[171] Fix | Delete
* $resolve_traversals will already be true).
[172] Fix | Delete
* 2. This is a 'one time' and unidirectional operation. We only wish to flip from false to true, and we
[173] Fix | Delete
* never wish to do this more than once.
[174] Fix | Delete
* 3. We only flip the switch after we have examined all leading '..' traversal segments.
[175] Fix | Delete
*/
[176] Fix | Delete
if ( false === $resolve_traversals && '..' !== $part && 'file' === $this->components['scheme'] && ! $this->is_absolute ) {
[177] Fix | Delete
$resolve_traversals = true;
[178] Fix | Delete
}
[179] Fix | Delete
[180] Fix | Delete
/*
[181] Fix | Delete
* Set a flag indicating that traversals should be retained. This is done to ensure we don't prematurely
[182] Fix | Delete
* discard traversals at the start of the path.
[183] Fix | Delete
*/
[184] Fix | Delete
$retain_traversals = $resolve_traversals && '..' === $part;
[185] Fix | Delete
[186] Fix | Delete
// Retain this part of the path.
[187] Fix | Delete
$this->path_parts[] = $part;
[188] Fix | Delete
}
[189] Fix | Delete
[190] Fix | Delete
// Protect against empty relative paths.
[191] Fix | Delete
if ( count( $this->path_parts ) === 0 && ! $this->is_absolute ) {
[192] Fix | Delete
$this->path_parts = array( '.' );
[193] Fix | Delete
$this->is_non_root_directory = true;
[194] Fix | Delete
}
[195] Fix | Delete
[196] Fix | Delete
// Reform the path from the processed segments, appending a leading slash if it is absolute and restoring
[197] Fix | Delete
// the Windows drive letter if we have one.
[198] Fix | Delete
$this->components['path'] = ( $this->is_absolute ? '/' : '' ) . implode( '/', $this->path_parts ) . ( $this->is_non_root_directory ? '/' : '' );
[199] Fix | Delete
}
[200] Fix | Delete
[201] Fix | Delete
/**
[202] Fix | Delete
* Returns the processed URL as a string.
[203] Fix | Delete
*
[204] Fix | Delete
* @return string
[205] Fix | Delete
*/
[206] Fix | Delete
public function __toString(): string {
[207] Fix | Delete
return $this->get_url();
[208] Fix | Delete
}
[209] Fix | Delete
[210] Fix | Delete
/**
[211] Fix | Delete
* Returns all possible parent URLs for the current URL.
[212] Fix | Delete
*
[213] Fix | Delete
* @return string[]
[214] Fix | Delete
*/
[215] Fix | Delete
public function get_all_parent_urls(): array {
[216] Fix | Delete
$max_parent = count( $this->path_parts );
[217] Fix | Delete
$parents = array();
[218] Fix | Delete
[219] Fix | Delete
/*
[220] Fix | Delete
* If we are looking at a relative path that begins with at least one traversal (example: "../../foo")
[221] Fix | Delete
* then we should only return one parent URL (otherwise, we'd potentially have to return an infinite
[222] Fix | Delete
* number of parent URLs since we can't know how far the tree extends).
[223] Fix | Delete
*/
[224] Fix | Delete
if ( $max_parent > 0 && ! $this->is_absolute && '..' === $this->path_parts[0] ) {
[225] Fix | Delete
$max_parent = 1;
[226] Fix | Delete
}
[227] Fix | Delete
[228] Fix | Delete
for ( $level = 1; $level <= $max_parent; $level++ ) {
[229] Fix | Delete
$parents[] = $this->get_parent_url( $level );
[230] Fix | Delete
}
[231] Fix | Delete
[232] Fix | Delete
return $parents;
[233] Fix | Delete
}
[234] Fix | Delete
[235] Fix | Delete
/**
[236] Fix | Delete
* Outputs the parent URL.
[237] Fix | Delete
*
[238] Fix | Delete
* For example, if $this->get_url() returns "https://example.com/foo/bar/baz" then
[239] Fix | Delete
* this method will return "https://example.com/foo/bar/".
[240] Fix | Delete
*
[241] Fix | Delete
* When a grand-parent is needed, the optional $level parameter can be used. By default
[242] Fix | Delete
* this is set to 1 (parent). 2 will yield the grand-parent, 3 will yield the great
[243] Fix | Delete
* grand-parent, etc.
[244] Fix | Delete
*
[245] Fix | Delete
* If a level is specified that exceeds the number of path segments, this method will
[246] Fix | Delete
* return false.
[247] Fix | Delete
*
[248] Fix | Delete
* @param int $level Used to indicate the level of parent.
[249] Fix | Delete
*
[250] Fix | Delete
* @return string|false
[251] Fix | Delete
*/
[252] Fix | Delete
public function get_parent_url( int $level = 1 ) {
[253] Fix | Delete
if ( $level < 1 ) {
[254] Fix | Delete
$level = 1;
[255] Fix | Delete
}
[256] Fix | Delete
[257] Fix | Delete
$parts_count = count( $this->path_parts );
[258] Fix | Delete
$parent_path_parts_to_keep = $parts_count - $level;
[259] Fix | Delete
[260] Fix | Delete
/*
[261] Fix | Delete
* With the exception of file URLs, we do not allow obtaining (grand-)parent directories that require
[262] Fix | Delete
* us to describe them using directory traversals. For example, given "http://hostname/foo/bar/baz.png" we do
[263] Fix | Delete
* not permit determining anything more than 2 levels up (we cannot go beyond "http://hostname/").
[264] Fix | Delete
*/
[265] Fix | Delete
if ( 'file' !== $this->components['scheme'] && $parent_path_parts_to_keep < 0 ) {
[266] Fix | Delete
return false;
[267] Fix | Delete
}
[268] Fix | Delete
[269] Fix | Delete
// In the specific case of an absolute filepath describing the root directory, there can be no parent.
[270] Fix | Delete
if ( 'file' === $this->components['scheme'] && $this->is_absolute && empty( $this->path_parts ) ) {
[271] Fix | Delete
return false;
[272] Fix | Delete
}
[273] Fix | Delete
[274] Fix | Delete
// Handle cases where the path starts with one or more 'dot segments'. Since the path has already been
[275] Fix | Delete
// processed, we can be confident that any such segments are at the start of the path.
[276] Fix | Delete
if ( $parts_count > 0 && ( '.' === $this->path_parts[0] || '..' === $this->path_parts[0] ) ) {
[277] Fix | Delete
// Determine the index of the last dot segment (ex: given the path '/../../foo' it would be 1).
[278] Fix | Delete
$single_dots = array_keys( $this->path_parts, '.', true );
[279] Fix | Delete
$double_dots = array_keys( $this->path_parts, '..', true );
[280] Fix | Delete
$max_dot_index = max( array_merge( $single_dots, $double_dots ) );
[281] Fix | Delete
[282] Fix | Delete
// Prepend the required number of traversals and discard unnecessary trailing segments.
[283] Fix | Delete
$last_traversal = $max_dot_index + ( $this->is_non_root_directory ? 1 : 0 );
[284] Fix | Delete
$parent_path = str_repeat( '../', $level ) . join( '/', array_slice( $this->path_parts, 0, $last_traversal ) );
[285] Fix | Delete
} elseif ( $parent_path_parts_to_keep < 0 ) {
[286] Fix | Delete
// For relative filepaths only, we use traversals to describe the requested parent.
[287] Fix | Delete
$parent_path = untrailingslashit( str_repeat( '../', $parent_path_parts_to_keep * -1 ) );
[288] Fix | Delete
} else {
[289] Fix | Delete
// Otherwise, in a very simple case, we just remove existing parts.
[290] Fix | Delete
$parent_path = implode( '/', array_slice( $this->path_parts, 0, $parent_path_parts_to_keep ) );
[291] Fix | Delete
}
[292] Fix | Delete
[293] Fix | Delete
if ( $this->is_relative() && '' === $parent_path ) {
[294] Fix | Delete
$parent_path = '.';
[295] Fix | Delete
}
[296] Fix | Delete
[297] Fix | Delete
// Append a trailing slash, since a parent is always a directory. The only exception is the current working directory.
[298] Fix | Delete
$parent_path .= '/';
[299] Fix | Delete
[300] Fix | Delete
// For absolute paths, apply a leading slash (does not apply if we have a root path).
[301] Fix | Delete
if ( $this->is_absolute && 0 !== strpos( $parent_path, '/' ) ) {
[302] Fix | Delete
$parent_path = '/' . $parent_path;
[303] Fix | Delete
}
[304] Fix | Delete
[305] Fix | Delete
// Form the parent URL (ditching the query and fragment, if set).
[306] Fix | Delete
$parent_url = $this->get_url(
[307] Fix | Delete
array(
[308] Fix | Delete
'path' => $parent_path,
[309] Fix | Delete
'query' => null,
[310] Fix | Delete
'fragment' => null,
[311] Fix | Delete
)
[312] Fix | Delete
);
[313] Fix | Delete
[314] Fix | Delete
// We process the parent URL through a fresh instance of this class, for consistency.
[315] Fix | Delete
return ( new self( $parent_url ) )->get_url();
[316] Fix | Delete
}
[317] Fix | Delete
[318] Fix | Delete
/**
[319] Fix | Delete
* Outputs the processed URL.
[320] Fix | Delete
*
[321] Fix | Delete
* Borrows from https://www.php.net/manual/en/function.parse-url.php#106731
[322] Fix | Delete
*
[323] Fix | Delete
* @param array $component_overrides If provided, these will override values set in $this->components.
[324] Fix | Delete
*
[325] Fix | Delete
* @return string
[326] Fix | Delete
*/
[327] Fix | Delete
public function get_url( array $component_overrides = array() ): string {
[328] Fix | Delete
$components = array_merge( $this->components, $component_overrides );
[329] Fix | Delete
[330] Fix | Delete
$scheme = null !== $components['scheme'] ? $components['scheme'] . '://' : '//';
[331] Fix | Delete
$host = null !== $components['host'] ? $components['host'] : '';
[332] Fix | Delete
$port = null !== $components['port'] ? ':' . $components['port'] : '';
[333] Fix | Delete
$path = $this->get_path( $components['path'] );
[334] Fix | Delete
[335] Fix | Delete
// Special handling for hostless URLs (typically, filepaths) referencing the current working directory.
[336] Fix | Delete
if ( '' === $host && ( '' === $path || '.' === $path ) ) {
[337] Fix | Delete
$path = './';
[338] Fix | Delete
}
[339] Fix | Delete
[340] Fix | Delete
$user = null !== $components['user'] ? $components['user'] : '';
[341] Fix | Delete
$pass = null !== $components['pass'] ? ':' . $components['pass'] : '';
[342] Fix | Delete
$user_pass = ( ! empty( $user ) || ! empty( $pass ) ) ? $user . $pass . '@' : '';
[343] Fix | Delete
[344] Fix | Delete
$query = null !== $components['query'] ? '?' . $components['query'] : '';
[345] Fix | Delete
$fragment = null !== $components['fragment'] ? '#' . $components['fragment'] : '';
[346] Fix | Delete
[347] Fix | Delete
return $scheme . $user_pass . $host . $port . $path . $query . $fragment;
[348] Fix | Delete
}
[349] Fix | Delete
[350] Fix | Delete
/**
[351] Fix | Delete
* Outputs the path. Especially useful if it was a a regular filepath that was passed in originally.
[352] Fix | Delete
*
[353] Fix | Delete
* @param string|null $path_override If provided this will be used as the URL path. Does not impact drive letter.
[354] Fix | Delete
*
[355] Fix | Delete
* @return string
[356] Fix | Delete
*/
[357] Fix | Delete
public function get_path( ?string $path_override = null ): string {
[358] Fix | Delete
return ( $this->components['drive'] ? $this->components['drive'] . ':' : '' ) . ( $path_override ?? $this->components['path'] );
[359] Fix | Delete
}
[360] Fix | Delete
[361] Fix | Delete
/**
[362] Fix | Delete
* Indicates if the URL or filepath was absolute.
[363] Fix | Delete
*
[364] Fix | Delete
* @return bool True if absolute, else false.
[365] Fix | Delete
*/
[366] Fix | Delete
public function is_absolute(): bool {
[367] Fix | Delete
return $this->is_absolute;
[368] Fix | Delete
}
[369] Fix | Delete
[370] Fix | Delete
/**
[371] Fix | Delete
* Indicates if the URL or filepath was relative.
[372] Fix | Delete
*
[373] Fix | Delete
* @return bool True if relative, else false.
[374] Fix | Delete
*/
[375] Fix | Delete
public function is_relative(): bool {
[376] Fix | Delete
return ! $this->is_absolute;
[377] Fix | Delete
}
[378] Fix | Delete
}
[379] Fix | Delete
[380] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function