Browse Source

Finetune of pregResponse, it now supports complex queries with multiple equal strings

tags/1.3.1
Paul Rock 6 years ago
parent
commit
e94bea685d
  1. 28
      src/Client.php

28
src/Client.php

@ -197,7 +197,7 @@ class Client implements Interfaces\ClientInterface
$commands = $query->getQuery(); $commands = $query->getQuery();
// Check if first command is export // Check if first command is export
if (strpos($commands[0], '/export') === 0) {
if (0 === strpos($commands[0], '/export')) {
// Convert export command with all arguments to valid SSH command // Convert export command with all arguments to valid SSH command
$arguments = explode('/', $commands[0]); $arguments = explode('/', $commands[0]);
@ -229,7 +229,7 @@ class Client implements Interfaces\ClientInterface
* @return array|string * @return array|string
* @since 1.0.0 * @since 1.0.0
*/ */
private function readRAW()
public function readRAW()
{ {
// By default response is empty // By default response is empty
$response = []; $response = [];
@ -321,8 +321,8 @@ class Client implements Interfaces\ClientInterface
* *
* Based on RouterOSResponseArray solution by @arily * Based on RouterOSResponseArray solution by @arily
* *
* @link https://github.com/arily/RouterOSResponseArray
* @since 1.0.0
* @see https://github.com/arily/RouterOSResponseArray
* @since 1.0.0
*/ */
private function rosario(array $raw): array private function rosario(array $raw): array
{ {
@ -399,7 +399,7 @@ class Client implements Interfaces\ClientInterface
* *
* @param string $value Value which should be parsed * @param string $value Value which should be parsed
* @param array $result Array with parsed response * @param array $result Array with parsed response
* @param null|array $matches Matched words
* @param array|null $matches Matched words
* @param string|int $iterator Type of iterations or number of item * @param string|int $iterator Type of iterations or number of item
*/ */
private function preParseResponse(string $value, array &$result, ?array &$matches, $iterator = 'after'): void private function preParseResponse(string $value, array &$result, ?array &$matches, $iterator = 'after'): void
@ -414,11 +414,11 @@ class Client implements Interfaces\ClientInterface
* Parse result from RouterOS by regular expression * Parse result from RouterOS by regular expression
* *
* @param string $value * @param string $value
* @param null|array $matches
* @param array|null $matches
*/ */
private function pregResponse(string $value, ?array &$matches): void
protected function pregResponse(string $value, ?array &$matches): void
{ {
preg_match_all('/^[=|.](.*)=(.*)/', $value, $matches);
preg_match_all('/^[=|.]([.\w-]+)=(.*)/', $value, $matches);
} }
/** /**
@ -441,13 +441,13 @@ class Client implements Interfaces\ClientInterface
// Now need use this hash for authorization // Now need use this hash for authorization
$query = new Query('/login', [ $query = new Query('/login', [
'=name=' . $this->config('user'), '=name=' . $this->config('user'),
'=response=00' . md5(chr(0) . $this->config('pass') . pack('H*', $response['after']['ret']))
'=response=00' . md5(chr(0) . $this->config('pass') . pack('H*', $response['after']['ret'])),
]); ]);
} else { } else {
// Just login with our credentials // Just login with our credentials
$query = new Query('/login', [ $query = new Query('/login', [
'=name=' . $this->config('user'), '=name=' . $this->config('user'),
'=password=' . $this->config('pass')
'=password=' . $this->config('pass'),
]); ]);
// If we set modern auth scheme but router with legacy firmware then need to retry query, // If we set modern auth scheme but router with legacy firmware then need to retry query,
@ -469,12 +469,12 @@ class Client implements Interfaces\ClientInterface
} }
// If RouterOS answered with invalid credentials then throw error // If RouterOS answered with invalid credentials then throw error
if (!empty($response[0]) && $response[0] === '!trap') {
if (!empty($response[0]) && '!trap' === $response[0]) {
throw new ClientException('Invalid user name or password'); throw new ClientException('Invalid user name or password');
} }
// Return true if we have only one line from server and this line is !done // Return true if we have only one line from server and this line is !done
return (1 === count($response)) && isset($response[0]) && ($response[0] === '!done');
return (1 === count($response)) && isset($response[0]) && ('!done' === $response[0]);
} }
/** /**
@ -487,7 +487,7 @@ class Client implements Interfaces\ClientInterface
*/ */
private function isLegacy(array $response): bool private function isLegacy(array $response): bool
{ {
return count($response) > 1 && $response[0] === '!done' && !$this->config('legacy');
return count($response) > 1 && '!done' === $response[0] && !$this->config('legacy');
} }
/** /**
@ -537,7 +537,7 @@ class Client implements Interfaces\ClientInterface
*/ */
private function isCustomOutput(): bool private function isCustomOutput(): bool
{ {
return $this->customOutput !== null;
return null !== $this->customOutput;
} }
/** /**

Loading…
Cancel
Save