";
/**
* Function to modify the guestbook to approve the entry supplied. If no entry
* is supplied then the function returns false. The function also returns false
* if the guestbook cannot be written to.
*
* @param entry - String representing the guestbook entry to be modified
* @return boolean - true if guestbook successfully modified else false
*/
function approveEntry( $entry ) {
global $GUESTBOOK, $DELIMITER;
if ( empty( $entry )) {
return false;
}
$fields = explode( $DELIMITER, $entry );
$fields[6] = "1";
$approvedEntry = implode( $DELIMITER, $fields );
$guestbook = file_get_contents( $GUESTBOOK );
$updatedGuestbook = str_replace($entry, $approvedEntry, $guestbook);
$file_handle = fopen( $GUESTBOOK, 'w+');
$bytesWritten = fwrite( $file_handle, $updatedGuestbook );
if ( $bytesWritten ) {
fclose( $file_handle );
return true;
} else {
fclose( $file_handle );
return false;
}
}
/**
* Function to verify the guestbook entry to be approved and to modify the
* guestbook for that entry. The function displays a user message to indicate
* the modification of the guestbook
*
* @param void - no parameters are required
* @return null - no values are returned
*/
function approveGuestEntry() {
$session = getSessionID();
if ( empty( $session )) {
displayGuestApproveFailure();
return;
}
$entry = getEntry( $session );
if ( approveEntry( $entry )) {
displayGuestApproveSuccess();
return;
} else {
displayGuestApproveFailure();
return;
}
}
/**
* Function to modify the guestbook to confirm the entry supplied. If no entry
* is supplied then the function returns false. The function also returns false
* if the guestbook cannot be written to.
*
* @param entry - String representing the guestbook entry to be modified
* @return boolean - true if guestbook successfully modified else false
*/
function confirmEntry( $entry ) {
global $GUESTBOOK, $DELIMITER;
if ( empty( $entry )) {
return false;
}
$fields = explode( $DELIMITER, $entry );
$fields[8] = "1";
$confirmedEntry = implode( $DELIMITER, $fields );
$guestbook = file_get_contents( $GUESTBOOK );
$updatedGuestbook = str_replace($entry, $confirmedEntry, $guestbook);
$file_handle = fopen( $GUESTBOOK, 'w+');
$bytesWritten = fwrite( $file_handle, $updatedGuestbook );
if ( $bytesWritten ) {
fclose( $file_handle );
return true;
} else {
fclose( $file_handle );
return false;
}
}
/**
* Function to verify the guestbook entry to be confirmed and to modify the
* guestbook for that entry. The function displays a user message to indicate
* the modification of the guestbook
*
* @param void - no paramaters are required
* @return null - no values are returned
*/
function confirmGuestEntry() {
global $DELIMITER;
$session = getSessionID();
if ( empty( $session )) {
displayGuestConfirmFailure();
return false;
}
$entry = getEntry( $session );
if ( confirmEntry( $entry )) {
$fields = explode( $DELIMITER, $entry );
emailAdminApproval( trim($fields[1]), trim($fields[2]), trim($fields[3]), trim($fields[4]), trim($fields[5]));
displayGuestConfirmSuccess();
return true;
} else {
displayGuestConfirmFailure();
return false;
}
}
/**
* Function to display HTML message to user upon failure to confirm a guestbook
* entry
*
* @param void - no paramaters are required
* @return null - no values are returned
*/
function displayEmailConfirmationFailure() {
print "The guestbook was unable to send a confirmation email to the address you provided.
\n";
print "This is a temporary failure. Please re-try again at a later time
\n";
print "
\n";
}
/**
* Generates the HTML code to display the guestbook entries. The entries are
* parsed from the flatfile and encoded as HTML. The email addresses listed as
* private are not displayed. Public email addresses are obfuscated to annoy
* email harvesters. The background colors of each entry is alternated between
* dark and light (specified in the CSS) to make the entries more readable
*
* @param void - no parameters are required
* @return null - no values are returned
*/
function displayEntries() {
global $NO_ENTRIES, $GUESTBOOK, $DELIMITER, $ENTRIES_DISPLAYED, $EMAIL_PRIVATE;
if ( file_exists( $GUESTBOOK )) {
$file_handle = file( $GUESTBOOK );
rsort( $file_handle );
$num_entries = count( $file_handle );
if ( $num_entries <= 0 ) {
printf("\n
\n");
$dark = 0;
} else {
printf("
\n");
$dark = 1;
}
$email = obsfuscateEmail($email);
printf("\n
Name: %s on %s
\n", $name, $date);
printf("Email: %s
\n", $email);
printf("Website: %s
\n", $url);
printf("Comments: %s
\n", $comments);
printf("
\n");
$displayed++;
}
}
}
printf("\n
%d %s
\n", $displayed, $ENTRIES_DISPLAYED);
}
} else {
printf("\n
%s
\n", $NO_ENTRIES);
}
}
/**
* Generates the HTML code to display the HTML form to accepting the guestbook
* entry. The form is used to accept the user input and process it. If any error
* messages are supplied then they are displayed at the bottom of the form as an
* error. Any supplied values that are set as defaults for the appropriate fields,
* else the application specific constants are used
*
* @param name - the text value for the name field (optional)
* @param email - the text value for the email field (optional)
* @param privateemail - the checkbox value for the privateemail checkbox (optional)
* @param url - the text value for the website field (optional)
* @param comments - the text value for the comments field (optional)
* @param errors - the text value for any error messages to be displayed (optional)
* @param session - the text value for the visitor session
* @return null - no values are returned
*/
function displayForm( $name, $email, $privateemail, $url, $comments, $errors, $session ) {
global $DEFAULT_NAME, $DEFAULT_EMAIL, $DEFAULT_EMAIL_PRIVACY, $DEFAULT_WEBSITE, $DEFAULT_COMMENTS;
$name = empty( $name ) ? $DEFAULT_NAME : trim( $name );
$email = empty( $email ) ? $DEFAULT_EMAIL : trim( $email );
$privateemail = empty( $privateemail ) ? $DEFAULT_EMAIL_PRIVACY : trim( $privateemail );
$url = empty( $url ) ? $DEFAULT_WEBSITE : trim( $url );
$comments = empty( $comments ) ? $DEFAULT_COMMENTS : trim( $comments );
$errors = trim( $errors );
$session = empty( $session ) ? getSessionID() : trim( $session );
print "
* indicates a required field
";
}
/**
* Generated the HTML code to display message to indicate a guestbook entry was
* not found, and the guestbook was not modified to approve the entry
*
* @param void - no parameters are required
* @return null - no values are returned
*/
function displayGuestApproveFailure() {
print "The guestbook entry was not found. This is a permanent error. Please contact the Guestbook administrator
\n";
print "
\n";
}
/**
* Generated the HTML code to display message to indicate a guestbook entry was
* found and the guestbook was modified to approve the entry
*
* @param void - no parameters are required
* @return null - no values are returned
*/
function displayGuestApproveSuccess() {
print "Thank you for approving the entry. The guestbook entry will not be displayed as part of the guestbook.
\n";
print "
\n";
}
/**
* Generated the HTML code to display message to indicate a guestbook entry was
* not found, and the guestbook was not modified to confirm the entry
*
* @param void - no parameters are required
* @return null - no values are returned
*/
function displayGuestConfirmFailure() {
print "The guestbook entry was not found. This is a permanent error. Please make another entry by
\n";
print "visiting the
Guestbook. If problems persist, please contact the Guestbook administrator
\n";
print "
\n";
}
/**
* Generated the HTML code to display message to indicate a guestbook entry was
* found and the guestbook was modified to confirm the entry
*
* @param void - no parameters are required
* @return null - no values are returned
*/
function displayGuestConfirmSuccess() {
print "Thank you for confirming your entry. An approval request email has been sent to the
\n";
print "Guestbook administrator. Once the administrator has approved the entry, it will be
\n";
print "displayed in the Guestbook.
\n";
print "
\n";
}
/**
* Generates the HTML code to acknowledge the signing of the guestbook
*
* @param void - no parameters are required
* @return null - no values are returned
*/
function displaySignAcknowledge() {
print "Thank you for signing the guestbook. Your entry will be displayed once it has been confirmed and approved.
\n";
print "
\n";
print "A confirmation email has been sent to the email address you submitted and requires your confirmation, once
\n";
print "it has been confirmed, the guestbook administrator's approval will be requested.
\n";
print "
\n";
print "Your requesting IP address has been logged for security reasons.
\n";
print "
\n";
}
/**
* Generates the HTML code to present the visitor the option to sign the guestbook.
*
* @param void - no parameters are required
* @return null - no values are returned
*/
function displaySignOption() {
print "Please feel free to
sign the guestbook
here\n";
}
/**
* Generates the HTML code to present the visitor the option to view the guestbook.
*
* @param void - no parameters are required
* @return null - no values are returned
*/
function displayViewOption() {
print "Please feel free to
view the guestbook
here\n";
}
/**
* Assembles the email headers and communicated with the MX server for the
* domain to send the email based on the paramaters supplied. The function
* complies witht he following RFC documents;
*
* http://www.ietf.org/rfc/rfc2821.txt
* http://www.ietf.org/rfc/rfc2822.txt
*
* @param from - text representing the email sender address
* @param namefrom - text representing the name of the sender
* @param to - text representing the email receipient address
* @param nameto - text representing the receipient name
* @param subject - text representing the subject of the email
* @param message - text representing the body of the email
* @returns boolean - true of the email is sent successfully else false
*/
function email( $from, $namefrom, $to, $nameto, $subject, $message ) {
global $SMTP_SERVER, $SMTP_PORT, $SMTP_USER, $SMTP_PASS, $SMTP_DOMAIN;
// Declare local variables
$timeout = 30;
$readLength = 998;
$mxServer = getMXserver( $to );
if ( empty ( $mxServer )) {
return false;
}
// Prepare email headers - origination date field and the originator address
// field(s) are mandatory.
$headers = "Date:" . date( 'r' ) . "\r\n";
$headers .= "From:$namefrom <$from>\r\n";
$headers .= "Sender:$namefrom <$from>\r\n"; // transmitter (Sender) not required if same as author (From)
$headers .= "Reply-To:$namefrom <$from>\r\n";
// Optional recommended headers
$headers .= "To:$nameto <$to>\r\n";
$headers .= "Message-ID:". getSessionID() . "\r\n";
$headers .= "Subject:$subject\r\n";
// Connect to SMTP server on SMTP port
$smtpServer = fsockopen( $mxServer, $SMTP_PORT, $errno, $errstr, $timeout);
if ( !$smtpServer ) return false;
$smtpReply = fgets( $smtpServer, $readLength );
// Client initiation
fputs( $smtpServer, "EHLO $SMTP_DOMAIN\r\n" );
$smtpReply = fgets( $smtpServer, $readLength );
// Mail transactions
fputs( $smtpServer, "MAIL FROM: <$from>\r\n" );
$smtpReply = fgets( $smtpServer, $readLength ); // Success = {250}, Failure ={550, 553}
fputs( $smtpServer, "RCPT TO:<$to>\r\n" );
$smtpReply = fgets( $smtpServer, $readLength ); // Success = {250}, Failure ={550, 503}
fputs( $smtpServer, "DATA \r\n" );
$smtpReply = fgets( $smtpServer, $readLength ); // Success = {354}
fputs( $smtpServer, "$headers\n" );
$message = wordwrap( $message, 79 );
fputs( $smtpServer, "$message\r\n" );
fputs( $smtpServer, "\r\n.\r\n" );
$smtpReply = fgets( $smtpServer, $readLength ); // Success = {250}, Failure = {503, 554}
// Terminate the connection
fputs( $smtpServer, "QUIT\r\n" );
$smtpReply = fgets( $smtpServer, $readLength ); // Success = {221}, Failure = {421}
return true;
}
/**
* Sends an approval request email to the guestbook administrator
*
* @param email - the text value for the email field
* @param url - the text value for the website field
* @param name - the text value for the name field
* @param comments - the text value for the comments field
* @param privateemail - the checkbox value for the privateemail checkbox
* @return boolean - true if email sent successfullyy else false
*/
function emailAdminApproval( $email, $url, $name, $comments, $privateemail ) {
global $EMAILS_FROM, $EMAILS_FROM_NAME, $ADMIN_SUBJECT, $ADMIN_EMAIL_ADDRESS, $ADMIN_EMAIL_NAME, $ADMIN_APPROVE_LINK;
$email_message = "\nDear $ADMIN_EMAIL_NAME,\n\nA guestbook entry has been made with the";
$email_message .= " following details:\n\nName:\t\t$name\n\nE-mail:\t$email\n\n";
$email_message .= "Website:\t$url\n\nComments:\n$comments\n\nIf you feel that the";
$email_message .= " entry is a legitimate one then please approve it by clicking the";
$email_message .= " link below.\n\n$ADMIN_APPROVE_LINK".getSessionID();
$email_message .= "\n\nSincerely\nThe Guestbook\n\n";
$email_message = wordwrap( $email_message );
return email( $EMAILS_FROM, $EMAILS_FROM_NAME, $ADMIN_EMAIL_ADDRESS, $ADMIN_EMAIL_NAME, $ADMIN_SUBJECT, $email_message );
}
/**
* Sends a confirmation request email to the submitted guest address
*
* @param email - the text value for the email field
* @param url - the text value for the website field
* @param name - the text value for the name field
* @param comments - the text value for the comments field
* @param privateemail - the checkbox value for the privateemail checkbox
* @return boolean - true if email sent successfullyy else false
*/
function emailGuestConfirmation( $email, $url, $name, $comments, $privateemail ) {
global $EMAILS_FROM, $EMAILS_FROM_NAME, $GUEST_SUBJECT, $GUEST_CONFIRM_LINK;
$email_message = "\nDear $name,\n\nA guestbook entry has been made with the";
$email_message .= " following details:\n\nName:\t\t$name\n\nE-mail:\t$email\n\n";
$email_message .= "Website:\t$url\n\nComments:\n$comments\n\nIf you signed the";
$email_message .= " guestbook then please click on the link below to confirm the";
$email_message .= " entry, else please ignore this email\n\n$GUEST_CONFIRM_LINK";
$email_message .= getSessionID()."\n\nSincerely\nThe Guestbook\n\n";
$email_message = wordwrap( $email_message );
return email( $EMAILS_FROM, $EMAILS_FROM_NAME, $email, $name, $GUEST_SUBJECT, $email_message );
}
/**
* Generates the HTML code for the footer of the page. The main contents of the
* page should have been dynamically generated already.
*
* @param void - no parameters are required
* @return null - no values are returned
*/
function endPage() {
print "