50cb3528a9418d0726756beeb9622c8fe66efe13
[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 $calculatedPermissions = $backendUser->calcPerms($calcPRec);
51 $hasAccess = (bool)($calculatedPermissions & Permission::PAGE_EDIT);
52 } else {
53 $calculatedPermissions = $backendUser->calcPerms(BackendUtility::getRecord('pages', $calcPRec['pid']));
54 // Fetching pid-record first.
55 $hasAccess = (bool)($calculatedPermissions & Permission::CONTENT_EDIT);
56 }
57 // Check internals regarding access:
58 if ($hasAccess) {
59 $hasAccess = $backendUser->recordEditAccessInternals($table, $calcPRec);
60 }
61 }
62 if (!$backendUser->check('tables_modify', $table)) {
63 $hasAccess = false;
64 }
65 return $hasAccess;
66 }
67
68 /**
69 * Returns the path (visually) of a page $uid, fx. "/First page/Second page/Another subpage"
70 * Each part of the path will be limited to $titleLimit characters
71 * Deleted pages are filtered out.
72 *
73 * @param int $uid Page uid for which to create record path
74 * @param string $clause is additional where clauses, eg.
75 * @param int $titleLimit Title limit
76 * @param int $fullTitleLimit Title limit of Full title (typ. set to 1000 or so)
77 * @return mixed Path of record (string) OR array with short/long title if $fullTitleLimit is set.
78 */
79 public static function getRecordPath($uid, $clause = '', $titleLimit = 1000, $fullTitleLimit = 0)
80 {
81 $uid = (int)$uid;
82 $output = ($fullOutput = '/');
83 if ($uid === 0) {
84 return $output;
85 }
86 $databaseConnection = static::getDatabaseConnection();
87 $clause = trim($clause) !== '' ? ' AND ' . $clause : '';
88 $loopCheck = 100;
89 while ($loopCheck > 0) {
90 $loopCheck--;
91 $res = $databaseConnection->exec_SELECTquery('uid,pid,title,deleted,t3ver_oid,t3ver_wsid', 'pages', 'uid=' . $uid . $clause);
92 if ($res !== false) {
93 $row = $databaseConnection->sql_fetch_assoc($res);
94 $databaseConnection->sql_free_result($res);
95 BackendUtility::workspaceOL('pages', $row);
96 if (is_array($row)) {
97 BackendUtility::fixVersioningPid('pages', $row);
98 $uid = (int)$row['pid'];
99 $output = '/' . htmlspecialchars(GeneralUtility::fixed_lgd_cs($row['title'], $titleLimit)) . $output;
100 if ($row['deleted']) {
101 $output = '<span class="text-danger">' . $output . '</span>';
102 }
103 if ($fullTitleLimit) {
104 $fullOutput = '/' . htmlspecialchars(GeneralUtility::fixed_lgd_cs($row['title'], $fullTitleLimit)) . $fullOutput;
105 }
106 } else {
107 break;
108 }
109 } else {
110 break;
111 }
112 }
113 if ($fullTitleLimit) {
114 return array($output, $fullOutput);
115 } else {
116 return $output;
117 }
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 * Gets the TCA of the table used in the current context.
137 *
138 * @param string $tableName Name of the table to get TCA for
139 * @return array|FALSE TCA of the table used in the current context
140 */
141 public static function getTableTCA($tableName)
142 {
143 $TCA = false;
144 if (isset($GLOBALS['TCA'][$tableName])) {
145 $TCA = $GLOBALS['TCA'][$tableName];
146 }
147 return $TCA;
148 }
149
150 /**
151 * Gets the current backend charset.
152 *
153 * @return string The current backend charset
154 */
155 public static function getCurrentCharset()
156 {
157 $lang = static::getLanguageService();
158 return $lang->csConvObj->parse_charset($lang->charSet);
159 }
160
161 /**
162 * Determines whether the current charset is not UTF-8
163 *
164 * @return bool Whether the current charset is not UTF-8
165 */
166 public static function isNotUtf8Charset()
167 {
168 return self::getCurrentCharset() !== 'utf-8';
169 }
170
171 /**
172 * Gets an UTF-8 encoded string (only if the current charset is not UTF-8!).
173 *
174 * @param string $string String to be converted to UTF-8 if required
175 * @return string UTF-8 encoded string
176 */
177 public static function getUtf8String($string)
178 {
179 if (self::isNotUtf8Charset()) {
180 $string = static::getLanguageService()->csConvObj->utf8_encode($string, self::getCurrentCharset());
181 }
182 return $string;
183 }
184
185 /**
186 * Returns an instance of DatabaseConnection
187 *
188 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
189 */
190 protected static function getDatabaseConnection()
191 {
192 return $GLOBALS['TYPO3_DB'];
193 }
194
195 /**
196 * Returns the BackendUser
197 *
198 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
199 */
200 protected static function getBackendUser()
201 {
202 return $GLOBALS['BE_USER'];
203 }
204
205 /**
206 * Returns an instance of LanguageService
207 *
208 * @return \TYPO3\CMS\Lang\LanguageService
209 */
210 protected static function getLanguageService()
211 {
212 return $GLOBALS['LANG'];
213 }
214
215 /**
216 * Returns the modifyable tables of the current user
217 */
218 public static function getModifyableTables()
219 {
220 if ((bool)$GLOBALS['BE_USER']->user['admin']) {
221 $tables = array_keys($GLOBALS['TCA']);
222 } else {
223 $tables = explode(',', $GLOBALS['BE_USER']->groupData['tables_modify']);
224 }
225 return $tables;
226 }
227 }