17 use GuzzleHttp\Cookie\CookieJar;
47 'User-Agent' =>
'TYPO3 linkvalidator',
49 'Accept-Language' =>
'*',
50 'Accept-Encoding' =>
'*',
65 $this->requestFactory =
$requestFactory ?: GeneralUtility::makeInstance(RequestFactory::class);
77 public function checkLink($origUrl, $softRefEntry, $reference)
81 if (isset($this->urlReports[$origUrl])) {
83 return $this->urlReports[$origUrl];
86 'cookies' => GeneralUtility::makeInstance(CookieJar::class),
87 'allow_redirects' => [
'strict' =>
true],
92 $isValidUrl = $this->
requestUrl($url,
'HEAD', $options);
95 $options[
'headers'][
'Range'] =
'bytes=0-4048';
96 $isValidUrl = $this->
requestUrl($url,
'GET', $options);
99 $this->urlReports[$origUrl] = $isValidUrl;
112 protected function requestUrl(
string $url,
string $method, array $options): bool
114 $this->errorParams = [];
117 $response = $this->requestFactory->request($url, $method, $options);
118 if ($response->getStatusCode() >= 300) {
119 $this->errorParams[
'errorType'] = $response->getStatusCode();
120 $this->errorParams[
'message'] = $this->
getErrorMessage($this->errorParams);
124 }
catch (\GuzzleHttp\Exception\TooManyRedirectsException $e) {
127 $this->errorParams[
'errorType'] =
'loop';
128 $this->errorParams[
'exception'] = $e->getMessage();
129 $this->errorParams[
'message'] = $this->
getErrorMessage($this->errorParams);
130 }
catch (\GuzzleHttp\Exception\ClientException $e) {
131 if ($e->hasResponse()) {
132 $this->errorParams[
'errorType'] = $e->getResponse()->getStatusCode();
134 $this->errorParams[
'errorType'] =
'unknown';
136 $this->errorParams[
'exception'] = $e->getMessage();
137 $this->errorParams[
'message'] = $this->
getErrorMessage($this->errorParams);
138 }
catch (\GuzzleHttp\Exception\RequestException $e) {
139 $this->errorParams[
'errorType'] =
'network';
140 $this->errorParams[
'exception'] = $e->getMessage();
141 $this->errorParams[
'message'] = $this->
getErrorMessage($this->errorParams);
142 }
catch (\Exception $e) {
144 $this->errorParams[
'errorType'] =
'exception';
145 $this->errorParams[
'exception'] = $e->getMessage();
146 $this->errorParams[
'message'] = $this->
getErrorMessage($this->errorParams);
161 switch ($errorType) {
163 $message = sprintf($lang->getLL(
'list.report.externalerror'), $errorType);
166 $message = $lang->getLL(
'list.report.pageforbidden403');
169 $message = $lang->getLL(
'list.report.pagenotfound404');
172 $message = $lang->getLL(
'list.report.internalerror500');
176 $lang->getLL(
'list.report.redirectloop'),
182 $message = sprintf($lang->getLL(
'list.report.httpexception'),
$errorParams[
'exception']);
185 $message = $lang->getLL(
'list.report.networkexception');
191 $message = sprintf($lang->getLL(
'list.report.otherhttpcode'), $errorType,
$errorParams[
'exception']);
205 public function fetchType($value, $type, $key)
207 preg_match_all(
'/((?:http|https))(?::\\/\\/)(?:[^\\s<>]+)/i', $value[
'tokenValue'], $urls, PREG_PATTERN_ORDER);
208 if (!empty($urls[0][0])) {
222 $url = html_entity_decode($url);
223 $parts = parse_url($url);
224 $host = (string)($parts[
'host'] ??
'');
228 if (strcmp($host, $newDomain) !== 0) {
229 $parts[
'host'] = $newDomain;
232 }
catch (\Exception | \Throwable $e) {