PEAR2_Net_RouterOS  1.0.0b5
A MikroTik RouterOS client
 All Data Structures Namespaces Files Functions Variables Pages
Util Class Reference
+ Inheritance diagram for Util:
+ Collaboration diagram for Util:

Public Member Functions

 __construct (Client $client)
 Creates a new Util instance. More...
 
 getMenu ()
 Gets the current menu. More...
 
 setMenu ($newMenu)
 Sets the current menu. More...
 
 exec ($source, array $params=array(), $policy=null, $name=null)
 Executes a RouterOS script. More...
 
 clearIdCache ()
 Clears the ID cache. More...
 
 find ()
 Finds the IDs of items at the current menu. More...
 
 get ($number, $valueName)
 Gets a value of a specified item at the current menu. More...
 
 enable ()
 Enables all items at the current menu matching certain criteria. More...
 
 disable ()
 Disables all items at the current menu matching certain criteria. More...
 
 remove ()
 Removes all items at the current menu matching certain criteria. More...
 
 set ($numbers, array $newValues)
 Sets new values. More...
 
 edit ($numbers, array $newValues)
 Alias of static::set(). More...
 
 unsetValue ($numbers, $valueName)
 Unsets a value of a specified item at the current menu. More...
 
 add (array $values)
 Adds a new item at the current menu. More...
 
 move ($numbers, $destination)
 Moves items at the current menu before a certain other item. More...
 
 count ($mode=COUNT_NORMAL, Query $query=null)
 Counts items at the current menu. More...
 
 getAll (array $args=array(), Query $query=null)
 Gets all items in the current menu. More...
 
 filePutContents ($filename, $data, $overwrite=false)
 Puts a file on RouterOS's file system. More...
 
 fileGetContents ($filename, $tmpScriptName=null)
 Gets the contents of a specified file. More...
 

Static Public Member Functions

static parseValue ($value)
 Parses a value from a RouterOS scripting context. More...
 
static prepareScript ($source, array $params=array())
 Prepares a script. More...
 
static appendScript ($stream, $source, array $params=array())
 Appends a script. More...
 
static escapeValue ($value)
 Escapes a value for a RouterOS scripting context. More...
 
static escapeString ($value)
 Escapes a string for a RouterOS scripting context. More...
 

Protected Member Functions

 doBulk ($what, array $args=array())
 Performs an action on a bulk of items at the current menu. More...
 

Protected Attributes

 $client
 
 $menu = '/'
 
 $idCache = null
 

Detailed Description

Definition at line 55 of file Util.php.

Constructor & Destructor Documentation

__construct ( Client  $client)

Creates a new Util instance.

Wraps around a connection to provide convinience methods.

Parameters
Client$clientThe connection to wrap around.

Definition at line 411 of file Util.php.

Member Function Documentation

add ( array  $values)

Adds a new item at the current menu.

Parameters
array$valuesAccepts one or more items to add to the current menu. The data about each item is specified as an array with the names of each property as an array key, and the value as an array value. Flags (properties with a value "true" that is interpreted as equivalent of "yes" from CLI) can also be specified with a numeric index as the array key, and the name of the flag as the array value.
array$... Additional items.
Returns
string A comma separated list of the new items' IDs. If a particular item was not added, this will be indicated by an empty string in its spot on the list. e.g. "*1D,,*1E" means that you supplied three items to be added, of which the second one was not added for some reason.

Definition at line 826 of file Util.php.

static appendScript (   $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$paramsAn 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(). Processing starts from the current position to the end of the stream, and the stream's pointer stays at the end after reading is done.
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 253 of file Util.php.

clearIdCache ( )

Clears the ID cache.

Normally, the ID cache improves performance when targeting items by a number. If you're using both Util's methods and other means (e.g. Client or Util::exec()) to add/move/remove items, the cache may end up being out of date. By calling this method right before targeting an item with a number, you can ensure number accuracy.

Note that Util's static::move() and static::remove() methods automatically clear the cache before returning, while static::add() adds the new item's ID to the cache as the next number. A change in the menu also clears the cache.

Note also that the cache is being rebuilt unconditionally every time you use static::find() with a callback.

Returns
$this The Util object itself.

Definition at line 526 of file Util.php.

count (   $mode = COUNT_NORMAL,
Query  $query = null 
)

Counts items at the current menu.

Counts items at the current menu. This executes a dedicated command ("print" with a "count-only" argument) on RouterOS, which is why only queries are allowed as a criteria, in contrast with static::find(), where numbers and callbacks are allowed also.

Parameters
int$modeThe counter mode. Currently ignored, but present for compatiblity with PHP 5.6+.
Query$queryA query to filter items by. Without it, all items are included in the count.
Returns
int The number of items, or -1 on failure (e.g. if the current menu does not have a "print" command or items to be counted).

Definition at line 899 of file Util.php.

disable ( )

Disables all items at the current menu matching certain criteria.

Zero or more arguments can be specified, each being a criteria. If zero arguments are specified, disables all items. See static::find() for a description of what criteria are accepted.

Returns
ResponseCollection Returns the response collection, allowing you to inspect errors, if any.

Definition at line 709 of file Util.php.

doBulk (   $what,
array  $args = array() 
)
protected

Performs an action on a bulk of items at the current menu.

Parameters
string$whatWhat action to perform.
array$argsZero or more arguments can be specified, each being a criteria. If zero arguments are specified, removes all items. See static::find() for a description of what criteria are accepted.
Returns
ResponseCollection Returns the response collection, allowing you to inspect errors, if any.

Definition at line 1080 of file Util.php.

edit (   $numbers,
array  $newValues 
)

Alias of static::set().

Parameters
mixed$numbersTargeted items. Can be any criteria accepted by static::find().
array$newValuesAn array with the names of each changed property as an array key, and the new value as an array value. Flags (properties with a value "true" that is interpreted as equivalent of "yes" from CLI) can also be specified with a numeric index as the array key, and the name of the flag as the array value.
Returns
ResponseCollection Returns the response collection, allowing you to inspect errors, if any.

Definition at line 780 of file Util.php.

enable ( )

Enables all items at the current menu matching certain criteria.

Zero or more arguments can be specified, each being a criteria. If zero arguments are specified, enables all items. See static::find() for a description of what criteria are accepted.

Returns
ResponseCollection returns the response collection, allowing you to inspect errors, if any.

Definition at line 693 of file Util.php.

static escapeString (   $value)
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.

Parameters
string$valueValue to be escaped.
Returns
string The escaped value.

Definition at line 370 of file Util.php.

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.

DateTime and DateInterval objects will be casted to RouterOS' "time" type. A DateTime object will be converted to a time relative to the UNIX epoch time. Note that if a DateInterval does not have the "days" property ("a" in formatting), then its months and years will be ignored, because they can't be unambigiously converted to a "time" value.

Unrecognized types (i.e. resources and other objects) are casted to strings.

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 303 of file Util.php.

exec (   $source,
array  $params = array(),
  $policy = null,
  $name = null 
)

Executes a RouterOS script.

Executes a RouterOS script, written as a string or a stream. Note that in cases of errors, the line numbers will be off, because the script is executed at the current menu as context, with the specified variables pre declared. This is achieved by prepending 1+count($params) lines before your actual script.

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$paramsAn 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 processed with static::escapeString(). 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. Note that the script's (generated) name is always added as the variable "_", which will be inadvertently lost if you overwrite it from here.
string$policyAllows you to specify a policy the script must follow. Has the same format as in terminal. If left NULL, the script has no restrictions beyond those imposed by the username.
string$nameThe script is executed after being saved in "/system script" under a random name (prefixed with the computer's name), and is removed after execution. To eliminate any possibility of name clashes, you can specify your own name.
Returns
ResponseCollection Returns the response collection of the run, allowing you to inspect errors, if any. If the script was not added successfully before execution, the ResponseCollection from the add attempt is going to be returned.

Definition at line 498 of file Util.php.

fileGetContents (   $filename,
  $tmpScriptName = null 
)

Gets the contents of a specified file.

Parameters
string$filenameThe name of the file to get the contents of.
string$tmpScriptNameIn order to get the file's contents, a script is created at "/system script" with a random name, the source of which is then overwriten with the file's contents, and finally retrieved. To eliminate any possibility of name clashes, you can specify your own name for the script.
Returns
string|resource|false The contents of the file as a string or as new PHP temp stream if the underliying Client::isStreamingResponses() is set to TRUE. FALSE is returned if there is no such file.

Definition at line 1049 of file Util.php.

filePutContents (   $filename,
  $data,
  $overwrite = false 
)

Puts a file on RouterOS's file system.

Puts a file on RouterOS's file system, regardless of the current menu. Note that this is a VERY VERY VERY time consuming method - it takes a minimum of a little over 4 seconds, most of which are in sleep. It waits 2 seconds after a file is first created (required to actually start writing to the file), and another 2 seconds after its contents is written (performed in order to verify success afterwards). Similarly for removal (when $data is NULL) - there are two seconds in sleep, used to verify the file was really deleted.

If you want an efficient way of transferring files, use (T)FTP. If you want an efficient way of removing files, use static::setMenu() to move to the "/file" menu, and call static::remove() without performing verification afterwards.

Parameters
string$filenameThe filename to write data in.
string | resource | null$dataThe data the file is going to have as a string or a seekable stream. Setting the value to NULL removes a file of this name. If a seekable stream is provided, it is sent from its current posistion to its end, and the pointer is seeked back to its current position after sending. Non seekable streams, as well as all other types, are casted to a string.
bool$overwriteWhether to overwrite the file if it exists.
Returns
bool TRUE on success, FALSE on failure.

Definition at line 981 of file Util.php.

find ( )

Finds the IDs of items at the current menu.

Finds the IDs of items based on specified criteria, and returns them as a comma separated string, ready for insertion at a "numbers" argument.

Accepts zero or more criteria as arguments. If zero arguments are specified, returns all items' IDs. The value of each criteria can be a number (just as in Winbox), a literal ID to be included, a Query object, or a callback. If a callback is specified, it is called for each item, with the item as an argument. If it returns a true value, the item's ID is included in the result. Every other value is casted to a string. A string is treated as a comma separated values of IDs, numbers or callback names. Non-existent callback names are instead placed in the result, which may be useful in menus that accept identifiers other than IDs, but note that it can cause errors on other menus.

Returns
string A comma separated list of all items matching the specified criteria.

Definition at line 552 of file Util.php.

get (   $number,
  $valueName 
)

Gets a value of a specified item at the current menu.

Parameters
int | string | null$numberA number identifying the item you're targeting. Can also be an ID or (in some menus) name. For menus where there are no items (e.g. "/system identity"), you can specify NULL.
string$valueNameThe name of the value you want to get.
Returns
string|null|bool The value of the specified property. If the property is not set, NULL will be returned. FALSE on failure (e.g. no such item, invalid property, etc.).

Definition at line 636 of file Util.php.

getAll ( array  $args = array(),
Query  $query = null 
)

Gets all items in the current menu.

Gets all items in the current menu, using a print request.

Parameters
array<int|string,string>$args Additional arguments to pass to the request. Each array key is the name of the argument, and each array value is the value of the argument to be passed. Arguments without a value (i.e. empty arguments) can also be specified using a numeric key, and the name of the argument as the array value.
Query | null$queryA query to filter items by. NULL to get all items.
Returns
ResponseCollection|false A response collection with all Response::TYPE_DATA responses. The collection will be empty when there are no matching items. FALSE on failure.

Definition at line 932 of file Util.php.

getMenu ( )

Gets the current menu.

Returns
string The current menu.

Definition at line 421 of file Util.php.

move (   $numbers,
  $destination 
)

Moves items at the current menu before a certain other item.

Moves items before a certain other item. Note that the "move" command is not available on all menus. As a rule of thumb, if the order of items in a menu is irrelevant to their interpretation, there won't be a move command on that menu. If in doubt, check from a terminal.

Parameters
mixed$numbersTargeted items. Can be any criteria accepted by static::find().
mixed$destinationitem before which the targeted items will be moved to. Can be any criteria accepted by static::find(). If multiple items match the criteria, the targeted items will move above the first match.
Returns
ResponseCollection Returns the response collection, allowing you to inspect errors, if any.

Definition at line 870 of file Util.php.

static parseValue (   $value)
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(). hat 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.
Returns
mixed Depending on RouterOS type detected:
  • "nil" or "nothing" - NULL.
  • "number" - int or double for large values.
  • "bool" - a boolean.
  • "time" - a DateInterval object.
  • "array" - an array, with the values processed recursively.
  • "str" - a string.
  • Unrecognized type - treated as an unquoted string.

Definition at line 97 of file Util.php.

static prepareScript (   $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 static::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$paramsAn 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(). Processing starts from the current position to the end of the stream, and the stream's pointer stays at the end after reading is done.
Returns
resource A new PHP temporary stream with the script as contents, with the pointer back at the start.
See also
static::appendScript()

Definition at line 218 of file Util.php.

remove ( )

Removes all items at the current menu matching certain criteria.

Zero or more arguments can be specified, each being a criteria. If zero arguments are specified, removes all items. See static::find() for a description of what criteria are accepted.

Returns
ResponseCollection Returns the response collection, allowing you to inspect errors, if any.

Definition at line 725 of file Util.php.

set (   $numbers,
array  $newValues 
)

Sets new values.

Sets new values on certain properties on all items at the current menu which match certain criteria.

Parameters
mixed$numbersTargeted items. Can be any criteria accepted by static::find() or NULL in case the menu is one without items (e.g. "/system identity").
array$newValuesAn array with the names of each property to set as an array key, and the new value as an array value. Flags (properties with a value "true" that is interpreted as equivalent of "yes" from CLI) can also be specified with a numeric index as the array key, and the name of the flag as the array value.
Returns
ResponseCollection Returns the response collection, allowing you to inspect errors, if any.

Definition at line 750 of file Util.php.

setMenu (   $newMenu)

Sets the current menu.

Sets the current menu.

Parameters
string$newMenuThe menu to change to. Can be specified with API or CLI syntax and can be either absolute or relative. If relative, it's relative to the current menu, which by default is the root.
Returns
$this The object itself. If an empty string is given for a new menu, no change is performed, but the ID cache is cleared anyway.
See also
static::clearIdCache()

Definition at line 441 of file Util.php.

unsetValue (   $numbers,
  $valueName 
)

Unsets a value of a specified item at the current menu.

Equivalent of scripting's "unset" command. The "Value" part in the method name is added because "unset" is a language construct, and thus a reserved word.

Parameters
mixed$numbersTargeted items. Can be any criteria accepted by static::find().
string$valueNameThe name of the value you want to unset.
Returns
ResponseCollection Returns the response collection, allowing you to inspect errors, if any.

Definition at line 799 of file Util.php.

Field Documentation

$client
protected

Definition at line 60 of file Util.php.

$idCache = null
protected

Definition at line 72 of file Util.php.

$menu = '/'
protected

Definition at line 65 of file Util.php.


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