[TASK] Migrate to short array syntax
[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 $uid = (int)$uid;
83 $output = ($fullOutput = '/');
84 if ($uid === 0) {
85 return $output;
86 }
87 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
88 $queryBuilder->getRestrictions()->removeAll();
89
90 $clause = trim($clause);
91 $loopCheck = 100;
92 while ($loopCheck > 0) {
93 $loopCheck--;
94
95 $queryBuilder
96 ->select('uid', 'pid', 'title', 'deleted', 't3ver_oid', 't3ver_wsid')
97 ->from('pages')
98 ->where($queryBuilder->expr()->eq('uid', (int)$uid));
99 if (!empty($clause)) {
100 $queryBuilder->andWhere($clause);
101 }
102 $row = $queryBuilder->execute()->fetch();
103 if ($row !== false) {
104 BackendUtility::workspaceOL('pages', $row);
105 if (is_array($row)) {
106 BackendUtility::fixVersioningPid('pages', $row);
107 $uid = (int)$row['pid'];
108 $output = '/' . htmlspecialchars(GeneralUtility::fixed_lgd_cs($row['title'], $titleLimit)) . $output;
109 if ($row['deleted']) {
110 $output = '<span class="text-danger">' . $output . '</span>';
111 }
112 if ($fullTitleLimit) {
113 $fullOutput = '/' . htmlspecialchars(GeneralUtility::fixed_lgd_cs($row['title'], $fullTitleLimit)) . $fullOutput;
114 }
115 } else {
116 break;
117 }
118 } else {
119 break;
120 }
121 }
122 if ($fullTitleLimit) {
123 return [$output, $fullOutput];
124 } else {
125 return $output;
126 }
127 }
128
129 /**
130 * Gets the name of the field with the information whether a record is deleted.
131 *
132 * @param string $tableName Name of the table to get the deleted field for
133 * @return string Name of the field with the information whether a record is deleted
134 */
135 public static function getDeletedField($tableName)
136 {
137 $TCA = self::getTableTCA($tableName);
138 if ($TCA && isset($TCA['ctrl']['delete']) && $TCA['ctrl']['delete']) {
139 return $TCA['ctrl']['delete'];
140 }
141 return '';
142 }
143
144 /**
145 * Check if parent record is deleted
146 *
147 * @param int $pid
148 * @return bool
149 */
150 public static function isParentPageDeleted($pid)
151 {
152 if ((int)$pid === 0) {
153 return false;
154 }
155 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
156 $queryBuilder->getRestrictions()->removeAll();
157
158 $deleted = $queryBuilder
159 ->select('deleted')
160 ->from('pages')
161 ->where($queryBuilder->expr()->eq('uid', (int)$pid))
162 ->execute()
163 ->fetchColumn();
164
165 return (bool)$deleted;
166 }
167
168 /**
169 * Get pid of uid
170 *
171 * @param int $uid
172 * @param string $table
173 * @return int
174 */
175 public static function getPidOfUid($uid, $table)
176 {
177 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
178 $queryBuilder->getRestrictions()->removeAll();
179
180 $pid = $queryBuilder
181 ->select('pid')
182 ->from($table)
183 ->where($queryBuilder->expr()->eq('uid', (int)$uid))
184 ->execute()
185 ->fetchColumn();
186
187 return (int)$pid;
188 }
189
190 /**
191 * Gets the TCA of the table used in the current context.
192 *
193 * @param string $tableName Name of the table to get TCA for
194 * @return array|FALSE TCA of the table used in the current context
195 */
196 public static function getTableTCA($tableName)
197 {
198 $TCA = false;
199 if (isset($GLOBALS['TCA'][$tableName])) {
200 $TCA = $GLOBALS['TCA'][$tableName];
201 }
202 return $TCA;
203 }
204
205 /**
206 * Returns the BackendUser
207 *
208 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
209 */
210 protected static function getBackendUser()
211 {
212 return $GLOBALS['BE_USER'];
213 }
214
215 /**
216 * Returns an instance of LanguageService
217 *
218 * @return \TYPO3\CMS\Lang\LanguageService
219 */
220 protected static function getLanguageService()
221 {
222 return $GLOBALS['LANG'];
223 }
224
225 /**
226 * Returns the modifyable tables of the current user
227 */
228 public static function getModifyableTables()
229 {
230 if ((bool)$GLOBALS['BE_USER']->user['admin']) {
231 $tables = array_keys($GLOBALS['TCA']);
232 } else {
233 $tables = explode(',', $GLOBALS['BE_USER']->groupData['tables_modify']);
234 }
235 return $tables;
236 }
237 }