<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 11/7/2016	ELL
//
	include ("check_session.php");
	
	$style_sheet = $_REQUEST['style_sheet'];
	$charset = $_REQUEST['charset'];
	$import = $_REQUEST['import'];
	$media_type_modifier = $_REQUEST['media_type_modifier'];
	$media_type = $_REQUEST['media_type'];
	$media_feature = $_REQUEST['media_feature'];
	$media_feature_value = $_REQUEST['media_feature_value'];
	
	$query = "UPDATE " . BWB_TABLE_PREFIX . "style_sheets SET charset = '$charset', import = '$import', media_type_modifier = '$media_type_modifier', media_type = '$media_type', media_feature = '$media_feature', media_feature_value = '$media_feature_value' WHERE name = '$style_sheet'";
	$result = $bwb_dbh->query($query);
	
	$_SESSION['last_style_sheet_edited'] = $style_sheet;
	
	// Build CSS string
	include("lib/build_css_string.php");
	$css_string = BuildCSSString($style_sheet, $bwb_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, BWB_TABLE_PREFIX);
	
	// Setup path to CSS file.
	$document_root = $_SERVER['DOCUMENT_ROOT'];
	if (BWB_BASE_DIR == '')
		$userfiles = '';
	else
		$userfiles = "/" . BWB_BASE_DIR;
		
	$css_file = $style_sheet . ".css";
	$file_name = $document_root . $userfiles . "/" . BWB_CSS_DIR . "/" . $css_file;
	// Backup existing CSS file.
	if (file_exists($file_name))
	{
		$bkup_file_name = $file_name . time();
		if (!rename($file_name, $bkup_file_name))
			echo EDIT_CSS_COULD_NOT_RENAME_1_ERR . " $file_name " . EDIT_CSS_COULD_NOT_RENAME_2_ERR . " $bkup_file_name.<br>";
		@chmod($bkup_file_name, 0644);
	}
	
	// Create CSS file.
	$fh = fopen($file_name, 'x') or die(EDIT_CSS_CANT_CREATE_ERR . " $file_name: $php_errormsg");
	$result = (fputs($fh, $css_string));
	fclose($fh) or die($php_errormsg);
	@chmod($file_name, 0644);
	
	// Build string for fckstyles.xml file /////
	include("build_jason_style_string.php");
	
	// Rename styles.js file for backup
	$file_name = $document_root . "/ckeditor/styles.js";
	if (file_exists($file_name))
	{
		$bkup_file_name = $file_name . time();
		if (!rename($file_name, $bkup_file_name))
			echo EDIT_CSS_COULD_NOT_RENAME_1_ERR . " $file_name " . EDIT_CSS_COULD_NOT_RENAME_2_ERR . " $bkup_file_name.<br>";
		@chmod($bkup_file_name, 0644);
	}
	
	// Create styles.js file.
	$fh = fopen($file_name, 'x') or die(EDIT_CSS_CANT_CREATE_ERR . " $file_name: $php_errormsg");
	$result = (fputs($fh, $jason_string));
	fclose($fh) or die($php_errormsg);
	@chmod($file_name, 0644);
	
	echo 1;
?>