1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Padosoft\Laravel\Google\StructuredDataTestingTool; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
|
7
|
|
|
class GoogleMarkupHelper |
8
|
|
|
{ |
9
|
|
|
protected $command; |
10
|
|
|
|
11
|
|
|
protected $url; |
12
|
|
|
|
13
|
|
|
protected $whitelist; |
14
|
|
|
|
15
|
|
|
public $tableEntities = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* constructor. |
19
|
|
|
* @param Command $objcommand |
20
|
|
|
* @param $whitelist |
21
|
|
|
*/ |
22
|
|
|
public function __construct(Command $objcommand, $whitelist) |
23
|
|
|
{ |
24
|
|
|
$this->command = $objcommand; |
25
|
|
|
$this->whitelist = $whitelist; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* |
30
|
|
|
* Send Request to sensiolab and return array of sensiolab vulnerabilities. |
31
|
|
|
* Empty array if here is no vulnerabilities. |
32
|
|
|
* |
33
|
|
|
* @param $url path to composer.lock file. |
34
|
|
|
* |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
|
|
public function checkUrl($url) |
38
|
|
|
{ |
39
|
|
|
$this->url = $url; |
40
|
|
|
|
41
|
|
|
$optArray = array( |
42
|
|
|
CURLOPT_URL => "https://search.google.com/structured-data/testing-tool/validate", |
43
|
|
|
CURLOPT_RETURNTRANSFER => true, |
44
|
|
|
CURLOPT_ENCODING => "", |
45
|
|
|
CURLOPT_MAXREDIRS => 10, |
46
|
|
|
CURLOPT_TIMEOUT => 30, |
47
|
|
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
48
|
|
|
CURLOPT_CUSTOMREQUEST => "POST", |
49
|
|
|
CURLOPT_POSTFIELDS => "url=" . urlencode($url), |
50
|
|
|
CURLOPT_HTTPHEADER => array( |
51
|
|
|
"cache-control: no-cache", |
52
|
|
|
"content-type: application/x-www-form-urlencoded;charset=UTF-8", |
53
|
|
|
"origin: https://search.google.com", |
54
|
|
|
"postman-token: 7ee787bf-1fe1-30c6-0b76-3dce8926bd5b", |
55
|
|
|
"referer: https://search.google.com/structured-data/testing-tool/u/0/", |
56
|
|
|
"user-agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36" |
57
|
|
|
), |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
$response = $this->sendRequest($optArray); |
61
|
|
|
if ($response === false) { |
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this->parseResponse($response); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param $optArray |
70
|
|
|
* @return bool|mixed |
71
|
|
|
*/ |
72
|
|
|
public function sendRequest($optArray) |
73
|
|
|
{ |
74
|
|
|
$curl = curl_init(); |
75
|
|
|
|
76
|
|
|
curl_setopt_array($curl, $optArray); |
77
|
|
|
|
78
|
|
|
$response = curl_exec($curl); |
79
|
|
|
$err = curl_error($curl); |
80
|
|
|
$information = curl_getinfo($curl); |
81
|
|
|
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
82
|
|
|
|
83
|
|
|
curl_close($curl); |
84
|
|
|
|
85
|
|
|
$colorTag = $this->getColorTagForStatusCode($http_code); |
86
|
|
|
$this->command->line("HTTP StatusCode: <{$colorTag}>" . $http_code . "</{$colorTag}>"); |
87
|
|
|
|
88
|
|
|
if ($err || $http_code != 200) { |
89
|
|
|
$this->command->error("cURL Error #:" . $err); |
90
|
|
|
$this->command->line("cURL info: " . print_r($information, true)); |
91
|
|
|
$this->command->line("Response: " . get_var_dump_output($response)); |
92
|
|
|
return false; |
93
|
|
|
} elseif ($this->command->option('verbose')=="true") { |
94
|
|
|
$this->command->line("cURL opt array:"); |
95
|
|
|
$this->command->line(get_var_dump_output($optArray)); |
96
|
|
|
$this->command->line("cURL info: " . print_r($information, true)); |
97
|
|
|
$this->command->line("Response: " . get_var_dump_output($response)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $response; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param $error |
105
|
|
|
* @param bool $tuttoOk |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
public function checkError($error, $tuttoOk) |
109
|
|
|
{ |
110
|
|
|
$tableEntities = array(); |
111
|
|
|
|
112
|
|
|
$arr = [ |
113
|
|
|
'name' => '', |
114
|
|
|
'type' => var_export($error['ownerSet'], true), |
115
|
|
|
'errors' => var_export($error['errorType'], true), |
116
|
|
|
'warnings' => var_export($error['args'], true), |
117
|
|
|
'isOk' => $tuttoOk, |
118
|
|
|
]; |
119
|
|
|
$tableEntities[] = $arr; |
120
|
|
|
|
121
|
|
|
return $tableEntities; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param $response |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
|
|
public function checkResponse($response) |
129
|
|
|
{ |
130
|
|
|
$tuttoOk = true; |
131
|
|
|
|
132
|
|
|
if ($response['numObjects'] == 0) { |
133
|
|
|
$this->command->info("No structured data Found"); |
134
|
|
|
} |
135
|
|
|
if ($response['totalNumErrors'] > 0) { |
136
|
|
|
$this->command->error("Found " . $response['totalNumErrors'] . " error"); |
137
|
|
|
$tuttoOk = false; |
138
|
|
|
} |
139
|
|
|
if ($response['totalNumWarnings'] > 0) { |
140
|
|
|
$this->command->warn("Found " . $response['totalNumWarnings'] . " warnings"); |
141
|
|
|
$tuttoOk = false; |
142
|
|
|
} |
143
|
|
|
if ($response['totalNumErrors'] == 0 && $response['totalNumWarnings'] == 0) { |
144
|
|
|
$this->command->info("No Errors or Warnings Found"); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if (in_array(rtrim($this->url), $this->whitelist)) { |
148
|
|
|
$tuttoOk = true; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$colorTag = $this->getColorTagForResponse($response); |
152
|
|
|
|
153
|
|
|
$this->tableEntities [] = [ |
154
|
|
|
'name' => "<{$colorTag}>" . $this->url . "</{$colorTag}>", |
155
|
|
|
'type' => $response['numObjects'] == 0 ? 'No structured data Found' : '', |
156
|
|
|
'errors' => $response['totalNumErrors'], |
157
|
|
|
'warnings' => $response['totalNumWarnings'], |
158
|
|
|
'isOk' => $tuttoOk, |
159
|
|
|
]; |
160
|
|
|
|
161
|
|
|
if(!array_key_exists_safe($response, 'errors')) { |
162
|
|
|
return $tuttoOk; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
foreach ($response['errors'] as $error) { |
166
|
|
|
$this->tableEntities = array_merge($this->tableEntities, $this->checkError($error, $tuttoOk)); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $tuttoOk; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Get the color tag for the given status code. |
174
|
|
|
* |
175
|
|
|
* @param string $code |
176
|
|
|
* |
177
|
|
|
* @return string |
178
|
|
|
* |
179
|
|
|
* @see https://github.com/spatie/http-status-check/blob/master/src/CrawlLogger.php#L96 |
180
|
|
|
*/ |
181
|
|
|
protected function getColorTagForStatusCode($code) |
182
|
|
|
{ |
183
|
|
|
if (starts_with($code, '2')) { |
184
|
|
|
return 'info'; |
185
|
|
|
} |
186
|
|
|
if (starts_with($code, '3')) { |
187
|
|
|
return 'comment'; |
188
|
|
|
} |
189
|
|
|
return 'error'; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Get the color tag for the given response. |
194
|
|
|
* |
195
|
|
|
* @param string $response |
196
|
|
|
* |
197
|
|
|
* @return string |
198
|
|
|
* |
199
|
|
|
*/ |
200
|
|
|
protected function getColorTagForResponse($response) |
201
|
|
|
{ |
202
|
|
|
if ($response['totalNumErrors'] > 0) { |
203
|
|
|
return 'error'; |
204
|
|
|
} |
205
|
|
|
if ($response['totalNumWarnings'] > 0 || $response['numObjects'] == 0) { |
206
|
|
|
return 'comment'; |
207
|
|
|
} |
208
|
|
|
return 'info'; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @param $response |
213
|
|
|
* @return mixed|string |
214
|
|
|
*/ |
215
|
|
|
protected function parseResponse($response) |
216
|
|
|
{ |
217
|
|
|
//sometimes response return with strange start chars |
218
|
|
|
if (!starts_with(trim($response), "{")) { |
219
|
|
|
$response = substr($response, 4); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
//strip html sometimes invalidate json |
223
|
|
|
$response = str_replace(["\r", "\n"], "", $response); |
224
|
|
|
$re = '/"html":"(.*),"errors"/'; |
225
|
|
|
$subst = '"html":"","errors"'; |
226
|
|
|
$response = preg_replace($re, $subst, $response); |
227
|
|
|
$re = '/"html": "(.*),"errors"/'; |
228
|
|
|
$subst = '"html":"","errors"'; |
229
|
|
|
$response = preg_replace($re, $subst, $response); |
230
|
|
|
|
231
|
|
|
$response = json_decode($response, true); |
232
|
|
|
|
233
|
|
|
return $response; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|