Location: PHPKode > projects > PHP Half-Life Monitor > phlmon-1.0.2-beta/functions.php
<?php

#    PHP Half-Life Monitor 1.0.2-beta
#
#    $Id: functions.php,v 1.10 2004/04/02 21:06:12 sloede Exp $
#
#    This file is part of the PHP Half-Life Monitor program
#
#    (c) 2002-2004 by Michael Schlottke <hide@address.com> <http://phlmon.sloede.com>
#
#    PHP Half-Life Monitor (short: PHLMon ) is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with PHLMon; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

function wrapLink($link) {
    global $phlmonConfig;
    if ($phlmonConfig['useSessions'] == "no") {
        global $unserializedData;
        $serializedData = base64_encode(serialize($unserializedData));
        if (preg_match("/.php\$/", $link)) {
            $link .= "?serializedData=".$serializedData;
        } else {
            $link .= "&serializedData=".$serializedData;
        }        
    } else {
        if (preg_match("/.php\$/", $link)) {
            $link .= "?".SESSION;
        } else {
            $link .= "&".SESSION;
        }
    }
    return $link;
}

function loadConfigList() {
    global $phlmonConfig, $configList;
    include($phlmonConfig['fileNames']['configList']);
    return true;
}

function loadServerList() {
    global $phlmonConfig, $serverList;
    include($phlmonConfig['fileNames']['serverList']);
    return true;
}

function setSessionVar($key, $value) {
    global $phlmonConfig;
    if ($phlmonConfig['useSessions'] == "no") {
        global $unserializedData;
        $unserializedData[$key] = $value;
    } else {
        $_SESSION[$key] = $value;
    }
}

function getSessionVar($key) {
    global $phlmonConfig;
    if ($phlmonConfig['useSessions'] == "no") {
        global $unserializedData;
        return $unserializedData[$key];
    } else {
        return $_SESSION[$key];
    }
}

function showContent($content) {
    global $language, $phlmonConfig, $halfc;
    $additionalMeta = "";		
    if ( is_numeric( $_REQUEST['reloadTime'] ) ) {
        $additionalMeta = "<meta http-equiv=\"refresh\" content=\""
        .$_REQUEST['reloadTime']."; URL=".wrapLink(getenv('SCRIPT_NAME'))."\">\n";
    }
    
    $form = explode( '%s', $language[7] );
    
    $template = new template($phlmonConfig['templateSet']);
    $template -> assign( 'meta', $additionalMeta );
    $template -> assign( 'title', 'PHP Half-Life Monitor '.VERSION );
    $template -> assign( 'overview', $language[0] );
    $template -> assign( 'server', $language[1]);
    $template -> assign( 'players', $language[2] );
    $template -> assign( 'bans', $language[3] );
    $template -> assign( 'configs', $language[4] );
    $template -> assign( 'about', $language[5] );
    $template -> assign( 'reload', $language[6] );
    $template -> assign( 'indexLink', wrapLink("index.php") );
    $template -> assign( 'serverLink', wrapLink("server.php") );
    $template -> assign( 'playersLink', wrapLink("players.php") );
    $template -> assign( 'bansLink', wrapLink("bans.php") );
    $template -> assign( 'configsLink', wrapLink("configs.php") );
    $template -> assign( 'aboutLink', wrapLink("about.php") );
    $template -> assign( 'selfLink', wrapLink(getenv('SCRIPT_NAME')) );
    $template -> assign( 'formStart', $form[0] );
    $template -> assign( 'formFinish', $form[1] );
    $header = $template -> get( 'header.tpl' );
    $template -> destroy();
    
    $template -> assign( 'version', VERSION );
    $footer = $template -> get( 'footer.tpl' );
    $template -> destroy();
    
    echo $header.$content.$footer;
    if (is_object($halfc) && $halfc->isServerOpen()) {
        $halfc->closeServer();
    }
    exit();
}

function isAuthorized() {
    global $phlmonConfig;
    if ($phlmonConfig['authType'] != 'http') {
        return true;
    } else if (empty($phlmonConfig['authAdmin']) || 
            empty($phlmonConfig['authAdminPassword']) && 
            getSessionVar('isAuthenticated')) {
        return true;
    } else if (getSessionVar('isAdmin')) {
        return true;
    } else {
        return false;
    }
}

function openServer() {
    global $halfc, $language, $server;
    if (!$halfc->isServerOpen() ) {
        if (!$server['serverIsSelected']) {
            errorMessage($language[93]);
        } else if (!$server['serverIsAvailable']) {
            errorMessage($language[94]);
        } else {
            $halfc->setServer($server['IP'], $server['port'], $server['rconPassword']);
            $halfc->openServer();
            if (!$halfc->checkRconPassword())  {
                errorMessage($language[95]);
            }
        }
    }
}

function closeServer() {
    global $halfc, $server;
    if ($halfc->isServerOpen()) {
        $halfc->closeServer();
    }
}

function initServer() {
    global $server, $serverList;
    $serverID = isset($_POST['serverID']) ? $_POST['serverID'] : $_GET['serverID'];
    if ($serverID == 'none') {
        $server['serverID'] = '';
        $server['serverIsSelected'] = false;
        setSessionVar('serverID', "");
    } else if (is_numeric($serverID)) {
        $server['serverID'] = $serverID;
        $server['serverIsSelected'] = true;
        setSessionVar('serverID', $serverID);
    } else if (is_numeric( getSessionVar('serverID'))) {
        $server['serverID'] = getSessionVar('serverID');
        $server['serverIsSelected'] = true;
    } else {
        $server['serverID'] = '';
        $server['serverIsSelected'] = false;
    }
    if ($server['serverIsSelected'] && 
            !empty($serverList[$server['serverID']]['IP']) && 
            !empty($serverList[$server['serverID']]['port']) && 
            !empty($serverList[$server['serverID']]['rconPassword'])) {
        $server['IP'] = $serverList[$server['serverID']]['IP'];
        $server['port'] = $serverList[$server['serverID']]['port'];
        $server['rconPassword'] = $serverList[$server['serverID']]['rconPassword'];
        $server['serverIsAvailable'] = true;
    } else {
        $server['serverIsAvailable'] = false;
    }
}

function initHostnames() {
    global $serverList;
    if (is_array($serverList)) {
        $hostnames = getSessionVar('hostnames');
        foreach($serverList AS $key => $value) {
            if (!empty($hostnames[$key])) {
                $serverList[$key]['hostname'] = $hostnames[$key];
            }
        }
    }
}

function errorMessage($message) {
    global $language;
    showContent($message
        ."<br><br><a href=\"javascript:history.back();\">"
        .$language[92]
        ."</a>");
}

function loginPage() {
    global $phlmonConfig, $language;
    $template = new template($phlmonConfig['templateSet']);
    $template -> assign( 'loginFormLink', wrapLink(getenv('SCRIPT_NAME')) );
    $template -> assign( 'login', $language[89] );
    $template -> assign( 'password', $language[90] );
    $template -> assign( 'submit', $language[91] );
    $content = $template->get("login.tpl");
    showContent($content);
}

function initAuthentication() {
    global $phlmonConfig;
    if ($phlmonConfig['authType'] != 'http' || (
            getSessionVar('isAuthenticated') &&
            getSessionVar('registeredIP') == getenv('REMOTE_ADDR') &&
            $phlmonConfig['useSessions'] != "no")) {
        setSessionVar('isAuthenticated', true);
        return true;
    } else if ($phlmonConfig['useSessions'] == "no" && getSessionVar('isAuthenticated')) {
        if (getSessionVar('isAdmin')) {
            if (getSessionVar('secureHash') == md5(getenv("REMOTE_ADDR").$phlmonConfig['authAdminPassword'])) {
                return true;
            }
        } else {
            if (getSessionVar('secureHash') == md5(getenv("REMOTE_ADDR").$phlmonConfig['authUserPassword'])) {
                return true;
            }
        }
    }
    
    if (!empty($phlmonConfig['authUser']) && 
        !empty($phlmonConfig['authUserPassword']) && (
        empty($phlmonConfig['authAdmin']) || empty($phlmonConfig['authAdminPassword']))) {

        if (!isset($_POST['login'])) {
            loginPage();
        } else if ($_POST['login'] != $phlmonConfig['authUser'] || $_POST['password'] != $phlmonConfig['authUserPassword']) {
            errorMessage("Access denied!");
        } else {
            if ($phlmonConfig['useSessions'] != "no") {
                setSessionVar('isAuthenticated', true);
                setSessionVar('registeredIP', getenv("REMOTE_ADDR"));
                setSessionVar('isAdmin', false);
            } else {
                setSessionVar('isAuthenticated', true);
                setSessionVar('secureHash', md5(getenv("REMOTE_ADDR").$phlmonConfig['authUserPassword']));
                setSessionVar('isAdmin', false);
            }
        }

    } else if (!empty($phlmonConfig['authUser']) && 
            !empty($phlmonConfig['authUserPassword']) && 
            !empty($phlmonConfig['authAdmin']) && 
            !empty($phlmonConfig['authAdminPassword'])) {

        if (!isset($_POST['login'])) {
            loginPage();
        } else if ($_POST['login'] == $phlmonConfig['authUser'] && $_POST['password'] != $phlmonConfig['authUserPassword']) {
            errorMessage("Access denied!");
        } else if ($_POST['login'] == $phlmonConfig['authAdmin'] && $_POST['password'] != $phlmonConfig['authAdminPassword']) {
            errorMessage("Access denied!");
        } else if ($_POST['login'] == $phlmonConfig['authUser'] && $_POST['password'] == $phlmonConfig['authUserPassword']) {
            if ($phlmonConfig['useSessions'] != "no") {
                setSessionVar('isAuthenticated', true);
                setSessionVar('registeredIP', getenv("REMOTE_ADDR"));
                setSessionVar('isAdmin', false);
            } else {
                setSessionVar('isAuthenticated', true);
                setSessionVar('secureHash', md5(getenv("REMOTE_ADDR").$phlmonConfig['authUserPassword']));
                setSessionVar('isAdmin', false);
            }
        } else if ($_POST['login'] = $phlmonConfig['authAdmin'] && $_POST['password'] == $phlmonConfig['authAdminPassword']) {
            if ($phlmonConfig['useSessions'] != "no") {
                setSessionVar('isAuthenticated', true);
                setSessionVar('registeredIP', getenv("REMOTE_ADDR"));
                setSessionVar('isAdmin', true);
            } else {
                setSessionVar('isAuthenticated', true);
                setSessionVar('secureHash', md5(getenv("REMOTE_ADDR").$phlmonConfig['authAdminPassword']));
                setSessionVar('isAdmin', true);
            }
        } else if (!empty($_POST['login']) && 
                ($_POST['login'] != $phlmonConfig['authAdmin'] && $_POST['login'] != $phlmonConfig['authUser'])) {
            errorMessage("Access denied!");
        } else {
            loginPage();
        }
    }    
}

?>
Return current item: PHP Half-Life Monitor