<?php

// Returns login ID of current user (authentificated)
// or 0.
function current_user()
{
       //TODO: do we need extra connect here?
       include "db_connect.inc";

       if(!array_key_exists("OBECUSER", $GLOBALS["HTTP_COOKIE_VARS"]) ||
          !array_key_exists("OBECPASS", $GLOBALS["HTTP_COOKIE_VARS"]))
       {
           return 0;
       }

       $login = $GLOBALS["HTTP_COOKIE_VARS"]["OBECUSER"];
       $pass  = $GLOBALS["HTTP_COOKIE_VARS"]["OBECPASS"];
       if(!$login || !$pass)
       {
            return 0;
       }

       $result = mysql_db_query($db_database,
                             "SELECT Password FROM Logins WHERE Login='" . addslashes($login) . "'", $link);
       $row = mysql_fetch_array($result);
       $realpass = trim($row[0]);
       mysql_free_result($result);

       if($pass == md5($realpass))
       {
                return $login;  
       }
       else
       {
               return 0;
       }
}

function show_header()
{
    $user=current_user();
    if($user)
    {
        print("<CENTER><FONT size=3 color=\"#660000\">" . htmlspecialchars($user) . ", Добро пожаловать на OBEC-сервер!</FONT><BR/><DIV><SCRIPT TYPE=\"text/javascript\" SRC=\"http://www.crocodile.org/linknet/adblock.php?lang=ru&charset=koi8-r\"></SCRIPT></DIV></CENTER>");
    } else
    {
        print("<CENTER><FONT size=3 color=\"#660000\">Добро пожаловать на OBEC-сервер!</FONT><BR/><DIV><SCRIPT TYPE=\"text/javascript\" SRC=\"http://www.crocodile.org/linknet/adblock.php?lang=ru&charset=koi8-r\"></SCRIPT></DIV></CENTER>");
    }
    print("<HR>");
}

function show_footer()
{
    print("<HR>");
    print("<CENTER>");
    print("<TABLE BORDER=\"0\" CELLSPACING=\"5\" CELLPADDING=\"0\">");
        print("<TR>");

            print("<TD>");
            print("<A HREF=\"index.php\" TARGET=_top><b>[Главная Страница]</b></A>");
            print("</TD>");

            print("<TD>");
            print("<A HREF=\"texts.php\" TARGET=_top><b>[Работы]</b></A>");
            print("</TD>");

            print("<TD>");
            print("<A HREF=\"authors.php\" TARGET=_top><b>[Авторы]</b></A>");
            print("</TD>");

            print("<TD>");
            print("<A HREF=\"threads.php\" TARGET=_top><b>[Комментарии]</b></A>");
            print("</TD>");

        print("</TR>");
    print("</TABLE>");
    print("</CENTER></P>");
}

function current_URL()
{
        $p = $GLOBALS["SERVER_PORT"]?$GLOBALS["SERVER_PORT"]:80;
        return "http://".$GLOBALS["SERVER_NAME"] . ($p!=80?(":".$p):"") . $GLOBALS["PHP_SELF"];
}

?>