<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright EB Dynamics, Inc., Edward Lemmers, P.E.
// For licensing, see license.txt.
//
// Revised: 9/30/2011	ELL
// Added building of download files for open-source customers who purchase templates.
// Mode will switch based on whether or not wizard is enabled in options_selected table.
// If wizard is enabled, setup_site.php copies files to perspective folders and runs
// SQL directly on tables just as originally designed.  If wizard is not enabled,
// setup_site.php creates SQL file from all of the SQL commands, copies all of the
// other necessary files to a temp location, zips these files into a single file, and
// initiates download of zip file to customer.  Open-source customers will then upload
// this zip file into their Breeze Website Builder which will then unpack the files and
// apply template to their site automatically by running the SQL commands in the SQL file
// and copying the perspective files to the correct locations.
// Revised: 2/10/2012	ELL
// Added table prefix handling.  Added reference to website folder configuration.
// Revised: 5/10/2013	ELL
// Added handling of new header and footer divs.
// Revised: 4/5/2014	ELL
// Retrofited for new layout templates and new plugin model.
// Revised: 6/16/2014	ELL
// Added storing of plugin ID's into page data to facilitate content sensitive help for relevant plugins in page editor.
// Revised: 12/9/2014	ELL
// Added copying of use_table_structure and use_table_structure_mobile bits.
// Revised: 1/13/2015	ELL
// Fixed bug with single quotes in plugin HTML doubling up in border areas and content.
// Revised: 10/2/2015	ELL
// Modified to set template field in page data record instead of pulling specific template.
// Modified to set border area and head fields in page data record instead of parsing template layout file to insert custom border area references.
// Added setting of body_onload function name as attribute it the page data table.
// Revised: 6/4/2016	ELL
// Fixed bug with got_splash not allowing plugins to be installed.
// Added slashing of single quotes for content HTML.
// Revised: 6/11/2016	ELL
// Modified template minimum requirements checking to include header and footer.
// Revised: 1/7/2017	ELL
// Modified to look for pages from template repository with names other than index or splash and to use those pages falling back to index if not found.
// Revised: 12/4/2019	ELL
// Globalized database connection handles throughout functions to 
// reduce CPU and memory usage and reduce time to complete building download.
// Modified: 1/31/2020	ELL
// Added handling of plugin field 'cookie_session_code'.
//
include_once ("check_session.php");

function CreatePage($page_name, $plugin, $template_name, $template_id, $cur_ts)
{
	global $bwb_dbh, $bwb_wiz_dbh, $wiz_mode;
	
	if ($page_name == "")
	{
		echo "Oops! You forgot to enter a page name.  Click <a href=\"main.php\" title=\"here\">here</a> and enter a page name.";
		exit;
	}
	
	//Remove Unix incompatible characters.
//	$page_name = ereg_replace("[[^[:space:]]", "_", $page_name);
//	$page_name = ereg_replace("[^a-zA-Z0-9\(\)\s_.-]", "", $page_name);
	$page_name = preg_replace("/(?:\s|&nbsp;)+/", "_", $page_name, -1);
	$page_name = preg_replace("/[^A-Za-z0-9_.-]/", "", $page_name, -1);
//	$query = "SELECT `option_value` FROM `options_selected` WHERE `option_name`='ForcePageNameToLowerCase'";
//	$result = $dbh->query($query);
//	$row = $result->fetch(PDO::FETCH_ASSOC);
//	$ForcePageNameToLowerCase = $row["option_value"];
//	if ($ForcePageNameToLowerCase == 1)
	$page_name = strtolower($page_name);
	$page_name = addslashes($page_name);

	//First check to see if file is already in database.
	if ($wiz_mode == 1)
	{
		$query = "SELECT `page_name` FROM `" . BWB_TABLE_PREFIX . "page_data` WHERE `page_name`='$page_name'";
		$result = $bwb_dbh->query($query);
		if ($result->rowCount() > 0)
			return("page exists");
	}

	//Test to see if file already exists.
	$document_root = $_SERVER['DOCUMENT_ROOT'];
	if ($wiz_mode == 1)
	{
		if (BWB_BASE_DIR == '')
			$userfiles = '';
		else
			$userfiles = "/" . BWB_BASE_DIR;
		if (BWB_WEBPAGE_DIR == '')
			$website_path = '';
		else
			$website_path = "/" . BWB_WEBPAGE_DIR;
		$file_name = $document_root . $userfiles . $website_path . "/" . $page_name . ".php";
	}
	else
		$file_name = $document_root . "/tmp" . $cur_ts . "/userfiles/file/" . $page_name . ".php";
//	echo "File Name: $file_name<br>";
	if (file_exists($file_name))
		$res = "page exists";
	
	// If index page, check to see if splash page exists in repository.
	$got_splash = false;
	if ($page_name == "index")
	{
		$query2 = "SELECT * FROM `page_data` WHERE `page_name`='splash' AND `template_id`=" . $template_id;
		$result2 = $bwb_wiz_dbh->query($query2);
		if ($result2->rowCount() > 0)	{
			$got_splash = true;
			$row2 = $result2->fetch(PDO::FETCH_ASSOC);
			$head_splash = $row2['head_name'];
			$header_splash = $row2['header_name'];
			$top_splash = $row2['top_name'];
			$left_splash = $row2['left_name'];
			$right_splash = $row2['right_name'];
			$bottom_splash = $row2['bottom_name'];
			$footer_splash = $row2['footer_name'];
			
			$insert_default_head_splash = $row2['insert_default_head'];
			$insert_default_header_splash = $row2['insert_default_header'];
			$insert_default_top_splash = $row2['insert_default_top'];
			$insert_default_left_splash = $row2['insert_default_left'];
			$insert_default_right_splash = $row2['insert_default_right'];
			$insert_default_bottom_splash = $row2['insert_default_bottom'];
			$insert_default_footer_splash = $row2['insert_default_footer'];
			
			$show_login_links_splash = $row2['show_login_links'];
			$disable_site_styles_splash = $row2['disable_site_styles'];
			
			$plugins_splash = $row2['plugins'];
		}
	}

	// If plugin code selected, then get plugin code snippet.
	if (((!isset($plugin)) || ($plugin == -1) || ($plugin == "")) && ($got_splash !== true))
		$plugin = "-not selected-";
	elseif ($got_splash === true)	{
		$plugin = $plugins_splash;
		if (strpos($plugin, ",") !== false)	{
			$plugin_arr = explode(",", $plugin);
			$plugin = $plugin_arr[0];
		}
	}
	
	if ($plugin != "-not selected-")
	{
		$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . "plugins` WHERE `id` = '$plugin'";
		$result = $bwb_dbh->query($query);
		if ($result->rowCount() < 1)	{
			echo "No plugin found for plugin ID " . $plugin;
			$plugin = "-not selected-";
		}
		else
		{
			$row = $result->fetch(PDO::FETCH_ASSOC);
			$plugin_id = $row["id"];
			$cookie_session_code = $row["cookie_session_code"];
			$head_code = $row["head_code"];
			$head_html = $row["head_html"];
			$header_code = $row["header_code"];
			$header_html = $row["header_html"];
			$top_code = $row["top_code"];
			$top_html = $row["top_html"];
			$left_code = $row["left_code"];
			$left_html = $row["left_html"];
			$content_code = $row["content_code"];
			$content_html = $row["content_html"];
			$right_code = $row["right_code"];
			$right_html = $row["right_html"];
			$bottom_code = $row["bottom_code"];
			$bottom_html = $row["bottom_html"];
			$footer_code = $row["footer_code"];
			$footer_html = $row["footer_html"];
			$style_sheet = $row["style_sheet"];
			$style_sheet_description = $row["style_sheet_description"];
			$class_sql = $row["class_sql"];
			$css_sql = $row["css_sql"];
			$custom_css_sql = $row["custom_css_sql"];
			$plugin_sql = $row["plugin_sql"];
			$deployed = $row["deployed"];
			$disable_site_styles = $row["disable_site_styles"];
			$body_onload = $row["body_onload"];
			$body_onload_function = $row["body_onload_function"];
			$body_onload_function = (isset($row["body_onload_function"]) ? $row["body_onload_function"] : $page_name . "_load");
			// Check template compatibility with plugin.
			if (strpos($template_name, "header_footer_top_bottom_left_right") === false)
			{
				$need_header = false;
				$need_footer = false;
				$need_top = false;
				$need_left = false;
				$need_right = false;
				$need_bottom = false;
				$need_alt = false;
				$need_alt2 = false;
				$dont_want_header = false;
				$dont_want_footer = false;
				$dont_want_top = false;
				$dont_want_left = false;
				$dont_want_right = false;
				$dont_want_bottom = false;
				
				if ((isset($header_code) && ($header_code != "")) || (isset($header_html) && ($header_html != "")) && (strpos($template_name, "header") === false))
					$need_header = true;
				if ((isset($footer_code) && ($footer_code != "")) || (isset($footer_html) && ($footer_html != "")) && (strpos($template_name, "footer") === false))
					$need_footer = true;
				if ((isset($top_code) && ($top_code != "")) || (isset($top_html) && ($top_html != "")) && (strpos($template_name, "top") === false))
					$need_top = true;
				if ((isset($left_code) && ($left_code != "")) || (isset($left_html) && ($left_html != "")) && (strpos($template_name, "left") === false))
					$need_left = true;
				if ((isset($right_code) && ($right_code != "")) || (isset($right_html) && ($right_html != "")) && (strpos($template_name, "right") === false))
					$need_right = true;
				if ((isset($bottom_code) && ($bottom_code != "")) || (isset($bottom_html) && ($bottom_html != "")) && (strpos($template_name, "bottom") === false))
					$need_bottom = true;
					
				if (strpos($template_name, "header") === false)
					$dont_want_header = true;
				if (strpos($template_name, "footer") === false)
					$dont_want_footer = true;
				if (strpos($template_name, "top") === false)
					$dont_want_top = true;
				if (strpos($template_name, "left") === false)
					$dont_want_left = true;
				if (strpos($template_name, "right") === false)
					$dont_want_right = true;
				if (strpos($template_name, "bottom") === false)
					$dont_want_bottom = true;
					
				if (strpos($template_name, "alt2") !== false)
					$need_alt2 = true;
				elseif (strpos($template_name, "alt") !== false)
					$need_alt = true;
					
				if (($need_header === true) || ($need_footer === true) || ($need_top === true) || ($need_left === true) || ($need_right === true) || ($need_bottom === true))
				{
					$where = "";
					if ($need_header === true)
						$where .= " AND option_value LIKE '%header%'";
					elseif (($dont_want_header === true) && ($need_footer !== true))
						$where .= " AND option_value NOT LIKE '%header%'";
					if ($need_footer === true)
						$where .= " AND option_value LIKE '%footer%'";
					elseif ($dont_want_footer === true)
						$where .= " AND option_value NOT LIKE '%footer%'";
					if ($need_top === true)
						$where .= " AND option_value LIKE '%top%'";
					elseif (($dont_want_top === true) && ($need_bottom !== true))
						$where .= " AND option_value NOT LIKE '%top%'";
					if ($need_left === true)
						$where .= " AND option_value LIKE '%left%'";
					elseif (($dont_want_left === true) && ($need_right !== true))
						$where .= " AND option_value NOT LIKE '%left%'";
					if ($need_right === true)
						$where .= " AND option_value LIKE '%right%'";
					elseif ($dont_want_right === true)
						$where .= " AND option_value NOT LIKE '%right%'";
					if ($need_bottom === true)
						$where .= " AND option_value LIKE '%bottom%'";
					elseif ($dont_want_bottom === true)
						$where .= " AND option_value NOT LIKE '%bottom%'";
						
					if ($need_alt2 === true)
						$where .= " AND option_value LIKE '%alt2%'";
					elseif ($need_alt === true)
						$where .= " AND option_value LIKE '%alt%' AND NOT LIKE '%alt2%'";
						
					$query = "SELECT option_value FROM " . BWB_TABLE_PREFIX . "options WHERE option_name = 'Template'" . $where . " ORDER BY dropdown_sort_order";
					$result = $bwb_dbh->query($query);
					$row = $result->fetch(PDO::FETCH_ASSOC);
					$template_name = $row['option_value'];
				}
			}
		}
	}
	else
	{
		$content_html = "\n<p>Enter your content here.</p>\n";
		$plugin_id = "";
		$body_onload = 0;
		$body_onload_function = $page_name . "_load";
		$disable_site_styles = 0;
	}

	//Copy repsossitory index page to new page record (if basic page without plugin).
	$query = "SELECT * FROM `page_data` WHERE `page_name`='" . $page_name . "' AND template_id=" . $template_id;
	if ($got_splash === true)
		$query = "SELECT * FROM `page_data` WHERE `page_name`='splash' AND template_id=" . $template_id;
	$result = $bwb_wiz_dbh->query($query);
	if ($result->rowCount() < 1)	{
		$query = "SELECT * FROM `page_data` WHERE `page_name`='index' AND template_id=" . $template_id;
		$result = $bwb_wiz_dbh->query($query);
	}
	$row = $result->fetch(PDO::FETCH_ASSOC);
	$page_content = addslashes(stripslashes($row['page_content']));
	
	if (isset($row['use_table_structure']) && ($row['use_table_structure'] != ""))
		$use_table_structure = $row['use_table_structure'];
	else
		$use_table_structure = 1;
		
	if (isset($row['use_table_structure_mobile']) && ($row['use_table_structure_mobile'] != ""))
		$use_table_structure_mobile = $row['use_table_structure_mobile'];
	else
		$use_table_structure_mobile = 0;
		
	$revised = $row['revised'];
	
	if (isset($row['plugins']) && ($row['plugins'] != ""))	{
		if (isset($plugin_id) && ($plugin != "-not selected-") && ($plugin_id != ""))
			$plugin_id .= "," . $row['plugins'];
		else
			$plugin_id = $row['plugins'];
	}
	
	if (isset($row['body_onload']) && ($row['body_onload'] == 1) && isset($row['body_onload_function']) && ($row['body_onload_function'] != "") && ($body_onload == 1) && ($row['body_onload_function'] != $body_onload_function))	{
		if (strpos($body_onload_function, ";") === false)
			$body_onload_function .= "(); " . $row['body_onload_function'];
		else
			$body_onload_function .= " " . $row['body_onload_function'];
	}
	elseif (isset($row['body_onload']) && ($row['body_onload'] == 1) && isset($row['body_onload_function']) && ($row['body_onload_function'] != "") && ($body_onload == 0))
		$body_onload_function = $row['body_onload_function'];
		
	if (($body_onload == 0) && isset($row['body_onload']) && ($row['body_onload'] == 1))
		$body_onload = 1;
		
	$head_name = $row['head_name'];
	$header_name = $row['header_name'];
	$top_name = $row['top_name'];
	$left_name = $row['left_name'];
	$right_name = $row['right_name'];
	$bottom_name = $row['bottom_name'];
	$footer_name = $row['footer_name'];
	
	$insert_default_head = $row['insert_default_head'];
	$insert_default_header = $row['insert_default_header'];
	$insert_default_top = $row['insert_default_top'];
	$insert_default_left = $row['insert_default_left'];
	$insert_default_right = $row['insert_default_right'];
	$insert_default_bottom = $row['insert_default_bottom'];
	$insert_default_footer = $row['insert_default_footer'];

	$show_login_links = $row['show_login_links'];
	$disable_site_styles = $row['disable_site_styles'];
	
	//Add page name to database.
	$content_html = addslashes(stripslashes($content_html));
	$insert_verb = "REPLACE";
	if ($wiz_mode == 1)
		$insert_verb = "INSERT";
	if (($plugin != "-not selected-") && ($content_html != "") && ($got_splash !== true))
		$query = "$insert_verb INTO " . BWB_TABLE_PREFIX . "page_data SET page_name='$page_name', page_content='$content_html', template='$template_name', body_onload='$body_onload', body_onload_function='$body_onload_function', use_table_structure=$use_table_structure, use_table_structure_mobile=$use_table_structure_mobile, revised='$revised', plugins='$plugin_id'";
	else
		$query = "$insert_verb INTO `" . BWB_TABLE_PREFIX . "page_data` SET `page_name`='$page_name', `page_content`='$page_content', template='$template_name', body_onload='$body_onload', body_onload_function='$body_onload_function', use_table_structure=$use_table_structure, use_table_structure_mobile=$use_table_structure_mobile, `revised`='$revised', plugins='$plugin_id'";
		
	if ($got_splash === true)	{
		$query .= ", head_name='$head_splash', header_name='$header_splash', top_name='$top_splash', left_name='$left_splash', right_name='$right_splash', bottom_name='$bottom_splash', footer_name='$footer_splash', insert_default_head='$insert_default_head_splash', insert_default_header='$insert_default_header_splash', insert_default_top='$insert_default_top_splash', insert_default_left='$insert_default_left_splash', insert_default_right='$insert_default_right_splash', insert_default_bottom='$insert_default_bottom_splash', insert_default_footer='$insert_default_footer_splash', show_login_links='$show_login_links_splash', disable_site_styles='$disable_site_styles_splash'";
	}
	else	{
		$query .= ", head_name='$head_name', header_name='$header_name', top_name='$top_name', left_name='$left_name', right_name='$right_name', bottom_name='$bottom_name', footer_name='$footer_name', insert_default_head='$insert_default_head',		insert_default_header='$insert_default_header', insert_default_top='$insert_default_top', insert_default_left='$insert_default_left', insert_default_right='$insert_default_right', insert_default_bottom='$insert_default_bottom', insert_default_footer='$insert_default_footer',	show_login_links='$show_login_links', disable_site_styles='$disable_site_styles'";
	}
//return ($query);
		
	if ($wiz_mode == 1)	{
		try {
			$cnt = $bwb_dbh->exec($query);
		} catch (PDOException $e) {
			echo 'Error inserting into page_data table: ' . $e->getMessage();
		}
	}
	else	{
		$query = str_replace( chr(10), "\\n", $query );
		$query = str_replace( chr(13), "\\r", $query );
		$sql = $query . ";\n\n";
	}
//	echo "\n\nquery: $query<br>\n\n\n";

	//Get template from database.
	$query = "SELECT `page_template` FROM `" . BWB_TABLE_PREFIX . "template` WHERE `template_name` = 'all'";
	$result = $bwb_dbh->query($query);
	$row = $result->fetch(PDO::FETCH_ASSOC);
	$page_template = $row["page_template"];
	
	//Insert page name into template.
	$zzz_found = false;
	if ($page_name == "zzz")	{
		$page_template = str_replace("xxx", "-ZZZ-", $page_template);
		$zzz_found = true;
	}
	else
		$page_template = str_replace("xxx", $page_name, $page_template);
	
	//Get meta-tags template from database.
	$query = "SELECT `meta_tags_template` FROM `" . BWB_TABLE_PREFIX . "meta_tags_template`";
	$result = $bwb_dbh->query($query);
	$row = $result->fetch(PDO::FETCH_ASSOC);
	$meta_tags_template = $row["meta_tags_template"];
//	echo "query: $query<br>";
	
	//Insert page name into meta-tags template.
	$meta_tags_template = str_replace("xxx", $page_name, $meta_tags_template);
	
	//Insert meta-tags template into page template.
	$page_template = str_replace("zzz", $meta_tags_template, $page_template);
	
	if ($zzz_found === true)
		$page_template = str_replace("-ZZZ-", $page_name, $page_template);
		
	//If plugin is selected, then insert cookie/session code snippet after //cookie_session_code// comment.
	if (($plugin != "-not selected-") && ($cookie_session_code != ""))
	{
		$page_temp_arr = explode("//cookie_session_code//", $page_template, 2);
		$page_template = $page_temp_arr[0] . $cookie_session_code . "\n\n//cookie_session_code//\n" . $page_temp_arr[1];
		unset($page_temp_arr);
	}

	//If plugin is selected, then insert head code snippet after <!--head_code--> comment.
	if (($plugin != "-not selected-") && ($head_code != ""))
	{
		$page_temp_arr = explode("<!--head_code-->", $page_template, 2);
		$page_template = $page_temp_arr[0] . $head_code . "\n\n<!--head_code-->\n" . $page_temp_arr[1];
		unset($page_temp_arr);
	}
	
	//If plugin is selected, then insert content code snippet after <!--content_code--> comment.
	if (($plugin != "-not selected-") && ($content_code != ""))
	{
		$page_temp_arr = explode("<!--content_code-->", $page_template, 5);
		$page_template = $page_temp_arr[0] . $content_code . "\n\n<!--content_code-->\n" . $page_temp_arr[1];
		if (isset($page_temp_arr[2]))
			$page_template .= $content_code . "\n\n<!--content_code-->\n" . $page_temp_arr[2];
		if (isset($page_temp_arr[3]))
			$page_template .= $content_code . "\n\n<!--content_code-->\n" . $page_temp_arr[3];
		if (isset($page_temp_arr[4]))
			$page_template .= $content_code . "\n\n<!--content_code-->\n" . $page_temp_arr[4];
		unset($page_temp_arr);
	}
		
	//If plugin is selected, then insert header code snippet after <!--header_code--> comment. (not to be confused with head)
	if (($plugin != "-not selected-") && ($header_code != ""))
	{
		$page_temp_arr = explode("<!--header_code-->", $page_template, 2);
		$page_template = $page_temp_arr[0] . $header_code . "\n\n<!--header_code-->\n" . $page_temp_arr[1];
		unset($page_temp_arr);
	}
		
	//If plugin is selected, then insert footer code snippet after <!--footer_code--> comment.
	if (($plugin != "-not selected-") && ($footer_code != ""))
	{
		$page_temp_arr = explode("<!--footer_code-->", $page_template, 2);
		$page_template = $page_temp_arr[0] . $footer_code . "\n\n<!--footer_code-->\n" . $page_temp_arr[1];
		unset($page_temp_arr);
	}

	//If plugin is selected, then insert top code snippet after <!--top_code--> comment.
	if (($plugin != "-not selected-") && ($top_code != ""))
	{
		$page_temp_arr = explode("<!--top_code-->", $page_template, 4);
		$page_template = $page_temp_arr[0] . $top_code . "\n\n<!--top_code-->\n" . $page_temp_arr[1];
		if (isset($page_temp_arr[2]))
			$page_template .= $top_code . "\n\n<!--top_code-->\n" . $page_temp_arr[2];
		if (isset($page_temp_arr[3]))
			$page_template .= $top_code . "\n\n<!--top_code-->\n" . $page_temp_arr[3];
		unset($page_temp_arr);
	}
	
	//If plugin is selected, then insert left code snippet after <!--left_code--> comment.
	if (($plugin != "-not selected-") && ($left_code != ""))
	{
		$page_temp_arr = explode("<!--left_code-->", $page_template, 4);
		$page_template = $page_temp_arr[0] . $left_code . "\n\n<!--left_code-->\n" . $page_temp_arr[1];
		if (isset($page_temp_arr[2]))
			$page_template .= $left_code . "\n\n<!--left_code-->\n" . $page_temp_arr[2];
		if (isset($page_temp_arr[3]))
			$page_template .= $left_code . "\n\n<!--left_code-->\n" . $page_temp_arr[3];
		unset($page_temp_arr);
	}
	
	//If plugin is selected, then insert right code snippet after <!--right_code--> comment.
	if (($plugin != "-not selected-") && ($right_code != ""))
	{
		$page_temp_arr = explode("<!--right_code-->", $page_template, 4);
		$page_template = $page_temp_arr[0] . $right_code . "\n\n<!--right_code-->\n" . $page_temp_arr[1];
		if (isset($page_temp_arr[2]))
			$page_template .= $right_code . "\n\n<!--right_code-->\n" . $page_temp_arr[2];
		if (isset($page_temp_arr[3]))
			$page_template .= $right_code . "\n\n<!--right_code-->\n" . $page_temp_arr[3];
		unset($page_temp_arr);
	}
	
	//If plugin is selected, then insert bottom code snippet after <!--bottom_code--> comment.
	if (($plugin != "-not selected-") && ($bottom_code != ""))
	{
		$page_temp_arr = explode("<!--bottom_code-->", $page_template, 4);
		$page_template = $page_temp_arr[0] . $bottom_code . "\n\n<!--bottom_code-->\n" . $page_temp_arr[1];
		if (isset($page_temp_arr[2]))
			$page_template .= $bottom_code . "\n\n<!--bottom_code-->\n" . $page_temp_arr[2];
		if (isset($page_temp_arr[3]))
			$page_template .= $bottom_code . "\n\n<!--bottom_code-->\n" . $page_temp_arr[3];
		unset($page_temp_arr);
	}	

	//If plugin is selected, then replace put HTML head  holder with head code snippet.
	if (($plugin != "-not selected-") && ($head_html != "") && ($got_splash !== true))
	{
		// Get last head name.
		$query = "SELECT head_name FROM " . BWB_TABLE_PREFIX . "head ORDER BY head_name DESC";
		$result = $bwb_dbh->query($query);
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$head_name = $row["head_name"];
//		echo "query: $query<br>";
		
		// Get head content from main head.  Most likely that we want this as default.  Can edit later.
		$query = "SELECT head_content FROM " . BWB_TABLE_PREFIX . "head WHERE head_name = 'head'";
		$result = $bwb_dbh->query($query);
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$head_content = $row["head_content"];
//		echo "query: $query<br>";
		
		// Combine HTML (Javascript) code with default head content.
		$head_content = $head_content . "\n\n\n" . $head_html;
		$head_content = addslashes(stripslashes($head_content));
		
		// Insert new head into table.
		if ($head_name == "head")
		{
			$query = $insert_verb . " INTO " . BWB_TABLE_PREFIX . "head (head_name, head_content, plugins) VALUES ('head1', '" . $head_content . "', '" . $plugin_id . "')";
			$index = 1;
		}
		else
		{
			$index = substr($head_name, strlen("head"));
			$index = $index + 1;
			$query = $insert_verb . " INTO " . BWB_TABLE_PREFIX . "head (head_name, head_content, plugins) VALUES ('head" . $index . "', '" . $head_content . "', '" . $plugin_id . "')";
		}
		if ($wiz_mode == 1)
			$cnt = $bwb_dbh->exec($query);
		else	{
			$query = str_replace( chr(10), "\\n", $query );
			$query = str_replace( chr(13), "\\r", $query );
			$sql .= $query . ";\n\n";
		}
		
		// Update head name in page record.
		$query = "UPDATE " . BWB_TABLE_PREFIX . "page_data SET head_name='$head_name' WHERE page_name='$page_name'";
		if ($wiz_mode == 1)
			$cnt = $bwb_dbh->exec($query);
		else
			$sql .= $query . ";\n\n";
	}
//		echo "page_template: $page_template<br>";

	//If plugin is selected, then replace HTML header holder with header HTML code snippet.
	if (($plugin != "-not selected-") && (isset($header_html)) && ($header_html != "") && ($got_splash !== true))
	{
		// Get last header name.
		$query = "SELECT page_name FROM " . BWB_TABLE_PREFIX . "page_data_special WHERE page_name LIKE 'header%' ORDER BY page_name DESC";
		$result = $bwb_dbh->query($query);
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$header_name = $row["page_name"];
		
		// Get header content from main header.  Most likely that we want this as default.  Can edit later.
		$query = "SELECT page_content FROM " . BWB_TABLE_PREFIX . "page_data_special WHERE page_name = 'header'";
		$result = $bwb_dbh->query($query);
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$header_content = $row["page_content"];
	
		// Combine HTML (Javascript) code with default header content.
		$header_content = $header_content . "\n\n\n" . $header_html;
		$header_content = addslashes(stripslashes($header_content));
		
		// Insert new header into table.
		if ($header_name == "header")
		{
			$query = $insert_verb . " INTO " . BWB_TABLE_PREFIX . "page_data_special (page_name, page_content, `revised`, plugins) VALUES ('header1', '" . $header_content . "', '" . $revised . "', '" . $plugin_id . "')";
			$index = 1;
		}
		else
		{
			$index = substr($header_name, strlen("header"));
			$index = $index + 1;
			$query = $insert_verb . " INTO " . BWB_TABLE_PREFIX . "page_data_special (page_name, page_content, `revised`, plugins) VALUES ('header" . $index . "', '" . $header_content . "', '" . $revised . "', '" . $plugin_id . "')";
		}
		if ($wiz_mode == 1)
			$cnt = $bwb_dbh->exec($query);
		else	{
			$query = str_replace( chr(10), "\\n", $query );
			$query = str_replace( chr(13), "\\r", $query );
			$sql .= $query . ";\n\n";
		}
			
		// Update header name in page record.
		$query = "UPDATE " . BWB_TABLE_PREFIX . "page_data SET header_name='$header_name' WHERE page_name='$page_name'";
		if ($wiz_mode == 1)
			$cnt = $bwb_dbh->exec($query);
		else
			$sql .= $query . ";\n\n";
	}

	//If plugin is selected, then replace HTML footer holder with footer HTML code snippet.
	if (($plugin != "-not selected-") && (isset($footer_html)) && ($footer_html != "") && ($got_splash !== true))
	{
		// Get last footer name.
		$query = "SELECT page_name FROM " . BWB_TABLE_PREFIX . "page_data_special WHERE page_name LIKE 'footer%' ORDER BY page_name DESC";
		$result = $bwb_dbh->query($query);
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$footer_name = $row["page_name"];
			
		// Get footer content from main footer.  Most likely that we want this as default.  Can edit later.
		$query = "SELECT page_content FROM " . BWB_TABLE_PREFIX . "page_data_special WHERE page_name = 'footer'";
		$result = $bwb_dbh->query($query);
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$footer_content = $row["page_content"];
		
		// Combine HTML (Javascript) code with default footer content.
		$footer_content = $footer_content . "\n\n\n" . $footer_html;
		$footer_content = addslashes(stripslashes($footer_content));
		
		// Insert new footer into table.
		if ($footer_name == "footer")
		{
			$query = $insert_verb . " INTO " . BWB_TABLE_PREFIX . "page_data_special (page_name, page_content, `revised`, plugins) VALUES ('footer1', '" . $footer_content . "', '" . $revised . "', '" . $plugin_id . "')";
			$index = 1;
		}
		else
		{
			$index = substr($footer_name, strlen("footer"));
			$index = $index + 1;
			$query = $insert_verb . " INTO " . BWB_TABLE_PREFIX . "page_data_special (page_name, page_content, `revised`, plugins) VALUES ('footer" . $index . "', '" . $footer_content . "', '" . $revised . "', '" . $plugin_id . "')";
		}
		if ($wiz_mode == 1)
			$cnt = $bwb_dbh->exec($query);
		else	{
			$query = str_replace( chr(10), "\\n", $query );
			$query = str_replace( chr(13), "\\r", $query );
			$sql .= $query . ";\n\n";
		}
		
		// Update footer name in page record.
		$query = "UPDATE " . BWB_TABLE_PREFIX . "page_data SET footer_name='$footer_name' WHERE page_name='$page_name'";
		if ($wiz_mode == 1)
			$cnt = $bwb_dbh->exec($query);
		else
			$sql .= $query . ";\n\n";
	}
	
	//If plugin is selected, then replace HTML top holder with top HTML code snippet.
	if (($plugin != "-not selected-") && (isset($top_html)) && ($top_html != "") && ($got_splash !== true))
	{
		// Get last top name.
		$query = "SELECT page_name FROM " . BWB_TABLE_PREFIX . "page_data_special WHERE page_name LIKE 'top%' ORDER BY page_name DESC";
		$result = $bwb_dbh->query($query);
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$top_name = $row["page_name"];
			
		// Get top content from main top.  Most likely that we want this as default.  Can edit later.
		$query = "SELECT page_content FROM " . BWB_TABLE_PREFIX . "page_data_special WHERE page_name = 'top'";
		$result = $bwb_dbh->query($query);
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$top_content = $row["page_content"];
		
		// Combine HTML (Javascript) code with default top content.
		$top_content = $top_content . "\n\n\n" . $top_html;
		$top_content = addslashes(stripslashes($top_content));
		
		// Insert new top into table.
		if ($top_name == "top")
		{
			$query = $insert_verb . " INTO " . BWB_TABLE_PREFIX . "page_data_special (page_name, page_content, `revised`, plugins) VALUES ('top1', '" . $top_content . "', '" . $revised . "', '" . $plugin_id . "')";
			$index = 1;
		}
		else
		{
			$index = substr($top_name, strlen("top"));
			$index = $index + 1;
			$query = $insert_verb . " INTO " . BWB_TABLE_PREFIX . "page_data_special (page_name, page_content, `revised`, plugins) VALUES ('top" . $index . "', '" . $top_content . "', '" . $revised . "', '" . $plugin_id . "')";
		}
		if ($wiz_mode == 1)
			$cnt = $bwb_dbh->exec($query);
		else	{
			$query = str_replace( chr(10), "\\n", $query );
			$query = str_replace( chr(13), "\\r", $query );
			$sql .= $query . ";\n\n";
		}
		
		// Update top name in page record.
		$query = "UPDATE " . BWB_TABLE_PREFIX . "page_data SET top_name='$top_name' WHERE page_name='$page_name'";
		if ($wiz_mode == 1)
			$cnt = $bwb_dbh->exec($query);
		else
			$sql .= $query . ";\n\n";
	}
	
	//If plugin is selected, then replace HTML left holder with left HTML code snippet.
	if (($plugin != "-not selected-") && (isset($left_html)) && ($left_html != "") && ($got_splash !== true))
	{
		// Get last left name.
		$query = "SELECT page_name FROM " . BWB_TABLE_PREFIX . "page_data_special WHERE page_name LIKE 'left%' ORDER BY page_name DESC";
		$result = $bwb_dbh->query($query);
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$left_name = $row["page_name"];
			
		// Get left content from main left.  Most likely that we want this as default.  Can edit later.
		$query = "SELECT page_content FROM " . BWB_TABLE_PREFIX . "page_data_special WHERE page_name = 'left'";
		$result = $bwb_dbh->query($query);
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$left_content = $row["page_content"];
		
		// Combine HTML (Javascript) code with default left content.
		$left_content = $left_content . "\n\n\n" . $left_html;
		$left_content = addslashes(stripslashes($left_content));
		
		// Insert new left into table.
		if ($left_name == "left")
		{
			$query = $insert_verb . " INTO " . BWB_TABLE_PREFIX . "page_data_special (page_name, page_content, `revised`, plugins) VALUES ('left1', '" . $left_content . "', '" . $revised . "', '" . $plugin_id . "')";
			$index = 1;
		}
		else
		{
			$index = substr($left_name, strlen("left"));
			$index = $index + 1;
			$query = $insert_verb . " INTO " . BWB_TABLE_PREFIX . "page_data_special (page_name, page_content, `revised`, plugins) VALUES ('left" . $index . "', '" . $left_content . "', '" . $revised . "', '" . $plugin_id . "')";
		}
		if ($wiz_mode == 1)
			$cnt = $bwb_dbh->exec($query);
		else	{
			$query = str_replace( chr(10), "\\n", $query );
			$query = str_replace( chr(13), "\\r", $query );
			$sql .= $query . ";\n\n";
		}
		
		// Update left name in page record.
		$query = "UPDATE " . BWB_TABLE_PREFIX . "page_data SET left_name='$left_name' WHERE page_name='$page_name'";
		if ($wiz_mode == 1)
			$cnt = $bwb_dbh->exec($query);
		else
			$sql .= $query . ";\n\n";
	}
	
	//If plugin is selected, then replace HTML right holder with right HTML code snippet.
	if (($plugin != "-not selected-") && (isset($right_html)) && ($right_html != "") && ($got_splash !== true))
	{
		// Get last right name.
		$query = "SELECT page_name FROM " . BWB_TABLE_PREFIX . "page_data_special WHERE page_name LIKE 'right%' ORDER BY page_name DESC";
		$result = $bwb_dbh->query($query);
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$right_name = $row["page_name"];
			
		// Get right content from main right.  Most likely that we want this as default.  Can edit later.
		$query = "SELECT page_content FROM " . BWB_TABLE_PREFIX . "page_data_special WHERE page_name = 'right'";
		$result = $bwb_dbh->query($query);
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$right_content = $row["page_content"];
		
		// Combine HTML (Javascript) code with default right content.
		$right_content = $right_content . "\n\n\n" . $right_html;
		$right_content = addslashes(stripslashes($right_content));
		
		// Insert new right into table.
		if ($right_name == "right")
		{
			$query = $insert_verb . " INTO " . BWB_TABLE_PREFIX . "page_data_special (page_name, page_content, `revised`, plugins) VALUES ('right1', '" . $right_content . "', '" . $revised . "', '" . $plugin_id . "')";
			$index = 1;
		}
		else
		{
			$index = substr($right_name, strlen("right"));
			$index = $index + 1;
			$query = $insert_verb . " INTO " . BWB_TABLE_PREFIX . "page_data_special (page_name, page_content, `revised`, plugins) VALUES ('right" . $index . "', '" . $right_content . "', '" . $revised . "', '" . $plugin_id . "')";
		}
		if ($wiz_mode == 1)
			$cnt = $bwb_dbh->exec($query);
		else	{
			$query = str_replace( chr(10), "\\n", $query );
			$query = str_replace( chr(13), "\\r", $query );
			$sql .= $query . ";\n\n";
		}
		
		// Update right name in page record.
		$query = "UPDATE " . BWB_TABLE_PREFIX . "page_data SET right_name='$right_name' WHERE page_name='$page_name'";
		if ($wiz_mode == 1)
			$cnt = $bwb_dbh->exec($query);
		else
			$sql .= $query . ";\n\n";
	}
	
	//If plugin is selected, then replace HTML bottom holder with bottom HTML code snippet.
	if (($plugin != "-not selected-") && (isset($bottom_html)) && ($bottom_html != "") && ($got_splash !== true))
	{
		// Get last bottom name.
		$query = "SELECT page_name FROM " . BWB_TABLE_PREFIX . "page_data_special WHERE page_name LIKE 'bottom%' ORDER BY page_name DESC";
		$result = $bwb_dbh->query($query);
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$bottom_name = $row["page_name"];
			
		// Get bottom content from main bottom.  Most likely that we want this as default.  Can edit later.
		$query = "SELECT page_content FROM " . BWB_TABLE_PREFIX . "page_data_special WHERE page_name = 'bottom'";
		$result = $bwb_dbh->query($query);
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$bottom_content = $row["page_content"];
		
		// Combine HTML (Javascript) code with default bottom content.
		$bottom_content = $bottom_content . "\n\n\n" . $bottom_html;
		$bottom_content = addslashes(stripslashes($bottom_content));
		
		// Insert new bottom into table.
		if ($bottom_name == "bottom")
		{
			$query = $insert_verb . " INTO " . BWB_TABLE_PREFIX . "page_data_special (page_name, page_content, `revised`, plugins) VALUES ('bottom1', '" . $bottom_content . "', '" . $revised . "', '" . $plugin_id . "')";
			$index = 1;
		}
		else
		{
			$index = substr($bottom_name, strlen("bottom"));
			$index = $index + 1;
			$query = $insert_verb . " INTO " . BWB_TABLE_PREFIX . "page_data_special (page_name, page_content, `revised`, plugins) VALUES ('bottom" . $index . "', '" . $bottom_content . "', '" . $revised . "', '" . $plugin_id . "')";
		}
		if ($wiz_mode == 1)
			$cnt = $bwb_dbh->exec($query);
		else	{
			$query = str_replace( chr(10), "\\n", $query );
			$query = str_replace( chr(13), "\\r", $query );
			$sql .= $query . ";\n\n";
		}
		
		// Update bottom name in page record.
		$query = "UPDATE " . BWB_TABLE_PREFIX . "page_data SET bottom_name='$bottom_name' WHERE page_name='$page_name'";
		if ($wiz_mode == 1)
			$cnt = $bwb_dbh->exec($query);
		else
			$sql .= $query . ";\n\n";
	}

	// Create file.
	ini_set('track_errors', 1);
	$fh = fopen($file_name, 'w') or die("Can't create $file_name: $php_errormsg");
	$result = fwrite ($fh, $page_template) or die("Can't write to $file_name: $php_errormsg");
	fclose($fh) or die($php_errormsg);
	@chmod($file_name, 0644);
	ini_set('track_errors', 0);
	if ($result)
		$res = "page added";

	if ($wiz_mode == 1)
		return($res);
	else
		return($sql);
}
?>