Location: PHPKode > projects > Guestbook > guestbook-v1/guestbook.class.php
<?php
############################################################
	/*
	 *		Guestbook Class by Tom Taylor
	 *		Theatons Website Design
	 *		http://www.theatons.com
	 *		28th July 06 
	 *		For PHP 4.x.x
	 *		Developed using Zend Development Studio 5
	*/
############################################################



class Guestbook { 
	
	var $TableWidth = 690; 
	var $TableBorder = 1; 
	var $TableId = null; 
	var $RowClass = null;
	var $TdClass = null; 
	var $h4class = null;
	var $DivTitleClass = null;
	var $DivTimeClass = null; 
	var $DivAuthorClass = null; 
	var $Add_TextInputSize = 14; 
	var $Add_TextareaSize = 400; 
	var $Add_TextareaHeight = 150; 
	var $MessageLimit = 1000; 
	var $NameAlign = 'right'; 
	var $TimeSetting = 'jS F y';			// date format for the guestbook
	var $GuestbookPage = ''; 
	var $FormError = null; 
	var $isSent = false; 
	var $EmailsFrom = ''; 
	var $SubmitConfirmationEmail = 'Your entry to the guestbook has been submitted. It will be reviewed before appearing on the website'; 
	var $ConfirmMessage = 0; 
	var $myEmail = null; 
	
	
	function showEntrys() { 
			$p = new Pager();
			$limit = 20;
			$start = $p->findStart($limit);
			$count = mysql_num_rows(mysql_query("SELECT * FROM guestbook WHERE Verified='1' ORDER BY id DESC"));
			$pages = $p->findPages($count, $limit);
			$result = mysql_query("SELECT * FROM guestbook WHERE Verified='1' ORDER BY id DESC LIMIT ".$start.", ".$limit);
			$pagelist = $p->pageList($_GET['page'], $pages);
			
			echo '<h4 class="'.$this->h4class.'">Autographink Guestbook.</h4>';
			echo '<table width="'.$this->TableWidth.'" id="'.$this->TableId.'" border="'.$this->TableBorder.'">';
			
			if(isset($_GET['code'])) { $this->showCoded($_GET['code']); } 
			
			echo '<tr class="'.$this->RowClass.'"><td class="'.$this->TdClass.'" colspan="2" align="right">'.$pagelist.'</td></tr>'; 
			echo '<tr class="'.$this->RowClass.'"><td colspan="2"></td></tr>'; 
			
			while($info = mysql_fetch_array($result)) { 
		
				echo '<tr class="'.$this->RowClass.'">';
					echo '<td class="'.$this->TdClass.'"><div class="'.$this->DivTitleClass.'">'.$this->HasWebsite($info['Website'],$info['Title']).'</div></td>';
					echo '<td class="'.$this->TdClass.'"><div class="'.$this->DivTimeClass.'">'.$this->TimeFormat($info['Timestamp']).'</div></td>';
				echo '</tr>';
				echo '<tr class="'.$this->RowClass.'">';
					echo '<td colspan="2" class="'.$this->TdClass.'">'.$this->ReturnMessage($info['Message']).'</td>';
				echo '</tr>';
				echo '<tr class="'.$this->RowClass.'">';
					echo '<td colspan="2" class="'.$this->TdClass.'" align="'.$this->NameAlign.'"><div class="'.$this->DivAuthorClass.'">'.$this->Author($info['Email'], $info['Name']).'</div></td>';
				echo '</tr>';
				echo '<tr><td colspan="2" height="20"></td></tr>';
					
			}
			echo '<tr class="'.$this->RowClass.'"><td class="'.$this->TdClass.'" colspan="2" align="right">'.$pagelist.'</td></tr>'; 
			echo '<tr><td colspan="2" align="left" class="'.$this->TdClass.'"><a href="javascript:toggleLayer(\'addToGuestbook\');">Add a comment</a>';
				echo '<div id="addToGuestbook" '.$this->AutoDisplayAddForm().'>';
			 			$this->showAddForm(); 
				echo '</div>';
			echo '</td></tr>'; 
			echo '</table>'; 
	}
	
	
	
	// does the person have a website ? 
	function HasWebsite($website = null, $title = null) { 
		if($website != '') { 
			$website = str_replace("https://", "", $website); 
			$website = str_replace("http://", "", $website);  
			return '<a href="http://'.$website.'" title="'.$this.'">'.$title.'</a>';
		} else {
			return $title; 
		}	 
	}
	
	
	
	// return the correct time format 
	function TimeFormat($var) { 
		return date($this->TimeSetting,$var); 
	} 
	
	
	
	// replace the linebreaks, allow p tags. 
	function ReturnMessage($text) { 
		$text = str_replace("\n", "<br>", $text);
		$text = strip_tags($text, "<p> <br>");
		return $text; 
	}
	
	
	
	// has the personal left an email ?  
	function Author($email = null, $name = null) {  
		if(!empty($email)) { 
			return '<a href="mailto:'.$email.'" title="'.$name.'">'.$name.'</a>';
		} else {
			return $name; 
		}
	}
	
	function showCoded($code) { 
		$sql = "SELECT * FROM guestbook WHERE Code='".$this->Escape($code)."'"; 
		$res = mysql_query($sql) or die(mysql_error()); 
		if(mysql_num_rows($res) > 0) { 
			$info = mysql_fetch_assoc($res); 
				echo '<form method="post" action="'.$this->GuestbookPage.'">';
				echo '<tr class="'.$this->RowClass.'"><td colspan="2" class="'.$this->TdClass.'"><h4 align="center">Confirm or delete post</h4></td></tr>';
				echo '<tr class="'.$this->RowClass.'">';
					echo '<td class="'.$this->TdClass.'"><div class="'.$this->DivTitleClass.'">'.$this->HasWebsite($info['Website'],$info['Title']).'</div></td>';
					echo '<td class="'.$this->TdClass.'"><div class="'.$this->DivTimeClass.'">'.$this->TimeFormat($info['Timestamp']).'</div></td>';
				echo '</tr>';
				echo '<tr class="'.$this->RowClass.'">';
					echo '<td colspan="2" class="'.$this->TdClass.'">'.$this->ReturnMessage($info['Message']).'</td>';
				echo '</tr>';
				echo '<tr class="'.$this->RowClass.'">';
					echo '<td colspan="2" class="'.$this->TdClass.'" align="'.$this->NameAlign.'"><div class="'.$this->DivAuthorClass.'">'.$this->Author($info['Email'], $info['Name']).'</div></td>';
				echo '</tr>';
					echo '<input type="hidden" name="Code" value="'.$info['Code'].'" />'; 
				echo '<tr class="'.$this->RowClass.'"><td colspan="2" class="'.$this->TdClass.'">';
					echo '<input type="submit" name="Delete" value="Delete" />';
					echo '<input type="submit" name="Approve" value="Approve" />';
				echo '</td></tr>';
				echo '<tr class="'.$this->RowClass.'"><td colspan="2" height="20"></td></tr>';
		}
	}
	
	function checkAddorDelete() { 
		if(isset($_POST['Delete'])) { 
			mysql_query("DELETE from guestbook WHERE Code='".$_POST['Code']."'") or die(mysql_error()); 
		} elseif (isset($_POST['Approve'])) { 
			mysql_query("UPDATE guestbook SET Verified='1' WHERE Code='".$_POST['Code']."'"); 
		} 
	} 
	
############################################################
	/*
	 *		The adding side of the guestbook
	*/
############################################################


	function showAddForm() { 
		echo '<h4 class="'.$this->h4class.'">Add an entry to the guestbook.</h4>';
		echo '<form action="'.$this->GuestbookPage.'?add=1" method="POST">';
		echo '<table cellpadding="4">';
			if(isset($this->FormError)) { echo '<tr><td colspan="2><h5 align="center" style="color:red">'.$this->FormError.'</h5></td></tr>'; } 
			if($this->isSent == false) { 
				echo '<tr>';
					echo '<td>Name*:</td>';
					echo '<td><input type="text" name="Name" value="'.$this->sent('Name').'" size="'.$this->Add_TextInputSize.'" /></td>';
				echo '</tr>';
				echo '<tr>';
					echo '<td>Email*:</td>';
					echo '<td><input type="text" name="Email" value="'.$this->sent('Email').'" size="'.$this->Add_TextInputSize.'" /></td>';
				echo '</tr>';
				echo '<tr>';
					echo '<td>Website:</td>';
					echo '<td><input type="text" name="Website" value="'.$this->sent('Website').'" size="'.$this->Add_TextInputSize.'" /></td>';
				echo '</tr>';
				echo '<tr>';
					echo '<td>Title*:</td>';
					echo '<td><input type="text" name="Title" value="'.$this->sent('Title').'" size="'.$this->Add_TextInputSize.'" /></td>';
				echo '</tr>';
				echo '<tr>';
					echo '<td valign="top">Message*:</td>';
					echo "<td><textarea name=\"Message\" style=\"width:".$this->Add_TextareaSize."; height:".$this->Add_TextareaHeight."\" id=\"Message\"  onKeyUp=\"Contar('Message','sMessage','{CHAR} characters left.',".$this->MessageLimit.");\">".$this->sent('Message')."</textarea><br /><span id=\"sMessage\">".$this->MessageLimit." characters left.</span></td>";
				echo '</tr>';
				echo '<tr>';
					echo '<td colspan="2" align="left"><input type="submit" name="addToGuestbook" value="Add Comments" /></td>';
				echo '</tr>'; 
				// hidden fields
				echo '<input type="hidden" name="TimeStamp" value="'.time().'" />';
			} else { 
				echo '';
			} 
		echo '</table>';
	}
		
	
	// value for form input fields
	function sent($value) { 
		if(isset($_POST[$value])) { return $_POST[$value]; } 
		else { return ''; } 
	}
	
	
	// if the form has been submitted, set the display so you dont havt to click the link to open it
	function AutoDisplayAddForm() { 
		if(isset($_GET['add'])) { 
			return 'style="display:block;"'; 
		} else {
			return;
		}
	} 
	
	
	// form validation
	function CheckForm($post) { 
		if(
			(isset($post['Name']) && $post['Name'] != '') &&
			(isset($post['Title']) && $post['Title'] != '') &&
			(isset($post['Email']) && $post['Email'] != '') &&
			(isset($post['Message']) && $post['Message'] != '') 
		) { 
			if(!$post['Email'] == "" && (!strstr($post['Email'],"@")) || !strstr($post['Email'],"."))  {
				// the email is not empty, does contain @ and has a . - return false
				$this->FormError = 'Email is not valid';
				return false;
			}			
			return true; 
		} else { 
			$this->FormError = 'Please ensure all fields are filled in.'; 
			return false;;
		}
	}
	
	
	// sending the emails 
	function SendConfirmEmail() { 
		$code = sha1($this->RandomString());
		
		$sql = "INSERT into guestbook 
				(TimeStamp, Email, Name, Website, Title, Message, Verified, Code) 
				VALUES ('".$this->Escape($_POST['TimeStamp'])."', 
						'".$this->Escape($_POST['Email'])."', 
						'".$this->Escape($_POST['Name'])."',
						'".$this->Escape($_POST['Website'])."',
						'".$this->Escape($_POST['Title'])."',
						'".$this->Escape($_POST['Message'])."',
						'0',
						'".$this->Escape($code)."')";
		
		$res = mysql_query($sql) or die(mysql_error());
		$id = mysql_insert_id(); 
		
		if($this->ConfirmMessage == 1) { 
			$message = $this->SubmitConfirmationEmail;
			$this->email($_POST['Email'], 'Your Guestbook entry.', $message);
		} 
		
		$message = '<h4>Guestbook Entry</h4>';
		$message .= '<p>A sucessfull submission was made to the guestbook.</p> <p>Name: '.$_POST['Name'].'</p><p>Time:'.$this->TimeFormat($_POST['TimeStamp']).'</p>';
		$message .= '<p><a href="'.$this->GuestbookPage.'?id='.$id.'&code='.$code.'">Click here to view ( then confirm or delete )</a></p>';
		$message .= '<p>Regards, <br />Guestbook</p>';
		$subject = 'Guestbook Entry'.$this->TimeFormat($_POST['TimeStamp']);
		if($this->email($this->myEmail, $subject, $message) == false ) { 
			die("There was a problem sending an email to the guestbook owner");
		} 
		
		$this->isSent = true; 
	}
	
	
	// escape the strings for sql
	function Escape($string) { 
		return  mysql_real_escape_string($string);
	}

	
	// email function
	function email($to, $subject, $message) {
			
		$headers  = 'MIME-Version: 1.0' . "\r\n";
		$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
		$headers .= 'To: '.$to. "\r\n";
		$headers .= 'From: '.$this->EmailsFrom. "\r\n";

		if(mail($to, $subject, $message, $headers)) return true;
		else return false; 
	}
	
	
	function RandomString($length=11) {
		$randstr='';
		srand((double)microtime()*1000000);
		//our array add all letters and numbers if you wish
		$chars = array ( 'a','b','c','d','e','f');
		for ($rand = 0; $rand <= $length; $rand++)
		{
		$random = rand(0, count($chars) -1);
		$randstr .= $chars[$random];
		}
		return $randstr;
	}

############################################################
	/*
	 *		Javascript show/hide add to guestbook form
	*/
############################################################

	function JavascriptShowHide() { 
		
		######################################################
		#
		#	*  Javascript toggle layer function
		# 	*  Unkown author
		#
		######################################################
		
		echo '
		<script type="text/javascript" language="javascript"> 
		
		function toggleLayer(whichLayer) {
			if (document.getElementById)
			{
				// this is the way the standards work
				var style2 = document.getElementById(whichLayer).style;
				style2.display = style2.display? "":"block";
			}
			else if (document.all)
			{
				// this is the way old msie versions work
				var style2 = document.all[whichLayer].style;
				style2.display = style2.display? "":"block";
			}
			else if (document.layers)
			{
				// this is the way nn4 works
				var style2 = document.layers[whichLayer].style;
				style2.display = style2.display? "":"block";
			}
		}
		
		</script>';
	}
	
	function JavascriptMessageLimit() { 
		
		######################################################
		#
		#	*  Javascript Message Limit
		# 	*  Written by Steve
		#
		######################################################
		
		echo "
		<script type=\"text/javascript\" language=\"javascript\"> 
		
			function getObject(obj) {
			  var theObj;
			  if(document.all) {
			    if(typeof obj==\"string\") {
			      return document.all(obj);
			    } else {
			      return obj.style;
			    }
			  }
			  if(document.getElementById) {
			    if(typeof obj==\"string\") {
			      return document.getElementById(obj);
			    } else {
			      return obj.style;
			    }
			  }
			  return null;
			}
		
			function Contar(entrada,salida,texto,caracteres) {
			  var entradaObj=getObject(entrada);
			  var salidaObj=getObject(salida);
			  var longitud=caracteres - entradaObj.value.length;
			  if(longitud <= 0) {
				longitud=0;
				texto='<span class=\"disable\"> '+texto+' </span>';
				entradaObj.value=entradaObj.value.substr(0,caracteres);
			  }
			  salidaObj.innerHTML = texto.replace(\"{CHAR}\",longitud);
			}
		
		</script>";
	} 
	
	
	

}
?>
Return current item: Guestbook