-
Notifications
You must be signed in to change notification settings - Fork 6
/
PDU.php
324 lines (276 loc) · 5.18 KB
/
PDU.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
<?php
/*
* Copyright (C) 2014 jackkum
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace jackkum\PHPPDU;
abstract class PDU {
/**
* Service Centre Address
* @var PDU\SCA
*/
protected $_sca;
/**
* Transport Protocol Data Unit
* @var PDU\Type
*/
protected $_type;
/**
* Originator or Destination Address
* @var PDU\SCA
*/
protected $_address;
/**
* Protoсol Identifier
* @var PDU\PID
*/
protected $_pid;
/**
* Data Coding Scheme
* @var PDU\DCS
*/
protected $_dcs;
/**
* User Data Length
* @var integer
*/
protected $_udl;
/**
* User Data
* @var string
*/
protected $_ud;
/**
* parsed string
* @var string
*/
protected static $_pduParse;
/**
* create pdu
*/
public function __construct()
{
$this->setSca();
$this->initType();
$this->setPid();
$this->setDcs();
}
/**
* get a part pdu string and cut them from pdu
* @param integer $length
* @return string|null
*/
public static function getPduSubstr($length)
{
$str = mb_substr(self::$_pduParse, 0, $length);
PDU::debug("PDU::getPduSubstr: " . $str);
self::$_pduParse = mb_substr(self::$_pduParse, $length);
return $str;
}
/**
* parse pdu string
* @param string $PDU
* @return PDU
* @throws Exception
*/
public static function parse($PDU)
{
// current pdu string
self::$_pduParse = $PDU;
// parse service center address
$sca = PDU\SCA::parse(FALSE);
// parse type of sms
$self = PDU\Helper::getPduByType();
// set sca
$self->_sca = $sca;
// parse sms address
$self->_address = PDU\SCA::parse();
return PDU\Helper::initVars($self);
}
/**
* getter for udl
* @return integer
*/
public function getUdl()
{
return $this->_udl;
}
/**
* setter for user data length
* @param type $udl
*/
public function setUdl($udl)
{
$this->_udl = $udl;
}
/**
* set sms center
* @param string|null|PDU\SCA $address
* @return PDU
*/
public function setSca($address = NULL)
{
if($address instanceof PDU\SCA){
$this->_sca = $address;
return;
}
if( ! $this->_sca){
$this->_sca = new PDU\SCA(FALSE);
}
if($address){
$this->_sca->setPhone($address, TRUE);
}
return $this;
}
/**
* getter for SCA
* @return PDU\SCA
*/
public function getSca()
{
return $this->_sca;
}
/**
* set pdu type
* @param array $params
* @return PDU
*/
abstract public function initType(array $params = array());
/**
* abstract method for override on the child classes
* return start pdu string for split message on parts
* @return string
*/
abstract public function getStart();
/**
* get pdu type
* @return PDU\Type
*/
public function getType()
{
return $this->_type;
}
/**
* setter for the type of pdu
* @param PDU\Type $type
*/
public function setType($type)
{
$this->_type = $type;
}
/**
* set address
* @param string|PDU\SCA $address
* @return PDU
*/
public function setAddress($address)
{
if($address instanceof PDU\SCA){
$this->_address = $address;
return $this;
}
$this->_address = new PDU\SCA();
$this->_address->setPhone($address);
return $this;
}
/**
* getter address
* @return PDU\SCA
*/
public function getAddress()
{
return $this->_address;
}
/**
* set Data Coding Scheme
* @param PDU\DCS $dcs
* @return PDU
*/
public function setDcs(PDU\DCS $dcs = NULL)
{
$this->_dcs = $dcs ? $dcs : new PDU\DCS();
return $this;
}
/**
* getter for dcs
* @return PDU\DCS
*/
public function getDcs()
{
return $this->_dcs;
}
/**
* set data
* @param string|PDU\Data $data
* @return PDU
*/
public function setData($data)
{
if($data instanceof PDU\Data){
$this->_ud = $data;
} else {
$this->_ud = new PDU\Data($this);
$this->_ud->setData($data);
}
return $this;
}
/**
* getter user data
* @return PDU\Data
*/
public function getData()
{
return $this->_ud;
}
/**
* set pid
* @param integer $pid
* @return PDU
*/
public function setPid(PDU\PID $pid = NULL)
{
$this->_pid = $pid ? $pid : new PDU\PID();
return $this;
}
/**
* get pid
* @return PDU/PID
*/
public function getPid()
{
return $this->_pid;
}
/**
* get parts sms
* @return array
*/
public function getParts()
{
if( ! $this->getAddress()){
throw new Exception("Address not set");
}
if( ! $this->getData()){
throw new Exception("Data not set");
}
return $this->getData()->getParts();
}
public static function debug($message)
{
if( ! defined('PDU_DEBUG')){
return;
}
echo "# " .date('H:i:s') . " - " . $message . "\n";
}
}