37ed9e3770c48b3e1f50344bb50d0f9988b6f401
[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\Utility\GeneralUtility;
19
20 /**
21 * Helper class for the 'recycler' extension.
22 *
23 * @author Julian Kleinhans <typo3@kj187.de>
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 alt_doc.php)
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 static public function checkAccess($table, $row) {
41 /* @var $backendUser \TYPO3\CMS\Core\Authentication\BackendUserAuthentication */
42 $backendUser = $GLOBALS['BE_USER'];
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 // If pages:
52 $calculatedPermissions = $backendUser->calcPerms($calcPRec);
53 $hasAccess = $calculatedPermissions & 2 ? TRUE : FALSE;
54 } else {
55 $calculatedPermissions = $backendUser->calcPerms(BackendUtility::getRecord('pages', $calcPRec['pid']));
56 // Fetching pid-record first.
57 $hasAccess = $calculatedPermissions & 16 ? TRUE : FALSE;
58 }
59 // Check internals regarding access:
60 if ($hasAccess) {
61 $hasAccess = $backendUser->recordEditAccessInternals($table, $calcPRec);
62 }
63 }
64 if (!$backendUser->check('tables_modify', $table)) {
65 $hasAccess = FALSE;
66 }
67 return $hasAccess;
68 }
69
70 /**
71 * Returns the path (visually) of a page $uid, fx. "/First page/Second page/Another subpage"
72 * Each part of the path will be limited to $titleLimit characters
73 * Deleted pages are filtered out.
74 *
75 * @param int $uid Page uid for which to create record path
76 * @param string $clause is additional where clauses, eg.
77 * @param int $titleLimit Title limit
78 * @param int $fullTitleLimit Title limit of Full title (typ. set to 1000 or so)
79 * @return mixed Path of record (string) OR array with short/long title if $fullTitleLimit is set.
80 */
81 static public function getRecordPath($uid, $clause = '', $titleLimit = 1000, $fullTitleLimit = 0) {
82 /* @var $databaseConnection \TYPO3\CMS\Core\Database\DatabaseConnection */
83 $databaseConnection = $GLOBALS['TYPO3_DB'];
84
85 $loopCheck = 100;
86 $output = ($fullOutput = '/');
87 while ($uid != 0 && $loopCheck > 0) {
88 $loopCheck--;
89 $res = $databaseConnection->exec_SELECTquery('uid,pid,title,deleted,t3ver_oid,t3ver_wsid', 'pages', 'uid=' . (int)$uid . (strlen(trim($clause)) ? ' AND ' . $clause : ''));
90 if (is_resource($res)) {
91 $row = $databaseConnection->sql_fetch_assoc($res);
92 $databaseConnection->sql_free_result($res);
93 BackendUtility::workspaceOL('pages', $row);
94 if (is_array($row)) {
95 BackendUtility::fixVersioningPid('pages', $row);
96 $uid = $row['pid'];
97 $output = '/' . htmlspecialchars(GeneralUtility::fixed_lgd_cs($row['title'], $titleLimit)) . $output;
98 if ($row['deleted']) {
99 $output = '<span class="deletedPath">' . $output . '</span>';
100 }
101 if ($fullTitleLimit) {
102 $fullOutput = '/' . htmlspecialchars(GeneralUtility::fixed_lgd_cs($row['title'], $fullTitleLimit)) . $fullOutput;
103 }
104 } else {
105 break;
106 }
107 } else {
108 break;
109 }
110 }
111 if ($fullTitleLimit) {
112 return array($output, $fullOutput);
113 } else {
114 return $output;
115 }
116 }
117
118 /**
119 * Gets the name of the field with the information whether a record is deleted.
120 *
121 * @param string $tableName Name of the table to get the deleted field for
122 * @return string Name of the field with the information whether a record is deleted
123 */
124 static public function getDeletedField($tableName) {
125 $TCA = self::getTableTCA($tableName);
126 if ($TCA && isset($TCA['ctrl']['delete']) && $TCA['ctrl']['delete']) {
127 return $TCA['ctrl']['delete'];
128 }
129 return '';
130 }
131
132 /**
133 * Gets the TCA of the table used in the current context.
134 *
135 * @param string $tableName Name of the table to get TCA for
136 * @return array|FALSE TCA of the table used in the current context
137 */
138 static public function getTableTCA($tableName) {
139 $TCA = FALSE;
140 if (isset($GLOBALS['TCA'][$tableName])) {
141 $TCA = $GLOBALS['TCA'][$tableName];
142 }
143 return $TCA;
144 }
145
146 /**
147 * Gets the current backend charset.
148 *
149 * @return string The current backend charset
150 */
151 static public function getCurrentCharset() {
152 return $GLOBALS['LANG']->csConvObj->parse_charset($GLOBALS['LANG']->charSet);
153 }
154
155 /**
156 * Determines whether the current charset is not UTF-8
157 *
158 * @return bool Whether the current charset is not UTF-8
159 */
160 static public function isNotUtf8Charset() {
161 return self::getCurrentCharset() !== 'utf-8';
162 }
163
164 /**
165 * Gets an UTF-8 encoded string (only if the current charset is not UTF-8!).
166 *
167 * @param string $string String to be converted to UTF-8 if required
168 * @return string UTF-8 encoded string
169 */
170 static public function getUtf8String($string) {
171 if (self::isNotUtf8Charset()) {
172 $string = $GLOBALS['LANG']->csConvObj->utf8_encode($string, self::getCurrentCharset());
173 }
174 return $string;
175 }
176
177 }