-
Notifications
You must be signed in to change notification settings - Fork 0
/
campaign_commander_notification_messaging.php
executable file
·398 lines (322 loc) · 12 KB
/
campaign_commander_notification_messaging.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
<?php
/**
* Campaign Commander Notification Messaging class
*
* This source file can be used to communicate with Campaign Commander Notification Messaging API (http://campaigncommander.com)
*
* The class is documented in the file itself. If you find any bugs help me out and report them. Reporting can be done by sending an email to php-campaign-commander-notificiation-messaging-bugs[at]verkoyen[dot]eu.
* If you report a bug, make sure you give me enough information (include your code).
*
* Changelog since 1.0.0
* - Bugfix: wrapped dyn fields in CDATA-tags.
*
* License
* Copyright (c), Tijs Verkoyen. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
*
* This software is provided by the author "as is" and any express or implied warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. In no event shall the author be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage.
*
* @author Tijs Verkoyen <[email protected]>
* @version 1.0.1
*
* @copyright Copyright (c), Tijs Verkoyen. All rights reserved.
* @license BSD License
*/
class CampaignCommanderNotificationMessaging
{
// internal constant to enable/disable debugging
const DEBUG = false;
// url for the api
const API_URL = 'http://api.notificationmessaging.com/NMSXML';
// port for the api
const API_PORT = 80;
// current version
const VERSION = '1.0.1';
/**
* A cURL instance
*
* @var resource
*/
private $curl;
/**
* The request queue
*
* @var array
*/
private $queue = array();
/**
* The timeout
*
* @var int
*/
private $timeOut = 60;
/**
* The user agent
*
* @var string
*/
private $userAgent;
// class methods
/**
* Make the call
*
* @return string
* @param string $xmlBody The XML to send.
*/
private function doCall($xmlBody)
{
// no body
if($xmlBody == '') return null;
// build XML
$xml = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
$xml .= '<MultiSendRequest>' . "\n";
// add body
$xml .= (string) $xmlBody;
// end XML
$xml .= '</MultiSendRequest>';
// set options
$options[CURLOPT_URL] = self::API_URL;
$options[CURLOPT_PORT] = self::API_PORT;
$options[CURLOPT_USERAGENT] = $this->getUserAgent();
$options[CURLOPT_FOLLOWLOCATION] = true;
$options[CURLOPT_RETURNTRANSFER] = true;
$options[CURLOPT_TIMEOUT] = (int) $this->getTimeOut();
$options[CURLOPT_POST] = 1;
$options[CURLOPT_POSTFIELDS] = $xml;
// init
if($this->curl == null) $this->curl = curl_init();
// set options
curl_setopt_array($this->curl, $options);
// execute
$response = curl_exec($this->curl);
$headers = curl_getinfo($this->curl);
// fetch errors
$errorNumber = curl_errno($this->curl);
$errorMessage = curl_error($this->curl);
// replace wierd stuff
$response = str_replace('ns1:', 'ns1_', $response);
// we expect XML, so decode it
$responseXML = @simplexml_load_string($response);
// validate
if($responseXML === false) throw new CampaignCommanderNotificationMessagingException('Invalid body.');
// status?
if((string) $responseXML->element['responseStatus'] == 'failed')
{
$message = '';
// add all errors
foreach($responseXML->element as $element) $message .= 'Failed for ' . $element['email'] . ': ' . (string) $element->result . "<br />\n";
// throw exception
throw new CampaignCommanderNotificationMessagingException($message);
}
// other error?
if(isset($responseXML->ns1_faultstring))
{
if(self::DEBUG)
{
echo '<pre>';
var_dump($xml);
echo '--';
var_dump($response);
echo '</pre>';
}
throw new CampaignCommanderNotificationMessagingException((string) $responseXML->ns1_faultstring);
}
// return
return $responseXML;
}
/**
* Get the timeout that will be used
*
* @return int
*/
public function getTimeOut()
{
return (int) $this->timeOut;
}
/**
* Get the useragent that will be used. Our version will be prepended to yours.
* It will look like: "PHP Campaing Commander/<version> <your-user-agent>"
*
* @return string
*/
public function getUserAgent()
{
return (string) 'PHP Campaing Commander Notification Messaging/' . self::VERSION . ' ' . $this->userAgent;
}
/**
* Set the timeout
* After this time the request will stop. You should handle any errors triggered by this.
*
* @return void
* @param int $seconds The timeout in seconds.
*/
public function setTimeOut($seconds)
{
$this->timeOut = (int) $seconds;
}
/**
* Set the user-agent for you application
* It will be appended to ours, the result will look like: "PHP Campaign Commander Notifiction Messaging/<version> <your-user-agent>"
*
* @return void
* @param string $userAgent Your user-agent, it should look like <app-name>/<app-version>.
*/
public function setUserAgent($userAgent)
{
$this->userAgent = (string) $userAgent;
}
/**
* Add a request to the queue
*
* @return int The id in the queue.
* @param string $uniqueIdentifier The unique identifier for the template to use.
* @param string $securityTag The security tag for the template to use.
* @param string $email The recipient's email address to which you want to send the message.
* @param array[optional] $dyn Dynamic fields.
* @param array[optional] $content Portion of preformated HTML or textblocks.
* @param string[optional] $type The action you want to execute on this member to synchronise with your Marketing CCMD Account (“MEMBER” table). Four synchronization options events are available: INSERT, UPDATE, INSERT_UPDATE, NOTHING.
* @param int[optional] $sendDate The date when the message has to be sent.
* @param string[optional] $uidKey The name of the column used to match with the MEMBER table. It will match the DYN parameters that has the same name to retrieve the associated value.
*/
public function addRequestToQueue($uniqueIdentifier, $securityTag, $email, array $dyn = null, array $content = null, $type = 'INSERT_UPDATE', $sendDate = null, $uidKey = 'email')
{
// init var
$allowedTypes = array('INSERT', 'UPDATE', 'INSERT_UPDATE', 'NOTHING');
// redefine
$uniqueIdentifier = utf8_encode((string) $uniqueIdentifier);
$securityTag = utf8_encode((string) $securityTag);
$email = utf8_encode((string) $email);
$type = utf8_encode((string) $type);
$sendDate = ($sendDate !== null) ? (int) $sendDate : (time() - 24 * 60 * 60);
$uidKey = utf8_encode((string) $uidKey);
// validate type
if(!in_array($type, $allowedTypes)) throw new CampaignCommanderNotificationMessagingException('Invalid type(' . $type . '), possible values are: ' . implode($allowedTypes));
// build XML
$xml = '<sendrequest>' . "\n";
// dynamic fields?
if($dyn !== null)
{
// start
$xml .= ' <dyn>' . "\n";
// loop pairs
foreach($dyn as $key => $value) $xml .= ' <entry><key>' . $key . '</key><value>' . utf8_encode($value) . '</value></entry>' . "\n";
// end
$xml .= ' </dyn>' . "\n";
}
// content fields?
if($content !== null)
{
// start
$xml .=' <content>' . "\n";
// loop pairs
foreach($content as $key => $value) $xml .= ' <entry><key>' . $key . '</key><value><![CDATA[' . utf8_encode($value) . ']]></value></entry>' . "\n";
// end
$xml .=' </content>' . "\n";
}
$xml .= ' <email>' . $email . '</email>' . "\n";
$xml .= ' <encrypt>' . $securityTag . '</encrypt>' . "\n";
$xml .= ' <random>' . $uniqueIdentifier . '</random>' . "\n";
$xml .= ' <senddate>' . date('Y-m-d\TH:i:s', $sendDate) . '</senddate>' . "\n";
$xml .= ' <synchrotype>' . $type . '</synchrotype>' . "\n";
$xml .= ' <uidkey>' . $uidKey . '</uidkey>' . "\n";
// end XML
$xml .= '</sendrequest>';
// create new id
$id = count($this->queue);
// add to queue
$this->queue[$id] = $xml;
// return the id
return $id;
}
/**
* Make a request
*
* @return void
* @param string $uniqueIdentifier The unique identifier for the template to use.
* @param string $securityTag The security tag for the template to use.
* @param string $email The recipient's email address to which you want to send the message.
* @param array[optional] $dyn Dynamic fields.
* @param array[optional] $content Portion of preformated HTML or textblocks.
* @param string[optional] $type The action you want to execute on this member to synchronise with your Marketing CCMD Account (“MEMBER” table). Four synchronization options events are available: INSERT, UPDATE, INSERT_UPDATE, NOTHING.
* @param int[optional] $sendDate The date when the message has to be sent.
* @param string[optional] $uidKey The name of the column used to match with the MEMBER table. It will match the DYN parameters that has the same name to retrieve the associated value.
*/
public function makeRequest($uniqueIdentifier, $securityTag, $email, array $dyn = null, array $content = null, $type = 'INSERT_UPDATE', $sendDate = null, $uidKey = 'email')
{
// init var
$allowedTypes = array('INSERT', 'UPDATE', 'INSERT_UPDATE', 'NOTHING');
// redefine
$uniqueIdentifier = (string) $uniqueIdentifier;
$securityTag = (string) $securityTag;
$email = (string) $email;
$type = (string) $type;
$sendDate = ($sendDate !== null) ? (int) $sendDate : time();
$uidKey = (string) $uidKey;
// validate type
if(!in_array($type, $allowedTypes)) throw new CampaignCommanderNotificationMessagingException('Invalid type(' . $type . '), possible values are: ' . implode($allowedTypes));
// build XML
$xml = '<sendrequest>' . "\n";
// dynamic fields?
if($dyn !== null)
{
// start
$xml .= ' <dyn>' . "\n";
// loop pairs
foreach($dyn as $key => $value) $xml .= ' <entry><key>' . $key . '</key><value><![CDATA['. $value .']]></value></entry>' . "\n";
// end
$xml .= ' </dyn>' . "\n";
}
// content fields?
if($content !== null)
{
// start
$xml .=' <content>' . "\n";
// loop pairs
foreach($content as $key => $value) $xml .= ' <entry><key>' . $key . '</key><value><![CDATA[' . $value . ']]></value></entry>' . "\n";
// end
$xml .=' </content>' . "\n";
}
$xml .= ' <email>' . $email . '</email>' . "\n";
$xml .= ' <encrypt>' . $securityTag . '</encrypt>' . "\n";
$xml .= ' <random>' . $uniqueIdentifier . '</random>' . "\n";
$xml .= ' <senddate>' . date('c', $sendDate) . '</senddate>' . "\n";
$xml .= ' <synchrotype>' . $type . '</synchrotype>' . "\n";
$xml .= ' <uidkey>' . $uidKey . '</uidkey>' . "\n";
// end XML
$xml .= '</sendrequest>';
// make the call
$this->doCall($xml);
}
/**
* Process the queue
*
* @return void
*/
public function processQueue()
{
// anything queued?
if(!empty($this->queue))
{
// init var
$xml = implode("\n", $this->queue);
// make the call
$response = $this->doCall($xml);
// clear queue
$this->queue = array();
}
}
}
/**
* Campaign Commander Notification Messaging
*
* @author Tijs Verkoyen <[email protected]>
*/
class CampaignCommanderNotificationMessagingException extends Exception
{
}
?>