Communicator.php
RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
PHP version 5
- Author
- Vasil Rangelov
- Category
- Net
- Copyright
- 2011 Vasil Rangelov
- License
- LGPL License 2.1
- Link
- http://pear2.php.net/PEAR2_Net_RouterOS
- Package
- PEAR2_Net_RouterOS
- Version
- 1.0.0b3
\PEAR2\Net\RouterOS\Communicator
Implementation of the RouterOS API protocol. Unlike the other classes in this package, this class doesn't provide any conviniences beyond the low level implementation details (automatic word length encoding/decoding, charset translation and data integrity), and because of that, its direct usage is strongly discouraged.
- See
- \PEAR2\Net\RouterOS\Client
- Author
- Vasil Rangelov
- Category
- Net
- License
- LGPL License 2.1
- Link
- http://pear2.php.net/PEAR2_Net_RouterOS
Constants

CHARSET_REMOTE
= 0
The remote charset is the charset in which RouterOS stores its data. If you want to keep compatibility with your Winbox, this charset should match the default charset from your Windows' regional settings.
Properties


array $charsets = array()
An array with the current charset types as keys, and the current charsets as values.
array()
Details- Type
- array


array $defaultCharsets = array(self::CHARSET_REMOTE => null, self::CHARSET_LOCAL => null)
An array with the default charset types as keys, and the default charsets as values.
array(self::CHARSET_REMOTE => null, self::CHARSET_LOCAL => null)
Details- Type
- array
Methods


__construct(string $host, int $port = 8728, bool $persist = false, float $timeout = null, string $key = '', resource $context = null) : void
Creates a new connection with the specified options.
Name | Type | Description |
---|---|---|
$host | string | Hostname (IP or domain) of the RouterOS server. |
$port | int | The port on which the RouterOS server provides the API service. |
$persist | bool | Whether or not the connection should be a persistent one. |
$timeout | float | The timeout for the connection. |
$key | string | A string that uniquely identifies the connection. |
$context | resource | A context for the socket. |
- See
- \PEAR2\Net\RouterOS\sendWord()


__invoke(string $string = null) : int | string
A shorthand gateway.
This is a magic PHP method that allows you to call the object as a function. Depending on the argument given, one of the other functions in the class is invoked and its returned value is returned by this function.
Name | Type | Description |
---|---|---|
$string | string | A string of the word to send, or NULL to get the next word as a string. |
Type | Description |
---|---|
int | string | If a string is provided, returns the number of bytes sent, otherwise retuns the next word as a string. |


_decodeLength(\PEAR2\Net\Transmitter\Stream $trans) : int
Decodes the lenght of the incoming message.
Decodes the lenght of the incoming message, as specified by the RouterOS API.
Difference with the non private function is that this one doesn't perform locking if the connection is a persistent one.
Name | Type | Description |
---|---|---|
$trans | \PEAR2\Net\Transmitter\Stream | The transmitter from which to decode the length of the incoming message. |
Type | Description |
---|---|
int | The decoded length. |


close() : bool
Closes the opened connection, even if it is a persistent one.
Type | Description |
---|---|
bool | TRUE on success, FALSE on failure. |


decodeLength(\PEAR2\Net\Transmitter\Stream $trans) : int
Decodes the lenght of the incoming message.
Decodes the lenght of the incoming message, as specified by the RouterOS API.
Name | Type | Description |
---|---|---|
$trans | \PEAR2\Net\Transmitter\Stream | The transmitter from which to decode the length of the incoming message. |
Type | Description |
---|---|
int | The decoded length. |


encodeLength(int $length) : string
Encodes the length as requred by the RouterOS API.
Name | Type | Description |
---|---|---|
$length | int | The length to encode. |
Type | Description |
---|---|
string | The encoded length. |


getCharset(int $charsetType) : string | array
Gets the charset(s) for this connection.
Name | Type | Description |
---|---|---|
$charsetType | int | Which charset to get. Valid values are the CHARSET_* constants. Any other value is treated as {@link CHARSET_ALL}. |
Type | Description |
---|---|
string | array | The current charset. If $charsetType is {@link CHARSET_ALL}, the current values will be returned as an array with the types as keys, and charsets as values. |
- See
- \PEAR2\Net\RouterOS\getDefaultCharset()
- See
- \PEAR2\Net\RouterOS\setCharset()


getDefaultCharset(int $charsetType) : string | array
Gets the default charset(s).
Name | Type | Description |
---|---|---|
$charsetType | int | Which charset to get. Valid values are the CHARSET_* constants. Any other value is treated as {@link CHARSET_ALL}. |
Type | Description |
---|---|
string | array | The current charset. If $charsetType is {@link CHARSET_ALL}, the current values will be returned as an array with the types as keys, and charsets as values. |
- See
- \PEAR2\Net\RouterOS\setDefaultCharset()


getNextWord() : string
Get the next word in queue as a string.
Get the next word in queue as a string, after automatically decoding its length.
Type | Description |
---|---|
string | The word. |
- See
- \PEAR2\Net\RouterOS\close()


getNextWordAsStream() : resource
Get the next word in queue as a stream.
Get the next word in queue as a stream, after automatically decoding its length.
Type | Description |
---|---|
resource | The word, as a stream. |
- See
- \PEAR2\Net\RouterOS\close()


getTransmitter() : \PEAR2\Net\Transmitter\TcpClient
Gets the transmitter for this connection.
Type | Description |
---|---|
\PEAR2\Net\Transmitter\TcpClient | The transmitter for this connection. |


iconvStream(string $in_charset, string $out_charset, resource $stream) : resource
Uses iconv to convert a stream from one charset to another.
Name | Type | Description |
---|---|---|
$in_charset | string | The charset of the stream. |
$out_charset | string | The desired resulting charset. |
$stream | resource | The stream to convert. |
Type | Description |
---|---|
resource | A new stream that uses the $out_charset. The stream is a subset from the original stream, from its current position to its end. |


isSeekableStream(mixed $var) : bool
Checks whether a variable is a seekable stream resource.
Name | Type | Description |
---|---|---|
$var | mixed | The value to check. |
Type | Description |
---|---|
bool | TRUE if $var is a seekable stream, FALSE otherwise. |


sendWord(string $word) : int
Sends a word.
Sends a word and automatically encodes its length when doing so.
Name | Type | Description |
---|---|---|
$word | string | The word to send. |
Type | Description |
---|---|
int | The number of bytes sent. |
- See
- \PEAR2\Net\RouterOS\sendWordFromStream()
- See
- \PEAR2\Net\RouterOS\getNextWord()


sendWordFromStream(string $prefix, resource $stream) : int
Sends a word based on a stream.
Sends a word based on a stream and automatically encodes its length when doing so. The stream is read from its current position to its end, and then returned to its current position. Because of those operations, the supplied stream must be seekable.
Name | Type | Description |
---|---|---|
$prefix | string | A string to prepend before the stream contents. |
$stream | resource | The stream to send. |
Type | Description |
---|---|
int | The number of bytes sent. |
- See
- \PEAR2\Net\RouterOS\sendWord()


setCharset(mixed $charset, int $charsetType = self::CHARSET_ALL) : string | array
Sets the charset(s) for this connection.
Sets the charset(s) for this connection. The specified charset(s) will be used for all future words. When sending, CHARSET_LOCAL is converted to CHARSET_REMOTE, and when receiving, CHARSET_REMOTE is converted to CHARSET_LOCAL. Setting NULL to either charset will disable charset convertion, and data will be both sent and received "as is".
Name | Type | Description |
---|---|---|
$charset | mixed | The charset to set. If $charsetType is {@link CHARSET_ALL}, you can supply either a string to use for all charsets, or an array with the charset types as keys, and the charsets as values. |
$charsetType | int | Which charset to set. Valid values are the Communicator::CHARSET_* constants. Any other value is treated as {@link CHARSET_ALL}. |
Type | Description |
---|---|
string | array | The old charset. If $charsetType is {@link CHARSET_ALL}, the old values will be returned as an array with the types as keys, and charsets as values. |
- See
- \PEAR2\Net\RouterOS\setDefaultCharset()


setDefaultCharset(mixed $charset, int $charsetType = self::CHARSET_ALL) : string | array
Sets the default charset(s) for new connections.
Name | Type | Description |
---|---|---|
$charset | mixed | The charset to set. If $charsetType is {@link CHARSET_ALL}, you can supply either a string to use for all charsets, or an array with the charset types as keys, and the charsets as values. |
$charsetType | int | Which charset to set. Valid values are the CHARSET_* constants. Any other value is treated as {@link CHARSET_ALL}. |
Type | Description |
---|---|
string | array | The old charset. If $charsetType is {@link CHARSET_ALL}, the old values will be returned as an array with the types as keys, and charsets as values. |
- See
- \PEAR2\Net\RouterOS\setCharset()


verifyLengthSupport(int $length) : void
Verifies that the length is supported.
Verifies if the specified length is supported by the API. Throws a LengthException if that's not the case. Currently, RouterOS supports words up to 0xFFFFFFF in length, so that's the only check performed.
Name | Type | Description |
---|---|---|
$length | int | The length to verify. |