DefaultFunctionsProvider.php 4.53 KB
Newer Older
1
<?php
2

3
declare(strict_types=1);
4
5
6
7
8
9
10
11
12
13
14
15
16
17

/*
 * This file is part of the TYPO3 CMS project.
 *
 * 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.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

18
19
namespace TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider;

20
21
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
22
use TYPO3\CMS\Core\Configuration\Features;
23
use TYPO3\CMS\Core\Context\Context;
24
use TYPO3\CMS\Core\Information\Typo3Version;
25
26
use TYPO3\CMS\Core\Utility\ArrayUtility;
use TYPO3\CMS\Core\Utility\Exception\MissingArrayPathException;
27
28
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\StringUtility;
29
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
30
31
32
33
34
35
36
37
38
39
40
41
42

/**
 * Class DefaultFunctionsProvider
 * @internal
 */
class DefaultFunctionsProvider implements ExpressionFunctionProviderInterface
{
    /**
     * @return ExpressionFunction[] An array of Function instances
     */
    public function getFunctions()
    {
        return [
43
44
            $this->getIpFunction(),
            $this->getCompatVersionFunction(),
45
46
47
            $this->getLikeFunction(),
            $this->getEnvFunction(),
            $this->getDateFunction(),
48
            $this->getFeatureToggleFunction(),
49
            $this->getTraverseArrayFunction(),
50
51
52
        ];
    }

53
54
55
56
    protected function getIpFunction(): ExpressionFunction
    {
        return new ExpressionFunction(
            'ip',
57
            static function () {
58
59
                // Not implemented, we only use the evaluator
            },
60
            static function ($arguments, $str) {
61
62
63
64
65
66
67
68
69
70
71
72
                if ($str === 'devIP') {
                    $str = trim($GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask'] ?? '');
                }
                return (bool)GeneralUtility::cmpIP(GeneralUtility::getIndpEnv('REMOTE_ADDR'), $str);
            }
        );
    }

    protected function getCompatVersionFunction(): ExpressionFunction
    {
        return new ExpressionFunction(
            'compatVersion',
73
74
            static fn () => null, // Not implemented, we only use the evaluator
            static function ($arguments, mixed $str) {
75
76
                $typo3Version = GeneralUtility::makeInstance(Typo3Version::class);
                return VersionNumberUtility::convertVersionNumberToInteger($typo3Version->getBranch()) >=
77
                   VersionNumberUtility::convertVersionNumberToInteger((string)$str);
78
79
80
81
            }
        );
    }

82
83
    protected function getLikeFunction(): ExpressionFunction
    {
84
        return new ExpressionFunction('like', static function () {
85
            // Not implemented, we only use the evaluator
86
        }, static function ($arguments, $haystack, $needle) {
87
88
89
90
91
92
93
94
95
96
97
98
            $result = StringUtility::searchStringWildcard((string)$haystack, (string)$needle);
            return $result;
        });
    }

    protected function getEnvFunction(): ExpressionFunction
    {
        return ExpressionFunction::fromPhp('getenv');
    }

    protected function getDateFunction(): ExpressionFunction
    {
99
        return new ExpressionFunction('date', static function () {
100
            // Not implemented, we only use the evaluator
101
        }, static function ($arguments, $format) {
102
103
104
105
            return GeneralUtility::makeInstance(Context::class)
                ->getAspect('date')->getDateTime()->format($format);
        });
    }
106
107
108

    protected function getFeatureToggleFunction(): ExpressionFunction
    {
109
        return new ExpressionFunction('feature', static function () {
110
            // Not implemented, we only use the evaluator
111
        }, static function ($arguments, $featureName) {
112
113
114
115
            return GeneralUtility::makeInstance(Features::class)
                ->isFeatureEnabled($featureName);
        });
    }
116
117
118

    public function getTraverseArrayFunction(): ExpressionFunction
    {
119
        return new ExpressionFunction('traverse', static function () {
120
            // Not implemented, we only use the evaluator
121
        }, static function ($arguments, $array, $path) {
122
123
124
125
126
127
128
129
130
131
            if (!is_array($array) || !is_string($path) || $path === '') {
                return '';
            }
            try {
                return ArrayUtility::getValueByPath($array, $path);
            } catch (MissingArrayPathException $e) {
                return '';
            }
        });
    }
132
}