[TASK] Use arrays in str_replace() calls
[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
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 = str_replace(
210 array(
211 '###title###',
212 '###uid###'
213 ),
214 array(
215 $errorParams['page']['title'],
216 $errorParams['page']['uid']
217 ),
218 $GLOBALS['LANG']->getLL('list.report.pagedeleted')
219 );
220 break;
221 case self::HIDDEN:
222 $errorPage = str_replace(
223 array(
224 '###title###',
225 '###uid###'
226 ),
227 array(
228 $errorParams['page']['title'],
229 $errorParams['page']['uid']
230 ),
231 $GLOBALS['LANG']->getLL('list.report.pagenotvisible')
232 );
233 break;
234 default:
235 $errorPage = str_replace(
236 '###uid###',
237 $errorParams['page']['uid'],
238 $GLOBALS['LANG']->getLL('list.report.pagenotexisting')
239 );
240 }
241 }
242 if (is_array($errorParams['content'])) {
243 switch ($errorType['content']) {
244 case self::DELETED:
245 $errorContent = str_replace(
246 array(
247 '###title###',
248 '###uid###'
249 ),
250 array(
251 $errorParams['content']['title'],
252 $errorParams['content']['uid']
253 ),
254 $GLOBALS['LANG']->getLL('list.report.contentdeleted')
255 );
256 break;
257 case self::HIDDEN:
258 $errorContent = str_replace(
259 array(
260 '###title###',
261 '###uid###'
262 ),
263 array(
264 $errorParams['content']['title'],
265 $errorParams['content']['uid']
266 ),
267 $GLOBALS['LANG']->getLL('list.report.contentnotvisible')
268 );
269 break;
270 case self::MOVED:
271 $errorContent = str_replace(
272 array(
273 '###title###',
274 '###uid###',
275 '###wrongpage###',
276 '###rightpage###'
277 ),
278 array(
279 $errorParams['content']['title'],
280 $errorParams['content']['uid'],
281 $errorParams['content']['wrongPage'],
282 $errorParams['content']['rightPage']
283 ),
284 $GLOBALS['LANG']->getLL('list.report.contentmoved')
285 );
286 break;
287 default:
288 $errorContent = str_replace('###uid###', $errorParams['content']['uid'], $GLOBALS['LANG']->getLL('list.report.contentnotexisting'));
289 }
290 }
291 if (isset($errorPage) && isset($errorContent)) {
292 $response = $errorPage . '<br />' . $errorContent;
293 } elseif (isset($errorPage)) {
294 $response = $errorPage;
295 } elseif (isset($errorContent)) {
296 $response = $errorContent;
297 } else {
298 // This should not happen
299 $response = $GLOBALS['LANG']->getLL('list.report.noinformation');
300 }
301 return $response;
302 }
303
304 /**
305 * Constructs a valid Url for browser output
306 *
307 * @param array $row Broken link record
308 * @return string Parsed broken url
309 */
310 public function getBrokenUrl($row) {
311 $domain = rtrim(\TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL'), '/');
312 $rootLine = \TYPO3\CMS\Backend\Utility\BackendUtility::BEgetRootLine($row['record_pid']);
313 // checks alternate domains
314 if (count($rootLine) > 0) {
315 $protocol = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SSL') ? 'https://' : 'http://';
316 $domainRecord = \TYPO3\CMS\Backend\Utility\BackendUtility::firstDomainRecord($rootLine);
317 if (!empty($domainRecord)) {
318 $domain = $protocol . $domainRecord;
319 }
320 }
321 return $domain . '/index.php?id=' . $row['url'];
322 }
323 }