-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPbsolSmsSender.php
346 lines (310 loc) · 10.8 KB
/
PbsolSmsSender.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
<?php
/**
* Class PbsolSmsSender
*
* Wrapper for pbsol.ru sms-server
*/
class PbsolSmsSender extends CApplicationComponent
{
//Config params
public $alphaNumber;
public $apiUrl = 'https://sms.pbsol.ru:1543/api/';
public $certPath;
public $delayMethod = 'cron';
public $delayCallback;
public $normalNumberCallback;
//Possible errors
const PBSOL_ERROR_NO_API_URL = 1;
const PBSOL_ERROR_NO_CERTIFICATE = 2;
const PBSOL_ERROR_CERTIFICATE_NOT_FOUND = 3;
const PBSOL_ERROR_LOG_RECORN_NOT_FOUND = 4;
const PBSOL_ERROR_GET_RESULT = 5;
const PBSOL_ERROR_EMPTY_PHONE = 6;
const PBSOL_ERROR_EMPTY_MESSAGE = 7;
const PBSOL_ERROR_EMPTY_ALPHA_NUMBER = 8;
const PBSOL_ERROR_DELAY_CALLBACK_NOT_FOUND = 9;
const PBSOL_ERROR_NORMAL_NUMBER_CALLBACK_NOT_FOUND = 10;
/**
* Initializing component
*
* @throws PbsolSmsSenderException
*/
public function init()
{
parent::init();
//import Pbsol Exception class
$dir = dirname(__FILE__);
$alias = md5($dir);
Yii::setPathOfAlias($alias, $dir);
Yii::import($alias . '.PbsolSmsSenderException');
if (!$this->apiUrl) {
throw new PbsolSmsSenderException(Yii::t('PbsolSmsSender', 'Api url needed'), self::PBSOL_ERROR_NO_API_URL);
}
// Check certificate
if (!$this->certPath) {
throw new PbsolSmsSenderException(Yii::t(
'PbsolSmsSender', 'Certificate needed'
), self::PBSOL_ERROR_NO_CERTIFICATE);
}
if (!file_exists($this->certPath)) {
$this->certPath = Yii::getPathOfAlias('application') . '/' . trim($this->certPath, '/');
if (!file_exists($this->certPath)) {
throw new PbsolSmsSenderException(Yii::t(
'PbsolSmsSender', 'Certificate file "{file}" not found', array('{file}' => $this->certPath)
), self::PBSOL_ERROR_CERTIFICATE_NOT_FOUND);
}
}
// Check delay methods
if ($this->delayMethod == 'callback') {
switch (gettype($this->delayCallback)) {
case 'array':
if (
isset($this->delayCallback[0])
&& isset($this->delayCallback[1])
&& method_exists($this->delayCallback[0], $this->delayCallback[1])
) {
$this->delayMethod = 'callbackArray';
} else {
throw new PbsolSmsSenderException(Yii::t(
'PbsolSmsSender', 'Delay callback not found'
), self::PBSOL_ERROR_DELAY_CALLBACK_NOT_FOUND);
}
break;
case 'object':
$this->delayMethod = 'callbackObject';
break;
default:
throw new PbsolSmsSenderException(Yii::t(
'PbsolSmsSender', 'Delay callback not found'
), self::PBSOL_ERROR_DELAY_CALLBACK_NOT_FOUND);
break;
}
}
}
/**
* Send sms message with pbsol.ru API
*
* @param string $phone Phone number
* @param string $message Text of message
* @param bool $now Send now (without Cron or Callback). Default - false
* @param bool $tryNormalNumber Try normalize phone number. Default - true
* @param string $alphaNumber Alpha number. If null - uses alphaNumber from config
*
* @return bool
* @throws PbsolSmsSenderException
*/
public function send($phone, $message, $now = false, $tryNormalNumber = true, $alphaNumber = null)
{
$phoneNumber = $phone;
if ($tryNormalNumber) {
$phoneNumber = $this->numberNormal($phone);
}
$alphaNumber = $alphaNumber ? : $this->alphaNumber;
$error = 0;
// Create PbsolSmsLog-record, check input data and save current status
if (empty($phoneNumber)) {
$error = self::PBSOL_ERROR_EMPTY_PHONE;
}
if (empty($message)) {
$error = self::PBSOL_ERROR_EMPTY_MESSAGE;
}
if (empty($alphaNumber)) {
$error = self::PBSOL_ERROR_EMPTY_ALPHA_NUMBER;
}
$smsGwLog = new PbsolSmsLog();
$smsGwLog->from = $alphaNumber;
$smsGwLog->to = $phoneNumber;
$smsGwLog->text = $message;
$smsGwLog->created = date('Y-m-d H:i:s');
switch ($error) {
case self::PBSOL_ERROR_EMPTY_PHONE:
$smsGwLog->status = Yii::t(
'PbsolSmsSender', 'Phone number is incorrect. Input number: {number}', array('{number}' => $phone)
);
break;
case self::PBSOL_ERROR_EMPTY_MESSAGE:
$smsGwLog->status = Yii::t('PbsolSmsSender', 'Message is empty');
break;
case self::PBSOL_ERROR_EMPTY_ALPHA_NUMBER:
$smsGwLog->status = Yii::t('PbsolSmsSender', 'Alpha number is empty');
break;
}
$smsGwLog->save();
if ($error != 0) {
throw new PbsolSmsSenderException($smsGwLog->status, $error);
}
// Sending message
if ($now) {
return $this->pushByModel($smsGwLog);
}
// Put message in the queue
switch ($this->delayMethod) {
case 'cron':
return $this->toCron($smsGwLog);
break;
case 'callbackArray':
return call_user_func($this->delayCallback, $smsGwLog);
break;
case 'callbackObject':
$method = $this->delayCallback;
return $method($smsGwLog);
break;
default:
return false;
break;
}
}
/**
* Normalizing phone number
*
* @param $number
*
* @return mixed
* @throws PbsolSmsSenderException
*/
public function numberNormal($number)
{
$number = preg_replace('/[^0-9]/x', '', trim($number));
switch (gettype($this->normalNumberCallback)) {
case 'array':
if (
isset($this->normalNumberCallback[0])
&& isset($this->normalNumberCallback[1])
&& method_exists($this->normalNumberCallback[0], $this->normalNumberCallback[1])
) {
$number = call_user_func($this->normalNumberCallback, $number);
} else {
throw new PbsolSmsSenderException(Yii::t(
'PbsolSmsSender', 'Normalize number callback not found'
), self::PBSOL_ERROR_NORMAL_NUMBER_CALLBACK_NOT_FOUND);
}
break;
case 'object':
$method = $this->normalNumberCallback;
$number = $method($number);
break;
}
return $number;
}
/**
* Send sms by ID of PbsolSmsLog-record
*
* @param $id integer ID of PbsolSmsLog-record
*
* @return bool
* @throws PbsolSmsSenderException
*/
public function pushById($id)
{
$model = PbsolSmsLog::model()->findByPk($id);
if (!$model) {
throw new PbsolSmsSenderException(Yii::t(
'PbsolSmsSender', 'PbsolSmsLog with id={id} not found', array('{id}' => $id)
), self::PBSOL_ERROR_LOG_RECORN_NOT_FOUND);
}
return $this->pushByModel($model);
}
/**
* Return text description of sms state
*
* @param $guid string Message ID on Pbsol server
* @return null|string
*/
public function getState($guid)
{
$result = $this->sendRequest('GetMessageState', array('MsgID' => $guid));
if (is_array($result) && isset($result['ResultDescription'])) {
return $result['ResultDescription'];
}
return null;
}
/**
* Send sms by PbsolSmsLog-record
*
* @param PbsolSmsLog $smsGwLog
*
* @return bool
*/
private function pushByModel(PbsolSmsLog $smsGwLog)
{
$smsGwLog->status = Yii::t(
'PbsolSmsSender', '{date} - Preparing send message', array('{date}' => date('Y-m-d H:i:s'))
);
$smsGwLog->save();
// It will be send to pbsol.ru API
$data = array(
'AlphaNumber' => $smsGwLog->from,
'Msisdn' => $smsGwLog->to,
'Text' => $smsGwLog->text
);
// Send request and read response
$result = $this->sendRequest('SendSms', $data);
if (is_array($result) && isset($result['ResultCode']) && $result['ResultCode'] == 0) {
$smsGwLog->guid = $result['Result'];
$smsGwLog->status = Yii::t('PbsolSmsSender', '{date} - OK send', array('{date}' => date('Y-m-d H:i:s')));
$smsGwLog->save();
return true;
}
$errorMsg = '';
if (!is_array($result)) {
$errorMsg = $result;
} elseif (isset($result['ResultDescription'])) {
$errorMsg = $result['ResultDescription'];
}
// False response
$smsGwLog->status = Yii::t(
'PbsolSmsSender', '{date} - Error send. Code: {code}. {description}', array(
'{date}' => date('Y-m-d H:i:s'),
'{code}' => isset($result['ResultCode']) ? $result['ResultCode'] : '',
'{description}' => $errorMsg
)
);
$smsGwLog->save();
return false;
}
/**
* Send request to Pbsol server. Try read response and decode to array.
*
* @param string $method
* @param array $params
*
* @return array|null
* @throws PbsolSmsSenderException
*/
private function sendRequest($method, $params = array())
{
$request = json_encode($params);
$opts = array(
'http' => array(
'method' => "POST",
'content' => $request,
'header' => 'Content-type: application/json',
)
);
$context = stream_context_create($opts);
stream_context_set_option($context, 'ssl', 'local_cert', $this->certPath);
$result = @file_get_contents(trim($this->apiUrl, '/') . '/' . $method, 0, $context);
if (!$result) {
throw new PbsolSmsSenderException(Yii::t(
'PbsolSmsSender', 'Error with get result with method {method}', array('{method}' => $method)
), self::PBSOL_ERROR_GET_RESULT);
}
return json_decode($result, true);
}
/**
* Mark sms as needed processing by Cron-Command
*
* @param PbsolSmsLog $smsGwLog
*
* @return bool
*/
private function toCron(PbsolSmsLog $smsGwLog)
{
$smsGwLog->is_wait = true;
$smsGwLog->status = Yii::t(
'PbsolSmsSender', '{date} - Waiting Cron job', array('{date}' => date('Y-m-d H:i:s'))
);
$smsGwLog->save();
return true;
}
}