PEAR2_Net_RouterOS  1.0.0b6
A MikroTik RouterOS client
Script Class Reference

Static Public Member Functions

static parseValue ($value, DateTimeZone $timezone=null)
 Parses a value from a RouterOS scripting context. More...
 
static parseValueToString ($value)
 Parses a RouterOS value into a PHP string. More...
 
static parseValueToSimple ($value)
 Parses a RouterOS value into a PHP simple type. More...
 
static parseValueToDateTime ( $value, DateTimeZone $timezone=null)
 Parses a RouterOS value into a PHP DateTime object. More...
 
static parseValueToDateInterval ($value)
 Parses a RouterOS value into a PHP DateInterval. More...
 
static parseValueToArray ( $value, DateTimeZone $timezone=null)
 Parses a RouterOS value into a PHP array. More...
 
static prepare ( $source, array $params=array())
 Prepares a script. More...
 
static append ( $stream, $source, array $params=array())
 Appends a script. More...
 
static escapeValue ($value)
 Escapes a value for a RouterOS scripting context. More...
 
static escapeString ($value, $full=false)
 Escapes a string for a RouterOS scripting context. More...
 

Detailed Description

Definition at line 62 of file Script.php.

Member Function Documentation

◆ append()

static append (   $stream,
  $source,
array  $params = array() 
)
static

Appends a script.

Appends a script to an existing stream.

Parameters
resource$streamAn existing stream to write the resulting script to.
string | resource$sourceThe source of the script, as a string or stream. If a stream is provided, reading starts from the current position to the end of the stream, and the pointer stays at the end after reading is done.
array<string|int,mixed>$params An array of parameters to make available in the script as local variables. Variable names are array keys, and variable values are array values. Array values are automatically processed with static::escapeValue(). Streams are also supported, and are processed in chunks, each with static::escapeString() with all bytes being escaped. Processing starts from the current position to the end of the stream, and the stream's pointer is left untouched after the reading is done. Variables with a value of type "nothing" can be declared with a numeric array key and the variable name as the array value (that is casted to a string).
Returns
int The number of bytes written to $stream is returned, and the pointer remains where it was after the write (i.e. it is not seeked back, even if seeking is supported).

Definition at line 476 of file Script.php.

◆ escapeString()

static escapeString (   $value,
  $full = false 
)
static

Escapes a string for a RouterOS scripting context.

Escapes a string for a RouterOS scripting context. The value can then be surrounded with quotes at a RouterOS script (or concatenated onto a larger string first), and you can be sure there won't be any code injections coming from it.

By default, for the sake of brevity of the output, ASCII alphanumeric characters and underscores are left untouched. And for the sake of character conversion, bytes above 0x7F are also left untouched.

Parameters
string$valueValue to be escaped.
bool$fullWhether to escape all bytes in the string, including ASCII alphanumeric characters, underscores and bytes above 0x7F.
Returns
string The escaped value.

Definition at line 605 of file Script.php.

◆ escapeValue()

static escapeValue (   $value)
static

Escapes a value for a RouterOS scripting context.

Turns any native PHP value into an equivalent whole value that can be inserted as part of a RouterOS script.

DateInterval objects will be casted to RouterOS' "time" type.

DateTime objects will be casted to a string following the "M/d/Y H:i:s" format. If the time is exactly midnight (including microseconds), and the timezone is UTC, the string will include only the "M/d/Y" date.

Unrecognized types (i.e. resources and other objects) are casted to strings, and those strings are then escaped.

Parameters
mixed$valueThe value to be escaped.
Returns
string A string representation that can be directly inserted in a script as a whole value.

Definition at line 531 of file Script.php.

◆ parseValue()

static parseValue (   $value,
DateTimeZone  $timezone = null 
)
static

Parses a value from a RouterOS scripting context.

Turns a value from RouterOS into an equivalent PHP value, based on determining the type in the same way RouterOS would determine it for a literal.

This method is intended to be the very opposite of static::escapeValue(). That is, results from that method, if given to this method, should produce equivalent results.

Parameters
string$valueThe value to be parsed. Must be a literal of a value, e.g. what static::escapeValue() will give you.
DateTimeZone | null$timezoneThe timezone which any resulting DateTime object (either the main value, or values within an array) will use. Defaults to UTC.
Returns
mixed Depending on RouterOS type detected:
  • "nil" (the string "[]") or "nothing" (empty string) - NULL.
  • "num" - int or double for large values.
  • "bool" - a boolean.
  • "array" - an array, with the keys and values processed recursively.
  • "time" - a DateInterval object.
  • "date" (pseudo type; string in the form "M/j/Y") - a DateTime object with the specified date, at midnight.
  • "datetime" (pseudo type; string in the form "M/j/Y H:i:s") - a DateTime object with the specified date and time.
  • "str" (a quoted string) - a string, with the contents escaped.
  • Unrecognized type - casted to a string, unmodified.

Definition at line 95 of file Script.php.

◆ parseValueToArray()

static parseValueToArray (   $value,
DateTimeZone  $timezone = null 
)
static

Parses a RouterOS value into a PHP array.

Parses a RouterOS value into a PHP array.

Parameters
string$valueThe value to be parsed. Must be a literal of a value, e.g. what static::escapeValue() will give you.
DateTimeZone | null$timezoneThe timezone which any resulting DateTime object within the array will use. Defaults to UTC.
Returns
array An array, with the keys and values processed recursively, the keys with static::parseValueToSimple(), and the values with static::parseValue().
Exceptions
ParserExceptionWhen the value is not of a recognized type.

Definition at line 347 of file Script.php.

◆ parseValueToDateInterval()

static parseValueToDateInterval (   $value)
static

Parses a RouterOS value into a PHP DateInterval.

Parses a RouterOS value into a PHP DateInterval.

Parameters
string$valueThe value to be parsed. Must be a literal of a value, e.g. what static::escapeValue() will give you.
Returns
DateInterval The value as a DateInterval object.
Exceptions
ParserExceptionWhen the value is not of a recognized type.

Definition at line 248 of file Script.php.

◆ parseValueToDateTime()

static parseValueToDateTime (   $value,
DateTimeZone  $timezone = null 
)
static

Parses a RouterOS value into a PHP DateTime object.

Parses a RouterOS value into a PHP DateTime object.

Parameters
string$valueThe value to be parsed. Must be a literal of a value, e.g. what static::escapeValue() will give you.
DateTimeZone | null$timezoneThe timezone which the resulting DateTime object will use. Defaults to UTC.
Returns
DateTime Depending on RouterOS type detected:
  • "date" (pseudo type; string in the form "M/j/Y") - a DateTime object with the specified date, at midnight UTC time (regardless of timezone provided).
  • "datetime" (pseudo type; string in the form "M/j/Y H:i:s") - a DateTime object with the specified date and time, with the specified timezone.
Exceptions
ParserExceptionWhen the value is not of a recognized type.

Definition at line 192 of file Script.php.

◆ parseValueToSimple()

static parseValueToSimple (   $value)
static

Parses a RouterOS value into a PHP simple type.

Parses a RouterOS value into a PHP simple type. "Simple" types being scalar types, plus NULL.

Parameters
string$valueThe value to be parsed. Must be a literal of a value, e.g. what static::escapeValue() will give you.
Returns
string|bool|int|double|null Depending on RouterOS type detected:
  • "nil" (the string "[]") or "nothing" (empty string) - NULL.
  • "num" - int or double for large values.
  • "bool" - a boolean.
  • Unrecognized type - casted to a string, unmodified.

Definition at line 155 of file Script.php.

◆ parseValueToString()

static parseValueToString (   $value)
static

Parses a RouterOS value into a PHP string.

Parameters
string$valueThe value to be parsed. Must be a literal of a value, e.g. what static::escapeValue() will give you.
Returns
string If a quoted string is provided, it would be parsed. Otherwise, the value is casted to a string, and returned unmodified.

Definition at line 127 of file Script.php.

◆ prepare()

static prepare (   $source,
array  $params = array() 
)
static

Prepares a script.

Prepares a script for eventual execution by prepending parameters as variables to it.

This is particularly useful when you're creating scripts that you don't want to execute right now (as with Util::exec(), but instead you want to store it for later execution, perhaps by supplying it to "/system scheduler".

Parameters
string | resource$sourceThe source of the script, as a string or stream. If a stream is provided, reading starts from the current position to the end of the stream, and the pointer stays at the end after reading is done.
array<string|int,mixed>$params An array of parameters to make available in the script as local variables. Variable names are array keys, and variable values are array values. Array values are automatically processed with static::escapeValue(). Streams are also supported, and are processed in chunks, each with static::escapeString() with all bytes being escaped. Processing starts from the current position to the end of the stream, and the stream's pointer is left untouched after the reading is done. Variables with a value of type "nothing" can be declared with a numeric array key and the variable name as the array value (that is casted to a string).
Returns
resource A new PHP temporary stream with the script as contents, with the pointer back at the start.
See also
static::append()

Definition at line 438 of file Script.php.


The documentation for this class was generated from the following file: