Form.class.php
7.7 KB
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
<?php
class AC_Form extends ActiveCampaign {
public $version;
public $url_base;
public $url;
public $api_key;
function __construct($version, $url_base, $url, $api_key) {
$this->version = $version;
$this->url_base = $url_base;
$this->url = $url;
$this->api_key = $api_key;
}
function getforms($params) {
$request_url = "{$this->url}&api_action=form_getforms&api_output={$this->output}";
$response = $this->curl($request_url);
return $response;
}
function html($params) {
$request_url = "{$this->url}&api_action=form_html&api_output={$this->output}&{$params}";
$response = $this->curl($request_url);
return $response;
}
function embed($params) {
$params_array = explode("&", $params);
$params_ = array();
foreach ($params_array as $expression) {
// IE: css=1
list($var, $val) = explode("=", $expression);
$params_[$var] = $val;
}
$id = (isset($params_["id"])) ? (int)$params_["id"] : 0;
$css = (isset($params_["css"])) ? (int)$params_["css"] : 1;
$ajax = (isset($params_["ajax"])) ? (int)$params_["ajax"] : 0;
// to set the current page as the action, pass "action=", or "action=[THIS URL]"
$action = (isset($params_["action"])) ? ($params_["action"] ? $params_["action"] : "this") : "";
$html = $this->html("id={$id}");
if (is_object($html) && !(int)$html->success) {
return $html->error;
}
if ($html) {
if ($action) {
if ($action != "this") {
// replace the action attribute with the one provided
$action_val = urldecode($action);
$html = preg_replace("/action=['\"][^'\"]+['\"]/", "action='{$action_val}'", $html);
}
else {
$action_val = "";
}
}
else {
// find the action attribute value (URL)
// should be the proc.php URL (at this point in the script)
$action_val = preg_match("/action=['\"][^'\"]+['\"]/", $html, $m);
$action_val = $m[0];
$action_val = substr($action_val, 8, strlen($action_val) - 9);
}
if (!$css) {
// remove all CSS
$html = preg_replace("/<style[^>]*>(.*)<\/style>/s", "", $html);
}
if (!$ajax) {
// replace the Submit button to be an actual submit type
$html = preg_replace("/input type='button'/", "input type='submit'", $html);
// if action = "this", remove the action attribute completely
if (!$action_val) {
$html = preg_replace("/action=['\"][^'\"]+['\"]/", "", $html);
}
}
else {
// if using Ajax, remove the <form> action attribute completely
$html = preg_replace("/action=['\"][^'\"]+['\"]/", "", $html);
// replace the Submit button to be a button type (for ajax).
// forms come out of AC now with a "submit" button (it used to be "button").
$html = preg_replace("/input type='submit'/", "input type='button'", $html);
$action_val = urldecode($action_val);
// add jQuery stuff
$extra = "<script type='text/javascript'>
var \$j = jQuery.noConflict();
\$j(document).ready(function() {
\$j('#_form_{$id} input[type*=\"button\"]').click(function() {
// rename the radio options for Subscribe/Unsubscribe, since they conflict with the hidden field.
\$j('input[type=radio][name=act]').attr('name','act_radio');
var form_data = {};
\$j('#_form_{$id}').each(function() {
form_data = \$j(this).serialize();
});
var geturl;
geturl = \$j.ajax({
url: '{$action_val}',
type: 'POST',
dataType: 'json',
data: form_data,
error: function(jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
},
success: function(data) {
\$j('#form_result_message').html(data.message);
var result_class = (data.success) ? 'form_result_success' : 'form_result_error';
\$j('#form_result_message').removeClass('form_result_success form_result_error').addClass(result_class);
}
});
});
});
</script>";
$html = $html . $extra;
}
}
return $html;
}
function process($params) {
$r = array();
if ($_SERVER["REQUEST_METHOD"] != "POST") return $r;
$sync = 0;
$captcha_in_form = 0;
if ($params) {
$params_array = explode("&", $params);
$params_ = array();
foreach ($params_array as $expression) {
// IE: css=1
list($var, $val) = explode("=", $expression);
$params_[$var] = $val;
}
$sync = (isset($params_["sync"])) ? (int)$params_["sync"] : 0;
$captcha_in_form = (isset($params_["captcha"])) ? (int)$params_["captcha"] : 0;
}
$formid = $_POST["f"];
// sub or unsub
$act = isset($_POST["act"]) ? $_POST["act"] : "sub";
if (isset($_POST["act_radio"])) {
// the radio options for Subscribe/Unsubscribe.
$act = $_POST["act_radio"];
}
$email = $_POST["email"];
$phone = isset($_POST["phone"]) ? $_POST["phone"] : "";
$lists = (isset($_POST["nlbox"]) && $_POST["nlbox"]) ? $_POST["nlbox"] : array();
if ($captcha_in_form) {
// Captcha is part of the form.
// Get the captcha value the user entered.
$captcha = "";
if (isset($_POST["captcha"])) {
$captcha = md5(strtoupper((string)$_POST["captcha"]));
}
if (!isset($_SESSION["image_random_value"]) || !isset($_SESSION["image_random_value"][$captcha])) {
return json_encode(array("success" => 0, "message" => "Invalid captcha"));
}
}
if (isset($_POST["fullname"])) {
$fullname = explode(" ", $_POST["fullname"]);
$firstname = array_shift($fullname);
$lastname = implode(" ", $fullname);
}
else {
$firstname = trim($_POST["firstname"]);
$lastname = trim($_POST["lastname"]);
if ($firstname == "" && isset($_POST["first_name"])) $firstname = trim($_POST["first_name"]);
if ($lastname == "" && isset($_POST["last_name"])) $lastname = trim($_POST["last_name"]);
}
$fields = (isset($_POST["field"])) ? $_POST["field"] : array();
$contact = array(
"form" => $formid,
"email" => $email,
"first_name" => $firstname,
"last_name" => $lastname,
"phone" => $phone,
);
foreach ($fields as $ac_field_id => $field_value) {
$contact["field"][$ac_field_id . ",0"] = $field_value;
}
// add lists
$status = ($act == "unsub") ? 2 : 1;
foreach ($lists as $listid) {
$contact["p[{$listid}]"] = $listid;
$contact["status[{$listid}]"] = $status;
}
if (!$sync) {
// do add/edit
$contact_exists = $this->api("contact/view?email={$email}", $contact);
if ( !isset($contact_exists->id) ) {
// contact does not exist - add them
$contact_request = $this->api("contact/add", $contact);
if ((int)$contact_request->success) {
// successful request
$contact_id = (int)$contact_request->subscriber_id;
$r = array(
"success" => 1,
"message" => $contact_request->result_message,
"contact_id" => $contact_id,
);
}
else {
// request failed
$r = array(
"success" => 0,
"message" => $contact_request->error,
);
}
}
else {
// contact already exists - edit them
$contact_id = $contact_exists->id;
$contact["id"] = $contact_id;
$contact_request = $this->api("contact/edit?overwrite=0", $contact);
}
}
else {
// perform sync (add or edit)
$contact_request = $this->api("contact/sync", $contact);
}
if ((int)$contact_request->success) {
// successful request
//$contact_id = (int)$contact_request->contact_id;
$r = array(
"success" => 1,
"message" => $contact_request->result_message,
//"contact_id" => $contact_id,
);
}
else {
// request failed
$r = array(
"success" => 0,
"message" => $contact_request->error,
);
}
return json_encode($r);
}
}
?>