forked from websms-com/websmscom-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebSmsCom_Toolkit.inc
926 lines (802 loc) · 39.8 KB
/
WebSmsCom_Toolkit.inc
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
<?php
if (version_compare(PHP_VERSION, '5.2.0') < 0) {
//------------------------------------------------------------
// For PHP < 5.2.0 Services_JSON is used for encoding/decoding
// Get it from PEAR or http://mike.teczno.com/JSON/JSON.phps
// See http://pear.php.net/pepr/pepr-proposal-show.php?id=198
//------------------------------------------------------------
if (!class_exists("Services_JSON")) {
require_once('JSON.phps'); // JSON service for JsonWrapper.
}
}
//-----------------------------------------------------------------
// sms.at Gateway Authentication mode Enum
//-----------------------------------------------------------------
abstract class WebSmsCom_AuthenticationMode
{
const USER_PW = 0;
const ACCESS_TOKEN = 1;
}
class WebSmsCom_JsonWrapper {
//------------------------------------------------------------
// Modify this JSON Wrapper if you want to use a different
// JSON service or influence JSON encoding/decoding.
//------------------------------------------------------------
private $json_service = null;
function __construct() {
if (class_exists("Services_JSON")) {
$this->json_service = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
} else {
if (version_compare(PHP_VERSION, '5.2.0') < 0) {
trigger_error(__CLASS__ . " requires Services_JSON or similar JSON encoder for PHP_VERSION below 5.2.0.");
}
}
}
function checkEncodingError($json) {
if (!is_null($json) && $json != false) {
return $json;
}
if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
// json_last_error supported
$last_error = json_last_error();
switch ($last_error) {
case JSON_ERROR_NONE:
break;
case JSON_ERROR_DEPTH:
throw new WebSmsCom_ParameterValidationException("JSON Error: The maximum stack depth has been exceeded.", $last_error);
case JSON_ERROR_STATE_MISMATCH:
throw new WebSmsCom_ParameterValidationException("JSON Error: Invalid or malformed JSON", $last_error);
case JSON_ERROR_CTRL_CHAR:
throw new WebSmsCom_ParameterValidationException("JSON Error: Control character error, possibly incorrectly encoded", $last_error);
case JSON_ERROR_SYNTAX:
throw new WebSmsCom_ParameterValidationException("JSON Error: Syntax error", $last_error);
// php 5.3.3
case JSON_ERROR_UTF8:
throw new WebSmsCom_ParameterValidationException("JSON Error: Malformed UTF-8 characters, possibly incorrectly encoded", $last_error);
// PHP 5.5.0
case JSON_ERROR_RECURSION:
// One or more recursive references in the value to be encoded
throw new WebSmsCom_ParameterValidationException(" JSON Error: ". json_last_error_msg(), $last_error);
case JSON_ERROR_INF_OR_NAN:
// One or more NAN or INF values in the value to be encoded
throw new WebSmsCom_ParameterValidationException(" JSON Error: ". json_last_error_msg(), $last_error);
case JSON_ERROR_UNSUPPORTED_TYPE:
// A value of a type that cannot be encoded was given
throw new WebSmsCom_ParameterValidationException(" JSON Error: ". json_last_error_msg(), $last_error);
// PHP 7.0.0
case JSON_ERROR_INVALID_PROPERTY_NAME:
// A property name that cannot be encoded was given
throw new WebSmsCom_ParameterValidationException(" JSON Error: ". json_last_error_msg(), $last_error);
case JSON_ERROR_UTF16:
// Malformed UTF-16 characters, possibly incorrectly encoded
throw new WebSmsCom_ParameterValidationException(" JSON Error: ". json_last_error_msg(), $last_error);
default:
throw new WebSmsCom_ParameterValidationException("JSON Error: Unknown JSON encoding/decoding error", $last_error);
}
} else {
throw new WebSmsCom_ParameterValidationException("JSON Error: Unknown JSON encoding/decoding error");
}
}
function encode($php_array) {
return (!is_null($this->json_service))
? $this->json_service->encode($php_array)
: $this->checkEncodingError(json_encode($php_array)); // php >= 5.2.0
}
function decode($json) {
return (!is_null($this->json_service))
? $this->json_service->decode($json)
: (array) json_decode($json); // php >= 5.2.0
}
}
//-----------------------------------------------------------------
// sms.at Gateway XML interface class
//
// Requires PHP >= 5.0.3
// Max tested version: PHP 5.4.7 VC9
//
// @author Gerd Reifenauer
//-----------------------------------------------------------------
class WebSmsCom_Client {
private $VERSION = "1.0.8";
private $REQUIRED_PHP_VERSION = "5.0.3"; # PHP>=5.0.3
private $username;
private $accessToken;
private $mode;
private $pass;
private $url;
private $path;
private $port;
private $scheme;
private $host;
private $verbose;
private $connectionTimeout = 10;
private $lastHttpTransfer = "";
private $endpoint_json = "/json/smsmessaging";
private $endpoint_text = "/text";
private $endpoint_binary = "/binary";
static $available_http_client_types = array('curl', 'fopen');
private $httpClientType = 'curl';
private $contentType = 'application/json;charset=UTF-8';
private $sslVerifyHost = true;
//@throws Exception
function __construct($userOrAccessToken, $pass, $url, $mode = WebSmsCom_AuthenticationMode::USER_PW) {
$this->initUrl($url);
if ((strlen($this->host) < 4)) {
if($mode === WebSmsCom_AuthenticationMode::USER_PW && (!$userOrAccessToken || !$pass) || $mode === WebSmsCom_AuthenticationMode::ACCESS_TOKEN && !$userOrAccessToken)
{
throw new WebSmsCom_ParameterValidationException("Invalid call of sms.at gateway class. Check arguments.");
}
}
if (!$this->port) {
throw new WebSmsCom_ParameterValidationException("Invalid url when calling sms.at gateway class. Missing Port.");
}
$this->json = new WebSmsCom_JsonWrapper();
$this->mode = $mode;
// username & password authentication
if ($this->mode === WebSmsCom_AuthenticationMode::USER_PW) {
$this->username = $userOrAccessToken;
$this->pass = $pass;
} else { // access token authentication
$this->accessToken = $userOrAccessToken;
}
}
private function initUrl($url) {
// remove trailing slashes from url
$url = preg_replace('/\/+$/', '', $url);
$parsed_url = parse_url($url);
$host = (isset($parsed_url['host'])) ? $parsed_url['host'] : '';
$path = (isset($parsed_url['path'])) ? $parsed_url['path'] : '';
$port = (isset($parsed_url['port'])) ? $parsed_url['port'] : '';
$scheme = (isset($parsed_url['scheme'])) ? $parsed_url['scheme'] : '';
if (!$port && (in_array($scheme, array(
'https'
)))) {
$port = 443;
}
if (!$port && (in_array($scheme, array(
'',
'http'
)))) {
$port = 80;
}
$this->url = $url;
$this->host = $host;
$this->path = $path;
$this->port = $port;
$this->scheme = $scheme;
}
//-----------------------------------------------
// send (obj message, int maxSmsPerMessage, bool test)
//
//@param $message - message object of type WebSmsCom_TextMessage or WebSmsCom_BinaryMessage
//@param $test - sms will not be sent when true
//
//@throws WebSmsCom_AuthorizationFailedException
//@throws WebSmsCom_ApiException
//@throws WebSmsCom_HttpConnectionException
//@throws WebSmsCom_UnknownResponseException
//@throws Exception
//-----------------------------------------------
function send($message, $maxSmsPerMessage = null, $test = null) {
if (!is_object($message) || !($message instanceof WebSmsCom_Message)) {
throw new WebSmsCom_ParameterValidationException("Invalid message object given");
}
if (count($message->getRecipientAddressList()) < 1) {
throw new WebSmsCom_ParameterValidationException("Missing recipients in message");
}
if (!is_null($maxSmsPerMessage) && $maxSmsPerMessage == 0) {
throw new WebSmsCom_ParameterValidationException("maxSmsPerMessage cannot be 0, try null.");
}
$request_data = $this->build_request_data($message, $maxSmsPerMessage, $test);
$Response = $this->do_request($request_data);
return $Response;
}
//-------------------------------------------------
// do_request
//-------------------------------------------------
private function do_request($request_data) {
if ($this->httpClientType == 'curl') {
$response_data = $this->do_curl_request($request_data);
} elseif ($this->httpClientType == 'fopen') {
$response_data = $this->do_fopen_request($request_data);
} else {
throw new Exception("Unsupported sending method (httpClientType)");
}
return $this->build_reponse($response_data);
}
//-------------------------------------------------
// build_request_data
//-------------------------------------------------
private function build_request_data($message, $maxSmsPerMessage, $test) {
$request_data = array();
//TODO: decision for endpoint format json/xml/simple.
// Maybe construct this as RequestObject, so no message cloning
// contact json endpoint with json data
$request_data['endpoint_path'] = $this->endpoint_json . (($message instanceof WebSmsCom_BinaryMessage) ? $this->endpoint_binary : $this->endpoint_text);
$request_data['url'] = $this->url . $request_data['endpoint_path']; # for curl
$request_data['endpoint_path'] = $this->path . $request_data['endpoint_path']; # for fopen
$request_data['http_header'] = array(
'Content-Type: ' . $this->contentType,
'Expect:'
);
// add access token in header
if ($this->mode === WebSmsCom_AuthenticationMode::ACCESS_TOKEN) {
$request_data['http_header'][] = 'Authorization: Bearer ' . $this->accessToken;
}
$json_data = $message->getJsonData();
if ($maxSmsPerMessage > 0) {$json_data['maxSmsPerMessage'] = $maxSmsPerMessage;}
if (is_bool($test)) {$json_data['test'] = $test;}
$request_data['content'] = $this->json->encode($json_data);
return $request_data;
}
//-------------------------------------------------
// build_reponse
//-------------------------------------------------
private function build_reponse($response_data) {
if (!isset($response_data['http_status']) || $response_data['http_status'] != 200) {
if (!isset($response_data['http_status']) || $response_data['http_status'] < 1) {
throw new WebSmsCom_HttpConnectionException("Couldn't connect to remote server");
} elseif ($response_data['http_status'] == 401) {
if ($this->mode === WebSmsCom_AuthenticationMode::ACCESS_TOKEN) {
throw new WebSmsCom_AuthorizationFailedException("Authentication failed. Invalid access token.");
} else {
throw new WebSmsCom_AuthorizationFailedException("Basic Authentication failed. Check given username and password. (Account has to be active)");
}
} else {
throw new WebSmsCom_HttpConnectionException("Response HTTP Status: " . $response_data['http_status'] . "\n" . $response_data['content'], $response_data['http_status']);
}
}
if (isset($response_data['content_type']) && preg_match('/json/', $response_data['content_type'])) {
$parsed_content = $this->json->decode($response_data['content']);
} else {
throw new WebSmsCom_UnknownResponseException("Received unknown content type '" . $response_data['content_type'] . "'. Content: " . $response_data['content']);
}
if ($parsed_content['statusCode'] < 2000 || $parsed_content['statusCode'] > 2001) {
throw new WebSmsCom_ApiException($parsed_content['statusMessage'], $parsed_content['statusCode']);
}
$Response = new WebSmsCom_Response($response_data['content'], $parsed_content['statusCode'], $parsed_content['statusMessage'], $parsed_content['transferId'], isset($parsed_content['clientMessageId']) ? $parsed_content['clientMessageId'] : NULL);
return $Response;
}
//-------------------------------------------------
// do request with curl
//-------------------------------------------------
private function do_curl_request($request_data) {
$response_data = array();
if (!extension_loaded('curl')) {
throw new Exception("cURL is not loaded. Try fsockopen method to send request. Use setter WebSmsCom_Client->setHttpClientType('fopen').");
}
$curlinfo_supported = (version_compare(PHP_VERSION, '5.1.3') >= 0) ? true : false;
$response_data['request_header'] = "- cURL Request Header not shown for PHP<5.1.3 -\r\n\r\n";
$curlHandle = curl_init($request_data['url']);
curl_setopt($curlHandle,CURLOPT_HTTPAUTH , CURLAUTH_BASIC);
if ($this->mode === WebSmsCom_AuthenticationMode::USER_PW) {
curl_setopt($curlHandle, CURLOPT_USERPWD, $this->username . ":" . $this->pass);
}
curl_setopt($curlHandle,CURLOPT_HTTPHEADER , $request_data['http_header']);
curl_setopt($curlHandle,CURLOPT_POST , true);
curl_setopt($curlHandle,CURLOPT_POSTFIELDS , $request_data['content']);
curl_setopt($curlHandle,CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlHandle,CURLOPT_HEADER , true);
curl_setopt($curlHandle, CURLOPT_SSLVERSION, CURL_SSLVERSION_MAX_TLSv1_2);
curl_setopt($curlHandle,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curlHandle,CURLOPT_SSL_VERIFYHOST, $this->sslVerifyHost);
//curl_setopt($curlHandle,CURLOPT_FORBID_REUSE , true);
curl_setopt($curlHandle,CURLOPT_FRESH_CONNECT , true);
curl_setopt($curlHandle,CURLOPT_TIMEOUT , $this->connectionTimeout);
curl_setopt($curlHandle,CURLOPT_USERAGENT , "PHP SDK Client (v" . $this->VERSION . ", PHP" . phpversion() . ")");
curl_setopt($curlHandle,CURLOPT_VERBOSE , $this->verbose);
if ($curlinfo_supported) {
curl_setopt($curlHandle, CURLINFO_HEADER_OUT, true);
}
$response = curl_exec($curlHandle);
if ($response == false) {
$response_data['header'] = ''; $response_data['content'] = '';
} else {
list($response_data['header'], $response_data['content']) = preg_split('/\r\n\r\n/', $response);
}
$response_data['http_status'] = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
$response_data['content_type'] = curl_getinfo($curlHandle, CURLINFO_CONTENT_TYPE);
if ($curlinfo_supported) {
$response_data['request_header'] = curl_getinfo($curlHandle, CURLINFO_HEADER_OUT);
}
$this->setLastHttpTransfer($response_data['request_header'] . $request_data['content'], $response);
if ($this->verbose) {
echo $this->getLastHttpTransfer();
}
curl_close($curlHandle);
return $response_data;
}
//-------------------------------------------------
// do request with fopen
//-------------------------------------------------
private function do_fopen_request($request_data) {
$response_data = array();
$fsh = @fsockopen("ssl://" . $this->host, $this->port, $errno, $errstr, $this->connectionTimeout);
if ($errno > 0) {
if ($this->verbose) {
echo "\n--fsockopen to port " . $this->port . "--\nerrno: $errno,\nerrstr: $errstr\n--\n";
}
}
if (is_resource($fsh)) {
$header = "";
$header .= "POST " . $request_data['endpoint_path'] . " HTTP/1.0\r\n";
if ($this->mode === WebSmsCom_AuthenticationMode::USER_PW) {
$header .= "Authorization: Basic " . base64_encode($this->username . ":" . $this->pass) . "\r\n";
}
$header .= "Host: " . $this->host . "\r\n";
$header .= implode("\r\n", $request_data['http_header']) . "\r\n";
$header .= "Content-Length: " . strlen($request_data['content']) . "\r\n";
$header .= "Connection: close\r\n\r\n";
fputs($fsh, $header);
fputs($fsh, $request_data['content']);
$response = "";
while (!feof($fsh)) {
$response .= @fgets($fsh, 2048);
}
fclose($fsh);
$this->setLastHttpTransfer($header . $request_data['content'], $response);
if ($this->verbose) {
echo $this->getLastHttpTransfer();
}
if (strlen($response) > 10) {
list($response_header, $response_data['content']) = preg_split('/\r\n\r\n/', $response);
$response_data['http_status'] = (preg_match('/HTTP\/\d\.\d (\d+)/i', $response_header, $m)) ? intval($m[1]) : 0;
$response_data['content_type'] = (preg_match('/Content-Type: ([^\r\n]+)/i', $response_header, $o)) ? $o[1] : "";
}
} else {
throw new Exception("OpenSSL extension for fsockopen probably not enabled in PHP.ini. Alternatively you could try curl method to send request. Use setter WebSmsCom_Client->setHttpClientType('curl').");
}
return $response_data;
}
//----------------------------------------------------------------
// getVersion()
// - returns version string of this WebSmsCom client
//----------------------------------------------------------------
function getVersion() {
return $this->VERSION;
}
//----------------------------------------------------------------
// getUsername()
// - returns username set at WebSmsCom client ceration
//----------------------------------------------------------------
function getUsername() {
return $this->username;
}
//----------------------------------------------------------------
// getUrl()
// - returns url set at WebSmsCom client ceration
//----------------------------------------------------------------
function getUrl() {
return $this->url;
}
//----------------------------------------------------------------
// getContentType()
// - returns used content-type in HTTP request
// defaults to "application/json;charset=UTF-8"
//----------------------------------------------------------------
function getContentType() {
return $this->content_type;
}
//----------------------------------------------------------------
// setContentType($sting)
// - possibility to set different content type
//----------------------------------------------------------------
function setContentType($value) {
$this->content_type = $value;
}
//----------------------------------------------------------------
// getLastHttpTransfer()
// - returns last HTTP Request and HTTP Response as string.
//----------------------------------------------------------------
public function getLastHttpTransfer() {
return $this->lastHttpTransfer;
}
private function setLastHttpTransfer($request_string, $response_string) {
$this->lastHttpTransfer = "---HTTP-Request---:\n" .
$request_string .
"\n---HTTP-Response---:\n" .
$response_string;
}
//----------------------------------------------------------------
// getHttpClientType()
// - returns used php connection method 'curl' or 'fopen'
//----------------------------------------------------------------
function getHttpClientType() {
return $this->httpClientType;
}
//----------------------------------------------------------------
// setHttpClientType($httpClientType)
// - set http connection method 'curl' or 'fopen'. default is 'curl'
//----------------------------------------------------------------
function setHttpClientType($httpClientType) {
if (in_array($httpClientType, self::$available_http_client_types)) {
$this->httpClientType = $httpClientType;
} else {
throw new WebSmsCom_ParameterValidationException("Unknown HttpClientType.");
}
}
//----------------------------------------------------------------
// getConnectionTimeout()
// - returns timeout in seconds
//--------------------------------------
function getConnectionTimeout() {
return $this->connectionTimeout;
}
//----------------------------------------------------------------
// setConnectionTimeout($seconds)
// - set time in seoncds for curl or fopen connection
//----------------------------------------------------------------
function setConnectionTimeout($connectionTimeout = 10) {
$this->connectionTimeout = $connectionTimeout;
}
//----------------------------------------------------------------
// setVerbose($boolean)
// - set verbose to see more information about request (echoes)
//----------------------------------------------------------------
function setVerbose($value) {
$this->verbose = $value;
}
//----------------------------------------------------------------
// setSslVerifyHost($boolean)
// - ignore ssl host security
//----------------------------------------------------------------
function setSslVerifyHost($value) {
$this->sslVerifyHost = $value;
}
} // end of class WebSmsCom_Client
//-----------------------------------------------------------------
// WebSmsCom_Message
//
// @throws Exception
//-----------------------------------------------------------------
abstract class WebSmsCom_Message {
static $availableSenderAddressType = array('national', 'international', 'alphanumeric', 'shortcode');
protected $recipientAddressList = array();
protected $senderAddress;
protected $senderAddressType;
protected $sendAsFlashSms;
protected $notificationCallbackUrl;
protected $clientMessageId;
protected $priority;
//----------------------------------------------------------------
// getRecipientAddressList()
// - returns array of set recipients
//----------------------------------------------------------------
public function getRecipientAddressList() {
return $this->recipientAddressList;
}
//----------------------------------------------------------------
// setRecipientAddressList($array)
// - set array of recipients
// (array of strings containing full international MSISDNs)
//----------------------------------------------------------------
public function setRecipientAddressList($recipientAddressList) {
if (is_array($recipientAddressList)) {
$this->recipientAddressList = $recipientAddressList;
} else {
throw new WebSmsCom_ParameterValidationException("given recipientAddressList is not an array");
}
}
//----------------------------------------------------------------
// getSenderAddress
// - returns set senderAddress
//----------------------------------------------------------------
public function getSenderAddress() {
return $this->senderAddress;
}
//----------------------------------------------------------------
// setSenderAddress(string senderAddress)
// - set string of sender address msisdn or alphanumeric
// sender address is dependend on user account
//----------------------------------------------------------------
public function setSenderAddress($senderAddress) {
$this->senderAddress = $senderAddress;
}
//----------------------------------------------------------------
// getSenderAddressType
// - returns set sender address type
//----------------------------------------------------------------
public function getSenderAddressType() {
return $this->senderAddressType;
}
//----------------------------------------------------------------
// setSenderAddressType(string senderAddressType)
// - depending on account settings this can be set to
// 'national', 'international', 'alphanumeric' or 'shortcode'
//----------------------------------------------------------------
public function setSenderAddressType($senderAddressType) {
if (!in_array($senderAddressType, self::$availableSenderAddressType)) {
throw new WebSmsCom_ParameterValidationException("given senderAddressType '$senderAddressType' is invalid");
} else {
$this->senderAddressType = $senderAddressType;
}
}
//----------------------------------------------------------------
// getSendAsFlashSms
// - returns set SendAsFlashSms flag
//----------------------------------------------------------------
public function getSendAsFlashSms() {
return $this->sendAsFlashSms;
}
//----------------------------------------------------------------
// setSendAsFlashSms($boolean)
// - set send as flash sms flag true or false
// (SMS is not saved on SIM, but shown directly on screen)
//----------------------------------------------------------------
public function setSendAsFlashSms($sendAsFlashSms) {
$this->sendAsFlashSms = $sendAsFlashSms;
}
//----------------------------------------------------------------
// getNotificationCallbackUrl
// - returns set notification callback url
//----------------------------------------------------------------
public function getNotificationCallbackUrl() {
return $this->notificationCallbackUrl;
}
//----------------------------------------------------------------
// setNotificationCallbackUrl(string $url)
// - set string og notification callback url
// customers url that listens for delivery report notifications
// or replies for this message
//----------------------------------------------------------------
public function setNotificationCallbackUrl($notificationCallbackUrl) {
$this->notificationCallbackUrl = $notificationCallbackUrl;
}
//----------------------------------------------------------------
// getClientMessageId
// - returns set clientMessageId
//----------------------------------------------------------------
public function getClientMessageId() {
return $this->clientMessageId;
}
//----------------------------------------------------------------
// setClientMessageId($string)
// - set message id for this message, returned with response
// and used for notifications
//----------------------------------------------------------------
public function setClientMessageId($clientMessageId) {
$this->clientMessageId = $clientMessageId;
}
//----------------------------------------------------------------
// getPriority
// - returns set message priority
//----------------------------------------------------------------
public function getPriority() {
return $this->priority;
}
//----------------------------------------------------------------
// setPriority(int $priority)
// - sets message priority as integer (1 to 9)
// (if supported by account settings)
//----------------------------------------------------------------
public function setPriority($priority) {
$this->priority = $priority;
}
//----------------------------------------------------------------
// getJsonData
// - used to build json array of message for json encoding
//----------------------------------------------------------------
public function getJsonData() {
$object_vars = get_object_vars($this);
$result = array();
foreach ($object_vars as $key => $value) {
if (is_object($value) && method_exists($value, 'getJsonData')) {
$value = $value->getJsonData();
}
if (!is_null($value)) {
$result[$key] = $value;
}
}
return $result;
}
//----------------------------------------------------------------
// checkRecipientAddressList($recipientAddressList)
// - used to check validity of array
//----------------------------------------------------------------
public function checkRecipientAddressList($recipientAddressList) {
if (!is_array($recipientAddressList)) {
throw new WebSmsCom_ParameterValidationException("Argument 'recipientAddressList' (array) invalid while contructing " . get_class($this));
}
foreach ($recipientAddressList as $value) {
if (!is_numeric($value)) {
throw new WebSmsCom_ParameterValidationException("Recipient '" . $value . "' is invalid. (must be numeric)");
}
if (is_string($value) && (!preg_match('/^\d{1,15}$/', $value) || preg_match('/^0/', $value))) {
throw new WebSmsCom_ParameterValidationException("Recipient '" . $value . "' is invalid. (max. 15 digits full international MSISDN. Example: 4367612345678)");
}
unset($value);
}
}
}
//-----------------------------------------------------------------
// WebSmsCom_TextMessage
//
// @throws Exception
//-----------------------------------------------------------------
class WebSmsCom_TextMessage extends WebSmsCom_Message {
# string
protected $messageContent;
function __construct($recipientAddressList, $messageContent) {
$this->checkRecipientAddressList($recipientAddressList);
if (!is_string($messageContent)) {
throw new WebSmsCom_ParameterValidationException("Argument 'messageContent' (string) invalid while contructing " . __CLASS__);
}
$this->messageContent = $messageContent;
$this->recipientAddressList = $recipientAddressList;
}
//----------------------------------------------------------------
// getMessageContent()
// - returns set messageContent
//----------------------------------------------------------------
public function getMessageContent() {
return $this->messageContent;
}
//----------------------------------------------------------------
// setMessageContent(string $messageContent)
// - set utf8 string message text
//----------------------------------------------------------------
public function setMessageContent($messageContent) {
$this->messageContent = $messageContent;
}
}
//-----------------------------------------------------------------
// WebSmsCom_BinaryMessage
//
// @throws Exception
//-----------------------------------------------------------------
class WebSmsCom_BinaryMessage extends WebSmsCom_Message {
# string
protected $messageContent;
protected $userDataHeaderPresent;
function __construct($recipientAddressList, $messageContent, $userDataHeaderPresent) {
$this->checkRecipientAddressList($recipientAddressList);
if (!is_array($messageContent)) {
throw new WebSmsCom_ParameterValidationException("Argument 'messageContent' (array) invalid while constructing " . __CLASS__ . ". Must be array of strings containing base64 encoded content.");
}
if (!is_bool($userDataHeaderPresent)) {
throw new WebSmsCom_ParameterValidationException("Argument 'userDataHeaderPresent' (boolean) invalid while contructing " . __CLASS__);
}
$this->messageContent = $messageContent;
$this->userDataHeaderPresent = $userDataHeaderPresent;
$this->recipientAddressList = $recipientAddressList;
}
//----------------------------------------------------------------
// getMessageContent()
// - returns set messageContent segments (array)
//----------------------------------------------------------------
public function getMessageContent() {
return $this->messageContent;
}
//----------------------------------------------------------------
// setMessageContent(array $messageContent)
// - set binary message content (array of base64 encoded binary strings)
//----------------------------------------------------------------
public function setMessageContent($messageContent) {
$this->messageContent = $messageContent;
}
//----------------------------------------------------------------
// getUserDataHeaderPresent()
// - returns set UserDataHeaderPresent flag
//----------------------------------------------------------------
public function getUserDataHeaderPresent() {
return $this->userDataHeaderPresent;
}
//----------------------------------------------------------------
// setUserDataHeaderPresent(boolean $userDataHeaderPresent)
// - set boolean userDataHeaderPresent flag
// When set to true, messageContent segments are expected
// to contain a UserDataHeader
//----------------------------------------------------------------
public function setUserDataHeaderPresent($userDataHeaderPresent) {
$this->userDataHeaderPresent = $userDataHeaderPresent;
}
}
//----------------------------------------------
// WebSmsCom send Response Object
//----------------------------------------------
class WebSmsCom_Response {
private $rawContent;
private $statusCode;
private $statusMessage;
private $transferId;
private $clientMessageId;
function __construct($raw_content, $statusCode, $statusMessage, $transferId, $clientMessageId) {
$this->rawContent = $raw_content;
$this->statusCode = $statusCode;
$this->statusMessage = $statusMessage;
$this->transferId = $transferId;
$this->clientMessageId = $clientMessageId;
}
//----------------------------------------------------------------
// getRawContent()
// - returns raw content of response
//----------------------------------------------------------------
function getRawContent() {
return $this->rawContent;
}
//----------------------------------------------------------------
// getStatusCode()
// - returns received StatusCode of API Response
//----------------------------------------------------------------
function getStatusCode() {
return $this->statusCode;
}
//----------------------------------------------------------------
// getStatusMessage()
// - returns received StatusMessage of API Response
//----------------------------------------------------------------
function getStatusMessage() {
return $this->statusMessage;
}
//----------------------------------------------------------------
// getTransferId()
// - returns received TransferId of API Response for successfully sent Message
//----------------------------------------------------------------
function getTransferId() {
return $this->transferId;
}
//----------------------------------------------------------------
// getClientMessageId()
// - returns received TransferId of API Response for successfully sent Message
//----------------------------------------------------------------
function getClientMessageId() {
return $this->clientMessageId;
}
}
//########################## Exceptions #############################
//-------------------------------------------------
// custom ParameterValidationException
//-------------------------------------------------
class WebSmsCom_ParameterValidationException extends Exception {
public function __construct($message, $code = 0) {
parent::__construct($message, $code);
}
public function __toString() {
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}
}
//-------------------------------------------------
// custom ApiException
//-------------------------------------------------
class WebSmsCom_ApiException extends Exception {
public function __construct($message, $code) {
parent::__construct($message, $code);
}
public function __toString() {
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}
}
//-------------------------------------------------
// custom AuthorizationFailedException
//-------------------------------------------------
class WebSmsCom_AuthorizationFailedException extends Exception {
public function __construct($message, $code = 401) {
parent::__construct($message, $code);
}
public function __toString() {
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}
}
//-------------------------------------------------
// custom WebSmsCom_HttpConnectionException
//-------------------------------------------------
class WebSmsCom_HttpConnectionException extends Exception {
public function __construct($message, $code = 0) {
parent::__construct($message, $code);
}
public function __toString() {
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}
}
//-------------------------------------------------
// custom WebSmsCom_UnknownResponseException
//-------------------------------------------------
class WebSmsCom_UnknownResponseException extends Exception {
public function __construct($message, $code = 0) {
parent::__construct($message, $code);
}
public function __toString() {
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}
}
?>