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