Communicator.phpView Source

Show: PublicProtectedPrivateinherited
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.0b4  

\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
 
Author
Vasil Rangelov  
Category
Net  
License
LGPL License 2.1  
Link
http://pear2.php.net/PEAR2_Net_RouterOS  

Constants

>VConstant  CHARSET_ALL = -1
Used when getting/setting all (default) charsets.
>VConstant  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.
>VConstant  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

>VPropertyprotectedarray $charsets = array()

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

Default valuearray()Details
Type
array
>VPropertyprotectedarray $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
>VPropertyprotected\PEAR2\Net\Transmitter\TcpClient $trans

The transmitter for the connection.

Methods

methodpublic__construct(string $host, int | null $port = 8728, bool $persist = false, float $timeout = null, string $key = '', string $crypto = \PEAR2\Net\Transmitter\NetworkStream::CRYPTO_OFF, resource $context = null) : void

Creates a new connection with the specified options.

Parameters
NameTypeDescription
$hoststring

Hostname (IP or domain) of the RouterOS server.

$portint | null

The port on which the RouterOS server provides the API service. You can also specify NULL, in which case the port will automatically be chosen between 8728 and 8729, depending on the value of $crypto.

$persistbool

Whether or not the connection should be a persistent one.

$timeoutfloat

The timeout for the connection.

$keystring

A string that uniquely identifies the connection.

$cryptostring

The encryption for this connection. Must be one of the PEAR2\Net\Transmitter\NetworkStream::CRYPTO_* constants. Off by default. RouterOS currently supports only TLS, but the setting is provided in this fashion for forward compatibility's sake. And for the sake of simplicity, if you specify an encryption, don't specify a context and your default context uses the value "DEFAULT" for ciphers, "ADH" will be automatically added to the list of ciphers.

$contextresource

A context for the socket.

Details
See
 
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
NameTypeDescription
$stringstring

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

Returns
TypeDescription
int | stringIf 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
NameTypeDescription
$trans\PEAR2\Net\Transmitter\Stream

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

Returns
TypeDescription
intThe decoded length.
methodpublicclose() : bool

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

Returns
TypeDescription
boolTRUE 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
NameTypeDescription
$trans\PEAR2\Net\Transmitter\Stream

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

Returns
TypeDescription
intThe decoded length.
methodpublicencodeLength(int $length) : string
static

Encodes the length as requred by the RouterOS API.

Parameters
NameTypeDescription
$lengthint

The length to encode.

Returns
TypeDescription
stringThe encoded length.
methodpublicgetCharset(int $charsetType) : string | array

Gets the charset(s) for this connection.

Parameters
NameTypeDescription
$charsetTypeint

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

Returns
TypeDescription
string | arrayThe 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
 
See
 
methodpublicgetDefaultCharset(int $charsetType) : string | array
static

Gets the default charset(s).

Parameters
NameTypeDescription
$charsetTypeint

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

Returns
TypeDescription
string | arrayThe 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
 
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
TypeDescription
stringThe word.
Details
See
 
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
TypeDescription
resourceThe word, as a stream.
Details
See
 
methodpublicgetTransmitter() : \PEAR2\Net\Transmitter\TcpClient

Gets the transmitter for this connection.

Returns
TypeDescription
\PEAR2\Net\Transmitter\TcpClientThe 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
NameTypeDescription
$in_charsetstring

The charset of the stream.

$out_charsetstring

The desired resulting charset.

$streamresource

The stream to convert.

Returns
TypeDescription
resourceA 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
NameTypeDescription
$varmixed

The value to check.

Returns
TypeDescription
boolTRUE 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
NameTypeDescription
$wordstring

The word to send.

Returns
TypeDescription
intThe number of bytes sent.
Details
See
 
See
 
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
NameTypeDescription
$prefixstring

A string to prepend before the stream contents.

$streamresource

The stream to send.

Returns
TypeDescription
intThe number of bytes sent.
Details
See
 
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, {@link CHARSET_LOCAL} is converted to {@link CHARSET_REMOTE}, and when receiving, {@link CHARSET_REMOTE} is converted to {@link CHARSET_LOCAL}. Setting NULL to either charset will disable charset convertion, and data will be both sent and received "as is".
Parameters
NameTypeDescription
$charsetmixed

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.

$charsetTypeint

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

Returns
TypeDescription
string | arrayThe 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
 
methodpublicsetDefaultCharset(mixed $charset, int $charsetType = self::CHARSET_ALL) : string | array
static

Sets the default charset(s) for new connections.

Parameters
NameTypeDescription
$charsetmixed

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.

$charsetTypeint

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

Returns
TypeDescription
string | arrayThe 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
 
methodprotectedverifyLengthSupport(int $length) : void
static

Verifies that the length is supported.

Verifies if the specified length is supported by the API. Throws a {@link LengthException} if that's not the case. Currently, RouterOS supports words up to 0xFFFFFFF in length, so that's the only check performed.
Parameters
NameTypeDescription
$lengthint

The length to verify.

Documentation was generated by phpDocumentor 2.1.0-DEV .