<?php
$config['PluginDir']            = __DIR__ . '/sysview_lib';
$config['jsonOptions']          = JSON_PRETTY_PRINT+JSON_UNESCAPED_UNICODE;

$config['BuildInFunctions']     = ['FUNCTIONS_LIST','FEHLER_TEST','LINUXKERNEL', 'HOSTNAME','OSNAME'];
$config['DataTypes']            = ['ARRAY', 'STRING', 'TEXT/PLAIN', 'TEXT/HTML', 'TEXT/JAVASCRIPT', 'TEXT/CSS'];

$config['ErrorReporting']       = true;

$e = 0; #1
$errors[0]['errorString']   = 'Testfehler';
$errors[0]['errorType']     = 'Notice';

$e = 1; #2
$errors[pow(2,$e)]['errorString']   = 'Nicht vorgesehener Reqest';
$errors[pow(2,$e)]['errorType']     = 'FATAL';


$e = 2; #4
$errors[pow(2,$e)]['errorString']   = 'Nicht lesbares Plugin (zwecks Demonstration provoziert!)';
$errors[pow(2,$e)]['errorType']     = 'NOTICE';

$e = 3; #8
$errors[pow(2,$e)]['errorString']   = 'Diese Schnittstelle ist veraltet und wird demnächst nicht mehr funktionieren.';
$errors[pow(2,$e)]['errorType']     = 'DEPRECATED';

$e = 15; #32768
$errors[pow(2,$e)]['errorString']   = 'Funktionsinterner Fehler';
$errors[pow(2,$e)]['errorType']     = 'NOTICE';


$e = 16; #65536
$errors[pow(2,$e)]['errorString']   = 'Nicht konfigurierter Fehler';
$errors[pow(2,$e)]['errorType']     = 'NOTICE';

$allOutput=array();

$config['allFunctions'] = getAllFunctions();
$arRequests=getRequests();


foreach ($arRequests as $Request) {
    try {
        if ( in_array($Request, $config['allFunctions']) ) {
            call_user_func($Request);
        }
    } catch (Exception $e) {
        error_log($e);
    }
}



/** Benutzte Funktionen **/
function getAllFunctions() {

    global $config;

    $ret = $config['BuildInFunctions'];
    if ( is_dir($config['PluginDir']) ) {
        if ($d = dir($config['PluginDir']) ) {
            while (false !== ($entry = $d -> read())) {
                if ( '.' != $entry[0]  && '.php' == substr($entry, -4)) {

                    $f = $config['PluginDir'] . '/' . $entry;
                    if ( is_readable($f) ) {
                        $ret[] = substr($entry,0, -4);
                        require_once($f);
                    } else {
                        error_log("$f ist nicht lesbar");
                    }
                }
            }
            $d->close();
        }
    }
    sort($ret);
    return $ret;
}

function getRequests () {
    global $config;
    $arRequests=array();
    if ( empty($_REQUEST['q']) ) {
        $arRequests =  $config['allFunctions'];
    } else {
        $arr=explode(',', $_REQUEST['q']);
        foreach ($arr as $item) {
            $item=trim($item);
            if ( in_array($item, $config['allFunctions']) ) {
                $arRequests[]=$item;
            } else {
                registerError($item, 2);
            }
        }
    }
    return $arRequests;
}


function registerOutput ($caller, $dataType, $data) {
    global $config, $errors, $allErrors, $allOutput;
    $o['dataType']    = $dataType;
    $o['data']    = $data;
    $allOutput[$caller]=$o;
}


function registerError ($caller, $errorNumber, $dataType=false, $data=false) {

    global $config, $errors, $allOutput;

    if ( $config['ErrorReporting'] ) {

    if ( ! false === strpos($caller, '/') ) {
        $callerAusgabe = '[DOCUMENT_ROOT]' . str_replace(realpath($_SERVER['DOCUMENT_ROOT']), '', $caller);
    } else {
        $callerAusgabe = $caller;
    }

    if ( isset($errors[$errorNumber]['dataType']) ) {
        $o['dataType']    = $errors[$errorNumber]['dataType'];
    } else {
        $o['dataType']    = $dataType;
    }

    if ( isset($errors[$errorNumber]['data']) ) {
        $o['data']    = $errors[$errorNumber]['data'];
    } else {
        $o['data']    = $data;
    }

    $o['errorNumber']     = $errorNumber;
    if (! isset($errors[$errorNumber]['errorString'])) {
        $o['errorString']     = $errors[65536]['errorString'] . " (errorNumber=$errorNumber)";
        $o['errorType']       = $errors[65536]['errorType'];
    } else {
        $o['errorString']     = $errors[$errorNumber]['errorString'];
        $o['errorType']       = $errors[$errorNumber]['errorType'];
    }
    error_log( $o['errorType'] . ': (' . $o['errorNumber'] . ') : ' . $o['errorString'] . ': '. $callerAusgabe );
    $allOutput[$caller]=$o;

    }
}


/** Build-In-Funktionen **/

function FUNCTIONS_LIST() {
    global $config;
    registerOutput(__FUNCTION__, 'ARRAY', $config['allFunctions']);
}

function FEHLER_TEST() {
    registerError(__FUNCTION__, 8, $dataType='STRING', $data='Funktionstest, dient zum Testen der Rückgabe und Auswertung von Fehlern. Sie sehen dieses, da das Error-reporting aktiviert ist.');
}

function LINUXKERNEL() {
    registerOutput( __FUNCTION__, 'STRING', trim(`uname -r`) );
}


function HOSTNAME() {
    registerOutput( __FUNCTION__, 'STRING', trim(`uname -n`) );
}

function OSNAME() {
    registerOutput( __FUNCTION__, 'STRING', trim(`uname -o`) );
}






#*/