<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 3/29/2017	ELL
// Created Wizard version: 4/5/2017	ELL
// 
	include_once ("check_session.php");

	if ($wiz_mode == 1)	{
		$query_custom_fonts = "SELECT * FROM " . BWB_TABLE_PREFIX . "fonts WHERE (src IS NOT NULL) AND (src <> '')";
		$result_custom_fonts = $bwb_dbh->query($query_custom_fonts);
	}
	else	{
		$query_custom_fonts = "SELECT * FROM fonts WHERE (src IS NOT NULL) AND (src <> '') AND (template_id = " . $template_id . ")";
		$result_custom_fonts = $bwb_wiz_dbh->query($query_custom_fonts);
	}
	
	$css_string = "";
	while ($row_custom_fonts = $result_custom_fonts->fetch(PDO::FETCH_ASSOC))
	{
		$font = $row_custom_fonts['font'];
		$src = $row_custom_fonts['src'];
		$weight = $row_custom_fonts['weight'];
		$style = $row_custom_fonts['style'];
		$stretch = $row_custom_fonts['stretch'];
		
		$css_string .= "@font-face {\n    font-family: " . $font . ";\n";
		if (strpos($src, "'") === false)
			$css_string .= "    src: url('" . $src . "');\n";
		else
			$css_string .= "    src: url(" . $src . ");\n";
		if (isset($weight) && ($weight != ""))
			$css_string .= "	font-weight: " . $weight . ";\n";
		if (isset($style) && ($style != ""))
			$css_string .= "	font-style: " . $style . ";\n";
		if (isset($stretch) && ($stretch != ""))
			$css_string .= "	font-stretch: " . $stretch . ";\n";
		$css_string .= "}\n\n";
	}
	
	if ($wiz_mode == 1)
		$file_name = $doc_root . $userfiles . "/" . BWB_CSS_DIR . "/fonts.css";
	else
		$file_name = $doc_root . "/tmp" . $cur_ts . "/userfiles/css/fonts.css";
		
	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);

?>