-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PISHPS-329 UrlParsingService - matching trackign code from tracking url #798
Closed
Closed
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ebf9a39
PISHPS-303: extended LineItemDataExtractor. It now also sanitizes que…
m-muxfeld-diw d16c21d
PISHPS-303: removed redundant comments
m-muxfeld-diw 81edacc
PISHPS-303: removed debug line
m-muxfeld-diw 987faad
PISHPS-303: fixed phpstan error
m-muxfeld-diw 519834b
PISHPS-303: added missing space
m-muxfeld-diw e8d64fb
PISHPS-329: added UrlParsingService to separate tracking code from tr…
m-muxfeld-diw 6bfba85
PISHPS-329: resolved merge conflict
m-muxfeld-diw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Kiener\MolliePayments\Service; | ||
|
||
class UrlParsingService | ||
{ | ||
/** | ||
* Checks if a given string is a valid URL. | ||
* | ||
* @param string $value The string to be checked. | ||
* @return bool True if the string is a valid URL, false otherwise. | ||
*/ | ||
public function isUrl(string $value): bool | ||
{ | ||
return filter_var($value, FILTER_VALIDATE_URL) !== false; | ||
} | ||
|
||
/** | ||
* Parses the tracking code from a given URL. | ||
* | ||
* This method searches for tracking codes in the URL in the following formats: | ||
* - As a query parameter (e.g., ?code=12345) | ||
* - As a path segment (e.g., /code/12345/) | ||
* - As a hash fragment (e.g., #code=12345) | ||
* | ||
* @param string $value The URL to be parsed. | ||
* @return array{0: string, 1: string} An array where: | ||
* - Index 0 contains the parsed tracking code (if found), or an empty string if no code is found. | ||
* - Index 1 contains the original URL. | ||
*/ | ||
public function parseTrackingCodeFromUrl(string $value): array | ||
{ | ||
// Case 1: Query parameter | ||
if ((bool)preg_match('#(code|shipment|track|tracking)=([a-zA-Z0-9]+)#i', $value, $matches)) { | ||
return [$matches[2], $value]; | ||
} | ||
|
||
// Case 2: Path-based tracking | ||
if ((bool)preg_match('#/(code|shipment|track|tracking)/([a-zA-Z0-9]+)/#i', $value, $matches)) { | ||
return [$matches[2], $value]; | ||
} | ||
|
||
// Case 3: Hash-based tracking | ||
if ((bool)preg_match('#\#(code|shipment|track|tracking)=([a-zA-Z0-9]+)#i', $value, $matches)) { | ||
return [$matches[2], $value]; | ||
} | ||
|
||
// could not determine code | ||
return ['', $value]; | ||
} | ||
|
||
public function encodePathAndQuery(string $fullUrl):string | ||
{ | ||
$urlParts = parse_url($fullUrl); | ||
|
||
$scheme = isset($urlParts['scheme']) ? $urlParts['scheme'] . '://' : ''; | ||
|
||
$host = isset($urlParts['host']) ? $urlParts['host'] : ''; | ||
|
||
$port = isset($urlParts['port']) ? ':' . $urlParts['port'] : ''; | ||
|
||
$user = isset($urlParts['user']) ? $urlParts['user'] : ''; | ||
|
||
$pass = isset($urlParts['pass']) ? ':' . $urlParts['pass'] : ''; | ||
|
||
$pass = ($user || $pass) ? "$pass@" : ''; | ||
|
||
$path = isset($urlParts['path']) ? $urlParts['path'] : ''; | ||
|
||
if (mb_strlen($path) > 0) { | ||
$pathParts = explode('/', $path); | ||
array_walk($pathParts, function (&$pathPart) { | ||
$pathPart = rawurlencode($pathPart); | ||
}); | ||
$path = implode('/', $pathParts); | ||
} | ||
|
||
$query = ''; | ||
if (isset($urlParts['query'])) { | ||
$urlParts['query'] = $this->sanitizeQuery(explode('&', $urlParts['query'])); | ||
$query = '?' . implode('&', $urlParts['query']); | ||
} | ||
|
||
|
||
$fragment = isset($urlParts['fragment']) ? '#' . rawurlencode($urlParts['fragment']) : ''; | ||
|
||
return trim($scheme.$user.$pass.$host.$port.$path.$query.$fragment); | ||
} | ||
|
||
/** | ||
* Sanitizes an array of query strings by URL encoding their components. | ||
* | ||
* This method takes an array of query strings, where each string is expected to be in the format | ||
* 'key=value'. It applies the sanitizeQueryPart method to each query string to ensure the keys | ||
* and values are URL encoded, making them safe for use in URLs. | ||
* | ||
* @param string[] $query An array of query strings to be sanitized. | ||
* @return string[] The sanitized array with URL encoded query strings. | ||
*/ | ||
public function sanitizeQuery(array $query): array | ||
{ | ||
// Use array_map to apply the sanitizeQueryPart method to each element of the $query array | ||
return array_map([$this, 'sanitizeQueryPart'], $query); | ||
} | ||
|
||
/** | ||
* Sanitizes a single query string part by URL encoding its key and value. | ||
* | ||
* This method takes a query string part, expected to be in the format 'key=value', splits it into | ||
* its key and value components, URL encodes each component, and then recombines them into a single | ||
* query string part. | ||
* | ||
* @param string $queryPart A single query string part to be sanitized. | ||
* @return string The sanitized query string part with URL encoded components. | ||
*/ | ||
public function sanitizeQueryPart(string $queryPart): string | ||
{ | ||
if (strpos($queryPart, '=') === false) { | ||
return $queryPart; | ||
} | ||
|
||
// Split the query part into key and value based on the '=' delimiter | ||
[$key, $value] = explode('=', $queryPart); | ||
|
||
$key = rawurlencode($key); | ||
$value = rawurlencode($value); | ||
|
||
return sprintf('%s=%s', $key, $value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think that can be dangerous, since we dont know for each provier the tracking code. i would rather suggest extract url and put query parameters as code (if code contains URL)