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