diff --git a/examples/ip_filrewall_address-list_print.php b/examples/ip_filrewall_address-list_print.php new file mode 100644 index 0000000..60791cf --- /dev/null +++ b/examples/ip_filrewall_address-list_print.php @@ -0,0 +1,18 @@ + 1, + 'host' => '127.0.0.1', + 'user' => 'admin', + 'pass' => 'admin' +]); + +// Send query to RouterOS +$response = $client->write('/ip/firewall/address-list/print')->read(); +print_r($response); diff --git a/src/Client.php b/src/Client.php index 41090b6..6f5b830 100644 --- a/src/Client.php +++ b/src/Client.php @@ -35,10 +35,10 @@ class Client implements Interfaces\ClientInterface /** * Client constructor. * - * @param array|\RouterOS\Config $config - * @throws \RouterOS\Exceptions\ClientException - * @throws \RouterOS\Exceptions\ConfigException - * @throws \RouterOS\Exceptions\QueryException + * @param array|\RouterOS\Config $config + * @throws \RouterOS\Exceptions\ClientException + * @throws \RouterOS\Exceptions\ConfigException + * @throws \RouterOS\Exceptions\QueryException */ public function __construct($config) { @@ -64,9 +64,9 @@ class Client implements Interfaces\ClientInterface /** * Get some parameter from config * - * @param string $parameter Name of required parameter - * @return mixed - * @throws \RouterOS\Exceptions\ConfigException + * @param string $parameter Name of required parameter + * @return mixed + * @throws \RouterOS\Exceptions\ConfigException */ private function config(string $parameter) { @@ -76,8 +76,8 @@ class Client implements Interfaces\ClientInterface /** * Return socket resource if is exist * - * @return \RouterOS\Config - * @since 0.6 + * @return \RouterOS\Config + * @since 0.6 */ public function getConfig(): Config { @@ -87,8 +87,8 @@ class Client implements Interfaces\ClientInterface /** * Set configuration of client * - * @param \RouterOS\Config $config - * @since 0.7 + * @param \RouterOS\Config $config + * @since 0.7 */ public function setConfig(Config $config) { @@ -98,9 +98,9 @@ class Client implements Interfaces\ClientInterface /** * Send write query to RouterOS (with or without tag) * - * @param string|array|\RouterOS\Query $query - * @return \RouterOS\Client - * @throws \RouterOS\Exceptions\QueryException + * @param string|array|\RouterOS\Query $query + * @return \RouterOS\Client + * @throws \RouterOS\Exceptions\QueryException */ public function write($query): Client { @@ -135,8 +135,8 @@ class Client implements Interfaces\ClientInterface * Reply ends with a complete !done or !fatal block (ended with 'empty line') * A !fatal block precedes TCP connexion close * - * @param bool $parse - * @return array + * @param bool $parse + * @return array */ public function read(bool $parse = true): array { @@ -178,9 +178,9 @@ class Client implements Interfaces\ClientInterface /** * Alias for ->write() method * - * @param string|array|\RouterOS\Query $query - * @return \RouterOS\Client - * @throws \RouterOS\Exceptions\QueryException + * @param string|array|\RouterOS\Query $query + * @return \RouterOS\Client + * @throws \RouterOS\Exceptions\QueryException */ public function w($query): Client { @@ -190,9 +190,9 @@ class Client implements Interfaces\ClientInterface /** * Alias for ->read() method * - * @param bool $parse - * @return array - * @since 0.7 + * @param bool $parse + * @return array + * @since 0.7 */ public function r(bool $parse = true): array { @@ -202,12 +202,12 @@ class Client implements Interfaces\ClientInterface /** * Alias for ->write()->read() combination of methods * - * @param string|array|\RouterOS\Query $query - * @param bool $parse - * @return array - * @throws \RouterOS\Exceptions\ClientException - * @throws \RouterOS\Exceptions\QueryException - * @since 0.6 + * @param string|array|\RouterOS\Query $query + * @param bool $parse + * @return array + * @throws \RouterOS\Exceptions\ClientException + * @throws \RouterOS\Exceptions\QueryException + * @since 0.6 */ public function wr($query, bool $parse = true): array { @@ -217,10 +217,10 @@ class Client implements Interfaces\ClientInterface /** * Parse response from Router OS * - * @param array $response Response data - * @return array Array with parsed data + * @param array $response Response data + * @return array Array with parsed data */ - private function parseResponse(array $response): array + protected function parseResponse(array $response): array { $result = []; $i = -1; @@ -260,8 +260,8 @@ class Client implements Interfaces\ClientInterface /** * Parse result from RouterOS by regular expression * - * @param string $value - * @param array $matches + * @param string $value + * @param array $matches */ private function pregResponse(string $value, &$matches) { @@ -271,11 +271,11 @@ class Client implements Interfaces\ClientInterface /** * Authorization logic * - * @param bool $legacyRetry Retry login if we detect legacy version of RouterOS - * @return bool - * @throws \RouterOS\Exceptions\ClientException - * @throws \RouterOS\Exceptions\ConfigException - * @throws \RouterOS\Exceptions\QueryException + * @param bool $legacyRetry Retry login if we detect legacy version of RouterOS + * @return bool + * @throws \RouterOS\Exceptions\ClientException + * @throws \RouterOS\Exceptions\ConfigException + * @throws \RouterOS\Exceptions\QueryException */ private function login(bool $legacyRetry = false): bool { @@ -320,9 +320,9 @@ class Client implements Interfaces\ClientInterface /** * Detect by login request if firmware is legacy * - * @param array $response - * @return bool - * @throws ConfigException + * @param array $response + * @return bool + * @throws ConfigException */ private function isLegacy(array &$response): bool { @@ -332,10 +332,10 @@ class Client implements Interfaces\ClientInterface /** * Connect to socket server * - * @return bool - * @throws \RouterOS\Exceptions\ClientException - * @throws \RouterOS\Exceptions\ConfigException - * @throws \RouterOS\Exceptions\QueryException + * @return bool + * @throws \RouterOS\Exceptions\ClientException + * @throws \RouterOS\Exceptions\ConfigException + * @throws \RouterOS\Exceptions\QueryException */ private function connect(): bool { diff --git a/src/Rosario.php b/src/Rosario.php new file mode 100644 index 0000000..1449268 --- /dev/null +++ b/src/Rosario.php @@ -0,0 +1,209 @@ +current = 0; + + // This RAW should't be an error + $position = array_keys($raw, '!re'); + + // Split RAW to chinks or use as subarray + if (isset($position[1])) { + $length = $position[1] - $position[0]; + $raw = array_chunk($raw, $length); + array_pop($raw); + } else { + $raw = [$raw]; + } + + // Store parsed RAW data + $this->raw = $raw; + + // Return ready to use array + return $orig; + } + + /** + * Move forward to next element + */ + public function next() + { + ++$this->current; + } + + /** + * Return the current element + * + * @return mixed + */ + public function current() + { + if (isset($this->parsed[$this->current])) { + return $this->parsed[$this->current]; + } + + if (isset($this->raw[$this->current])) { + return $this->parseResponse($this->raw[$this->current])[0]; + } + + return false; + } + + /** + * Return the key of the current element + * + * @return mixed + */ + public function key() + { + return $this->current; + } + + /** + * Checks if current position is valid + * + * @return bool + */ + public function valid(): bool + { + return isset($this->raw[$this->current]); + } + + /** + * Count elements of an object + * + * @return int + */ + public function count(): int + { + return count($this->raw); + } + + /** + * Rewind the Iterator to the first element + */ + public function rewind() + { + $this->current = 0; + } + + /** + * Offset to set + * + * @param mixed $offset + * @param mixed $value + */ + public function offsetSet($offset, $value) + { + if (null === $offset) { + $this->parsed[] = $value; + } + $this->parsed[$offset] = $value; + } + + /** + * Whether a offset exists + * + * @param mixed $offset + * @return bool + */ + public function offsetExists($offset): bool + { + return isset($this->raw[$offset]); + } + + /** + * Offset to unset + * + * @param mixed $offset + */ + public function offsetUnset($offset) + { + unset($this->parsed[$offset], $this->raw[$offset]); + } + + /** + * Offset to retrieve + * + * @param mixed $offset + * @return mixed + */ + public function offsetGet($offset) + { + if (isset($this->parsed[$offset])) { + return $this->parsed[$offset]; + } + + if (isset($this->raw[$offset])) { + return $this->parsed[$offset] = $this->parseResponse($this->raw[$offset])[0]; + } + + // For empty() function + return null; + } + + /** + * Cleanup the array + */ + public function flush() + { + $this->raw = []; + $this->parsed = []; + } + +}