[FEATURE] Allow multiple domain variants per site
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / ExpressionLanguage / FunctionsProvider / Typo3ConditionFunctionsProvider.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use Symfony\Component\ExpressionLanguage\ExpressionFunction;
19 use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
20 use TYPO3\CMS\Core\ExpressionLanguage\RequestWrapper;
21 use TYPO3\CMS\Core\Site\Entity\Site;
22 use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25 /**
26 * Class TypoScriptConditionProvider
27 * @internal
28 */
29 class Typo3ConditionFunctionsProvider implements ExpressionFunctionProviderInterface
30 {
31 /**
32 * @return ExpressionFunction[] An array of Function instances
33 */
34 public function getFunctions()
35 {
36 return [
37 $this->getLoginUserFunction(),
38 $this->getTSFEFunction(),
39 $this->getUsergroupFunction(),
40 $this->getSessionFunction(),
41 $this->getSiteFunction(),
42 $this->getSiteLanguageFunction(),
43 ];
44 }
45
46 protected function getLoginUserFunction(): ExpressionFunction
47 {
48 return new ExpressionFunction('loginUser', function ($str) {
49 // Not implemented, we only use the evaluator
50 }, function ($arguments, $str) {
51 $user = $arguments['backend']->user ?? $arguments['frontend']->user;
52 if ($user->isLoggedIn) {
53 foreach (GeneralUtility::trimExplode(',', $str, true) as $test) {
54 if ($test === '*' || (string)$user->userId === (string)$test) {
55 return true;
56 }
57 }
58 }
59 return false;
60 });
61 }
62
63 protected function getTSFEFunction(): ExpressionFunction
64 {
65 return new ExpressionFunction('getTSFE', function ($str) {
66 // Not implemented, we only use the evaluator
67 }, function ($arguments) {
68 return $GLOBALS['TSFE'];
69 });
70 }
71
72 protected function getUsergroupFunction(): ExpressionFunction
73 {
74 return new ExpressionFunction('usergroup', function ($str) {
75 // Not implemented, we only use the evaluator
76 }, function ($arguments, $str) {
77 $user = $arguments['backend']->user ?? $arguments['frontend']->user;
78 $groupList = $user->userGroupList ?? '';
79 // '0,-1' is the default usergroups string when not logged in!
80 if ($groupList !== '0,-1' && $groupList !== '') {
81 foreach (GeneralUtility::trimExplode(',', $str, true) as $test) {
82 if ($test === '*' || GeneralUtility::inList($groupList, $test)) {
83 return true;
84 }
85 }
86 }
87 return false;
88 });
89 }
90
91 protected function getSessionFunction(): ExpressionFunction
92 {
93 return new ExpressionFunction(
94 'session',
95 function ($str) {
96 // Not implemented, we only use the evaluator
97 },
98 function ($arguments, $str) {
99 $retVal = null;
100 $keyParts = explode('|', $str);
101 $sessionKey = array_shift($keyParts);
102 // @todo fetch data from be session if available
103 $tsfe = $GLOBALS['TSFE'] ?? null;
104 if ($tsfe && is_object($tsfe->fe_user)) {
105 $retVal = $tsfe->fe_user->getSessionData($sessionKey);
106 foreach ($keyParts as $keyPart) {
107 if (is_object($retVal)) {
108 $retVal = $retVal->{$keyPart};
109 } elseif (is_array($retVal)) {
110 $retVal = $retVal[$keyPart];
111 } else {
112 break;
113 }
114 }
115 }
116 return $retVal;
117 }
118 );
119 }
120
121 protected function getSiteFunction(): ExpressionFunction
122 {
123 return new ExpressionFunction(
124 'site',
125 function ($str) {
126 // Not implemented, we only use the evaluator
127 },
128 function ($arguments, $str) {
129 /** @var RequestWrapper $requestWrapper */
130 $requestWrapper = $arguments['request'];
131 $site = $requestWrapper->getSite();
132 if ($site instanceof Site) {
133 $methodName = 'get' . ucfirst(trim($str));
134 if (method_exists($site, $methodName)) {
135 return $site->$methodName();
136 }
137 }
138 return null;
139 }
140 );
141 }
142
143 protected function getSiteLanguageFunction(): ExpressionFunction
144 {
145 return new ExpressionFunction(
146 'siteLanguage',
147 function ($str) {
148 // Not implemented, we only use the evaluator
149 },
150 function ($arguments, $str) {
151 /** @var RequestWrapper $requestWrapper */
152 $requestWrapper = $arguments['request'];
153 $siteLanguage = $requestWrapper->getSiteLanguage();
154 if ($siteLanguage instanceof SiteLanguage) {
155 $methodName = 'get' . ucfirst(trim($str));
156 if (method_exists($siteLanguage, $methodName)) {
157 return $siteLanguage->$methodName();
158 }
159 }
160 return null;
161 }
162 );
163 }
164 }