<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Revised: 8/1/2009	ELL
// Added ability to operate using different class tables.  This was done to give the ability
// to style calendar grids and event grids without adding 14 more classes to the site style list.
// Renamed from delete_tag.php to delete_class.php.
// Added feed thru to show/hide custom styles.
// Revised: 9/15/2009	ELL
// Added removal of class from CSS table as well.  Otherwise, style sheet will continue to contain this style even
// though you cannot edit it.
// Revised: 2/8/2012	ELL
// Added table prefix handling.
// Modified: 12/2/2016	ELL
// Consolidated all class tables into site_classes table indexed by style_sheet field.
// Consolidated all CSS tables into site_css table indexed by style_sheet field.
//
	include ("check_session.php");
	include("lib/build_css_string.php");

	$class = $_REQUEST['class'];
	$search = array("PERIOD", "POUND", "SPACE", "COMMA", "GREATERTHAN", "GREATERTHAN", "PLUS", "TILDA");
	$replace = array(".", "#", " ", ",", ">", "&gt;", "+", "~");
	$class = str_replace($search, $replace, $class);
	$style_sheet = $_REQUEST['style_sheet'];
	$css_file =  $style_sheet . ".css";
	
	// Go ahead and delete tag from class table.
	$query = "DELETE FROM `" . BWB_TABLE_PREFIX . "site_classes` WHERE `style_sheet` = '$style_sheet' AND `class` = '$class'";
	$cnt = $bwb_dbh->exec($query);
	
	// Also remove tag from tag from css table.
	$query = "DELETE FROM `" . BWB_TABLE_PREFIX . "site_css` WHERE `style_sheet` = '$style_sheet' AND `class` = '$class'";
	$cnt = $bwb_dbh->exec($query);
	
	// Build new CSS string without deleted class.
	$css_string = BuildCSSString($style_sheet, $bwb_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, BWB_TABLE_PREFIX);
	
	////////////////////////////////////////////////////////
	/// Write new css file /////////////
	//Test to see if file already exists.
	$document_root = $_SERVER['DOCUMENT_ROOT'];
	if (BWB_BASE_DIR == '')
		$userfiles = '';
	else
		$userfiles = "/" . BWB_BASE_DIR;
		
	$file_name = $document_root . $userfiles . "/" . BWB_CSS_DIR . "/" . $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 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);
	
	/////////////////////////////////////////////////////////
	// Add custom styles to CKEditor style list. ///////////
	// 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 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;
	
?>