/** * Cancels a request. * * Cancels an active request. Using this function in favor of a plain call * to the "/cancel" command is highly reccomended, as it also updates the * counter of pending requests properly. Note that canceling a request also * removes any responses for it that were not previously extracted with * {@link extractNewResponses()}. * * @param string $tag Tag of the request to cancel. Setting NULL will cancel * all requests. * * @return self|Client The client object. * @see sendAsync() * @see close() */ public function cancelRequest($tag = null) { $cancelRequest = new Request('/cancel'); $hasTag = !('' == $tag); $hasReg = null !== $this->registry; if ($hasReg && !$hasTag) { $tags = array_merge(array_keys($this->responseBuffer), array_keys($this->callbacks)); $this->registry->setTaglessMode(true); foreach ($tags as $t) { $cancelRequest->setArgument('tag', $this->registry->getOwnershipTag() . $t); $this->sendSync($cancelRequest); } $this->registry->setTaglessMode(false); } else { if ($hasTag) { if ($this->isRequestActive($tag)) { if ($hasReg) { $this->registry->setTaglessMode(true); $cancelRequest->setArgument('tag', $this->registry->getOwnershipTag() . $tag); } else { $cancelRequest->setArgument('tag', $tag); } } else { throw new DataFlowException('No such request. Canceling aborted.', DataFlowException::CODE_CANCEL_FAIL); } } $this->sendSync($cancelRequest); if ($hasReg) { $this->registry->setTaglessMode(false); } } if ($hasTag) { if ($this->isRequestActive($tag, self::FILTER_BUFFER)) { $this->responseBuffer[$tag] = $this->completeRequest($tag); } else { $this->completeRequest($tag); } } else { $this->loop(); } return $this; }
/** * Executes a RouterOS script. * * Same as the public equivalent, with the addition of allowing you to get * the contents of the script post execution, instead of removing it. * * @param string $source A script to execute. * @param array $params An array of local variables to make available in * the script. Variable names are array keys, and variable values are * array values. Note that the script's (generated) name is always added * as the variable "_", which you can overwrite from here. * Native PHP types will be converted to their RouterOS equivalents. * DateTime and DateInterval objects will be casted to RouterOS' "time" * type. Other types are casted to strings. * @param string $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. * @param string $name The 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. * @param bool $get Whether to keep the script after execution. * * @return ResponseCollection|string If the script was not added * successfully before execution, the ResponseCollection from the add * attempt is going to be returned. Otherwise, the (generated) name of * the script. */ private function _exec($source, array $params = array(), $policy = null, $name = null, $get = false) { $request = new Request('/system/script/add'); if (null === $name) { $name = uniqid(gethostname(), true); } $request->setArgument('name', $name); $request->setArgument('policy', $policy); $finalSource = '/' . str_replace('/', ' ', substr($this->menu, 1)) . "\n"; $params += array('_' => $name); foreach ($params as $pname => $pvalue) { $pname = static::escapeString($pname); $pvalue = static::escapeValue($pvalue); $finalSource .= ":local \"{$pname}\" {$pvalue};\n"; } $finalSource .= $source . "\n"; $request->setArgument('source', $finalSource); $result = $this->client->sendSync($request); if (0 === count($result->getAllOfType(Response::TYPE_ERROR))) { $request = new Request('/system/script/run'); $request->setArgument('number', $name); $result = $this->client->sendSync($request); if ($get) { $result = $this->client->sendSync(new Request('/system/script/print .proplist="source"', Query::where('name', $name)))->getArgument('source'); } $request = new Request('/system/script/remove'); $request->setArgument('numbers', $name); $this->client->sendSync($request); } return $result; }