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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
<?php
/**
*
* Class ki vsebuje funkcije Squalo APIJA (dodajanje prejemnikov, posiljanje...)
*
*/
class SqualoApi {
// Squalo api url
var $api_url = 'https://api.squalomail.com/v1/';
public function __construct(){
}
private function executeCall($action, $method, $data){
// Add credentials
$data['apiUser'] = AppSettings::getInstance()->getSetting('squalo-user');
$data['apiKey'] = AppSettings::getInstance()->getSetting('squalo-key');
// GET call - set url params
if($method == 'GET'){
$response = $this->executeGET($action, $data);
}
// POST call
else{
$response = $this->executePOST($action, $data);
}
// Decode json response
$response_array = json_decode($response, true);
// Zalogiramo kaj se je dogajalo
$SL = new SurveyLog();
// Error
if($response_array['errorCode'] != '0'){
$result['error'] = $response_array["errorMessage"]. ' (code '.$response_array["errorCode"].')';
$result['success'] = false;
$SL->addMessage(SurveyLog::MAILER, "NAPAKA pri SQUALO API klicu ('.$action.')! ".$result['error']);
}
else{
$result = $response_array;
$result['success'] = true;
$SL->addMessage(SurveyLog::MAILER, "USPEŠEN SQUALO API klic ('.$action.').");
}
$SL->write();
return $result;
}
// Izvedemo post klic
private function executePOST($action, $data){
// Nastavimo url
$url = $this->api_url.$action;
// Init curl
$ch = curl_init($url);
// JSON string za POST
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// for debug only!
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$headers = array(
"Content-Type: application/json",
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Izvedemo klic
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
// Izvedemo get klic
private function executeGET($action, $data){
// GET params
$params = '?';
foreach($data as $name => $value){
$params .= $name.'='.$value.'&';
}
$params = substr($params, 0, -1);
// Nastavimo celoten url s parametri
$url = $this->api_url.$action.$params;
// Init curl
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Izvedemo klic
$response = curl_exec($ch);
return $response;
}
/*
Ustvarimo seznam uporabnikov za pošiljanje
create-list
"name":"String content",
"description":"String content",
"listTag":"String content",
"color":"String content",
"ordering":2147483647,
"published":true
{subtag:name}
{subtag:code}
{subtag:url}
{date:4}
{unsubscribe}{/unsubscribe}
*/
public function createList($list_name){
$action = 'create-list';
$method = 'POST';
$data = array(
'name' => $list_name,
'ordering' => $list_number,
'published' => true
);
$response = $this->executeCall($action, $method, $data);
$list_id = ($response['success']) ? $response['list']['id'] : '0';
return $list_id;
}
/*
Ustvarimo email,ki se bo poslal
"altBody":"String content",
"body":"String content",
"fromEmail":"String content",
"fromName":"String content",
"language":"String content",
"listIds":[2147483647],
"published":2147483647,
"replyToEmail":"String content",
"replyToName":"String content",
"subject":"String content",
"visible":true
*/
public function createNewsletter($list_id, $subject, $body, $body_alt, $from_email, $from_name, $reply_to_email, $language){
$action = 'create-newsletter';
$method = 'POST';
$data = array(
'listIds' => array($list_id),
'subject' => $subject,
'body' => $body,
'altBody' => $body_alt,
'fromEmail' => $from_email,
'fromName' => $from_name,
'replyToEmail' => $reply_to_email,
'language' => $language,
'visible' => true
);
$response = $this->executeCall($action, $method, $data);
$newsletter_id = ($response['success']) ? $response['newsletter']['id'] : '0';
return $newsletter_id;
}
/*
Dodamo prejemnika
"accept": true,
"confirmed": true,
"customAttributes": [{
"name": "firstname",
"value": "John"
},
{
"name": "lastname",
"value": "Smith"
}
],
"email": "String content",
"enabled": true,
"html": true,
"listIds": [2147483647],
"name": "String content",
"surname": "String content",
"gender": "null | male | female | other",
"gdprCanSend": true,
"gdprCanTrack": true
*/
public function addRecipient($email, $phone, $list_id, $custom_attributes=array()){
$action = 'create-recipient';
$method = 'POST';
// Pretvorimo atribute po meri v pravo obliko
$custom_attributes_squalo = array();
$i = 0;
foreach($custom_attributes as $key => $value){
$custom_attributes_squalo[$i] = array(
"name" => $key,
"value" => $value
);
$i++;
}
$data = array(
'email' => $email,
'phone' => $phone,
'listIds' => array($list_id),
'accept' => true,
'confirmed' => true,
'enabled' => true,
'gdprCanSend' => true,
'gdprCanTrack' => true,
'customAttributes' => $custom_attributes_squalo
);
$response = $this->executeCall($action, $method, $data);
$recipient_id = ($response['success']) ? $response['recipient']['id'] : '0';
return $recipient_id;
}
/*
Pošljemo emaile
"newsletterId":2147483647,
"sendDate":2147483647
*/
public function sendEmails($newsletter_id){
$action = 'send-newsletter';
$method = 'GET';
$data = array(
'newsletterId' => $newsletter_id,
'sendDate' => time()+30
);
$response = $this->executeCall($action, $method, $data);
return $response;
}
// Pobrisemo prejemnika
public function deleteRecipient($recipient_id){
$action = 'delete-recipient';
$method = 'GET';
$data = array(
'recipientId' => $recipient_id
);
$response = $this->executeCall($action, $method, $data);
return $response;
}
}
|