2019-11-27 06:25:33 +01:00
|
|
|
<?php
|
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
/*
|
|
|
|
* This file is part of RaspiSMS.
|
|
|
|
*
|
|
|
|
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
|
|
|
|
*
|
|
|
|
* This source file is subject to the GPL-3.0 license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
2019-11-27 06:25:33 +01:00
|
|
|
namespace controllers\internals;
|
|
|
|
|
2021-12-01 23:53:50 +01:00
|
|
|
use DateTime;
|
2019-11-27 06:25:33 +01:00
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
|
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
|
|
|
|
|
|
|
|
class ExpressionProvider implements ExpressionFunctionProviderInterface
|
|
|
|
{
|
|
|
|
public function getFunctions()
|
|
|
|
{
|
2020-04-03 21:22:13 +02:00
|
|
|
//Override default constant() function to make it return null
|
|
|
|
//This will prevent the use of constant() func to read constants with security impact (such as session, db credentials, etc.)
|
2020-06-23 21:06:13 +02:00
|
|
|
$neutralized_constant = new ExpressionFunction('constant', function ($str)
|
|
|
|
{
|
2020-04-03 21:22:13 +02:00
|
|
|
return null;
|
2020-06-23 21:06:13 +02:00
|
|
|
}, function ($arguments, $str)
|
|
|
|
{
|
2020-04-03 21:22:13 +02:00
|
|
|
return null;
|
|
|
|
});
|
2021-02-23 00:31:54 +01:00
|
|
|
|
2020-06-10 01:44:05 +02:00
|
|
|
//Exists must be personnalized because it inverse is_null
|
2021-01-14 03:52:54 +01:00
|
|
|
$exists = new ExpressionFunction('exists', function ($str)
|
2020-06-23 21:06:13 +02:00
|
|
|
{
|
2021-01-14 03:52:54 +01:00
|
|
|
return sprintf('isset(%1$s)', $str);
|
2020-06-23 21:06:13 +02:00
|
|
|
}, function ($arguments, $var)
|
|
|
|
{
|
2021-01-14 03:52:54 +01:00
|
|
|
return isset($var);
|
2020-06-10 01:44:05 +02:00
|
|
|
});
|
|
|
|
|
2021-12-02 00:30:21 +01:00
|
|
|
//Check if today is birthday given a birthdate as string and a potential format
|
|
|
|
$is_birthday = new ExpressionFunction('is_birthday', function ($birthdate, $format = null)
|
2021-12-01 23:53:50 +01:00
|
|
|
{
|
2021-12-02 00:30:21 +01:00
|
|
|
return sprintf('isset(%1$s) && ((%2$s ? DateTime::createFromFormat(%2$s, %1$s) : new DateTime(%1$s))->format(\'m-d\') == (new DateTime())->format(\'m-d\'))', $birthdate, $format);
|
|
|
|
}, function ($arguments, $birthdate, $format = null)
|
2021-12-01 23:53:50 +01:00
|
|
|
{
|
2021-12-02 00:30:21 +01:00
|
|
|
if (!($birthdate ?? false))
|
2021-12-01 23:53:50 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-02 00:30:21 +01:00
|
|
|
|
|
|
|
$birthdate = $format ? DateTime::createFromFormat($format, $birthdate) : new DateTime($birthdate);
|
2021-12-01 23:53:50 +01:00
|
|
|
|
2021-12-02 00:30:21 +01:00
|
|
|
return ($birthdate && ($birthdate->format('m-d') == (new DateTime())->format('m-d')));
|
2021-12-01 23:53:50 +01:00
|
|
|
});
|
|
|
|
|
2019-11-27 06:25:33 +01:00
|
|
|
return [
|
2020-04-03 21:22:13 +02:00
|
|
|
$neutralized_constant,
|
2020-06-10 01:44:05 +02:00
|
|
|
$exists,
|
2021-12-02 00:30:21 +01:00
|
|
|
$is_birthday,
|
2019-11-27 06:25:33 +01:00
|
|
|
ExpressionFunction::fromPhp('mb_strtolower', 'lower'),
|
|
|
|
ExpressionFunction::fromPhp('mb_strtoupper', 'upper'),
|
|
|
|
ExpressionFunction::fromPhp('mb_substr', 'substr'),
|
2020-06-10 01:30:14 +02:00
|
|
|
ExpressionFunction::fromPhp('mb_strlen', 'strlen'),
|
2019-11-27 06:25:33 +01:00
|
|
|
ExpressionFunction::fromPhp('abs', 'abs'),
|
2021-12-02 00:30:21 +01:00
|
|
|
ExpressionFunction::fromPhp('strtotime', 'strtotime'),
|
2021-12-01 23:53:50 +01:00
|
|
|
ExpressionFunction::fromPhp('date', 'date'),
|
2021-12-02 00:30:21 +01:00
|
|
|
ExpressionFunction::fromPhp('intval', 'intval'),
|
|
|
|
ExpressionFunction::fromPhp('boolval', 'boolval'),
|
2019-11-27 06:25:33 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|