forked from domingopa/WhatsAPI-Official
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.class.php
More file actions
executable file
·302 lines (272 loc) · 6.99 KB
/
protocol.class.php
File metadata and controls
executable file
·302 lines (272 loc) · 6.99 KB
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
<?php
require 'exception.php';
class IncompleteMessageException extends CustomException
{
private $input;
public function __construct($message = null, $code = 0)
{
parent::__construct($message, $code);
}
public function setInput($input)
{
$this->input = $input;
}
public function getInput()
{
return $this->input;
}
}
class ProtocolNode
{
private $tag;
private $attributeHash;
private $children;
private $data;
private static $cli = null;
/**
* check if call is from command line.
*
* @return bool
*/
private static function isCli()
{
if (self::$cli === null) {
//initial setter
if (php_sapi_name() == 'cli') {
self::$cli = true;
} else {
self::$cli = false;
}
}
return self::$cli;
}
/**
* @return string
*/
public function getData()
{
return $this->data;
}
/**
* @return string
*/
public function getTag()
{
return $this->tag;
}
/**
* @return string[]
*/
public function getAttributes()
{
return $this->attributeHash;
}
/**
* @return ProtocolNode[]
*/
public function getChildren()
{
return $this->children;
}
/**
* @param ProtocolNode $node
*/
public function addChild(ProtocolNode $node)
{
$this->children[] = $node;
}
/**
* @param ProtocolNode $node
*/
public function removeChild($tag, $attrs = [])
{
if ($this->children) {
if (is_int($tag)) {
if (isset($this->children[$tag])) {
array_slice($this->children, $tag, 1);
}
} else {
foreach ($this->children as $i => $child) {
$index = -1;
if (strcmp($child->tag, $tag) == 0) {
$index = $i;
foreach ($attrs as $key => $val) {
if (strcmp($child->getAttribute($key), $val) != 0) {
$index = -1; // attrs not equal
break;
}
}
}
if ($index != -1) {
array_slice($this->children, $index, 1);
return;
}
}
}
}
}
public function __construct($tag, $attributeHash, $children, $data)
{
$this->tag = $tag;
$this->attributeHash = $attributeHash;
$this->children = $children;
$this->data = $data;
}
/**
* @param string $indent
* @param bool $isChild
*
* @return string
*/
public function nodeString($indent = '', $isChild = false)
{
// non printable characters regex
$nonPrintable = '#[^\x20-\x7E]#';
//formatters
$lt = '<';
$gt = '>';
$nl = "\n";
if (!self::isCli()) {
$lt = '<';
$gt = '>';
$nl = '<br />';
$indent = str_replace(' ', ' ', $indent);
}
$ret = $indent.$lt.$this->tag;
if ($this->attributeHash != null) {
foreach ($this->attributeHash as $key => $value) {
$ret .= ' '.$key.'="'.$value.'"';
}
}
$ret .= $gt;
if (strlen($this->data) > 0) {
if (strlen($this->data) <= 1024) {
//message
if (preg_match($nonPrintable, $this->data)) {
$ret .= bin2hex($this->data);
} else {
$ret .= $this->data;
}
} else {
//raw data
$ret .= ' '.strlen($this->data).' byte data';
}
}
if ($this->children) {
$ret .= $nl;
$foo = [];
foreach ($this->children as $child) {
$foo[] = $child->nodeString($indent.' ', true);
}
$ret .= implode($nl, $foo);
$ret .= $nl.$indent;
}
$ret .= $lt.'/'.$this->tag.$gt;
if (!$isChild) {
$ret .= $nl;
if (!self::isCli()) {
$ret .= $nl;
}
}
return $ret;
}
/**
* @param $attribute
*
* @return string
*/
public function getAttribute($attribute)
{
$ret = '';
if (isset($this->attributeHash[$attribute])) {
$ret = $this->attributeHash[$attribute];
}
return $ret;
}
/**
* @param string $needle
*
* @return bool
*/
public function nodeIdContains($needle)
{
return strpos($this->getAttribute('id'), $needle) !== false;
}
//get children supports string tag or int index
/**
* @param $tag
* @param array $attrs
*
* @return ProtocolNode
*/
public function getChild($tag, $attrs = [])
{
$ret = null;
if ($this->children) {
if (is_int($tag)) {
if (isset($this->children[$tag])) {
return $this->children[$tag];
} else {
return;
}
}
foreach ($this->children as $child) {
if (strcmp($child->tag, $tag) == 0) {
$found = true;
foreach ($attrs as $key => $value) {
if (strcmp($child->getAttribute($key), $value) != 0) {
$found = false;
break;
}
}
if ($found) {
return $child;
}
}
$ret = $child->getChild($tag, $attrs);
if ($ret) {
return $ret;
}
}
}
}
/**
* @param $tag
*
* @return bool
*/
public function hasChild($tag)
{
return $this->getChild($tag) == null ? false : true;
}
/**
* @param int $offset
*/
public function refreshTimes($offset = 0)
{
if (isset($this->attributeHash['id'])) {
$id = $this->attributeHash['id'];
$parts = explode('-', $id);
$parts[0] = time() + $offset;
$this->attributeHash['id'] = implode('-', $parts);
}
if (isset($this->attributeHash['t'])) {
$this->attributeHash['t'] = time();
}
}
/**
* Print human readable ProtocolNode object.
*
* @return string
*/
public function __toString()
{
$readableNode = [
'tag' => $this->tag,
'attributeHash' => $this->attributeHash,
'children' => $this->children,
'data' => $this->data,
];
return print_r($readableNode, true);
}
}