|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Xparse\Parser\Helper; |
|
6
|
|
|
|
|
7
|
|
|
use GuzzleHttp\Psr7\Uri; |
|
8
|
|
|
use GuzzleHttp\Psr7\UriResolver; |
|
9
|
|
|
use Xparse\ElementFinder\ElementFinder; |
|
10
|
|
|
use Xparse\ElementFinder\Helper\NodeHelper; |
|
11
|
|
|
use Xparse\ElementFinder\Helper\StringHelper; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @author Ivan Shcherbak <[email protected]> 12/28/14 |
|
15
|
|
|
*/ |
|
16
|
|
|
class RelativeToAbsoluteLinkConverter implements LinkConverterInterface { |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @inheritdoc |
|
20
|
|
|
*/ |
|
21
|
|
|
public function convert(ElementFinder $finder, string $affectedUrl = ''): ElementFinder { |
|
22
|
|
|
$affected = new Uri($affectedUrl); |
|
23
|
|
|
$dom = new \DomDocument(); |
|
24
|
|
|
$internalErrors = libxml_use_internal_errors(true); |
|
25
|
|
|
$disableEntities = libxml_disable_entity_loader(); |
|
26
|
|
|
$data = mb_convert_encoding(StringHelper::safeEncodeStr((string) $finder->content('.')->first()), 'HTML-ENTITIES', 'UTF-8'); |
|
27
|
|
|
$dom->loadHTML($data, LIBXML_NOCDATA & LIBXML_NOERROR); |
|
28
|
|
|
libxml_clear_errors(); |
|
29
|
|
|
libxml_use_internal_errors($internalErrors); |
|
30
|
|
|
libxml_disable_entity_loader($disableEntities); |
|
31
|
|
|
$xpath = new \DomXPath($dom); |
|
32
|
|
|
|
|
33
|
|
|
$srcElements = $xpath->query('//*[@src] | //*[@href] | //form[@action]'); |
|
34
|
|
|
$baseUrlAttr = $xpath->query('//base/@href')->item(0) ?? new \DOMAttr('href', ''); |
|
35
|
|
|
$baseUrl = $baseUrlAttr->value; |
|
36
|
|
|
|
|
37
|
|
|
foreach ($srcElements as $element) { |
|
38
|
|
|
/** @var \DOMElement $element */ |
|
39
|
|
|
$attributeName = 'href'; |
|
40
|
|
|
|
|
41
|
|
|
if ($element->hasAttribute('action') === true and $element->tagName === 'form') { |
|
42
|
|
|
$attributeName = 'action'; |
|
43
|
|
|
} else if ($element->hasAttribute('src') === true) { |
|
44
|
|
|
$attributeName = 'src'; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$relative = $element->getAttribute($attributeName); |
|
48
|
|
|
|
|
49
|
|
|
# don`t change javascript in href |
|
50
|
|
|
if (preg_match('!^\s*javascript\s*:\s*!', $relative)) { |
|
51
|
|
|
continue; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if (parse_url($relative) === false) { |
|
55
|
|
|
continue; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if ($baseUrl !== '' and !preg_match('!^(/|http)!i', $relative)) { |
|
59
|
|
|
$relative = UriResolver::resolve(new Uri($baseUrl), new Uri($relative)); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$url = UriResolver::resolve($affected, new Uri($relative)); |
|
|
|
|
|
|
63
|
|
|
$element->setAttribute($attributeName, (string) $url); |
|
64
|
|
|
} |
|
65
|
|
|
$output = NodeHelper::getInnerContent( |
|
66
|
|
|
$xpath->query('.')->item(0) ?? new \DOMNode() |
|
67
|
|
|
); |
|
68
|
|
|
return new ElementFinder( |
|
69
|
|
|
$output |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
} |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.