summaryrefslogtreecommitdiffstats
path: root/modules-available/serversetup-bwlp-ipxe/inc/menuentry.inc.php
blob: eff7f24e42134b5c650f369151b56c6a9fae00f8 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php

class MenuEntry
{
	/**
	 * @var int id of entry, used for pw
	 */
	private $menuentryid;
	/**
	 * @var false|string key code as expected by iPXE
	 */
	private $hotkey;
	/**
	 * @var string
	 */
	private $title;
	/**
	 * @var bool
	 */
	private $hidden;
	/**
	 * @var bool
	 */
	private $gap;
	/**
	 * @var int
	 */
	private $sortval;
	/**
	 * @var BootEntry
	 */
	private $bootEntry = null;

	private $md5pass = null;

	/**
	 * MenuEntry constructor.
	 *
	 * @param array $row row from database
	 */
	public function __construct($row)
	{
		if (is_array($row)) {
			foreach ($row as $key => $value) {
				if (property_exists($this, $key)) {
					$this->{$key} = $value;
				}
			}
			$this->hotkey = self::getKeyCode($row['hotkey']);
			if (!empty($row['bootentry'])) {
				$this->bootEntry = BootEntry::fromJson($row['bootentry']);
			} elseif ($row['refmenuid'] !== null) {
				$this->bootEntry = BootEntry::forMenu($row['refmenuid']);
			}
			$this->gap = (array_key_exists('entryid', $row) && $row['entryid'] === null && $row['refmenuid'] === null);
		}
		settype($this->hidden, 'bool');
		settype($this->gap, 'bool');
		settype($this->sortval, 'int');
		settype($this->menuentryid, 'int');
	}

	public function getMenuItemScript($lblPrefix, $requestedDefaultId, $mode, $slxExtensions)
	{
		if ($this->bootEntry !== null && !$this->bootEntry->supportsMode($mode))
			return '';
		$str = 'item ';
		if ($this->gap) {
			$str .= '--gap -- ';
		} else {
			if ($this->hidden && $slxExtensions) {
				if ($this->hotkey === false)
					return ''; // Hidden entries without hotkey are illegal
				$str .= '--hidden ';
			}
			if ($this->hotkey !== false) {
				$str .= '--key ' . $this->hotkey . ' ';
			}
			if ($this->menuentryid == $requestedDefaultId) {
				$str .= '--default ';
			}
			$str .= "-- {$lblPrefix}_{$this->menuentryid} ";
		}
		if (empty($this->title)) {
			$str .= '${}';
		} else {
			$str .= $this->title;
		}
		return $str . " || prompt Could not create menu item for {$lblPrefix}_{$this->menuentryid}\n";
	}

	public function getBootEntryScript($lblPrefix, $failLabel, $mode)
	{
		if ($this->bootEntry === null || !$this->bootEntry->supportsMode($mode))
			return '';
		$str = ":{$lblPrefix}_{$this->menuentryid}\n";
		if (!empty($this->md5pass)) {
			$str .= "set slx_hash {$this->md5pass} || goto $failLabel\n"
				. "set slx_salt {$this->menuentryid} || goto $failLabel\n"
				. "set slx_pw_ok {$lblPrefix}_ok || goto $failLabel\n"
				. "set slx_pw_fail slx_menu || goto $failLabel\n"
				. "goto slx_pass_check || goto $failLabel\n"
				. ":{$lblPrefix}_ok\n";
		}
		return $str . $this->bootEntry->toScript($failLabel, $mode);
	}

	public function menuEntryId()
	{
		return $this->menuentryid;
	}

	public function title()
	{
		return $this->title;
	}

	/*
	 *
	 */

	private static function getKeyArray()
	{
		static $data = false;
		if ($data === false) {
			$data = [
				'F5' => 0x107e,
				'F6' => 0x127e,
				'F7' => 0x137e,
				'F8' => 0x147e,
				'F9' => 0x157e,
				'F10' => 0x167e,
				'F11' => 0x187e,
				'F12' => 0x197e,
			];
			for ($i = 1; $i <= 26; ++$i) {
				$letter = chr(0x40 + $i);
				$data['SHIFT_' . $letter] = 0x40 + $i;
				if ($letter !== 'C') {
					$data['CTRL_' . $letter] = $i;
				}
				$data[$letter] = 0x60 + $i;
			}
			for ($i = 0; $i <= 9; ++$i) {
				$data[chr(0x30 + $i)] = 0x30 + $i;
			}
			asort($data, SORT_NUMERIC);
		}
		return $data;
	}

	/**
	 * Get all the known/supported keys, usable for menu items.
	 *
	 * @return string[] list of known key names
	 */
	public static function getKeyList()
	{
		return array_keys(self::getKeyArray());
	}

	/**
	 * Get the key code ipxe expects for the given named
	 * key. Returns false if the key name is unknown.
	 *
	 * @param string $keyName
	 * @return false|string Key code as hex string, or false if not found
	 */
	public static function getKeyCode($keyName)
	{
		$data = self::getKeyArray();
		if (isset($data[$keyName]))
			return '0x' . dechex($data[$keyName]);
		return false;
	}

	/**
	 * @param string $keyName desired key name
	 * @return string $keyName if it's known, empty string otherwise
	 */
	public static function filterKeyName($keyName)
	{
		$data = self::getKeyArray();
		if (isset($data[$keyName]))
			return $keyName;
		return '';
	}

}