3
3
namespace Bluerhinos ;
4
4
5
5
/*
6
- phpMQTT
7
- A simple php class to connect/publish/subscribe to an MQTT broker
6
+ phpMQTT
7
+ A simple php class to connect/publish/subscribe to an MQTT broker
8
8
9
9
*/
10
10
11
11
/*
12
- Licence
13
-
14
- Copyright (c) 2010 Blue Rhinos Consulting | Andrew Milsted
15
- andrew@bluerhinos.co.uk | http://www.bluerhinos.co.uk
16
-
17
- Permission is hereby granted, free of charge, to any person obtaining a copy
18
- of this software and associated documentation files (the "Software"), to deal
19
- in the Software without restriction, including without limitation the rights
20
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21
- copies of the Software, and to permit persons to whom the Software is
22
- furnished to do so, subject to the following conditions:
23
-
24
- The above copyright notice and this permission notice shall be included in
25
- all copies or substantial portions of the Software.
26
-
27
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33
- THE SOFTWARE.
34
-
12
+ Licence
13
+
14
+ Copyright (c) 2010 Blue Rhinos Consulting | Andrew Milsted
15
+ andrew@bluerhinos.co.uk | http://www.bluerhinos.co.uk
16
+
17
+ Permission is hereby granted, free of charge, to any person obtaining a copy
18
+ of this software and associated documentation files (the "Software"), to deal
19
+ in the Software without restriction, including without limitation the rights
20
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21
+ copies of the Software, and to permit persons to whom the Software is
22
+ furnished to do so, subject to the following conditions:
23
+
24
+ The above copyright notice and this permission notice shall be included in
25
+ all copies or substantial portions of the Software.
26
+
27
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33
+ THE SOFTWARE.
34
+
35
35
*/
36
36
37
37
/* phpMQTT */
@@ -90,24 +90,14 @@ public function __construct($address, $port, $clientid, $cafile = null)
90
90
* @param $clientid
91
91
* @param null $cafile
92
92
*/
93
- public function broker ($ address , $ port , $ clientid , $ cafile = null )
93
+ public function broker ($ address , $ port , $ clientid , $ cafile = null ): void
94
94
{
95
95
$ this ->address = $ address ;
96
96
$ this ->port = $ port ;
97
97
$ this ->clientid = $ clientid ;
98
98
$ this ->cafile = $ cafile ;
99
99
}
100
100
101
- private function getSubChar ($ str , $ idx )
102
- {
103
- return substr ($ str , $ idx , 1 );
104
- }
105
-
106
- private function setSubChar (&$ str , $ idx , $ char )
107
- {
108
- $ str = substr_replace ($ str , $ char , $ idx , 1 );
109
- }
110
-
111
101
/**
112
102
* Will try and connect, if fails it will sleep 10s and try again, this will enable the script to recover from a network outage
113
103
*
@@ -118,7 +108,7 @@ private function setSubChar(&$str, $idx, $char)
118
108
*
119
109
* @return bool
120
110
*/
121
- public function connect_auto ($ clean = true , $ will = null , $ username = null , $ password = null )
111
+ public function connect_auto ($ clean = true , $ will = null , $ username = null , $ password = null ): bool
122
112
{
123
113
while ($ this ->connect ($ clean , $ will , $ username , $ password ) === false ) {
124
114
sleep (10 );
@@ -134,7 +124,7 @@ public function connect_auto($clean = true, $will = null, $username = null, $pas
134
124
*
135
125
* @return bool
136
126
*/
137
- public function connect ($ clean = true , $ will = null , $ username = null , $ password = null )
127
+ public function connect ($ clean = true , $ will = null , $ username = null , $ password = null ): bool
138
128
{
139
129
if ($ will ) {
140
130
$ this ->will = $ will ;
@@ -150,9 +140,8 @@ public function connect($clean = true, $will = null, $username = null, $password
150
140
$ socketContext = stream_context_create (
151
141
[
152
142
'ssl ' => [
153
- 'verify_peer ' => false ,
154
- "verify_peer_name " => false ,
155
- "cafile " => $ this ->cafile
143
+ 'verify_peer_name ' => true ,
144
+ 'cafile ' => $ this ->cafile
156
145
]
157
146
]
158
147
);
@@ -250,14 +239,14 @@ public function connect($clean = true, $will = null, $username = null, $password
250
239
251
240
$ string = $ this ->read (4 );
252
241
253
- if (ord ($ this -> getSubChar ( $ string, 0 )) >> 4 === 2 && $ this -> getSubChar ( $ string, 3 ) === chr (0 )) {
242
+ if (ord ($ string[ 0 ]) >> 4 === 2 && $ string[ 3 ] === chr (0 )) {
254
243
$ this ->_debugMessage ('Connected to Broker ' );
255
244
} else {
256
245
$ this ->_errorMessage (
257
246
sprintf (
258
247
"Connection failed! (Error: 0x%02x 0x%02x) \n" ,
259
- ord ($ this -> getSubChar ( $ string, 0 ) ),
260
- ord ($ this -> getSubChar ( $ string, 3 ) )
248
+ ord ($ string[ 0 ] ),
249
+ ord ($ string[ 3 ] )
261
250
)
262
251
);
263
252
return false ;
@@ -302,7 +291,7 @@ public function read($int = 8192, $nb = false)
302
291
*
303
292
* @return string
304
293
*/
305
- public function subscribeAndWaitForMessage ($ topic , $ qos )
294
+ public function subscribeAndWaitForMessage ($ topic , $ qos ): string
306
295
{
307
296
$ this ->subscribe (
308
297
[
@@ -326,7 +315,7 @@ public function subscribeAndWaitForMessage($topic, $qos)
326
315
* @param $topics
327
316
* @param int $qos
328
317
*/
329
- public function subscribe ($ topics , $ qos = 0 )
318
+ public function subscribe ($ topics , $ qos = 0 ): void
330
319
{
331
320
$ i = 0 ;
332
321
$ buffer = '' ;
@@ -361,7 +350,7 @@ public function subscribe($topics, $qos = 0)
361
350
/**
362
351
* Sends a keep alive ping
363
352
*/
364
- public function ping ()
353
+ public function ping (): void
365
354
{
366
355
$ head = chr (0xc0 );
367
356
$ head .= chr (0x00 );
@@ -373,18 +362,18 @@ public function ping()
373
362
/**
374
363
* sends a proper disconnect cmd
375
364
*/
376
- public function disconnect ()
365
+ public function disconnect (): void
377
366
{
378
367
$ head = ' ' ;
379
- $ this -> setSubChar ( $ head, 0 , chr (0xe0 ) );
380
- $ this -> setSubChar ( $ head, 1 , chr (0x00 ) );
368
+ $ head[ 0 ] = chr (0xe0 );
369
+ $ head[ 1 ] = chr (0x00 );
381
370
fwrite ($ this ->socket , $ head , 2 );
382
371
}
383
372
384
373
/**
385
374
* Sends a proper disconnect, then closes the socket
386
375
*/
387
- public function close ()
376
+ public function close (): void
388
377
{
389
378
$ this ->disconnect ();
390
379
stream_socket_shutdown ($ this ->socket , STREAM_SHUT_WR );
@@ -398,7 +387,7 @@ public function close()
398
387
* @param int $qos
399
388
* @param bool $retain
400
389
*/
401
- public function publish ($ topic , $ content , $ qos = 0 , $ retain = false )
390
+ public function publish ($ topic , $ content , $ qos = 0 , $ retain = false ): void
402
391
{
403
392
$ i = 0 ;
404
393
$ buffer = '' ;
@@ -425,7 +414,7 @@ public function publish($topic, $content, $qos = 0, $retain = false)
425
414
++$ cmd ;
426
415
}
427
416
428
- $ this -> setSubChar ( $ head, 0 , chr ($ cmd) );
417
+ $ head[ 0 ] = chr ($ cmd );
429
418
$ head .= $ this ->setmsglength ($ i );
430
419
431
420
fwrite ($ this ->socket , $ head , strlen ($ head ));
@@ -460,7 +449,7 @@ protected function _fwrite($buffer)
460
449
*/
461
450
public function message ($ msg )
462
451
{
463
- $ tlen = (ord ($ this -> getSubChar ( $ msg, 0 )) << 8 ) + ord ($ this -> getSubChar ( $ msg, 1 ) );
452
+ $ tlen = (ord ($ msg[ 0 ]) << 8 ) + ord ($ msg[ 1 ] );
464
453
$ topic = substr ($ msg , 2 , $ tlen );
465
454
$ msg = substr ($ msg , ($ tlen + 2 ));
466
455
$ found = false ;
@@ -485,6 +474,8 @@ public function message($msg)
485
474
) . '$/ ' ,
486
475
$ topic
487
476
)) {
477
+ $ found = true ;
478
+
488
479
if ($ top ['function ' ] === '__direct_return_message__ ' ) {
489
480
return $ msg ;
490
481
}
@@ -594,7 +585,7 @@ protected function getmsglength(&$msg, &$i)
594
585
$ multiplier = 1 ;
595
586
$ value = 0 ;
596
587
do {
597
- $ digit = ord ($ this -> getSubChar ( $ msg, $ i ) );
588
+ $ digit = ord ($ msg[ $ i ] );
598
589
$ value += ($ digit & 127 ) * $ multiplier ;
599
590
$ multiplier *= 128 ;
600
591
$ i ++;
@@ -608,7 +599,7 @@ protected function getmsglength(&$msg, &$i)
608
599
*
609
600
* @return string
610
601
*/
611
- protected function setmsglength ($ len )
602
+ protected function setmsglength ($ len ): string
612
603
{
613
604
$ string = '' ;
614
605
do {
@@ -629,7 +620,7 @@ protected function setmsglength($len)
629
620
*
630
621
* @return string
631
622
*/
632
- protected function strwritestring ($ str , &$ i )
623
+ protected function strwritestring ($ str , &$ i ): string
633
624
{
634
625
$ len = strlen ($ str );
635
626
$ msb = $ len >> 8 ;
@@ -650,9 +641,9 @@ public function printstr($string): void
650
641
{
651
642
$ strlen = strlen ($ string );
652
643
for ($ j = 0 ; $ j < $ strlen ; $ j ++) {
653
- $ num = ord ($ this -> getSubChar ( $ string, $ j ) );
644
+ $ num = ord ($ string[ $ j ] );
654
645
if ($ num > 31 ) {
655
- $ chr = $ this -> getSubChar ( $ string, $ j ) ;
646
+ $ chr = $ string[ $ j ] ;
656
647
} else {
657
648
$ chr = ' ' ;
658
649
}
@@ -663,7 +654,7 @@ public function printstr($string): void
663
654
/**
664
655
* @param string $message
665
656
*/
666
- protected function _debugMessage (string $ message )
657
+ protected function _debugMessage (string $ message ): void
667
658
{
668
659
if ($ this ->debug === true ) {
669
660
echo date ('r: ' ) . $ message . PHP_EOL ;
@@ -673,7 +664,7 @@ protected function _debugMessage(string $message)
673
664
/**
674
665
* @param string $message
675
666
*/
676
- protected function _errorMessage (string $ message )
667
+ protected function _errorMessage (string $ message ): void
677
668
{
678
669
error_log ('Error: ' . $ message );
679
670
}
0 commit comments