[TASK] Refactor code to match new structure
[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 - 2013 Jochen Rieger (j.rieger@connecta.ag)
8 * (c) 2010 - 2013 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 /**
29 * This class provides Check External Links plugin implementation
30 *
31 * @author Dimitri K├Ânig <dk@cabag.ch>
32 * @author Michael Miousse <michael.miousse@infoglobe.ca>
33 * @author Philipp Gampe <typo3.dev@philippgampe.info>
34 */
35 class ExternalLinktype extends \TYPO3\CMS\Linkvalidator\Linktype\AbstractLinktype {
36
37 /**
38 * Cached list of the URLs, which were already checked for the current processing
39 *
40 * @var array $urlReports
41 */
42 protected $urlReports = array();
43
44 /**
45 * Cached list of all error parameters of the URLs, which were already checked for the current processing
46 *
47 * @var array $urlErrorParams
48 */
49 protected $urlErrorParams = array();
50
51 /**
52 * List of headers to be used for matching an URL for the current processing
53 *
54 * @var array $additionalHeaders
55 */
56 protected $additionalHeaders = array();
57
58 /**
59 * Checks a given URL for validity
60 *
61 * @param string $url The URL to check
62 * @param array $softRefEntry The soft reference entry which builds the context of that URL
63 * @param \TYPO3\CMS\Linkvalidator\LinkAnalyzer $reference Parent instance of tx_linkvalidator_Processor
64 * @return boolean TRUE on success or FALSE on error
65 */
66 public function checkLink($url, $softRefEntry, $reference) {
67 $errorParams = array();
68 $isValidUrl = TRUE;
69 if (isset($this->urlReports[$url])) {
70 if (!$this->urlReports[$url]) {
71 if (is_array($this->urlErrorParams[$url])) {
72 $this->setErrorParams($this->urlErrorParams[$url]);
73 }
74 }
75 return $this->urlReports[$url];
76 }
77 $config = array(
78 'follow_redirects' => TRUE,
79 'strict_redirects' => TRUE
80 );
81 /** @var $request \TYPO3\CMS\Core\Http\HttpRequest|\HTTP_Request2 */
82 $request = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Http\\HttpRequest', $url, 'HEAD', $config);
83 // Observe cookies
84 $request->setCookieJar(TRUE);
85 try {
86 /** @var $response \HTTP_Request2_Response */
87 $response = $request->send();
88 // HEAD was not allowed, now trying GET
89 if (isset($response) && $response->getStatus() === 405) {
90 $request->setMethod('GET');
91 $request->setHeader('Range', 'bytes = 0 - 4048');
92 /** @var $response \HTTP_Request2_Response */
93 $response = $request->send();
94 }
95 } catch (\Exception $e) {
96 $isValidUrl = FALSE;
97 // A redirect loop occurred
98 if ($e->getCode() === 40) {
99 // Parse the exception for more information
100 $trace = $e->getTrace();
101 $traceUrl = $trace[0]['args'][0]->getUrl()->getUrl();
102 $traceCode = $trace[0]['args'][1]->getStatus();
103 $errorParams['errorType'] = 'loop';
104 $errorParams['location'] = $traceUrl;
105 $errorParams['errorCode'] = $traceCode;
106 } else {
107 $errorParams['errorType'] = 'exception';
108 }
109 $errorParams['message'] = $e->getMessage();
110 }
111 if (isset($response) && $response->getStatus() >= 300) {
112 $isValidUrl = FALSE;
113 $errorParams['errorType'] = $response->getStatus();
114 $errorParams['message'] = $response->getReasonPhrase();
115 }
116 if (!$isValidUrl) {
117 $this->setErrorParams($errorParams);
118 }
119 $this->urlReports[$url] = $isValidUrl;
120 $this->urlErrorParams[$url] = $errorParams;
121 return $isValidUrl;
122 }
123
124 /**
125 * Generate the localized error message from the error params saved from the parsing
126 *
127 * @param array $errorParams All parameters needed for the rendering of the error message
128 * @return string Validation error message
129 */
130 public function getErrorMessage($errorParams) {
131 $errorType = $errorParams['errorType'];
132 switch ($errorType) {
133 case 300:
134 $response = sprintf($GLOBALS['LANG']->getLL('list.report.externalerror'), $errorType);
135 break;
136 case 403:
137 $response = $GLOBALS['LANG']->getLL('list.report.pageforbidden403');
138 break;
139 case 404:
140 $response = $GLOBALS['LANG']->getLL('list.report.pagenotfound404');
141 break;
142 case 500:
143 $response = $GLOBALS['LANG']->getLL('list.report.internalerror500');
144 break;
145 case 'loop':
146 $response = sprintf($GLOBALS['LANG']->getLL('list.report.redirectloop'), $errorParams['errorCode'], $errorParams['location']);
147 break;
148 case 'exception':
149 $response = sprintf($GLOBALS['LANG']->getLL('list.report.httpexception'), $errorParams['message']);
150 break;
151 default:
152 $response = sprintf($GLOBALS['LANG']->getLL('list.report.otherhttpcode'), $errorType, $errorParams['message']);
153 }
154 return $response;
155 }
156
157 /**
158 * Get the external type from the softRefParserObj result
159 *
160 * @param array $value Reference properties
161 * @param string $type Current type
162 * @param string $key Validator hook name
163 * @return string Fetched type
164 */
165 public function fetchType($value, $type, $key) {
166 preg_match_all('/((?:http|https))(?::\\/\\/)(?:[^\\s<>]+)/i', $value['tokenValue'], $urls, PREG_PATTERN_ORDER);
167 if (!empty($urls[0][0])) {
168 $type = 'external';
169 }
170 return $type;
171 }
172
173 }
174 ?>