Skip to content

Commit

Permalink
Notifications api by basic auth
Browse files Browse the repository at this point in the history
  • Loading branch information
lucatacconi committed May 19, 2023
1 parent 1c52b7e commit e55433c
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 9 deletions.
8 changes: 0 additions & 8 deletions routes/api/task.php
Original file line number Diff line number Diff line change
Expand Up @@ -781,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 @@ -908,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
91 changes: 90 additions & 1 deletion routes/api/tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,99 @@

$app->group('/tools', function (RouteCollectorProxy $group) {

$group->get('/test-conn', function (Request $request, Response $response, array $args) {
$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"]);

unset($messageObject);

$aSENT = [];
foreach ($params["RECIPIENT_LST"] ?? [] as $recipient_name => $recipient_mail) {

$messageObject = (new Email())
->from($from)
->subject($subject)
->text($message)
;

$messageObject->addTo(new Address($recipient_mail, $recipient_name));

if($mailer->send($messageObject)){
$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)
Expand Down

0 comments on commit e55433c

Please sign in to comment.