[CLEANUP] The correct case must be used for standard PHP types in phpdoc
[Packages/TYPO3.CMS.git] / typo3 / sysext / recycler / Classes / Utility / RecyclerUtility.php
1 <?php
2 namespace TYPO3\CMS\Recycler\Utility;
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\Database\ConnectionPool;
19 use TYPO3\CMS\Core\Type\Bitmask\Permission;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22 /**
23 * Helper class for the 'recycler' extension.
24 */
25 class RecyclerUtility
26 {
27 /************************************************************
28 * USER ACCESS
29 *
30 *
31 ************************************************************/
32 /**
33 * Checks the page access rights (Code for access check mostly taken from FormEngine)
34 * as well as the table access rights of the user.
35 *
36 * @param string $table The table to check access for
37 * @param string $row Record array
38 * @return bool Returns TRUE is the user has access, or FALSE if not
39 */
40 public static function checkAccess($table, $row)
41 {
42 $backendUser = static::getBackendUser();
43
44 // Checking if the user has permissions? (Only working as a precaution, because the final permission check is always down in TCE. But it's good to notify the user on beforehand...)
45 // First, resetting flags.
46 $hasAccess = false;
47 $calcPRec = $row;
48 BackendUtility::fixVersioningPid($table, $calcPRec);
49 if (is_array($calcPRec)) {
50 if ($table === 'pages') {
51 $calculatedPermissions = $backendUser->calcPerms($calcPRec);
52 $hasAccess = (bool)($calculatedPermissions & Permission::PAGE_EDIT);
53 } else {
54 $calculatedPermissions = $backendUser->calcPerms(BackendUtility::getRecord('pages', $calcPRec['pid']));
55 // Fetching pid-record first.
56 $hasAccess = (bool)($calculatedPermissions & Permission::CONTENT_EDIT);
57 }
58 // Check internals regarding access:
59 if ($hasAccess) {
60 $hasAccess = $backendUser->recordEditAccessInternals($table, $calcPRec);
61 }
62 }
63 if (!$backendUser->check('tables_modify', $table)) {
64 $hasAccess = false;
65 }
66 return $hasAccess;
67 }
68
69 /**
70 * Returns the path (visually) of a page $uid, fx. "/First page/Second page/Another subpage"
71 * Each part of the path will be limited to $titleLimit characters
72 * Deleted pages are filtered out.
73 *
74 * @param int $uid Page uid for which to create record path
75 * @param string $clause is additional where clauses, eg.
76 * @param int $titleLimit Title limit
77 * @param int $fullTitleLimit Title limit of Full title (typ. set to 1000 or so)
78 * @return mixed Path of record (string) OR array with short/long title if $fullTitleLimit is set.
79 */
80 public static function getRecordPath($uid, $clause = '', $titleLimit = 1000, $fullTitleLimit = 0)
81 {
82 if ($clause !== '' || (int)$titleLimit !== 1000 || (int)$fullTitleLimit !== 0) {
83 GeneralUtility::deprecationLog('The arguments "clause", "tileLimit" and "fullTitleLimit" ' .
84 'have been deprecated since TYPO3 CMS 8 and will be removed in TYPO3 CMS 9');
85 }
86 $uid = (int)$uid;
87 $output = ($fullOutput = '/');
88 if ($uid === 0) {
89 return $output;
90 }
91 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
92 $queryBuilder->getRestrictions()->removeAll();
93
94 $clause = trim($clause);
95 $loopCheck = 100;
96 while ($loopCheck > 0) {
97 $loopCheck--;
98
99 $queryBuilder
100 ->select('uid', 'pid', 'title', 'deleted', 't3ver_oid', 't3ver_wsid')
101 ->from('pages')
102 ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)));
103 if (!empty($clause)) {
104 $queryBuilder->andWhere($clause);
105 }
106 $row = $queryBuilder->execute()->fetch();
107 if ($row !== false) {
108 BackendUtility::workspaceOL('pages', $row);
109 if (is_array($row)) {
110 BackendUtility::fixVersioningPid('pages', $row);
111 $uid = (int)$row['pid'];
112 $output = '/' . htmlspecialchars(GeneralUtility::fixed_lgd_cs($row['title'], $titleLimit)) . $output;
113 if ($row['deleted']) {
114 $output = '<span class="text-danger">' . $output . '</span>';
115 }
116 if ($fullTitleLimit) {
117 $fullOutput = '/' . htmlspecialchars(GeneralUtility::fixed_lgd_cs($row['title'], $fullTitleLimit)) . $fullOutput;
118 }
119 } else {
120 break;
121 }
122 } else {
123 break;
124 }
125 }
126 if ($fullTitleLimit) {
127 return [$output, $fullOutput];
128 }
129 return $output;
130 }
131
132 /**
133 * Gets the name of the field with the information whether a record is deleted.
134 *
135 * @param string $tableName Name of the table to get the deleted field for
136 * @return string Name of the field with the information whether a record is deleted
137 */
138 public static function getDeletedField($tableName)
139 {
140 $TCA = self::getTableTCA($tableName);
141 if ($TCA && isset($TCA['ctrl']['delete']) && $TCA['ctrl']['delete']) {
142 return $TCA['ctrl']['delete'];
143 }
144 return '';
145 }
146
147 /**
148 * Check if parent record is deleted
149 *
150 * @param int $pid
151 * @return bool
152 */
153 public static function isParentPageDeleted($pid)
154 {
155 if ((int)$pid === 0) {
156 return false;
157 }
158 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
159 $queryBuilder->getRestrictions()->removeAll();
160
161 $deleted = $queryBuilder
162 ->select('deleted')
163 ->from('pages')
164 ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($pid, \PDO::PARAM_INT)))
165 ->execute()
166 ->fetchColumn();
167
168 return (bool)$deleted;
169 }
170
171 /**
172 * Get pid of uid
173 *
174 * @param int $uid
175 * @param string $table
176 * @return int
177 */
178 public static function getPidOfUid($uid, $table)
179 {
180 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
181 $queryBuilder->getRestrictions()->removeAll();
182
183 $pid = $queryBuilder
184 ->select('pid')
185 ->from($table)
186 ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)))
187 ->execute()
188 ->fetchColumn();
189
190 return (int)$pid;
191 }
192
193 /**
194 * Gets the TCA of the table used in the current context.
195 *
196 * @param string $tableName Name of the table to get TCA for
197 * @return array|false TCA of the table used in the current context
198 */
199 public static function getTableTCA($tableName)
200 {
201 $TCA = false;
202 if (isset($GLOBALS['TCA'][$tableName])) {
203 $TCA = $GLOBALS['TCA'][$tableName];
204 }
205 return $TCA;
206 }
207
208 /**
209 * Returns the BackendUser
210 *
211 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
212 */
213 protected static function getBackendUser()
214 {
215 return $GLOBALS['BE_USER'];
216 }
217
218 /**
219 * Returns an instance of LanguageService
220 *
221 * @return \TYPO3\CMS\Lang\LanguageService
222 */
223 protected static function getLanguageService()
224 {
225 return $GLOBALS['LANG'];
226 }
227
228 /**
229 * Returns the modifyable tables of the current user
230 */
231 public static function getModifyableTables()
232 {
233 if ((bool)$GLOBALS['BE_USER']->user['admin']) {
234 $tables = array_keys($GLOBALS['TCA']);
235 } else {
236 $tables = explode(',', $GLOBALS['BE_USER']->groupData['tables_modify']);
237 }
238 return $tables;
239 }
240 }