[BUGFIX] Fix several typos in php comments
[Packages/TYPO3.CMS.git] / typo3 / sysext / recycler / Classes / Utility / RecyclerUtility.php
index 9325a93..3da23e9 100644 (file)
 <?php
 namespace TYPO3\CMS\Recycler\Utility;
 
-/***************************************************************
- *  Copyright notice
+/*
+ * This file is part of the TYPO3 CMS project.
  *
- *  (c) 2009-2011 Julian Kleinhans <typo3@kj187.de>
- *  All rights reserved
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
  *
- *  This script is part of the TYPO3 project. The TYPO3 project is
- *  free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
  *
- *  The GNU General Public License can be found at
- *  http://www.gnu.org/copyleft/gpl.html.
- *
- *  This script is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  This copyright notice MUST APPEAR in all copies of the script!
- ***************************************************************/
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Backend\Utility\BackendUtility;
+use TYPO3\CMS\Core\Database\ConnectionPool;
+use TYPO3\CMS\Core\Type\Bitmask\Permission;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
 /**
  * Helper class for the 'recycler' extension.
- *
- * @author     Julian Kleinhans <typo3@kj187.de>
- * @package    TYPO3
- * @subpackage         tx_recycler
+ * @internal
  */
-class RecyclerUtility {
-
-       /************************************************************
-        * USER ACCESS
-        *
-        *
-        ************************************************************/
-       /**
-        * Checks the page access rights (Code for access check mostly taken from alt_doc.php)
-        * as well as the table access rights of the user.
-        *
-        * @param       string          $cmd: The command that sould be performed ('new' or 'edit')
-        * @param       string          $table: The table to check access for
-        * @param       string          $theUid: The record uid of the table
-        * @return      boolean         Returns TRUE is the user has access, or FALSE if not
-        */
-       static public function checkAccess($table, $row) {
-               // 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...)
-               // First, resetting flags.
-               $hasAccess = 0;
-               $calcPRec = $row;
-               \TYPO3\CMS\Backend\Utility\BackendUtility::fixVersioningPid($table, $calcPRec);
-               if (is_array($calcPRec)) {
-                       if ($table == 'pages') {
-                               // If pages:
-                               $CALC_PERMS = $GLOBALS['BE_USER']->calcPerms($calcPRec);
-                               $hasAccess = $CALC_PERMS & 2 ? 1 : 0;
-                       } else {
-                               $CALC_PERMS = $GLOBALS['BE_USER']->calcPerms(\TYPO3\CMS\Backend\Utility\BackendUtility::getRecord('pages', $calcPRec['pid']));
-                               // Fetching pid-record first.
-                               $hasAccess = $CALC_PERMS & 16 ? 1 : 0;
-                       }
-                       // Check internals regarding access:
-                       if ($hasAccess) {
-                               $hasAccess = $GLOBALS['BE_USER']->recordEditAccessInternals($table, $calcPRec);
-                       }
-               }
-               if (!$GLOBALS['BE_USER']->check('tables_modify', $table)) {
-                       $hasAccess = 0;
-               }
-               return $hasAccess ? TRUE : FALSE;
-       }
-
-       /**
-        * Returns the path (visually) of a page $uid, fx. "/First page/Second page/Another subpage"
-        * Each part of the path will be limited to $titleLimit characters
-        * Deleted pages are filtered out.
-        *
-        * @param       integer         Page uid for which to create record path
-        * @param       string          $clause is additional where clauses, eg.
-        * @param       integer         Title limit
-        * @param       integer         Title limit of Full title (typ. set to 1000 or so)
-        * @return      mixed           Path of record (string) OR array with short/long title if $fullTitleLimit is set.
-        */
-       static public function getRecordPath($uid, $clause = '', $titleLimit = 1000, $fullTitleLimit = 0) {
-               $loopCheck = 100;
-               $output = ($fullOutput = '/');
-               while ($uid != 0 && $loopCheck > 0) {
-                       $loopCheck--;
-                       $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,pid,title,deleted,t3ver_oid,t3ver_wsid', 'pages', ('uid=' . intval($uid)) . (strlen(trim($clause)) ? ' AND ' . $clause : ''));
-                       if (is_resource($res)) {
-                               $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
-                               $GLOBALS['TYPO3_DB']->sql_free_result($res);
-                               \TYPO3\CMS\Backend\Utility\BackendUtility::workspaceOL('pages', $row);
-                               if (is_array($row)) {
-                                       \TYPO3\CMS\Backend\Utility\BackendUtility::fixVersioningPid('pages', $row);
-                                       $uid = $row['pid'];
-                                       $output = ('/' . htmlspecialchars(\TYPO3\CMS\Core\Utility\GeneralUtility::fixed_lgd_cs($row['title'], $titleLimit))) . $output;
-                                       if ($row['deleted']) {
-                                               $output = ('<span class="deletedPath">' . $output) . '</span>';
-                                       }
-                                       if ($fullTitleLimit) {
-                                               $fullOutput = ('/' . htmlspecialchars(\TYPO3\CMS\Core\Utility\GeneralUtility::fixed_lgd_cs($row['title'], $fullTitleLimit))) . $fullOutput;
-                                       }
-                               } else {
-                                       break;
-                               }
-                       } else {
-                               break;
-                       }
-               }
-               if ($fullTitleLimit) {
-                       return array($output, $fullOutput);
-               } else {
-                       return $output;
-               }
-       }
-
-       /**
-        * Gets the name of the field with the information whether a record is deleted.
-        *
-        * @param       string          $tableName: Name of the table to get the deleted field for
-        * @return      string          Name of the field with the information whether a record is deleted
-        */
-       static public function getDeletedField($tableName) {
-               $TCA = self::getTableTCA($tableName);
-               if (($TCA && isset($TCA['ctrl']['delete'])) && $TCA['ctrl']['delete']) {
-                       return $TCA['ctrl']['delete'];
-               }
-       }
-
-       /**
-        * Gets the TCA of the table used in the current context.
-        *
-        * @param       string          $tableName: Name of the table to get TCA for
-        * @return      mixed           TCA of the table used in the current context (array)
-        */
-       static public function getTableTCA($tableName) {
-               $TCA = FALSE;
-               if (isset($GLOBALS['TCA'][$tableName])) {
-                       $TCA = $GLOBALS['TCA'][$tableName];
-               }
-               return $TCA;
-       }
-
-       /**
-        * Gets the current backend charset.
-        *
-        * @return      string          The current backend charset
-        */
-       static public function getCurrentCharset() {
-               return $GLOBALS['LANG']->csConvObj->parse_charset($GLOBALS['LANG']->charSet);
-       }
-
-       /**
-        * Determines whether the current charset is not UTF-8
-        *
-        * @return      boolean         Whether the current charset is not UTF-8
-        */
-       static public function isNotUtf8Charset() {
-               return self::getCurrentCharset() !== 'utf-8';
-       }
-
-       /**
-        * Gets an UTF-8 encoded string (only if the current charset is not UTF-8!).
-        *
-        * @param       string          $string: String to be converted to UTF-8 if required
-        * @return      string          UTF-8 encoded string
-        */
-       static public function getUtf8String($string) {
-               if (self::isNotUtf8Charset()) {
-                       $string = $GLOBALS['LANG']->csConvObj->utf8_encode($string, self::getCurrentCharset());
-               }
-               return $string;
-       }
+class RecyclerUtility
+{
+    /************************************************************
+     * USER ACCESS
+     *
+     *
+     ************************************************************/
+    /**
+     * Checks the page access rights (Code for access check mostly taken from FormEngine)
+     * as well as the table access rights of the user.
+     *
+     * @param string $table The table to check access for
+     * @param array $row Record array
+     * @return bool Returns TRUE is the user has access, or FALSE if not
+     */
+    public static function checkAccess($table, $row)
+    {
+        $backendUser = static::getBackendUser();
 
-}
+        if ($backendUser->isAdmin()) {
+            return true;
+        }
+
+        if (!$backendUser->check('tables_modify', $table)) {
+            return false;
+        }
+
+        // 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...)
+        // First, resetting flags.
+        $hasAccess = false;
+        $calcPRec = $row;
+        BackendUtility::fixVersioningPid($table, $calcPRec);
+        if (is_array($calcPRec)) {
+            if ($table === 'pages') {
+                $calculatedPermissions = $backendUser->calcPerms($calcPRec);
+                $hasAccess = (bool)($calculatedPermissions & Permission::PAGE_EDIT);
+            } else {
+                $calculatedPermissions = $backendUser->calcPerms(BackendUtility::getRecord('pages', $calcPRec['pid']));
+                // Fetching pid-record first.
+                $hasAccess = (bool)($calculatedPermissions & Permission::CONTENT_EDIT);
+            }
+            // Check internals regarding access:
+            if ($hasAccess) {
+                $hasAccess = $backendUser->recordEditAccessInternals($table, $calcPRec);
+            }
+        }
+        return $hasAccess;
+    }
+
+    /**
+     * Returns the path (visually) of a page $uid, fx. "/First page/Second page/Another subpage"
+     * Each part of the path will be limited to $titleLimit characters
+     * Deleted pages are filtered out.
+     *
+     * @param int $uid Page uid for which to create record path
+     * @return string Path of record (string) OR array with short/long title if $fullTitleLimit is set.
+     */
+    public static function getRecordPath($uid)
+    {
+        $uid = (int)$uid;
+        $output = '/';
+        if ($uid === 0) {
+            return $output;
+        }
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
+        $queryBuilder->getRestrictions()->removeAll();
+
+        $loopCheck = 100;
+        while ($loopCheck > 0) {
+            $loopCheck--;
 
+            $queryBuilder
+                ->select('uid', 'pid', 'title', 'deleted', 't3ver_oid', 't3ver_wsid')
+                ->from('pages')
+                ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)));
+            $row = $queryBuilder->execute()->fetch();
+            if ($row !== false) {
+                BackendUtility::workspaceOL('pages', $row);
+                if (is_array($row)) {
+                    BackendUtility::fixVersioningPid('pages', $row);
+                    $uid = (int)$row['pid'];
+                    $output = '/' . htmlspecialchars(GeneralUtility::fixed_lgd_cs($row['title'], 1000)) . $output;
+                    if ($row['deleted']) {
+                        $output = '<span class="text-danger">' . $output . '</span>';
+                    }
+                } else {
+                    break;
+                }
+            } else {
+                break;
+            }
+        }
+        return $output;
+    }
 
-?>
\ No newline at end of file
+    /**
+     * Gets the name of the field with the information whether a record is deleted.
+     *
+     * @param string $tableName Name of the table to get the deleted field for
+     * @return string Name of the field with the information whether a record is deleted
+     */
+    public static function getDeletedField($tableName)
+    {
+        $TCA = self::getTableTCA($tableName);
+        if ($TCA && isset($TCA['ctrl']['delete']) && $TCA['ctrl']['delete']) {
+            return $TCA['ctrl']['delete'];
+        }
+        return '';
+    }
+
+    /**
+     * Check if parent record is deleted
+     *
+     * @param int $pid
+     * @return bool
+     */
+    public static function isParentPageDeleted($pid)
+    {
+        if ((int)$pid === 0) {
+            return false;
+        }
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
+        $queryBuilder->getRestrictions()->removeAll();
+
+        $deleted = $queryBuilder
+            ->select('deleted')
+            ->from('pages')
+            ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($pid, \PDO::PARAM_INT)))
+            ->execute()
+            ->fetchColumn();
+
+        return (bool)$deleted;
+    }
+
+    /**
+     * Get pid of uid
+     *
+     * @param int $uid
+     * @param string $table
+     * @return int
+     */
+    public static function getPidOfUid($uid, $table)
+    {
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
+        $queryBuilder->getRestrictions()->removeAll();
+
+        $pid = $queryBuilder
+            ->select('pid')
+            ->from($table)
+            ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)))
+            ->execute()
+            ->fetchColumn();
+
+        return (int)$pid;
+    }
+
+    /**
+     * Gets the TCA of the table used in the current context.
+     *
+     * @param string $tableName Name of the table to get TCA for
+     * @return array|false TCA of the table used in the current context
+     */
+    public static function getTableTCA($tableName)
+    {
+        $TCA = false;
+        if (isset($GLOBALS['TCA'][$tableName])) {
+            $TCA = $GLOBALS['TCA'][$tableName];
+        }
+        return $TCA;
+    }
+
+    /**
+     * Returns the BackendUser
+     *
+     * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
+     */
+    protected static function getBackendUser()
+    {
+        return $GLOBALS['BE_USER'];
+    }
+
+    /**
+     * Returns an instance of LanguageService
+     *
+     * @return \TYPO3\CMS\Core\Localization\LanguageService
+     */
+    protected static function getLanguageService()
+    {
+        return $GLOBALS['LANG'];
+    }
+
+    /**
+     * Returns the modifiable tables of the current user
+     */
+    public static function getModifyableTables()
+    {
+        if ($GLOBALS['BE_USER']->isAdmin()) {
+            $tables = array_keys($GLOBALS['TCA']);
+        } else {
+            $tables = explode(',', $GLOBALS['BE_USER']->groupData['tables_modify']);
+        }
+        return $tables;
+    }
+}