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', [], ); } if ($id === '3') { return new ExecTemplate('3', Dictionary::translateFileModule('rebootcontrol', 'module', 'exec_dnbd3_proxy_list'), << /dev/null; then if tree --help 2>&1 | grep -qP '\s-I\s'; then TREE_CHARSET=UTF-8 tree -h -I "*.crc" -I "*.meta" -I "*.map" "\$dir" else tree "\$dir" | grep -v -e '\.crc$' -e '\.meta$' -e '\.map$' fi else find "\$dir" -type f \! \( -name "*.crc" -o -name "*.meta" -o -name "*.map" \) fi EOF, [], ); } return null; } /** * @return ExecTemplate[] */ public static function list(): array { return [self::get('1'), self::get('2'), self::get('3')]; } } 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 ''; } return '
???
'; } }