e004677ad80d2847f2f8a2c20dc19467e54d2a36
[Packages/TYPO3.CMS.git] / typo3 / sysext / linkvalidator / classes / linktype / class.tx_linkvalidator_linktype_internal.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2005 - 2010 Jochen Rieger (j.rieger@connecta.ag)
6 * (c) 2010 - 2011 Michael Miousse (michael.miousse@infoglobe.ca)
7 * All rights reserved
8 *
9 * This script is part of the TYPO3 project. The TYPO3 project is
10 * free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * The GNU General Public License can be found at
16 * http://www.gnu.org/copyleft/gpl.html.
17 *
18 * This script is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * This copyright notice MUST APPEAR in all copies of the script!
24 ***************************************************************/
25 /**
26 * This class provides Check Internal Links plugin implementation
27 *
28 * @author Dimitri K├Ânig <dk@cabag.ch>
29 * @author Michael Miousse <michael.miousse@infoglobe.ca>
30 * @package TYPO3
31 * @subpackage linkvalidator
32 */
33 class tx_linkvalidator_linktype_Internal extends tx_linkvalidator_linktype_Abstract {
34
35 const DELETED = 'deleted';
36 const HIDDEN = 'hidden';
37 const MOVED = 'moved';
38 const NOTEXISTING = 'notExisting';
39
40 /**
41 * All parameters needed for rendering the error message
42 *
43 * @var array
44 */
45 protected $errorParams = array();
46
47 /**
48 * Result of the check, if the current page uid is valid or not
49 *
50 * @var boolean
51 */
52 protected $responsePage = TRUE;
53
54 /**
55 * Result of the check, if the current content uid is valid or not
56 *
57 * @var boolean
58 */
59 protected $responseContent = TRUE;
60
61 /**
62 * Checks a given URL + /path/filename.ext for validity
63 *
64 * @param string $url Url to check as page-id or page-id#anchor (if anchor is present)
65 * @param array $softRefEntry: The soft reference entry which builds the context of that url
66 * @param tx_linkvalidator_Processor $reference Parent instance of tx_linkvalidator_Processor
67 * @return boolean TRUE on success or FALSE on error
68 */
69 public function checkLink($url, $softRefEntry, $reference) {
70 $anchor = '';
71 $this->responseContent = TRUE;
72
73 // Might already contain values - empty it
74 unset($this->errorParams);
75
76 // defines the linked page and anchor (if any).
77 if (strpos($url, '#c') !== FALSE) {
78 $parts = explode('#c', $url);
79 $page = $parts[0];
80 $anchor = $parts[1];
81 } else {
82 $page = $url;
83 }
84
85 // Check if the linked page is OK
86 $this->responsePage = $this->checkPage($page, $softRefEntry, $reference);
87
88 // Check if the linked content element is OK
89 if ($anchor) {
90
91 // Check if the content element is OK
92 $this->responseContent = $this->checkContent($page, $anchor, $softRefEntry, $reference);
93
94 }
95
96 if ((is_array($this->errorParams['page']) && !$this->responsePage)
97 || (is_array($this->errorParams['content']) && !$this->responseContent)) {
98 $this->setErrorParams($this->errorParams);
99 }
100
101 if (($this->responsePage === TRUE) && ($this->responseContent === TRUE)) {
102 $response = TRUE;
103 } else {
104 $response = FALSE;
105 }
106
107 return $response;
108 }
109
110 /**
111 * Checks a given page uid for validity
112 *
113 * @param string $page Page uid to check
114 * @param array $softRefEntry The soft reference entry which builds the context of that url
115 * @param tx_linkvalidator_Processor $reference Parent instance of tx_linkvalidator_Processor
116 * @return boolean TRUE on success or FALSE on error
117 */
118 protected function checkPage($page, $softRefEntry, $reference) {
119 $row = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
120 'uid, title, deleted, hidden, starttime, endtime',
121 'pages',
122 'uid = ' . intval($page)
123 );
124 $this->responsePage = TRUE;
125
126 if ($row) {
127 if ($row['deleted'] == '1') {
128 $this->errorParams['errorType']['page'] = self::DELETED;
129 $this->errorParams['page']['title'] = $row['title'];
130 $this->errorParams['page']['uid'] = $row['uid'];
131 $this->responsePage = FALSE;
132 } elseif ($row['hidden'] == '1'
133 || $GLOBALS['EXEC_TIME'] < intval($row['starttime'])
134 || ($row['endtime'] && intval($row['endtime']) < $GLOBALS['EXEC_TIME'])) {
135 $this->errorParams['errorType']['page'] = self::HIDDEN;
136 $this->errorParams['page']['title'] = $row['title'];
137 $this->errorParams['page']['uid'] = $row['uid'];
138 $this->responsePage = FALSE;
139 }
140 } else {
141 $this->errorParams['errorType']['page'] = self::NOTEXISTING;
142 $this->errorParams['page']['uid'] = intval($page);
143 $this->responsePage = FALSE;
144 }
145
146 return $this->responsePage;
147 }
148
149 /**
150 * Checks a given content uid for validity
151 *
152 * @param string $page Uid of the page to which the link is pointing
153 * @param string $anchor Uid of the content element to check
154 * @param array $softRefEntry The soft reference entry which builds the context of that url
155 * @param tx_linkvalidator_Processor $reference Parent instance of tx_linkvalidator_Processor
156 * @return boolean TRUE on success or FALSE on error
157 */
158 protected function checkContent($page, $anchor, $softRefEntry, $reference) {
159 // Get page ID on which the content element in fact is located
160 $res = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
161 'uid, pid, header, deleted, hidden, starttime, endtime',
162 'tt_content',
163 'uid = ' . intval($anchor)
164 );
165 $this->responseContent = TRUE;
166
167 // this content element exists
168 if ($res) {
169 // page ID on which this CE is in fact located.
170 $correctPageID = $res['pid'];
171
172 // Check if the element is on the linked page
173 // (The element might have been moved to another page)
174 if (!($correctPageID === $page)) {
175 $this->errorParams['errorType']['content'] = self::MOVED;
176 $this->errorParams['content']['uid'] = intval($anchor);
177 $this->errorParams['content']['wrongPage'] = intval($page);
178 $this->errorParams['content']['rightPage'] = intval($correctPageID);
179 $this->responseContent = FALSE;
180
181 } else {
182 // The element is located on the page to which the link is pointing
183 if ($res['deleted'] == '1') {
184 $this->errorParams['errorType']['content'] = self::DELETED;
185 $this->errorParams['content']['title'] = $res['header'];
186 $this->errorParams['content']['uid'] = $res['uid'];
187 $this->responseContent = FALSE;
188 } elseif ($res['hidden'] == '1'
189 || $GLOBALS['EXEC_TIME'] < intval($res['starttime'])
190 || ($res['endtime'] && intval($res['endtime']) < $GLOBALS['EXEC_TIME'])) {
191 $this->errorParams['errorType']['content'] = self::HIDDEN;
192 $this->errorParams['content']['title'] = $res['header'];
193 $this->errorParams['content']['uid'] = $res['uid'];
194 $this->responseContent = FALSE;
195 }
196 }
197
198 } else {
199 // The content element does not exist
200 $this->errorParams['errorType']['content'] = self::NOTEXISTING;
201 $this->errorParams['content']['uid'] = intval($anchor);
202 $this->responseContent = FALSE;
203 }
204
205 return $this->responseContent;
206 }
207
208 /**
209 * Generate the localized error message from the error params saved from the parsing
210 *
211 * @param array $errorParams All parameters needed for the rendering of the error message
212 * @return string Validation error message
213 */
214 public function getErrorMessage($errorParams) {
215 $errorType = $errorParams['errorType'];
216
217 if (is_array($errorParams['page'])) {
218 switch ($errorType['page']) {
219 case self::DELETED:
220 $errorPage = $GLOBALS['LANG']->getLL('list.report.pagedeleted');
221 $errorPage = str_replace('###title###', $errorParams['page']['title'], $errorPage);
222 $errorPage = str_replace('###uid###', $errorParams['page']['uid'], $errorPage);
223 break;
224
225 case self::HIDDEN:
226 $errorPage = $GLOBALS['LANG']->getLL('list.report.pagenotvisible');
227 $errorPage = str_replace('###title###', $errorParams['page']['title'], $errorPage);
228 $errorPage = str_replace('###uid###', $errorParams['page']['uid'], $errorPage);
229 break;
230
231 default:
232 $errorPage = $GLOBALS['LANG']->getLL('list.report.pagenotexisting');
233 $errorPage = str_replace('###uid###', $errorParams['page']['uid'], $errorPage);
234 }
235 }
236
237 if (is_array($errorParams['content'])) {
238 switch ($errorType['content']) {
239 case self::DELETED:
240 $errorContent = $GLOBALS['LANG']->getLL('list.report.contentdeleted');
241 $errorContent = str_replace('###title###', $errorParams['content']['title'], $errorContent);
242 $errorContent = str_replace('###uid###', $errorParams['content']['uid'], $errorContent);
243 break;
244
245 case self::HIDDEN:
246 $errorContent = $GLOBALS['LANG']->getLL('list.report.contentnotvisible');
247 $errorContent = str_replace('###title###', $errorParams['content']['title'], $errorContent);
248 $errorContent = str_replace('###uid###', $errorParams['content']['uid'], $errorContent);
249 break;
250
251 case self::MOVED:
252 $errorContent = $GLOBALS['LANG']->getLL('list.report.contentmoved');
253 $errorContent = str_replace('###title###', $errorParams['content']['title'], $errorContent);
254 $errorContent = str_replace('###uid###', $errorParams['content']['uid'], $errorContent);
255 $errorContent = str_replace('###wrongpage###', $errorParams['content']['wrongPage'], $errorContent);
256 $errorContent = str_replace('###rightpage###', $errorParams['content']['rightPage'], $errorContent);
257 break;
258
259 default:
260 $errorContent = $GLOBALS['LANG']->getLL('list.report.contentnotexisting');
261 $errorContent = str_replace('###uid###', $errorParams['content']['uid'], $errorContent);
262 }
263 }
264
265 if (isset($errorPage) && isset($errorContent)) {
266 $response = $errorPage . '<br />' . $errorContent;
267 } elseif (isset($errorPage)) {
268 $response = $errorPage;
269 } elseif (isset($errorContent)) {
270 $response = $errorContent;
271 } else {
272 // This should not happen
273 $response = $GLOBALS['LANG']->getLL('list.report.noinformation');
274 }
275
276 return $response;
277 }
278
279 /**
280 * Construct a valid Url for browser output
281 *
282 * @param array $row Broken link record
283 * @return string Parsed broken url
284 */
285 public function getBrokenUrl($row) {
286 $domain = rtrim(t3lib_div::getIndpEnv('TYPO3_SITE_URL'), '/');
287 $rootLine = t3lib_BEfunc::BEgetRootLine($row['record_pid']);
288 // checks alternate domains
289 if (count($rootLine) > 0) {
290 $protocol = t3lib_div::getIndpEnv('TYPO3_SSL') ? 'https://' : 'http://';
291 $domainRecord = t3lib_BEfunc::firstDomainRecord($rootLine);
292 if(!empty($domainRecord)) {
293 $domain = $protocol . $domainRecord;
294 }
295 }
296 return $domain . '/index.php?id=' . $row['url'];
297 }
298 }
299
300 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/linkvalidator/classes/linktypes/class.tx_linkvalidator_linktypes_internal.php'])) {
301 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/linkvalidator/classes/linktypes/class.tx_linkvalidator_linktypes_internal.php']);
302 }
303
304 ?>