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

Fix/handle unencrypted message #94

Merged
merged 6 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
38 changes: 38 additions & 0 deletions examples/cli/hist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

set_time_limit(0);

require('../../vendor/autoload.php');

use PubNub\PubNub;
use PubNub\PNConfiguration;
use PubNub\CryptoModule;

$pnUuid = 'pn-610da4553bb079.92567429'; //uniqid('pn-', true);

$pnConfig = new PNConfiguration();
$pnConfig->setPublishKey(getenv('PN_KEY_PUBLISH'));
$pnConfig->setSubscribeKey(getenv('PN_KEY_SUBSCRIBE'));
// $pnConfig->setSecretKey('sec-c-YjZiZWQzZWItMmZlNS00NjBlLTkyNTUtOGFhZjZiY2E1ZDc1');
$pnConfig->setUuid($pnUuid);
if (array_key_exists(2, $argv)) {
$pnConfig->setCrypto(CryptoModule::aesCbcCryptor($argv[2], true));
}

$pubnub = new PubNub($pnConfig);

$ts = $pubnub->time()->sync();

$channelName = $argv[1];
$history = $pubnub->history()
->channel($channelName)
->reverse(false)
->end((int)($ts->getTimetoken() - 3000000000))
->count(50)
->sync();

foreach ($history->getMessages() as $key => $message) {
print($message);
}
26 changes: 26 additions & 0 deletions examples/cli/pub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

set_time_limit(0);
seba-aln marked this conversation as resolved.
Show resolved Hide resolved

require('../../vendor/autoload.php');


use PubNub\PubNub;
use PubNub\PNConfiguration;
use PubNub\CryptoModule;

$pnUuid = $argv[1] . '-pn-610da4553bb079.92567429';

$pnConfig = new PNConfiguration();
$pnConfig->setPublishKey(getenv('PN_KEY_PUBLISH'));
$pnConfig->setSubscribeKey(getenv('PN_KEY_SUBSCRIBE'));
$pnConfig->setUuid($pnUuid);
if (array_key_exists(4, $argv)) {
$pnConfig->setCrypto(CryptoModule::aesCbcCryptor($argv[4], true));
}

$pubnub = new PubNub($pnConfig);

$pubResult = $pubnub->publish()->channel($argv[2])->message($argv[3])->sync();
104 changes: 104 additions & 0 deletions examples/cli/sub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

declare(strict_types=1);

error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED);

set_time_limit(0);

require('../../vendor/autoload.php');

use PubNub\Callbacks\SubscribeCallback;
use PubNub\Exceptions\PubNubException;
use PubNub\Models\Consumer\PubSub\PNMessageResult;
use PubNub\Models\Consumer\PubSub\PNPresenceEventResult;
use PubNub\Models\ResponseHelpers\PNStatus;
use PubNub\PubNub;
use PubNub\PNConfiguration;
use PubNub\CryptoModule;


$pnUuid = 'pn-610da4553bb079.92567429';

$pnConfig = new PNConfiguration();
$pnConfig->setPublishKey(getenv('PN_KEY_PUBLISH'));
$pnConfig->setSubscribeKey(getenv('PN_KEY_SUBSCRIBE'));
if (array_key_exists(2, $argv)) {
$pnConfig->setCrypto(CryptoModule::aesCbcCryptor($argv[2], true));
}
$pnConfig->setUuid($pnUuid);

$pubnub = new PubNub($pnConfig);

$channelName = $argv[1];

// phpcs:ignore
class MySubscribeCallback extends SubscribeCallback
{
/**
* @param PubNub $pubnub
* @param PNStatus $status
* @return void
* @throws PubNubException
*/
public function status($pubnub, $status)
{
printf(
"Category: %s\nPublisher user_id: %s",
$status->getCategory(),
$status->getUuid()
);
}

/**
* @param PubNub $pubnub
* @param PNMessageResult $messageResult
* @return void
*/
public function message($pubnub, $messageResult)
{
printf(
"\nMessage %s\n Channel: %s\n Timetoken: %s\n Publisher: %s\n",
$messageResult->getMessage(),
$messageResult->getChannel(),
$messageResult->getTimetoken(),
$messageResult->getPublisher(),
);
if ($messageResult->isError()) {
printf('\nError occured during parsing the message: %s', $messageResult->getError()->getMessage());
}
}

/**
* @param PubNub $pubnub
* @param PNPresenceEventResult $presence
* @return void
*/
public function presence($pubnub, $presence)
{
print("{$presence->getEvent()}: {$presence->getUuid()}");
}

/**
* @param PubNub $pubnub
* @param PNSignalMessageResult $signal
* @return void
*/
public function signal($pubnub, $signal)
{
}
}

$subscribeCallback = new MySubscribeCallback();

$pubnub->addListener($subscribeCallback);

echo "subscribing to: $channelName\n";

$subResult = $pubnub->subscribe()
->channels($channelName)
->withTimetoken(true)
->withPresence(true)
->execute();

echo "done.\n";
16 changes: 9 additions & 7 deletions src/PubNub/Managers/SubscriptionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace PubNub\Managers;


use PubNub\Exceptions\PubNubResponseParsingException;
use PubNub\Models\Consumer\PubSub\PNPresenceEventResult;
use PubNub\Builders\DTO\SubscribeOperation;
Expand All @@ -24,7 +23,6 @@
use PubNub\PubNub;
use PubNub\PubNubUtil;


class SubscriptionManager
{
/** @var PubNub */
Expand Down Expand Up @@ -251,17 +249,21 @@ protected function processIncomingPayload($message)
);

$this->listenerManager->announcePresence($pnPresenceResult);

} else {
$extractedMessage = $this->processMessage($message->getPayload());
$messageError = null;
try {
$extractedMessage = $this->processMessage($message->getPayload());
} catch (PubNubResponseParsingException $exception) {
$extractedMessage = $message->getPayload();
$messageError = $exception;
}
seba-aln marked this conversation as resolved.
Show resolved Hide resolved
$publisher = $message->getIssuingClientId();

if ($extractedMessage === null) {
$this->pubnub->getLogger()->debug("unable to parse payload on #processIncomingMessages");
}

if (MessageType::SIGNAL == $message->getMessageType()) {

$pnSignalResult = new PNSignalMessageResult(
$extractedMessage,
$channel,
Expand All @@ -272,13 +274,13 @@ protected function processIncomingPayload($message)

$this->listenerManager->announceSignal($pnSignalResult);
} else {

$pnMessageResult = new PNMessageResult(
$extractedMessage,
$channel,
$subscriptionMatch,
$publishMetadata->getPublishTimetoken(),
$publisher
$publisher,
$messageError
);

$this->listenerManager->announceMessage($pnMessageResult);
Expand Down
17 changes: 11 additions & 6 deletions src/PubNub/Models/Consumer/History/PNHistoryItemResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use PubNub\PubNubCryptoCore;


class PNHistoryItemResult
{
/** @var any */
Expand All @@ -16,6 +15,8 @@ class PNHistoryItemResult
/** @var int */
private $timetoken;

private $error = null;

/**
* PNHistoryItemResult constructor.
* @param string $entry
Expand All @@ -36,10 +37,14 @@ public function __toString()

public function decrypt()
{
if (is_string($this->entry)) {
$this->entry = $this->crypto->decrypt($this->entry);
} elseif (is_array($this->entry) and key_exists('pn_other', $this->entry)) {
$this->entry['pn_other'] = $this->crypto->decrypt($this->entry['pn_other']);
try {
if (is_string($this->entry)) {
$this->entry = $this->crypto->decrypt($this->entry);
} elseif (is_array($this->entry) and key_exists('pn_other', $this->entry)) {
$this->entry['pn_other'] = $this->crypto->decrypt($this->entry['pn_other']);
}
} catch (\Exception $error) {
$this->error = $error;
}
}

Expand All @@ -58,4 +63,4 @@ public function getTimetoken()
{
return $this->timetoken;
}
}
}
16 changes: 14 additions & 2 deletions src/PubNub/Models/Consumer/PubSub/PNMessageResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace PubNub\Models\Consumer\PubSub;


class PNMessageResult
{
/** @var array */
Expand All @@ -20,6 +19,8 @@ class PNMessageResult
/** @var string */
private $publisher;

private $error;

/**
* PNMessageResult constructor.
* @param array $message
Expand All @@ -28,13 +29,14 @@ class PNMessageResult
* @param int $timetoken
* @param string $publisher
*/
public function __construct($message, $channel, $subscription, $timetoken, $publisher)
public function __construct($message, $channel, $subscription, $timetoken, $publisher, $error = null)
{
$this->message = $message;
$this->channel = $channel;
$this->subscription = $subscription;
$this->timetoken = $timetoken;
$this->publisher = $publisher;
$this->error = $error;
}

/**
Expand Down Expand Up @@ -76,4 +78,14 @@ public function getPublisher()
{
return $this->publisher;
}

public function isError(): bool
{
return !is_null($this->error);
}

public function getError()
{
return $this->error;
}
}