<?php

// Use PHP session management.
session_start ( ) ;

// Load the database functions.
require "database.php" ;

// Display a document if requested to do so.
if ( $selection = $_REQUEST['selection'] ) {
    
my_export ( $selection  ) ; exit ; }

// Otherwise display the basic interface.
?>

<html>
  <head>
    <title>
      Memories Incorporated
    </title>
    <script language="JavaScript" src="java/timestamp_picker.js"></script>
  </head>
  <body>
    <div style="background-color: blue; color: white">
      <table width="100%">
        <tr>
          <td>
            <div style="color: white; text-align: center">
              <div>
                <span style="font-size: large">
                  Memories Incorporated
                </span>
                <span>
                   powered by
                </span>
                <span style="font-size: large">
                  PHP and PostgreSQL
                </span>
              </div>
            </div>
          </td>
        </tr>
      </table>
    </div>
    <div style="border-color: blue; border-style: solid">
      <table width="100%">
        <tr style="text-align: center">
          <td>
            <a href="page.php?action=upload">
              Upload File
            </a>
          </td>
          <td>
            <a href="page.php?action=search">
              Search
            </a>
          </td>
          <td>
            <a href="page.php?action=about">
              About
            </a>
          </td>
          <td>
           <?php
           
if ( ! isset ( $_SESSION['user'] ) ) {
             if (
$_REQUEST['action'] != "checklogin" ) {
               
$_REQUEST['action'] = "login" ;
             }
             echo
'<a href="page.php?action=login">Log In</a>' ;
           } else {
             if (
$_REQUEST['action'] == "logout" ) {
               echo
'<a href="page.php?action=login">Log In</a>' ;
             } else {
               echo
'<a href="page.php?action=logout">Log Out</a>' ;
             }
           }
           
?>
          </td>
        </tr>
      </table>
    </div>
    <div style="padding: 4ex 8ex 4ex 8ex">
    <?php
    
switch ( $_REQUEST['action'] ) {
    case     
"about"  : about_body ( ) ; break ;
    case     
"login"  : login_body ( ) ; break ;
    case
"checklogin" : check_login ( ) ; break ;
    case     
"logout" : logout_body ( ) ; break ;
    case     
"failed" : failed_login_body ( ) ; break ;
    case     
"search" : search_body ( ) ; break ;
    case    
"listing" : listing_body ( ) ; break ;
    case    
"display" : display_listing ( ) ; break ;
    case     
"upload" : upload_body ( ) ; break ;
    case     
"insert" : insert_body ( ) ; break ;
    default           :
login_body ( ) ; break ;
    }
    
?>
    </div>
    <div style="background-color: blue; color: white; font-weight: bold">
     <?php
     
if ( !isset($_SESSION['user']) ) {
        echo
"<span>Not logged in</span>" ;
     } else {
        echo
"<span>Logged in as <em>" . $_SESSION['user'] . "</em></span>" ;
     }
     
?>
    </div>
  </body>
</html>

<?php

function login_body ( ) {
  echo
'<div>' ;
  echo
'  <h2>' ;
  echo
'    Log into your account:' ;
  echo
'  </h2>' ;
  echo
'  <form action="page.php" method="post">' ;
  echo
'    <input type="hidden" name="action" value="checklogin"></input>' ;
  echo
'    <table>' ;
  echo
'      <tr>' ;
  echo
'        <td>Username</td>' ;
  echo
'        <td><input type="text" name="username"></input></td>' ;
  echo
'      </tr>' ;
  echo
'      <tr>' ;
  echo
'        <td>Password</td>' ;
  echo
'        <td><input type="password" name="password"></input></td>' ;
  echo
'      </tr>' ;
  echo
'      <tr>' ;
  echo
'        <td>' ;
  echo
'          <input type="submit" name="submit" value="Log in"></input>' ;
  echo
'        </td>' ;
  echo
'      </tr>' ;
  echo
'    </table>' ;
  echo
'  </form>' ;
  echo
'</div>' ;
}

function
check_login ( ) {
  if (
valid_login ( ) ) {
    
upload_body ( ) ;
  } else {
    
failed_login_body ( ) ; }
}

function
logout_body ( ) {
  unset(
$_SESSION['user']) ;
  echo
'<div>' ;
  echo
'  <h2>' ;
  echo
'    Logged out.' ;
  echo
'  </h2>' ;
  echo
'</div>' ;
}

function
failed_login_body ( ) {
  echo
'<div>' ;
  echo
'  <h2>' ;
  echo
'    Login failed!' ;
  echo
'  </h2>' ;
  echo
'</div>' ;
}

function
search_body ( ) {
  echo
'<div>' ;
  echo
'  <h2>' ;
  echo
'    Search for documents in the database:' ;
  echo
'  </h2>' ;
  echo
'  <form name="search" action="page.php" method="get">' ;
  echo
'    <input type="hidden" name="action" value="listing"></input>' ;
  echo
'    <div>' ;
  echo
'      <p>' ;
  echo
'        Enter search keywords: ' ;
  echo
'        <br>' ;
  echo
'        <input name="searchstring" type="text" size="48" value=""></input>' ;
  echo
'      </p>' ;
  echo
'      <p>' ;
  echo
'        Start of the search interval: ' ;
  echo
'        <br>' ;
  echo
'        <input name="begin" type="text" size="32" value=""></input>' ;
  echo
'        <a href="javascript:show_calendar(\'document.search.begin\',  ' ;
  echo
'                                           document.search.begin.value)">' ;
  echo
'          <img src="images/cal.gif" width="16" height="16" border="0" ' ;
  echo
'               alt="Click here to pick the start of search interval">' ;
  echo
'        </a>' ;
  echo
'      </p>' ;
  echo
'      <p>' ;
  echo
'        End of the search interval: ' ;
  echo
'        <br>' ;
  echo
'        <input name="end" type="text" size="32" value=""></input>' ;
  echo
'        <a href="javascript:show_calendar(\'document.search.end\',  ' ;
  echo
'                                           document.search.end.value)">' ;
  echo
'          <img src="images/cal.gif" width="16" height="16" border="0" ' ;
  echo
'               alt="Click here to pick the end of search interval">' ;
  echo
'        </a>' ;
  echo
'      </p>' ;
  echo
'      <p>' ;
  echo
'        <input name="submitsearch" type="submit" value="Search"></input>' ;
  echo
'      </p>' ;
  echo
'    </div>' ;
  echo
'  </form>' ;
  echo
'</div>' ;
}

function
upload_body ( ) {
  echo
'<div>' ;
  echo
'  <h2>' ;
  echo
'    Add a document to the database:' ;
  echo
'  </h2>' ;
  echo
'  <form action="page.php" enctype="multipart/form-data" method="post">' ;
  echo
'    <input type="hidden" name="action" value="insert"></input>' ;
  echo
'    Select file: ' ;
  echo
'    <div>' ;
  echo
'      <input name="file" type="file" size="32" maxlength="100000"></input>' ;
  echo
'    </div>' ;
  echo
'    <br>' ;
  echo
'    Description: ' ;
  echo
'    <div>' ;
  echo
'      <textarea name="description" cols="64" rows="4" ' ;
  echo
'                wrap="soft" value=""></textarea>' ;
  echo
'    </div>' ;
  echo
'    <div>' ;
  echo
'      <input type="submit" value="Upload file"></input>' ;
  echo
'    </div>' ;
  echo
'  </form>' ;
  echo
'</div>' ;
}

function
insert_body ( ) {
  
my_import ( ) ;
  
upload_body ( ) ;
}

function
about_body ( ) {
  echo
'<div>' ;
  echo
'  <h2>' ;
  echo
'    Information about this project:' ;
  echo
'  </h2>' ;
  echo
'  <p>' ;
  echo
'    This experimental document management system ' ;
  echo
'    is designed to provide a framework for testing ' ;
  echo
'    new information retrieval techniques.' ;
  echo
'  </p>' ;
  echo
'</div>' ;
}

function
failed_upload_body ( ) {
  echo
'<div>' ;
  echo
'  <h2>' ;
  echo
'    The system currently only handles HTML files.' ;
  echo
'  </h2>' ;
  echo
'</div>' ;
}

function
failed_display_body ( ) {
  echo
'<div>' ;
  echo
'  <h2>' ;
  echo
'    No keywords!' ;
  echo
'  </h2>' ;
  echo
'</div>' ;
}

function
listing_body ( ) {
  
$searchstring = $_REQUEST['searchstring'] ;
  if (
preg_match ( "/^\s*$/", $searchstring ) ) {
    
failed_display_body ( ) ;
  } else {
    echo
'<form action="page.php" method="post">' ;
    echo
'  <input type="hidden" name="action" value="display"></input>' ;
    echo
'  <table border="1" cellpadding="5" cellspacing="2">' ;
    echo
'    <tr style="text-align: left">' ;
    echo
'      <th>View:</th>' ;
    echo
'      <th>Date:</th>' ;
    echo
'      <th>Description:</th>' ;
    echo
'      <th><input type="submit" name="score" value="update"></input></th>' ;
    echo
'      <th>Score:</th>' ;
    echo
'    </tr>' ;
    
// Query the database and construct table items.
    
listing_contents ( $searchstring ) ;
    
// Remember these search parameters for next time.
    
$_SESSION['searchstring'] = $_REQUEST['searchstring'] ;
    
$_SESSION['begin'] = $_REQUEST['begin'] ;
    
$_SESSION['end'] = $_REQUEST['end'] ;
    echo
'  </table>' ;
    echo
'</form>' ;
  }
}

function
display_listing ( ) {
  
// Update the scoring function for ranking results.
  
update_scoring_function ( ) ;
  
// Restore the search parameters from last time.
  
$_REQUEST['searchstring'] = $_SESSION['searchstring'] ;
  
$_REQUEST['begin'] = $_SESSION['begin'] ;
  
$_REQUEST['end'] = $_SESSION['end'] ;
  
// Redisplay the results with new scoring function.
  
listing_body ( ) ;
}

function
listing_item ( $id, $mark, $description, $score ) {
  echo
'<tr style="text-align: left">' ;
  echo
'  <td>' ;
  echo
'    <input type="submit" name="selection" value="' . $id . '"></input>' ;
  echo
'  </td>' ;
  echo
'  <td>' . $mark . '</td>' ;
  echo
'  <td>' . $description . '</td>' ;
  echo
'  <td><input type="checkbox" ' ;
  echo
'             name="OID-' . $id . '"'  ;
  echo
'             value="' . $id . '"></input>' ;
  echo
'  </td>' ;
  echo
'  <td>' . $score . '</td>' ;
  echo
'</tr>' ;
}

?>