1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\MixedContentScanner; |
4
|
|
|
|
5
|
|
|
use Spatie\Crawler\Url; |
6
|
|
|
use Spatie\Crawler\Crawler; |
7
|
|
|
use Spatie\Crawler\CrawlProfile; |
8
|
|
|
use Spatie\Crawler\CrawlInternalUrls; |
9
|
|
|
use Spatie\MixedContentScanner\Exceptions\InvalidUrl; |
10
|
|
|
|
11
|
|
|
class MixedContentScanner |
12
|
|
|
{ |
13
|
|
|
/** @var \Spatie\MixedContentScanner\MixedContentObserver */ |
14
|
|
|
public $mixedContentObserver; |
15
|
|
|
|
16
|
|
|
/** @var null|\Spatie\Crawler\CrawlProfile */ |
17
|
|
|
public $crawlProfile; |
18
|
|
|
|
19
|
|
|
/** @var int|null */ |
20
|
|
|
protected $maximumCrawlCount; |
21
|
|
|
|
22
|
|
|
public function __construct(MixedContentObserver $mixedContentObserver) |
23
|
|
|
{ |
24
|
|
|
$this->mixedContentObserver = $mixedContentObserver; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function scan(string $url, array $clientOptions = []) |
28
|
|
|
{ |
29
|
|
|
$this->guardAgainstInvalidUrl($url); |
30
|
|
|
|
31
|
|
|
$url = Url::create($url); |
32
|
|
|
|
33
|
|
|
$crawler = Crawler::create($clientOptions); |
34
|
|
|
|
35
|
|
|
if ($this->maximumCrawlCount) { |
|
|
|
|
36
|
|
|
$crawler->setMaximumCrawlCount($this->maximumCrawlCount); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$crawler->setCrawlProfile($this->crawlProfile ?? new CrawlInternalUrls($url)) |
40
|
|
|
->setCrawlObserver($this->mixedContentObserver) |
41
|
|
|
->startCrawling($url); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function setCrawlProfile(CrawlProfile $crawlProfile) |
45
|
|
|
{ |
46
|
|
|
$this->crawlProfile = $crawlProfile; |
47
|
|
|
|
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function setMaximumCrawlCount(int $maximumCrawlCount) |
52
|
|
|
{ |
53
|
|
|
$this->maximumCrawlCount = $maximumCrawlCount; |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function guardAgainstInvalidUrl(string $url) |
59
|
|
|
{ |
60
|
|
|
if ($url == '') { |
61
|
|
|
throw InvalidUrl::urlIsEmpty(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (!$this->startsWith($url, ['http://', 'https://'])) { |
65
|
|
|
throw InvalidUrl::invalidScheme($url); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function startsWith(string $haystack, array $needles): bool |
70
|
|
|
{ |
71
|
|
|
foreach ((array)$needles as $needle) { |
72
|
|
|
if ($needle != '' && substr($haystack, 0, strlen($needle)) === (string)$needle) { |
73
|
|
|
return true; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: