TYPO3 CMS  TYPO3_7-6
ExternalLinktype.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
19 
24 {
30  protected $urlReports = [];
31 
37  protected $urlErrorParams = [];
38 
44  protected $additionalHeaders = [];
45 
54  public function checkLink($url, $softRefEntry, $reference)
55  {
56  $errorParams = [];
57  $isValidUrl = true;
58  if (isset($this->urlReports[$url])) {
59  if (!$this->urlReports[$url]) {
60  if (is_array($this->urlErrorParams[$url])) {
61  $this->setErrorParams($this->urlErrorParams[$url]);
62  }
63  }
64  return $this->urlReports[$url];
65  }
66  $config = [
67  'follow_redirects' => true,
68  'strict_redirects' => true
69  ];
71  $request = GeneralUtility::makeInstance(HttpRequest::class, $url, 'HEAD', $config);
72  // Observe cookies
73  $request->setCookieJar(true);
74  try {
76  $response = $request->send();
77  $status = isset($response) ? $response->getStatus() : 0;
78  // HEAD was not allowed or threw an error, now trying GET
79  if ($status >= 400) {
80  $request->setMethod('GET');
81  $request->setHeader('Range', 'bytes = 0 - 4048');
83  $response = $request->send();
84  }
85  } catch (\Exception $e) {
86  $isValidUrl = false;
87  // A redirect loop occurred
88  if ($e->getCode() === 40) {
89  $traceUrl = $request->getUrl()->getURL();
91  $event = $request->getLastEvent();
92  if ($event['data'] instanceof \HTTP_Request2_Response) {
93  $traceCode = $event['data']->getStatus();
94  } else {
95  $traceCode = 'loop';
96  }
97  $errorParams['errorType'] = 'loop';
98  $errorParams['location'] = $traceUrl;
99  $errorParams['errorCode'] = $traceCode;
100  } else {
101  $errorParams['errorType'] = 'exception';
102  }
103  $errorParams['message'] = $e->getMessage();
104  }
105  $status = isset($response) ? $response->getStatus() : 0;
106  if ($status >= 300) {
107  $isValidUrl = false;
108  $errorParams['errorType'] = $status;
109  $errorParams['message'] = $response->getReasonPhrase();
110  }
111  if (!$isValidUrl) {
113  }
114  $this->urlReports[$url] = $isValidUrl;
115  $this->urlErrorParams[$url] = $errorParams;
116  return $isValidUrl;
117  }
118 
125  public function getErrorMessage($errorParams)
126  {
127  $lang = $this->getLanguageService();
128  $errorType = $errorParams['errorType'];
129  switch ($errorType) {
130  case 300:
131  $response = sprintf($lang->getLL('list.report.externalerror'), $errorType);
132  break;
133  case 403:
134  $response = $lang->getLL('list.report.pageforbidden403');
135  break;
136  case 404:
137  $response = $lang->getLL('list.report.pagenotfound404');
138  break;
139  case 500:
140  $response = $lang->getLL('list.report.internalerror500');
141  break;
142  case 'loop':
143  $response = sprintf($lang->getLL('list.report.redirectloop'), $errorParams['errorCode'], $errorParams['location']);
144  break;
145  case 'exception':
146  $response = sprintf($lang->getLL('list.report.httpexception'), $errorParams['message']);
147  break;
148  default:
149  $response = sprintf($lang->getLL('list.report.otherhttpcode'), $errorType, $errorParams['message']);
150  }
151  return $response;
152  }
153 
162  public function fetchType($value, $type, $key)
163  {
164  preg_match_all('/((?:http|https))(?::\\/\\/)(?:[^\\s<>]+)/i', $value['tokenValue'], $urls, PREG_PATTERN_ORDER);
165  if (!empty($urls[0][0])) {
166  $type = 'external';
167  }
168  return $type;
169  }
170 }
checkLink($url, $softRefEntry, $reference)