diff --git a/examples/callback-and-loop.php b/examples/Client/callback-and-loop.php similarity index 92% rename from examples/callback-and-loop.php rename to examples/Client/callback-and-loop.php index 25f8d14..95cc50f 100644 --- a/examples/callback-and-loop.php +++ b/examples/Client/callback-and-loop.php @@ -3,7 +3,7 @@ require_once 'PEAR2/Autoload.php'; -$client = new RouterOS\Client('192.168.0.1', 'admin'); +$client = new RouterOS\Client('192.168.88.1', 'admin', 'password'); //Custom function, defined specifically for the example $responseHandler = function ($response) { diff --git a/examples/loop-and-extract.php b/examples/Client/loop-and-extract.php similarity index 92% rename from examples/loop-and-extract.php rename to examples/Client/loop-and-extract.php index b58b4bc..9c46e4c 100644 --- a/examples/loop-and-extract.php +++ b/examples/Client/loop-and-extract.php @@ -3,7 +3,7 @@ require_once 'PEAR2/Autoload.php'; -$client = new RouterOS\Client('192.168.0.1', 'admin'); +$client = new RouterOS\Client('192.168.88.1', 'admin', 'password'); $addRequest = new RouterOS\Request('/ip/arp/add'); diff --git a/examples/send-and-complete.php b/examples/Client/send-and-complete.php similarity index 92% rename from examples/send-and-complete.php rename to examples/Client/send-and-complete.php index 41e2cf7..8253b0e 100644 --- a/examples/send-and-complete.php +++ b/examples/Client/send-and-complete.php @@ -3,7 +3,7 @@ require_once 'PEAR2/Autoload.php'; -$client = new RouterOS\Client('192.168.0.1', 'admin'); +$client = new RouterOS\Client('192.168.88.1', 'admin', 'password'); $addRequest = new RouterOS\Request('/ip/arp/add'); diff --git a/examples/send-and-forget.php b/examples/Client/send-and-forget.php similarity index 87% rename from examples/send-and-forget.php rename to examples/Client/send-and-forget.php index 2ba3924..9880698 100644 --- a/examples/send-and-forget.php +++ b/examples/Client/send-and-forget.php @@ -3,7 +3,7 @@ require_once 'PEAR2/Autoload.php'; -$client = new RouterOS\Client('192.168.0.1', 'admin'); +$client = new RouterOS\Client('192.168.88.1', 'admin', 'password'); $addRequest = new RouterOS\Request('/ip/arp/add'); diff --git a/examples/sync-request-arguments.php b/examples/Client/sync-request-arguments.php similarity index 90% rename from examples/sync-request-arguments.php rename to examples/Client/sync-request-arguments.php index a445902..affa54b 100644 --- a/examples/sync-request-arguments.php +++ b/examples/Client/sync-request-arguments.php @@ -3,7 +3,7 @@ require_once 'PEAR2/Autoload.php'; -$client = new RouterOS\Client('192.168.0.1', 'admin'); +$client = new RouterOS\Client('192.168.88.1', 'admin', 'password'); $addRequest = new RouterOS\Request('/ip/arp/add'); diff --git a/examples/sync-request-simple.php b/examples/Client/sync-request-simple.php similarity index 87% rename from examples/sync-request-simple.php rename to examples/Client/sync-request-simple.php index 3a57313..ec0186a 100644 --- a/examples/sync-request-simple.php +++ b/examples/Client/sync-request-simple.php @@ -3,7 +3,7 @@ require_once 'PEAR2/Autoload.php'; -$client = new RouterOS\Client('192.168.0.1', 'admin'); +$client = new RouterOS\Client('192.168.88.1', 'admin', 'password'); $responses = $client->sendSync(new RouterOS\Request('/ip/arp/print')); diff --git a/examples/Util/add.php b/examples/Util/add.php new file mode 100644 index 0000000..46f4e34 --- /dev/null +++ b/examples/Util/add.php @@ -0,0 +1,16 @@ +setMenu('/ip arp'); +$util->add( + array( + 'address' => '192.168.88.100', + 'mac-address' => '00:00:00:00:00:01' + ), + array( + 'address' => '192.168.88.101', + 'mac-address' => '00:00:00:00:00:02' + ) +); \ No newline at end of file diff --git a/examples/Util/count.php b/examples/Util/count.php new file mode 100644 index 0000000..1f64209 --- /dev/null +++ b/examples/Util/count.php @@ -0,0 +1,15 @@ +setMenu('/ip arp'); + +//With function +echo count($util) . "\n"; + +//With method call +echo $util->count() . "\n"; + +//Count only disabled ARP items +echo $util->count(COUNT_NORMAL, RouterOS\Query::where('disabled', 'true')) . "\n"; diff --git a/examples/Util/enable_disable_remove.php b/examples/Util/enable_disable_remove.php new file mode 100644 index 0000000..177c703 --- /dev/null +++ b/examples/Util/enable_disable_remove.php @@ -0,0 +1,9 @@ +setMenu('/ip arp'); +$util->remove(0); +$util->disable(Query::where('comment', 'DISABLE ME')); +$util->enable(1); \ No newline at end of file diff --git a/examples/Util/exec-basic.php b/examples/Util/exec-basic.php new file mode 100644 index 0000000..2a4af72 --- /dev/null +++ b/examples/Util/exec-basic.php @@ -0,0 +1,14 @@ +setMenu('/ip arp'); + +$util->exec(' +add address=192.168.88.100 mac-address=00:00:00:00:00:01 comment=customer_1 +add address=192.168.88.101 mac-address=00:00:00:00:00:02 comment=customer_2 +/tool +fetch url="http://example.com/?name=customer_1" +fetch url="http://example.com/?name=customer_2" +'); \ No newline at end of file diff --git a/examples/Util/exec-params.php b/examples/Util/exec-params.php new file mode 100644 index 0000000..98014fe --- /dev/null +++ b/examples/Util/exec-params.php @@ -0,0 +1,28 @@ +setMenu('/ip arp'); + +$source = ' +add address="192.168.88.$ip" mac-address="00:00:00:00:00:$mac" comment=$name +/tool +fetch url="http://example.com/?name=$name" +'; +$util->exec( + $source, + array( + 'ip' => 100, + 'mac' => '01', + 'name' => 'customer_1' + ) +); +$util->exec( + $source, + array( + 'ip' => 101, + 'mac' => '02', + 'name' => 'customer_2' + ) +); \ No newline at end of file diff --git a/examples/Util/exec-policy.php b/examples/Util/exec-policy.php new file mode 100644 index 0000000..b09f397 --- /dev/null +++ b/examples/Util/exec-policy.php @@ -0,0 +1,23 @@ +setMenu('/tool'); + +$url = $_GET['url'];//assume $_GET['url'] equals something akin to "http://example.com/geoip.rsc?filter=all"... + +$source = ' +fetch url=$db keep-result=yes dst-path=$filename +# Give the script time to be written onto disk +:delay 2 +/import file=$filename +'; +$util->exec( + $source, + array( + 'db' => $url, + 'filename' => pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_FILENAME) //... then this would be equal to "geoip.rsc" + ), + 'read,write' +); \ No newline at end of file diff --git a/examples/Util/file-get.php b/examples/Util/file-get.php new file mode 100644 index 0000000..ce2021f --- /dev/null +++ b/examples/Util/file-get.php @@ -0,0 +1,8 @@ +fileGetContents($filename)); \ No newline at end of file diff --git a/examples/Util/file-put.php b/examples/Util/file-put.php new file mode 100644 index 0000000..2ec5df3 --- /dev/null +++ b/examples/Util/file-put.php @@ -0,0 +1,8 @@ +filePutContents($filename, file_get_contents($filename)); \ No newline at end of file diff --git a/examples/Util/find-callback.php b/examples/Util/find-callback.php new file mode 100644 index 0000000..2d15329 --- /dev/null +++ b/examples/Util/find-callback.php @@ -0,0 +1,9 @@ +changeMenu('/ip arp'); +echo $util->find(function ($response) { + return preg_match('/^\d\d/', $response->getArgument('comment'));//Matches any entry who's comment starts with two digits +}); \ No newline at end of file diff --git a/examples/Util/find-numbers.php b/examples/Util/find-numbers.php new file mode 100644 index 0000000..95f742e --- /dev/null +++ b/examples/Util/find-numbers.php @@ -0,0 +1,7 @@ +setMenu('/ip arp'); +echo $util->find(0, 1);//Outputs something similar to "*4de,*16a", since we targeted two entries - the one in position 0 and position 1. \ No newline at end of file diff --git a/examples/Util/get-null.php b/examples/Util/get-null.php new file mode 100644 index 0000000..03bb718 --- /dev/null +++ b/examples/Util/get-null.php @@ -0,0 +1,7 @@ +setMenu('/system identity'); +echo $util->get(null, 'name');//echoes "MikroTik", assuming you've never altered your router's identity. diff --git a/examples/Util/get-number.php b/examples/Util/get-number.php new file mode 100644 index 0000000..a341d40 --- /dev/null +++ b/examples/Util/get-number.php @@ -0,0 +1,7 @@ +setMenu('/ip arp'); +echo $util->get(0, 'address');//echoes "192.168.88.1", assuming we had the previous example executed under an empty ARP list diff --git a/examples/Util/getAll.php b/examples/Util/getAll.php new file mode 100644 index 0000000..6eb4d8e --- /dev/null +++ b/examples/Util/getAll.php @@ -0,0 +1,12 @@ +setMenu('/ip arp'); + +foreach($util->getAll() as $item) { + echo 'IP: ', $item->getProperty('address'), + ' MAC: ', $item->getProperty('mac-address'), + "\n"; +} \ No newline at end of file diff --git a/examples/Util/move.php b/examples/Util/move.php new file mode 100644 index 0000000..6af0192 --- /dev/null +++ b/examples/Util/move.php @@ -0,0 +1,8 @@ +setMenu('/queue simple'); +$util->move(2, 0);//Place the queue at position 2 above the queue at position 0 +$util->move($util->find(3,4), 0);//Place the queues at positions 3 and 4 above the queue at position 0 (the same one that a line ago was at position 2) diff --git a/examples/Util/parseValue.php b/examples/Util/parseValue.php new file mode 100644 index 0000000..ff9e550 --- /dev/null +++ b/examples/Util/parseValue.php @@ -0,0 +1,13 @@ +setMenu('/system resource'); +$uptime = RouterOS\Util::parseValue($util->get(null, 'uptime')); + +$now = new DateTime; + +//Will output something akin to 'The router has been in operation since Sunday, 18 Aug 2013 14:03:01' +echo 'The router has been in operation since ' . $now->sub($uptime)->format(DateTime::COOKIE); diff --git a/examples/Util/set-multiple.php b/examples/Util/set-multiple.php new file mode 100644 index 0000000..7fd4f49 --- /dev/null +++ b/examples/Util/set-multiple.php @@ -0,0 +1,16 @@ +setMenu('/ip arp'); +$util->set( + $util->find( + function ($response) { + return preg_match('/^\d\d/', $response->getArgument('comment'));//Matches any entry who's comment starts with two digits + } + ), + array( + 'address' => '192.168.88.103' + ) +); \ No newline at end of file diff --git a/examples/Util/set-single.php b/examples/Util/set-single.php new file mode 100644 index 0000000..6dd184d --- /dev/null +++ b/examples/Util/set-single.php @@ -0,0 +1,9 @@ +setMenu('/ip arp'); +$util->set(0, array( + 'address' => '192.168.88.103' +)); \ No newline at end of file diff --git a/scripts/roscon.php b/scripts/roscon.php index 505992c..22af4e4 100644 --- a/scripts/roscon.php +++ b/scripts/roscon.php @@ -314,7 +314,7 @@ following command from a terminal: ``` /ip firewall filter - add place-before=[find where chain="input" && action="drop"] \ + add place-before=[:pick [find where chain="input"] 0] \ chain="input" action="accept" \ dst-port=[/ip service get "api" "port"] ``` diff --git a/tests/Client/Safe.php b/tests/Client/Safe.php index cb0c493..4cbcc75 100644 --- a/tests/Client/Safe.php +++ b/tests/Client/Safe.php @@ -1140,6 +1140,9 @@ public function testSendSyncWithQueryBetween() */ public function testResponseCollectionWordCount() { + $this->markTestIncomplete( + 'PHP reverted Countable::count($mode) support before the final 5.6' + ); $this->assertEquals( 3/*!re + .id*/+ 2/*!done*/, count( diff --git a/tests/Util/Unsafe.php b/tests/Util/Unsafe.php index a91fe79..5b319aa 100644 --- a/tests/Util/Unsafe.php +++ b/tests/Util/Unsafe.php @@ -262,6 +262,49 @@ public function testSetAndEdit() $this->util->remove($id); } + /** + * @depends testRemove + * + * @return void + */ + public function testComment() + { + $this->util->setMenu('/queue/simple'); + $id = $this->util->add( + array( + 'name' => TEST_QUEUE_NAME, + 'target' => HOSTNAME_SILENT . '/32' + ) + ); + + $printRequest = new Request( + '/queue/simple/print', + Query::where('.id', $id) + ); + + $responses = $this->client->sendSync($printRequest); + $this->assertSame( + HOSTNAME_SILENT . '/32', + $responses->getProperty('target') + ); + $this->assertNull( + $responses->getProperty('comment') + ); + $this->util->comment(TEST_QUEUE_NAME, 'test comment'); + + $responses = $this->client->sendSync($printRequest); + $this->assertSame( + HOSTNAME_SILENT . '/32', + $responses->getProperty('target') + ); + $this->assertSame( + 'test comment', + $responses->getProperty('comment') + ); + + $this->util->remove($id); + } + /** * @depends testAdd * @depends testRemove