[TASK] Make TYPO3 Core PSR-2 standard compliant
[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\Type\Bitmask\Permission;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Helper class for the 'recycler' extension.
23 */
24 class RecyclerUtility
25 {
26 /************************************************************
27 * USER ACCESS
28 *
29 *
30 ************************************************************/
31 /**
32 * Checks the page access rights (Code for access check mostly taken from FormEngine)
33 * as well as the table access rights of the user.
34 *
35 * @param string $table The table to check access for
36 * @param string $row Record array
37 * @return bool Returns TRUE is the user has access, or FALSE if not
38 */
39 public static function checkAccess($table, $row)
40 {
41 $backendUser = static::getBackendUser();
42
43 // 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...)
44 // First, resetting flags.
45 $hasAccess = false;
46 $calcPRec = $row;
47 BackendUtility::fixVersioningPid($table, $calcPRec);
48 if (is_array($calcPRec)) {
49 if ($table === 'pages') {
50 // If 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 $databaseConnection = static::getDatabaseConnection();
88 $clause = trim($clause) !== '' ? ' AND ' . $clause : '';
89 $loopCheck = 100;
90 while ($loopCheck > 0) {
91 $loopCheck--;
92 $res = $databaseConnection->exec_SELECTquery('uid,pid,title,deleted,t3ver_oid,t3ver_wsid', 'pages', 'uid=' . $uid . $clause);
93 if ($res !== false) {
94 $row = $databaseConnection->sql_fetch_assoc($res);
95 $databaseConnection->sql_free_result($res);
96 BackendUtility::workspaceOL('pages', $row);
97 if (is_array($row)) {
98 BackendUtility::fixVersioningPid('pages', $row);
99 $uid = (int)$row['pid'];
100 $output = '/' . htmlspecialchars(GeneralUtility::fixed_lgd_cs($row['title'], $titleLimit)) . $output;
101 if ($row['deleted']) {
102 $output = '<span class="text-danger">' . $output . '</span>';
103 }
104 if ($fullTitleLimit) {
105 $fullOutput = '/' . htmlspecialchars(GeneralUtility::fixed_lgd_cs($row['title'], $fullTitleLimit)) . $fullOutput;
106 }
107 } else {
108 break;
109 }
110 } else {
111 break;
112 }
113 }
114 if ($fullTitleLimit) {
115 return array($output, $fullOutput);
116 } else {
117 return $output;
118 }
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 * Gets the TCA of the table used in the current context.
138 *
139 * @param string $tableName Name of the table to get TCA for
140 * @return array|FALSE TCA of the table used in the current context
141 */
142 public static function getTableTCA($tableName)
143 {
144 $TCA = false;
145 if (isset($GLOBALS['TCA'][$tableName])) {
146 $TCA = $GLOBALS['TCA'][$tableName];
147 }
148 return $TCA;
149 }
150
151 /**
152 * Gets the current backend charset.
153 *
154 * @return string The current backend charset
155 */
156 public static function getCurrentCharset()
157 {
158 $lang = static::getLanguageService();
159 return $lang->csConvObj->parse_charset($lang->charSet);
160 }
161
162 /**
163 * Determines whether the current charset is not UTF-8
164 *
165 * @return bool Whether the current charset is not UTF-8
166 */
167 public static function isNotUtf8Charset()
168 {
169 return self::getCurrentCharset() !== 'utf-8';
170 }
171
172 /**
173 * Gets an UTF-8 encoded string (only if the current charset is not UTF-8!).
174 *
175 * @param string $string String to be converted to UTF-8 if required
176 * @return string UTF-8 encoded string
177 */
178 public static function getUtf8String($string)
179 {
180 if (self::isNotUtf8Charset()) {
181 $string = static::getLanguageService()->csConvObj->utf8_encode($string, self::getCurrentCharset());
182 }
183 return $string;
184 }
185
186 /**
187 * Returns an instance of DatabaseConnection
188 *
189 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
190 */
191 protected static function getDatabaseConnection()
192 {
193 return $GLOBALS['TYPO3_DB'];
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\Lang\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 ((bool)$GLOBALS['BE_USER']->user['admin']) {
222 $tables = array_keys($GLOBALS['TCA']);
223 } else {
224 $tables = explode(',', $GLOBALS['BE_USER']->groupData['tables_modify']);
225 }
226 return $tables;
227 }
228 }