<?php
#
# Copyright 2003 James Seng. See LICENSE for the Software License.
#

function showmsg($type,$str) {
    print <<<_SHOWMSG_
<center>
<font class="$type">$str</font>
</center>
_SHOWMSG_;
}

function showlogin($userid = "") {
    print <<< _LOGIN_
<center>
<form method="POST" action="?q=login">
<table class=table cellpadding="5" width="50%">
<tr>
  <td class="title" colspan="2" align="center">
    PONG Login
    </td>
  </tr>
<tr>
  <td align="right" width="45%">
    Username</td>
  <td align="left" width="55%">
    <input class="customize" name="_userid" value="$userid"></td>
  </tr>
<tr>
  <td align="right" width="45%">
    Password</td>
  <td align="left" width="55%">
    <input class="customize" name="_passwd" type="password"></td>
  </tr>
<tr>
  <td colspan="2" align="center">
    <input class="button" type="submit" name="submit" value="login"></td>
  </tr>
</table>
</form>
</center>

_LOGIN_;
}

function cmd_login() {
    global $config;

    if (!isset($_REQUEST['submit'])) {
        showlogin();
        return;
    }

    $userid = $_REQUEST['_userid'];
    $passwd = $_REQUEST['_passwd'];

    if ($config->get('userid') != $userid 
    ||  $config->get('passwd') != crypt($passwd,$config->get('passwd'))) {
        showmsg("error","Wrong userid or password.");
        showlogin($userid);
        return;
    }

    $_SESSION['login'] = time();

    cmd_default();

    return;
}

function cmd_logout() {
    unset($_SESSION['login']);
    showlogin();
}

function cmd_default() {
    global $module_tree;

    print <<< _DEFAULT_
<center>
<table class=table cellpadding="5" width="60%">
<tr>
  <td class="title" colspan="2" align="center">
    PONG Menu
    </td>
  </tr>
_DEFAULT_;

    $module_tree->module_invoke_sub("function_base","print_menu","long");

    print <<< _DEFAULT2_
<tr>
  <td align="right" width="25%" valign="top">
    <a href="?q=show_config">Edit Configuration</a></td>
  <td align="left" width="75%" valign="top">
    Edit PonG Configuration. You can also change your userid and password
    here.
    </td>
  </tr>
</table>
</center>
_DEFAULT2_;
}

?>
