update Client.php routerOS

This commit is contained in:
Ibnu Maksum 2024-03-28 18:53:41 +07:00
parent 7eae86d861
commit 773cfe0139
No known key found for this signature in database
GPG Key ID: 7FC82848810579E5

View File

@ -19,21 +19,25 @@
/** /**
* The namespace declaration. * The namespace declaration.
*/ */
namespace PEAR2\Net\RouterOS; namespace PEAR2\Net\RouterOS;
/** /**
* Refers to transmitter direction constants. * Refers to transmitter direction constants.
*/ */
use PEAR2\Net\Transmitter\Stream as S; use PEAR2\Net\Transmitter\Stream as S;
/** /**
* Refers to the cryptography constants. * Refers to the cryptography constants.
*/ */
use PEAR2\Net\Transmitter\NetworkStream as N; use PEAR2\Net\Transmitter\NetworkStream as N;
/** /**
* Catches arbitrary exceptions at some points. * Catches arbitrary exceptions at some points.
*/ */
use Exception as E; use Exception as E;
/** /**
@ -108,11 +112,12 @@ class Client
protected $registry = null; protected $registry = null;
/** /**
* Whether to stream future responses. * Stream response words that are above this many bytes.
* NULL to disable streaming completely.
* *
* @var bool * @var int|null
*/ */
private $_streamingResponses = false; private $_streamingResponses = null;
/** /**
* Creates a new instance of a RouterOS API client. * Creates a new instance of a RouterOS API client.
@ -233,7 +238,8 @@ class Client
$password = '', $password = '',
$timeout = null $timeout = null
) { ) {
if (null !== ($remoteCharset = $com->getCharset($com::CHARSET_REMOTE)) if (
null !== ($remoteCharset = $com->getCharset($com::CHARSET_REMOTE))
&& null !== ($localCharset = $com->getCharset($com::CHARSET_LOCAL)) && null !== ($localCharset = $com->getCharset($com::CHARSET_LOCAL))
) { ) {
$password = iconv( $password = iconv(
@ -287,31 +293,48 @@ class Client
$timeout = null $timeout = null
) { ) {
$request = new Request('/login'); $request = new Request('/login');
$request->send($com);
$response = new Response($com, false, $timeout);
$request->setArgument('name', $username); $request->setArgument('name', $username);
$request->setArgument('password', $password); $request->setArgument('password', $password);
// $request->setArgument( $oldCharset = $com->getCharset($com::CHARSET_ALL);
// 'response', $com->setCharset(null, $com::CHARSET_ALL);
// '00' . md5( $request->verify($com)->send($com);
// chr(0) . $password $com->setCharset($oldCharset, $com::CHARSET_ALL);
// . pack('H*', $response->getProperty('ret')) $response = new Response($com, false, $timeout);
// ) if (
// ); $response->getType() === Response::TYPE_FINAL
&& null === $response->getProperty('ret')
) {
// version >= 6.43
return null === $response->getProperty('message');
} elseif ($response->getType() === Response::TYPE_FINAL) {
// version < 6.43
$request->setArgument('password', '');
$request->setArgument(
'response',
'00' . md5(
chr(0) . $password
. pack(
'H*',
is_string($response->getProperty('ret'))
? $response->getProperty('ret')
: stream_get_contents($response->getProperty('ret'))
)
)
);
$request->verify($com)->send($com); $request->verify($com)->send($com);
$response = new Response($com, false, $timeout); $response = new Response($com, false, $timeout);
if ($response->getType() === Response::TYPE_FINAL) { if ($response->getType() === Response::TYPE_FINAL) {
return null === $response->getProperty('ret'); return null === $response->getProperty('ret');
} else { }
while ($response->getType() !== Response::TYPE_FINAL }
while (
$response->getType() !== Response::TYPE_FINAL
&& $response->getType() !== Response::TYPE_FATAL && $response->getType() !== Response::TYPE_FATAL
) { ) {
$response = new Response($com, false, $timeout); $response = new Response($com, false, $timeout);
} }
return false; return false;
} }
}
/** /**
* Sets the charset(s) for this connection. * Sets the charset(s) for this connection.
@ -322,7 +345,7 @@ class Client
* {@link Communicator::CHARSET_REMOTE}, and when receiving, * {@link Communicator::CHARSET_REMOTE}, and when receiving,
* {@link Communicator::CHARSET_REMOTE} is converted to * {@link Communicator::CHARSET_REMOTE} is converted to
* {@link Communicator::CHARSET_LOCAL}. Setting NULL to either charset will * {@link Communicator::CHARSET_LOCAL}. Setting NULL to either charset will
* disable charset convertion, and data will be both sent and received "as * disable charset conversion, and data will be both sent and received "as
* is". * is".
* *
* @param mixed $charset The charset to set. If $charsetType is * @param mixed $charset The charset to set. If $charsetType is
@ -387,7 +410,7 @@ class Client
{ {
//Error checking //Error checking
$tag = $request->getTag(); $tag = $request->getTag();
if ('' == $tag) { if ('' === (string)$tag) {
throw new DataFlowException( throw new DataFlowException(
'Asynchonous commands must have a tag.', 'Asynchonous commands must have a tag.',
DataFlowException::CODE_TAG_REQUIRED DataFlowException::CODE_TAG_REQUIRED
@ -582,7 +605,8 @@ class Client
} }
} else { } else {
list($usStart, $sStart) = explode(' ', microtime()); list($usStart, $sStart) = explode(' ', microtime());
while ($this->getPendingRequestsCount() !== 0 while (
$this->getPendingRequestsCount() !== 0
&& ($sTimeout >= 0 || $usTimeout >= 0) && ($sTimeout >= 0 || $usTimeout >= 0)
) { ) {
$this->dispatchNextResponse($sTimeout, $usTimeout); $this->dispatchNextResponse($sTimeout, $usTimeout);
@ -647,7 +671,7 @@ class Client
public function cancelRequest($tag = null) public function cancelRequest($tag = null)
{ {
$cancelRequest = new Request('/cancel'); $cancelRequest = new Request('/cancel');
$hasTag = !('' == $tag); $hasTag = !('' === (string)$tag);
$hasReg = null !== $this->registry; $hasReg = null !== $this->registry;
if ($hasReg && !$hasTag) { if ($hasReg && !$hasTag) {
$tags = array_merge( $tags = array_merge(
@ -703,34 +727,39 @@ class Client
/** /**
* Sets response streaming setting. * Sets response streaming setting.
* *
* Sets whether future responses are streamed. If responses are streamed, * Sets when future response words are streamed. If a word is streamed,
* the argument values are returned as streams instead of strings. This is * the property value is returned a stream instead of a string, and
* particularly useful if you expect a response that may contain one or more * unrecognized words are returned entirely as streams instead of strings.
* very large words. * This is particularly useful if you expect a response that may contain
* one or more very large words.
* *
* @param bool $streamingResponses Whether to stream future responses. * @param int|null $threshold Threshold after which to stream
* a word. That is, a word less than this length will not be streamed.
* If set to 0, effectively all words are streamed.
* NULL to disable streaming altogether.
* *
* @return bool The previous value of the setting. * @return $this The client object.
* *
* @see isStreamingResponses() * @see getStreamingResponses()
*/ */
public function setStreamingResponses($streamingResponses) public function setStreamingResponses($threshold)
{ {
$oldValue = $this->_streamingResponses; $this->_streamingResponses = $threshold === null
$this->_streamingResponses = (bool) $streamingResponses; ? null
return $oldValue; : (int) $threshold;
return $this;
} }
/** /**
* Gets response streaming setting. * Gets response streaming setting.
* *
* Gets whether future responses are streamed. * Gets when future response words are streamed.
* *
* @return bool The value of the setting. * @return int|null The value of the setting.
* *
* @see setStreamingResponses() * @see setStreamingResponses()
*/ */
public function isStreamingResponses() public function getStreamingResponses()
{ {
return $this->_streamingResponses; return $this->_streamingResponses;
} }
@ -822,8 +851,8 @@ class Client
* If NULL, wait indefinitely. * If NULL, wait indefinitely.
* @param int $usTimeout Microseconds to add to the waiting time. * @param int $usTimeout Microseconds to add to the waiting time.
* *
* @throws SocketException When there's no response within the time limit.
* @return Response The dispatched response. * @return Response The dispatched response.
* @throws SocketException When there's no response within the time limit.
*/ */
protected function dispatchNextResponse($sTimeout = 0, $usTimeout = 0) protected function dispatchNextResponse($sTimeout = 0, $usTimeout = 0)
{ {
@ -846,13 +875,14 @@ class Client
$this->pendingRequestsCount--; $this->pendingRequestsCount--;
} }
if ('' != $tag) { if ('' !== (string)$tag) {
if ($this->isRequestActive($tag, self::FILTER_CALLBACK)) { if ($this->isRequestActive($tag, self::FILTER_CALLBACK)) {
if ($this->callbacks[$tag]($response, $this)) { if ($this->callbacks[$tag]($response, $this)) {
try { try {
$this->cancelRequest($tag); $this->cancelRequest($tag);
} catch (DataFlowException $e) { } catch (DataFlowException $e) {
if ($e->getCode() !== DataFlowException::CODE_UNKNOWN_REQUEST if (
$e->getCode() !== $e::CODE_UNKNOWN_REQUEST
) { ) {
throw $e; throw $e;
} }