<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 7/7/2012	ELL
// 
//

include ("check_session.php");
		
$page_name = $_REQUEST['page_name'];
$plugin = "Blog Template";
$mode = $_REQUEST['mode'];

	if ($page_name == "")
	{
		echo "Oops! You forgot to select a page to block to.  Click <a href=\"main.php?mode=$mode\" title=\"here\">here</a> and select a page.";
		exit;
	}
	
	//First check to see if file is already in database.
	$query = "SELECT `page_name` FROM `" . BWB_TABLE_PREFIX . "page_data` WHERE `page_name`='$page_name'";
	$result = $bwb_dbh->query($query);
	$row = $result->fetch(PDO::FETCH_ASSOC);
	$page_name_retrieved = $row["page_name"];
	if (!$page_name_retrieved)
	{
		echo "$file_name not found in database.  Click <a href=\"main.php?mode=$mode\" title=\"here\">here</a> to go back and try again. If still not found, contact your system administrator to correct the problem.";
		exit;
	}
	
	
	$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . "plugins` WHERE `name` = '$plugin'";
	$result = $bwb_dbh->query($query);
	$row = $result->fetch(PDO::FETCH_ASSOC);
	$content_html = $row["content_html"];
	
	$today = date("Y-m-d H:i:s");
	$blog_time_stamp = date("n-j-Y g:i:s A");
	
	$content_html = str_replace("mm/dd/yyyy hh:ii:ss PM", $blog_time_stamp, $content_html);
	$content_html = str_replace("Username", $userid, $content_html);
	
	//Add blog to database.
	$query = "INSERT INTO `" . BWB_TABLE_PREFIX . "blogs` SET `page_name`='$page_name', `post`='$content_html', `owner`='$userid', `revised`='$today'";
	$cnt = $bwb_dbh->exec($query);
//	echo $query;
	
	// Get last ID for new post.
	$blog_id = $bwb_dbh->lastInsertId();
	
	//Build post location stting, prepend and append to post, and update.
	$post_locate_string = "<!-- Post locate string for blog ID " . $blog_id . ". Do not remove this line. -->";
	$content_html = $post_locate_string . $content_html . $post_locate_string;
	$query = "UPDATE `" . BWB_TABLE_PREFIX . "blogs` SET `post`='$content_html' WHERE `id`=" . $blog_id;
	$cnt = $bwb_dbh->exec($query);
	
	//Go to edit page.
	$domain = $_SERVER['HTTP_HOST'];
	if (BWB_REQUIRE_SSL == 1)
		$web_path = "https://" . $domain;
	else
		$web_path = "http://" . $domain;
	$php_self = $_SERVER['PHP_SELF'];
	$this_file = substr(strrchr($php_self, "/"), 1);
	$app_dir = substr($php_self, 0, (strlen($php_self)- strlen($this_file)));
	$edit_page = $web_path . $app_dir . "edit_page.php?blog_id=$blog_id&page_name=$page_name&mode=$mode";
	header("Location: $edit_page");
	
?>
