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