<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 5/11/2015	ELL
// Revised: 3/29/2017	ELL
// Added include of write_custom_font_css.php to move creation of font CSS from editing style sheets to adding/deleting custom fonts.
// 
	include ("check_session.php");

	$font = $_POST['font'];
	$src_type = $_POST['src_type'];
	$stretch = $_POST['stretch'];
	$style = $_POST['style'];
	$weight = $_POST['weight'];
	$style_sheet = $_POST['style_sheet'];
	
	if (!defined("BWB_FONTS_DIR"))
		define('BWB_FONTS_DIR', 'fonts');
	
	if ($src_type == "upload")
	{
		$document_root = $_SERVER['DOCUMENT_ROOT'];
		if (BWB_BASE_DIR == "")
			$font_dir = $document_root . "/" . BWB_FONTS_DIR;
		else
			$font_dir = $document_root . "/" . BWB_BASE_DIR . "/" . BWB_FONTS_DIR;
		if (!file_exists($font_dir))
			mkdir($font_dir);
		
		$target_file = $font_dir . "/" . basename($_FILES["src_file"]["name"]);
		if (BWB_BASE_DIR == "")
			$src = "/" . BWB_FONTS_DIR . "/" . basename($_FILES["src_file"]["name"]);
		else
			$src = "/" . BWB_BASE_DIR . "/" . BWB_FONTS_DIR . "/" . basename($_FILES["src_file"]["name"]);
		$fontFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
	
		// Check if file already exists
		if (file_exists($target_file)) {
			echo ERROR_CAPS_WORD . ": " . MANAGE_CUSTOM_FONTS_ADDING_FONT_UPLOAD_ERR . " '$target_file' " . MANAGE_CUSTOM_FONTS_ADDING_FONT_2_ERR;
			exit;
		}		
		// Check file types
		if (($fontFileType != "ttf") && ($fontFileType != "otf") && ($fontFileType != "woff") && ($fontFileType != "woff2") && ($fontFileType != "svg")  && ($fontFileType != "eot")) {
			echo MANAGE_CUSTOM_FONTS_ADDING_FONT_FILE_TYPE_ERR;
			exit;
		}
		else {
			if (!move_uploaded_file($_FILES["src_file"]["tmp_name"], $target_file))	{
				echo ERROR_CAPS_WORD . ": " . MANAGE_CUSTOM_FONTS_ADDING_FONT_UPLOAD_ERR;
				exit;
			}
		}
	}
	else
	{
		$src = $_POST['src_url'];
		$src = str_replace("'", "", $src);
		if (substr($src, 0, 4) != "http")
			$src = "http://" . $src;
	}

	//First check to see if font exists.
	$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . "fonts` WHERE `font` = '$font' AND `src` = '$src'";
	$result = $bwb_dbh->query($query);
	if ($result->rowCount() > 0)
	{
		echo ERROR_CAPS_WORD . ": '$font' " . MANAGE_CUSTOM_FONTS_ADDING_FONT_1_ERR . " '$src' " . MANAGE_CUSTOM_FONTS_ADDING_FONT_2_ERR;
		exit;
	}
	
	// Add font to database.
	$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 = $bwb_dbh->exec($query);
	}
	
	// Create cust font CSS file.
	include("write_custom_font_css.php");
	
	header("Location: manage_custom_fonts.php?style_sheet=$style_sheet&font_added=1");
?>
