summaryrefslogtreecommitdiffstats
path: root/modules-available/rebootcontrol/inc
diff options
context:
space:
mode:
Diffstat (limited to 'modules-available/rebootcontrol/inc')
-rw-r--r--modules-available/rebootcontrol/inc/exectemplate.inc.php106
-rw-r--r--modules-available/rebootcontrol/inc/rebootcontrol.inc.php8
2 files changed, 112 insertions, 2 deletions
diff --git a/modules-available/rebootcontrol/inc/exectemplate.inc.php b/modules-available/rebootcontrol/inc/exectemplate.inc.php
new file mode 100644
index 00000000..483dc31f
--- /dev/null
+++ b/modules-available/rebootcontrol/inc/exectemplate.inc.php
@@ -0,0 +1,106 @@
+<?php
+declare(strict_types=1);
+
+class ExecTemplate
+{
+
+ /** @var string */
+ public $id;
+ /** @var string */
+ public $title;
+ /** @var string */
+ public $command;
+ /** @var ExecTemplateField[] */
+ public $args;
+
+ public function __construct(string $id, string $title, string $command, array $args)
+ {
+ $this->id = $id;
+ $this->title = $title;
+ $this->command = $command;
+ $this->args = $args;
+ }
+
+ public function buildFromPost(): string
+ {
+ $args = [];
+ foreach ($this->args as $arg) {
+ $args[$arg->id] = Request::post('param-' . $arg->id, '', 'string');
+ }
+ return preg_replace_callback('/%([0-9]+)([a-z]*)%/', function($out) use ($args) {
+ if (!isset($args[$out[1]])) {
+ ErrorHandler::traceError('Invalid Argument Index: ' . $out[1]);
+ }
+ $str = preg_replace('/\r\n?/', "\n", $args[$out[1]]);
+ if (strpos($out[2], 'r') === false) {
+ $str = $this->bashString($str);
+ }
+ return $str;
+ }, $this->command);
+ }
+
+ private function bashString(string $string): string
+ {
+ if (strpos($string, "'") === false) {
+ return "'$string'";
+ }
+ return "'" . str_replace("'", "'\\''", $string) . "'";
+ }
+
+ // ## STATIC ##
+
+ public static function get(string $id): ?ExecTemplate
+ {
+ // TODO: some day, maybe put these in a json file? Or DB and allow user defined ones...
+ // XXX: Add to list() too if you add something here
+ if ($id === '1') {
+ return new ExecTemplate('1', Dictionary::translateFileModule('rebootcontrol', 'module', 'exec_debug_report'),
+ 'debug_report --message %1%',
+ [new ExecTemplateField('1', Dictionary::translateFileModule('rebootcontrol', 'module', 'exec_debug_report_message'), 'string')],
+ );
+ }
+ if ($id === '2') {
+ return new ExecTemplate('2', Dictionary::translateFileModule('rebootcontrol', 'module', 'exec_systemd_plot'),
+ 'systemd-analyze plot',
+ [],
+ );
+ }
+ return null;
+ }
+
+ /**
+ * @return ExecTemplate[]
+ */
+ public static function list(): array
+ {
+ return [self::get('1'), self::get('2')];
+ }
+
+}
+
+class ExecTemplateField
+{
+
+ /** @var string */
+ private $type;
+ /** @var string */
+ public $title;
+ /** @var string */
+ public $id;
+
+ public function __construct(string $id, string $title, string $type)
+ {
+ $this->id = $id;
+ $this->title = $title;
+ $this->type = $type;
+ }
+
+ public function render(): string
+ {
+ if ($this->type === 'string') {
+ return '<input type="text" class="form-control" name="param-' . $this->id . '">';
+ }
+ return '<div>???</div>';
+ }
+
+} \ No newline at end of file
diff --git a/modules-available/rebootcontrol/inc/rebootcontrol.inc.php b/modules-available/rebootcontrol/inc/rebootcontrol.inc.php
index 107c2a50..b6ad7a16 100644
--- a/modules-available/rebootcontrol/inc/rebootcontrol.inc.php
+++ b/modules-available/rebootcontrol/inc/rebootcontrol.inc.php
@@ -446,7 +446,7 @@ class RebootControl
$subnet['iclients'] = array_slice($subnet['iclients'], 0, 3);
}
- public static function prepareExec()
+ public static function prepareExec(string $presetId = null)
{
User::assertPermission('.rebootcontrol.action.exec');
$uuids = array_values(Request::post('uuid', Request::REQUIRED, 'array'));
@@ -455,7 +455,11 @@ class RebootControl
return;
$id = mt_rand();
Session::set('exec-' . $id, $machines, 60);
- Util::redirect('?do=rebootcontrol&show=exec&what=prepare&id=' . $id);
+ $q = $id;
+ if ($presetId !== null) {
+ $q .= '&preset=' . $presetId;
+ }
+ Util::redirect('?do=rebootcontrol&show=exec&what=prepare&id=' . $q);
}
/**