<?php

#$AUTHD_USERNAMES = array(
#  "davids",
#  "marlenes",
#   );
#
#$IMAP_HOST = "sowder.com";
#$IMAP_PORT = 993;
##$IMAP_PROTOCOL = "imap/ssl/validate-cert";  #**FIXME** This needs the sowder.com CA to be trusted by PHP's cert store (which I haven't looked up the location of yet)
#$IMAP_PROTOCOL = "imap/ssl/novalidate-cert";

function html_footer() {
  echo " </body>\n";
  echo "</html>\n";
}

function html_header( $title = "v.xzwq.net", $head_extras = '', $body_attribs = '' ) {
  if( !empty( $body_attribs ) and ' ' != substr( $body_attribs, 0, 1 )) {
    $body_attribs = ' ' . $body_attribs;
  }
  echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n";
  echo "<html>\n";
  echo " <head>\n";
  echo "  <title>${title}</title>\n";
  echo "  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">";
  echo "  <meta http-equiv=\"expires\" content=\"0\">";
  echo "  <meta http-equiv=\"pragma\" content=\"no-cache\">";
  if( !empty( $head_extras )) {
    echo "${head_extras}\n";
  }
  echo " </head>\n";
  echo " <body${body_attribs}>\n";
  echo "  <a name=\"pagetop\"></a>\n";
}

# Disallow any client/proxy caching
session_cache_limiter('nocache');

# Set the cache expire to 30 minutes
session_cache_expire(30);

# Start the session (or resume the session)
session_start();

## Is IMAP even supported by the server?
#if( !function_exists( "imap_open" )) { 
#  die( 'IMAP support is not available on the server.  Bailing.' );
#}

$script = $_SERVER[ 'SCRIPT_NAME' ];
$script_basename = basename( $script );
if( "index.php" == $script_basename ) {
  $script = dirname( $script ) . '/';
}

## Ensure that we're connected with SSL
#if( empty( $_SERVER[ 'HTTPS' ] )) {
#  if( !empty( $_SERVER[ 'HTTP_HOST' ] )) {
#    header( "Location: https://{$_SERVER[ 'HTTP_HOST' ]}${script}" );
#    exit;
#  } else {
#    die( 'Must connect with https://' );
#  }
#}

$user_message = '';
$username = '';

## If we have a logout request, then log out the user
#if( !empty( $_SESSION[ 'username' ] ) and isset( $_REQUEST[ 'logout' ] )) {
#  $username = $_SESSION[ 'username' ];
#  session_unset();
#  session_destroy();
#  $user_message = "'${username}' logged out.";
#}
#
## If we do not have a valid session and we have a username and password passed in a form POST, use them
#if( empty( $_SESSION[ 'username' ] ) and !empty( $_POST[ 'username' ] ) and isset( $_POST[ 'password' ] )) {
#  $username = $_POST[ 'username' ];
#  $password = $_POST[ 'password' ];
#  if( 1 == get_magic_quotes_gpc()) {
#    $password = stripslashes( $password );
#  }
#  if( empty( $password )) {
#    $user_message = "Please supply a password.";
#  } else {
#    $imap_server_mailbox = '{' . $IMAP_HOST . ':' . $IMAP_PORT . '/' . $IMAP_PROTOCOL . '}INBOX';
#    #echo "DEBUG: imap_server_mailbox: [${imap_server_mailbox}]<br>\n";
#    @$imap_open_result = imap_open( $imap_server_mailbox, $username, $password, OP_HALFOPEN );
#    if( FALSE === $imap_open_result ) {
#      $user_message = "Failed to connect/authentication to/with IMAP server.  Try again later.";
#    } else {
#      $_SESSION[ 'username' ] = $username;
#      header( "Location: ${script}" );
#      exit;
#    }
#  }
#}
#
## If we still do not have a valid session, prompt the user for their credentials
#if( empty( $_SESSION[ 'username' ] )) {
#  session_write_close();
#  html_header();
#  if( !empty( $user_message )) {
#    echo "<span style=\"color: #ff0000;\">${user_message}</span><br>\n";
#  }
#?><!--
#  <form method="post" name="loginform" action="<?php echo $script; ?>">
#   <fieldset>
#    Enter your credentials:<br>
#    <b>IMAP Username:</b><br>
#    <input type="text" size="15" name="username" value="<?php echo $username; ?>"><br>
#    <b>Password:</b><br>
#    <input type="password" size="15" name="password"><br>
#    <input type="submit" name="submit">
#   </fieldset>             
#  </form>
#--><?php
#  if( empty( $username )) { 
#    echo "  <script type=\"text/javascript\">\n";
#    echo "   document.body.onLoad = document.loginform.username.focus();\n";
#    echo "  </script>\n";
#  } else {
#    echo "  <script type=\"text/javascript\">\n";
#    echo "   document.body.onLoad = document.loginform.password.focus();\n";
#    echo "  </script>\n";
#  }
#  html_footer();
#  exit;
#}
#
## Do we have an authorized user?
#if( !in_array( $_SESSION[ 'username' ], $AUTHD_USERNAMES )) {
#  session_unset();
#  session_destroy();
#  html_header();
#  echo "User not authorized.  <a href=\"${script}\">Login as another user</a>.<br>\n";
#  html_footer();
#  exit;
#}

$message_unlocked = FALSE;

$l = "";
if( isset( $_GET[ "l" ] )) {
    $l = $_GET[ "l" ];
}

if( !isset( $_SESSION[ "state" ] )) {
    $_SESSION[ "state" ] = 0;
}
if( 0 == $_SESSION[ "state" ] and "i" == $l ) {
    $_SESSION[ "state" ] = 1;
    $user_message = "input noted";
} else if( 1 == $_SESSION[ "state" ] and "h" == $l ) {
    $_SESSION[ "state" ] = 2;
    $user_message = "input noted";
} else if( 2 == $_SESSION[ "state" ] and "u" == $l ) {
    $message_unlocked = TRUE;
    $_SESSION[ "state" ] = 0;
} else {
    $_SESSION[ "state" ] = 0;
    $user_message = "input unexpected";
}

session_write_close();

$header_style = "  <style type=\"text/css\">\n   .sectionhead {\n    font-size: 115%;\n    font-weight: bold;\n   }\n   </style>";
#html_header( $title = "lunch links for davids", $head_extras = $header_style );
#html_header( $title = "lunch links for davids" );
html_header( $title = "v.xzwq.net" );
#echo "  <div style=\"padding-bottom: 1%;\">\n";
#echo "   <div style=\"font-size: 60%; float: left;\">\n";
#echo "    <a href=\"http://eosunix.uta.david.sowder.com/\">ARDC Area Wheather</a> (brought to you by \"fish/cat\")<br>\n";
#echo "   </div>\n";
#echo "   <div style=\"font-size: 60%; float: right; text-align: right;\">\n";
##echo "    Logged in as '{$_SESSION[ 'username' ]}' | <a href=\"${script}?logout=1\">Logout</a><br>\n";
#echo "    &nbsp;<br>\n";
#echo "   </div>\n";
#echo "  </div>\n";
if( !empty( $user_message )) {
    echo "<div style=\"font-size: 125%; text-align: center;\"><span style=\"color: #000000; \">${user_message}</span></div>\n";
} else if( !$message_unlocked ) {
    echo "Huh?  Type 'help' for help (though it might not do much good).<br>\n";
}

if( $message_unlocked ) {
    echo "<br>\n";
    echo "Marlene,<br>\n";
    echo "<br>\n";
    echo "(Sandy didn't seem to want to let me record her singing a saranade to you in the background here.  'course, on the other hand, I guess I didn't really ask her all that directly either.  She did purr though, of course.)  I love you very, very much.  Will you be my valentine?<br>\n";
    echo "<br>\n";
    echo "Yours,<br>\n";
    echo "David<br>\n";
}

# links here

#$nav_links = array(
#    "apache_web_tomcat" => "Apache/Web/Tomcat",
#    "mavspace" => "Mavspace",
#    "other" => "Other",
#    "rrd" => "RRD",
#    "albums" => "Albums",
#    "hobbit" => "Hobbit",
#    "dracs" => "DRACs"
#    );

function output_nav_bar( $current_section ) {
  global $nav_links;
  
  $nav_bar_array = array( "  <a href=\"#pagetop\">Top</a>" );
  foreach( $nav_links as $key => $value ) {
    if( $current_section == $key ) {
      $nav_bar_array[] = "  ${value}";
    }
    else {
      $nav_bar_array[] = "  <a href=\"#${key}\">${value}</a>";
    }
  }
  $nav_bar_str = implode( " |\n", $nav_bar_array );
  echo $nav_bar_str;
  echo "  <hr>";
}

html_footer();

?>
