diff options
Diffstat (limited to 'modules-available/serversetup-bwlp-ipxe/inc/pxelinux.inc.php')
-rw-r--r-- | modules-available/serversetup-bwlp-ipxe/inc/pxelinux.inc.php | 174 |
1 files changed, 11 insertions, 163 deletions
diff --git a/modules-available/serversetup-bwlp-ipxe/inc/pxelinux.inc.php b/modules-available/serversetup-bwlp-ipxe/inc/pxelinux.inc.php index ff548c4c..24b099dc 100644 --- a/modules-available/serversetup-bwlp-ipxe/inc/pxelinux.inc.php +++ b/modules-available/serversetup-bwlp-ipxe/inc/pxelinux.inc.php @@ -7,11 +7,14 @@ class PxeLinux /** * Takes a (partial) pxelinux menu and parses it into * a PxeMenu object. + * * @param string $input The pxelinux menu to parse - * @return PxeMenu the parsed menu + * @return ?PxeMenu the parsed menu, or null if input is not a PXELinux menu */ - public static function parsePxeLinux($input, $isCp437) + public static function parsePxeLinux(string $input, bool $isCp437): ?PxeMenu { + if (empty($input)) + return null; if ($isCp437) { $input = iconv('IBM437', 'UTF8//TRANSLIT//IGNORE', $input); } @@ -76,12 +79,14 @@ class PxeLinux } $section->helpText = $text; } elseif (self::handleKeyword($key, $val, $sectionPropMap, $section)) { - continue; + //continue; } } if ($section !== null) { $menu->sections[] = $section; } + if (empty($menu->sections)) + return null; // Probably not a PXE menu but random text? foreach ($menu->sections as $section) { $section->mangle(); } @@ -93,13 +98,14 @@ class PxeLinux * to the given object. The map to look up the keyword has to be passed * as well as the object to set the value in. Map and object should * obviously match. + * * @param string $key keyword of parsed line * @param string $val raw value of currently parsed line (empty if not present) * @param array $map Map in which $key is looked up as key - * @param PxeMenu|PxeSection The object to set the parsed and sanitized value in + * @param PxeMenu|PxeSection $object The object to set the parsed and sanitized value in * @return bool true if the value was found in the map (and set in the object), false otherwise */ - private static function handleKeyword($key, $val, $map, $object) + private static function handleKeyword(string $key, string $val, array $map, $object): bool { if (!isset($map[$key])) return false; @@ -122,161 +128,3 @@ class PxeLinux } -/** - * Class representing a parsed pxelinux menu. Members - * will be set to their annotated type if present or - * be null otherwise, except for present-only boolean - * options, which will default to false. - */ -class PxeMenu -{ - - /** - * @var string menu title, shown at the top of the menu - */ - public $title; - /** - * @var int initial timeout after which $timeoutLabel would be executed - */ - public $timeoutMs; - /** - * @var int if the user canceled the timeout by pressing a key, this timeout would still eventually - * trigger and launch the $timeoutLabel section - */ - public $totalTimeoutMs; - /** - * @var string label of section which will execute if the timeout expires - */ - public $timeoutLabel; - /** - * @var bool hide menu and just show background after triggering an entry - */ - public $menuClear = false; - /** - * @var bool boot the associated entry directly if its corresponding hotkey is pressed instead of just highlighting - */ - public $immediateHotkeys = false; - /** - * @var PxeSection[] list of sections the menu contains - */ - public $sections = []; - /** - * @var string The DEFAULT entry of the menu. Usually refers either to a - * LABEL, or a loadable module (like vesamenu.c32) - */ - public $default; - - /** - * Check if any of the sections has the given label. - */ - public function hasLabel($label) - { - foreach ($this->sections as $section) { - if ($section->label === $label) - return true; - } - return false; - } - -} - -/** - * Class representing a parsed pxelinux menu entry. Members - * will be set to their annotated type if present or - * be null otherwise, except for present-only boolean - * options, which will default to false. - */ -class PxeSection -{ - - /** - * @var string label used internally in PXEMENU definition to address this entry - */ - public $label; - /** - * @var string MENU LABEL of PXEMENU - title of entry displayed to the user - */ - public $title; - /** - * @var int Number of spaces to prefix the title with - */ - public $indent; - /** - * @var string help text to display when the entry is highlighted - */ - public $helpText; - /** - * @var string Kernel to load - */ - public $kernel; - /** - * @var string|string[] initrd to load for the kernel. - * If mangle() has been called this will be an array, - * otherwise it's a comma separated list. - */ - public $initrd; - /** - * @var string command line options to pass to the kernel - */ - public $append; - /** - * @var int IPAPPEND from PXEMENU. Bitmask of valid options 1 and 2. - */ - public $ipAppend; - /** - * @var string Password protecting the entry. This is most likely in crypted form. - */ - public $passwd; - /** - * @var bool whether this section is marked as default (booted after timeout) - */ - public $isDefault = false; - /** - * @var bool Menu entry is not visible (can only be triggered by timeout) - */ - public $isHidden = false; - /** - * @var bool Disable this entry, making it unselectable - */ - public $isDisabled = false; - /** - * @var int|false Value of the LOCALBOOT field, false if not set - */ - public $localBoot = false; - /** - * @var string hotkey to trigger item. Only valid after calling mangle() - */ - public $hotkey; - - public function __construct($label) { $this->label = $label; } - - public function mangle() - { - if (($i = strpos($this->title, '^')) !== false) { - $this->hotkey = strtoupper($this->title{$i+1}); - $this->title = substr($this->title, 0, $i) . substr($this->title, $i + 1); - } - if (strpos($this->append, 'initrd=') !== false) { - $parts = preg_split('/\s+/', $this->append); - $this->append = ''; - for ($i = 0; $i < count($parts); ++$i) { - if (preg_match('/^initrd=(.*)$/', $parts[$i], $out)) { - if (!empty($this->initrd)) { - $this->initrd .= ','; - } - $this->initrd .= $out[1]; - } else { - $this->append .= ' ' . $parts[$i]; - } - } - $this->append = trim($this->append); - } - if (is_string($this->initrd)) { - $this->initrd = explode(',', $this->initrd); - } elseif (!is_array($this->initrd)) { - $this->initrd = []; - } - } - -} - |