Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for custom SSL context options #137

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions examples/sslContextOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

require('../phpMQTT.php');

$server = 'localhost'; // change if necessary
$port = 8883; // change if necessary
$username = ''; // set your username
$password = ''; // set your password
$client_id = 'phpMQTT-publisher'; // make sure this is unique for connecting to sever - you could use uniqid()

$mqtt = new Bluerhinos\phpMQTT($server, $port, $client_id);

// This can be used if the server needs an encrypted connection, but you can not verify the
// server (because you don't have the certificate) or you just don't need to verify it.
// See https://www.php.net/manual/en/context.ssl.php for further options.
$mqtt->setSslContextOptions(
[
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false
]
]
);

if ($mqtt->connect(true, NULL, $username, $password)) {
$mqtt->publish('bluerhinos/phpMQTT/examples/sslContextOptionsTest', 'Hello World! at ' . date('r'), 0, false);
$mqtt->close();
} else {
echo "Time out!\n";
}

?>
18 changes: 17 additions & 1 deletion phpMQTT.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class phpMQTT
protected $username; /* stores username */
protected $password; /* stores password */

public $sslContextOptions; /* null or an associative array */
public $cafile;
protected static $known_commands = [
1 => 'CONNECT',
Expand Down Expand Up @@ -79,6 +80,7 @@ class phpMQTT
*/
public function __construct($address, $port, $clientid, $cafile = null)
{
$this->sslContextOptions = null;
$this->broker($address, $port, $clientid, $cafile);
}

Expand All @@ -98,6 +100,16 @@ public function broker($address, $port, $clientid, $cafile = null): void
$this->cafile = $cafile;
}

/**
* Forces the usage of the "tls://..." protocol and sets the context options.
*
* @param $options - associative array containing the options according to https://www.php.net/manual/en/context.ssl.php
*/
public function setSslContextOptions($options): void
{
$this->sslContextOptions = $options;
}

/**
* Will try and connect, if fails it will sleep 10s and try again, this will enable the script to recover from a network outage
*
Expand Down Expand Up @@ -136,7 +148,11 @@ public function connect($clean = true, $will = null, $username = null, $password
$this->password = $password;
}

if ($this->cafile) {
if (!is_null($this->sslContextOptions)) {
$socketContext = stream_context_create($this->sslContextOptions);
$this->socket = stream_socket_client('tls://' . $this->address . ':' . $this->port, $errno, $errstr, 60, STREAM_CLIENT_CONNECT, $socketContext);
}
elseif ($this->cafile) {
$socketContext = stream_context_create(
[
'ssl' => [
Expand Down