davide cucurnia
2024-01-30 9f6455d9b12bda65377e8501e00557982be9ff36
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
class VolaRestMailAPI
{
 
    private $serviceUri;
    private $username;
    private $password;
    private $login_request_api_endpoint;
    private $mail_request_api_endpoint;
 
    public function __construct()
    {
        $this->serviceUri = "localhost:8000/api";
        //$this->serviceUri = "https://volamailtool-local.nginx-apps-ns:4448/api";
        $this->username = "vola01";
        $this->password = "Password01";
        $this->login_request_api_endpoint = "/auth/login";
        $this->mail_request_api_endpoint = "/mail/request";
        $this->ssl_verify_host = 0;
        $this->ssl_verify_peer = false;
    }
 
    public function mailRequest($params)
    {
        try {
            $mapping = [
                "template_mail_id" => $params["template_mail"],
                "template_mail_params" => $params["template_mail_params"],
                "recipient_to" => [$params["registration_email"]],
                "recipient_cc" => [],
                "template_pdf_params" => (isset($params["template_pdf_params"])) ? $params["template_pdf_params"] : array(),
                "attached_files" => (isset($params["attached_files"])) ? $params["attached_files"] : array(),
                //"optional_pdf_group" => "test_group",
            ];
 
            $token = $this->login();
            if ($token) {
                return ["result" => true, "idreq" => $this->call($token, $mapping)];
            }
            return ["result" => false];
 
        } catch (Exception $e) {
            error_log("Errore nell'esecuzione della chiamata a Vola Rest Mail Tool:\r".$e->getTraceAsString());
            return ["result" => false];
        }
    }
 
    private function login()
    {
        $curl = curl_init();
 
        curl_setopt_array($curl, array(
            CURLOPT_URL => $this->serviceUri.$this->login_request_api_endpoint,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => json_encode(["username"=>$this->username,"password"=>$this->password]),
            CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
            CURLOPT_SSL_VERIFYHOST => $this->ssl_verify_host,
            CURLOPT_SSL_VERIFYPEER => $this->ssl_verify_peer,
        ));
 
        $response = curl_exec($curl);
        curl_close($curl);
 
        if (isset($response)) {
            $parsing = json_decode($response);
            if (isset($parsing->token)) {
                return $parsing->token;
            } else {
                error_log("Token non ricevuto. Risposta:\r" . print_r($response, 1));
                return false;
            }
        }
        error_log("Si è verificato un errore nella chiamata di login");
        return false;
    }
 
    private function call($token, $data)
    {
        $curl = curl_init();
 
        curl_setopt_array($curl, array(
            CURLOPT_URL => $this->serviceUri.$this->mail_request_api_endpoint,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => json_encode($data),
            CURLOPT_HTTPHEADER => array(
                'Authorization: Bearer '.$token,
                'Content-Type: application/json',
            ),
            CURLOPT_SSL_VERIFYHOST => $this->ssl_verify_host,
            CURLOPT_SSL_VERIFYPEER => $this->ssl_verify_peer,
        ));
 
        $response = curl_exec($curl);
        curl_close($curl);
 
        if (isset($response)) {
            $parsing = json_decode($response);
            if (isset($parsing->result)) {
                return $parsing->result;
            } else {
                error_log("Id Req non ricevuto. Risposta:\r" . print_r($response, 1));
                return false;
            }
        }
        error_log("Si è verificato un errore nella chiamata di richiesta mail");
        return false;
    }
 
}