|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace LaravelFreelancerNL\LaravelIndexNow; |
|
6
|
|
|
|
|
7
|
|
|
use Exception; |
|
8
|
|
|
use Illuminate\Foundation\Bus\PendingDispatch; |
|
9
|
|
|
use Illuminate\Http\Client\Response; |
|
10
|
|
|
use Illuminate\Support\Facades\File; |
|
11
|
|
|
use Illuminate\Support\Facades\Http; |
|
12
|
|
|
use Illuminate\Support\Facades\Log; |
|
13
|
|
|
use Illuminate\Support\Str; |
|
14
|
|
|
use LaravelFreelancerNL\LaravelIndexNow\Exceptions\KeyFileDirectoryMissing; |
|
15
|
|
|
use LaravelFreelancerNL\LaravelIndexNow\Exceptions\TooManyUrlsException; |
|
16
|
|
|
use LaravelFreelancerNL\LaravelIndexNow\Jobs\IndexNowSubmitJob; |
|
17
|
|
|
|
|
18
|
|
|
class IndexNow |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @throws KeyFileDirectoryMissing |
|
22
|
|
|
*/ |
|
23
|
7 |
|
public function generateKey(): string |
|
24
|
|
|
{ |
|
25
|
7 |
|
$prefix = config('index-now.key-location'); |
|
26
|
|
|
|
|
27
|
7 |
|
$key = Str::uuid()->toString(); |
|
28
|
|
|
|
|
29
|
7 |
|
$filename = $prefix . $key . '.txt'; |
|
30
|
|
|
|
|
31
|
7 |
|
if (!file_exists(public_path(dirname($filename)))) { |
|
32
|
1 |
|
throw new KeyFileDirectoryMissing(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
6 |
|
File::put(public_path() . '/' . $filename, $key); |
|
36
|
|
|
|
|
37
|
6 |
|
return $key; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string|string[] $url |
|
42
|
|
|
* |
|
43
|
|
|
* @throws Exception |
|
44
|
|
|
*/ |
|
45
|
8 |
|
public function submit(string|array $url): Response|false |
|
46
|
|
|
{ |
|
47
|
8 |
|
if (config('app.env') !== config('index-now.production-env')) { |
|
48
|
2 |
|
$this->logFailedAttempt($url); |
|
49
|
|
|
|
|
50
|
2 |
|
return false; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
6 |
|
if (is_string($url)) { |
|
|
|
|
|
|
54
|
4 |
|
return $this->submitUrl($url); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
2 |
|
return $this->submitUrls($url); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param string|string[] $url |
|
62
|
|
|
* |
|
63
|
|
|
* @throws Exception |
|
64
|
|
|
*/ |
|
65
|
2 |
|
public function delaySubmission(string|array $url, ?int $delayInSeconds = null): PendingDispatch |
|
66
|
|
|
{ |
|
67
|
2 |
|
$delayInSeconds ??= (int) config('index-now.delay'); |
|
68
|
|
|
|
|
69
|
2 |
|
return IndexNowSubmitJob::dispatch($url)->delay(now()->addSeconds($delayInSeconds)); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string|string[] $url |
|
74
|
|
|
*/ |
|
75
|
2 |
|
protected function logFailedAttempt(string|array $url): void |
|
76
|
|
|
{ |
|
77
|
2 |
|
if (config('index-now.log-failed-submits') === false) { |
|
78
|
1 |
|
return; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
Log::info( |
|
82
|
1 |
|
'IndexNow: page submissions are only sent in production environments.', |
|
83
|
1 |
|
['url' => $url], |
|
84
|
1 |
|
); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @throws Exception |
|
89
|
|
|
*/ |
|
90
|
4 |
|
protected function submitUrl(string $url): Response |
|
91
|
|
|
{ |
|
92
|
4 |
|
$targetUrl = $this->generateTargetUrl(); |
|
93
|
4 |
|
$queryData = $this->getQueryData(); |
|
94
|
4 |
|
$queryData['url'] = $url; |
|
95
|
|
|
|
|
96
|
4 |
|
return Http::get($targetUrl, $queryData); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param string[] $urls |
|
101
|
|
|
* |
|
102
|
|
|
* @throws TooManyUrlsException |
|
103
|
|
|
*/ |
|
104
|
2 |
|
protected function submitUrls(array $urls): Response |
|
105
|
|
|
{ |
|
106
|
2 |
|
$targetUrl = $this->generateTargetUrl(); |
|
107
|
2 |
|
$queryData = $this->getQueryData(); |
|
108
|
2 |
|
$queryData['host'] = config('index-now.host'); |
|
109
|
2 |
|
$queryData['urlList'] = $this->prepareUrls($urls); |
|
110
|
|
|
|
|
111
|
1 |
|
return Http::post($targetUrl, $queryData); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
6 |
|
protected function generateTargetUrl(): string |
|
115
|
|
|
{ |
|
116
|
6 |
|
$searchEngineDomain = config('index-now.search-engine'); |
|
117
|
|
|
|
|
118
|
6 |
|
return 'https://' . $searchEngineDomain . '/indexnow'; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return array<string, string> |
|
123
|
|
|
*/ |
|
124
|
6 |
|
protected function getQueryData(): array |
|
125
|
|
|
{ |
|
126
|
6 |
|
$queryParameters = []; |
|
127
|
|
|
|
|
128
|
6 |
|
$keyLocation = config('index-now.key-location'); |
|
129
|
|
|
|
|
130
|
6 |
|
$queryParameters['key'] = config('index-now.key'); |
|
131
|
6 |
|
if (isset($keyLocation) && $keyLocation !== '') { |
|
132
|
2 |
|
$queryParameters['keyLocation'] = $keyLocation; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
6 |
|
return $queryParameters; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param string[] $urls |
|
140
|
|
|
* @return string[] |
|
141
|
|
|
* |
|
142
|
|
|
* @throws TooManyUrlsException |
|
143
|
|
|
*/ |
|
144
|
2 |
|
protected function prepareUrls(array $urls): array |
|
145
|
|
|
{ |
|
146
|
2 |
|
if (count($urls) > 10000) { |
|
147
|
1 |
|
throw new TooManyUrlsException(); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
1 |
|
foreach ($urls as $key => $url) { |
|
151
|
1 |
|
$urls[$key] = urlencode($url); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
1 |
|
return $urls; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|