<?php

// ---------------------------------------------------------------------------
//
// PIVOT - LICENSE:
//
// This file is part of Pivot. Pivot and all its parts are licensed under 
// the GPL version 2. see: http://www.pivotlog.net/help/help_about_gpl.php
// for more information.
//
// ---------------------------------------------------------------------------


function fill_comment_form($name, $email, $url, $cookie, $comm) {
	
	$output = "<script language=\"JavaScript\" type=\"text/javascript\">\n";
	if ($name!="") {
		$output .=  "document.getElementById('form').piv_name.value='".($name)."';\n";
	}
	if ($email!="") {
		$output .=  "document.getElementById('form').piv_email.value='".($email)."';\n";
	}
	if ($url!="") {
		$output .=  "document.getElementById('form').value='".($url)."';\n";
	}
	if ($comm!="") {
		$output .=  "document.getElementById('form').piv_comment.value='".($comm)."';\n";
	}

	if ($cookie=="yes") {
		$output .=  "document.getElementById('form').piv_rememberinfo[0].checked=true;\n";
	} else {
		$output .=  "document.getElementById('form').piv_rememberinfo[1].checked=true;\n";
	}

	$output .=  "</script>\n";
	return $output;

}		

define("__SILENT__", TRUE);
include_once "includes/getref.inc.php";
include_once "pv_core.php";

$direct_output = TRUE;

// some global initialisation stuff
if (!isset($_GET)) { 
	$Pivot_Vars = array_merge($HTTP_GET_VARS , $HTTP_POST_VARS, $HTTP_SERVER_VARS);
} else {
	$Pivot_Vars = array_merge($_GET , $_POST, $_SERVER);
}
if (!isset($_COOKIE)) { $_COOKIE = $HTTP_COOKIE_VARS; }


if (isset($Pivot_Vars['uid'])) {	
	$Pivot_Vars['uid'] = str_replace("standard-", "", $Pivot_Vars['uid']);
	$Pivot_Vars['id'] = $Pivot_Vars['uid'];
}

$override_weblog="";

// check if we need to override the template.
if (isset($Pivot_Vars['t'])) {
	// explicitly defined template is most important.	
	$override_template = $Pivot_Vars['t'];
} else if (isset($Pivot_Vars['w'])) {	
	// then, explicitly defined weblog
	list($override_weblog) = (explode("#", $Pivot_Vars['w']));
} else {
	// Else, we need to check if we can determine which weblog this entry belongs 
	// to. it might belong to multiple weblogs, and here we check to see it the 
	// referer can be matched to a weblog's homepage. If so, we use _that_ 
	// template instead of the one that would be chosen otherwise.
	$referer = str_replace("http://".$Pivot_Vars['HTTP_HOST'], "", $Pivot_Vars['HTTP_REFERER']);
	if ($referer!="") { 
		foreach($Weblogs as $weblogkey => $weblog) {
			$filename = fixpath($Paths['pivot_url'] . $weblog['front_path'] . $weblog['front_filename']);
			if ( ($referer == $filename) || ($referer == str_replace("/index.php", "/", $filename)) || 
					($referer == str_replace("/index.php", "/", $filename) ) ) {
				$override_weblog = $weblogkey;
			}
		}
	}
}



// load an entry
if (isset($Pivot_Vars['id'])) {
	
	$output = parse_entry($Pivot_Vars['id'], $override_weblog,  TRUE);

	if (count($_COOKIE)>0) {
		$cookie = @fill_comment_form($_COOKIE['piv_name'], $_COOKIE['piv_email'], $_COOKIE['piv_url'], $_COOKIE['piv_rememberinfo'], $_COOKIE['piv_comment']);
		$output = str_replace("</body>", $cookie."</body>", $output);
	}

	echo $output;

} else if (isset($Pivot_Vars['f_title'])) {

	// This shows the entry in preview mode.

	$db = new db();

	$entry = get_entry_from_post();
	
	$entry['status'] = 'publish';
	$entry = $db->set_entry($entry);
	echo parse_entry($entry['code'], "",  TRUE);

} else {

	echo "No id..";

}



//BBClone code..
if (!(defined('_BBC_PAGE_NAME'))) { 
	$title = "Pivot Entry (".format_date($db->entry['date'], "%month% - %year%").")";	
	define("_BBC_PAGE_NAME", $title  );
	define("_BBCLONE_DIR", "../bbclone/");
	define("COUNTER", _BBCLONE_DIR."mark_page.php");
	if (is_readable(COUNTER)) { include_once(COUNTER) ; }
}
// end BBclone code..

?>