summaryrefslogtreecommitdiffstats
path: root/modules-available/serversetup-bwlp-ipxe/inc/execdata.inc.php
blob: b82ce2e70a00274e5b58bc1b236d65d1330cc08a (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
<?php

class ExecData
{

	/**
	 * @var string The binary to launch
	 */
	public $executable = '';

	/**
	 * @var string[] List of additional images to load (initrds)
	 */
	public $initRd = [];

	/**
	 * @var string Command line to pass to executable
	 */
	public $commandLine = '';

	/**
	 * @var bool Call imgfree before loading and executing this entry
	 */
	public $imageFree = false;

	/**
	 * @var bool Whether to completely replace the currently running iPXE stack
	 */
	public $replace = false;

	/**
	 * @var bool Whether to automatically unload the binary after execution
	 */
	public $autoUnload = false;

	/**
	 * @var bool Whether to reset the console before execution
	 */
	public $resetConsole = false;

	/**
	 * @var array DHCP options to override Maps OPTIONNUM -> Value
	 */
	public $dhcpOptions = [];

	/**
	 * Supported Options
	 */
	const DHCP_OPTIONS = [
		17 => [
			'name' => 'Root Path',
			'type' => 'string',
		],
		43 => [
			'name' => 'Vendor Specific',
			'type' => 'string',
		],
		66 => [
			'name' => 'Next Server',
			'type' => 'string',
		],
		67 => [
			'name' => 'Boot File',
			'type' => 'string',
		],
		209 => [
			'name' => 'Configuration File',
			'type' => 'string',
		],
		210 => [
			'name' => 'Path Prefix',
			'type' => 'string',
		],
	];

	private function sanitize()
	{
		settype($this->executable, 'string');
		settype($this->initRd, 'array');
		foreach ($this->initRd as &$entry) {
			settype($entry, 'string');
		}
		settype($this->commandLine, 'string');
		settype($this->imageFree, 'bool');
		settype($this->replace, 'bool');
		settype($this->autoUnload, 'bool');
		settype($this->resetConsole, 'bool');
		settype($this->dhcpOptions, 'array');
		foreach (array_keys($this->dhcpOptions) as $key) {
			$val =& $this->dhcpOptions[$key];
			if (!empty($val['override'])) {
				unset($val['override']);
				$val['opt'] = $key;
				if (isset($val['hex']) && isset($val['value'])) {
					$val['value'] = preg_replace('/[^0-9a-f]/i', '', $val['value']);
					$val['value'] = substr($val['value'], 0, floor(strlen($val['value']) / 2) * 2);
					$val['value'] = strtolower($val['value']);
				}
			}
			if (!isset($val['opt']) || !is_numeric($val['opt']) || $val['opt'] <= 0 || $val['opt'] >= 255) {
				unset($this->dhcpOptions[$key]);
				continue;
			}
			if (!array_key_exists($val['opt'], self::DHCP_OPTIONS))
				continue; // Not known...
			settype($val['value'], self::DHCP_OPTIONS[$val['opt']]['type']);
		}
		$this->dhcpOptions = array_values($this->dhcpOptions);
	}

	public function toArray()
	{
		$this->sanitize();
		return [
			'executable' => $this->executable,
			'initRd' => $this->initRd,
			'commandLine' => $this->commandLine,
			'imageFree' => $this->imageFree,
			'replace' => $this->replace,
			'autoUnload' => $this->autoUnload,
			'resetConsole' => $this->resetConsole,
			'dhcpOptions' => $this->dhcpOptions,
		];
	}

	public function toFormFields($arch)
	{
		$this->sanitize();
		$opts = [];
		foreach (self::DHCP_OPTIONS as $opt => $val) {
			$opts[$opt] = [
				'opt' => $opt,
				'name' => $val['name'],
			];
		}
		foreach ($this->dhcpOptions as $val) {
			if (!isset($opts[$val['opt']])) {
				$opts[$val['opt']] = [];
			}
			$opts[$val['opt']] += [
				'opt' => $val['opt'],
				'value' => $val['value'],
				'override_checked' => 'checked',
				'hex_checked' => empty($val['hex']) ? '' : 'checked',
			];
		}
		ksort($opts);
		return [
			'is' . $arch => true,
			'mode' => $arch,
			'executable' => $this->executable,
			'initRd' => implode(',', $this->initRd),
			'commandLine' => $this->commandLine,
			'imageFree_checked' => $this->imageFree ? 'checked' : '',
			'replace_checked' => $this->replace ? 'checked' : '',
			'autoUnload_checked' => $this->autoUnload ? 'checked' : '',
			'resetConsole_checked' => $this->resetConsole ? 'checked' : '',
			'opts' => array_values($opts),
		];
	}

}