TYPO3 CMS  TYPO3_6-2
ExternalLinktype.php
Go to the documentation of this file.
1 <?php
3 
25 
31  protected $urlReports = array();
32 
38  protected $urlErrorParams = array();
39 
45  protected $additionalHeaders = array();
46 
55  public function checkLink($url, $softRefEntry, $reference) {
56  $errorParams = array();
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 = array(
67  'follow_redirects' => TRUE,
68  'strict_redirects' => TRUE
69  );
71  $request = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Http\\HttpRequest', $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  $errorType = $errorParams['errorType'];
127  switch ($errorType) {
128  case 300:
129  $response = sprintf($GLOBALS['LANG']->getLL('list.report.externalerror'), $errorType);
130  break;
131  case 403:
132  $response = $GLOBALS['LANG']->getLL('list.report.pageforbidden403');
133  break;
134  case 404:
135  $response = $GLOBALS['LANG']->getLL('list.report.pagenotfound404');
136  break;
137  case 500:
138  $response = $GLOBALS['LANG']->getLL('list.report.internalerror500');
139  break;
140  case 'loop':
141  $response = sprintf($GLOBALS['LANG']->getLL('list.report.redirectloop'), $errorParams['errorCode'], $errorParams['location']);
142  break;
143  case 'exception':
144  $response = sprintf($GLOBALS['LANG']->getLL('list.report.httpexception'), $errorParams['message']);
145  break;
146  default:
147  $response = sprintf($GLOBALS['LANG']->getLL('list.report.otherhttpcode'), $errorType, $errorParams['message']);
148  }
149  return $response;
150  }
151 
160  public function fetchType($value, $type, $key) {
161  preg_match_all('/((?:http|https))(?::\\/\\/)(?:[^\\s<>]+)/i', $value['tokenValue'], $urls, PREG_PATTERN_ORDER);
162  if (!empty($urls[0][0])) {
163  $type = 'external';
164  }
165  return $type;
166  }
167 
168 }
checkLink($url, $softRefEntry, $reference)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]