<?php
#############################################################################
# A simple PHP/MySQL guest book
#  written by Jim Auldridge
#
# Obtained for free at http://www.christian-web-masters.com/
#
# Please read entire "readme" file, else do not continue!
#
#############################################################################
$openPass = fopen(".htpasswd","r");
$readPass = fread($openPass,filesize(".htpasswd"));
fclose($openPass);
if (eregi("\n",$readPass)) {
        $readPass = str_replace("\n","",$readPass);
}
if (eregi("\r",$readPass)) {
        $readPass = str_replace("\r","",$readPass);
}
$userPass = explode(":",$readPass);
if ( ($_SERVER['PHP_AUTH_USER'] !== $userPass[0]) OR (md5($_SERVER['PHP_AUTH_PW']) !== $userPass[1]) ) {
        header("WWW-Authenticate: Basic realm=\"Guest Book Administration\"");
        header("HTTP/1.0 401 Unauthorized");
        echo "<h1>Unauthorized (401)</h1>";
        echo "You have not been authorized access to this area.<br>If you believe this to be an error, please contact the administrator.\n";
        exit;
}
else {
        require_once("functions.php");

        /**
         * @return void
         * @desc Generates Administration Menu
         */
        function menu() {
                $home = "http://" . $_SERVER['HTTP_HOST'];
                echo "<table width=\"415\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">\n"
                 ." <tr>\n"
        ."  <td><img src=\"admin_head.gif\" alt=\"\" /></td>\n"
                 ." </tr>\n"
                 ." <tr>\n"
                 ."  <td align=\"center\" style=\"background-color: #98837D;height: 180px;\"><p><form action=\"admin.php\" method=\"POST\">\n"
                ."  <input type=\"hidden\" name=\"action\" value=\"delMenu\" />\n"
                ."  <img src=\"dot.gif\" border=\"0\" alt=\"\"><input type=\"submit\" value=\"Delete a Record\" style=\"width: 200px;\" />\n"
                ."  </form></p>\n\n"
                ."  <p><form action=\"admin.php\" method=\"POST\">\n"
                ."  <input type=\"hidden\" name=\"action\" value=\"cpForm\" />\n"
                ."  <img src=\"dot.gif\" border=\"0\" alt=\"\"><input type=\"submit\" value=\"Change Password\" style=\"width: 200px;\" />\n"
                ."  </form></p>\n\n"
                ."  <p><a style=\"font-family: arial;color: #ffffff;text-decoration: underline;\" href=\"gbook.php\">>Return to GuestBook</a></p>\n\n"
                ."  <p><a style=\"font-family: arial;color: #ffffff;text-decoration: underline;\" href=\"$home\">>Return Home</a></p>\n\n</td>\n"
                ." </tr>\n"
                ." <tr>\n"
                  ."  <td width=\"100%\" colspan=\"2\"><img src=\"foot.gif\" alt=\"\" /></td>\n"
                 ." </tr>\n"
                ."</table>\n";
        }
        
        /**
         * @return void
         * @desc Generates list of records with an option to delete them
         */
        function delMenu() {
                //require_once("functions.php");
                echo "<table width=\"415\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">\n"
                 ." <tr>\n"
        ."  <td><img src=\"de_head.gif\" alt=\"\" /></td>\n"
                 ." </tr>\n"
                 ." <tr>\n"
                 ."  <td align=\"center\" style=\"background-color: #98837D;height: 30px;\"><p style=\"font-family: arial;color: #ffffff;\">Select a record to delete:</p></td>\n"
                 ." </tr>\n"
                ." <tr>\n"
                  ."  <td width=\"100%\" colspan=\"2\"><img src=\"foot.gif\" alt=\"\" /></td>\n"
                 ." </tr>\n"
                ."</table>\n";                 
                viewAll("yes");
        }
        
        /**
         * @return void
         * @param id unknown
         * @desc Performs requested record deletion
         */
        function delete($id) {
                //require_once("functions.php");
                $delete = new MySQL_class();
                $delete->Connect("");
                $delete->Query("SELECT * FROM gBook where id = $id");
                menu();
                if ($delete->a_rows == 1) {
                        $delete->Delete("DELETE FROM gBook WHERE id = $id");
                        echo "<br /><img src=\"dot_w.gif\" alt=\"\" height=\"18\" width=\"45\" border=\"0\">$delete->a_rows record(s) deleted.\n";        
                }
                elseif ($delete->a_rows > 1) {
                        echo "<br /><img src=\"dot_w.gif\" alt=\"\" height=\"18\" width=\"45\" border=\"0\">Error:  multiple rows with same ID.  Can not delete";
                }
                else {
                        echo "<br /><img src=\"dot_w.gif\" alt=\"\" height=\"18\" width=\"45\" border=\"0\">No row found to delete with the id: $id.";
                }
        }
        
        /**
         * @return void
         * @desc Generates form for User/Pass change request
         */
        function cpForm() {
                echo "<table width=\"415\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">\n"
                 ." <tr>\n"
        ."  <td><img src=\"cp_head.gif\" alt=\"\" /></td>\n"
                 ." </tr>\n"
                 ." <tr>\n"
                 ."  <td align=\"center\" style=\"background-color: #98837D;height: 200px;\">\n"
                ."  <form action=\"admin.php\" method=\"POST\">\n"
                ."  <input type=\"hidden\" name=\"action\" value=\"passChange\" />\n"
                ."  <p><b>New Username:&nbsp;&nbsp;</b><input type=\"text\" name=\"newU\" maxlength=\"20\" /><br /><small>*if not changing username, be sure to type old username</small></p>\n"
                ."  <p><b>New Password:&nbsp;&nbsp;</b><input type=\"password\" name=\"newP\" maxlength=\"20\" /><br /><small>*if not changing password, be sure to type old password</small></p>\n"
                ."  <p><b>Verify Password:&nbsp;&nbsp;</b><input type=\"password\" name=\"VnewP\" maxlength=\"20\" /></p>\n"
                ."  <p><input type=\"submit\" value=\"Submit\" />\n  </form></p></td>\n"
                ." </tr>\n"
                ." <tr>\n"
                  ."  <td width=\"100%\" colspan=\"2\"><img src=\"foot.gif\" alt=\"\" /></td>\n"
                 ." </tr>\n"
                ."</table>\n";
        }
        
        /**
         * @return void
         * @param U unknown
         * @param P unknown
         * @param V unknown
         * @desc Makes the requested User/Password change after validating info
         */
        function changePass($U,$P,$V) {
                if ( isset($U) AND $U !== "" AND !ereg(" ",$U) AND strlen($U) <= 20 AND
                        isset($P) AND $P !== "" AND !ereg(" ",$P) AND strlen($P) <= 20 AND
                        isset($V) AND $V !== "" AND !ereg(" ",$V) AND  strlen($V) <= 20
                   ) {
                    if ($V !== $P) {
                              die('<h3 style="color: red;">Your password entries do not match</h3>');
                    }
                    else {
                                if ($openPass = fopen(".htpasswd","w")) {
                            $newUserPass = $U . ":" . md5($P);
                              fwrite($openPass,$newUserPass);
                              fclose($openPass);
                                echo "<table width=\"415\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">\n"
                                 ." <tr>\n"
                        ."  <td><img src=\"cp_head.gif\" alt=\"\" /></td>\n"
                                 ." </tr>\n"
                                 ." <tr>\n"
                                 ."  <td align=\"center\" style=\"background-color: #98837D;height: 30px;\"><p style=\"font-family: arial;color: #ffffff;\">\n" 
                                ."  User/Password updated...please wait</p>\n  <script type=\"text/javascript\">\n    var t = 2000\n    setTimeout(\"window.location='" . $_SERVER['HTTP_REFERER'] . "'\" , t);\n  </script></td>\n"
                                ." </tr>\n"
                                ." <tr>\n"
                                  ."  <td width=\"100%\" colspan=\"2\"><img src=\"foot.gif\" alt=\"\" /></td>\n"
                                 ." </tr>\n"
                                ."</table>\n";
                            }
                            else {
                                    die('<h3 style="color: red;">Failed to open file.  Please see step 5 in the "User Installation/Configuration" portion of the "readme" file</h3>');
                                }
                        }
                }
                  else {
                    die('<h3>Invalid or No data posted</h3>');
                  }
        }

        if (isset($_POST['action'])) {
                switch ($_POST['action']) {
                        case delMenu:
                        delMenu();
                        break;
                        
                        case del:
                        delete($_POST['id']);
                        break;
                        
                        case cpForm:
                        cpForm();
                        break;
                                
                        case passChange:
                        changePass($_POST['newU'],$_POST['newP'],$_POST['VnewP']);
                        break;
                }
        }
        else {
                menu();
        }        
}                                
?>
