<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 3/27/2018	ELL
// 
	include ("check_session.php");

	$font = $_POST['font'];
	$src = $_POST['src'];
	$old_font = $_POST['old_font'];
	$old_src = $_POST['old_src'];
	$src_type = $_POST['src_type'];
	$stretch = $_POST['stretch'];
	$style = $_POST['style'];
	$weight = $_POST['weight'];
	
	if (!defined("BWB_FONTS_DIR"))
		define('BWB_FONTS_DIR', 'fonts');
	
	$src = str_replace("'", "", $src);
	if ($src_type == "url")	{
		if (substr($src, 0, 4) != "http")	{
			$src2 = $src;
			if (substr($str, 0, 1) != "/")
				$src2 = "/" . $src2;
			$doc_root = $_SERVER['DOCUMENT_ROOT'];
			if (!file_exists($doc_root . $src2))
				$src = $http . $src;
		}
	}

	//First check to see if font exists.
	$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . "fonts` WHERE `font` = '$old_font' AND `src` = '$old_src'";
	$result = $bwb_dbh->query($query);
	if ($result->rowCount() > 0)
		$query = "UPDATE `" . BWB_TABLE_PREFIX . "fonts` SET `src` = '$src', `stretch` = '$stretch', `style` = '$style', `weight` = '$weight' WHERE (`font` = '$old_font') AND (`src` = '$old_src')";
	else
		$query = "INSERT INTO `" . BWB_TABLE_PREFIX . "fonts` SET `font` = '$font', `src` = '$src', `stretch` = '$stretch', `style` = '$style', `weight` = '$weight'";
	$cnt = $bwb_dbh->exec($query);
	
	// Double check and make sure font style sheet in style sheets list.
	$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . "style_sheets` WHERE `name` = 'fonts'";
	$result = $bwb_dbh->query($query);
	if ($result->rowCount() < 1)
	{
		$query = "INSERT INTO `" . BWB_TABLE_PREFIX . "style_sheets` (`name`, `description`, `show_class_editor`, `show_custom_styles`, `sort_order`) VALUES ('fonts', 'Custom Font Styles', 0, 0, 5)";
		$cnt = $cnt + $bwb_dbh->exec($query);
	}
	
	// Create cust font CSS file.
	include("write_custom_font_css.php");
	
	echo $cnt
?>
