summaryrefslogtreecommitdiffstats
path: root/modules-available/rebootcontrol/pages/exec.inc.php
blob: 422f88f8c437d50ab2701263ab65b48e8a85b923 (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
<?php

class SubPage
{

	public static function doPreprocess()
	{
		$action = Request::post('action', false, 'string');
		if ($action === 'exec') {
			self::execExec();
		}
	}

	private static function execExec()
	{
		$uuids = array_values(Request::post('uuid', Request::REQUIRED, 'array'));
		$machines = RebootUtils::getFilteredMachineList($uuids, 'action.exec');
		if (empty($machines)) {
			ErrorHandler::traceError('No machines');
		}
		RebootUtils::sortRunningFirst($machines);
		$preset = self::presetFromRequest();
		if ($preset !== null) {
			$script = $preset->buildFromPost();
		}
		if (empty($script)) {
			$script = preg_replace('/\r\n?/', "\n",
				Request::post('script', Request::REQUIRED, 'string'));
		}
		$task = RebootControl::runScript($machines, $script, 15);
		if (Taskmanager::isTask($task)) {
			Util::redirect("?do=rebootcontrol&show=task&what=task&taskid=" . $task["id"]);
		}
	}

	/*
	 * Render
	 */

	public static function doRender()
	{
		$what = Request::get('what', 'list', 'string');
		if ($what === 'prepare') {
			self::showPrepare();
		}
	}

	private static function showPrepare()
	{
		$id = Request::get('id', Request::REQUIRED, 'int');
		$machines = Session::get('exec-' . $id);
		if (!is_array($machines)) {
			Message::addError('unknown-exec-job', $id);
			return;
		}
		Session::set('exec-' . $id, false);
		$preset = self::presetFromRequest();
		Render::addTemplate('exec-enter-command', ['clients' => $machines, 'id' => $id, 'preset' => $preset]);
	}

	private static  function presetFromRequest(): ?ExecTemplate
	{
		$presetId = Request::any('preset', null, 'string');
		if ($presetId === null)
			return null;
		$preset = ExecTemplate::get($presetId);
		if ($preset === null) {
			Message::addError('exec-template-not-found', $presetId);
		}
		return $preset;
	}

	public static function doAjax()
	{

	}

}