|
|
@ -10,6 +10,8 @@ class Ratchet implements MessageComponentInterface |
|
|
|
|
|
|
|
|
|
|
|
private object $_config; |
|
|
|
private object $_config; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private array $_namespaces; |
|
|
|
|
|
|
|
|
|
|
|
public function __construct( |
|
|
|
public function __construct( |
|
|
|
object $config |
|
|
|
object $config |
|
|
|
) { |
|
|
|
) { |
|
|
@ -31,6 +33,50 @@ class Ratchet implements MessageComponentInterface |
|
|
|
throw new \Exception(); // @TODO |
|
|
|
throw new \Exception(); // @TODO |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Init allowed chat rooms registry |
|
|
|
|
|
|
|
if ($namespaces = $this->_kevacoin->kevaListNamespaces()) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$i = 1; foreach ((array) $namespaces as $namespace) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// Skip system namespaces |
|
|
|
|
|
|
|
if (str_starts_with($namespace['displayName'], '_')) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Skip blacklist |
|
|
|
|
|
|
|
if (in_array($namespace['namespaceId'], $this->_config->kevacoin->wallet->namespace->blacklist)) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Check whitelist on contain records |
|
|
|
|
|
|
|
if (count($this->_config->kevacoin->wallet->namespace->whitelist) && |
|
|
|
|
|
|
|
!in_array($namespace['namespaceId'], $this->_config->kevacoin->wallet->namespace->whitelist)) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Append room to the namespace registry |
|
|
|
|
|
|
|
$this->_namespaces[$i++] = [ |
|
|
|
|
|
|
|
'hash' => $namespace['namespaceId'], |
|
|
|
|
|
|
|
'name' => $namespace['displayName'] |
|
|
|
|
|
|
|
]; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Sort rooms by name ASC |
|
|
|
|
|
|
|
array_multisort( |
|
|
|
|
|
|
|
array_column( |
|
|
|
|
|
|
|
$this->_namespaces, |
|
|
|
|
|
|
|
'name' |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
SORT_ASC, |
|
|
|
|
|
|
|
SORT_STRING | SORT_NATURAL | SORT_FLAG_CASE |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else throw new \Exception(); // @TODO |
|
|
|
|
|
|
|
|
|
|
|
// Dump event on enabled |
|
|
|
// Dump event on enabled |
|
|
|
if ($this->_config->nps->event->init->debug->enabled) |
|
|
|
if ($this->_config->nps->event->init->debug->enabled) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -40,7 +86,8 @@ class Ratchet implements MessageComponentInterface |
|
|
|
'{time}', |
|
|
|
'{time}', |
|
|
|
'{host}', |
|
|
|
'{host}', |
|
|
|
'{port}', |
|
|
|
'{port}', |
|
|
|
'{keva}' |
|
|
|
'{keva}', |
|
|
|
|
|
|
|
'{room}' |
|
|
|
], |
|
|
|
], |
|
|
|
[ |
|
|
|
[ |
|
|
|
(string) date('c'), |
|
|
|
(string) date('c'), |
|
|
@ -48,6 +95,10 @@ class Ratchet implements MessageComponentInterface |
|
|
|
(string) $this->_config->nps->server->port, |
|
|
|
(string) $this->_config->nps->server->port, |
|
|
|
(float) $this->_kevacoin->getBalance( |
|
|
|
(float) $this->_kevacoin->getBalance( |
|
|
|
$this->_config->kevacoin->wallet->account |
|
|
|
$this->_config->kevacoin->wallet->account |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
(string) print_r( |
|
|
|
|
|
|
|
$this->_namespaces, |
|
|
|
|
|
|
|
true |
|
|
|
) |
|
|
|
) |
|
|
|
], |
|
|
|
], |
|
|
|
$this->_config->nps->event->init->debug->template |
|
|
|
$this->_config->nps->event->init->debug->template |
|
|
@ -104,6 +155,9 @@ class Ratchet implements MessageComponentInterface |
|
|
|
// Init connection confirmed |
|
|
|
// Init connection confirmed |
|
|
|
$connection->confirmed = false; |
|
|
|
$connection->confirmed = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Init connection room |
|
|
|
|
|
|
|
$connection->namespace = null; |
|
|
|
|
|
|
|
|
|
|
|
// Init connection counter |
|
|
|
// Init connection counter |
|
|
|
$connection->count = 0; |
|
|
|
$connection->count = 0; |
|
|
|
|
|
|
|
|
|
|
@ -179,6 +233,9 @@ class Ratchet implements MessageComponentInterface |
|
|
|
|
|
|
|
|
|
|
|
// Connection confirmed |
|
|
|
// Connection confirmed |
|
|
|
if ($connection->confirmed) |
|
|
|
if ($connection->confirmed) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// Room selected, begin message compose |
|
|
|
|
|
|
|
if ($connection->namespace) |
|
|
|
{ |
|
|
|
{ |
|
|
|
// Check message commit by dot |
|
|
|
// Check message commit by dot |
|
|
|
if ($request == '.') |
|
|
|
if ($request == '.') |
|
|
@ -198,7 +255,7 @@ class Ratchet implements MessageComponentInterface |
|
|
|
else |
|
|
|
else |
|
|
|
{ |
|
|
|
{ |
|
|
|
// Save massage to KevaCoin blockchain |
|
|
|
// Save massage to KevaCoin blockchain |
|
|
|
if ($txid = $this->_kevacoin->kevaPut($this->_config->kevacoin->wallet->namespace, time(), $connection->message)) |
|
|
|
if ($txid = $this->_kevacoin->kevaPut($connection->namespace, time(), $connection->message)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
// Return success response |
|
|
|
// Return success response |
|
|
|
$connection->send( |
|
|
|
$connection->send( |
|
|
@ -208,7 +265,7 @@ class Ratchet implements MessageComponentInterface |
|
|
|
'{txid}' |
|
|
|
'{txid}' |
|
|
|
], |
|
|
|
], |
|
|
|
[ |
|
|
|
[ |
|
|
|
(string) $this->_config->kevacoin->wallet->namespace, |
|
|
|
(string) $connection->namespace, |
|
|
|
(string) $txid |
|
|
|
(string) $txid |
|
|
|
], |
|
|
|
], |
|
|
|
implode( |
|
|
|
implode( |
|
|
@ -235,7 +292,7 @@ class Ratchet implements MessageComponentInterface |
|
|
|
(string) date('c'), |
|
|
|
(string) date('c'), |
|
|
|
(string) $connection->remoteAddress, |
|
|
|
(string) $connection->remoteAddress, |
|
|
|
(string) $connection->resourceId, |
|
|
|
(string) $connection->resourceId, |
|
|
|
(string) $this->_config->kevacoin->wallet->namespace, |
|
|
|
(string) $connection->namespace, |
|
|
|
(string) $txid, |
|
|
|
(string) $txid, |
|
|
|
(string) $this->_kevacoin->getBalance( |
|
|
|
(string) $this->_kevacoin->getBalance( |
|
|
|
$this->_config->kevacoin->wallet->account |
|
|
|
$this->_config->kevacoin->wallet->account |
|
|
@ -293,27 +350,99 @@ class Ratchet implements MessageComponentInterface |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Captcha request |
|
|
|
// Room not selected yet and number given match registry |
|
|
|
|
|
|
|
else if (isset($this->_namespaces[$request])) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// Update connection namespace |
|
|
|
|
|
|
|
$connection->namespace = $this->_namespaces[$request]['hash']; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Send room selection request |
|
|
|
|
|
|
|
$connection->send( |
|
|
|
|
|
|
|
str_ireplace( |
|
|
|
|
|
|
|
[ |
|
|
|
|
|
|
|
'{room}' |
|
|
|
|
|
|
|
], |
|
|
|
|
|
|
|
[ |
|
|
|
|
|
|
|
str_replace( // filter possible meta mask in the room names |
|
|
|
|
|
|
|
'%', |
|
|
|
|
|
|
|
'%%', |
|
|
|
|
|
|
|
$this->_namespaces[$request]['name'] |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
], |
|
|
|
|
|
|
|
implode( |
|
|
|
|
|
|
|
PHP_EOL, |
|
|
|
|
|
|
|
$config->response->room->success |
|
|
|
|
|
|
|
) . PHP_EOL |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Room number not found in registry |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// Reset connection room anyway |
|
|
|
|
|
|
|
$connection->namespace = null; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Send fail response |
|
|
|
|
|
|
|
$connection->send( |
|
|
|
|
|
|
|
implode( |
|
|
|
|
|
|
|
PHP_EOL, |
|
|
|
|
|
|
|
$config->response->room->failure |
|
|
|
|
|
|
|
) . PHP_EOL |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Keep connection alive for new attempt.. |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Captcha confirmation code expected |
|
|
|
else |
|
|
|
else |
|
|
|
{ |
|
|
|
{ |
|
|
|
// Request match captcha |
|
|
|
// Request match captcha |
|
|
|
if ($request == $connection->captcha) |
|
|
|
if ($request == $connection->captcha) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
// Set connection confirmed |
|
|
|
$connection->confirmed = true; |
|
|
|
$connection->confirmed = true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Build room list |
|
|
|
|
|
|
|
$rooms = []; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach ($this->_namespaces as $number => $namespace) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$rooms[] = sprintf( |
|
|
|
|
|
|
|
'[%d] %s', |
|
|
|
|
|
|
|
$number, |
|
|
|
|
|
|
|
$namespace['name'] |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Send room selection request |
|
|
|
$connection->send( |
|
|
|
$connection->send( |
|
|
|
|
|
|
|
str_ireplace( |
|
|
|
|
|
|
|
[ |
|
|
|
|
|
|
|
'{room:list}' |
|
|
|
|
|
|
|
], |
|
|
|
|
|
|
|
[ |
|
|
|
|
|
|
|
implode( |
|
|
|
|
|
|
|
PHP_EOL, |
|
|
|
|
|
|
|
$rooms |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
], |
|
|
|
implode( |
|
|
|
implode( |
|
|
|
PHP_EOL, |
|
|
|
PHP_EOL, |
|
|
|
$config->response->captcha->success |
|
|
|
$config->response->captcha->success |
|
|
|
) . PHP_EOL |
|
|
|
) . PHP_EOL |
|
|
|
|
|
|
|
) |
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Captcha request invalid |
|
|
|
// Captcha request invalid |
|
|
|
else |
|
|
|
else |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
// Reset confirmed status |
|
|
|
$connection->confirmed = false; |
|
|
|
$connection->confirmed = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Send fail response |
|
|
|
$connection->send( |
|
|
|
$connection->send( |
|
|
|
implode( |
|
|
|
implode( |
|
|
|
PHP_EOL, |
|
|
|
PHP_EOL, |
|
|
@ -321,6 +450,7 @@ class Ratchet implements MessageComponentInterface |
|
|
|
) . PHP_EOL |
|
|
|
) . PHP_EOL |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Drop connection to prevent brute force |
|
|
|
$connection->close(); |
|
|
|
$connection->close(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|