TYPO3  7.6
InternalLinktype.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Linkvalidator\Linktype;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
19 
24 {
28  const DELETED = 'deleted';
29 
33  const HIDDEN = 'hidden';
34 
38  const MOVED = 'moved';
39 
43  const NOTEXISTING = 'notExisting';
44 
50  protected $responsePage = true;
51 
57  protected $responseContent = true;
58 
67  public function checkLink($url, $softRefEntry, $reference)
68  {
69  $anchor = '';
70  $this->responseContent = true;
71  // Might already contain values - empty it
72  unset($this->errorParams);
73  // Only check pages records. Content elements will also be checked
74  // as we extract the anchor in the next step.
75  if (strpos($softRefEntry['substr']['recordRef'], 'pages:') !== 0) {
76  return true;
77  }
78  // Defines the linked page and anchor (if any).
79  if (strpos($url, '#c') !== false) {
80  $parts = explode('#c', $url);
81  $page = $parts[0];
82  $anchor = $parts[1];
83  } else {
84  $page = $url;
85  }
86  // Check if the linked page is OK
87  $this->responsePage = $this->checkPage($page);
88  // Check if the linked content element is OK
89  if ($anchor) {
90  // Check if the content element is OK
91  $this->responseContent = $this->checkContent($page, $anchor);
92  }
93  if (
94  is_array($this->errorParams['page']) && !$this->responsePage
95  || is_array($this->errorParams['content']) && !$this->responseContent
96  ) {
97  $this->setErrorParams($this->errorParams);
98  }
99 
100  return ($this->responsePage && $this->responseContent);
101  }
102 
109  protected function checkPage($page)
110  {
111  $row = $this->getDatabaseConnection()->exec_SELECTgetSingleRow('uid, title, deleted, hidden, starttime, endtime', 'pages', 'uid = ' . (int)$page);
112  $this->responsePage = true;
113  if ($row) {
114  if ($row['deleted'] == '1') {
115  $this->errorParams['errorType']['page'] = self::DELETED;
116  $this->errorParams['page']['title'] = $row['title'];
117  $this->errorParams['page']['uid'] = $row['uid'];
118  $this->responsePage = false;
119  } elseif ($row['hidden'] == '1' || $GLOBALS['EXEC_TIME'] < (int)$row['starttime'] || $row['endtime'] && (int)$row['endtime'] < $GLOBALS['EXEC_TIME']) {
120  $this->errorParams['errorType']['page'] = self::HIDDEN;
121  $this->errorParams['page']['title'] = $row['title'];
122  $this->errorParams['page']['uid'] = $row['uid'];
123  $this->responsePage = false;
124  }
125  } else {
126  $this->errorParams['errorType']['page'] = self::NOTEXISTING;
127  $this->errorParams['page']['uid'] = (int)$page;
128  $this->responsePage = false;
129  }
130  return $this->responsePage;
131  }
132 
140  protected function checkContent($page, $anchor)
141  {
142  // Get page ID on which the content element in fact is located
143  $res = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
144  'uid, pid, header, deleted, hidden, starttime, endtime',
145  'tt_content',
146  'uid = ' . (int)$anchor
147  );
148  $this->responseContent = true;
149  // this content element exists
150  if ($res) {
151  // page ID on which this CE is in fact located.
152  $correctPageID = $res['pid'];
153  // Check if the element is on the linked page
154  // (The element might have been moved to another page)
155  if (!($correctPageID === $page)) {
156  $this->errorParams['errorType']['content'] = self::MOVED;
157  $this->errorParams['content']['uid'] = (int)$anchor;
158  $this->errorParams['content']['wrongPage'] = (int)$page;
159  $this->errorParams['content']['rightPage'] = (int)$correctPageID;
160  $this->responseContent = false;
161  } else {
162  // The element is located on the page to which the link is pointing
163  if ($res['deleted'] == '1') {
164  $this->errorParams['errorType']['content'] = self::DELETED;
165  $this->errorParams['content']['title'] = $res['header'];
166  $this->errorParams['content']['uid'] = $res['uid'];
167  $this->responseContent = false;
168  } elseif ($res['hidden'] == '1' || $GLOBALS['EXEC_TIME'] < (int)$res['starttime'] || $res['endtime'] && (int)$res['endtime'] < $GLOBALS['EXEC_TIME']) {
169  $this->errorParams['errorType']['content'] = self::HIDDEN;
170  $this->errorParams['content']['title'] = $res['header'];
171  $this->errorParams['content']['uid'] = $res['uid'];
172  $this->responseContent = false;
173  }
174  }
175  } else {
176  // The content element does not exist
177  $this->errorParams['errorType']['content'] = self::NOTEXISTING;
178  $this->errorParams['content']['uid'] = (int)$anchor;
179  $this->responseContent = false;
180  }
181  return $this->responseContent;
182  }
183 
190  public function getErrorMessage($errorParams)
191  {
192  $lang = $this->getLanguageService();
193  $errorType = $errorParams['errorType'];
194  if (is_array($errorParams['page'])) {
195  switch ($errorType['page']) {
196  case self::DELETED:
197  $errorPage = str_replace(
198  array(
199  '###title###',
200  '###uid###'
201  ),
202  array(
203  $errorParams['page']['title'],
204  $errorParams['page']['uid']
205  ),
206  $lang->getLL('list.report.pagedeleted')
207  );
208  break;
209  case self::HIDDEN:
210  $errorPage = str_replace(
211  array(
212  '###title###',
213  '###uid###'
214  ),
215  array(
216  $errorParams['page']['title'],
217  $errorParams['page']['uid']
218  ),
219  $lang->getLL('list.report.pagenotvisible')
220  );
221  break;
222  default:
223  $errorPage = str_replace(
224  '###uid###',
225  $errorParams['page']['uid'],
226  $lang->getLL('list.report.pagenotexisting')
227  );
228  }
229  }
230  if (is_array($errorParams['content'])) {
231  switch ($errorType['content']) {
232  case self::DELETED:
233  $errorContent = str_replace(
234  array(
235  '###title###',
236  '###uid###'
237  ),
238  array(
239  $errorParams['content']['title'],
240  $errorParams['content']['uid']
241  ),
242  $lang->getLL('list.report.contentdeleted')
243  );
244  break;
245  case self::HIDDEN:
246  $errorContent = str_replace(
247  array(
248  '###title###',
249  '###uid###'
250  ),
251  array(
252  $errorParams['content']['title'],
253  $errorParams['content']['uid']
254  ),
255  $lang->getLL('list.report.contentnotvisible')
256  );
257  break;
258  case self::MOVED:
259  $errorContent = str_replace(
260  array(
261  '###title###',
262  '###uid###',
263  '###wrongpage###',
264  '###rightpage###'
265  ),
266  array(
267  $errorParams['content']['title'],
268  $errorParams['content']['uid'],
269  $errorParams['content']['wrongPage'],
270  $errorParams['content']['rightPage']
271  ),
272  $lang->getLL('list.report.contentmoved')
273  );
274  break;
275  default:
276  $errorContent = str_replace('###uid###', $errorParams['content']['uid'], $lang->getLL('list.report.contentnotexisting'));
277  }
278  }
279  if (isset($errorPage) && isset($errorContent)) {
280  $response = $errorPage . '<br />' . $errorContent;
281  } elseif (isset($errorPage)) {
282  $response = $errorPage;
283  } elseif (isset($errorContent)) {
284  $response = $errorContent;
285  } else {
286  // This should not happen
287  $response = $lang->getLL('list.report.noinformation');
288  }
289  return $response;
290  }
291 
298  public function getBrokenUrl($row)
299  {
300  $domain = rtrim(GeneralUtility::getIndpEnv('TYPO3_SITE_URL'), '/');
301  $rootLine = BackendUtility::BEgetRootLine($row['record_pid']);
302  // checks alternate domains
303  if (!empty($rootLine)) {
304  $protocol = GeneralUtility::getIndpEnv('TYPO3_SSL') ? 'https://' : 'http://';
305  $domainRecord = BackendUtility::firstDomainRecord($rootLine);
306  if (!empty($domainRecord)) {
307  $domain = $protocol . $domainRecord;
308  }
309  }
310  return $domain . '/index.php?id=' . $row['url'];
311  }
312 }