Communicator.php

Show: inherited
Table of Contents
RouterOS API client implementation.

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

Package: PEAR2\Net\RouterOS
A 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

Constant  CHARSET_ALL = -1
Used when getting/setting all (default) charsets.
Constant  CHARSET_REMOTE = 0
Used when getting/setting the (default) remote charset.

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.

Constant  CHARSET_LOCAL = 1
Used when getting/setting the (default) local charset.

The local charset is the charset in which the data from RouterOS will be returned as. This charset should match the charset of the place the data will eventually be written to.

Properties

Propertyprotectedarray $charsets = array()

An array with the current charset types as keys, and the current charsets as values.

Default valuearray()Details
Type
array
Propertyprotectedarray $defaultCharsets = array(self::CHARSET_REMOTE => null, self::CHARSET_LOCAL => null)
static

An array with the default charset types as keys, and the default charsets as values.

Default valuearray(self::CHARSET_REMOTE => null, self::CHARSET_LOCAL => null)Details
Type
array
Propertyprotected\PEAR2\Net\Transmitter\TcpClient $trans

The transmitter for the connection.

Details
Type
\PEAR2\Net\Transmitter\TcpClient

Methods

methodpublic__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.

Parameters
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.

Details
See
\PEAR2\Net\RouterOS\sendWord()  
methodpublic__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.

Parameters
Name Type Description
$string string

A string of the word to send, or NULL to get the next word as a string.

Returns
Type Description
int | string If a string is provided, returns the number of bytes sent, otherwise retuns the next word as a string.
methodprivate_decodeLength(\PEAR2\Net\Transmitter\Stream $trans) : int
static

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.

Parameters
Name Type Description
$trans \PEAR2\Net\Transmitter\Stream

The transmitter from which to decode the length of the incoming message.

Returns
Type Description
int The decoded length.
methodpublicclose() : bool

Closes the opened connection, even if it is a persistent one.

Returns
Type Description
bool TRUE on success, FALSE on failure.
methodpublicdecodeLength(\PEAR2\Net\Transmitter\Stream $trans) : int
static

Decodes the lenght of the incoming message.

Decodes the lenght of the incoming message, as specified by the RouterOS API.

Parameters
Name Type Description
$trans \PEAR2\Net\Transmitter\Stream

The transmitter from which to decode the length of the incoming message.

Returns
Type Description
int The decoded length.
methodpublicencodeLength(int $length) : string
static

Encodes the length as requred by the RouterOS API.

Parameters
Name Type Description
$length int

The length to encode.

Returns
Type Description
string The encoded length.
methodpublicgetCharset(int $charsetType) : string | array

Gets the charset(s) for this connection.

Parameters
Name Type Description
$charsetType int

Which charset to get. Valid values are the CHARSET_* constants. Any other value is treated as {@link CHARSET_ALL}.

Returns
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.
Details
See
\PEAR2\Net\RouterOS\getDefaultCharset()  
See
\PEAR2\Net\RouterOS\setCharset()  
methodpublicgetDefaultCharset(int $charsetType) : string | array
static

Gets the default charset(s).

Parameters
Name Type Description
$charsetType int

Which charset to get. Valid values are the CHARSET_* constants. Any other value is treated as {@link CHARSET_ALL}.

Returns
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.
Details
See
\PEAR2\Net\RouterOS\setDefaultCharset()  
methodpublicgetNextWord() : string

Get the next word in queue as a string.

Get the next word in queue as a string, after automatically decoding its length.

Returns
Type Description
string The word.
Details
See
\PEAR2\Net\RouterOS\close()  
methodpublicgetNextWordAsStream() : resource

Get the next word in queue as a stream.

Get the next word in queue as a stream, after automatically decoding its length.

Returns
Type Description
resource The word, as a stream.
Details
See
\PEAR2\Net\RouterOS\close()  
methodpublicgetTransmitter() : \PEAR2\Net\Transmitter\TcpClient

Gets the transmitter for this connection.

Returns
Type Description
\PEAR2\Net\Transmitter\TcpClient The transmitter for this connection.
methodpubliciconvStream(string $in_charset, string $out_charset, resource $stream) : resource
static

Uses iconv to convert a stream from one charset to another.

Parameters
Name Type Description
$in_charset string

The charset of the stream.

$out_charset string

The desired resulting charset.

$stream resource

The stream to convert.

Returns
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.
methodpublicisSeekableStream(mixed $var) : bool
static

Checks whether a variable is a seekable stream resource.

Parameters
Name Type Description
$var mixed

The value to check.

Returns
Type Description
bool TRUE if $var is a seekable stream, FALSE otherwise.
methodpublicsendWord(string $word) : int

Sends a word.

Sends a word and automatically encodes its length when doing so.

Parameters
Name Type Description
$word string

The word to send.

Returns
Type Description
int The number of bytes sent.
Details
See
\PEAR2\Net\RouterOS\sendWordFromStream()  
See
\PEAR2\Net\RouterOS\getNextWord()  
methodpublicsendWordFromStream(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.

Parameters
Name Type Description
$prefix string

A string to prepend before the stream contents.

$stream resource

The stream to send.

Returns
Type Description
int The number of bytes sent.
Details
See
\PEAR2\Net\RouterOS\sendWord()  
methodpublicsetCharset(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".

Parameters
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}.

Returns
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.
Details
See
\PEAR2\Net\RouterOS\setDefaultCharset()  
methodpublicsetDefaultCharset(mixed $charset, int $charsetType = self::CHARSET_ALL) : string | array
static

Sets the default charset(s) for new connections.

Parameters
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}.

Returns
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.
Details
See
\PEAR2\Net\RouterOS\setCharset()  
methodprotectedverifyLengthSupport(int $length) : void
static

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.

Parameters
Name Type Description
$length int

The length to verify.

Documentation was generated by phpDocumentor 2.0.0a8.