-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSMSMobiltekClass.php
417 lines (400 loc) · 12.8 KB
/
SMSMobiltekClass.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<?php
require_once 'SMSClass.php';
/**
* Send SMS by Mobiltek gate
*
* @package php-sms-classes
* @author Rafal Zielonka
* @copyright Copyright (C) 2012 Rafał Zielonka
* @license This program is free software:
* you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
* @filesource
*/
class SMSMobiltek extends SMSClasses\SMS implements SMSClasses\SMSable
{
/**
* @desc path to send action
*/
const SEND_URL_PATH = 'send.php';
/**
* @desc server response separator
*/
const RESPONSE_SEPARATOR = PHP_EOL;
/**
* @desc server response status
*/
const RESPONSE_OK = 'OK';
/**
* @desc maximum integer allowd to be set by client
*/
const MAX_SMS_ID = 2147483647;
/**
* @desc url param user name
*
* required: yes <br />
* type: string
*/
const PARAM_LOGIN = 'login';
/**
* @desc url param password
*
* required: yes <br />
* type: string
*/
const PARAM_PASS = 'passwd';
/**
* @desc url param service id
*
* required: yes <br />
* type: string
*/
const PARAM_SERVICE_ID = 'service';
/**
* @desc url param SMS message
*
* required: yes <br />
* type: string
*/
const PARAM_TEXT = 'text';
/**
* @desc url param SMS message encoded in base64
*
* required: yes <br />
* type: string
*/
const PARAM_B64_TEXT = 'b64text';
/**
* @desc url param telephone number with prefix
*
* required: no <br />
* type: string
*/
const PARAM_DEST = 'dest';
/**
* @desc url param valid to date
*
* required: yes <br />
* type: date
* value: date string formatted ddMMyyHHmmss
*/
const PARAM_VALID_TO = 'validTo';
/**
* @desc param url for deliver notify request
*
* required: no <br />
* default: false <br />
* type: boolean
*/
const PARAM_DELIV_NOTIF_REQUEST = 'delrq';
/**
* @desc url param for zeroclass message
*
* required: no <br />
* default: false <br />
* type: boolean
*/
const PARAM_ZEROCLASS = 'zeroclass';
/**
* @desc param url for advanced encoding
*
* required: no <br />
* default: false <br />
* type: boolean
*/
const PARAM_ADVANCED_ENCODING = 'encoding';
/**
* @desc param url for delete content
*
* required: no <br />
* default: false <br />
* type: boolean
*/
const PARAM_DELETE_CONTENT = 'deleteContent';
/**
* @desc param url for resposne SMS numeric identifier
*
* required: no <br />
* default: null<br />
* type: integer
*/
const PARAM_SMS_IN_ID = 'id';
/**
* @desc param url for alphanumeric originator address (up to 11 characters)
*
* required: no <br />
* type: string
*/
const PARAM_ORIG = 'orig';
/**
* @desc param url for SMS numeric identifier
*
* required: yes <br />
* type: integer
*/
const PARAM_SMS_ID = 'id';
/**
* @desc param url for timeout
*
* required: no <br />
* type: integer
*/
const PARAM_TIMEOUT = 'timeout';
/**
* @desc param url for manual confrim while getting sms
*
* required: no <br />
* default: false <br />
* type: boolean
*/
const PARAM_MANUAL_CONFIRM = 'manualconfirm';
/**
* @desc param url for manual confrim while getting sms
*
* required: no <br />
* type: string
*/
const PARAM_SCHEDULE = 'schedule';
/**
* @desc url param unix timestamp date when sms was recived by operator
*
* required: no <br />
* type: string
*/
const PARAM_RECIVED_TIME = 'tmux';
/**
* @desc digital sign generated from md5 hash of configuration paramters
* and password, known only for client and gate server
*
* required: no <br />
* type: string
*/
const PARAM_SIGN = 'sign';
/**
* @desc operator numeric idenfier i.e.:
* - 501 PTK
* - 601 Polkomtel
* - 602 PTC
* - 701 P4
* - 26012 Polsat Cyfrowy
*
* required: no <br />
* type: string
*/
const PARAM_OPERATOR_ID = 'op';
/**
* @desc information about type of method of recivng multipart messages:
* - splitted
* - joined
*
* required: no <br />
* type: striog
*/
const PARAM_MPART_TYPE = 'mpart_type';
/**
* @desc information about number of parts of (joined) message
*
* required: no <br />
* type: integer
*/
const PARAM_MPART_PARTS = 'mpart_parts';
/**
* @desc numeric identifier of (splitted) message the same for all splitted
*
* required: no <br />
* type: integer
*/
const PARAM_MPART_ID = 'mpart_id';
/**
* @desc define number of part of (splitted) message
*
* required: no <br />
* type: integer
*/
const PARAM_MPART_NO = 'mpart_no';
/**
* @desc define max number of parts of (splitted) message
*
* required: no <br />
* type: integer
*/
const PARAM_MPART_MAX = 'mpart_max';
/**
* Returns send URL
*
* @return string
* @throws UnexpectedValueException
*/
protected function _get_send_url()
{
$data = array(
self::PARAM_LOGIN => $this->getGateUser(),
self::PARAM_PASS => $this->getGatePassword(),
self::PARAM_SERVICE_ID => $this->getGateServiceId(),
self::PARAM_TEXT => $this->getText(),
self::PARAM_SMS_ID => $this->getSMSId(),
self::PARAM_DEST => $this->getNumberPrefix() . $this->getTelephoneNumber(),
);
(!is_null($this->getOriginator())) AND $data[self::PARAM_ORIG] = $this->getOriginator();
(!is_null($this->getValidTo())) AND $data[self::PARAM_VALID_TO] = $this->_format_date($this->getValidTo());
($this->isDelivNotifRequest()) AND $data[self::PARAM_DELIV_NOTIF_REQUEST] = "true";
($this->isZeroClass()) AND $data[self::PARAM_ZEROCLASS] = "true";
($this->isAdvancedEncoding()) AND $data[self::PARAM_ADVANCED_ENCODING] = "UTF-8";
($this->isDeleteContent()) AND $data[self::PARAM_DELETE_CONTENT] = "true";
(!is_null($this->getSchedule())) AND $data[self::PARAM_SCHEDULE] = $this->_format_date($this->getSchedule());
if ( in_array(null, $data) ) {
throw new UnexpectedValueException("Not enough data to send sms.", 1);
}
return $this->getGateURL() . self::SEND_URL_PATH . "?" . http_build_query($data);
}
/**
* Send request to SMS gate server
*
* @param string $uri
* @return boolean
*/
protected function _request($uri)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $uri);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$this->_parse_respone(curl_exec($curl));
if ( curl_error($curl) ) {
$this->response['status'] = -1;
$this->response['error'] = curl_error($curl);
return false;
}
if ( $this->response['error'] ) {
return false;
}
curl_close($curl);
return true;
}
/**
* Parse SMS gate server response
*
* @param string $response
*/
protected function _parse_respone($response)
{
$response = explode(self::RESPONSE_SEPARATOR, rtrim($response, self::RESPONSE_SEPARATOR));
if ( is_array($response) ) {
$this->response['status'] = $response[0];
if ( $this->response['status'] == self::RESPONSE_OK ) {
unset($response[0]);
$this->response['data'] = $response;
} else {
$this->response['error'] = $response[1];
}
}
}
/**
* Returns date formatted for API
*
* @param string $date DateTime object string i.e. "+1 day" max "+72 hours"
* @return string
*/
protected function _format_date($date)
{
$dateTime = new DateTime($date);
return $dateTime->format("Y-m-d H:i:s");
}
/**
* Return date TIMESTAMP
*
* @param string $date date formatted by API
* @return string
* @throws UnexpectedValueException
*/
protected function _unformat_date($date)
{
try {
$dateTime = new DateTime($date);
return $dateTime->format("U");
} catch ( Exception $e ) {
throw new UnexpectedValueException($e->getMessage(), 5);
}
}
/**
* Send SMS by gate
*
* @return boolean
*/
public function send()
{
$this->_request($this->_get_send_url());
if ( $this->response['error'] ) {
$this->actionStatus = parent::ACTION_STATUS_FAIL;
$this->dataExplained['' . parent::KEY_RESPONSE_STATUS . ''] = $this->response['status'];
$this->dataExplained['' . parent::KEY_ERROR_CODE] = $this->response['error'];
return false;
} else {
$this->actionStatus = ($this->response['status'] == self::RESPONSE_OK) ? parent::ACTION_STATUS_SUCCESS : parent::ACTION_STATUS_FAIL;
$this->dataExplained['' . parent::KEY_RESPONSE_STATUS . ''] = $this->response['status'];
$this->dataExplained['' . parent::KEY_SMS_ID . ''] = $this->getSMSId();
return true;
}
}
/**
* Obtain information about sended SMS
*
* @return boolean
*/
public function info()
{
$this->actionStatus = parent::ACTION_STATUS_FAIL;
$this->dataExplained['' . parent::KEY_RESPONSE_STATUS . ''] = null;
return false;
}
/**
* Confirm SMS delivery
*
* @return boolean
*/
public function confirm()
{
$this->actionStatus = parent::ACTION_STATUS_FAIL;
$this->dataExplained['' . parent::KEY_RESPONSE_STATUS . ''] = null;
return false;
}
/**
* Listen for request from gate
*
* @return string
*/
public function get()
{
if ( isset($_REQUEST[self::PARAM_SMS_ID]) ) {
$this->actionStatus = parent::ACTION_STATUS_SUCCESS;
$this->dataExplained['' . parent::KEY_RESPONSE_STATUS . ''] = null;
$this->dataExplained['' . parent::KEY_SMS_ID . ''] = isset($_REQUEST[self::PARAM_SMS_ID]) ? $_REQUEST[self::PARAM_SMS_ID] : null;
$this->dataExplained['' . parent::KEY_TEXT . ''] = isset($_REQUEST[self::PARAM_TEXT]) ? $_REQUEST[self::PARAM_TEXT] : null;
$this->dataExplained['' . parent::KEY_SERVICE_ID . ''] = isset($_REQUEST[self::PARAM_SERVICE_ID]) ? $_REQUEST[self::PARAM_SERVICE_ID] : null;
$this->dataExplained['' . parent::KEY_SMSC_RECIVED_DATE . ''] = isset($_REQUEST[self::PARAM_RECIVED_TIME]) ? $_REQUEST[self::PARAM_RECIVED_TIME] : null;
$this->dataExplained['' . parent::KEY_ORIG . ''] = isset($_REQUEST[self::PARAM_ORIG]) ? $_REQUEST[self::PARAM_ORIG] : null;
$this->dataExplained['' . parent::KEY_DEST . ''] = isset($_REQUEST[self::PARAM_DEST]) ? $_REQUEST[self::PARAM_DEST] : null;
$this->dataExplained['' . parent::KEY_OPERATOR_ID . ''] = isset($_REQUEST[self::PARAM_OPERATOR_ID]) ? $_REQUEST[self::PARAM_OPERATOR_ID] : null;
$this->dataExplained['' . parent::KEY_MPART_TYPE . ''] = isset($_REQUEST[self::PARAM_MPART_TYPE]) ? $_REQUEST[self::PARAM_MPART_TYPE] : null;
$this->dataExplained['' . parent::KEY_MPART_PARTS . ''] = isset($_REQUEST[self::PARAM_MPART_PARTS]) ? $_REQUEST[self::PARAM_MPART_PARTS] : null;
$this->dataExplained['' . parent::KEY_MPART_ID . ''] = isset($_REQUEST[self::PARAM_MPART_ID]) ? $_REQUEST[self::PARAM_MPART_ID] : null;
$this->dataExplained['' . parent::KEY_MPART_NO . ''] = isset($_REQUEST[self::PARAM_MPART_NO]) ? $_REQUEST[self::PARAM_MPART_NO] : null;
$this->dataExplained['' . parent::KEY_MPART_MAX . ''] = isset($_REQUEST[self::PARAM_MPART_MAX]) ? $_REQUEST[self::PARAM_MPART_MAX] : null;
} else {
throw new RuntimeException('The variables in $_REQUEST are NOT provided to the script via the GET or POST method.', 1);
}
print self::RESPONSE_OK;
}
}