From 36e47dcdfb7d19d4c8982a77b4dd3b87d8c4ca31 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 16 Oct 2019 18:12:17 +0200 Subject: [serversetup-bwlp-ipxe/minilinux] Implement minilinux hook for ipxe --- .../serversetup-bwlp-ipxe/inc/bootentry.inc.php | 1 + .../inc/bootentryhook.inc.php | 134 ++++++++++++++++++++- .../serversetup-bwlp-ipxe/inc/execdata.inc.php | 2 +- 3 files changed, 130 insertions(+), 7 deletions(-) (limited to 'modules-available/serversetup-bwlp-ipxe/inc') diff --git a/modules-available/serversetup-bwlp-ipxe/inc/bootentry.inc.php b/modules-available/serversetup-bwlp-ipxe/inc/bootentry.inc.php index e97d1389..174f4459 100644 --- a/modules-available/serversetup-bwlp-ipxe/inc/bootentry.inc.php +++ b/modules-available/serversetup-bwlp-ipxe/inc/bootentry.inc.php @@ -260,6 +260,7 @@ class StandardBootEntry extends BootEntry } else { $entry = $this->efi; } + $entry->sanitize(); $script = ''; if ($entry->resetConsole) { diff --git a/modules-available/serversetup-bwlp-ipxe/inc/bootentryhook.inc.php b/modules-available/serversetup-bwlp-ipxe/inc/bootentryhook.inc.php index 2e2e5009..f89031a3 100644 --- a/modules-available/serversetup-bwlp-ipxe/inc/bootentryhook.inc.php +++ b/modules-available/serversetup-bwlp-ipxe/inc/bootentryhook.inc.php @@ -14,18 +14,24 @@ abstract class BootEntryHook private $selectedId; + private $data = []; + + /** + * @return string + */ public abstract function name(); /** - * @return HookEntryGroup[] + * @return HookExtraField[] */ - protected abstract function groupsInternal(); + public abstract function extraFields(); + + public abstract function isValidId($id); /** - * @param $id - * @return BootEntry|null the actual boot entry instance for given entry, null if invalid id + * @return HookEntryGroup[] */ - public abstract function getBootEntry($id); + protected abstract function groupsInternal(); /** * @return HookEntryGroup[] @@ -43,11 +49,40 @@ abstract class BootEntryHook return $groups; } + /** + * @param $id + * @return BootEntry|null the actual boot entry instance for given entry, null if invalid id + */ + public abstract function getBootEntryInternal($data); + + public final function getBootEntry($data) + { + if (!is_array($data)) { + $data = json_decode($data, true); + } + return $this->getBootEntryInternal($data); + } + public function setSelected($id) { + $json = @json_decode($id, true); + if (is_array($json)) { + $id = $json['id']; + $this->data = $json; + } $this->selectedId = $id; } + public function renderExtraFields() + { + $list = $this->extraFields(); + foreach ($list as &$entry) { + $entry->currentValue = isset($this->data[$entry->name]) ? $this->data[$entry->name] : $entry->default; + $entry->hook = $this; + } + return $list; + } + } class HookEntryGroup @@ -78,14 +113,101 @@ class HookEntry * @var string */ public $name; + /** + * @var bool + */ + public $valid; + /** + * @var string if !valid, this will be the string 'disabled', empty otherwise + */ + public $disabled; /** * @var string internal - to be set by ipxe module */ public $selected; - public function __construct($id, $name) + /** + * HookEntry constructor. + * + * @param string $id + * @param string $name + * @param bool $valid + */ + public function __construct($id, $name, $valid) { $this->id = $id; $this->name = $name; + $this->valid = $valid; + $this->disabled = $valid ? '' : 'disabled'; } +} + +class HookExtraField +{ + /** + * @var string ID of extra field, [a-z0-9\-] please. Must not be 'id' + */ + public $name; + /** + * @var string type of field, use string, bool, or an array of predefined options + */ + public $type; + /** + * @var mixed default value + */ + public $default; + + public $currentValue; + + /** + * @var BootEntryHook + */ + public $hook; + + public function __construct($name, $type, $default) + { + $this->name = $name; + $this->type = $type; + $this->default = $default; + } + + public function fromPost($typePrefix) + { + if (is_array($this->type)) { + $val = Request::post('extra-' . $typePrefix . '-' . $this->name, '', 'array'); + if (!in_array($val, $this->type)) { + $val = $this->default; + } + } else { + $val = Request::post('extra-' . $typePrefix . '-' . $this->name, '', $this->type); + settype($val, $this->type); + } + return $val; + } + + public function html() + { + $fieldId = 'extra-' . $this->hook->moduleId . '-' . $this->name; + $fieldText = htmlspecialchars(Dictionary::translateFileModule($this->hook->moduleId, 'module', 'ipxe-' . $this->name, true)); + if (is_array($this->type)) { + $out = ''; + return $out; + } + if ($this->type === 'bool') { + $checked = $this->currentValue ? 'checked' : ''; + return '
'; + } + // Default + return '' + . ''; + } + } \ No newline at end of file diff --git a/modules-available/serversetup-bwlp-ipxe/inc/execdata.inc.php b/modules-available/serversetup-bwlp-ipxe/inc/execdata.inc.php index b82ce2e7..97f98b94 100644 --- a/modules-available/serversetup-bwlp-ipxe/inc/execdata.inc.php +++ b/modules-available/serversetup-bwlp-ipxe/inc/execdata.inc.php @@ -73,7 +73,7 @@ class ExecData ], ]; - private function sanitize() + public function sanitize() { settype($this->executable, 'string'); settype($this->initRd, 'array'); -- cgit v1.2.3-55-g7522