[BUGFIX] Skip link validation of RTE filelink
[Packages/TYPO3.CMS.git] / typo3 / sysext / linkvalidator / Classes / Linktype / InternalLinktype.php
1 <?php
2 namespace TYPO3\CMS\Linkvalidator\Linktype;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2005 - 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 Internal Links plugin implementation
30 *
31 * @author Dimitri K├Ânig <dk@cabag.ch>
32 * @author Michael Miousse <michael.miousse@infoglobe.ca>
33 */
34 class InternalLinktype extends \TYPO3\CMS\Linkvalidator\Linktype\AbstractLinktype {
35
36 /**
37 * @var string
38 */
39 const DELETED = 'deleted';
40
41 /**
42 * @var string
43 */
44 const HIDDEN = 'hidden';
45
46 /**
47 * @var string
48 */
49 const MOVED = 'moved';
50
51 /**
52 * @var string
53 */
54 const NOTEXISTING = 'notExisting';
55
56 /**
57 * All parameters needed for rendering the error message
58 *
59 * @var array
60 */
61 protected $errorParams = array();
62
63 /**
64 * Result of the check, if the current page uid is valid or not
65 *
66 * @var boolean
67 */
68 protected $responsePage = TRUE;
69
70 /**
71 * Result of the check, if the current content uid is valid or not
72 *
73 * @var boolean
74 */
75 protected $responseContent = TRUE;
76
77 /**
78 * Checks a given URL + /path/filename.ext for validity
79 *
80 * @param string $url Url to check as page-id or page-id#anchor (if anchor is present)
81 * @param array $softRefEntry: The soft reference entry which builds the context of that url
82 * @param \TYPO3\CMS\Linkvalidator\LinkAnalyzer $reference Parent instance of tx_linkvalidator_Processor
83 * @return boolean TRUE on success or FALSE on error
84 */
85 public function checkLink($url, $softRefEntry, $reference) {
86 $anchor = '';
87 $this->responseContent = TRUE;
88 // Might already contain values - empty it
89 unset($this->errorParams);
90 // Ignore FAL file references
91 if (substr($url, 0, 5) === 'file:') {
92 // TODO: validate this resource file
93 return TRUE;
94 }
95 // Defines the linked page and anchor (if any).
96 if (strpos($url, '#c') !== FALSE) {
97 $parts = explode('#c', $url);
98 $page = $parts[0];
99 $anchor = $parts[1];
100 } else {
101 $page = $url;
102 }
103 // Check if the linked page is OK
104 $this->responsePage = $this->checkPage($page);
105 // Check if the linked content element is OK
106 if ($anchor) {
107 // Check if the content element is OK
108 $this->responseContent = $this->checkContent($page, $anchor);
109 }
110 if (is_array($this->errorParams['page']) && !$this->responsePage
111 || is_array($this->errorParams['content']) && !$this->responseContent
112 ) {
113 $this->setErrorParams($this->errorParams);
114 }
115 if ($this->responsePage === TRUE && $this->responseContent === TRUE) {
116 $response = TRUE;
117 } else {
118 $response = FALSE;
119 }
120 return $response;
121 }
122
123 /**
124 * Checks a given page uid for validity
125 *
126 * @param string $page Page uid to check
127 * @return boolean TRUE on success or FALSE on error
128 */
129 protected function checkPage($page) {
130 $row = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('uid, title, deleted, hidden, starttime, endtime', 'pages', 'uid = ' . intval($page));
131 $this->responsePage = TRUE;
132 if ($row) {
133 if ($row['deleted'] == '1') {
134 $this->errorParams['errorType']['page'] = self::DELETED;
135 $this->errorParams['page']['title'] = $row['title'];
136 $this->errorParams['page']['uid'] = $row['uid'];
137 $this->responsePage = FALSE;
138 } elseif ($row['hidden'] == '1' || $GLOBALS['EXEC_TIME'] < intval($row['starttime']) || $row['endtime'] && intval($row['endtime']) < $GLOBALS['EXEC_TIME']) {
139 $this->errorParams['errorType']['page'] = self::HIDDEN;
140 $this->errorParams['page']['title'] = $row['title'];
141 $this->errorParams['page']['uid'] = $row['uid'];
142 $this->responsePage = FALSE;
143 }
144 } else {
145 $this->errorParams['errorType']['page'] = self::NOTEXISTING;
146 $this->errorParams['page']['uid'] = intval($page);
147 $this->responsePage = FALSE;
148 }
149 return $this->responsePage;
150 }
151
152 /**
153 * Checks a given content uid for validity
154 *
155 * @param string $page Uid of the page to which the link is pointing
156 * @param string $anchor Uid of the content element to check
157 * @return boolean TRUE on success or FALSE on error
158 */
159 protected function checkContent($page, $anchor) {
160 // Get page ID on which the content element in fact is located
161 $res = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('uid, pid, header, deleted, hidden, starttime, endtime', 'tt_content', 'uid = ' . intval($anchor));
162 $this->responseContent = TRUE;
163 // this content element exists
164 if ($res) {
165 // page ID on which this CE is in fact located.
166 $correctPageID = $res['pid'];
167 // Check if the element is on the linked page
168 // (The element might have been moved to another page)
169 if (!($correctPageID === $page)) {
170 $this->errorParams['errorType']['content'] = self::MOVED;
171 $this->errorParams['content']['uid'] = intval($anchor);
172 $this->errorParams['content']['wrongPage'] = intval($page);
173 $this->errorParams['content']['rightPage'] = intval($correctPageID);
174 $this->responseContent = FALSE;
175 } else {
176 // The element is located on the page to which the link is pointing
177 if ($res['deleted'] == '1') {
178 $this->errorParams['errorType']['content'] = self::DELETED;
179 $this->errorParams['content']['title'] = $res['header'];
180 $this->errorParams['content']['uid'] = $res['uid'];
181 $this->responseContent = FALSE;
182 } elseif ($res['hidden'] == '1' || $GLOBALS['EXEC_TIME'] < intval($res['starttime']) || $res['endtime'] && intval($res['endtime']) < $GLOBALS['EXEC_TIME']) {
183 $this->errorParams['errorType']['content'] = self::HIDDEN;
184 $this->errorParams['content']['title'] = $res['header'];
185 $this->errorParams['content']['uid'] = $res['uid'];
186 $this->responseContent = FALSE;
187 }
188 }
189 } else {
190 // The content element does not exist
191 $this->errorParams['errorType']['content'] = self::NOTEXISTING;
192 $this->errorParams['content']['uid'] = intval($anchor);
193 $this->responseContent = FALSE;
194 }
195 return $this->responseContent;
196 }
197
198 /**
199 * Generates the localized error message from the error params saved from the parsing
200 *
201 * @param array $errorParams All parameters needed for the rendering of the error message
202 * @return string Validation error message
203 */
204 public function getErrorMessage($errorParams) {
205 $errorType = $errorParams['errorType'];
206 if (is_array($errorParams['page'])) {
207 switch ($errorType['page']) {
208 case self::DELETED:
209 $errorPage = $GLOBALS['LANG']->getLL('list.report.pagedeleted');
210 $errorPage = str_replace('###title###', $errorParams['page']['title'], $errorPage);
211 $errorPage = str_replace('###uid###', $errorParams['page']['uid'], $errorPage);
212 break;
213 case self::HIDDEN:
214 $errorPage = $GLOBALS['LANG']->getLL('list.report.pagenotvisible');
215 $errorPage = str_replace('###title###', $errorParams['page']['title'], $errorPage);
216 $errorPage = str_replace('###uid###', $errorParams['page']['uid'], $errorPage);
217 break;
218 default:
219 $errorPage = $GLOBALS['LANG']->getLL('list.report.pagenotexisting');
220 $errorPage = str_replace('###uid###', $errorParams['page']['uid'], $errorPage);
221 }
222 }
223 if (is_array($errorParams['content'])) {
224 switch ($errorType['content']) {
225 case self::DELETED:
226 $errorContent = $GLOBALS['LANG']->getLL('list.report.contentdeleted');
227 $errorContent = str_replace('###title###', $errorParams['content']['title'], $errorContent);
228 $errorContent = str_replace('###uid###', $errorParams['content']['uid'], $errorContent);
229 break;
230 case self::HIDDEN:
231 $errorContent = $GLOBALS['LANG']->getLL('list.report.contentnotvisible');
232 $errorContent = str_replace('###title###', $errorParams['content']['title'], $errorContent);
233 $errorContent = str_replace('###uid###', $errorParams['content']['uid'], $errorContent);
234 break;
235 case self::MOVED:
236 $errorContent = $GLOBALS['LANG']->getLL('list.report.contentmoved');
237 $errorContent = str_replace('###title###', $errorParams['content']['title'], $errorContent);
238 $errorContent = str_replace('###uid###', $errorParams['content']['uid'], $errorContent);
239 $errorContent = str_replace('###wrongpage###', $errorParams['content']['wrongPage'], $errorContent);
240 $errorContent = str_replace('###rightpage###', $errorParams['content']['rightPage'], $errorContent);
241 break;
242 default:
243 $errorContent = $GLOBALS['LANG']->getLL('list.report.contentnotexisting');
244 $errorContent = str_replace('###uid###', $errorParams['content']['uid'], $errorContent);
245 }
246 }
247 if (isset($errorPage) && isset($errorContent)) {
248 $response = $errorPage . '<br />' . $errorContent;
249 } elseif (isset($errorPage)) {
250 $response = $errorPage;
251 } elseif (isset($errorContent)) {
252 $response = $errorContent;
253 } else {
254 // This should not happen
255 $response = $GLOBALS['LANG']->getLL('list.report.noinformation');
256 }
257 return $response;
258 }
259
260 /**
261 * Constructs a valid Url for browser output
262 *
263 * @param array $row Broken link record
264 * @return string Parsed broken url
265 */
266 public function getBrokenUrl($row) {
267 $domain = rtrim(\TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL'), '/');
268 $rootLine = \TYPO3\CMS\Backend\Utility\BackendUtility::BEgetRootLine($row['record_pid']);
269 // checks alternate domains
270 if (count($rootLine) > 0) {
271 $protocol = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SSL') ? 'https://' : 'http://';
272 $domainRecord = \TYPO3\CMS\Backend\Utility\BackendUtility::firstDomainRecord($rootLine);
273 if (!empty($domainRecord)) {
274 $domain = $protocol . $domainRecord;
275 }
276 }
277 return $domain . '/index.php?id=' . $row['url'];
278 }
279 }
280 ?>