summaryrefslogtreecommitdiffstats
path: root/modules-available/rebootcontrol/inc/exectemplate.inc.php
diff options
context:
space:
mode:
Diffstat (limited to 'modules-available/rebootcontrol/inc/exectemplate.inc.php')
-rw-r--r--modules-available/rebootcontrol/inc/exectemplate.inc.php106
1 files changed, 106 insertions, 0 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