<?php

//#!/usr/bin/env php
define('WEBSOCKET', '1');
require_once dirname(__FILE__) . '/includes/LoginManager.php';
require_once dirname(__FILE__) . '/includes/DatabaseManager.php';
require_once dirname(__FILE__) . '/includes/PluginManager.php';
require_once dirname(__FILE__) . '/includes/AsyncManager.php';
require_once dirname(__FILE__) . '/websockets/websockets.php';
require_once dirname(__FILE__) . '/config.php';
$pluginManager = new PluginManager();
$pluginManager->enableNotifications(true);
require_once $pluginManager->getController('plg_serversettings', 'servertools');
function shutdown()
{
    exec('sudo /var/www/sh/wsrestart.sh');
    echo 'Script executed with success', PHP_EOL;
}
function myErrorHandler($fehlercode, $fehlertext, $fehlerdatei, $fehlerzeile)
{
    echo $fehlertext . ' (' . $fehlercode . ') in ' . $fehlerdatei . ' on line ' . $fehlerzeile;
    return true;
}
function triggerhourly()
{
    global $pluginManager;
    $pluginManager->triggerHourly();
}
function triggerminutely()
{
    global $pluginManager;
            $fgcolor = $conf['fgcolor'];
        }
        $output['fgcolor'] = $fgcolor;
        $bgcolor = $loginManager->getUserPreference("bgcolor", null);
        if (empty($bgcolor)) {
            $bgcolor = $conf['bgcolor'];
        }
        $output['bgcolor'] = $bgcolor;
        echo json_encode($output);
    }
} else {
    if (!empty($_POST['username']) and !empty($_POST['password'])) {
        $authtoken = $loginManager->loginUserByPassword($_POST['username'], $_POST['password'], 'Webinterface');
        if (!empty($authtoken)) {
            $pluginManager = new PluginManager();
            $pluginManager->enableNotifications(TRUE);
            $notificationManager = $pluginManager->getNotificationManager();
            $notification = new Notification("Sie haben sich mit ihrem Benutzerkonto auf einem neuen Endgerät angemeldet.", "Neue Anmeldung", time() + 86400);
            $notification->addReaded($authtoken);
            $notification->setActionOpenPlugin("plg_user");
            $notification->setIcon(FileManager::getImageHashByPath('images/user-add.png'));
            $notificationManager->addNotification($notification, $loginManager->getId());
            echo '{"token":' . json_encode($authtoken) . ',"username":"******",';
            echo '"mainplugins":' . $pluginManager->getMainPlugins();
            /* TODO */
            if (!$loginManager->getShareManager()->isShared()) {
                $seamless = $loginManager->getUserPreference("seamless");
                if (!empty($seamless) && strtoupper($seamless) == "TRUE") {
                    $seamlessString = $loginManager->getUserPreference("seamless-current");
                    if (!empty($seamlessString)) {
                        echo ',"seamless":' . $seamlessString;