[TASK] Cleanup character after PHP closing tag
[Packages/TYPO3.CMS.git] / typo3 / sysext / linkvalidator / Classes / Linktype / ExternalLinktype.php
1 <?php
2 namespace TYPO3\CMS\Linkvalidator\Linktype;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010 - 2009 Jochen Rieger (j.rieger@connecta.ag)
8 * (c) 2010 - 2011 Michael Miousse (michael.miousse@infoglobe.ca)
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * This class provides Check External Links plugin implementation
29 *
30 * @author Dimitri K├Ânig <dk@cabag.ch>
31 * @author Michael Miousse <michael.miousse@infoglobe.ca>
32 * @author Philipp Gampe <typo3.dev@philippgampe.info>
33 * @package TYPO3
34 * @subpackage linkvalidator
35 */
36 class ExternalLinktype extends \TYPO3\CMS\Linkvalidator\Linktype\AbstractLinktype {
37
38 /**
39 * Cached list of the URLs, which were already checked for the current processing
40 *
41 * @var array $urlReports
42 */
43 protected $urlReports = array();
44
45 /**
46 * Cached list of all error parameters of the URLs, which were already checked for the current processing
47 *
48 * @var array $urlErrorParams
49 */
50 protected $urlErrorParams = array();
51
52 /**
53 * List of headers to be used for matching an URL for the current processing
54 *
55 * @var array $additionalHeaders
56 */
57 protected $additionalHeaders = array();
58
59 /**
60 * Checks a given URL for validity
61 *
62 * @param string $url The URL to check
63 * @param array $softRefEntry The soft reference entry which builds the context of that URL
64 * @param \TYPO3\CMS\Linkvalidator\LinkAnalyzer $reference Parent instance of tx_linkvalidator_Processor
65 * @return boolean TRUE on success or FALSE on error
66 */
67 public function checkLink($url, $softRefEntry, $reference) {
68 $errorParams = array();
69 $isValidUrl = TRUE;
70 if (isset($this->urlReports[$url])) {
71 if (!$this->urlReports[$url]) {
72 if (is_array($this->urlErrorParams[$url])) {
73 $this->setErrorParams($this->urlErrorParams[$url]);
74 }
75 }
76 return $this->urlReports[$url];
77 }
78 $config = array(
79 'follow_redirects' => TRUE,
80 'strict_redirects' => TRUE
81 );
82 /** @var t3lib_http_Request|HTTP_Request2 $request */
83 $request = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Http\\HttpRequest', $url, 'HEAD', $config);
84 // Observe cookies
85 $request->setCookieJar(TRUE);
86 try {
87 /** @var HTTP_Request2_Response $response */
88 $response = $request->send();
89 // HEAD was not allowed, now trying GET
90 if (isset($response) && $response->getStatus() === 405) {
91 $request->setMethod('GET');
92 $request->setHeader('Range', 'bytes = 0 - 4048');
93 /** @var HTTP_Request2_Response $response */
94 $response = $request->send();
95 }
96 } catch (\Exception $e) {
97 $isValidUrl = FALSE;
98 // A redirect loop occurred
99 if ($e->getCode() === 40) {
100 // Parse the exception for more information
101 $trace = $e->getTrace();
102 $traceUrl = $trace[0]['args'][0]->getUrl()->getUrl();
103 $traceCode = $trace[0]['args'][1]->getStatus();
104 $errorParams['errorType'] = 'loop';
105 $errorParams['location'] = $traceUrl;
106 $errorParams['errorCode'] = $traceCode;
107 } else {
108 $errorParams['errorType'] = 'exception';
109 }
110 $errorParams['message'] = $e->getMessage();
111 }
112 if (isset($response) && $response->getStatus() >= 300) {
113 $isValidUrl = FALSE;
114 $errorParams['errorType'] = $response->getStatus();
115 $errorParams['message'] = $response->getReasonPhrase();
116 }
117 if (!$isValidUrl) {
118 $this->setErrorParams($errorParams);
119 }
120 $this->urlReports[$url] = $isValidUrl;
121 $this->urlErrorParams[$url] = $errorParams;
122 return $isValidUrl;
123 }
124
125 /**
126 * Generate the localized error message from the error params saved from the parsing
127 *
128 * @param array $errorParams All parameters needed for the rendering of the error message
129 * @return string Validation error message
130 */
131 public function getErrorMessage($errorParams) {
132 $errorType = $errorParams['errorType'];
133 switch ($errorType) {
134 case 300:
135 $response = sprintf($GLOBALS['LANG']->getLL('list.report.externalerror'), $errorType);
136 break;
137 case 403:
138 $response = $GLOBALS['LANG']->getLL('list.report.pageforbidden403');
139 break;
140 case 404:
141 $response = $GLOBALS['LANG']->getLL('list.report.pagenotfound404');
142 break;
143 case 500:
144 $response = $GLOBALS['LANG']->getLL('list.report.internalerror500');
145 break;
146 case 'loop':
147 $response = sprintf($GLOBALS['LANG']->getLL('list.report.redirectloop'), $errorParams['errorCode'], $errorParams['location']);
148 break;
149 case 'exception':
150 $response = sprintf($GLOBALS['LANG']->getLL('list.report.httpexception'), $errorParams['message']);
151 break;
152 default:
153 $response = sprintf($GLOBALS['LANG']->getLL('list.report.otherhttpcode'), $errorType, $errorParams['message']);
154 }
155 return $response;
156 }
157
158 /**
159 * Get the external type from the softRefParserObj result
160 *
161 * @param array $value Reference properties
162 * @param string $type Current type
163 * @param string $key Validator hook name
164 * @return string Fetched type
165 */
166 public function fetchType($value, $type, $key) {
167 preg_match_all('/((?:http|https))(?::\\/\\/)(?:[^\\s<>]+)/i', $value['tokenValue'], $urls, PREG_PATTERN_ORDER);
168 if (!empty($urls[0][0])) {
169 $type = 'external';
170 }
171 return $type;
172 }
173
174 }
175 ?>