Location: PHPKode > projects > NBook > nbook/nbook.php
<?php
#######################################################
# nbook -- Nar3 Guestbook -- A Guestbook Scripts
# v1.1.2
#2003.2.3
#
# Jason Profitt
# hide@address.com
# http://devel.narnarnar.com/nbook/
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#######################################################
if(is_array($HTTP_POST_VARS)){$_POST=$HTTP_POST_VARS;}
if(is_array($HTTP_SERVER_VARS)){$_SERVER=$HTTP_SERVER_VARS;}
if(is_array($HTTP_SERVER_VARS)){$_GET=$HTTP_GET_VARS;}
$config[powered_by]="<p align=\"right\"><font size=\"-2\">Powered by <a href=\"http://devel.narnarnar.com/nbook/\">Nbook</a> v1.1.2.</font></p>";

#First, let's see if it's configged
if(!file_exists("config.php")) {
	make_config();
}
else {
	require_once("config.php");
	if(!file_exists($config[out_file])) {
		$header=DoHeader("Fatal Error");
		print $header;
		print "<b>Fatal Error:</b> The file $config[out_file] cannot be found. NBook is aborting...";
		$footer=DoFooter();
		print $footer;
		exit;
	}
        if((!file_exists($config[template])) && ($config[use_template]==1)) {
                $config[use_template]=0;
                print DoHeader("Fatal Error");
                print "<b>Fatal Error:</b> The template file $config[template] cannot be found. NBook is aborting...";
                print DoFooter();
                exit;
        }
}
$config[opentable]="<table border=\"$config[table_border]\" bordercolor=\"$config[table_bordercolor]\" cellpadding=\"$config[table_cellpadding]\" cellspacing=\"$config[table_cellspacing]\" bgcolor=\"$config[table_bgcolor]\">";
$config[tabfonttag]="<font face=\"$config[font_face]\" size=\"$config[font_size]\" color=\"$config[table_font_color]\">";
$config[fonttag]="<font face=\"$config[font_face]\" size=\"$config[font_size]\">";

#Now, let's do a switch/case
if ($_GET[op]=="") {$_GET[op]="sign";}
switch($_GET[op]) {
	case view:
		if($nbook=@implode("", @file("$config[out_file]"))) {
                        print $nbook;
                        exit;
                }
                else {
                        print DoHeader("Fatal Error");
                        print "<b>Fatal Error:</b> The file $config[out_file] could not be read. NBook is aborting...";
                        print DoFooter();
                        exit;
                }
	case sign:
		sign();
}


#This signs the guestbook
function sign() {
	global $_POST, $config, $bad_send, $msg, $_SERVER, $name_ul, $end_name_ul, $comments_ul, $end_comments_ul;
	if(($_POST[sent]==1) && ($bad_send != 1)) {
		#let's check all of the vars that are required
		$required=array(name => "Your Name", comments=>"Comments");
		foreach($required as $key => $value) {
			if ($_POST[$key]=="") {
				$bad_send=1;
				$msg.="You forgot to fill out the <b>$value</b> field.<br>";
                                ${"$key" . "_ul"}="<u><i>";
                                ${"end_" . "$key" . "_ul"}="</i></u>";
			}
		}
		#Check and make sure all went well...
		if ($bad_send==1) {
			sign();
			exit;
		}
		$ip=$_SERVER[REMOTE_ADDR];
		$date=date("F j, Y @ g:i:s A");
		$entry=<<<___eofh
<!--IP: $ip-->
$config[opentable]
	<tr>
		<td bgcolor="$config[table_head_bgcolor]">
			$config[tabfonttag]
			<b>Date</b>
			</font>
		</td>
		<td>
			$config[tabfonttag]
			$date
			</font>
		</td>
	</tr>
	<tr>
		<td bgcolor="$config[table_head_bgcolor]">
			$config[tabfonttag]
			<b>Name</b>
			</font>
		</td>
		<td>
			$config[tabfonttag]\n
___eofh;
		#OK, let's decide what to do if they have a homepage, and email
		$_POST[name]=stripslashes($_POST[name]);
                #Let's check the size andm ake sure they don't screw up the formatting
                $words=split(" ", $_POST[name]);
                foreach($words as $key => $each_word) {
                        $words[$key]=SplitWord($each_word);
                }
                $_POST[name]=implode(" ", $words);
                $_POST[name]=ereg_replace("<", "&lt;", $_POST[name]);
                $_POST[name]=ereg_replace(">", "&gt;", $_POST[name]);
                $_POST[name]=ereg_replace("\n", "<br>\n", $_POST[name]);
		if(($_POST[homepage] != "") && ($_POST[homepage] != "none")) {
			$_POST[homepage]=stripslashes($_POST[homepage]);
                        $_POST[homepage]=ereg_replace("<", "&lt;", $_POST[homepage]);
                        $_POST[homepage]=ereg_replace(">", "&gt;", $_POST[homepage]);
                        $_POST[homepage]=ereg_replace("\"", "&quot;", $_POST[homepage]);
			if(substr($_POST[homepage], 0, 4) != "http") {
				$_POST[homepage]="http://$_POST[homepage]";
			}
			$entry .="\t\t\t<a href=\"$_POST[homepage]\">$_POST[name]</a>\n";
		}
		else {
			$entry.="\t\t\t$_POST[name]\n";
		}
		$entry.=<<<___eofh
		</td>
	</tr>\n
___eofh;
		if($_POST[email] != "") {
			$_POST[email]=stripslashes($_POST[email]);
                        $email_link=$_POST[email];
                        $words=split(" ", $_POST[email]);
                        foreach($words as $key => $each_word) {
                                $words[$key]=SplitWord($each_word);
                        }
                        $_POST[email]=implode(" ", $words);
                        $_POST[email]=ereg_replace("<", "&lt;", $_POST[email]);
                        $email_link=ereg_replace("<", "&lt;", $email_link);
                        $_POST[email]=ereg_replace(">", "&gt;", $_POST[email]);
                        $email_link=ereg_replace(">", "&gt;", $email_link);
                        $_POST[email]=ereg_replace("\n", "<br>\n", $_POST[email]);
			$entry.=<<<___eofh
	<tr>
		<td bgcolor="$config[table_head_bgcolor]">
			$config[tabfonttag]
			<b>Email</b>
			</font>
		</td>
		<td>
			$config[tabfonttag]<a href="mailto:$email_link">$_POST[email]</a></font>
		</td>
	</tr>\n
___eofh;
		}
		if($_POST[location]!= "") {
			$_POST[location]=stripslashes($_POST[location]);
			$entry.=<<<___eofh
	<tr>
		<td bgcolor="$config[table_head_bgcolor]">
			$config[tabfonttag]
			<b>Location</b>
			</font>
		</td>
		<td>
			$config[tabfonttag]
			$_POST[location]
			</font>
		</td>
	</tr>\n
___eofh;
		}
                #OK, now let's do some checking on the comments
		$_POST[comments]=stripslashes($_POST[comments]);
                $_POST[comments]=ereg_replace("<", "&lt;", $_POST[comments]);
		$_POST[comments]=ereg_replace(">", "&gt;", $_POST[comments]);
                #check for an email address
                $_POST[comments] = preg_replace("/(([\w\.]+))(@)([\w\.]+)\b/i","<a href=\"mailto:$0\">$0</a>",$_POST[comments]);
                #check for a url to something
                $_POST[comments] = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\">\\0</a>", $_POST[comments]);
                #ok, let's separate everything and chop words over 80 characters into 80 character chunks
		#First, let's split by nwe lines
		$lines=split("\n", $_POST[comments]);
		foreach($lines as $key1 => $each_line) {
			$words=split(" ", $each_line);
			foreach($words as $key => $each_word) {
				#let's check if this is in an html tag. if it is, we'll ignore it.
				#otherwise, we need to check on it
				if(ereg("<", $each_word) && ($each_word != "<a")) {
					#OK, we need to split it with the < and then the > to get the middle part, which is all
					#we want to screw w/
					list($keep, $junk2)=split("<", $each_word);
					list($junk1, $keep)=split(">", $keep);
					$words[$key]="$junk1>" . SplitWord($keep) . "<$junk2";
				}
				else{
					$words[$key]=SplitWord($each_word);
				}
			}
			$new_lines[$key1]=implode(" ", $words);
		}
		$_POST[comments]=implode("<br>", $new_lines);
		$entry.=<<<___eofh
	<tr>
		<td bgcolor="$config[table_head_bgcolor]">
			$config[tabfonttag]
			<b>Comments</b>
			</font>
		</td>
		<td>
			$config[tabfonttag]
			$_POST[comments]
			</font>
		</td>
	</tr>\n
___eofh;
		$entry.="</table>\n<br>";
		$header=DoHeader("Entry Added");
		print $header;
		print <<<___eofh
$config[opentable]
	<tr bgcolor="#77aadd">
		<td width="130"A>
			<a href="http://devel.narnarnar.com">
			<img src="http://devel.narnarnar.com/images/logo_77aadd.gif"  border="0" align="right" alt="Nar3 Devel"></a>
		</td>
		<td>
			<font face="$config[font_face]" size="+2" color="$config[table_font_color]">
			<b>$config[site_name]</b>
			</font>
		</td>
	</tr>
	
	<tr valign="middle" bgcolor="$config[table_head_bgcolor]">
		<td align="center" colspan="2">
			<font face="$config[font_face]" size="+2" color="$config[table_font_color]"><I><B>Thanks! Entry Added!<i></b></font>
		</td>
	</tr>   
	<tr>
		<td colspan="2">
			$config[tabfonttag]
			Thank you for signing my guestbook! Click <a href="$config[out_file]">here</a> to view the other entries.<br>
			</font>
		</td>
	</tr>
</table>
<br>
$entry
___eofh;
		#Now let's load the guestbook file
		$guestbook=@implode("", @file($config[out_file]));
		$guestbook=ereg_replace("<!--GBOOK-->", "<!--GBOOK-->\n$entry", $guestbook);
                #OK, i think this is just some g00fy windoze thing, but i'm gonna try to stop
                #it from making so many new lines
                $guestbook=ereg_replace("\r","", $guestbook);
		if($fp=@fopen("$config[out_file]", "w")) {
                        fwrite($fp, $guestbook);
                        fclose($fp);
                }
                else {
                        print "<br><b>There was an error writing the entry to the guestbook file. Please email the owner with this error message.</b>";
                }
	}
        #Just print out the signing part
	else {
		$header=DoHeader("Sign My Guestbook");
		print $header;
		print <<<___eofh
$msg<br>
<form method="POST" action="$config[script_filename]">
$config[opentable]
	<tr bgcolor="$config[table_head_bgcolor]">
		<td colspan="2">
			<font face="$config[font_face]" size="+2" color="$config[table_font_color]">
			Sign My Guestbook
			</font>
		</td>
	</tr>
	<tr>
		<td>
			$config[tabfonttag]
			$name_ul<b>Your Name</b>$end_name_ul *
			</font>
		</td>
		<td>
			<input type="text" value="$_POST[name]" name="name" size="50">
		</td>
	</tr>
	<tr>
		<td>
			$config[tabfonttag]
			<b>Email Address</b>
			</font>
		</td>
		<td>
			<input type="text" value="$_POST[email]" name="email" size="50">
		</td>
	</tr>
	<tr>
		<td>
			$config[tabfonttag]
			<b>Homepage</b>
			</font>
		</td>
		<td>
			<input type="text" value="$_POST[homepage]" name="homepage" size="50">
		</td>
	</tr>
	<tr>
		<td>
			$config[tabfonttag]
			<b>Location</b>
			</font>
		</td>
		<td>
			<input type="text" value="$_POST[location]" name="location" size="50">
		</td>
	</tr>
	<tr>
		<td>
			$config[tabfonttag]
			$comments_ul<b>Comments</b>$end_comments_ul *
			</font>
		</td>
		<td>
			<textarea cols="60" rows="5" name="comments">$_POST[comments]</textarea>
		</td>
	</tr>
</table>
<input type="hidden" value="1" name="sent">
<input type="submit" value="Sign Guestbook">
</form><br>
$config[fonttag]
Don&#039t feel like signing? View the entries <a href="$config[script_filename]?op=view">here</a>.<br>
<font size="-2">* Required</font>
</font>
___eofh;
	}
	$footer=DoFooter();
	print $footer;
	exit;
}


#This function makes a new config file
#It's here if it's a fresh install
function make_config() {
	global $_POST, $_SERVER, $_GET, $config;
	if($_POST[table_bgcolor]==""){$config[table_bgcolor]="#b3b3b3";}else{$config[table_bgcolor]=$_POST[table_bgcolor];}
	if($_POST[table_head_bgcolor]==""){$config[table_head_bgcolor]="#77aadd";}else{$config[table_head_bgcolor]=$_POST[table_head_bgcolor];}
	if($_POST[table_border]==""){$config[table_border]="1";}else{$config[table_border]=$_POST[table_border];}
	if($_POST[table_bordercolor]==""){$config[table_bordercolor]="#000000";}else{$config[table_bordercolor]=$_POST[table_bordercolor];}
	if($_POST[table_font_color]==""){$config[table_font_color]="#000000";}else{$config[table_font_color]=$_POST[table_font_color];}
	#if($_POST[table_font_face]==""){$config[table_font_face]="Arial";}else{$config[table_font_face]=$_POST[table_font_face];}
	if($_POST[table_cellspacing]==""){$config[table_cellspacing]="0";}else{$config[table_cellspacing]=$_POST[table_cellspacing];}
	if($_POST[table_cellpadding]==""){$config[table_cellpadding]="1";}else{$config[table_cellpadding]=$_POST[table_cellpadding];}
	if($_POST[bgcolor]==""){$config[bgcolor]="#FFFFFF";}else{$config[bgcolor]=$_POST[bgcolor];}
	if($_POST[background]==""){$config[background]="";}else{$config[background]=$_POST[background];}
	if($_POST[text]==""){$config[text]="#000000";}else{$config[text]=$_POST[text];}
	if($_POST[link]==""){$config[link]="#444444";}else{$config[link]=$_POST[link];}
	if($_POST[alink]==""){$config[alink]="#000000";}else{$config[alink]=$_POST[alink];}
	if($_POST[vlink]==""){$config[vlink]="#555555";}else{$config[vlink]=$_POST[vlink];}
	if($_POST[site_name]==""){$config[site_name]="My NBook";}else{$config[site_name]=stripslashes($_POST[site_name]);}
	if($_POST[give_credit]==""){$config[give_credit]="1";}else{$config[give_credit]=$_POST[give_credit];}
	#if($_POST[doc_root]==""){$config[doc_root]=$_SERVER[DOCUMENT_ROOT];}else{$config[doc_root]=$_POST[doc_root];}
	if($_POST[font_face]==""){$config[font_face]="Arial";}else{$config[font_face]=$_POST[font_face];}
	if($_POST[font_size]==""){$config[font_size]="-1";}else{$config[font_size]=$_POST[font_size];}
	if($_POST[use_template]==""){$config[use_template]="0";}else{$config[use_template]=$_POST[use_template];}
	if($_POST[gbook_template]==""){$config[gbook_template]="1";}else{$config[gbook_template]=$_POST[gbook_template];}
	if($_POST[template]==""){$config[template]="";}else{$config[template]=$_POST[template];}
	if($_POST[out_file]==""){$config[out_file]="guestbook.html";}else{$config[out_file]=$_POST[out_file];}
	$this_file_array=split("/", $_SERVER[PHP_SELF]);
	$script_filename=$this_file_array[count($this_file_array) -1];
	unset($this_file_array[count($this_file_array) -1]);
	$script_dir=implode("/", $this_file_array);
	if($_POST[script_filename]==""){
		$config[script_filename]=$script_filename;
	}else{$config[script_filename]=$_POST[script_filename];}
	#if($_POST[script_dir]==""){$config[script_dir]=$script_dir;}else{$config[script_dir]=$_POST[script_dir];}
	if($_POST[gbook_url]==""){$config[gbook_url]="http://$_SERVER[HTTP_HOST]$script_dir";}else{$config[gbook_url]=$_POST[gbook_url];}
	#if($_POST[]==""){$config[]="";}else{$config[]=$_POST[];}
	#Let's do a simple write/delete test to see if the permissions are set right
	#Create a filename to delete
	$dir_array=split("/", $_SERVER[SCRIPT_FILENAME]);
	unset($dir_array[count($dir_array)-1]);
	$dir=implode("/", $dir_array);
	$dir="$dir/DELETE_ME";
	if(!@mkdir($dir, 0666)) {
		$msg="<b>Cannot write to disk. Nbook will be unable to save your configuration. Please set permissions correctly.</b><br>";
	}
	else {
		rmdir($dir);
	}
	#Let's verify some info
	#check and make sure the template exists and the 
	if ($_POST[use_template]==1) {
		if($_POST[template]=="") {
			$config[use_template]=0;
			$msg.="<b>You have not set the template file.</b><br>";
		}
		elseif(!file_exists($_POST[template])) {
			$config[use_template]=0;
			$msg.="<b>The template file <i>$_config[template]</i> does not exist.</b><Br>";
		}
		else{
			#Now we need to find out if the template is set up right
			if($template=@implode("", @file($config[template]))) {
                                if (!ereg("%%NBOOK%%", $template)) {
                                        $msg.="<b>Your template does not contain %%NBOOK%% and cannot be used. Template disabled.</b><br>\n";
                                        $config[use_template]=0;
                                }
                                if (!ereg("%%TITLE%%", $template)) {
                                        $msg.="<b>Your template does not contain %%TITLE%% and cannot be used. Template disabled.</b><br>\n";
                                        $config[use_template]=0;
                                }
                        }
                        else {
                                $msg.="<b>Your template file could not be read. Template disabled.</b><br>\n";
                                $config[use_template]=0;
                        }
		}
	}
	#First thing we need to do is print out the top stuff
	$config[opentable]="<table border=\"$config[table_border]\" bordercolor=\"$config[table_bordercolor]\" cellpadding=\"$config[table_cellpadding]\" cellspacing=\"$config[table_cellspacing]\" bgcolor=\"$config[table_bgcolor]\">";
	$config[tabfonttag]="<font face=\"$config[font_face]\" size=\"$config[font_size]\" color=\"$config[table_font_color]\">";
	$config[fonttag]="<font face=\"$config[font_face]\" size=\"$config[font_size]\">";
	$header=DoHeader("Nar3 Guestbook Configuration");
	print $header;
	#Now let's decide if they're previewing or whatever
	if(($_POST[preview] != 1) && ($_POST[create] != 1)){
		$preview_hidden="<input type=\"hidden\" name=\"preview\" value=\"1\">";
		$submit_value="Preview";
	}
	elseif(($_POST[preview] == 1) && ($_POST[create] != 1)){
		$preview_hidden="<input type=\"hidden\" name=\"preview\" value=\"1\">\n"
					. "<input type=\"checkbox\" name=\"create\" value=\"1\">Create Config File<br>";
		$submit_value="Go";
	}
	#Create the config file
	elseif($_POST[create]==1){
		if($fp=@fopen("config.php", "w")){
			$msg.="The Configuration file was successfully created. If you need to make " 
			. "changes in the future, simply edit the file <b>config.php</b> with your favorite text editor. You can also " 
			. "delete the config file to use the web form to configure the guestbook. You can click <a href=\"$config[script_filename]\">here</a>"
			. " to view the form to sign the guestbook, or click <a href=\"$config[out_file]\">here</a> to view the guestbook.<br>";
		}
		else {
			$msg.="The Configuration file was <b>not</b> created. Check your file permissions and try again. ";
		}
		$config_file=<<<___eofh
<?php
#######################################################
# nbook -- Nar3 Guestbook -- A Guestbook Scripts
# v1.1.2
#2003.2.3
#
# Jason Profitt
# hide@address.com
# http://devel.narnarnar.com/nbook/
#config.php This is the configuration file, you can edit at will
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#######################################################\n
___eofh;
		foreach($config as $key => $value) {
			if($key != "powered_by"){
				$value=ereg_replace("\"", "\\\"", $value);
				$config_file.="\$config[$key]=\"$value\";\n";
			}
		}
		$config_file.="?>";
		@fwrite($fp, $config_file);
		@fclose($fp);
		#Now let's check for the guestbook file
		#if it doesn't, then create it
		if(!file_exists($config[out_file])) {
			if(!$fp=@fopen("$config[out_file]", "w")) {
				$msg .= "The script was unable to create the file <b>$config[out_file]</b>, you will have to create it yourself "
					.   "or, if the config file was created, delete the config file and change the location of the Output File. ";
			}
			else {
				if($config[gbook_template]==1) {
					$header=DoHeader("The Entries");
					$footer=DoFooter();
				}
				else{$header="";}
				@fwrite($fp, "$header\n");
				@fwrite($fp, "<center><font face=\"$config[font_face]\" size=\"+2\"><b>$config[site_name]</b></font></center>\n");
				@fwrite($fp, "<!--GBOOK-->\n");
				@fwrite($fp, "$footer\n");
				@fclose($fp);
			}
		}
		print <<<___eofh
$config[opentable]
	<tr bgcolor="#77aadd">
		<td width="130"A>
			<a href="http://devel.narnarnar.com">
			<img src="http://devel.narnarnar.com/images/logo_77aadd.gif"  border="0" align="right" alt="Nar3 Devel"></a>
		</td>
		<td>
			<font face="$config[font_face]" size="+2" color="$config[table_font_color]">
			<b>Nbook: Nar3 Guestbook<br>v1.1.2</b>
			</font>
		</td>
	</tr>
	
	<tr valign="middle" bgcolor="$config[table_head_bgcolor]">
		<td align="center" colspan="2">
			<font face="$config[font_face]" size="+2" color="$config[table_font_color]"><I><B>Configuration<i></b></font>
		</td>
	</tr>
	<tr>
		<td colspan="2">
			$config[tabfonttag]
			$msg
			</font>
		</td>
	</tr>
</table>
<br>
___eofh;
		$footer=DoFooter();
		print $footer;
		exit;
	}
	
	#Print out the form to configure the script
	print <<<___eofh
<center>

$config[opentable]
	<tr bgcolor="#77aadd">
		<td width="130">
			<a href="http://devel.narnarnar.com">
			<img src="http://devel.narnarnar.com/images/logo_77aadd.gif"  border="0" align="right" alt="Nar3 Devel"></a>
		</td>
		<td>
			<font face="$config[font_face]" size="+2" color="$config[table_font_color]">
			<b>Nbook: Nar3 Guestbook<br>v1.1.2</b>
			</font>
		</td>
	</tr>
	
	<tr valign="middle" bgcolor="$config[table_head_bgcolor]">
		<td align="center" colspan="2">
			<font face="$config[font_face]" size="+2" color="$config[table_font_color]"><I><B>Configuration<i></b></font>
		</td>
	</tr>
</table>
<br>
$config[opentable]
	<tr bgcolor="$config[table_head_bgcolor]">
		<td>
			<font face="$config[font_face]" size="+1" color="$config[table_font_color]"><I><B>Messages<i></b></font>
		</td>
	</tr>
	<tr>
		<td width="400">
			$config[tabfonttag]
			This is your first time using NBook, so before you can actually put it into action, you have to configure it.
			You should be able to just create the config file and be on your way. To reset the variables to predefined
			values, click <a href="nbook.php">here</a>.<br>
			$msg
			</font>
		</td>
	</tr>
</table>
___eofh;


print <<<___eofh
<br>
<form method="POST" action="nbook.php">

$config[opentable]
	<tr bgcolor="$config[table_head_bgcolor]">
		<td bgcolor="#77aadd" colspan="2">
			<font face="$config[font_face]" size="+1" color="$config[table_font_color]"><b><i>General Configuration</i></b></font>
		</td>
	</tr>
	<tr valign="top">
		<td>
			$config[tabfonttag]
			Use a Template<br>
			<font size="-2">1 for yes, 0 for no</font>
			</font>
		</td>
		<td>
			<input type="text" value="$config[use_template]" name="use_template">
		</td>
	</tr>
	<tr valign="top">
		<td>
			$config[tabfonttag]
			Template File<br>
			<font size="-2">
			Leave blank if you don&#039t want a template.<br>
			Script replaces %%NBOOK%% with the guestbook, and %%TITLE%% with the<br>
			title in the template. Both are required.
			</font>
		</td>
		<td>
			<input type="text" value="$config[template]" name="template">
		</td>
	</tr>
	<tr valign="top">
		<td>
			$config[tabfonttag]
			Output File<br>
			</font>
			<font size="-2">What file do you want the guestbook to output to?</font>
		</td>
		<td>
			<input type="text" value="$config[out_file]" name="out_file">
		</td>
	</tr>
	<tr valign="top">
		<td>
			$config[tabfonttag]
			Template on Output File<br>
			</font>
			<font size="-2">If you would like a header and footer on the guestbook file,<br> set this to 1, otherwise, 0.</font>
		</td>
		<td>
			<input type="text" value="$config[gbook_template]" name="gbook_template">
		</td>
	</tr>
	<tr valign="top">
		<td>
			$config[tabfonttag]
			Guestbook Name<br>
			</font>
			<font size="-2">
			If you want the header and footer applied to the output<br>file, set to 1, otherwise, set to 0.<br>
			If this doesn&#039t make any sense, leave a 1.
			</font>
		</td>
		<td>
			<input type="text" value="$config[site_name]" name="site_name">
		</td>
	</tr>
	</tr>
	<tr valign="top">
		<td>
			$config[tabfonttag]
			Guestbook Directory URL<br>
			<font size="-2">No trailing slash</font>
			</font>
		</td>
		<td>
			<input type="text" value="$config[gbook_url]" name="site_url">
		</td>
	</tr>
	<tr valign="top">
		<td>
			$config[tabfonttag]
			Script Filename<br>
			</font>
			<font size="-2">The name of this file.</font>
		</td>
		<td>
			<input type="text" value="$config[script_filename]" name="script_filename">
		</td>
	</tr>
	<tr valign="top">
		<td>
			$config[tabfonttag]
			Give me credit?<br>
			</font>
			<font size="-2">If you really want to, you can set this to 0 to disable<br> the "Powered By" tag at the bottom of the page.</font>
		</td>
		<td>
			<input type="text" value="$config[give_credit]" name="give_credit">
		</td>
	</tr>
</table>
<br><br>
$config[opentable]
	<tr bgcolor="$config[table_head_bgcolor]">
		<td colspan="2">
			<font face="$config[font_face]" size="+1" color="$config[table_font_color]"><b><i>Page Style Configuration</i></b></font>
		</td>
	</tr>
	<tr>
		<td>
			$config[tabfonttag]
			Background Color
			</font>
		</td>
		<td>
			<input type="text" value="$config[bgcolor]" name="bgcolor">
		</tr>
	</tr>
	<tr>
		<td>
			$config[tabfonttag]
			Background Picture
			</font>
		</td>
		<td>
			<input type="text" value="$config[background]" name="background">
		</td>
	</tr>
	<tr>
		<td>
			$config[tabfonttag]
			Text Color
			</font>
		</td>
		<td>
			<input type="text" value="$config[text]" name="text">
		</tr>
	</tr>
	<tr>
		<td>
			$config[tabfonttag]
			Link Color
			</font>
		</td>
		<td>
			<input type="text" value="$config[link]" name="link">
		</td>
	</tr>
	<tr>
		<td>
			$config[tabfonttag]
			Visited Link Color
			</font>
		</td>
		<td>
			<input type="text" value="$config[vlink]" name="vlink">
		</td>
	</tr>
	<tr>
		<td>
			$config[tabfonttag]
			Active Link Color
			</font>
		</td>
		<td>
			<input type="text" value="$config[alink]" name="alink">
		</td>
	</tr>
	<tr>
		<td>
			$config[tabfonttag]
			Font Face
			</font>
		</td>
		<td>
			<input type="text" value="$config[font_face]" name="font_face">
		</td>
	</tr>
	<tr>
		<td>
			$config[tabfonttag]
			Font Size
			</font>
		</td>
		<td>
			<input type="text" value="$config[font_size]" name="font_size">
		</td>
	</tr>
</table>
<br>
<br>
$config[opentable]
	<tr bgcolor="$config[table_head_bgcolor]">
		<td colspan="2">
			<font face="$config[font_face]" size="+1" color="$config[table_font_color]"><b><i>Table Configuration</i></b></font>
		</td>
	</tr>
	<tr>
		<td>
			$config[tabfonttag]
			Header Background Color
			</font>
		</td>
		<td>
			<input type="text" value="$config[table_head_bgcolor]" name="table_head_bgcolor">
		</td>
	</tr>
	<tr>
		<td>
			$config[tabfonttag]
			Background Color
			</font>
		</td>
		<td>
			<input type="text" value="$config[table_bgcolor]" name="table_bgcolor">
		</td>
	</tr>
	<tr>
		<td>
			$config[tabfonttag]
			Border Size
			</font>
		</td>
		<td>
			<input type="text" value="$config[table_border]" name="table_border">
		</td>
	</tr>
	<tr>
		<td>
			$config[tabfonttag]
			Border Color
			</font>
		</td>
		<td>
			<input type="text" value="$config[table_bordercolor]" name="table_bordercolor">
		</td>
	</tr>
	<tr>
		<td>
			$config[tabfonttag]
			Text Color
			</font>
		</td>
		<td>
			<input type="text" value="$config[table_font_color]" name="table_font_color">
		</td>
	</tr>
	<tr>
		<td>
			$config[tabfonttag]
			Cellspacing
			</font>
		</td>
		<td>
			<input type="text" value="$config[table_cellspacing]" name="table_cellspacing">
		</td>
	</tr>
	<tr>
		<td>
			$config[tabfonttag]
			Cellpadding
			</font>
		</td>
		<td>
			<input type="text" value="$config[table_cellpadding]" name="table_cellpadding">
		</td>
	</tr>
</table>
$preview_hidden
<input type="submit" value="$submit_value">
</center>\n
___eofh;
	$footer=DoFooter();
	print $footer;
	exit;
}

#This function prints out the header
function DoHeader($title) {
	global $config;
	if($config[use_template]==0) {
	$header=<<<eofh
<html>
<head>
<title>$config[site_name]: $title</title>
</head>
<body bgcolor="$config[bgcolor]" text="$config[text]" background="$config[background]" link="$config[link]" alink="$config[alink]" vlink="$config[vlink]">
$config[fonttag]
eofh;
	}
	else {
		#Open the template and print everything before %%NBOOK%%
		if($file=@implode("", @file("$config[template]"))) {
                        list($header, $bottom) =split("%%NBOOK%%", $file);
                        $header=ereg_replace("%%TITLE%%", $title, $header);
                        $header;
                }
                else {
                        $config[use_template]=0;
                        print DoHeader("Fatal Error");
                        print "<b>Fatal Error:</b> The template file $config[template] could not be read. NBook is aborting...";
                        print DoFooter();
                        exit;
                }
	}
	return $header;
}

function DoFooter() {
	global $config;
	if ($config[powered_by_off]==1) {$config[powered_by]="";}
	if($config[use_template]==0) {
	$footer=<<<eofh
$config[powered_by]<br>
</font>
</body>
</html>
eofh;
	return $footer;
	}
	else {
		#Open the template and print everything before %%NBOOK%%
                #we shouldn't need to throw the template error in because if it couldn't read
                #it when it was doing the header, it shouldn't need to say it again.
		$file=@implode("", @file("$config[template]"));
                list($header, $footer) =split("%%NBOOK%%", $file);
                $footer=$config[fonttag] . $config[powered_by] . "</font>" . $footer;
                return $footer;
	}
}

function SplitWord($word) {
        if(strlen($word) > 100) {
                $word1=substr($word, 0, 100) . "\n";
                $word2=substr($word, 100, strlen($word));
                #Let's check to make sure we won't be splitting a </a>
                if(substr($word2, 0, -3) != "</a>") {
                        $word2=SplitWord($word2);
                }
                else{
                        if(strlen($word2) >=103) {
                                $word2=SplitWord($word2);
                        }
                }
         $the_word=$word1 . $word2;
       }
        else {
                $the_word=$word;
        }
        
        return $the_word;
}

?>
Return current item: NBook