summaryrefslogtreecommitdiffstats
path: root/modules-available/serversetup-bwlp-ipxe/inc/pxesection.inc.php
blob: 2d9cd6abd5297a7722444f62a28930d136835a06 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php

/**
 * 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 encrypted 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(?string $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 = [];
		}
	}

	/**
	 * Does this appear to be an entry that triggers localboot?
	 */
	public function isLocalboot(): bool
	{
		return $this->localBoot !== false || preg_match('/chain\.c32$/i', $this->kernel);
	}

	/**
	 * Is this (most likely) a separating entry only that cannot be selected?
	 */
	public function isTextOnly(): bool
	{
		return ($this->label === null || empty($this->kernel)) && !$this->isHidden && !empty($this->title);
	}

}