Skip to content

Commit af86c09

Browse files
committed
Update phpMQTT.php
1 parent 22156ae commit af86c09

File tree

1 file changed

+52
-61
lines changed

1 file changed

+52
-61
lines changed

3rdparty/phpmqtt/phpMQTT.php

Lines changed: 52 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,35 @@
33
namespace Bluerhinos;
44

55
/*
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
88
99
*/
1010

1111
/*
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+
3535
*/
3636

3737
/* phpMQTT */
@@ -90,24 +90,14 @@ public function __construct($address, $port, $clientid, $cafile = null)
9090
* @param $clientid
9191
* @param null $cafile
9292
*/
93-
public function broker($address, $port, $clientid, $cafile = null)
93+
public function broker($address, $port, $clientid, $cafile = null): void
9494
{
9595
$this->address = $address;
9696
$this->port = $port;
9797
$this->clientid = $clientid;
9898
$this->cafile = $cafile;
9999
}
100100

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-
111101
/**
112102
* Will try and connect, if fails it will sleep 10s and try again, this will enable the script to recover from a network outage
113103
*
@@ -118,7 +108,7 @@ private function setSubChar(&$str, $idx, $char)
118108
*
119109
* @return bool
120110
*/
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
122112
{
123113
while ($this->connect($clean, $will, $username, $password) === false) {
124114
sleep(10);
@@ -134,7 +124,7 @@ public function connect_auto($clean = true, $will = null, $username = null, $pas
134124
*
135125
* @return bool
136126
*/
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
138128
{
139129
if ($will) {
140130
$this->will = $will;
@@ -150,9 +140,8 @@ public function connect($clean = true, $will = null, $username = null, $password
150140
$socketContext = stream_context_create(
151141
[
152142
'ssl' => [
153-
'verify_peer' => false,
154-
"verify_peer_name" => false,
155-
"cafile" => $this->cafile
143+
'verify_peer_name' => true,
144+
'cafile' => $this->cafile
156145
]
157146
]
158147
);
@@ -250,14 +239,14 @@ public function connect($clean = true, $will = null, $username = null, $password
250239

251240
$string = $this->read(4);
252241

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)) {
254243
$this->_debugMessage('Connected to Broker');
255244
} else {
256245
$this->_errorMessage(
257246
sprintf(
258247
"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])
261250
)
262251
);
263252
return false;
@@ -302,7 +291,7 @@ public function read($int = 8192, $nb = false)
302291
*
303292
* @return string
304293
*/
305-
public function subscribeAndWaitForMessage($topic, $qos)
294+
public function subscribeAndWaitForMessage($topic, $qos): string
306295
{
307296
$this->subscribe(
308297
[
@@ -326,7 +315,7 @@ public function subscribeAndWaitForMessage($topic, $qos)
326315
* @param $topics
327316
* @param int $qos
328317
*/
329-
public function subscribe($topics, $qos = 0)
318+
public function subscribe($topics, $qos = 0): void
330319
{
331320
$i = 0;
332321
$buffer = '';
@@ -361,7 +350,7 @@ public function subscribe($topics, $qos = 0)
361350
/**
362351
* Sends a keep alive ping
363352
*/
364-
public function ping()
353+
public function ping(): void
365354
{
366355
$head = chr(0xc0);
367356
$head .= chr(0x00);
@@ -373,18 +362,18 @@ public function ping()
373362
/**
374363
* sends a proper disconnect cmd
375364
*/
376-
public function disconnect()
365+
public function disconnect(): void
377366
{
378367
$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);
381370
fwrite($this->socket, $head, 2);
382371
}
383372

384373
/**
385374
* Sends a proper disconnect, then closes the socket
386375
*/
387-
public function close()
376+
public function close(): void
388377
{
389378
$this->disconnect();
390379
stream_socket_shutdown($this->socket, STREAM_SHUT_WR);
@@ -398,7 +387,7 @@ public function close()
398387
* @param int $qos
399388
* @param bool $retain
400389
*/
401-
public function publish($topic, $content, $qos = 0, $retain = false)
390+
public function publish($topic, $content, $qos = 0, $retain = false): void
402391
{
403392
$i = 0;
404393
$buffer = '';
@@ -425,7 +414,7 @@ public function publish($topic, $content, $qos = 0, $retain = false)
425414
++$cmd;
426415
}
427416

428-
$this->setSubChar($head,0,chr($cmd));
417+
$head[0] = chr($cmd);
429418
$head .= $this->setmsglength($i);
430419

431420
fwrite($this->socket, $head, strlen($head));
@@ -460,7 +449,7 @@ protected function _fwrite($buffer)
460449
*/
461450
public function message($msg)
462451
{
463-
$tlen = (ord($this->getSubChar($msg, 0)) << 8) + ord($this->getSubChar($msg, 1));
452+
$tlen = (ord($msg[0]) << 8) + ord($msg[1]);
464453
$topic = substr($msg, 2, $tlen);
465454
$msg = substr($msg, ($tlen + 2));
466455
$found = false;
@@ -485,6 +474,8 @@ public function message($msg)
485474
) . '$/',
486475
$topic
487476
)) {
477+
$found = true;
478+
488479
if ($top['function'] === '__direct_return_message__') {
489480
return $msg;
490481
}
@@ -594,7 +585,7 @@ protected function getmsglength(&$msg, &$i)
594585
$multiplier = 1;
595586
$value = 0;
596587
do {
597-
$digit = ord($this->getSubChar($msg, $i));
588+
$digit = ord($msg[$i]);
598589
$value += ($digit & 127) * $multiplier;
599590
$multiplier *= 128;
600591
$i++;
@@ -608,7 +599,7 @@ protected function getmsglength(&$msg, &$i)
608599
*
609600
* @return string
610601
*/
611-
protected function setmsglength($len)
602+
protected function setmsglength($len): string
612603
{
613604
$string = '';
614605
do {
@@ -629,7 +620,7 @@ protected function setmsglength($len)
629620
*
630621
* @return string
631622
*/
632-
protected function strwritestring($str, &$i)
623+
protected function strwritestring($str, &$i): string
633624
{
634625
$len = strlen($str);
635626
$msb = $len >> 8;
@@ -650,9 +641,9 @@ public function printstr($string): void
650641
{
651642
$strlen = strlen($string);
652643
for ($j = 0; $j < $strlen; $j++) {
653-
$num = ord($this->getSubChar($string, $j));
644+
$num = ord($string[$j]);
654645
if ($num > 31) {
655-
$chr = $this->getSubChar($string, $j);
646+
$chr = $string[$j];
656647
} else {
657648
$chr = ' ';
658649
}
@@ -663,7 +654,7 @@ public function printstr($string): void
663654
/**
664655
* @param string $message
665656
*/
666-
protected function _debugMessage(string $message)
657+
protected function _debugMessage(string $message): void
667658
{
668659
if ($this->debug === true) {
669660
echo date('r: ') . $message . PHP_EOL;
@@ -673,7 +664,7 @@ protected function _debugMessage(string $message)
673664
/**
674665
* @param string $message
675666
*/
676-
protected function _errorMessage(string $message)
667+
protected function _errorMessage(string $message): void
677668
{
678669
error_log('Error:' . $message);
679670
}

0 commit comments

Comments
 (0)