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