diff --git a/.env b/.env index 39bd369..cc9c5b6 100644 --- a/.env +++ b/.env @@ -15,3 +15,9 @@ RUN_MODE = "PRODU" #PRODU | DEVEL #Configuring the parameter CHECK_PHP_TASKS_SYNTAX to false in case of syntax errors in the tasks could cause anomalous behavior in the Crunz-ui interfaces CHECK_PHP_TASKS_SYNTAX = true #true | false + +#TOOLS BASIC AUTH CONFIGURATION +TOOLS_LOGIN = "tools_user" +TOOLS_PASSWORD = "dc7068ff-3d57-4813-88f4-c3a648e0fd58" + +#Please, update tools user grant as soon as possible diff --git a/.github/ROADMAP.md b/.github/ROADMAP.md index 4d19132..8c7da00 100644 --- a/.github/ROADMAP.md +++ b/.github/ROADMAP.md @@ -10,6 +10,15 @@ ## Release history +### v.2.7.10 (2023-05-19) + +Crunz-ui updated stable release: +* Tools api accessible via basic authentication +* PHP 8.1 compatibility fix +* Library version update +* Bug fixes and and code cleanup + + ### v.2.7.9 (2023-02-17) Crunz-ui updated stable release: diff --git a/composer.json b/composer.json index 0ee1757..8581f3f 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ "slim/slim": "^4", "slim/psr7": "^1", "nyholm/psr7": "^1", - "nyholm/psr7-server": "^0", + "nyholm/psr7-server": ">=0", "guzzlehttp/guzzle": "^7", "laminas/laminas-diactoros": "^2", "php-di/php-di": "^6", diff --git a/config/application.json b/config/application.json index 471917a..5409ffb 100644 --- a/config/application.json +++ b/config/application.json @@ -5,5 +5,5 @@ "authors": "Luca Tacconi, Emanuele Marchesotti", "keywords": "Slim4, Api, JWT, Vue, Vuetify, Router, Http-vue-loader, Protected area, Login system, Menu tool, Crunz, Crontab", "descriptions": "Graphical interface for GitHub Lavary/Crunz project", - "version": "v2.7.9" + "version": "v2.7.10" } diff --git a/routes/api/task.php b/routes/api/task.php index 891edef..55b51de 100644 --- a/routes/api/task.php +++ b/routes/api/task.php @@ -716,9 +716,10 @@ static function (array $file) { continue; } - if($date_ref_tmp > $event_interval_to_orig){ - break; - } + //Need a limit to avoid infinite loop + // if($date_ref_tmp > $event_interval_to_orig){ + // break; + // } $step = 1; @@ -780,10 +781,6 @@ static function (array $file) { continue; } - if($date_ref_tmp < $event_interval_from_orig){ - break; - } - $step = 1; $calculated_last_run = $date_ref_tmp; @@ -907,10 +904,6 @@ static function (array $file) { continue; } - if($calc_run_ref > $event_interval_to_orig){ - break; - } - $step = 1; if($calc_run_ref < $date_now && $past_planned_tasks != "Y"){ diff --git a/routes/api/tools.php b/routes/api/tools.php new file mode 100644 index 0000000..bcb0bd7 --- /dev/null +++ b/routes/api/tools.php @@ -0,0 +1,128 @@ +group('/tools', function (RouteCollectorProxy $group) { + + $group->post('/notification', function (Request $request, Response $response, array $args) { + + $data = []; + + $params = []; + if(!empty($request->getParsedBody())){ + $params = array_change_key_case($request->getParsedBody(), CASE_UPPER); + } + + if( empty($params["MAIL_SUBJECT"]) ) throw new Exception("ERROR - No mail subject submitted"); + if( empty($params["MAIL_CONTENT"]) ) throw new Exception("ERROR - No mail content submitted"); + if( empty($params["RECIPIENT_LST"]) || !array($params["RECIPIENT_LST"]) ) throw new Exception("ERROR - No recipient list submitted"); + + + $app_configs = $this->get('configs')["app_configs"]; + $base_path =$app_configs["paths"]["base_path"]; + + if(empty($_ENV["CRUNZ_BASE_DIR"])){ + $crunz_base_dir = $base_path; + }else{ + $crunz_base_dir = $_ENV["CRUNZ_BASE_DIR"]; + } + + if(!file_exists ( $crunz_base_dir."/crunz.yml" )) throw new Exception("ERROR - Crunz.yml configuration file not found"); + $crunz_config_yml = file_get_contents($crunz_base_dir."/crunz.yml"); + + if(empty($crunz_config_yml)) throw new Exception("ERROR - Crunz configuration file empty"); + + try { + $crunz_config = Yaml::parse($crunz_config_yml); + } catch (ParseException $exception) { + throw new Exception("ERROR - Crunz configuration file error"); + } + + if(empty($crunz_config_yml)) throw new Exception("ERROR - Crunz configuration file empty"); + + try { + $crunz_config = Yaml::parse($crunz_config_yml); + } catch (ParseException $exception) { + throw new Exception("ERROR - Crunz configuration file error"); + } + + if( !empty($crunz_config) + && !empty($crunz_config["mailer"]) + && !empty($crunz_config["mailer"]["transport"]) && $crunz_config["mailer"]["transport"] == 'smtp' + && !empty($crunz_config["mailer"]["recipients"]) + && !empty($crunz_config["mailer"]["sender_name"]) + && !empty($crunz_config["mailer"]["sender_email"]) + && !empty($crunz_config["smtp"]) + && !empty($crunz_config["smtp"]["host"]) + && !empty($crunz_config["smtp"]["port"]) + ){ + + $userPart = ''; + if(!empty($crunz_config["smtp"]["username"])){ + $userPart = $crunz_config["smtp"]["username"].":".$crunz_config["smtp"]["password"]."@"; + } + + $dsn = "smtp://".$userPart.$crunz_config["smtp"]["host"].":".$crunz_config["smtp"]["port"]."?verifyPeer=".$crunz_config["smtp"]["encryption"]; + + $transport = Transport::fromDsn($dsn); + $mailer = new Mailer($transport); + + $subject = $params["MAIL_SUBJECT"]; + $message = $params["MAIL_CONTENT"]; + + $from = new Address($crunz_config["mailer"]["sender_email"], $crunz_config["mailer"]["sender_name"]); + + $aSENT = []; + foreach ($params["RECIPIENT_LST"] ?? [] as $recipient_name => $recipient_mail) { + + unset($messageObject); + + $messageObject = (new Email()) + ->from($from) + ->subject($subject) + ->text($message) + ; + + $messageObject->addTo(new Address($recipient_mail, $recipient_name)); + + $check_mail = $mailer->send($messageObject); + + if( $check_mail == null ){ + $aSENT[$recipient_name] = 'SENT'; + }else{ + $aSENT[$recipient_name] = 'ERR'; + } + } + + }else{ + throw new Exception("ERROR - No mailer configuration found"); + } + + $data = $aSENT; + + $response->getBody()->write(json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); + return $response->withStatus(200) + ->withHeader("Content-Type", "application/json"); + }); + +}); diff --git a/routes/index.php b/routes/index.php index db79f87..13c337c 100644 --- a/routes/index.php +++ b/routes/index.php @@ -67,7 +67,24 @@ "secure" => false, "secret" => $_ENV["JWT_SECRET"], - "ignore" => [$base_path."/auth/login", $base_path."/test"], + "ignore" => [$base_path."/auth/login", $base_path."/tools" ], + + "error" => function ($response, $arguments) { + $data = []; + $data["status"] = "Authentication error"; + $data["message"] = $arguments["message"]; + + $response->getBody()->write(json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); + return $response->withHeader("Content-Type", "application/json"); + } +])); + +$app->add(new Tuupola\Middleware\HttpBasicAuthentication([ + "path" => [ $base_path."/tools" ], + "realm" => "Protected", + "users" => [ + $_ENV["TOOLS_LOGIN"] => $_ENV["TOOLS_PASSWORD"] + ], "error" => function ($response, $arguments) { $data = [];