[TASK] Clean up unused / dead code
[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 array $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 if ($backendUser->isAdmin()) {
45 return true;
46 }
47
48 if (!$backendUser->check('tables_modify', $table)) {
49 return false;
50 }
51
52 // 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...)
53 // First, resetting flags.
54 $hasAccess = false;
55 $calcPRec = $row;
56 BackendUtility::fixVersioningPid($table, $calcPRec);
57 if (is_array($calcPRec)) {
58 if ($table === 'pages') {
59 $calculatedPermissions = $backendUser->calcPerms($calcPRec);
60 $hasAccess = (bool)($calculatedPermissions & Permission::PAGE_EDIT);
61 } else {
62 $calculatedPermissions = $backendUser->calcPerms(BackendUtility::getRecord('pages', $calcPRec['pid']));
63 // Fetching pid-record first.
64 $hasAccess = (bool)($calculatedPermissions & Permission::CONTENT_EDIT);
65 }
66 // Check internals regarding access:
67 if ($hasAccess) {
68 $hasAccess = $backendUser->recordEditAccessInternals($table, $calcPRec);
69 }
70 }
71 return $hasAccess;
72 }
73
74 /**
75 * Returns the path (visually) of a page $uid, fx. "/First page/Second page/Another subpage"
76 * Each part of the path will be limited to $titleLimit characters
77 * Deleted pages are filtered out.
78 *
79 * @param int $uid Page uid for which to create record path
80 * @return string Path of record (string) OR array with short/long title if $fullTitleLimit is set.
81 */
82 public static function getRecordPath($uid)
83 {
84 $uid = (int)$uid;
85 $output = '/';
86 if ($uid === 0) {
87 return $output;
88 }
89 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
90 $queryBuilder->getRestrictions()->removeAll();
91
92 $loopCheck = 100;
93 while ($loopCheck > 0) {
94 $loopCheck--;
95
96 $queryBuilder
97 ->select('uid', 'pid', 'title', 'deleted', 't3ver_oid', 't3ver_wsid')
98 ->from('pages')
99 ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)));
100 $row = $queryBuilder->execute()->fetch();
101 if ($row !== false) {
102 BackendUtility::workspaceOL('pages', $row);
103 if (is_array($row)) {
104 BackendUtility::fixVersioningPid('pages', $row);
105 $uid = (int)$row['pid'];
106 $output = '/' . htmlspecialchars(GeneralUtility::fixed_lgd_cs($row['title'], 1000)) . $output;
107 if ($row['deleted']) {
108 $output = '<span class="text-danger">' . $output . '</span>';
109 }
110 } else {
111 break;
112 }
113 } else {
114 break;
115 }
116 }
117 return $output;
118 }
119
120 /**
121 * Gets the name of the field with the information whether a record is deleted.
122 *
123 * @param string $tableName Name of the table to get the deleted field for
124 * @return string Name of the field with the information whether a record is deleted
125 */
126 public static function getDeletedField($tableName)
127 {
128 $TCA = self::getTableTCA($tableName);
129 if ($TCA && isset($TCA['ctrl']['delete']) && $TCA['ctrl']['delete']) {
130 return $TCA['ctrl']['delete'];
131 }
132 return '';
133 }
134
135 /**
136 * Check if parent record is deleted
137 *
138 * @param int $pid
139 * @return bool
140 */
141 public static function isParentPageDeleted($pid)
142 {
143 if ((int)$pid === 0) {
144 return false;
145 }
146 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
147 $queryBuilder->getRestrictions()->removeAll();
148
149 $deleted = $queryBuilder
150 ->select('deleted')
151 ->from('pages')
152 ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($pid, \PDO::PARAM_INT)))
153 ->execute()
154 ->fetchColumn();
155
156 return (bool)$deleted;
157 }
158
159 /**
160 * Get pid of uid
161 *
162 * @param int $uid
163 * @param string $table
164 * @return int
165 */
166 public static function getPidOfUid($uid, $table)
167 {
168 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
169 $queryBuilder->getRestrictions()->removeAll();
170
171 $pid = $queryBuilder
172 ->select('pid')
173 ->from($table)
174 ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)))
175 ->execute()
176 ->fetchColumn();
177
178 return (int)$pid;
179 }
180
181 /**
182 * Gets the TCA of the table used in the current context.
183 *
184 * @param string $tableName Name of the table to get TCA for
185 * @return array|false TCA of the table used in the current context
186 */
187 public static function getTableTCA($tableName)
188 {
189 $TCA = false;
190 if (isset($GLOBALS['TCA'][$tableName])) {
191 $TCA = $GLOBALS['TCA'][$tableName];
192 }
193 return $TCA;
194 }
195
196 /**
197 * Returns the BackendUser
198 *
199 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
200 */
201 protected static function getBackendUser()
202 {
203 return $GLOBALS['BE_USER'];
204 }
205
206 /**
207 * Returns an instance of LanguageService
208 *
209 * @return \TYPO3\CMS\Core\Localization\LanguageService
210 */
211 protected static function getLanguageService()
212 {
213 return $GLOBALS['LANG'];
214 }
215
216 /**
217 * Returns the modifyable tables of the current user
218 */
219 public static function getModifyableTables()
220 {
221 if ($GLOBALS['BE_USER']->isAdmin()) {
222 $tables = array_keys($GLOBALS['TCA']);
223 } else {
224 $tables = explode(',', $GLOBALS['BE_USER']->groupData['tables_modify']);
225 }
226 return $tables;
227 }
228 }