|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\MixedContentScanner; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
7
|
|
|
use Psr\Http\Message\UriInterface; |
|
8
|
|
|
use Spatie\Crawler\CrawlObserver; |
|
9
|
|
|
|
|
10
|
|
|
class MixedContentObserver extends CrawlObserver |
|
11
|
|
|
{ |
|
12
|
|
|
private $crawledCss = []; |
|
13
|
|
|
|
|
14
|
|
|
private $shouldExtractLinkedCss = false; |
|
15
|
|
|
|
|
16
|
|
|
public function crawled(UriInterface $url, ResponseInterface $response, ?UriInterface $foundOnUrl = null) |
|
17
|
|
|
{ |
|
18
|
|
|
[$mixedContent, $linkedCss] = MixedContentExtractor::extract((string) $response->getBody(), $url); |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
if ($this->shouldExtractLinkedCss) { |
|
21
|
|
|
foreach ($linkedCss as $css) { |
|
22
|
|
|
$mixedContent = array_merge((array) $mixedContent, (array) $this->linkedCssFound($css, $url)); |
|
|
|
|
|
|
23
|
|
|
} |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
if (! count($mixedContent)) { |
|
27
|
|
|
$this->noMixedContentFound($url); |
|
28
|
|
|
|
|
29
|
|
|
return; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
foreach ($mixedContent as $mixedContentItem) { |
|
33
|
|
|
$this->mixedContentFound($mixedContentItem); |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function crawlFailed( |
|
38
|
|
|
UriInterface $url, |
|
39
|
|
|
RequestException $requestException, |
|
40
|
|
|
?UriInterface $foundOnUrl = null |
|
41
|
|
|
) { |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Will be called when mixed content was found. |
|
46
|
|
|
* |
|
47
|
|
|
* @param \Spatie\MixedContentScanner\MixedContent $mixedContent |
|
48
|
|
|
*/ |
|
49
|
|
|
public function mixedContentFound(MixedContent $mixedContent) |
|
|
|
|
|
|
50
|
|
|
{ |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Will be called when no mixed content was found on the given url. |
|
55
|
|
|
* |
|
56
|
|
|
* @param \Psr\Http\Message\UriInterface |
|
57
|
|
|
*/ |
|
58
|
|
|
public function noMixedContentFound(UriInterface $crawledUrl) |
|
|
|
|
|
|
59
|
|
|
{ |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function linkedCssFound(UriInterface $css, UriInterface $linkedByUrl) |
|
63
|
|
|
{ |
|
64
|
|
|
if (! in_array($css, $this->crawledCss)) { |
|
65
|
|
|
$this->crawledCss[] = $css; |
|
66
|
|
|
|
|
67
|
|
|
return MixedContentLinkedCssExtractor::extract(Body::get($css), $css, $linkedByUrl); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function withLinkedCss() |
|
72
|
|
|
{ |
|
73
|
|
|
$this->shouldExtractLinkedCss = true; |
|
74
|
|
|
|
|
75
|
|
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
This error can happen if you refactor code and forget to move the variable initialization.
Let’s take a look at a simple example:
The above code is perfectly fine. Now imagine that we re-order the statements:
In that case,
$xwould be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.