<?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 reference to website folder configuration.
//
include_once ("check_session.php");
	
function WriteCSSFile($css_file, $css_string, $wiz_mode, $template_id, $cur_ts, $website_path, $css_folder)
{
	$document_root = $_SERVER['DOCUMENT_ROOT'];
	if ($wiz_mode == 1)
	{
		if ($website_path == '')
			$userfiles = '';
		else
			$userfiles = "/" . $website_path;
		$file_name = $document_root . $userfiles . "/" . $css_folder . "/" . $css_file;
	}
	else
		$file_name = $document_root . "/tmp" . $cur_ts . "/userfiles/css/" . $css_file;
	
	if (file_exists($file_name))
	{
		$bkup_file_name = $file_name . time();
		if (!rename($file_name, $bkup_file_name))
			echo "Could not rename $file_name to $bkup_file_name.<br>";
		@chmod($bkup_file_name, 0644);
	}
	
	// Create file.
	$fh = fopen($file_name, 'x') or die("Can't create $file_name: $php_errormsg");
	$result = (fputs($fh, $css_string));
	fclose($fh) or die($php_errormsg);
	@chmod($file_name, 0644);
	
	/////////////////////////////////////////////////////////
	// Add custom styles to CKEditor style list. ///////////
	if ($css_file == "site.css")
	{
		// Build string for styles.js file /////
		include("build_jason_style_string_wiz.php");
		
		// Rename styles.js file for backup
		if ($wiz_mode == 1)
			$file_name = $document_root . "/ckeditor/styles.js";
		else
			$file_name = $document_root . "/tmp" . $cur_ts . "/ckeditor/styles.js";
		if (file_exists($file_name))
		{
			$bkup_file_name = $file_name . time();
			if (!rename($file_name, $bkup_file_name))
				echo "Could not rename $file_name to $bkup_file_name.<br>";
			@chmod($bkup_file_name, 0644);
		}
		
		// Create file.
		$fh = fopen($file_name, 'x') or die("Can't create $file_name: $php_errormsg");
		$result = (fputs($fh, $jason_string));
		fclose($fh) or die($php_errormsg);
		@chmod($file_name, 0644);
	}
}

?>
