<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright EB Dynamics, Inc., Edward Lemmers, P.E.
// For licensing, see license.txt.
// 
// $dbh = connection to doner database.
// $dbh2 = connection to repository database.
// $database = doner database.
// $database2 = repository database.
//
// 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: 11/25/2011	ELL
// Added code to redirect to cart if (1) non-subscription user (download), and (2) template is not free.
// Added creating of menu ITEM css files as this was left out before because it was assumed that:
// (1) Templates would not have individual item styling
// (2) Item styling needed to be created only after each item was added to the menu.  But as it turns out,
// item styles work just as well creating before hand waiting for items to be added.
// Revised: 1/13/2012	ELL
// Added truncating of new page_data_draft table to make sure orphans don't cause problems later on.
// Revised: 2/10/2012	ELL
// Added table prefix handling.  Added reference to images folder configuration.
// Revised: 10/13/2012	ELL
// Added deployment of plugins to match new design of version 3.5 of the BWB.
// Revised: 5/18/2013	ELL
// Added checking for existence of menu3_css and menu4_css.
// Revised: 12/9/2014	ELL
// Added mobile site CSS, custom site CSS and custom mobile site CSS handling.
// Revised:	1/1/2015	ELL
// Fixed bug with not being able to re-assign bwb_database and bwb_dbh by unsetting first before including db_connect.php file
// from source account.
// Revised: 6/25/2015	ELL
// Added handling of mobile menus, slide show styles and header and footer areas to bring up to date with all of the revisions
// done to the Breeze Website Builder.
// Revised: 6/26/2015	ELL
// Added checking of header for customizing logo, header_title and tagline.
// Revised: 6/27/2015	ELL
// Revised lookup of images posted to use general purpose key names.
// Revised lookup of colors posted to use general purpose key names.
// Revised: 11/23/2015	ELL
// Modified to handle new Breeze setup of not having calendar, mail form or slideshow style tables already existing is they
// had not yet been deployed on the website.
// Revised: 6/4/2016	ELL
// Fixed bug with plugin not getting installed.
// Revised: 7/13/2016	ELL
// Replaced archive class and archive.php include with PharData class.
// Revised: 8/12/2016	ELL
// Added hooks for the Build Your Own pages.
// Revised: 12/3/2016	ELL
// Re-configured for consolidated classes and CSS tables.
// Revised: 1/12/2017	ELL
// Added creation of child plugin pages from parent plugin.
// Added plugin price lookup to total for checkout. plugin_price is field in page_name_suggestions.
// Added packaging of plugin zip file along with template when applicable.
// Revised: 7/17/2017	ELL
// Added deploying of site_css_important.
// Revised: 2/6/2018	ELL
// Added deployment of custom fonts.
	
	include ("check_session.php");
	include ("db_connection.php");
//echo "<pre>\n";
//print_r($_POST);
//print_r($menu_row_cache);
//echo "\n</pre>\n";
//return;
	
	$logo_width = 200;
	$logo_height = 150;
	$run_local_lockout = true;
	
	$template_id = $_POST['template'];
	$install_oc = 0;
	if (isset($_SESSION['install_oc']))
		$install_oc = $_SESSION['install_oc'];
	
	$query = "SELECT * FROM `templates` WHERE ID = " . $template_id;
	$result = $bwb_wiz_dbh->query($query);
	if ($result->rowCount() < 1)
		exit;
	$row = $result->fetch(PDO::FETCH_ASSOC);
	$price = $row['price'];
	$upload_only = array();
	foreach ($_POST as $key => $val)
	{
		if (strpos($key, "image_tag_", 0) === 0)	{
			$index = substr($key, strlen("image_tag_"));
			$img_key = "file_name_" . $index;
			switch ($val)	{
				case "logo":
					$logo = $_POST[$img_key];
					break;
				case "body_bg_image":
					$body_bg_image = $_POST[$img_key];
					break;
				case "wrap_bg_image":
					$wrap_bg_image = $_POST[$img_key];
					break;
				case "content_td_bg_image":
					$content_td_bg_image = $_POST[$img_key];
					break;
				case "top_td_bg_image":
					$top_td_bg_image = $_POST[$img_key];
					break;
				case "left_td_bg_image":
					$left_td_bg_image = $_POST[$img_key];
					break;
				case "bottom_td_bg_image":
					$bottom_td_bg_image = $_POST[$img_key];
					break;
				case "right_td_bg_image":
					$right_td_bg_image = $_POST[$img_key];
					break;
				case "upload_only":
					$upload_only[] = $_POST[$img_key];
					break;
			}
			continue;
		}
		
		if (strpos($key, "color_tag_", 0) === 0)	{
			$index = substr($key, strlen("color_tag_"));
			$color_key = "color_" . $index;
			switch ($val)	{
				case "body_color":
					$body_color = $_POST[$color_key];
					break;
				case "wrap_color":
					$wrap_color = $_POST[$color_key];
					break;
				case "content_td_color":
					$content_td_color = $_POST[$color_key];
					break;
				case "top_td_color":
					$top_td_color = $_POST[$color_key];
					break;
				case "left_td_color":
					$left_td_color = $_POST[$color_key];
					break;
				case "bottom_td_color":
					$bottom_td_color = $_POST[$color_key];
					break;
				case "right_td_color":
					$right_td_color = $_POST[$color_key];
					break;
			}
			continue;
		}		
	}
	
	$header_title = addslashes($_POST['header_title']);
	$tagline = addslashes($_POST['tagline']);
	
	$doc_root = $_SERVER['DOCUMENT_ROOT'];
	$doc_root_arr = explode("/", $doc_root);
	$cpanel_user_id = $doc_root_arr[2];
	
	if (strpos($doc_root, "ebconten") !== false)
		$repository_acct = "ebconten";
	else
		$repository_acct = "itsabree";
	
	include "lib/copy_dir.php";
	include "lib/image_processing.php";
	include "create_page_wiz.php";
	include "lib/add_menu_item_wiz.php";
	include "lib/build_css_string.php";
	include "write_css_file.php";

	if ($run_local_lockout)
		$wiz_mode = 0;
	else	{
		// Get wizard mode from site options
		$query = "SELECT option_value FROM " . BWB_TABLE_PREFIX . "options_selected WHERE option_name='RunWizardLocal'";
		$result = $bwb_dbh->query($query);
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$wiz_mode = $row['option_value'];	// 1 = local, 0 = download
	}
	
	//////////////////////////////////////
	ini_set('max_execution_time', 10000);
	set_time_limit(10000);
	ini_set('memory_limit','512M');
	//////////////////////////////////////////
	
	$menu1_exists = false;
	$menu2_exists = false;
	$menu3_exists = false;
	$menu4_exists = false;
	$menu1_mobile_exists = false;
	$menu2_mobile_exists = false;
	$menu3_mobile_exists = false;
	$menu4_mobile_exists = false;
	$scrolling_slide_exists = false;

	// Check to see if splash page exists in repository.
	$got_splash = false;
	$query2 = "SELECT `page_name`, `plugins` 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);
		$splash_plugins = $row2['plugins'];
	}

	$sql = "";
	$table = "head";
	$sql .= deploy_table($table, BWB_TABLE_PREFIX, $bwb_dbh, $bwb_wiz_dbh, $bwb_database, $bwb_wiz_database, $template_id, $wiz_mode);
	
	$query = "TRUNCATE TABLE " . BWB_TABLE_PREFIX . "menu_items";
	if ($wiz_mode == 1) $cnt = $bwb_dbh->exec($query); else $sql .= $query . ";\n\n";
	$style_sheet = "menu1";
	$query = "SELECT * FROM site_css WHERE template_id = " . $template_id . " AND style_sheet = '" . $style_sheet . "'";
	$num_rows = $result->rowCount();
	if ($num_rows > 0)
		$menu1_exists = true;

	$style_sheet = "menu2";
	$query = "SELECT * FROM site_css WHERE template_id = " . $template_id . " AND style_sheet = '" . $style_sheet . "'";
	$result = $bwb_wiz_dbh->query($query);
	$num_rows = $result->rowCount();
	if ($num_rows > 0)
		$menu2_exists = true;

	$style_sheet = "menu3";
	$query = "SELECT * FROM site_css WHERE template_id = " . $template_id . " AND style_sheet = '" . $style_sheet . "'";
	$result = $bwb_wiz_dbh->query($query);
	$num_rows = $result->rowCount();
	if ($num_rows > 0)
		$menu3_exists = true;

	$style_sheet = "menu4";
	$query = "SELECT * FROM site_css WHERE template_id = " . $template_id . " AND style_sheet = '" . $style_sheet . "'";
	$result = $bwb_wiz_dbh->query($query);
	$num_rows = $result->rowCount();
	if ($num_rows > 0)
		$menu4_exists = true;

	$style_sheet = "menu1_mobile";
	$query = "SELECT * FROM site_css WHERE template_id = " . $template_id . " AND style_sheet = '" . $style_sheet . "'";
	$result = $bwb_wiz_dbh->query($query);
	$num_rows = $result->rowCount();
	if ($num_rows > 0)
		$menu1_mobile_exists = true;

	$style_sheet = "menu2_mobile";
	$query = "SELECT * FROM site_css WHERE template_id = " . $template_id . " AND style_sheet = '" . $style_sheet . "'";
	$result = $bwb_wiz_dbh->query($query);
	$num_rows = $result->rowCount();
	if ($num_rows > 0)
		$menu2_mobile_exists = true;

	$style_sheet = "menu3_mobile";
	$query = "SELECT * FROM site_css WHERE template_id = " . $template_id . " AND style_sheet = '" . $style_sheet . "'";
	$result = $bwb_wiz_dbh->query($query);
	$num_rows = $result->rowCount();
	if ($num_rows > 0)
		$menu3_mobile_exists = true;

	$style_sheet = "menu4_mobile";
	$query = "SELECT * FROM site_css WHERE template_id = " . $template_id . " AND style_sheet = '" . $style_sheet . "'";
	$result = $bwb_wiz_dbh->query($query);
	$num_rows = $result->rowCount();
	if ($num_rows > 0)
		$menu4_mobile_exists = true;
	
	$table = "menus";
	$sql .= deploy_table($table, BWB_TABLE_PREFIX, $bwb_dbh, $bwb_wiz_dbh, $bwb_database, $bwb_wiz_database, $template_id, $wiz_mode);
	$table = "menu_assignments";
	$sql .= deploy_table($table, BWB_TABLE_PREFIX, $bwb_dbh, $bwb_wiz_dbh, $bwb_database, $bwb_wiz_database, $template_id, $wiz_mode);

	$table = "page_data_special";
	$sql .= deploy_table($table, BWB_TABLE_PREFIX, $bwb_dbh, $bwb_wiz_dbh, $bwb_database, $bwb_wiz_database, $template_id, $wiz_mode);
	$table = "site_classes";
	$sql .= deploy_table($table, BWB_TABLE_PREFIX, $bwb_dbh, $bwb_wiz_dbh, $bwb_database, $bwb_wiz_database, $template_id, $wiz_mode, true);
	$table = "site_css";
	$sql .= deploy_table($table, BWB_TABLE_PREFIX, $bwb_dbh, $bwb_wiz_dbh, $bwb_database, $bwb_wiz_database, $template_id, $wiz_mode);
	$table = "site_css_important";
	$sql .= deploy_table($table, BWB_TABLE_PREFIX, $bwb_dbh, $bwb_wiz_dbh, $bwb_database, $bwb_wiz_database, $template_id, $wiz_mode);

	$table = "style_sheets";
	$sql .= deploy_table($table, BWB_TABLE_PREFIX, $bwb_dbh, $bwb_wiz_dbh, $bwb_database, $bwb_wiz_database, $template_id, $wiz_mode);
	$table = "fonts";
	$sql .= deploy_table($table, BWB_TABLE_PREFIX, $bwb_dbh, $bwb_wiz_dbh, $bwb_database, $bwb_wiz_database, $template_id, $wiz_mode, true);
	
	$query = "TRUNCATE TABLE " . BWB_TABLE_PREFIX . "page_data_draft";
	if ($wiz_mode == 1) $cnt = $bwb_dbh->exec($query); else $sql .= $query . ";\n\n";

	if (BWB_BASE_DIR == '')
		$userfiles = '';
	else
		$userfiles = "/" . BWB_BASE_DIR;

	$cur_ts = date('YmdHis', time());	// Used in download mode to create unique tmp folder names.
	
	if ($wiz_mode == 1)
		$destination = $doc_root . $userfiles . "/" . BWB_IMAGE_DIR . "/";
	else
	{
		$destination = "/home/" . $cpanel_user_id . "/public_html/tmp" . $cur_ts . "/userfiles/image/";
		ini_set('track_errors', 1);
		if (!file_exists($destination))
			mkdir($destination, 0755, true) or die("Can't create $destination: $php_errormsg");
		if (!file_exists("/home/" . $cpanel_user_id . "/public_html/tmp" . $cur_ts . "/userfiles/image/orig/"))
			mkdir("/home/" . $cpanel_user_id . "/public_html/tmp" . $cur_ts . "/userfiles/image/orig/", 0755);
		if (!file_exists("/home/" . $cpanel_user_id . "/public_html/tmp" . $cur_ts . "/userfiles/file/"))
			mkdir("/home/" . $cpanel_user_id . "/public_html/tmp" . $cur_ts . "/userfiles/file/", 0755);
		if (!file_exists("/home/" . $cpanel_user_id . "/public_html/tmp" . $cur_ts . "/userfiles/css/"))
			mkdir("/home/" . $cpanel_user_id . "/public_html/tmp" . $cur_ts . "/userfiles/css/", 0755);
		if (!file_exists("/home/" . $cpanel_user_id . "/public_html/tmp" . $cur_ts . "/userfiles/media/"))
			mkdir("/home/" . $cpanel_user_id . "/public_html/tmp" . $cur_ts . "/userfiles/media/", 0755);
		if (!file_exists("/home/" . $cpanel_user_id . "/public_html/tmp" . $cur_ts . "/userfiles/flash/"))
			mkdir("/home/" . $cpanel_user_id . "/public_html/tmp" . $cur_ts . "/userfiles/flash/", 0755);
		if (!file_exists("/home/" . $cpanel_user_id . "/public_html/tmp" . $cur_ts . "/userfiles/scripts/"))
			mkdir("/home/" . $cpanel_user_id . "/public_html/tmp" . $cur_ts . "/userfiles/scripts/", 0755);
		if (!file_exists("/home/" . $cpanel_user_id . "/public_html/tmp" . $cur_ts . "/ckeditor/"))
			mkdir("/home/" . $cpanel_user_id . "/public_html/tmp" . $cur_ts . "/ckeditor/", 0755);
		ini_set('track_errors', 0);
	}
	$source = "/home/" . $repository_acct . "/public_html/template_admin/template_images/" . $template_id . "/";
	copydir($source, $destination, 0755, true);

	if (isset($logo) && ($logo != ""))
	{
		$file_parts = explode(".", $logo);
		$j = count($file_parts) - 1;
		$ext = strtolower($file_parts[$j]);
	
	//////////////// copy logo ////////////////////////////////////
		$source2 = $doc_root . $userfiles . "/" . BWB_IMAGE_DIR . "/" . $logo;
//		echo "source2: $source2<br>";
		if ( ($ext == "jpg") || ($ext == "jpeg") || ($ext == "gif") || ($ext == "png") )
		{
			if ($wiz_mode == 1)
			{
				$destination2 = $doc_root . $userfiles . "/" . BWB_IMAGE_DIR . "/orig/" . $logo;
				$dest_dir2 = $doc_root . $userfiles . "/" . BWB_IMAGE_DIR . "/orig";
			}
			else
			{
				$destination2 = "/home/" . $cpanel_user_id . "/public_html/tmp" . $cur_ts . "/userfiles/image/orig/" . $logo;
				$dest_dir2 = "/home/" . $cpanel_user_id . "/public_html/tmp" . $cur_ts . "/userfiles/image/orig";
			}
			if (!file_exists($dest_dir2))
				mkdir($dest_dir2, 0755);

			copy($source2, $destination2);
			
			$destination .= "/" . $logo;
			resize_image($source2, $logo_width, $logo_height, $destination, 1);
		}
		else
		{
			$destination .= "/" . $logo;
			copy($source2, $destination);
		}
	}

	if ($wiz_mode == 1)
		$destination = $doc_root . $userfiles . "/" . BWB_FONTS_DIR . "/";
	else
	{
		$destination = "/home/" . $cpanel_user_id . "/public_html/tmp" . $cur_ts . "/userfiles/fonts/";
		ini_set('track_errors', 1);
		if (!file_exists($destination))
			mkdir($destination, 0755, true) or die("Can't create $destination: $php_errormsg");
		ini_set('track_errors', 0);
	}
	$source = "/home/" . $repository_acct . "/public_html/template_admin/template_fonts/" . $template_id . "/";
	if (file_exists($source))
		copydir($source, $destination);
		
	//////////////////////////////////////////////////////////////
	/////////////// create pages /////////////////////////////////
	$query = "SELECT * FROM options_selected WHERE template_id = " . $template_id . " AND option_name = 'Template'";
	$result = $bwb_wiz_dbh->query($query);
	$row = $result->fetch(PDO::FETCH_ASSOC);
	$template_name = $row['option_value'];
	$query = "SELECT * FROM options_selected WHERE template_id = " . $template_id . " AND option_name = 'UseTableStructure'";
	$result = $bwb_wiz_dbh->query($query);
	$row = $result->fetch(PDO::FETCH_ASSOC);
	$UseTableStructure = $row['option_value'];
	$query = "SELECT * FROM options_selected WHERE template_id = " . $template_id . " AND option_name = 'UseTableStructureMobile'";
	$result = $bwb_wiz_dbh->query($query);
	$row = $result->fetch(PDO::FETCH_ASSOC);
	$UseTableStructureMobile = $row['option_value'];
	
	$sql .= "REPLACE INTO " . BWB_TABLE_PREFIX . "options_selected SET option_name='Template', option_value='" . $template_name . "';\n";
	$sql .= "REPLACE INTO " . BWB_TABLE_PREFIX . "options_selected SET option_name='UseTableStructure', option_value='" . $UseTableStructure . "';\n";
	$sql .= "REPLACE INTO " . BWB_TABLE_PREFIX . "options_selected SET option_name='UseTableStructureMobile', option_value='" . $UseTableStructureMobile . "';\n\n";
	$page_arr = array();
	foreach ($_POST as $key => $val)
	{
		if (strpos($key, "menu_page_name") === false)
		{
			$pos = strpos($key, "_page", 3);
			if (strpos($key, "_page", 3) > 0)
			{
				if ((substr($key, 0, 5)) == "other")
					$page_name = $val;
				else
				{
					$page_name = $key;
					$page_name = str_replace("_page", "", $page_name);
				}
				$sort_name = $page_name . "_sort";
				$sort_order = $_POST[$sort_name];
				
				$link_text_name = $page_name . "_link_text";
				$link_text = $_POST[$link_text_name];
				
				$plugin_id_name = $page_name . "_plugin_id";
				if ((isset($_POST[$plugin_id_name])) && ($_POST[$plugin_id_name] != ""))
					$plugin_id = intval($_POST[$plugin_id_name]);
				else
					$plugin_id = -1;
				
				if (isset($sort_order) && ($sort_order != "") && isset($link_text) && ($link_text != ""))
					$page_arr[$sort_order] = array($page_name, $link_text, $plugin_id);
			}
		}
	}

	ksort($page_arr);
	$menu_cnt = 1;
//	echo "\n<pre>\n";
//	print_r($page_arr);
//	echo "\n</pre>\n";
//	exit;
	$deployed_plugins = array();
	foreach ($page_arr as $key => $val)
	{
		$page_name = $val[0];
		$link_text = $val[1];
		$plugin = $val[2];
		if ($page_name != "")
		{
			if ($plugin > -1)	{
				$query = "SELECT plugin_price FROM page_name_suggestions WHERE plugin_id = '$plugin'";
				$result = $bwb_wiz_dbh->query($query);
				$row = $result->fetch(PDO::FETCH_ASSOC);
				$price = $price + $row['plugin_price'];	// Add any cost for plugin to checkout price.
				
				$query = "SELECT name, parent FROM " . BWB_TABLE_PREFIX . "plugins WHERE id = '$plugin'";
				$result = $bwb_dbh->query($query);
				$row = $result->fetch(PDO::FETCH_ASSOC);
//				if (isset($row['parent']) && ($row['parent'] != "") && ($row['parent'] == $row['name']))	{
				if (isset($row['parent']) && ($row['parent'] != "") && ($row['parent'] == $plugin))	{
					// Got parent plugin page, need to create child plugin pages as well.
					$parent = $row['parent'];
					$ret_result = CreatePage(strtolower($page_name), $plugin, $template_name, $bwb_dbh, $bwb_wiz_dbh, $template_id, $wiz_mode, $cur_ts);
					if ($wiz_mode == 0)
						$sql .= $ret_result;
//					$query = "SELECT id, name, parent, default_page_name, page_name_suffix FROM " . BWB_TABLE_PREFIX . "plugins WHERE parent = '$parent' AND name <> parent";
					$query = "SELECT id, name, parent, default_page_name, page_name_suffix FROM " . BWB_TABLE_PREFIX . "plugins WHERE parent = '$parent' AND id <> parent";
					$result2 = $bwb_dbh->query($query);
					while ($row2 = $result2->fetch(PDO::FETCH_ASSOC))
					{
						$child_page_name = $page_name . $row2['page_name_suffix'];
						$child_plugin = $row2['id'];
						$ret_result = CreatePage(strtolower($child_page_name), $child_plugin, $template_name, $bwb_dbh, $bwb_wiz_dbh, $template_id, $wiz_mode, $cur_ts);
						if ($wiz_mode == 0)
							$sql .= $ret_result;
					}
				}
				else	{
					$ret_result = CreatePage(strtolower($page_name), $plugin, $template_name, $bwb_dbh, $bwb_wiz_dbh, $template_id, $wiz_mode, $cur_ts);
					if ($wiz_mode == 0)
						$sql .= $ret_result;
				}
			}
			else	{
				$ret_result = CreatePage(strtolower($page_name), $plugin, $template_name, $bwb_dbh, $bwb_wiz_dbh, $template_id, $wiz_mode, $cur_ts);
				if ($wiz_mode == 0)
					$sql .= $ret_result;
			}
			
			if ($menu1_exists) {
				$sql .= AddMenuItem("menu1", $page_name, $link_text, $bwb_dbh, $wiz_mode, $menu_cnt, BWB_TABLE_PREFIX);
			}
			if ($menu2_exists) {
				$sql .= AddMenuItem("menu2", $page_name, $link_text, $bwb_dbh, $wiz_mode, $menu_cnt, BWB_TABLE_PREFIX);
			}
			if ($menu3_exists) {
				$sql .= AddMenuItem("menu3", $page_name, $link_text, $bwb_dbh, $wiz_mode, $menu_cnt, BWB_TABLE_PREFIX);
			}
			if ($menu4_exists) {
				$sql .= AddMenuItem("menu4", $page_name, $link_text, $bwb_dbh, $wiz_mode, $menu_cnt, BWB_TABLE_PREFIX);
			}
			$menu_cnt++;
		}
		
		// Create table structures and deploy plugin if need be.
		if ($plugin > -1)
		{
			$ret_result = DeployPlugin($plugin, $bwb_dbh, $bwb_wiz_dbh, BWB_TABLE_PREFIX, $wiz_mode, $template_id);
			if ($wiz_mode == 0)
				$sql .= $ret_result;
			$deployed_plugins[] = $plugin;
		}
	}
	$sql .= "\n";
//	echo $sql;
//exit;
	// Look for plugins in splash page and deploy if not already deployed.
	if (($got_splash === true) && isset($splash_plugins) && ($splash_plugins != ""))	{
		$plugin_arr = explode(",", $splash_plugins);
		foreach($plugin_arr as $splash_plugin)
		{
			if (($splash_plugin != "") && (!in_array($splash_plugin, $deployed_plugins)))	{
				$ret_result = DeployPlugin($splash_plugin, $bwb_dbh, $bwb_wiz_dbh, BWB_TABLE_PREFIX, $wiz_mode, $template_id);
				if ($wiz_mode == 0)
					$sql .= $ret_result;
			}
		}
	}

	////////////////////////////////////////////////////////////////////////////
	///////// Change site colors if diffirent colors selected //////////////////
	
	if ((isset($body_color)) && ($body_color != ""))
	{
		$query = "UPDATE " . BWB_TABLE_PREFIX . "site_css SET bkgd_color = '" . $body_color . "' WHERE class = 'body'";
		if ($wiz_mode == 1)
			$cnt = $bwb_dbh->exec($query);
		else
			$sql .= $query . ";\n\n";
	}
	if ((isset($wrap_color)) && ($wrap_color != ""))
	{
		$query = "UPDATE " . BWB_TABLE_PREFIX . "site_css SET bkgd_color = '" . $wrap_color . "' WHERE class = '#wrap'";
		if ($wiz_mode == 1)
			$cnt = $bwb_dbh->exec($query);
		else
			$sql .= $query . ";\n\n";
	}
	if ((isset($content_td_color)) && ($content_td_color != ""))
	{
		$query = "UPDATE " . BWB_TABLE_PREFIX . "site_css SET bkgd_color = '" . $content_td_color . "' WHERE class = '#content_td'";
		if ($wiz_mode == 1)
			$cnt = $bwb_dbh->exec($query);
		else
			$sql .= $query . ";\n\n";
	}
	if ((isset($top_td_color)) && ($top_td_color != ""))
	{
		$query = "UPDATE " . BWB_TABLE_PREFIX . "site_css SET bkgd_color = '" . $top_td_color . "' WHERE class = '#top_td'";
		if ($wiz_mode == 1)
			$cnt = $bwb_dbh->exec($query);
		else
			$sql .= $query . ";\n\n";
	}
	if ((isset($left_td_color)) && ($left_td_color != ""))
	{
		$query = "UPDATE " . BWB_TABLE_PREFIX . "site_css SET bkgd_color = '" . $left_td_color . "' WHERE class = '#left_td'";
		if ($wiz_mode == 1)
			$cnt = $bwb_dbh->exec($query);
		else
			$sql .= $query . ";\n\n";
	}
	if ((isset($bottom_td_color)) && ($bottom_td_color != ""))
	{
		$query = "UPDATE " . BWB_TABLE_PREFIX . "site_css SET bkgd_color = '" . $bottom_td_color . "' WHERE class = '#bottom_td'";
		if ($wiz_mode == 1)
			$cnt = $bwb_dbh->exec($query);
		else
			$sql .= $query . ";\n\n";
	}
	if ((isset($right_td_color)) && ($right_td_color != ""))
	{
		$query = "UPDATE " . BWB_TABLE_PREFIX . "site_css SET bkgd_color = '" . $right_td_color . "' WHERE class = '#right_td'";
		if ($wiz_mode == 1)
			$cnt = $bwb_dbh->exec($query);
		else
			$sql .= $query . ";\n\n";
	}
	//------------------------------------------------------------------//
	if ((isset($body_bg_image)) && ($body_bg_image != ""))
	{
		$query = "UPDATE " . BWB_TABLE_PREFIX . "site_css SET background_image = '" . $body_bg_image . "', background_repeat = 'repeat' WHERE class = 'body'";
		if ($wiz_mode == 1)
			$cnt = $bwb_dbh->exec($query);
		else	{
			$sql .= $query . ";\n\n";
			CopyBackgroundImage($body_bg_image, $doc_root, $userfiles, BWB_IMAGE_DIR, $cur_ts, $cpanel_user_id);
		}
	}
	if ((isset($wrap_bg_image)) && ($wrap_bg_image != ""))
	{
		$query = "UPDATE " . BWB_TABLE_PREFIX . "site_css SET background_image = '" . $wrap_bg_image . "', background_repeat = 'repeat' WHERE class = '#wrap'";
		if ($wiz_mode == 1)
			$cnt = $bwb_dbh->exec($query);
		else	{
			$sql .= $query . ";\n\n";
			CopyBackgroundImage($wrap_bg_image, $doc_root, $userfiles, BWB_IMAGE_DIR, $cur_ts, $cpanel_user_id);
		}
	}
	if ((isset($content_td_bg_image)) && ($content_td_bg_image != ""))
	{
		$query = "UPDATE " . BWB_TABLE_PREFIX . "site_css SET background_image = '" . $content_td_bg_image . "', background_repeat = 'no-repeat' WHERE class = '#content_td'";
		if ($wiz_mode == 1)
			$cnt = $bwb_dbh->exec($query);
		else	{
			$sql .= $query . ";\n\n";
			CopyBackgroundImage($content_td_bg_image, $doc_root, $userfiles, BWB_IMAGE_DIR, $cur_ts, $cpanel_user_id);
		}
	}
	if ((isset($top_td_bg_image)) && ($top_td_bg_image != ""))
	{
		$query = "UPDATE " . BWB_TABLE_PREFIX . "site_css SET background_image = '" . $top_td_bg_image . "', background_repeat = 'repeat-x' WHERE class = '#top_td'";
		if ($wiz_mode == 1)
			$cnt = $bwb_dbh->exec($query);
		else	{
			$sql .= $query . ";\n\n";
			CopyBackgroundImage($top_td_bg_image, $doc_root, $userfiles, BWB_IMAGE_DIR, $cur_ts, $cpanel_user_id);
		}
	}
	if ((isset($left_td_bg_image)) && ($left_td_bg_image != ""))
	{
		$query = "UPDATE " . BWB_TABLE_PREFIX . "site_css SET background_image = '" . $left_td_bg_image . "', background_repeat = 'repeat-y' WHERE class = '#left_td'";
		if ($wiz_mode == 1)
			$cnt = $bwb_dbh->exec($query);
		else	{
			$sql .= $query . ";\n\n";
			CopyBackgroundImage($left_td_bg_image, $doc_root, $userfiles, BWB_IMAGE_DIR, $cur_ts, $cpanel_user_id);
		}
	}
	if ((isset($bottom_td_bg_image)) && ($bottom_td_bg_image != ""))
	{
		$query = "UPDATE " . BWB_TABLE_PREFIX . "site_css SET background_image = '" . $bottom_td_bg_image . "', background_repeat = 'repeat-x' WHERE class = '#bottom_td'";
		if ($wiz_mode == 1)
			$cnt = $bwb_dbh->exec($query);
		else	{
			$sql .= $query . ";\n\n";
			CopyBackgroundImage($bottom_td_bg_image, $doc_root, $userfiles, BWB_IMAGE_DIR, $cur_ts, $cpanel_user_id);
		}
	}
	if ((isset($right_td_bg_image)) && ($right_td_bg_image != ""))
	{
		$query = "UPDATE " . BWB_TABLE_PREFIX . "site_css SET background_image = '" . $right_td_bg_image . "', background_repeat = 'repeat-y' WHERE class = '#right_td'";
		if ($wiz_mode == 1)
			$cnt = $bwb_dbh->exec($query);
		else	{
			$sql .= $query . ";\n\n";
			CopyBackgroundImage($right_td_bg_image, $doc_root, $userfiles, BWB_IMAGE_DIR, $cur_ts, $cpanel_user_id);
		}
	}
	if (count($upload_only) > 0)
	{
		if ($wiz_mode != 1)	{
			foreach ($upload_only as $upload_only_item)	{
				CopyBackgroundImage($upload_only_item, $doc_root, $userfiles, BWB_IMAGE_DIR, $cur_ts, $cpanel_user_id);
			}
		}
	}
//	echo $sql;
	//////////////////////////////////////////////////////////////
	///////// Build CSS Files ////////////////////////////////////	
	$query = "SELECT * FROM custom_site_css WHERE template_id = " . $template_id;
	$result = $bwb_wiz_dbh->query($query);
	$custom_css = array();
	if ($result->rowCount() > 0)	{
		while ($row = $result->fetch(PDO::FETCH_ASSOC))	{
			$name = addslashes(stripslashes($row['name']));
			$custom_css[$name] = addslashes(stripslashes($row['custom_css']));
		}
	}
	else	{
		$custom_css['site'] = "";
		$custom_css['site_mobile'] = "";
	}
		
	$query = "TRUNCATE TABLE `" . BWB_TABLE_PREFIX . "custom_site_css`";
	if ($wiz_mode == 1)
		$cnt = $bwb_dbh->exec($query);
	else
		$sql .= $query . ";\n\n";
		
	foreach ($custom_css as $name => $css)
	{
		$query = "INSERT INTO " . BWB_TABLE_PREFIX . "custom_site_css SET name='" . $name . "', custom_css='" . $css . "'";
		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";
		}
	}
	
	if ($wiz_mode == 1)
		$css_string = BuildCSSString("site", $bwb_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, BWB_TABLE_PREFIX);
	else
		$css_string = BuildCSSString("site", $bwb_wiz_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, "", $template_id);
	WriteCSSFile("site.css", $css_string, $wiz_mode, $template_id, $cur_ts, BWB_BASE_DIR, BWB_CSS_DIR);

	if ($wiz_mode == 1)
		$css_string = BuildCSSString("site_mobile", $bwb_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, BWB_TABLE_PREFIX);
	else
		$css_string = BuildCSSString("site_mobile", $bwb_wiz_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, "", $template_id);
	WriteCSSFile("site_mobile.css", $css_string, $wiz_mode, $template_id, $cur_ts, BWB_BASE_DIR, BWB_CSS_DIR);
	
	if ($menu1_exists) {
		if ($wiz_mode == 1)
			$css_string = BuildCSSString("menu1", $bwb_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, BWB_TABLE_PREFIX);
		else
			$css_string = BuildCSSString("menu1", $bwb_wiz_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, "", $template_id);
		WriteCSSFile("menu1.css", $css_string, $wiz_mode, $template_id, $cur_ts, BWB_BASE_DIR, BWB_CSS_DIR);
		
		if ($wiz_mode == 1)
			$css_string = BuildCSSString("menu1_item", $bwb_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, BWB_TABLE_PREFIX);
		else
			$css_string = BuildCSSString("menu1_item", $bwb_wiz_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, "", $template_id);
		WriteCSSFile("menu1_item.css", $css_string, $wiz_mode, $template_id, $cur_ts, BWB_BASE_DIR, BWB_CSS_DIR);
	}
	
	if ($menu2_exists) {
		if ($wiz_mode == 1)
			$css_string = BuildCSSString("menu2", $bwb_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, BWB_TABLE_PREFIX);
		else
			$css_string = BuildCSSString("menu2", $bwb_wiz_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, "", $template_id);
		WriteCSSFile("menu2.css", $css_string, $wiz_mode, $template_id, $cur_ts, BWB_BASE_DIR, BWB_CSS_DIR);
		
		if ($wiz_mode == 1)
			$css_string = BuildCSSString("menu2_item", $bwb_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, BWB_TABLE_PREFIX);
		else
			$css_string = BuildCSSString("menu2_item", $bwb_wiz_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, "", $template_id);
		WriteCSSFile("menu2_item.css", $css_string, $wiz_mode, $template_id, $cur_ts, BWB_BASE_DIR, BWB_CSS_DIR);
	}
	
	if ($menu3_exists) {
		if ($wiz_mode == 1)
			$css_string = BuildCSSString("menu3", $bwb_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, BWB_TABLE_PREFIX);
		else
			$css_string = BuildCSSString("menu3", $bwb_wiz_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, "", $template_id);
		WriteCSSFile("menu3.css", $css_string, $wiz_mode, $template_id, $cur_ts, BWB_BASE_DIR, BWB_CSS_DIR);
		
		if ($wiz_mode == 1)
			$css_string = BuildCSSString("menu3_item", $bwb_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, BWB_TABLE_PREFIX);
		else
			$css_string = BuildCSSString("menu3_item", $bwb_wiz_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, "", $template_id);
		WriteCSSFile("menu3_item.css", $css_string, $wiz_mode, $template_id, $cur_ts, BWB_BASE_DIR, BWB_CSS_DIR);
	}
	
	if ($menu4_exists) {
		if ($wiz_mode == 1)
			$css_string = BuildCSSString("menu4", $bwb_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, BWB_TABLE_PREFIX);
		else
			$css_string = BuildCSSString("menu4", $bwb_wiz_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, "", $template_id);
		WriteCSSFile("menu4.css", $css_string, $wiz_mode, $template_id, $cur_ts, BWB_BASE_DIR, BWB_CSS_DIR);
		
		if ($wiz_mode == 1)
			$css_string = BuildCSSString("menu4_item", $bwb_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, BWB_TABLE_PREFIX);
		else
			$css_string = BuildCSSString("menu4_item", $bwb_wiz_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, "", $template_id);
		WriteCSSFile("menu4_item.css", $css_string, $wiz_mode, $template_id, $cur_ts, BWB_BASE_DIR, BWB_CSS_DIR);
	}
	
	
	if ($menu1_mobile_exists) {
		if ($wiz_mode == 1)
			$css_string = BuildCSSString("menu1_mobile", $bwb_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, BWB_TABLE_PREFIX);
		else
			$css_string = BuildCSSString("menu1_mobile", $bwb_wiz_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, "", $template_id);
		WriteCSSFile("menu1_mobile.css", $css_string, $wiz_mode, $template_id, $cur_ts, BWB_BASE_DIR, BWB_CSS_DIR);
		
		if ($wiz_mode == 1)
			$css_string = BuildCSSString("menu1_mobile_item", $bwb_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, BWB_TABLE_PREFIX);
		else
			$css_string = BuildCSSString("menu1_mobile_item", $bwb_wiz_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, "", $template_id);
		WriteCSSFile("menu1_mobile_item.css", $css_string, $wiz_mode, $template_id, $cur_ts, BWB_BASE_DIR, BWB_CSS_DIR);
	}
	
	if ($menu2_mobile_exists) {
		if ($wiz_mode == 1)
			$css_string = BuildCSSString("menu2_mobile", $bwb_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, BWB_TABLE_PREFIX);
		else
			$css_string = BuildCSSString("menu2_mobile", $bwb_wiz_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, "", $template_id);
		WriteCSSFile("menu2_mobile.css", $css_string, $wiz_mode, $template_id, $cur_ts, BWB_BASE_DIR, BWB_CSS_DIR);
		
		if ($wiz_mode == 1)
			$css_string = BuildCSSString("menu2_mobile_item", $bwb_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, BWB_TABLE_PREFIX);
		else
			$css_string = BuildCSSString("menu2_mobile_item", $bwb_wiz_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, "", $template_id);
		WriteCSSFile("menu2_mobile_item.css", $css_string, $wiz_mode, $template_id, $cur_ts, BWB_BASE_DIR, BWB_CSS_DIR);
	}
	
	if ($menu3_mobile_exists) {
		if ($wiz_mode == 1)
			$css_string = BuildCSSString("menu3_mobile", $bwb_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, BWB_TABLE_PREFIX);
		else
			$css_string = BuildCSSString("menu3_mobile", $bwb_wiz_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, "", $template_id);
		WriteCSSFile("menu3_mobile.css", $css_string, $wiz_mode, $template_id, $cur_ts, BWB_BASE_DIR, BWB_CSS_DIR);
		
		if ($wiz_mode == 1)
			$css_string = BuildCSSString("menu3_item", $bwb_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, BWB_TABLE_PREFIX);
		else
			$css_string = BuildCSSString("menu3_mobile_item", $bwb_wiz_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, "", $template_id);
		WriteCSSFile("menu3_mobile_item.css", $css_string, $wiz_mode, $template_id, $cur_ts, BWB_BASE_DIR, BWB_CSS_DIR);
	}
	
	if ($menu4_mobile_exists) {
		if ($wiz_mode == 1)
			$css_string = BuildCSSString("menu4_mobile", $bwb_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, BWB_TABLE_PREFIX);
		else
			$css_string = BuildCSSString("menu4_mobile", $bwb_wiz_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, "", $template_id);
		WriteCSSFile("menu4_mobile.css", $css_string, $wiz_mode, $template_id, $cur_ts, BWB_BASE_DIR, BWB_CSS_DIR);
		
		if ($wiz_mode == 1)
			$css_string = BuildCSSString("menu4_mobile_item", $bwb_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, BWB_TABLE_PREFIX);
		else
			$css_string = BuildCSSString("menu4_mobile_item", $bwb_wiz_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, "", $template_id);
		WriteCSSFile("menu4_mobile_item.css", $css_string, $wiz_mode, $template_id, $cur_ts, BWB_BASE_DIR, BWB_CSS_DIR);
	}
	
	
	if ($calendar_exists)	{
		if ($wiz_mode == 1)
			$css_string = BuildCSSString("calendar", $bwb_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, BWB_TABLE_PREFIX);
		else
			$css_string = BuildCSSString("calendar", $bwb_wiz_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, "", $template_id);
		WriteCSSFile("calendar.css", $css_string, $wiz_mode, $template_id, $cur_ts, BWB_BASE_DIR, BWB_CSS_DIR);
	}
	
	if ($mail_form_exists)	{
		if ($wiz_mode == 1)
			$css_string = BuildCSSString("mail_form", $bwb_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, BWB_TABLE_PREFIX);
		else
			$css_string = BuildCSSString("mail_form", $bwb_wiz_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, "", $template_id);
		WriteCSSFile("mail_form.css", $css_string, $wiz_mode, $template_id, $cur_ts, BWB_BASE_DIR, BWB_CSS_DIR);
	}
	
	if ($scrolling_slideshow_exists) {
		if ($wiz_mode == 1)
			$css_string = BuildCSSString("scrolling_slideshow", $bwb_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, BWB_TABLE_PREFIX);
		else
			$css_string = BuildCSSString("scrolling_slideshow", $bwb_wiz_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, "", $template_id);
		WriteCSSFile("scrolling_slideshow.css", $css_string, $wiz_mode, $template_id, $cur_ts, BWB_BASE_DIR, BWB_CSS_DIR);
	}
//	echo $css_string;

	include("write_custom_font_css.php");
	
	//////////////////////////////////////////////////////////////////
	if ($wiz_mode == 0)
	{
		$page_content_header = "";
		$page_content_top = "";
		$page_content_left = "";
		$page_content_right = "";
		
		$page_content_header_splash = "";
		$page_content_top_splash = "";
		$page_content_left_splash = "";
		$page_content_right_splash = "";
	}
	//////////////////////////////////////////////////////////////////////////
	///////// Insert logo into border area ///////////////////////////////////
	if (isset($logo) && ($logo != ""))
	{	
		if ($wiz_mode == 1) {
			$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "page_data_special WHERE page_name = 'header'";
			$result = $bwb_dbh->query($query);
		} else {
			$query = "SELECT * FROM page_data_special WHERE (page_name='header') AND (template_id=" . $template_id . ")";
			$result = $bwb_wiz_dbh->query($query);
		}
		$row_cnt = $result->rowCount();
		if ($row_cnt > 0)
		{
			$row = $result->fetch(PDO::FETCH_ASSOC);
			$page_content = $row['page_content'];
			if ((stripos($page_content, 'id="logo"') !== false) || (stripos($page_content, "id='logo'") !== false) || (stripos($page_content, 'id=\\"logo\\"') !== false) || (stripos($page_content, "id=\\'logo\\'") !== false) || (stripos($page_content, "id=''logo''") !== false))	{
				$area = "header";
			}
			else
			{
				if ($wiz_mode == 1) {
					$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "page_data_special WHERE page_name = 'top'";
					$result = $bwb_dbh->query($query);
				} else {
					$query = "SELECT * FROM page_data_special WHERE (page_name='top') AND (template_id=" . $template_id . ")";
					$result = $bwb_wiz_dbh->query($query);
				}
				$row_cnt = $result->rowCount();
				if ($row_cnt > 0)
				{
					$row = $result->fetch(PDO::FETCH_ASSOC);
					$page_content = $row['page_content'];
					if ((stripos($page_content, 'id="logo"') !== false) || (stripos($page_content, "id='logo'") !== false) || (stripos($page_content, 'id=\\"logo\\"') !== false) || (stripos($page_content, "id=\\'logo\\'") !== false) || (stripos($page_content, "id=''logo''") !== false))	{
						$area = "top";
					}
					else
					{
						if ($wiz_mode == 1) {
							$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "page_data_special WHERE page_name = 'left'";
							$result = $bwb_dbh->query($query);
						} else {
							$query = "SELECT * FROM page_data_special WHERE (page_name='left') AND (template_id=" . $template_id . ")";
							$result = $bwb_wiz_dbh->query($query);
						}
						$row_cnt = $result->rowCount();
						if ($row_cnt > 0)
						{
							$row = $result->fetch(PDO::FETCH_ASSOC);
							$page_content = $row['page_content'];
							if ((stripos($page_content, 'id="logo"') !== false) || (stripos($page_content, "id='logo'") !== false) || (stripos($page_content, 'id=\\"logo\\"') !== false) || (stripos($page_content, "id=\\'logo\\'") !== false) || (stripos($page_content, "id=''logo''") !== false))	{
								$area = "left";
							}
							else
							{
								if ($wiz_mode == 1) {
									$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "page_data_special WHERE page_name = 'right'";
									$result = $bwb_dbh->query($query);
								} else {
									$query = "SELECT * FROM page_data_special WHERE (page_name='right') AND (template_id=" . $template_id . ")";
									$result = $bwb_wiz_dbh->query($query);
								}
								$row_cnt = $result->rowCount();
								if ($row_cnt > 0)
								{
									$row = $result->fetch(PDO::FETCH_ASSOC);
									$page_content = $row['page_content'];
									if ((stripos($page_content, 'id="logo"') !== false) || (stripos($page_content, "id='logo'") !== false) || (stripos($page_content, 'id=\\"logo\\"') !== false) || (stripos($page_content, "id=\\'logo\\'") !== false) || (stripos($page_content, "id=''logo''") !== false))	{
										$area = "right";
									}
									else
									{
										$area = "-not found-";
									}
								}
							}
						}
					}
				}
			}
		}

		if ($area != "-not found-")
		{
			if ($wiz_mode == 1)
			{
				$page_content = ContentWithNewLogo($page_content, $logo, BWB_WEBPAGE_DIR, BWB_IMAGE_DIR);
				$query = "UPDATE " . BWB_TABLE_PREFIX . "page_data_special SET page_content = '" . $page_content . "' WHERE page_name = '" . $area . "'";
				$cnt = $bwb_dbh->exec($query);
			}
			else
			{
				switch ($area)	{
					case "header":
						if ($page_content_header == "")
							$page_content_header = ContentWithNewLogo($page_content, $logo, BWB_WEBPAGE_DIR, BWB_IMAGE_DIR);
						else
							$page_content_header = ContentWithNewLogo($page_content_header, $logo, BWB_WEBPAGE_DIR, BWB_IMAGE_DIR);
						break;
					case "top":
						if ($page_content_top == "")
							$page_content_top = ContentWithNewLogo($page_content, $logo, BWB_WEBPAGE_DIR, BWB_IMAGE_DIR);
						else
							$page_content_top = ContentWithNewLogo($page_content_top, $logo, BWB_WEBPAGE_DIR, BWB_IMAGE_DIR);
						break;
					case "left":
						if ($page_content_left == "")
							$page_content_left = ContentWithNewLogo($page_content, $logo, BWB_WEBPAGE_DIR, BWB_IMAGE_DIR);
						else
							$page_content_left = ContentWithNewLogo($page_content_left, $logo, BWB_WEBPAGE_DIR, BWB_IMAGE_DIR);
						break;
					case "right":
						if ($page_content_right == "")
							$page_content_right = ContentWithNewLogo($page_content, $logo, BWB_WEBPAGE_DIR, BWB_IMAGE_DIR);
						else
							$page_content_right = ContentWithNewLogo($page_content_right, $logo, BWB_WEBPAGE_DIR, BWB_IMAGE_DIR);
						break;
					default:
						break;
				}
			}
			
			if ($got_splash === true)
			{
				if ($wiz_mode == 1) {
					$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "page_data_special WHERE page_name = '" . $area . "_splash'";
					$result = $bwb_dbh->query($query);
				} else {
					$query = "SELECT * FROM page_data_special WHERE (page_name='" . $area . "_splash') AND (template_id=" . $template_id . ")";
					$result = $bwb_wiz_dbh->query($query);
				}
				if ($result->rowCount() > 0)	{
					$row = $result->fetch(PDO::FETCH_ASSOC);
					$page_content = $row['page_content'];
					
					if ($wiz_mode == 1)
					{
						$page_content = ContentWithNewLogo($page_content, $logo, BWB_WEBPAGE_DIR, BWB_IMAGE_DIR);
						$query = "UPDATE " . BWB_TABLE_PREFIX . "page_data_special SET page_content = '" . $page_content . "' WHERE page_name = '" . $area . "_splash'";
						$cnt = $bwb_dbh->exec($query);
					}
					else
					{
						switch ($area)	{
							case "header":
								if ($page_content_header_splash == "")
									$page_content_header_splash = ContentWithNewLogo($page_content, $logo, BWB_WEBPAGE_DIR, BWB_IMAGE_DIR);
								else
									$page_content_header_splash = ContentWithNewLogo($page_content_header_splash, $logo, BWB_WEBPAGE_DIR, BWB_IMAGE_DIR);
								break;
							case "top":
								if ($page_content_top_splash == "")
									$page_content_top_splash = ContentWithNewLogo($page_content, $logo, BWB_WEBPAGE_DIR, BWB_IMAGE_DIR);
								else
									$page_content_top_splash = ContentWithNewLogo($page_content_top_splash, $logo, BWB_WEBPAGE_DIR, BWB_IMAGE_DIR);
								break;
							case "left":
								if ($page_content_left_splash == "")
									$page_content_left_splash = ContentWithNewLogo($page_content, $logo, BWB_WEBPAGE_DIR, BWB_IMAGE_DIR);
								else
									$page_content_left_splash = ContentWithNewLogo($page_content_left_splash, $logo, BWB_WEBPAGE_DIR, BWB_IMAGE_DIR);
								break;
							case "right":
								if ($page_content_right_splash == "")
									$page_content_right_splash = ContentWithNewLogo($page_content, $logo, BWB_WEBPAGE_DIR, BWB_IMAGE_DIR);
								else
									$page_content_right_splash = ContentWithNewLogo($page_content_right_splash, $logo, BWB_WEBPAGE_DIR, BWB_IMAGE_DIR);
								break;
							default:
								break;
						}
					}
				}
			}
		}
	}
	
	///////// Insert site title into border area ////////////////////////////////////
	if (isset($header_title) && ($header_title != ""))
	{
		$id_string = 'id="header_title"';
		$id_string2 = "id='header_title'";
		$id_string3 = 'id=\\"header_title\\"';
		$id_string4 = "id=\\'header_title\\'";
		
		if ($wiz_mode == 1) {
			$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "page_data_special WHERE page_name = 'header'";
			$result = $bwb_dbh->query($query);
		} else {
			$query = "SELECT * FROM page_data_special WHERE (page_name='header') AND (template_id=" . $template_id . ")";
			$result = $bwb_wiz_dbh->query($query);
		}
		$row_cnt = $result->rowCount();
		if ($row_cnt > 0)
		{
			$row = $result->fetch(PDO::FETCH_ASSOC);
			$page_content = $row['page_content'];
			if ((stripos($page_content, $id_string) !== false) || (stripos($page_content, $id_string2) !== false) || (stripos($page_content, $id_string3) !== false) || (stripos($page_content, $id_string4) !== false))	{
				$area = "header";
			}
			else
			{
				if ($wiz_mode == 1) {
					$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "page_data_special WHERE page_name = 'top'";
					$result = $bwb_dbh->query($query);
				} else {
					$query = "SELECT * FROM page_data_special WHERE (page_name='top') AND (template_id=" . $template_id . ")";
					$result = $bwb_wiz_dbh->query($query);
				}
				$row_cnt = $result->rowCount();
				if ($row_cnt > 0)
				{
					$row = $result->fetch(PDO::FETCH_ASSOC);
					$page_content = $row['page_content'];
					if ((stripos($page_content, $id_string) !== false) || (stripos($page_content, $id_string2) !== false) || (stripos($page_content, $id_string3) !== false) || (stripos($page_content, $id_string4) !== false))	{
						$area = "top";
					}
					else
					{
						if ($wiz_mode == 1) {
							$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "page_data_special WHERE page_name = 'left'";
							$result = $bwb_dbh->query($query);
						} else {
							$query = "SELECT * FROM page_data_special WHERE (page_name='left') AND (template_id=" . $template_id . ")";
							$result = $bwb_wiz_dbh->query($query);
						}
						$row_cnt = $result->rowCount();
						if ($row_cnt > 0)
						{
							$row = $result->fetch(PDO::FETCH_ASSOC);
							$page_content = $row['page_content'];
							if ((stripos($page_content, $id_string) !== false) || (stripos($page_content, $id_string2) !== false) || (stripos($page_content, $id_string3) !== false) || (stripos($page_content, $id_string4) !== false))	{
								$area = "left";
							}
							else
							{
								if ($wiz_mode == 1) {
									$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "page_data_special WHERE page_name = 'right'";
									$result = $bwb_dbh->query($query);
								} else {
									$query = "SELECT * FROM page_data_special WHERE (page_name='right') AND (template_id=" . $template_id . ")";
									$result = $bwb_wiz_dbh->query($query);
								}
								$row_cnt = $result->rowCount();
								if ($row_cnt > 0)
								{
									$row = $result->fetch(PDO::FETCH_ASSOC);
									$page_content = $row['page_content'];
									if ((stripos($page_content, $id_string) !== false) || (stripos($page_content, $id_string2) !== false) || (stripos($page_content, $id_string3) !== false) || (stripos($page_content, $id_string4) !== false))	{
										$area = "right";
									}
									else
									{
										$area = "-not found-";
									}
								}
							}
						}
					}
				}
			}
		}

		if ($area != "-not found-")
		{		
			if ($wiz_mode == 1)
			{
				$page_content = ContentWithNewElement($page_content, $header_title, "header_title");
				$query = "UPDATE " . BWB_TABLE_PREFIX . "page_data_special SET page_content = '" . $page_content . "' WHERE page_name = '" . $area . "'";
				$cnt = $bwb_dbh->exec($query);
			}
			else
			{
				switch ($area)	{
					case "header":
						if ($page_content_header == "")
							$page_content_header = ContentWithNewElement($page_content, $header_title, "header_title");
						else
							$page_content_header = ContentWithNewElement($page_content_header, $header_title, "header_title");
						break;
					case "top":
						if ($page_content_top == "")
							$page_content_top = ContentWithNewElement($page_content, $header_title, "header_title");
						else
							$page_content_top = ContentWithNewElement($page_content_top, $header_title, "header_title");
						break;
					case "left":
						if ($page_content_left == "")
							$page_content_left = ContentWithNewElement($page_content, $header_title, "header_title");
						else
							$page_content_left = ContentWithNewElement($page_content_left, $header_title, "header_title");
						break;
					case "right":
						if ($page_content_right == "")
							$page_content_right = ContentWithNewElement($page_content, $header_title, "header_title");
						else
							$page_content_right = ContentWithNewElement($page_content_right, $header_title, "header_title");
						break;
					default:
						break;
				}
			}
			
			if ($got_splash === true)
			{
				if ($wiz_mode == 1) {
					$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "page_data_special WHERE page_name = '" . $area . "_splash'";
					$result = $bwb_dbh->query($query);
				} else {
					$query = "SELECT * FROM page_data_special WHERE (page_name='" . $area . "_splash') AND (template_id=" . $template_id . ")";
					$result = $bwb_wiz_dbh->query($query);
				}
				if ($result->rowCount() > 0)	{
					$row = $result->fetch(PDO::FETCH_ASSOC);
					$page_content = $row['page_content'];
					
					if ($wiz_mode == 1)
					{
						$page_content = ContentWithNewElement($page_content, $header_title, "header_title");
						$query = "UPDATE " . BWB_TABLE_PREFIX . "page_data_special SET page_content = '" . $page_content . "' WHERE page_name = '" . $area . "_splash'";
						$cnt = $bwb_dbh->exec($query);
					}
					else
					{
						switch ($area)	{
							case "header":
								if ($page_content_header_splash == "")
									$page_content_header_splash = ContentWithNewElement($page_content, $header_title, "header_title");
								else
									$page_content_header_splash = ContentWithNewElement($page_content_header_splash, $header_title, "header_title");
								break;
							case "top":
								if ($page_content_top_splash == "")
									$page_content_top_splash = ContentWithNewElement($page_content, $header_title, "header_title");
								else
									$page_content_top_splash = ContentWithNewElement($page_content_top_splash, $header_title, "header_title");
								break;
							case "left":
								if ($page_content_left_splash == "")
									$page_content_left_splash = ContentWithNewElement($page_content, $header_title, "header_title");
								else
									$page_content_left_splash = ContentWithNewElement($page_content_left_splash, $header_title, "header_title");
								break;
							case "right":
								if ($page_content_right_splash == "")
									$page_content_right_splash = ContentWithNewElement($page_content, $header_title, "header_title");
								else
									$page_content_right = ContentWithNewElement($page_content_right_splash, $header_title, "header_title");
								break;
							default:
								break;
						}
					}
				}
			}
		}	
	}

	///////// Insert tagline into border area ////////////////////////////////////
	if (isset($tagline) && ($tagline != ""))
	{
		$id_string = 'id="tagline"';
		$id_string2 = "id='tagline'";
		$id_string3 = 'id=\\"tagline\\"';
		$id_string4 = "id=\\'tagline\\'";
		
		if ($wiz_mode == 1) {
			$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "page_data_special WHERE page_name = 'header'";
			$result = $bwb_dbh->query($query);
		} else {
			$query = "SELECT * FROM page_data_special WHERE (page_name='header') AND (template_id=" . $template_id . ")";
			$result = $bwb_wiz_dbh->query($query);
		}
		$row_cnt = $result->rowCount();
		if ($row_cnt > 0)
		{
			$row = $result->fetch(PDO::FETCH_ASSOC);
			$page_content = $row['page_content'];
			if ((stripos($page_content, $id_string) !== false) || (stripos($page_content, $id_string2) !== false) || (stripos($page_content, $id_string3) !== false) || (stripos($page_content, $id_string4) !== false))	{
				$area = "header";
			}
			else
			{
				if ($wiz_mode == 1) {
					$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "page_data_special WHERE page_name = 'top'";
					$result = $bwb_dbh->query($query);
				} else {
					$query = "SELECT * FROM page_data_special WHERE (page_name='top') AND (template_id=" . $template_id . ")";
					$result = $bwb_wiz_dbh->query($query);
				}
				$row_cnt = $result->rowCount();
				if ($row_cnt > 0)
				{
					$row = $result->fetch(PDO::FETCH_ASSOC);
					$page_content = $row['page_content'];
					if ((stripos($page_content, $id_string) !== false) || (stripos($page_content, $id_string2) !== false) || (stripos($page_content, $id_string3) !== false) || (stripos($page_content, $id_string4) !== false))	{
						$area = "top";
					}
					else
					{
						if ($wiz_mode == 1) {
							$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "page_data_special WHERE page_name = 'left'";
							$result = $bwb_dbh->query($query);
						} else {
							$query = "SELECT * FROM page_data_special WHERE (page_name='left') AND (template_id=" . $template_id . ")";
							$result = $bwb_wiz_dbh->query($query);
						}
						$row_cnt = $result->rowCount();
						if ($row_cnt > 0)
						{
							$row = $result->fetch(PDO::FETCH_ASSOC);
							$page_content = $row['page_content'];
							if ((stripos($page_content, $id_string) !== false) || (stripos($page_content, $id_string2) !== false) || (stripos($page_content, $id_string3) !== false) || (stripos($page_content, $id_string4) !== false))	{
								$area = "left";
							}
							else
							{
								if ($wiz_mode == 1) {
									$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "page_data_special WHERE page_name = 'right'";
									$result = $bwb_dbh->query($query);
								} else {
									$query = "SELECT * FROM page_data_special WHERE (page_name='right') AND (template_id=" . $template_id . ")";
									$result = $bwb_wiz_dbh->query($query);
								}
								$row_cnt = $result->rowCount();
								if ($row_cnt > 0)
								{
									$row = $result->fetch(PDO::FETCH_ASSOC);
									$page_content = $row['page_content'];
									if ((stripos($page_content, $id_string) !== false) || (stripos($page_content, $id_string2) !== false) || (stripos($page_content, $id_string3) !== false) || (stripos($page_content, $id_string4) !== false))	{
										$area = "right";
									}
									else
									{
										$area = "-not found-";
									}
								}
							}
						}
					}
				}
			}
		}
			
		if ($area != "-not found-")
		{		
			if ($wiz_mode == 1)
			{
				$page_content = ContentWithNewElement($page_content, $tagline, "tagline");
				$query = "UPDATE " . BWB_TABLE_PREFIX . "page_data_special SET page_content = '" . $page_content . "' WHERE page_name = '" . $area . "'";
				$cnt = $bwb_dbh->exec($query);
			}
			else
			{
				switch ($area)	{
					case "header":
						if ($page_content_header == "")
							$page_content_header = ContentWithNewElement($page_content, $tagline, "tagline");
						else
							$page_content_header = ContentWithNewElement($page_content_header, $tagline, "tagline");
						break;
					case "top":
						if ($page_content_top == "")
							$page_content_top = ContentWithNewElement($page_content, $tagline, "tagline");
						else
							$page_content_top = ContentWithNewElement($page_content_top, $tagline, "tagline");
						break;
					case "left":
						if ($page_content_left == "")
							$page_content_left = ContentWithNewElement($page_content, $tagline, "tagline");
						else
							$page_content_left = ContentWithNewElement($page_content_left, $tagline, "tagline");
						break;
					case "right":
						if ($page_content_right == "")
							$page_content_right = ContentWithNewElement($page_content, $tagline, "tagline");
						else
							$page_content_right = ContentWithNewElement($page_content_right, $tagline, "tagline");
						break;
					default:
						break;
				}
			}
			
			if ($got_splash === true)
			{
				if ($wiz_mode == 1) {
					$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "page_data_special WHERE page_name = '" . $area . "_splash'";
					$result = $bwb_dbh->query($query);
				} else {
					$query = "SELECT * FROM page_data_special WHERE (page_name='" . $area . "_splash') AND (template_id=" . $template_id . ")";
					$result = $bwb_wiz_dbh->query($query);
				}
				if ($result->rowCount() > 0)	{
					$row = $result->fetch(PDO::FETCH_ASSOC);
					$page_content = $row['page_content'];
					
					if ($wiz_mode == 1)
					{	
						$page_content = ContentWithNewElement($page_content, $tagline, "tagline");
						$query = "UPDATE " . BWB_TABLE_PREFIX . "page_data_special SET page_content = '" . $page_content . "' WHERE page_name = '" . $area . "_splash'";
						$cnt = $bwb_dbh->exec($query);
					}
					else
					{	
						switch ($area)	{
							case "header":
								if ($page_content_header_splash == "")
									$page_content_header_splash = ContentWithNewElement($page_content, $tagline, "tagline");
								else
									$page_content_header_splash = ContentWithNewElement($page_content_header_splash, $tagline, "tagline");
								break;
							case "top":
								if ($page_content_top_splash == "")
									$page_content_top_splash = ContentWithNewElement($page_content, $tagline, "tagline");
								else
									$page_content_top_splash = ContentWithNewElement($page_content_top_splash, $tagline, "tagline");
								break;
							case "left":
								if ($page_content_left_splash == "")
									$page_content_left_splash = ContentWithNewElement($page_content, $tagline, "tagline");
								else
									$page_content_left_splash = ContentWithNewElement($page_content_left_splash, $tagline, "tagline");
								break;
							case "right":
								if ($page_content_right_splash == "")
									$page_content_right_splash = ContentWithNewElement($page_content, $tagline, "tagline");
								else
									$page_content_right_splash = ContentWithNewElement($page_content_right_splash, $tagline, "tagline");
								break;
							default:
								break;
						}
					}
				}
			}
		}
	}

	// If builder SQL file for download, take all combined modifications for special areas and add to SQL file.
	if ($wiz_mode == 0)
	{
		if ($page_content_header != "")
			$sql .= "UPDATE " . BWB_TABLE_PREFIX . "page_data_special SET page_content = '" . $page_content_header . "' WHERE page_name = 'header';\n\n";
		if ($page_content_top != "")
			$sql .= "UPDATE " . BWB_TABLE_PREFIX . "page_data_special SET page_content = '" . $page_content_top . "' WHERE page_name = 'top';\n\n";
		if ($page_content_left != "")
			$sql .= "UPDATE " . BWB_TABLE_PREFIX . "page_data_special SET page_content = '" . $page_content_left . "' WHERE page_name = 'left';\n\n";
		if ($page_content_right != "")
			$sql .= "UPDATE " . BWB_TABLE_PREFIX . "page_data_special SET page_content = '" . $page_content_right . "' WHERE page_name = 'right';\n\n";
			
		if ($got_splash === true)
		{
			if ($page_content_header_splash != "")
				$sql .= "UPDATE " . BWB_TABLE_PREFIX . "page_data_special SET page_content = '" . $page_content_header_splash . "' WHERE page_name = 'header_splash';\n\n";
			if ($page_content_top_splash != "")
				$sql .= "UPDATE " . BWB_TABLE_PREFIX . "page_data_special SET page_content = '" . $page_content_top_splash . "' WHERE page_name = 'top_splash';\n\n";
			if ($page_content_left_splash != "")
				$sql .= "UPDATE " . BWB_TABLE_PREFIX . "page_data_special SET page_content = '" . $page_content_left_splash . "' WHERE page_name = 'left_splash';\n\n";
			if ($page_content_right_splash != "")
				$sql .= "UPDATE " . BWB_TABLE_PREFIX . "page_data_special SET page_content = '" . $page_content_right_splash . "' WHERE page_name = 'right_splash';\n\n";
		}
	}
	
	// Set default page titles same as header_title.
	if (isset($header_title) && ($header_title != ""))
		$page_title = $header_title;
	else
		$page_title = "";
	if ($wiz_mode == 1)
	{
		$query = "SELECT COUNT(*) as default_cnt FROM " . BWB_TABLE_PREFIX . "page_titles WHERE page_name='_default_'";
		$result = $bwb_dbh->query($query);
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$default_cnt = $row['default_cnt'];
		if ($default_cnt > 0)
			$query = "UPDATE " . BWB_TABLE_PREFIX . "page_titles SET title='$page_title' WHERE page_name='_default_'";
		else
			$query = "INSERT INTO " . BWB_TABLE_PREFIX . "page_titles SET title='$page_title', page_name='_default_'";
		$cnt = $bwb_dbh->exec($query);
	}
	else
		$sql .= "REPLACE INTO " . BWB_TABLE_PREFIX . "page_titles SET title='$page_title', page_name='_default_';";
	
	// Copy any plugin files into template folder to be zipped.
	$plugin_zip_file_dir = $doc_root . "/plugins/";
	foreach ($deployed_plugins as $plugin_id)
	{
		$query = "SELECT plugin_zip_file FROM page_name_suggestions WHERE plugin_id = '$plugin_id'";
		$result = $bwb_wiz_dbh->query($query);
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$plugin_zip_file = $row['plugin_zip_file'];
		if (isset($plugin_zip_file) && ($plugin_zip_file != ""))	{
			$plugin_zip_file_path = $plugin_zip_file_dir . $plugin_zip_file;
			$destination = $doc_root . "/tmp" . $cur_ts . "/" . $plugin_zip_file;
			copy($plugin_zip_file_path, $destination);
		}	
	}

	// Log wizard usage.
	$user_id = $_SESSION['userid'];
	$website = $_SERVER['SERVER_NAME'];
	$query = "INSERT INTO wizard_log SET user_id='$user_id', wizard_mode=$wiz_mode, template_id=$template_id, website='$website'";
	$cnt = $bwb_wiz_dbh->exec($query);
	
	////////////////////////////////////////////////////////////////////////
	// Template done. Now deliver according to wizard mode and price.
	if ($wiz_mode == 1)
	{
		session_start();
		$_SESSION['access_level'] = "admin";
		$_SESSION['logged_in'] = 10;
		echo "<script>\n";
		echo "window.location='/manage/wizard_complete.php';\n";
		echo "</script>\n";
		exit;
	}
	else
	{
		// Create SQL file for deploying template at another site.
		$file_name = $doc_root . "/tmp" . $cur_ts . "/template.sql";
		$fh = fopen($file_name, 'x') or die("Can't create $file_name: $php_errormsg");
		$res = (fwrite($fh, $sql));
		fclose($fh) or die($php_errormsg);
		@chmod($file_name, 0755);
		
		// Compress all files into single file for download.
		$file_name = $doc_root . "/tmp" . $cur_ts . "/template_" . $template_id . ".tgz";
		$gz_file_name = $doc_root . "/tmp" . $cur_ts . "/template_" . ".tar.gz";
		$tar_file_name = $doc_root . "/tmp" . $cur_ts . "/template_" . ".tar";

		$compression_err = false;
		try	{
			$phar = new PharData($tar_file_name);
		
			// ADD FILES TO archive.tar FILE
			$phar->buildFromDirectory($doc_root . "/tmp" . $cur_ts);
					
			// COMPRESS archive.tar FILE. COMPRESSED FILE WILL BE archive.tar.gz
			$phar->compress(Phar::GZ);
		
			// NOTE THAT BOTH FILES WILL EXISTS. SO IF YOU WANT YOU CAN UNLINK archive.tar
			unlink($tar_file_name);
			rename($gz_file_name, $file_name);
		} 
		catch (Exception $e) {
			$compression_err = true;
		}

		// Check for errors (you can check for errors at any point)
		if ($compression_err)
			print ("Errors occurred: " . $e);
		elseif ($_POST['wizVar_build_your_own'] == 1)
		{

//echo "<pre>\n";
//print_r($_POST);
//print_r($page_arr);
//echo "\n</pre>\n";
//return;
			$query = "SELECT plugin_id FROM page_name_suggestions WHERE page_name = 'catalog'";
			$result = $bwb_wiz_dbh->query($query);
			$row = $result->fetch(PDO::FETCH_ASSOC);
			$catalog_plugin_id = $row['plugin_id'];

			$plugin_str = "";
			$plugin_delim = "";
			foreach ($page_arr as $key => $val)
			{
				$plugin_str .= $plugin_delim . $val[2];
				$plugin_delim = "_";
				if ($val[2] == $catalog_plugin_id)
					$install_oc = 1;
			}
			
			echo "<script>\n";
			echo "window.location='https://getthebreeze.com/php/start_acct_setup.php?template_folder=tmp" . $cur_ts . "&template_file=template_" . $template_id . "%2Etgz&template_id=" . $template_id . "&plugins=" . $plugin_str . "&install_oc=" . $install_oc . "&coupon=" . $_POST['wizVar_coupon'] . "';\n";	
			echo "</script>\n";
		}
		elseif ($price > 0)
		{
			$temp_folder = "tmp" . $cur_ts;
			$temp_file = "template_" . $template_id . ".tgz";
			$wiz_website = $_SERVER['HTTP_HOST'];
			$user_id = $_SESSION['userid'];
			
			echo "<script>\n";
			echo "window.location='https://breezewebsitebuilder.com/php/checkout.php?item_name=Template%20Download&download_mod_dir=wizard&download_module=download_template.php&price=$price&temp_folder=$temp_folder&temp_file=$temp_file&wiz_website=$wiz_website&user_id=$user_id';\n";
			echo "</script>\n";	
		}
		else
		{		
?>
<script>
	document.getElementById("packaging_div").style.display = "none";
	document.getElementById("jp_container_1").style.display = "none";
</script>
	<h1 style="text-align:center;">The Breeze Wizard is complete!</h1>
	<p style="text-align:center;">Congratulations! Your template is ready for download. Click the download button to download your template.</p>
     <div id="download"><a href="../tmp<?php echo $cur_ts; ?>/template_<?php echo $template_id; ?>.tgz">Download</a></div>
	<p style="text-align:center;">Use the template installation wizard to install your template in the Breeze Website Builder.</p>
	
<?php
		}
	}

	////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////////////////////////////
	function deploy_table($table, $table_prefix, $dbh, $dbh2, $database, $database2, $template_id, $wiz_mode, $replace = false)
	{
		if ($wiz_mode == 0)
		{
			$sql = create_table_sql($table, $table_prefix, $dbh, $dbh2, $database, $database2, $template_id, $replace);
			return $sql;
		}

		if (!$replace)	{
			$query = "TRUNCATE TABLE `" . $table_prefix . $table . "`";
			$cnt = $dbh->exec($query);
		}

		$query = "SELECT * FROM " . $table . " WHERE template_id = " . $template_id;
//		echo "query: $query\n<br><br>\n";
		$result = $dbh2->query($query);

		while ($row = $result->fetch(PDO::FETCH_ASSOC))
		{
			if ($replace)
				$sql = "REPLACE INTO " . $table_prefix . $table . " SET ";
			else
				$sql = "INSERT INTO " . $table_prefix . $table . " SET ";
				
			if (substr($table, strlen($table) - 4) == "_css")
				$query2 = "SHOW COLUMNS FROM " . $table_prefix . "site_css FROM " . $database;
			elseif (substr($table, strlen($table) - 8) == "_classes")
				$query2 = "SHOW COLUMNS FROM " . $table_prefix . "site_classes FROM " . $database;
			else
				$query2 = "SHOW COLUMNS FROM " . $table_prefix . $table . " FROM " . $database;

			$result2 = $dbh->query($query2);
			while ($column = $result2->fetch(PDO::FETCH_ASSOC))
			{
				if (($column['Field'] != "id") && ($column['Field'] != "template_id"))
				{
					if (strpos($sql, "=")) 
						$sql .= ", ";
					$field = $column['Field'];
					$sql .= "`" . $field . "` = '" . addslashes($row[$field]) . "'";
				}
			}
//			echo "sql: $sql\n<br><br>\n";
			$cnt = $dbh->exec($sql);
		}
		return "";
	}

	///////////////////////////////////////////////////////////////////
	function create_table_sql($table, $table_prefix, $dbh, $dbh2, $database, $database2, $template_id, $replace = false)
	{	
		$sql = "";

		if (!$replace)
			$sql .= "TRUNCATE TABLE `" . $table_prefix . $table . "`;\n\n";
			
		$query = "SELECT * FROM " . $table . " WHERE template_id = " . $template_id;
		$result = $dbh2->query($query);
		while ($row = $result->fetch(PDO::FETCH_ASSOC))
		{
			if ($replace)
				$sqlstr = "REPLACE INTO " . $table_prefix . $table . " SET ";
			else
				$sqlstr = "INSERT INTO " . $table_prefix . $table . " SET ";
			
			if ($source_table)
				$query2 = "SHOW COLUMNS FROM " . $source_table . " FROM " . $database2;
			else
				$query2 = "SHOW COLUMNS FROM " . $table . " FROM " . $database2;
			
			$result2 = $dbh2->query($query2);
			while ($column = $result2->fetch(PDO::FETCH_ASSOC))
			{
				if (($column['Field'] != "id") && ($column['Field'] != "template_id"))
				{
					if (strpos($sqlstr, "=")) 
						$sqlstr .= ", ";
					$field = $column['Field'];
					$sqlstr .= "`" . $field . "` = '" . addslashes($row[$field]) . "'";
				}
			}
			if ((strpos($table, "page_data_special") !== false) || (strpos($table, "head") !== false))	{
				$sqlstr = str_replace( chr(10), "\\n", $sqlstr );
				$sqlstr = str_replace( chr(13), "\\r", $sqlstr );
			}
			$sqlstr .= ";\n";
			$sql .= $sqlstr;	
		}
		$sql .= "\n\n";
		return $sql;
	}
	
	//////////////////////////////////////////////////////////////////////
	function ContentWithNewLogo($page_content, $logo, $website_path, $image_folder)
	{
		if ($website_path == '')
			$rel_path = '';
		else
			$rel_path = "../";
		$logo_code = "<img src=\"" . $rel_path . $image_folder . "/" . $logo . "\" alt=\"logo\">\n";
		$logo_src = " src=\"" . $rel_path . $image_folder . "/" . $logo . "\" ";
		$logo_src = addslashes($logo_src);
		
		if (strpos($page_content, "<img") !== false)	// Look for template logo and replace.
		{
			$img_start = strpos($page_content, "<img");
			$img_end = strpos($page_content, ">", $img_start + 5);
			$img = stripslashes(substr($page_content, $img_start, $img_end - $img_start + 1));

			if (stripos($img, 'id="logo"') !== false)
			{
				$src_start = stripos($page_content, "src=", $img_start);
				
				$src_end = strpos($page_content, '"', $src_start + 6);
				
				//////////////////////////////////////////////////////////
				$page_content_front = substr($page_content, 0, $src_start - 1);
				$page_content_back = substr($page_content, $src_end + 1);
				$page_content = $page_content_front . $logo_src . $page_content_back;
			}
			else
				$page_content = $logo_code . $page_content;
		}
		return ($page_content);
	}
	
	//////////////////////////////////////////////////////////////////////
	function ContentWithNewElement($page_content, $string, $element_id)
	{
		$id_string = 'id="' . $element_id . '"';
		$id_string2 = "id='" . $element_id . "'";
		$id_string3 = 'id=\\"' . $element_id . '\\"';
		$id_string4 = "id=\\'" . $element_id . "\\'";
		
		if ((strpos($page_content, $id_string) !== false) || (strpos($page_content, $id_string2) !== false) || (strpos($page_content, $id_string3) !== false) || (strpos($page_content, $id_string4) !== false))	// Look for template element and replace.
		{
			$found = false;
			$el_start = -1;
			$el_end = -1;
			if (strpos($page_content, $id_string) !== false)
				$id_start = strpos($page_content, $id_string);
			elseif (strpos($page_content, $id_string2) !== false)
				$id_start = strpos($page_content, $id_string2);
			elseif (strpos($page_content, $id_string3) !== false)
				$id_start = strpos($page_content, $id_string3);
			elseif (strpos($page_content, $id_string4) !== false)
				$id_start = strpos($page_content, $id_string4);
			$offset = 0;
			while(!$found)
			{
				$el_start = strpos($page_content, "<", $offset);
				$el_start = strpos($page_content, ">", $el_start);
				$el_end = strpos($page_content, "<", $el_start + 1);
				if ($el_end > $id_start)
					$found = true;
				elseif ($el_start === false)
					return($page_content);
				else
					$offset = $el_end;
			}
			
			$page_content_front = substr($page_content, 0, $el_start + 1);
			$page_content_back = substr($page_content, $el_end);
			$page_content = $page_content_front . $string . $page_content_back;
		}
		return ($page_content);
	}
	
	/////////////////////////////////////////////////////////////////////////////////////////
	//$plugin, $bwb_dbh, $bwb_database, BWB_TABLE_PREFIX, $wiz_mode
	function DeployPlugin($plugin, $dbh, $wiz_dbh, $table_prefix, $wiz_mode, $template_id)
	{
		$sql = "";
		
		$query = "SELECT * FROM `" . $table_prefix . "plugins` WHERE `id` = $plugin";
		$result = $dbh->query($query);
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$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"];
		$plugin_name = $row["name"];

		if ($wiz_mode == 1)	// local
		{
			if (isset($style_sheet) && ($style_sheet != ""))
			{
				$query = "SELECT * FROM " . $table_prefix . "style_sheets WHERE `name`='" . $style_sheet . "'";
				$result = $dbh->query($query);
				if ($result->rowCount() < 1)
				{
					$query = "SELECT MAX(sort_order) AS sort_order FROM " . $table_prefix . "style_sheets";
					$result = $dbh->query($query);
					$row = $result->fetch(PDO::FETCH_ASSOC);
					$sort_order = $row['sort_order'] + 10;
					
					$query = "REPLACE INTO " . $table_prefix . "style_sheets SET `name`='" . $style_sheet . "', `description`='" . $style_sheet_description . "', `sort_order`=" . $sort_order;
					$cnt = $dbh->exec($query);
					
					if (isset($class_sql) && ($class_sql != ""))
					{
						$result = $dbh->query($class_sql);
					}
					
					if (isset($css_sql) && ($css_sql != ""))
					{
						try	{
							$result = $dbh->query($css_sql);
						} catch (Exception $e) {
							echo 'Error: Possible column mismatch in CSS SQL: ' . $e->getMessage() . "<br />\n";
						}
					}
					
					if (isset($custom_css_sql) && ($custom_css_sql != ""))
					{
						$result = $dbh->query($custom_css_sql);
					}
				}
			}
			
			if (isset($plugin_sql) && ($plugin_sql != ""))
			{
				$result = $dbh->query($plugin_sql);
			}
			
			$query = "REPLACE INTO " . $table_prefix . "plugins_deployed SET `plugin_id`='$plugin', `name`='$plugin_name', `deployed`=1";
			$cnt = $dbh->exec($query);
		}
		else	// download
		{
			if (isset($style_sheet) && ($style_sheet != ""))
			{
				// Always add classes from plugin because stock classes are not stored in repository.
				if (isset($class_sql) && ($class_sql != ""))
					$sql .= $class_sql . "\n\n";
						
				$query = "SELECT * FROM `style_sheets` WHERE `name` = '$style_sheet' AND template_id = '$template_id'";
				$result = $wiz_dbh->query($query);
				if ($result->rowCount() < 1)
				{
					$sql .= "REPLACE INTO " . $table_prefix . "style_sheets SET `name`='" . $style_sheet . "', `description`='" . $style_sheet_description . "', `sort_order`=10000 ;\n\n";
					
					if (isset($css_sql) && ($css_sql != ""))
						$sql .= $css_sql . "\n\n";
					
					if (isset($custom_css_sql) && ($custom_css_sql != ""))
						$sql .= $css_sql . "\n\n";
				}
			}
			
			if (isset($plugin_sql) && ($plugin_sql != ""))
				$sql .= $plugin_sql . "\n\n";
			
			$sql .= "REPLACE INTO " . $table_prefix . "plugins_deployed SET `plugin_id`='$plugin', `name`='$plugin_name', `deployed`=1;\n\n";
		}		
		return($sql);
	}
	
	////////////////////////////////////////////////////////////////////////////////////////////////
	function copy_table_structure($table, $table_prefix, $dbh, $database, $wiz_mode)
	{
		$sql = "";
		
		if ($wiz_mode == 1)	// local
		{
			$query = "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '" . $database . "' AND table_name = '" . $table_prefix . $table . "'";
			$result = $dbh->query($query);
			$row = $result->fetch(PDO::FETCH_ASSOC);
			$table_count = $row['COUNT(*)'];
			if ($table_count < 1)
			{
				// Must be a missing menu table, find suffix.
				if (strpos($table, "_classes") > 0)
					$source_table = $table_prefix . "site_classes";
				else
					$source_table = $table_prefix . "site_css";
				$query = "CREATE TABLE IF NOT EXISTS " . $table_prefix . $table . " SELECT * FROM " . $source_table;
				$cnt = $dbh->exec($query);
				
				$query = " ALTER TABLE `" . $table_prefix . $table . "` ADD PRIMARY KEY (`class`)";
				$cnt = $dbh->exec($query);
			}
			
			$query = "TRUNCATE TABLE `" . $table_prefix . $table . "`";
			$cnt = $dbh->exec($query);
		}
		else	// download
		{
			if (strpos($table, "_classes") > 0)
				$source_table = $table_prefix . "site_classes";
			else
				$source_table = $table_prefix . "site_css";
			$sql .= "CREATE TABLE IF NOT EXISTS " . $table_prefix . $table . " SELECT * FROM " . $source_table . " ;\n\n";
			
			$sql .= " ALTER TABLE `" . $table_prefix . $table . "` ADD PRIMARY KEY (`class`) ;\n\n";
			
			$sql .= "TRUNCATE TABLE `" . $table_prefix . $table . "` ;\n\n";
		}
			
		return($sql);
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////////
	function CopyBackgroundImage($image_file, $doc_root, $userfiles, $image_folder, $cur_ts, $cpanel_user_id)
	{
		$source = $doc_root . $userfiles . "/" . $image_folder . "/" . $image_file;
		$destination = "/home/" . $cpanel_user_id . "/public_html/tmp" . $cur_ts . "/userfiles/image/" . $image_file;
		copy($source, $destination);
	}
?>