<?php
class MenuEntry
{
/**
* @var int id of entry, used for pw
*/
public $menuentryid;
/**
* @var false|string key code as expected by iPXE
*/
public $hotkey;
/**
* @var string
*/
public $title;
/**
* @var bool
*/
public $hidden;
/**
* @var bool
*/
public $gap;
/**
* @var int
*/
public $sortval;
/**
* @var ?BootEntry
*/
public $bootEntry = null;
public $plainpass = null;
public $md5pass = null;
public static function get(int $menuEntryId): ?MenuEntry
{
$row = Database::queryFirst("SELECT e.menuentryid, e.entryid, e.refmenuid, e.hotkey, e.title,
e.hidden, e.sortval, e.plainpass, e.md5pass, b.module, b.data AS bootentry, b.title AS betitle
FROM serversetup_menuentry e
LEFT JOIN serversetup_bootentry b USING (entryid)
WHERE e.menuentryid = :id", ['id' => $menuEntryId]);
if ($row === false)
return null;
return new MenuEntry($row);
}
/**
* MenuEntry constructor.
*
* @param array $row row from database
*/
public function __construct(array $row)
{
if (empty($row['title']) && !empty($row['betitle'])) {
$row['title'] = $row['betitle'];
}
foreach ($row as $key => $value) {
if (property_exists($this, $key)) {
$this->{$key} = $value;
}
}
$this->hotkey = self::getKeyCode($row['hotkey'] ?? '');
if (!empty($row['bootentry']) && $row['module'] !== null) {
$this->bootEntry = BootEntry::fromJson($row['module'], $row['bootentry']);
} elseif (isset($row['refmenuid'])) {
$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 getBootEntryScript(ScriptBuilderBase $builder): string
{
if ($this->bootEntry === null)
return '';
return $this->bootEntry->toScript($builder);
}
public function menuEntryId(): int
{
return $this->menuentryid;
}
public function title(): string
{
return $this->title;
}
public function internalId(): string
{
if ($this->bootEntry === null)
return '';
return $this->bootEntry->internalId();
}
/*
*
*/
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(): array
{
return array_keys(self::getKeyArray());
}
/**
* Get the key code ipxe expects for the given named
* key. Returns false if the key name is unknown.
*
* @return false|string Key code as hex string, or false if not found
*/
public static function getKeyCode(string $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(string $keyName): string
{
$data = self::getKeyArray();
if (isset($data[$keyName]))
return $keyName;
return '';
}
}