/
Http.php
334 lines (278 loc) · 10.4 KB
/
Http.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
<?php
/**
* User: bruno
* Date: 28/01/16
* Time: 13.12
*/
namespace d4rkstar\arubasms;
use Yii;
use yii\base\Component;
use linslin\yii2\curl\Curl;
class Http extends Component {
const ARUBA_QUALITY_NOTIFY = 'n';
const ARUBA_QUALITY_HIGH = 'h';
const ARUBA_QUALITY_AUTO = 'a';
const ARUBA_QUALITY_MEDIUM = 'l';
const ARUBA_QUALITY_LOW = 'll';
const ARUBA_OPERATION_TEXT = 'TEXT';
const ARUBA_OPERATION_WAPPUSH = 'WAPPUSH';
const ARUBA_OPERATION_UCS2 = 'UCS2';
const ARUBA_OPERATION_MULTITEXT = 'MULTITEXT';
const ARUBA_OPERATION_MULTIUCS2 = 'MULTIUCS2';
const ARUBA_CREDIT_TYPE_CREDIT = 'credit';
const ARUBA_CREDIT_TYPE_NOTIFY = 'n';
const ARUBA_CREDIT_TYPE_HIGH = 'h';
const ARUBA_CREDIT_TYPE_AUTO = 'a';
const ARUBA_CREDIT_TYPE_MEDIUM = 'l';
const ARUBA_CREDIT_TYPE_LOW = 'll';
const ARUBA_REPORT_TYPE_QUEUE = 'queue';
const ARUBA_REPORT_TYPE_NOTIFY = 'notify';
const ARUBA_SMS_LENGTH_SINGLE = 160;
const ARUBA_SMS_LENGTH_MULTITEXT = 918;
/** @var string $url HTTP URL for single send */
public $urlSingle = 'http://admin.sms.aruba.it/sms/send.php';
/** @var string $url HTTP URL for batch send*/
public $urlBatch = 'http://admin.sms.aruba.it/sms/batch.php';
/** @var string $url HTTP URL for send status*/
public $urlBatchStatus = 'http://admin.sms.aruba.it/sms/batch-status.php';
/** @var string $url HTTP URL for batch send*/
public $urlCredit = 'http://admin.sms.aruba.it/sms/credit.php';
/** @var string $user Client Login */
public $user = '';
/** @var string $pass Client password */
public $pass = '';
/** @var string $sender Default sender */
public $sender = '';
/** @var string Default quality for sent SMSs */
public $quality = self::ARUBA_QUALITY_AUTO;
/** @var string creditTypeCheck Default type for credit check */
public $creditTypeCheck = self::ARUBA_CREDIT_TYPE_CREDIT;
/** @var string reportType Default report type */
public $reportType = self::ARUBA_REPORT_TYPE_QUEUE;
/** @var string matchPattern Pattern to match Aruba's sms response */
public $matchPattern = '/(OK|KO) (.*)/';
/**
* @param array $params
* @param $return
* @return array|bool
*/
protected function prepareSendData($params=[], &$return) {
$mandatoryParams = ['user','pass', 'rcpt','data','sender','qty'];
$staticParams = [
'user'=>$this->user,
'pass'=>$this->pass,
'return_id'=>'1'
];
$params = array_merge($params, $staticParams);
if (!array_key_exists('sender', $params) && !empty($this->sender)) {
$params['sender']=$this->sender;
}
if (!array_key_exists('qty', $params) && !empty($this->quality)) {
$params['qty']=$this->quality;
}
$errors = $this->checkMandatoryParams($params, $mandatoryParams);
if ($errors!==false) {
$return = $errors;
return false;
}
if(strlen($params['data']) > self::ARUBA_SMS_LENGTH_SINGLE) {
$params[self::ARUBA_OPERATION_MULTITEXT] = true;
$params['data'] = substr($params['data'], 0, self::ARUBA_SMS_LENGTH_MULTITEXT);
//$params['--' . self::ARUBA_OPERATION_MULTITEXT] = true;
}
return $params;
}
/**
* @param array $params [
* 'rcpt'=>'', // recipient in international format +XXYYYZZZZZZZ
* 'data'=>'', // message body (the lenght depends on operation type)
* 'sender'=>'', // message sender (required if no global sender is set)
* 'qty'=>'', // quality (see Http::ARUBA_QUALITY_* const),
* 'operation' => '' , // type of message to send (see Http::ARUBA_OPERATION_* const)
* 'url' => '', // URL where the phone should connect to in case of operation WAPPUSH
* ]
* @param array $return return data
* @return boolean
*
*/
public function sendSingle($params=[], &$return) {
$params = $this->prepareSendData($params, $return);
if(!is_scalar($params['rcpt'])) {
$errors['rcpt'] = Yii::t('app','Paramter {param} must be a scalar', ['param' => 'rcpt']);
return false;
}
$post = http_build_query($params);
$curl = new Curl();
$result = $curl->setOption(
CURLOPT_POSTFIELDS, $post
)->post($this->urlSingle);
Yii::info($result);
if (preg_match($this->matchPattern, $result, $ret)) {
if (count($ret)==3) {
$return = [$ret[2]];
return ($ret[1]=='OK');
}
}
return false;
}
/**
* @param array $params [
* 'rcpt'=>'', // recipients in international format +XXYYYZZZZZZZ (comma separated string or an array)
* 'data'=>'', // message body (the lenght depends on operation type)
* 'sender'=>'', // message sender (required if no global sender is set)
* 'qty'=>'', // quality (see Http::ARUBA_QUALITY_* const),
* 'operation' => '' , // type of message to send (see Http::ARUBA_OPERATION_* const)
* 'url' => '', // URL where the phone should connect to in case of operation WAPPUSH
* ]
* @param array $return return data
* @return boolean
*
*/
public function sendMultiple($params=[], &$return) {
$params = $this->prepareSendData($params, $return);
if(is_array($params['rcpt']))
$params['rcpt'] = implode(',', $params['rcpt']);
$post = http_build_query($params);
$curl = new Curl();
$result = $curl->setOption(
CURLOPT_POSTFIELDS, $post
)->post($this->urlBatch);
Yii::info($result);
if (preg_match($this->matchPattern, $result, $ret)) {
if (count($ret)==3) {
$return = [$ret[2]];
return ($ret[1]=='OK');
}
}
return false;
}
/**
* @param array $params [
* 'id'=>'' the request_id of the sending
* 'type'=>'' the type of report (see Http::ARUBA_TYPE_REPORT_* const),
* ]
* @param $return array
* @return bool
*/
public function checkStatus($params= [], &$return) {
$mandatoryParams = ['user','pass', 'id','type','schema'];
$staticParams = [
'user'=>$this->user,
'pass'=>$this->pass,
'schema'=>'1'
];
$params = array_merge($params, $staticParams);
if (!array_key_exists('type', $params) && !empty($this->reportType)) {
$params['type']=$this->reportType;
}
$errors = $this->checkMandatoryParams($params, $mandatoryParams);
if ($errors!==false) {
$return = $errors;
return false;
}
$post = http_build_query($params);
$curl = new Curl();
$result = $curl->setOption(
CURLOPT_POSTFIELDS, $post
)->post($this->urlBatchStatus);
Yii::info($result);
if (substr($result,0,2)=='KO') {
if (preg_match($this->matchPattern, $result, $ret)) {
if (count($ret) == 3) {
$return = [$ret[2]];
}
}
return false;
}
$return = [self::parseStatusMessage($result)];
return true;
}
/**
* @param array $params [
* 'type'=>'', // optional parameter (see Http::ARUBA_CREDIT_TYPE_* const)
* ]
* @param array $return return data
* @return bool
*/
public function checkCredit($params = [], &$return) {
$mandatoryParams = ['user','pass'];
$staticParams = [
'user'=>$this->user,
'pass'=>$this->pass,
];
$params = array_merge($params, $staticParams);
if (!array_key_exists('type', $params) && !empty($this->type)) {
$params['type']=$this->type;
}
$errors = $this->checkMandatoryParams($params, $mandatoryParams);
if ($errors!==false) {
$return = $errors;
return false;
}
$post = http_build_query($params);
$curl = new Curl();
$result = $curl->setOption(
CURLOPT_POSTFIELDS, $post
)->post($this->urlCredit);
Yii::info($result);
if (preg_match($this->matchPattern, $result, $ret)) {
if (count($ret)==3) {
$return = [$ret[2]];
return ($ret[1]=='OK');
}
}
return false;
}
/**
* Check if mandatory params are set
* @param array $params key-based array with parameter=>value to be checked
* @param array $mandatoryParams key based array of mandatory parameters
* @param bool $checkEmpty check if all the values are not empty
* @return array|bool array with errors or false
*/
private function checkMandatoryParams($params = [], $mandatoryParams = [], $checkEmpty=true) {
$errors = [];
foreach($mandatoryParams as $mandatory) {
if (!array_key_exists($mandatory, $params)) {
$errors[$mandatory] = Yii::t('app','Paramter {param} is missing',['param'=>$mandatory]);
} else {
if ($checkEmpty) {
$value = isset($params[$mandatory]) ? $params[$mandatory] : '';
if (empty($value) ) {
$errors[$mandatory] = Yii::t('app','Paramter {param} is empty',['param'=>$mandatory]);
}
}
}
}
return (count($errors)>0) ? $errors : false;
}
/**
* @param array $status
* @return SmsStatus[] array of status messages
*/
private static function parseStatusMessage($statusText) {
$statuses = [];
$statusLines = explode("\n", $statusText);
$isFirst = true;
foreach($statusLines as $line) {
if ($isFirst) {
$isFirst = false;
continue;
}
$data = explode(",", $line);
if (count($data)==5) {
$attrs = [
'id'=>$data[0],
'timestamp'=>$data[1],
'dest'=>$data[2],
'status'=>$data[3],
'status_text'=>$data[4],
];
$status = new SmsStatus();
$status->attributes=$attrs;
$statuses[] = $status;
}
}
return $statuses;
}
}