<?php
    session_start();
	if ( empty($_SESSION['login']) ) {
        header( 'Location: login.php' );
        die;
	}

    include 'include/database.php';

    $connection = connect($database);

    $canControl = canControlSystems($connection, $_SESSION['login']);

    if ($canControl) {
        $base_path = 'command_processors';
        $command = getCommand($connection, $_POST['button_id']);
        
        $processor = realpath($base_path . '/' . $command['processor']);
        $argument = str_replace('$$CMD', $command['command'], $command['argument']);
        
        if ($argument === $command['command']) {
            $argument = $command['command'] . ' ' . $command['argument'];
        }
        
        $argument = escapeshellcmd($argument);
		
        $full_command = $processor . ' ' . $argument;
        exec($full_command);
    }

    $connection = null;
?>