@@ -89,6 +89,10 @@ public static function escapeValue($value)
8989 $ value = $ value ? 'true ' : 'false ' ;
9090 break ;
9191 case 'array ' :
92+ if (0 === count ($ value )) {
93+ $ value = '{} ' ;
94+ break ;
95+ }
9296 $ result = '' ;
9397 foreach ($ value as $ val ) {
9498 $ result .= '; ' . static ::escapeValue ($ val );
@@ -447,8 +451,9 @@ public function set($numbers, array $newValues)
447451 foreach ($ newValues as $ name => $ value ) {
448452 $ setRequest ->setArgument ($ name , $ value );
449453 }
450- $ setRequest ->setArgument ('numbers ' , $ this ->find ($ numbers ));
451- return $ this ->client ->sendSync ($ setRequest );
454+ return $ this ->client ->sendSync (
455+ $ setRequest ->setArgument ('numbers ' , $ this ->find ($ numbers ))
456+ );
452457 }
453458
454459 /**
@@ -467,6 +472,28 @@ public function edit($numbers, array $newValues)
467472 return $ this ->set ($ numbers , $ newValues );
468473 }
469474
475+ /**
476+ * Unsets a value of a specified entry at the current menu.
477+ *
478+ * Equivalent of scripting's "unset" command. The "Value" part in the method
479+ * name is added because "unset" is a language construct, and thus a
480+ * reserved word.
481+ *
482+ * @param mixed $numbers Targeted entries. Can be any criteria accepted
483+ * by {@link find()}.
484+ * @param string $value_name The name of the value you want to unset.
485+ *
486+ * @return ResponseCollection
487+ */
488+ public function unsetValue ($ numbers , $ value_name )
489+ {
490+ $ unsetRequest = new Request ($ this ->menu . '/unset ' );
491+ return $ this ->client ->sendSync (
492+ $ unsetRequest ->setArgument ('numbers ' , $ this ->find ($ numbers ))
493+ ->setArgument ('value-name ' , $ value_name )
494+ );
495+ }
496+
470497 /**
471498 * Adds a new entry at the current menu.
472499 *
@@ -537,58 +564,59 @@ public function move($numbers, $destination)
537564 public function filePutContents ($ filename , $ data , $ overwrite = false )
538565 {
539566 $ printRequest = new Request (
540- '/file/print .proplist="size " ' ,
567+ '/file/print .proplist="" ' ,
541568 Query::where ('name ' , $ filename )
542569 );
543570 if (!$ overwrite && count ($ this ->client ->sendSync ($ printRequest )) > 1 ) {
544571 return false ;
545572 }
546- $ source = <<<'NEWDOC'
547- /file
548- print file=$filename where name=""
549- NEWDOC;
550- $ this ->exec (
551- $ source ,
552- array ('filename ' => $ filename )
573+ $ result = $ this ->client ->sendSync (
574+ $ printRequest ->setArgument ('file ' , $ filename )
553575 );
576+ if (count ($ result ->getAllOfType (Response::TYPE_ERROR )) > 0 ) {
577+ return false ;
578+ }
579+ //Required for RouterOS to write the initial file.
554580 sleep (2 );
555- $ source = <<<'NEWDOC'
556- /file
557- set numbers=$filename contents=""
558- set numbers=$filename contents=$data
559- NEWDOC;
560- $ this ->exec (
561- $ source ,
562- array ('filename ' => $ filename , 'data ' => $ data )
563- );
581+ $ setRequest = new Request ('/file/set contents="" ' );
582+ $ setRequest ->setArgument ('numbers ' , $ filename );
583+ $ this ->client ->sendSync ($ setRequest );
584+ $ this ->client ->sendSync ($ setRequest ->setArgument ('contents ' , $ data ));
585+ //Required for RouterOS to write the file's new contents.
564586 sleep (2 );
565587 return strlen ($ data ) == $ this ->client ->sendSync (
566- $ printRequest
588+ $ printRequest ->setArgument ('file ' , null )
589+ ->setArgument ('.proplist ' , 'size ' )
567590 )->getArgument ('size ' );
568591 }
569592
570593 /**
571594 * Gets the contents of a specified file.
572595 *
573- * @param string $filename The name of the file to get contents of.
596+ * @param string $filename The name of the file to get the contents of.
597+ * @param string $tmpScriptName In order to get the file's contents, a
598+ * script is created at "/system script" with a random name, the
599+ * source of which is then overwriten with the file's contents, and
600+ * finally retrieved. To eliminate any possibility of name clashes, you
601+ * can specify your own name for the script.
574602 *
575603 * @return string|bool The contents of the file or FALSE if there is no such
576604 * file.
577605 */
578- public function fileGetContents ($ filename )
606+ public function fileGetContents ($ filename, $ tmpScriptName = null )
579607 {
580608 $ checkRequest = new Request (
581609 '/file print ' ,
582610 Query::where ('name ' , $ filename )
583611 );
584- if (1 === $ this ->client ->sendSync ($ checkRequest )) {
612+ if (1 === count ( $ this ->client ->sendSync ($ checkRequest) )) {
585613 return false ;
586614 }
587615 $ name = $ this ->_exec (
588616 '/system script set $"_" source=[/file get $filename contents] ' ,
589617 array ('filename ' => $ filename ),
590618 null ,
591- null ,
619+ $ tmpScriptName ,
592620 true
593621 );
594622 $ printRequest = new Request (
0 commit comments