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

abstract class ScriptBuilderBase
{

	private $lblId = 0;

	protected $serverIp;

	protected $platform = '';

	/** @var string  */
	protected $clientIp;

	/** @var ?string */
	protected $uuid;

	/**
	 * @var bool Running iPXE has slx-extensions
	 */
	protected $hasExtension = false;

	public function hasExtensions(): bool
	{
		return $this->hasExtension;
	}

	public function platform(): string
	{
		return $this->platform;
	}

	public function uuid(): ?string
	{
		return $this->uuid;
	}

	public function clientIp(): string
	{
		return $this->clientIp;
	}

	public function getLabel(): string
	{
		return 'b' . mt_rand(100, 999) . 'x' . (++$this->lblId);
	}

	public function __construct(?string $platform = null, ?string $serverIp = null, ?bool $slxExtensions = null)
	{
		$this->clientIp = (string)$_SERVER['REMOTE_ADDR'];
		if (substr($this->clientIp, 0, 7) === '::ffff:') {
			$this->clientIp = substr($this->clientIp, 7);
		}
		$this->serverIp = $serverIp ?? $_SERVER['SERVER_ADDR'] ?? Property::getServerIp();
		$this->platform = $platform ?? Request::any('platform', null, 'string');
		if ($this->platform !== null) {
			$this->platform = strtoupper($this->platform);
		}
		if ($this->platform !== 'EFI' && $this->platform !== 'PCBIOS') {
			$this->platform = '';
		}
		$this->hasExtension = $slxExtensions ?? (bool)Request::any('slx-extensions', false, 'int');
		$uuid = Request::any('uuid', null, 'string');
		if ($uuid !== null
				&& preg_match('/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i', $uuid)) {
			$this->uuid = (string)$uuid;
		}
	}

	/**
	 * Output given string (script) to client, in a suitable encoding, headers, etc.
	 */
	public abstract function output(string $string): void;

	public abstract function bootstrapLive();

	public abstract function getMenu(IPxeMenu $menu, bool $bootstrap);

	/**
	 * @param MenuEntry|null $menuEntry The according menu entry, or null if invalid.
	 * @param bool $honorPassword Whether we should generate a password dialog if protected, or skip
	 * @return string generated script/code/...
	 */
	public abstract function getMenuEntry(?MenuEntry $entry, bool $honorPassword = true): string;

	/**
	 * @param BootEntry|null|false $bootEntry
	 */
	public abstract function getBootEntry(?BootEntry $entry): string;

	public abstract function getSpecial(string $special);

	public abstract function menuToScript(IPxeMenu $menu): string;

	/**
	 * Pass EITHER only $agnostic, OR $bios and/or $efi
	 * If $agnostic is given, it should be used unconditionally,
	 * and $bios/$efi should be ignored.
	 */
	public abstract function execDataToScript(?ExecData $agnostic, ?ExecData $bios, ?ExecData $efi): string;

}