|
| __construct (Client $client) |
| Creates a new Util instance. More...
|
|
| getMenu () |
| Gets the current menu. More...
|
|
| setMenu ($newMenu) |
| Sets the current menu. More...
|
|
| newRequest ( $command, array $args=array(), Query $query=null, $tag=null) |
| Creates a Request object. More...
|
|
| exec ( $source, array $params=array(), $policy=null, $name=null) |
| Executes a RouterOS script. More...
|
|
| clearIdCache () |
| Clears the ID cache. More...
|
|
| getCurrentTime () |
| Gets the current time on the router. More...
|
|
| find () |
| Finds the IDs of items at the current menu. More...
|
|
| get ($number, $valueName=null) |
| 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...
|
|
| comment ($numbers, $comment) |
| Comments items. More...
|
|
| set ($numbers, array $newValues) |
| Sets new values. More...
|
|
| edit ($numbers, $valueName, $newValue) |
| 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=null) |
| Moves items at the current menu before a certain other item. More...
|
|
| count (Query $query=null, $from=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...
|
|
Definition at line 61 of file Util.php.
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 350 of file Util.php.
comment |
( |
|
$numbers, |
|
|
|
$comment |
|
) |
| |
Comments items.
Sets new comments on all items at the current menu which match certain criteria, using the "comment" command.
Note that not all menus have a "comment" command. Most notably, those are menus without items in them (e.g. "/system identity"), and menus with fixed items (e.g. "/ip service").
- Parameters
-
mixed | $numbers | Targeted items. Can be any criteria accepted by static::find(). |
string | resource | $comment | The new comment to set on the item as a string or a seekable stream. If a seekable stream is provided, it is sent from its current position 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. |
- Returns
- ResponseCollection Returns the response collection, allowing you to inspect the output. Current RouterOS versions don't return anything useful, but if future ones do, you can read it right away.
- Exceptions
-
RouterErrorException | When the router returns one or more errors. |
Definition at line 699 of file Util.php.
count |
( |
Query |
$query = null , |
|
|
|
$from = 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
-
Query | null | $query | A query to filter items by. Without it, all items are included in the count. |
string | resource | null | $from | A comma separated list of item IDs. Any items in the set that still exist at the time of couting are included in the final tally. Note that the $query filters this set further (i.e. the item must be in the list AND match the $query). Leaving the value to NULL means all matching items at the current menu 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 949 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 | $source | The 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). 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 | null | $policy | Allows 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 | null | $name | The script is executed after being saved in "/system script" and is removed after execution. If this argument is left NULL, a random string, prefixed with the computer's name, is generated and used as the script's name. To eliminate any possibility of name clashes, you can specify your own name instead. |
- Returns
- ResponseCollection The responses of all requests involved, i.e. the add, the run and the remove.
- Exceptions
-
RouterErrorException | When there is an error in any step of the way. The reponses include all successful commands prior to the error as well. If the error occurs during the run, there will also be a remove attempt, and the results will include its results as well. |
Definition at line 259 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 | $filename | The filename to write data in. |
string | resource | null | $data | The 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 position 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 | $overwrite | Whether to overwrite the file if it exists. |
- Returns
- bool TRUE on success, FALSE on failure.
Definition at line 1067 of file Util.php.
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 427 of file Util.php.
Gets the current time on the router.
Gets the current time on the router, regardless of the current menu.
If the timezone is one known to both RouterOS and PHP, it will be used as the timezone identifier. Otherwise (e.g. "manual"), the current GMT offset will be used as a timezone, without any DST awareness.
- Returns
- DateTime The current time of the router, as a DateTime object.
Definition at line 367 of file Util.php.
move |
( |
|
$numbers, |
|
|
|
$destination = null |
|
) |
| |
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 | $numbers | Targeted items. Can be any criteria accepted by static::find(). |
mixed | $destination | Item 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. If NULL is given (or this argument is omitted), the targeted items will be moved to the bottom of the menu. |
- Returns
- ResponseCollection Returns the response collection, allowing you to inspect the output. Current RouterOS versions don't return anything useful, but if future ones do, you can read it right away.
- Exceptions
-
RouterErrorException | When the router returns one or more errors. |
Definition at line 905 of file Util.php.