summaryrefslogblamecommitdiffstats
path: root/modules-available/rebootcontrol/pages/task.inc.php
blob: 7db2a90b70dac0e66ead6fc5f6edcaa6f3431259 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12











                                             





                                                                                             









                                                                   

                                                                                          
                                                                              
                                                              





                                                                                   
                         
                                             





                                                                             


                                      

                                                               


































                                                                                                               
                                                                                                                                  
                                                               
                                                                                                                                 







                                                                                                               
                                                           
                 
                                                                          
                                                                 



                                              
                                                   
                                                              
                                                                     


                                                           




                                                                      



                                                                                     
                                                                                              

                                      
                                                                                              



                                                                              


                                                         
                                                              








                                                                   





                                       
<?php

class SubPage
{

	public static function doPreprocess()
	{

	}

	public static function doRender()
	{
		$xxx = Request::get('tasks');
		if (is_array($xxx)) {
			$data = array_map(function($item) { return ['id' => $item]; }, $xxx);
			Render::addTemplate('status-wol', ['tasks' => $data]);
			return;
		}
		$show = Request::get('what', 'tasklist', 'string');
		if ($show === 'tasklist') {
			self::showTaskList();
		} elseif ($show === 'task') {
			self::showTask();
		}
	}

	private static function showTask()
	{
		// No permission check here - user had to guess the UUID, not very likely,
		// but this way we can still link to some implicitly triggered job
		$taskid = Request::get("taskid", Request::REQUIRED, 'string');
		$type = Request::get('type', false, 'string');
		if ($type === 'checkhost') {
			// Override
			$task = Taskmanager::status($taskid);
			if (!Taskmanager::isTask($task) || !isset($task['data'])) {
				Message::addError('no-such-task', $taskid);
				return;
			}
			$td =& $task['data'];
			$ip = array_key_first($td['result']);
			$data = [
				'taskId' => $task['id'],
				'host' => $ip,
			];
			Render::addTemplate('status-checkconnection', $data);
			return;
		}
		if ($type !== false) {
			Message::addError('unknown-task-type');
		}

		$job = RebootControl::getActiveTasks(null, $taskid);
		if ($job === false) {
			Message::addError('no-such-task', $taskid);
			return;
		}
		if (isset($job['type'])) {
			$type = $job['type'];
		}
		if ($type === RebootControl::TASK_EXEC) {
			$template = $perm = 'exec';
		} elseif ($type === RebootControl::TASK_REBOOTCTL) {
			$template = 'reboot';
			if ($job['action'] === RebootControl::SHUTDOWN) {
				$perm = 'shutdown';
			} else {
				$perm = 'reboot';
			}
		} elseif ($type == RebootControl::TASK_WOL) {
			$template = $perm = 'wol';
		} else {
			Message::addError('unknown-task-type', $type);
			return;
		}
		if (!empty($job['locations'])) {
			$allowedLocs = User::getAllowedLocations("action.$perm");
			if (!in_array(0, $allowedLocs) && array_diff($job['locations'], $allowedLocs) !== []) {
				Message::addError('main.no-permission');
				return;
			}
			self::expandLocationIds($job['locations']);
		}

		// Output
		if ($type === RebootControl::TASK_REBOOTCTL) {
			$job['clients'] = RebootUtils::getMachinesByUuid(ArrayUtil::flattenByKey($job['clients'], 'machineuuid'));
		} elseif ($type === RebootControl::TASK_EXEC) {
			$details = RebootUtils::getMachinesByUuid(ArrayUtil::flattenByKey($job['clients'], 'machineuuid'), true);
			foreach ($job['clients'] as &$client) {
				if (isset($client['machineuuid']) && isset($details[$client['machineuuid']])) {
					$client += $details[$client['machineuuid']];
				}
			}
		} elseif ($type === RebootControl::TASK_WOL) {
			// Nothing (yet)
		} else {
			ErrorHandler::traceError('oopsie');
		}
		$job['timestamp_s'] = Util::prettyTime($job['timestamp']);
		Render::addTemplate('status-' . $template, $job);
	}

	private static function showTaskList()
	{
		Render::addTemplate('task-header');
		// Append list of active reboot/shutdown tasks
		$allowedLocs = User::getAllowedLocations("action.*");
		if (empty($allowedLocs)) {
			User::assertPermission('action.*');
		}
		$active = RebootControl::getActiveTasks($allowedLocs);
		if (empty($active)) {
			Message::addInfo('no-current-tasks');
		} else {
			foreach ($active as &$entry) {
				self::expandLocationIds($entry['locations']);
				if (isset($entry['clients'])) {
					$entry['clients'] = count($entry['clients']);
				}
				$entry['timestamp_s'] = Util::prettyTime($entry['timestamp']);
			}
			unset($entry);
			ArrayUtil::sortByColumn($active, 'timestamp', SORT_ASC, SORT_NUMERIC);
			Render::addTemplate('task-list', ['list' => $active]);
		}
	}

	private static function expandLocationIds(&$lids)
	{
		foreach ($lids as &$locid) {
			if ($locid === null || $locid === 0) {
				$name = '-';
			} else {
				$name = Location::getName($locid);
			}
			$locid = ['id' => $locid, 'name' => $name];
		}
		$lids = array_values($lids);
	}

	public static function doAjax()
	{

	}

}