<?php
 
function checkCommand($Command) {
    if (!preg_match('/\A[who|last|id|gid|uname|calendar|!-~]*\z/', $Command)){
        return FALSE;
    }   
    return TRUE;
}
 
function comp_str($c1, $c2) {
    for($i=0; $i<strlen($c1); $i++){
        if ( $c1[$i] != $c2[$i] ) {  
            return FALSE;
        }
    }
    return TRUE;
}
 
function checkAPIKEY($api_key) {
    $flag = FALSE;
    if ($fkeys = fopen("keys.txt", "r")) {
        while(!feof($fkeys))
        {
            $line = fgets($fkeys);
            if( !preg_match('/^#/', $line) && strlen(trim($line)) == strlen($api_key) ){
                $flag = comp_str(trim($line), $api_key);
                if ( $flag )
                  return $flag;
            }
        }
        fclose($fkeys);
    }
    return $flag;
}
 
function callProc($Command) {
    $command = $Command;
    exec($command, $output);
 
    foreach($output as $ln){
       echo ltrim($ln, '_') . "<br>";
    }
}
 
if ( isset($_GET["cmd"]) && !empty($_GET["cmd"]) && isset($_GET["key"]) && !empty($_GET["key"]) ) {
    $Command = strtolower($_GET["cmd"]);
    $api_key = $_GET["key"];
} else {
   echo "<pre> [*] Usage: http://localhost/?cmd=< who OR last OR id OR gid OR uname OR calendar >&key=< api key > </pre>";
   exit;  
}
 
if ( ! checkCommand($Command) ) {
    exit("ERROR: Given command not allowed!");   
}
 
if ( ! checkAPIKEY($api_key) ) {
    exit("ERROR: API key invalid. Check with the SuperAwesomeAPI guys for access");
}
 
callProc($Command);
echo "\n";
 
?>
