[FOLLOWUP][TASK] Introduce getter for $GLOBALS['LANG'] in EXT:cshmanual
[Packages/TYPO3.CMS.git] / typo3 / sysext / linkvalidator / Classes / Linktype / LinkHandler.php
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
17 use TYPO3\CMS\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Utility\StringUtility;
19
20 /**
21 * This class provides Check Link Handler plugin implementation
22 *
23 * @author Dimitri K├Ânig <dk@cabag.ch>
24 * @author Michael Miousse <michael.miousse@infoglobe.ca>
25 */
26 class LinkHandler extends AbstractLinktype {
27
28 /**
29 * @var string
30 */
31 const DELETED = 'deleted';
32
33 /**
34 * @var string
35 */
36 const DISABLED = 'disabled';
37
38 /**
39 * If this is TRUE an error will also be reported if the linked record
40 * is disabled. Otherwise the error will only be reported if the
41 * record is deleted or does not exist.
42 *
43 * @var bool
44 */
45 protected $reportHiddenRecords;
46
47 /**
48 * Checks a given URL for validity
49 *
50 * @param string $url Url to check
51 * @param array $softRefEntry The soft reference entry which builds the context of that url
52 * @param \TYPO3\CMS\Linkvalidator\LinkAnalyzer $reference Parent instance
53 * @return bool TRUE on success or FALSE on error
54 */
55 public function checkLink($url, $softRefEntry, $reference) {
56 $response = TRUE;
57 $errorType = '';
58 $errorParams = array();
59 $parts = explode(':', $url);
60 if (count($parts) !== 3) {
61 return $response;
62 }
63
64 $tableName = htmlspecialchars($parts[1]);
65 $rowid = (int)$parts[2];
66 $row = NULL;
67 $tsConfig = $reference->getTSConfig();
68 $reportHiddenRecords = (bool)$tsConfig['linkhandler.']['reportHiddenRecords'];
69
70 // First check, if we find a non disabled record if the check
71 // for hidden records is enabled.
72 if ($reportHiddenRecords) {
73 $row = $this->getRecordRow($tableName, $rowid, 'disabled');
74 if ($row === NULL) {
75 $response = FALSE;
76 $errorType = self::DISABLED;
77 }
78 }
79
80 // If no enabled record was found or we did not check that see
81 // if we can find a non deleted record.
82 if ($row === NULL) {
83 $row = $this->getRecordRow($tableName, $rowid, 'deleted');
84 if ($row === NULL) {
85 $response = FALSE;
86 $errorType = self::DELETED;
87 }
88 }
89
90 // If we did not find a non deleted record, check if we find a
91 // deleted one.
92 if ($row === NULL) {
93 $row = $this->getRecordRow($tableName, $rowid, 'all');
94 if ($row === NULL) {
95 $response = FALSE;
96 $errorType = '';
97 }
98 }
99
100 if (!$response) {
101 $errorParams['errorType'] = $errorType;
102 $errorParams['tablename'] = $tableName;
103 $errorParams['uid'] = $rowid;
104 $this->setErrorParams($errorParams);
105 }
106
107 return $response;
108 }
109
110 /**
111 * Type fetching method, based on the type that softRefParserObj returns
112 *
113 * @param array $value Reference properties
114 * @param string $type Current type
115 * @param string $key Validator hook name
116 * @return string fetched type
117 */
118 public function fetchType($value, $type, $key) {
119 if ($value['type'] === 'string' && StringUtility::beginsWith(strtolower($value['tokenValue']), 'record:')) {
120 $type = 'linkhandler';
121 }
122 return $type;
123 }
124
125 /**
126 * Generate the localized error message from the error params saved from the parsing
127 *
128 * @param array $errorParams All parameters needed for the rendering of the error message
129 * @return string Validation error message
130 */
131 public function getErrorMessage($errorParams) {
132 $errorType = $errorParams['errorType'];
133 $tableName = $errorParams['tablename'];
134 if (!empty($GLOBALS['TCA'][$tableName]['ctrl']['title'])) {
135 $title = $this->getLanguageService()->sL($GLOBALS['TCA'][$tableName]['ctrl']['title'], TRUE);
136 } else {
137 $title = $tableName;
138 }
139 switch ($errorType) {
140 case self::DISABLED:
141 $response = $this->getLanguageService()->getLL('list.report.rownotvisible');
142 $response = str_replace('###title###', $title, $response);
143 $response = str_replace('###uid###', $errorParams['uid'], $response);
144 break;
145 case self::DELETED:
146 $response = str_replace(
147 array(
148 '###title###',
149 '###uid###'
150 ),
151 array(
152 $title,
153 $errorParams['uid']
154 ),
155 $this->getLanguageService()->getLL('list.report.rowdeleted')
156 );
157 break;
158 default:
159 $response = str_replace('###uid###', $errorParams['uid'], $this->getLanguageService()->getLL('list.report.rownotexisting'));
160 }
161 return $response;
162 }
163
164 /**
165 * Fetches the record with the given UID from the given table.
166 *
167 * The filter option accepts two values:
168 *
169 * "disabled" will filter out disabled and deleted records.
170 * "deleted" filters out deleted records but will return disabled records.
171 * If nothing is specified all records will be returned (including deleted).
172 *
173 * @param string $tableName The name of the table from which the record should be fetched.
174 * @param string $uid The UID of the record that should be fetched.
175 * @param string $filter A filter setting, can be empty or "disabled" or "deleted".
176 * @return array|NULL The result row as associative array or NULL if nothing is found.
177 */
178 protected function getRecordRow($tableName, $uid, $filter = '') {
179
180 $whereStatement = 'uid = ' . (int)$uid;
181
182 switch ($filter) {
183 case 'disabled':
184 $whereStatement .= BackendUtility::BEenableFields($tableName) . BackendUtility::deleteClause($tableName);
185 break;
186 case 'deleted':
187 $whereStatement .= BackendUtility::deleteClause($tableName);
188 break;
189 }
190
191 $row = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
192 '*',
193 $tableName,
194 $whereStatement
195 );
196
197 // Since exec_SELECTgetSingleRow can return NULL or FALSE we
198 // make sure we always return NULL if no row was found.
199 if ($row === FALSE) {
200 $row = NULL;
201 }
202
203 return $row;
204 }
205
206 }