forked from elidickinson/EmailLabsAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmailLabsAPI.php
More file actions
380 lines (329 loc) · 11.1 KB
/
EmailLabsAPI.php
File metadata and controls
380 lines (329 loc) · 11.1 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
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
<?php
/**
* EmailLabsAPI - Wrapper for some common EmailLabs API functions. Also POSTs form
* data to several forms that are not officially part of the API.
*
* Originally intended for internal use only. Sorry for the poor state of documentation.
* See README for more.
*
* Author: Eli Dickinson, eli (at) fiercemarkets.com
*
* http://github.com/elidickinson/EmailLabsAPI
*
* File Revision: r4520 | 2009-07-17 15:55:38 -0400 (Fri, 17 Jul 2009)
*
*/
/**
* CLASS: EmailLabsAPIResponse
*
* Parses the XML response from EmailLabs API into easy-to-use fields
* Notes that only API functions (i.e. callApi() below) return XML. POSTs to
* EmailLabs /functions/*.html URLs return HTML pages or redirect on success.
*/
class EmailLabsAPIResponse {
var $xml, $string, $firstRecord;
protected $type;
function EmailLabsAPIResponse($response_string) {
$this->xml = new DOMDocument();
$this->xml->loadXML($response_string);
$this->string = $response_string;
$types = $this->xml->getElementsByTagName('TYPE');
if($types->length >= 1) {
$this->type = $types->item(0)->textContent;
}
$this->firstRecord = $this->record();
}
function record($num = 0) {
$recs = $this->xml->getElementsByTagName('RECORD');
if($num >= $recs->length) {
return false;
}
// OK, the logic here gets a little ridiculous.
// Case 1 (apiRecordQueryData) has records with "rows" that look like this:
// <DATA type="demographic" id="1">Mary</DATA>
// Case 2 (apiDemographicQueryEnabled) has records with data "rows" that work together like:
// <DATA type="name">Where do you live?</DATA>
// <DATA type="id">1425</DATA>
// <DATA type="type">select list</DATA>
$result = array();
foreach ($recs->item($num)->childNodes as $tag) {
if ($tag->nodeType != XML_TEXT_NODE && $tag->attributes) {
$type = $tag->getAttribute('type');
$id = $tag->getAttribute('id');
if($type && $id) { // ------ Case 1
if(!isset($result[$type]) || !is_array($result[$type])) {
$result[$type] = array();
}
$result[$type][$id] = $tag->textContent;
}
else if($type) { // ------ Case 2
$result[$type] = $tag->textContent;
}
}
}
return $result;
}
function allRecords() {
$recs = array();
$num_recs = $this->xml->getElementsByTagName('RECORD')->length;
for($i=0;$i < $num_recs;$i++) {
$recs[] = $this->record($i);
}
return $recs;
}
function success() {
return ($this->type == 'success');
}
}
/**
* CLASS: EmailLabsAPI
*
*/
class EmailLabsAPI {
var $config = array('siteid'=>'8121');
/**
* Subscribes a user to the specified list(s). If the person is already
* subscribed, this function can be used to update demographics.
*
* @param $email
* @param $mlids
* Either an array of Mailing List IDs or a single MLID.
* Note: time increases linearly with number of MLIDs specified
* @param $demographics
* Hash in form of array('1'=>'Joe', '2'=>'Smith','47'=>'Engineer')
* @return void
* @author Eli Dickinson
**/
function subscribe($email, $mlids, $demographics = array(),$send_welcome=true) {
// sanity check
if(!is_array($mlids)) {
if(is_numeric($mlids))
$mlids = array($mlids);
else
return FALSE;
}
if(!$this->is_valid_email($email)) {
return FALSE;
}
$params = array();
$params['submitaction'] = '3';
$params['siteid'] = $this->config['siteid'];
$params['email'] = $email;
$params['tagtype'] = 'q2';
$params['welcome'] = $send_welcome ? "on" : "off";
$params['activity'] = 'submit';
$params['redirection'] = 'http://example.com/success';
// $params['val_95785'] = $_SERVER['REMOTE_ADDR']; // Default Source IP Address to REMOTE_ADDR. Can be overwritten by $demographics
foreach($demographics as $dem_id=> $dem_value) {
// IDs like 1234 or val_1234 are added to params. Anything else in demographics array is ignored
if(stripos($dem_id,"val_") === 0) {
$params[$dem_id] = $dem_value;
}
else if(is_numeric($dem_id)) {
$params['val_'.$dem_id] = $dem_value;
}
}
$result = true;
foreach($mlids as $mlid) {
$params['mlid'] = $mlid;
$resp = $this->http_post_form("http://www.uptilt.com/functions/mailing_list.html", $params);
// Check if EmailLabs returned the 'success' redirect
$result = ($result && (strpos($resp,'example.com/success') !== FALSE));
}
// Returns true if all attempts to subscribe to all MLIDs went through. False otherwise.
return $result;
}
static function is_valid_email($email, $check_bogosity = FALSE) {
$regex = '/^[a-zA-Z0-9\._\-\+]+\@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/';
if (preg_match($regex, $email) == 0) {
return FALSE;
}
if($check_bogosity && (preg_match('/^(asdf|jkl|1234?5?|x+|junk|spam)\@(asdf|jkl|1234?5?|456|x+|spam)\./i', $email))) {
return FALSE;
}
return TRUE;
}
/*
//
//This function works and is more efficient than above, but does NOT update demographics for existing subscribers AT ALL
//
function subscribe($email, $mlids, $demographics = array(),$send_welcome=true) {
if(!is_array($mlids)) {
$mlids = array($mlids);
}
$params = array();
$params['submitaction'] = '3';
$params['siteid'] = $this->config['siteid'];
$params['email'] = $email;
$params['tagtype'] = 'q2';
$params['welcome'] = $send_welcome ? "on" : "off";
$params['update_demographics'] = "true";
$params['activity'] = 'submit';
foreach($mlids as $mlid) {
$params['mlid'.$mlid] = "sub";
}
$params = array_merge($demographics, $params);
//print_r($params);
$result = $this->http_post_form("http://www.uptilt.com/functions/multiple_mailing_list.html", $params);
}
*/
/**
* Unsubscribes a user from specified list(s).
*
* @param $email
* @param $mlids
* either a single MLID or an array of MLIDs
* @return void
*
*/
function unsubscribe($email, $mlids) {
if(!is_array($mlids)) {
$mlids = array($mlids);
}
$params = array();
$params['submitaction'] = '2';
$params['siteid'] = $this->config['siteid'];
$params['email'] = $email;
$params['tagtype'] = 'q2';
$params['activity'] = 'submit';
$params['redirection'] = 'http://example.com/success';
foreach($mlids as $mlid) {
$params['mlid'.$mlid] = "unsub";
}
$result = $this->http_post_form("http://www.uptilt.com/functions/multiple_mailing_list.html", $params);
}
/**
* Get user demographics for the specified email address in the specified list
*/
function get_demographics($email,$mlid) {
$response = $this->apiRecordQueryData($email,$mlid);
if (is_array($response->firstRecord['demographic'])) {
return $response->firstRecord['demographic'];
}
else {
return false;
}
}
function list_enabled_demographics($mlid) {
$response = $this->apiDemographicQueryEnabled($mlid);
if ($response->success()) {
return $response;
}
else {
return false;
}
}
function is_subscribed($email, $mlid) {
$response = $this->apiRecordQueryData($email,$mlid);
//var_dump($response);
if(!$response->success() || !is_array($response->firstRecord)) {
// no record or error searching
return false;
}
else {
//has a record indicating subscriber is trashed
return ($response->firstRecord['extra']['trashed'] == 'n');
}
}
/**
* Forwards a message to a non-subscriber (fwd to a friend feature). Requires that an
* enhancer be created in each mailing list in which you want to use it.
* Oddly, enhancer ID (aka "p") is required, but MLID is not.
*
* @return true on success, false on fail
*/
function forward_message($enhancer_id,$from_email,$to_email,$message) {
if (!is_numeric($enhancer_id) || $enhancer_id <= 0) {
trigger_error("Invalid enhancer ID", E_USER_ERROR);
}
if (!preg_match('/.+@.+\..+/',$from_email)) {
trigger_error("Invalid From Email Address", E_USER_ERROR);
}
if (!preg_match('/.+@.+\..+/',$to_email)) {
trigger_error("Invalid To Email Address", E_USER_ERROR);
}
// note: this is not actually an API call
$url = 'http://www.uptilt.com/functions/email_referral_newsletter.html';
$data = array();
$data['mid'] = '';
$data['mlid'] = '';
$data['siteid'] = '';
$data['activity'] = 'submit';
$data['data'] = '';
$data['sender'] = $from_email;
$data['uid'] = '';
$data['pers_mess'] = $message;
$data['p'] = $enhancer_id;
$data['recipients'] = $to_email;
$resp = $this->http_post_form($url,$data);
// Check if EmailLabs returned the 'success' redirect
$result = strpos($resp,'has been sent') !== FALSE;
return $result;
}
// ******************** Wrappers for EmailLabs API functions ************************
/**
* Query data about a particular record in a particular list
*/
protected function apiRecordQueryData($email,$mlid) {
if (!is_numeric($mlid)) {
trigger_error("Invalid MLID", E_USER_ERROR);
}
if (!preg_match('/.+@.+\..+/',$email)) {
trigger_error("Invalid Email Address", E_USER_ERROR);
}
$data = "<DATASET><SITE_ID>{$this->config['siteid']}</SITE_ID><MLID>$mlid</MLID><DATA type=\"email\">$email</DATA></DATASET>";
$apiresult = $this->callApi('record','query-data',$data);
$resp = new EmailLabsAPIResponse($apiresult);
return $resp;
}
/**
* Returns a list of active demographics for the given MLID
*/
protected function apiDemographicQueryEnabled($mlid) {
if (!is_numeric($mlid)) {
trigger_error("Invalid MLID", E_USER_ERROR);
}
$data = "<DATASET><SITE_ID>{$this->config['siteid']}</SITE_ID><MLID>$mlid</MLID></DATASET>";
$apiresult = $this->callApi('demographic','query-enabled',$data);
$resp = new EmailLabsAPIResponse($apiresult);
return $resp;
}
// ******************** EmailLabs Low-Level functions **************************
protected function callApi($api_type, $api_activity, $api_input=''){
$data = array();
$data['type'] = $api_type;
$data['activity'] = $api_activity;
$data['input'] = $api_input;
$result = $this->http_post_form("http://www.uptilt.com/API/mailing_list.html", $data);
return $result;
}
/**
* Posts form data to specified URL using PHP cURL extensions. Data is a hash of key=>value
*
* @return Returns resulting body of the POST. Fails on error or timeout.
* @author Eli Dickinson
**/
protected function http_post_form($url,$data,$timeout=20) {
if(!function_exists("curl_exec")) {
trigger_error("PHP/cURL is not installed on this server");
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); // times out after $timeout secs
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_POST, 1); // set POST method
// Translate $data hash into form encoded string (e.g. "param1=val¶m2=val¶m3=val")
$values = array();
foreach($data as $key=>$value)
$values[]="$key=".urlencode($value);
$data_string=implode("&",$values);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); // add POST fields
$result = curl_exec($ch); // run the whole process
curl_close($ch);
return $result;
}
}
?>