forked from sailthru/sailthru-wordpress-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sailthru_ajax.php
94 lines (76 loc) · 2.36 KB
/
sailthru_ajax.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
<?php
/**
* @package Sailthru
* @author Jon Tascher
*/
$wp_load = realpath("../../../wp-load.php");
if(!file_exists($wp_load)) {
$wp_config = realpath("../../../wp-config.php");
if (!file_exists($wp_config)) {
exit("Can't find wp-config.php or wp-load.php");
} else {
require_once($wp_config);
}
} else {
require_once($wp_load);
}
switch(@$_GET['action']) {
case 'subscribe':
try {
$form = new sailthru_form($_POST['form_id']);
}
catch(Exception $e) {
echo "{error: {$e->getMessage()}";
}
if(is_object($form)) {
//validate again b/c this has only passed client side (tainted) validation
$required_fields = $form->required_fields();
$errors = array();
foreach($required_fields as $field => $error) {
if(!isset($_POST[$field]) || !$_POST[$field]) {
$errors[] = $error;
}
}
if(!preg_match('/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i', $_POST['email'])) {
$errors[] = 'You must enter a valid email address.';
}
$has_lists = $form->has_lists();
$_POST['lists'] = explode(',', $_POST['lists']); //annoying hack..
if($has_lists && (!is_array($_POST['lists']) || !count($_POST['lists']))) {
$errors[] = 'You must subscribe to at least one list.';
}
if(count($errors)) {
echo json_encode($errors);
die();
}
else {
require_once('client/requires.php');
$client = new Sailthru_Client(get_option('sailthru_api_key'), get_option('sailthru_secret'));
$replacement_fields = array();
if(isset($_POST['fname'])) {
$replacement_fields['first_name'] = $_POST['fname'];
}
if(isset($_POST['lname'])) {
$replacement_fields['last_name'] = $_POST['lname'];
}
$all_lists = $form->get_all_lists();
$lists = array();
if($has_lists) {
foreach($all_lists as $id => $list_name) {
if(in_array($id, $_POST['lists'])){
$lists[$list_name]='1';
}
}
}
$client->setEmail($_POST['email'], $replacement_fields, $lists);
if ((bool)get_option('sailthru_welcome') && $template = get_option('sailthru_welcome_template')) {
require_once('client/requires.php');
$client = new Sailthru_Client(get_option('sailthru_api_key'), get_option('sailthru_secret'));
$r = $client->send($template, $_POST['email'], $replacement_fields, array());
}
echo '{}';
}
}
break;
}
die();