-
Notifications
You must be signed in to change notification settings - Fork 1
/
form-handler.php
executable file
·540 lines (503 loc) · 23.5 KB
/
form-handler.php
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
<?php
/* This script allows you to receive data from forms to your email */
/* SETTINGS */
// URL of this file using https
$scriptUrl = "https://".$_SERVER["HTTP_HOST"].$_SERVER["SCRIPT_NAME"];
// Email "subject"
$title = 'New message from my Landing page';
// Email field "From" - name of sender (e.g. your first & last name)
$from_name = "John Jonson";
// Email field "From" - email of sender (e.g. "robot@domain.com")
$from_email = "robot@domain.com";
// Email to receive message - PUT YOUR EMAIL HERE (or leave it blank if you won't receive emails)
$to = ""; // e.g. my@email.com"
// Telegram integration: token of created bot
//(leave string empty if you don't want to use Telegram integration or check how to get Token here: https://designmodo.com/startup/documentation/#telegram)
$telegramToken ='';
// Telegram integration: ID of chat with created bot to send message
//(leave string empty if you don't want to use Telegram integration or check how to get Chat ID here: https://designmodo.com/startup/documentation/#telegram)
$telegramChatId = '';
// Viber integration: token of created bot
//(leave string empty if you don't want to use Viber integration or check how to get Token here: https://designmodo.com/startup/documentation/#viber)
$viberToken = '';
// MailChimp integration: Your API key
//(leave string empty if you don't want to use MailChimp integration or check how to get your API key here: https://designmodo.com/startup/documentation/#mailchimp)
$MailChimpAPIkey = '';
// MailChimp integration: ID of list where contact will be added to
//(leave string empty if you don't want to use MailChimp integration or check how to get ID of list here: https://designmodo.com/startup/documentation/#mailchimp)
$MailChimpListID = '';
// SendInBlue integration: Your API key
//(leave string empty if you don't want to use SendInBlue integration or check how to get your API key here: https://designmodo.com/startup/documentation/#sendinblue)
$sendInBlueAPIkey = '';
// SendInBlue integration: ID of list where contact will be added to
//(leave string empty if you don't want to use SendInBlue integration or check how to get ID of list here: https://designmodo.com/startup/documentation/#sendinblue)
$sendInBlueListIDs = array(); // IDs of lists to add contact to; separate by comma
// HubSpot integration: Your API key
//(leave string empty if you don't want to use HubSpot integration or check how to get your API key here: https://designmodo.com/startup/documentation/#hubspot)
$hubSpotAPIkey = '';
// Google ReCaptcha "secret". Please, get it on https://www.google.com/recaptcha/admin/create
//(leave string empty if you don't want to use gReCaptcha in your forms; learn more about gReCaptcha integration: https://designmodo.com/startup/documentation/#grecaptcha)
$gRecaptchaSecret = '';
/* END OF SETTINGS */
if (!empty($_POST)){
/* CHECK RECAPCHA RESPONSE */
if(!empty($gRecaptchaSecret)){
if(!empty($_POST["g-recaptcha-response"])){
$params = array("secret"=>$gRecaptchaSecret, "response"=>$_POST["g-recaptcha-response"]);
$query = http_build_query($params, '', '&');
$ch = curl_init("https://www.google.com/recaptcha/api/siteverify");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
$response = curl_exec($ch);
if(json_decode($response)->success!==true){
echo 'Error: wrong recaptcha.';
exit;
}
}else{
echo 'Error: wrong recaptcha.';
exit;
}
}
/* END OF CHECK RECAPCHA */
/* COLLECT DATA FROM FORM FIELDS */
foreach($_POST as $fieldKey=>$fieldValue){
if(!empty($fieldValue)){
switch($fieldKey){
case "name":
$message .= "<b>Name:</b> ".$fieldValue.'<br />';
if(empty($MailChimpContact["FNAME"]) && empty($_POST["firstname"])) $MailChimpContact["FNAME"] = $fieldValue;
if(empty($sendInBlueContact["attributes"]["FIRSTNAME"]) && empty($_POST["firstname"])) $sendInBlueContact["attributes"]["FIRSTNAME"] = $fieldValue;
if(empty($hubSpotContact["firstname"]) && empty($_POST["firstname"])) $hubSpotContact["firstname"] = $fieldValue;
continue;
case "firstname":
$message .= "<b>First Name:</b> ".$fieldValue.'<br />';
if(empty($MailChimpContact["FNAME"])) $MailChimpContact["FNAME"] = $fieldValue;
if(empty($sendInBlueContact["attributes"]["FIRSTNAME"])) $sendInBlueContact["attributes"]["FIRSTNAME"] = $fieldValue;
if(empty($hubSpotContact["firstname"]) && empty($_POST["firstname"])) $hubSpotContact["firstname"] = $fieldValue;
continue;
case "lastname":
$message .= "<b>Last Name:</b> ".$fieldValue.'<br />';
$MailChimpContact["LNAME"] = $fieldValue;
$sendInBlueContact["attributes"]["LASTNAME"] = $fieldValue;
$hubSpotContact["lastname"] = $fieldValue;
continue;
case "phone":
$message .= "<b>Phone:</b> ".$fieldValue.'<br />';
$MailChimpContact["PHONE"] = $fieldValue;
$sendInBlueContact["attributes"]["sms"] = str_replace(' ','',str_replace('-','',str_replace('(','',str_replace(')','',$fieldValue))));
$hubSpotContact["phone"] = $fieldValue;
continue;
case "email":
$message .= "<b>E-mail:</b> ".$fieldValue.'<br />';
$sendInBlueContact["email"] = $fieldValue;
$hubSpotContact["email"] = $fieldValue;
continue;
case "username":
$message .= "<b>Username:</b> ".$fieldValue.'<br />';
if(empty($MailChimpContact["FNAME"]) && empty($_POST["name"]) && empty($_POST["firstname"])){
$MailChimpContact["FNAME"] = $fieldValue;
}
if(empty($sendInBlueContact["attributes"]["FIRSTNAME"]) && empty($_POST["name"]) && empty($_POST["firstname"])){
$sendInBlueContact["attributes"]["FIRSTNAME"] = $fieldValue;
}
if(empty($hubSpotContact["firstname"]) && empty($_POST["name"]) && empty($_POST["firstname"])){
$hubSpotContact["firstname"] = $fieldValue;
}
continue;
case "username2":
$message .= "<b>Username:</b> ".$fieldValue.'<br />';
if(empty($MailChimpContact["LNAME"]) && empty($_POST["lastname"])){
$MailChimpContact["LNAME"] = $fieldValue;
}
if(empty($sendInBlueContact["attributes"]["LASTNAME"]) && empty($_POST["lastname"])){
$sendInBlueContact["attributes"]["LASTNAME"] = $fieldValue;
}
if(empty($hubSpotContact["lastname"]) && empty($_POST["lastname"])){
$hubSpotContact["lastname"] = $fieldValue;
}
continue;
case "password":
$message .= "<b>Password:</b> ".$fieldValue.'<br />';
continue;
case "password2":
$message .= "<b>Password:</b> ".$fieldValue.'<br />';
continue;
case "budget":
$message .= "<b>Budget:</b> ".$fieldValue.'<br />';
$hubSpotContact["hs_content_membership_notes"] .= 'Budget: '.$fieldValue."\n";
continue;
case "company_size":
$message .= "<b>Company Size:</b> ".$fieldValue.'<br />';
$hubSpotContact["company_size"] = $fieldValue;
continue;
case "card":
$message .= "<b>Card Number:</b> ".$fieldValue.'<br />';
continue;
case "exp":
$message .= "<b>Expiration date:</b> ".$fieldValue.'<br />';
continue;
case "cvv":
$message .= "<b>CVV:</b> ".$fieldValue.'<br />';
continue;
case "zip":
$message .= "<b>ZIP code:</b> ".$fieldValue.'<br />';
$MailChimpContact["ADDRESS"]['zip'] = $fieldValue;
$sendInBlueContact["attributes"]["ZIP_CODE"] = $fieldValue;
$hubSpotContact["zip"] = $fieldValue;
continue;
case "country":
$message .= "<b>Country:</b> ".$fieldValue.'<br />';
$MailChimpContact["ADDRESS"]['country'] = $fieldValue;
$hubSpotContact["country"] = $fieldValue;
continue;
case "city":
$message .= "<b>City:</b> ".$fieldValue.'<br />';
$MailChimpContact["ADDRESS"]['city'] = $fieldValue;
$sendInBlueContact["attributes"]["CITY"] = $fieldValue;
$hubSpotContact["city"] = $fieldValue;
continue;
case "address":
$message .= "<b>Address:</b> ".$fieldValue.'<br />';
$MailChimpContact["ADDRESS"]['addr1'] = $fieldValue;
$sendInBlueContact["attributes"]["ADDRESS"] = (!empty($_POST["country"])) ? $_POST["country"].$fieldValue : $fieldValue;
$hubSpotContact["address"] = $fieldValue;
continue;
case "method":
$message .= "<b>Payment method:</b> ".$fieldValue.'<br />';
continue;
case "coupon":
$message .= "<b>Coupon code:</b> ".$fieldValue.'<br />';
continue;
case "message":
$message .= "<b>Message:</b> ".str_replace("\n", "<br />", $fieldValue).'<br />';
$hubSpotContact["message"] = $fieldValue;
continue;
case "send_copy":
$message .= "<b>User checked field:</b> Send me a copy<br />";
continue;
case "remember":
$message .= "<b>User checked field:</b> Remember me<br />";
continue;
case "rules":
$message .= "<b>User checked field:</b> I agree to the Terms of Service<br />";
continue;
default:
$message .= "<b>".str_replace("_"," ",ucfirst($fieldKey)).":</b> ".str_replace("\n", "<br />", $fieldValue).'<br />';
$hubSpotContact["hs_content_membership_notes"] .= str_replace("_"," ",ucfirst($fieldKey)).': '.$fieldValue."\n";
}
}
}
/* END OF COLLECT DATA FROM FORM FIELDS */
/* SEND DATA FROM FORMS TO YOUR EMAIL */
if(!empty($to)){
$subject = $title;
$headers = "Content-Type: text/html; charset=UTF-8\r\n";
$headers .= "From: \"".$from_name."\" <".$from_email.">\r\n";
$headers .= "Reply-To: \"".$from_name."\" <".$from_email.">\r\n";
$mail = mail($to, $subject, $message, $headers); // send email
}
/* MAILCHIMP INTEGRATION */
if(!empty($MailChimpAPIkey) && !empty($MailChimpListID) && !empty($_POST['email'])){
$MailChimpSubdomain = explode("-",$MailChimpAPIkey)[1];
$MailChimpAddRequestUrl = 'https://'.$MailChimpSubdomain.'.api.mailchimp.com/3.0/lists/'.$MailChimpListID.'/members/';
$MailChimpEditRequestUrl = 'https://'.$MailChimpSubdomain.'.api.mailchimp.com/3.0/lists/'.$MailChimpListID.'/members/'.md5(strtolower($_POST['email']));
// The data to send to the API
if(!empty($MailChimpContact["ADDRESS"])){
// Check required fields for address, they should not be empty
if(empty($MailChimpContact["ADDRESS"]["addr1"])) $MailChimpContact["ADDRESS"]["addr1"]='Address not set';
if(empty($MailChimpContact["ADDRESS"]["city"])) $MailChimpContact["ADDRESS"]["city"]='City not set';
if(empty($MailChimpContact["ADDRESS"]["state"])) $MailChimpContact["ADDRESS"]["state"]='State not set';
if(empty($MailChimpContact["ADDRESS"]["zip"])) $MailChimpContact["ADDRESS"]["zip"]='ZIP not set';
}
$SubscriberData = array(
"email_address" => $_POST['email'],
"status" => "subscribed",
"merge_fields" => $MailChimpContact,
);
// Setup cURL
$ch = curl_init($MailChimpAddRequestUrl);
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
'Authorization: apikey '.$MailChimpAPIkey,
'Content-Type: application/json'
),
CURLOPT_POSTFIELDS => json_encode($SubscriberData),
));
// Send the request
$MailChimpResult = json_decode(curl_exec($ch));
curl_close($ch);
// if this member already in your MailChimp list, update his info
if($MailChimpResult->status==400 && $MailChimpResult->title=="Member Exists"){
$ch = curl_init($MailChimpEditRequestUrl);
curl_setopt_array($ch, array(
CURLOPT_USERPWD => 'user:'.$MailChimpAPIkey,
CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POSTFIELDS => json_encode($SubscriberData),
));
// Send the request
$MailChimpResult = json_decode(curl_exec($ch));
curl_close($ch);
}
}
/* END OF MAILCHIMP INTEGRATION */
/* SENDINBLUE INTEGRATION */
if(!empty($sendInBlueAPIkey) && !empty($sendInBlueListIDs) && (!empty($_POST['email']) || !empty($_POST['phone']))){
$APIurl = 'https://api.sendinblue.com/v3/contacts';
$sendInBlueContact["listIds"]=$sendInBlueListIDs;
// Setup cURL
$ch = curl_init($APIurl);
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
'api-key: '.$sendInBlueAPIkey,
'content-type: application/json',
'accept: application/json',
),
CURLOPT_POSTFIELDS => json_encode($sendInBlueContact),
));
// Send the request
$sendInBlueResult = json_decode(curl_exec($ch));
curl_close($ch);
if($sendInBlueResult->id){
$sendInBlueResult = "ok";
}
// if this member already in your SendInBlue contacts, update his info
if($sendInBlueResult->code=="duplicate_parameter"){
if(empty($sendInBlueContact["email"])){
$sendInBlueContact["email"] = str_replace('+','',$sendInBlueContact["attributes"]["sms"])."@mailin-sms.com";
}
$ch = curl_init($APIurl.'/'.urlencode($sendInBlueContact["email"]));
var_dump($APIurl.'/'.urlencode($sendInBlueContact["email"]));
curl_setopt_array($ch, array(
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
'api-key: '.$sendInBlueAPIkey,
'content-type: application/json',
'accept: application/json',
),
CURLOPT_POSTFIELDS => json_encode($sendInBlueContact),
));
// Send the request
$sendInBlueResult = json_decode(curl_exec($ch));
$sendInBlueResponseCode = curl_getinfo($ch,CURLINFO_RESPONSE_CODE);
if($sendInBlueResult==NULL && $sendInBlueResponseCode==204){
$sendInBlueResult = "ok";
}
curl_close($ch);
}
}
/* END OF SENDINBLUE INTEGRATION */
/* HUBSPOT INTEGRATION */
if(!empty($hubSpotAPIkey)){
if(!empty($hubSpotContact)){
foreach($hubSpotContact as $key=>$value){
$properties[] = array(
'property' => $key,
'value' => $value
);
}
$properties = array('properties' => $properties);
$properties = json_encode($properties);
$ch = @curl_init();
@curl_setopt($ch, CURLOPT_POST, true);
@curl_setopt($ch, CURLOPT_POSTFIELDS, $properties);
@curl_setopt($ch, CURLOPT_URL, 'https://api.hubapi.com/contacts/v1/contact?hapikey='.$hubSpotAPIkey);
@curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
@curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$hubSpotResult = @curl_exec($ch);
$status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE);
@curl_close($ch);
if($status_code==200){
$hubSpotResult = "ok";
}else{
$hubSpotResult = json_decode($hubSpotResult);
if($hubSpotResult->error == "CONTACT_EXISTS"){ // Then update existing contact
$ch = @curl_init();
@curl_setopt($ch, CURLOPT_POST, true);
@curl_setopt($ch, CURLOPT_POSTFIELDS, $properties);
@curl_setopt($ch, CURLOPT_URL, 'https://api.hubapi.com/contacts/v1/contact/vid/'.$hubSpotResult->identityProfile->vid.'/profile?hapikey='.$hubSpotAPIkey);
@curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
@curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$hubSpotResult = @curl_exec($ch);
$status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE);
@curl_close($ch);
if($status_code==204){
$hubSpotResult = "ok";
}else{
$hubSpotResult = json_decode($hubSpotResult);
$hubSpotResult = 'Error: Message was not sent to HubSpot. Error message: '.$hubSpotResult->message.'. Check <a href="https://designmodo.com/startup/documentation/#hubspot" target="_blank" class="link color-transparent-white">how to set HubSpot integration</a>.';
}
}else{
$hubSpotResult = 'Error: Message was not sent to HubSpot. Error message: '.$hubSpotResult->message.'. Check <a href="https://designmodo.com/startup/documentation/#hubspot" target="_blank" class="link color-transparent-white">how to set HubSpot integration</a>.';
}
}
}else{
$hubSpotResult = 'Error: Message was not sent to HubSpot. Please, add at least one field to your form that could be added to HubSpot. Check <a href="https://designmodo.com/startup/documentation/#hubspot" target="_blank" class="link color-transparent-white">what fields are supported</a>.';
}
}
/* END OF HUBSPOT INTEGRATION */
/* TELEGRAM INTEGRATION */
if(!empty($telegramChatId) && !empty($telegramToken)){
$telegramMessage = $message;
$telegramMessage = str_replace("<br>","\n",$telegramMessage);
$telegramMessage = str_replace("<br/>","\n",$telegramMessage);
$telegramMessage = str_replace("<br />","\n",$telegramMessage);
if(!empty($telegramMessage)){
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://api.telegram.org/bot'.$telegramToken.'/sendMessage?chat_id='.$telegramChatId.'&text='.urlencode($telegramMessage).'&parse_mode=HTML',
]);
$telegramResult = json_decode(curl_exec($curl));
curl_close($curl);
}
}
/* END OF TELEGRAM INTEGRATION */
/* VIBER INTEGRATION */
if(!empty($viberToken)){
if(file_exists("viberUserID.txt") && file_exists("viberWebHook.txt")){
$viberUserID = file_get_contents("viberUserID.txt");
$viberMessage = $message;
$viberMessage = str_replace("<br>","\n",$viberMessage);
$viberMessage = str_replace("<br/>","\n",$viberMessage);
$viberMessage = str_replace("<br />","\n",$viberMessage);
$viberMessage = str_replace("<b>","",$viberMessage);
$viberMessage = str_replace("</b>","",$viberMessage);
$ch = curl_init("https://chatapi.viber.com/pa/send_message");
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
"X-Viber-Auth-Token: ".$viberToken,
),
CURLOPT_POSTFIELDS => json_encode(array(
"receiver"=>$viberUserID,
"type"=>"text",
"sender"=>array(
"name"=>"Startup Notification Bot"
),
"text"=>$viberMessage,
)),
));
// Send the request
$viberResult = json_decode(curl_exec($ch),true);
if($viberResult["status"]==0 && $viberResult["status_message"]=="ok"){
$viberResult = "ok";
}else{
$viberResult = 'Error: Message was not sent. Viber error message: '.$viberResult["status_message"].'. Check <a href="https://designmodo.com/startup/documentation/#viber" target="_blank" class="link color-transparent-white">how to set up viber integration</a>.';
}
}else{
if(!file_exists("viberWebHook.txt")){ // webhook is not set
$ch = curl_init("https://chatapi.viber.com/pa/set_webhook");
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
"X-Viber-Auth-Token: ".$viberToken,
),
CURLOPT_POSTFIELDS => json_encode(array(
"url"=>$scriptUrl,
"event_types"=>array("message", "subscribed", "unsubscribed", "conversation_started"),
"send_name"=> true,
"send_photo"=> true
)),
));
// Send the request
$viberResult = json_decode(curl_exec($ch), true);
if($viberResult["status_message"]=="ok"){
$f = fopen("viberWebHook.txt","w");
fclose($f);
clearstatcache();
$viberResult = 'Viber webhook successfully set! Now you need to subscribe to your Viber bot and send it a message. Check <a href="https://designmodo.com/startup/documentation/#viber" target="_blank" class="link color-transparent-white">more info here</a>.';
}else{
$viberResult = 'Error: Viber webhook is not set! Viber error message: '.$viberResult["status_message"].'. Check <a href="https://designmodo.com/startup/documentation/#viber" target="_blank" class="link color-transparent-white">how to set up viber integration</a>.';
}
}else{
$viberResult = 'Error: file viberUserID.txt does not exist or empty. You need to subscribe to your Viber bot and send it a message. Check <a href="https://designmodo.com/startup/documentation/#viber" target="_blank" class="link color-transparent-white">how to set up viber integration</a>.';
}
}
}
/* END OF VIBER INTEGRATION */
/* MAKE A RESPONSE */
$response = "Error: unknown error. Please, check if your hosting supports PHP.";
if(empty($to) && empty($MailChimpAPIkey) && empty($MailChimpListID) && empty($telegramToken) && empty($telegramChatId) && empty($viberToken)){
$response = 'Error: not any integration is set. Check our <a href="https://designmodo.com/startup/documentation/" target="_blank" class="link color-transparent-white">documentation</a> to know how to do that.';
}
if(!empty($to)){
if($mail){
$response = "ok";
}else{
$response = 'Error: PHP mail() function returns "false". Check is "$to" email address set and your hosting supports PHP mail() function. Also check <a href="https://designmodo.com/startup/documentation/#form-handler" target="_blank" class="link color-transparent-white">how to set up forms data sending</a>.';
}
}
if(!empty($MailChimpAPIkey) && !empty($MailChimpListID)){
if($MailChimpResult->id){
$response = "ok";
}else{
$response = 'Error: Data was not sent to MailChimp. MailChimp error message: "'.$MailChimpResult->title.'. '.$MailChimpResult->detail.'". Suggestion: check if "$MailChimpAPIkey" and "$MailChimpListID" are set correctly, form has <input name="email"> field filled. Also check <a href="https://designmodo.com/startup/documentation/#mailchimp" target="_blank" class="link color-transparent-white">how to make MailChimp integration</a>.';
if(empty($_POST['email'])){
$response = 'Error: Data was not sent to MailChimp, "email" field is empty.';
}
}
}
if(!empty($sendInBlueAPIkey) && !empty($sendInBlueListIDs)){
if($sendInBlueResult=="ok"){
$response = "ok";
}else{
$response = 'Error: Data was not sent to SendInBlue. MailChimp SendInBlue message: "'.$sendInBlueResult->message.' (error code = '.$sendInBlueResult->code.')". Suggestion: check if your form has <input name="email"> or <input name="phone"> field filled. Also check <a href="https://designmodo.com/startup/documentation/#sendinblue" target="_blank" class="link color-transparent-white">how to make SendInBlue integration</a>.';
if(empty($_POST['email']) && empty($_POST['phone'])){
$response = 'Error: Data was not sent to SendInBlue. Both "email" and "phone" fields are empty.';
}
}
}
if(!empty($hubSpotAPIkey)){
$response = $hubSpotResult;
}
if(!empty($telegramToken) && !empty($telegramChatId)){
if($telegramResult->ok){
$response = "ok";
}else{
$response = 'Error: Data was not sent to Telegram. Telegram error message: "'.$telegramResult->description.'". Suggestion: check if "$telegramToken" and "$telegramChatId" are set correctly. Also check <a href="https://designmodo.com/startup/documentation/#telegram" target="_blank" class="link color-transparent-white">how to make Telegram integration</a>.';
}
}
if(!empty($viberToken)){
$response = $viberResult;
}
echo $response;
}else{
echo 'Error: $_POST PHP variable is empty (no data received from form). Check if your <form> tag has method="POST" attribute.';
}
/* Viber webhook */
if(!empty($viberToken)){
$viberUserFile = "viberUserID.txt";
if(!file_exists($viberUserFile)){ // create file if not exists
$f = fopen($viberUserFile,"w");
fclose($f);
clearstatcache();
}
$viberUserID = file_get_contents($viberUserFile);
if(strlen($viberUserID)==0){ // if no stored users to send messages
$callback = file_get_contents("php://input"); // callback from viber
$callbackArray = json_decode($callback,true);
if(!empty($callbackArray["event"])){
if($callbackArray["event"]=="message" || $callbackArray["event"]=="conversation_started" || $callbackArray["event"]=="subscribed"){
// save ID of user who have interaction with bot
if($callbackArray["event"]!="message"){
$userId = $callbackArray["user"]["id"];
}else{
$userId = $callbackArray["sender"]["id"];
}
file_put_contents($viberUserFile,$userId);
$viberUserID = $userId;
}
}
}
}