<script language="php">
#
# Get the required action, setting the default as needed
#
if (empty($_POST['action']))
{
     $Action = 'List';
}
else
{
    $Action = $_POST['action'];
}

#
# If the Exit button was selected, do it.
#
if ($Action == 'Exit')
{
    header('Location: //zoogz/linux_index.html');
    exit;
}

#
# In all other cases, output the page headers.
#
printf("<html>\n");
printf("<head>\n");
printf("<title>Quarantined Spam Inspector</title>\n");
printf("</head>\n");
printf("<body>\n");
printf("<h1>Quarantined Spam Inspector</h1>\n");

#
# Define constants
#
    $self = $_SERVER['PHP_SELF'];
    $filestore = "/home/getmail/quarantined";

#
# If the View button was clicked, show the selected spam message
# to the user.
#  
if ($Action == 'View')
{
    printf("<form action='%s' method='POST'>\n", $self);
    printf("<input type='submit' name='action' value='List'>\n");
    printf("<input type='submit' name='action' value='Exit'>\n");
    if (array_key_exists('spam', $_POST))
    {
        $bname = $_POST['spam'];
        $fileref = $filestore . '/' . $bname;
        $content = file_get_contents($fileref);
        printf("<h2>%s</h2>\n", $bname);
        printf("<textarea %s %s %s %s>\n",
               "name='content'",
               "rows='25'",
               "cols='100'",
               "readonly='readonly'");
        print($content);
        printf("</textarea>\n");
        printf("<br><br>\n");
    }
    else
    {
        printf("<h2>No spam was selected</h2>");
    }

    printf("<input type='submit' name='action' value='List'>\n");
    printf("<input type='submit' name='action' value='Exit'>\n");
    printf("</form>\n");
}
else
{
    #
    # See if any quarantined spam exists and list the items if so.
    # Each file is accompanied by a selection check box.
    #
    $filecount = 0;
    $filelist = array();
    if ($d = opendir($filestore))
    {
        while ($ent = readdir($d))
        {
            if (!($ent == '.' || $ent == '..'))
            {
                $filecount++;
                $filelist[] = $ent;
            }
        }
        
        closedir($d);
        natcasesort($filelist);
    }

    printf("<form action='%s' method='POST'>\n", $self);
    if ($filecount > 10)
    {
        printf("<input type='submit' name='action' value='View'>\n");
        printf("<input type='submit' name='action' value='Exit'>\n");
        printf("<p></p>\n");
    }
    
    if ($filecount > 0)
    {
        printf("<form action='%s' method='POST'>\n", $self);
        printf("<table summary='File list' border='1'>\n");
        printf("<tr><td><b>%s</b></td><td><b>%s</b></td></tr>\n",
               "Select",
               "Spam id");
        
        foreach ($filelist as $f)
        {
            printf("<tr><td>%s%s%s</td><td>%s</td></tr>\n",
                   "<input type='radio' name='spam' value='",
                   $f,
                   "'>",
                   $f);
        }

        printf("</table>\n");
        printf("<p></p>\n");
        printf("<input type='submit' name='action' value='View'>\n");
        printf("<input type='submit' name='action' value='Exit'>\n");
        printf("</form>\n");
    }
}
</script>
</body>
</html>
