From 14808eb4b0fa1f41c59677533c648ecbabf86fac Mon Sep 17 00:00:00 2001 From: osaajani <> Date: Wed, 1 Dec 2021 23:53:50 +0100 Subject: [PATCH 1/7] Add first version of is_birthdate to ruler engine --- controllers/internals/ExpressionProvider.php | 42 +++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/controllers/internals/ExpressionProvider.php b/controllers/internals/ExpressionProvider.php index 7ee1f9c..4e9cce6 100644 --- a/controllers/internals/ExpressionProvider.php +++ b/controllers/internals/ExpressionProvider.php @@ -11,6 +11,7 @@ namespace controllers\internals; +use DateTime; use Symfony\Component\ExpressionLanguage\ExpressionFunction; use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface; @@ -37,15 +38,54 @@ class ExpressionProvider implements ExpressionFunctionProviderInterface return isset($var); }); + //Birthdate allow to compare two date to check if a date is a birthdate + $is_birthdate = new ExpressionFunction('is_birthdate', function ($birthdate, $comparison_date = null, $birthdate_format = null, $comparison_date_format = null) + { + return sprintf('isset(%1$s) && (new DateTime(%1$s, %3$s ?? null))->format(\'m-d\') == (new DateTime(%2$s ?? \'now\', %4$s ?? null))->format(\'m-d\'))', $birthdate, $comparison_date, $birthdate_format, $comparison_date_format); + }, function ($arguments, $birthdate, $comparison_date = null, $birthdate_format = null, $comparison_date_format = null) + { + if (!$birthdate) + { + return false; + } + + if ($birthdate_format) + { + $birthdate = DateTime::createFromFormat($birthdate_format, $birthdate); + } + else + { + $birthdate = new DateTime($birthdate); + } + + if ($comparison_date_format) + { + $comparison_date = DateTime::createFromFormat($comparison_date_format, $comparison_date); + } + else + { + $comparison_date = new DateTime($comparison_date ?? 'now'); + } + + if (!$birthdate || !$comparison_date) + { + return false; + } + + return ($birthdate->format('m-d') == $comparison_date->format('m-d')); + }); + return [ $neutralized_constant, $exists, + $is_birthdate, ExpressionFunction::fromPhp('mb_strtolower', 'lower'), ExpressionFunction::fromPhp('mb_strtoupper', 'upper'), ExpressionFunction::fromPhp('mb_substr', 'substr'), ExpressionFunction::fromPhp('mb_strlen', 'strlen'), ExpressionFunction::fromPhp('abs', 'abs'), - ExpressionFunction::fromPhp('strtotime', 'date'), + ExpressionFunction::fromPhp('date', 'strtotime'), + ExpressionFunction::fromPhp('date', 'date'), ]; } } From 6bd18c95cc2096996d5b66afe904d7441f26685b Mon Sep 17 00:00:00 2001 From: osaajani <> Date: Thu, 2 Dec 2021 00:30:21 +0100 Subject: [PATCH 2/7] change is_birthdate to a simpler is_birthday always using current date. Add date to get current date to the wanted format. Add intval and boolval --- controllers/internals/ExpressionProvider.php | 43 ++++++-------------- 1 file changed, 12 insertions(+), 31 deletions(-) diff --git a/controllers/internals/ExpressionProvider.php b/controllers/internals/ExpressionProvider.php index 4e9cce6..5cb2d4e 100644 --- a/controllers/internals/ExpressionProvider.php +++ b/controllers/internals/ExpressionProvider.php @@ -38,54 +38,35 @@ class ExpressionProvider implements ExpressionFunctionProviderInterface return isset($var); }); - //Birthdate allow to compare two date to check if a date is a birthdate - $is_birthdate = new ExpressionFunction('is_birthdate', function ($birthdate, $comparison_date = null, $birthdate_format = null, $comparison_date_format = null) + //Check if today is birthday given a birthdate as string and a potential format + $is_birthday = new ExpressionFunction('is_birthday', function ($birthdate, $format = null) { - return sprintf('isset(%1$s) && (new DateTime(%1$s, %3$s ?? null))->format(\'m-d\') == (new DateTime(%2$s ?? \'now\', %4$s ?? null))->format(\'m-d\'))', $birthdate, $comparison_date, $birthdate_format, $comparison_date_format); - }, function ($arguments, $birthdate, $comparison_date = null, $birthdate_format = null, $comparison_date_format = null) + 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) { - if (!$birthdate) + if (!($birthdate ?? false)) { return false; } + + $birthdate = $format ? DateTime::createFromFormat($format, $birthdate) : new DateTime($birthdate); - if ($birthdate_format) - { - $birthdate = DateTime::createFromFormat($birthdate_format, $birthdate); - } - else - { - $birthdate = new DateTime($birthdate); - } - - if ($comparison_date_format) - { - $comparison_date = DateTime::createFromFormat($comparison_date_format, $comparison_date); - } - else - { - $comparison_date = new DateTime($comparison_date ?? 'now'); - } - - if (!$birthdate || !$comparison_date) - { - return false; - } - - return ($birthdate->format('m-d') == $comparison_date->format('m-d')); + return ($birthdate && ($birthdate->format('m-d') == (new DateTime())->format('m-d'))); }); return [ $neutralized_constant, $exists, - $is_birthdate, + $is_birthday, ExpressionFunction::fromPhp('mb_strtolower', 'lower'), ExpressionFunction::fromPhp('mb_strtoupper', 'upper'), ExpressionFunction::fromPhp('mb_substr', 'substr'), ExpressionFunction::fromPhp('mb_strlen', 'strlen'), ExpressionFunction::fromPhp('abs', 'abs'), - ExpressionFunction::fromPhp('date', 'strtotime'), + ExpressionFunction::fromPhp('strtotime', 'strtotime'), ExpressionFunction::fromPhp('date', 'date'), + ExpressionFunction::fromPhp('intval', 'intval'), + ExpressionFunction::fromPhp('boolval', 'boolval'), ]; } } From 3f632e9db7143765534f30fd4b91e1f6a51104cf Mon Sep 17 00:00:00 2001 From: osaajani <> Date: Thu, 2 Dec 2021 01:03:42 +0100 Subject: [PATCH 3/7] Catch php error in ruler evaluate. Unify all dates functions by using real DateTime objects and function date -> date_create and date_from_format -> date_create_from_format. --- controllers/internals/ExpressionProvider.php | 17 ++++++++--------- controllers/internals/Ruler.php | 12 +++++------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/controllers/internals/ExpressionProvider.php b/controllers/internals/ExpressionProvider.php index 5cb2d4e..9d056ab 100644 --- a/controllers/internals/ExpressionProvider.php +++ b/controllers/internals/ExpressionProvider.php @@ -38,20 +38,18 @@ class ExpressionProvider implements ExpressionFunctionProviderInterface return isset($var); }); - //Check if today is birthday given a birthdate as string and a potential format - $is_birthday = new ExpressionFunction('is_birthday', function ($birthdate, $format = null) + //Check if today is birthday given a birthdate as DateTime + $is_birthday = new ExpressionFunction('is_birthday', function ($birthdate) { - 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) + return sprintf('isset(%1$s) && is_a(%1$s, \'DateTime\') && %1$s->format(\'m-d\') == (new \\DateTime())->format(\'m-d\')', $birthdate); + }, function ($arguments, DateTime $birthdate) { if (!($birthdate ?? false)) { return false; } - - $birthdate = $format ? DateTime::createFromFormat($format, $birthdate) : new DateTime($birthdate); - return ($birthdate && ($birthdate->format('m-d') == (new DateTime())->format('m-d'))); + return $birthdate->format('m-d') == (new DateTime())->format('m-d'); }); return [ @@ -63,10 +61,11 @@ class ExpressionProvider implements ExpressionFunctionProviderInterface ExpressionFunction::fromPhp('mb_substr', 'substr'), ExpressionFunction::fromPhp('mb_strlen', 'strlen'), ExpressionFunction::fromPhp('abs', 'abs'), - ExpressionFunction::fromPhp('strtotime', 'strtotime'), - ExpressionFunction::fromPhp('date', 'date'), + ExpressionFunction::fromPhp('date_create', 'date'), + ExpressionFunction::fromPhp('date_create_from_format', 'date_from_format'), ExpressionFunction::fromPhp('intval', 'intval'), ExpressionFunction::fromPhp('boolval', 'boolval'), + ExpressionFunction::fromPhp('count', 'count'), ]; } } diff --git a/controllers/internals/Ruler.php b/controllers/internals/Ruler.php index 1682ef4..0dc3103 100644 --- a/controllers/internals/Ruler.php +++ b/controllers/internals/Ruler.php @@ -43,18 +43,15 @@ use Symfony\Component\ExpressionLanguage\ExpressionLanguage; { try { - $this->expression_language->parse($condition, array_keys($data)); + //Use @ to hide notices on non defined vars + @$this->expression_language->parse($condition, array_keys($data)); return true; } - catch (\Exception $e) + catch (\Throwable $t) //Catch both, exceptions and php error { return false; } - catch (\Throwable $t) - { - //Just ignore non critical php warning and notice - } } /** @@ -69,11 +66,12 @@ use Symfony\Component\ExpressionLanguage\ExpressionLanguage; { try { + //Use @ to hide notices on non defined vars @$result = $this->expression_language->evaluate($condition, $data); return (bool) $result; } - catch (\Exception $e) + catch (\Throwable $t) //Catch both, exceptions and php error { return null; } From cc188f3118796d21bfde15c89f929108320b6c4d Mon Sep 17 00:00:00 2001 From: osaajani <> Date: Tue, 21 Dec 2021 14:43:07 +0100 Subject: [PATCH 4/7] Add delay between two read operation on daemon phones to prevent too many api call --- daemons/Phone.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/daemons/Phone.php b/daemons/Phone.php index 1be7856..1d54b47 100644 --- a/daemons/Phone.php +++ b/daemons/Phone.php @@ -20,6 +20,7 @@ use Monolog\Logger; class Phone extends AbstractDaemon { private $max_inactivity = 5 * 60; + private $read_delay = 20 / 0.5; private $msg_queue; private $msg_queue_id; private $webhook_queue; @@ -56,13 +57,20 @@ class Phone extends AbstractDaemon { usleep(0.5 * 1000000); //Micro sleep for perfs + $read_tick = ($read_tick ?? 0) + 1; //Count tick + $this->bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8'); //Send smss in queue $this->send_smss(); - //Read received smss - $this->read_smss(); + //Read only every x ticks (x/2 seconds) to prevent too many call + if ($read_tick >= $this->read_delay) + { + //Read received smss + $this->read_smss(); + $read_tick = 0; + } //Stop after 5 minutes of inactivity to avoid useless daemon if ((microtime(true) - $this->last_message_at) > $this->max_inactivity) From c69527a5ad4c9658356fb9f020cdb7afd6a57966 Mon Sep 17 00:00:00 2001 From: osaajani <> Date: Tue, 21 Dec 2021 15:00:34 +0100 Subject: [PATCH 5/7] fix counting tick for read phone --- daemons/Phone.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/daemons/Phone.php b/daemons/Phone.php index 1d54b47..28ec19c 100644 --- a/daemons/Phone.php +++ b/daemons/Phone.php @@ -21,6 +21,7 @@ class Phone extends AbstractDaemon { private $max_inactivity = 5 * 60; private $read_delay = 20 / 0.5; + private $read_tick = 0; private $msg_queue; private $msg_queue_id; private $webhook_queue; @@ -57,7 +58,7 @@ class Phone extends AbstractDaemon { usleep(0.5 * 1000000); //Micro sleep for perfs - $read_tick = ($read_tick ?? 0) + 1; //Count tick + $this->read_tick += 1; $this->bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, 'UTF8'); @@ -65,11 +66,12 @@ class Phone extends AbstractDaemon $this->send_smss(); //Read only every x ticks (x/2 seconds) to prevent too many call - if ($read_tick >= $this->read_delay) + if ($this->read_tick >= $this->read_delay) { //Read received smss $this->read_smss(); - $read_tick = 0; + $this->logger->info('Read smss' ); + $this->read_tick = 0; } //Stop after 5 minutes of inactivity to avoid useless daemon From 85c09673b1857a270650a156627569436c66b5d9 Mon Sep 17 00:00:00 2001 From: osaajani <> Date: Tue, 21 Dec 2021 15:01:46 +0100 Subject: [PATCH 6/7] remove useless log --- daemons/Phone.php | 1 - 1 file changed, 1 deletion(-) diff --git a/daemons/Phone.php b/daemons/Phone.php index 28ec19c..a6c7ba9 100644 --- a/daemons/Phone.php +++ b/daemons/Phone.php @@ -70,7 +70,6 @@ class Phone extends AbstractDaemon { //Read received smss $this->read_smss(); - $this->logger->info('Read smss' ); $this->read_tick = 0; } From b5d148304874a27a5cfbdccd8956042bf8b87db3 Mon Sep 17 00:00:00 2001 From: osaajani <> Date: Tue, 21 Dec 2021 15:03:10 +0100 Subject: [PATCH 7/7] Add delay on direct sms reading to prevent api rate limit exceed --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index 5103369..293fbd1 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -v3.2.3 +v3.2.4