Skip to content

Commit

Permalink
Merge branch 'devel'
Browse files Browse the repository at this point in the history
  • Loading branch information
lucatacconi committed May 19, 2023
2 parents 289da89 + 96cb42a commit 3766465
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 14 deletions.
6 changes: 6 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions .github/ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion config/application.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
15 changes: 4 additions & 11 deletions routes/api/task.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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"){
Expand Down
128 changes: 128 additions & 0 deletions routes/api/tools.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Routing\RouteCollectorProxy;

use Ramsey\Uuid\Uuid;

use Crunz\Configuration\Configuration;
use Crunz\Schedule;
use Crunz\Filesystem;
use Crunz\Task\Collection;
use Crunz\Task\WrongTaskInstanceException;

foreach (glob(__DIR__ . '/../classes/*.php') as $filename){
require_once $filename;
}

use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Address;

$app->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");
});

});
19 changes: 18 additions & 1 deletion routes/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down

0 comments on commit 3766465

Please sign in to comment.