ed9dde303aa51f0e9dca40258dfb95d5ef6ca17d
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / ExpressionLanguage / FunctionsProvider / DefaultFunctionsProvider.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\Context\Context;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Core\Utility\StringUtility;
23 use TYPO3\CMS\Core\Utility\VersionNumberUtility;
24
25 /**
26 * Class DefaultFunctionsProvider
27 * @internal
28 */
29 class DefaultFunctionsProvider implements ExpressionFunctionProviderInterface
30 {
31 /**
32 * @return ExpressionFunction[] An array of Function instances
33 */
34 public function getFunctions()
35 {
36 return [
37 $this->getIpFunction(),
38 $this->getCompatVersionFunction(),
39 $this->getLikeFunction(),
40 $this->getEnvFunction(),
41 $this->getDateFunction(),
42 ];
43 }
44
45 protected function getIpFunction(): ExpressionFunction
46 {
47 return new ExpressionFunction(
48 'ip',
49 function () {
50 // Not implemented, we only use the evaluator
51 },
52 function ($arguments, $str) {
53 if ($str === 'devIP') {
54 $str = trim($GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask'] ?? '');
55 }
56 return (bool)GeneralUtility::cmpIP(GeneralUtility::getIndpEnv('REMOTE_ADDR'), $str);
57 }
58 );
59 }
60
61 protected function getCompatVersionFunction(): ExpressionFunction
62 {
63 return new ExpressionFunction(
64 'compatVersion',
65 function () {
66 // Not implemented, we only use the evaluator
67 },
68 function ($arguments, $str) {
69 return VersionNumberUtility::convertVersionNumberToInteger(TYPO3_branch) >=
70 VersionNumberUtility::convertVersionNumberToInteger($str);
71 }
72 );
73 }
74
75 protected function getLikeFunction(): ExpressionFunction
76 {
77 return new ExpressionFunction('like', function () {
78 // Not implemented, we only use the evaluator
79 }, function ($arguments, $haystack, $needle) {
80 $result = StringUtility::searchStringWildcard((string)$haystack, (string)$needle);
81 return $result;
82 });
83 }
84
85 protected function getEnvFunction(): ExpressionFunction
86 {
87 return ExpressionFunction::fromPhp('getenv');
88 }
89
90 protected function getDateFunction(): ExpressionFunction
91 {
92 return new ExpressionFunction('date', function () {
93 // Not implemented, we only use the evaluator
94 }, function ($arguments, $format) {
95 return GeneralUtility::makeInstance(Context::class)
96 ->getAspect('date')->getDateTime()->format($format);
97 });
98 }
99 }