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

(function() {
	$type = Request::any('type', null, 'string');
	if ($type === null && isset($_SERVER['HTTP_USER_AGENT'])) {
		if (preg_match('/^iPXE/', $_SERVER['HTTP_USER_AGENT'])) {
			$type = 'ipxe';
		} elseif (preg_match('/^GRUB/', $_SERVER['HTTP_USER_AGENT'])) {
			$type = 'grub';
		} elseif (preg_match('/wget|curl/i', $_SERVER['HTTP_USER_AGENT'])) {
			$type = 'bash';
		}
	}
	if ($type === 'bash') {
		$builder = new ScriptBuilderBash();
	} elseif ($type === 'grub') {
		$builder = new ScriptBuilderGrub();
	} else {
		$builder = new ScriptBuilderIpxe();
	}
	$bootEntryId = Request::get('beid', false, 'string');
	if ($bootEntryId !== false) {
		$bootEntryId = Util::cleanUtf8($bootEntryId);
	}
	$entryId = Request::get('entryid', false, 'int');
	if ($bootEntryId !== false) {
		$entry = BootEntry::fromDatabaseId($bootEntryId);
		$data = $builder->getBootEntry($entry);
	} elseif ($entryId !== false) {
		$entry = MenuEntry::get($entryId);
		$data = $builder->getMenuEntry($entry);
	} else {
		// Get bootstrap code if required...
		$data = $builder->bootstrapLive();
		if ($data === false) {
			// ...otherwise, generate normal code
			$menuId = Request::get('menuid', false, 'int');
			if ($menuId !== false) {
				$menu = IPxeMenu::get($menuId, true);
			} else {
				$menu = IPxeMenu::forClient($builder->clientIp(), $builder->uuid());
			}
			$data = null;
			// In exam mode, either skip menu if only one entry, or shorten timeout to 1 second
			$isExamMode = Module::isAvailable('exams')
				&& Exams::isInExamMode([Location::getFromIpAndUuid($builder->clientIp(), $builder->uuid())]);
			if ($menu->itemCount() === 1 && ($menu->timeoutMs === 0 || $isExamMode)) {
				// One entry, no timeout -- direct boot
				$entry = $menu->defaultEntry();
				if ($entry !== null) {
					$data = $builder->getMenuEntry($entry);
				}
			} elseif ($isExamMode) {
				$menu->timeoutMs = 1000;
			}
			if ($data === null) {
				// Show menu
				$data = $builder->getMenu($menu, true);
			}
		}
	}
	$builder->output($data);
})();