<?php
/*
	Author: WordPress Gallery Team
	Updated: 13/09/2007

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
*/

/*
* Performs a Template Redirect to Output Gallery2 Content
*
* @param External Page Redirect
* @return None
*/
function wpg2_template() {

	global $g2data;

	// Get WPG2 Option Settings
	$wpg2_option = get_option('wpg2_options');

//	Keep Support for Internal (Non Permalink Pages) - Need Better WP Filters However to handle this..
	if (is_page($wpg2_option['g2_embedpageid']) ) {

		// Redirect Template VIA Wordpress Page
		// Get Gallery2 Paths Settings
		$wpg2_g2path = get_option('wpg2_g2paths');

		// Remove the Magic Quotes That Wordpress Adds, it just confuses Gallery2
		$_POST = stripslashes_deep($_POST);
		$_GET = stripslashes_deep($_GET);
		$_COOKIE = stripslashes_deep($_COOKIE);
		$_SERVER = stripslashes_deep($_SERVER);

		//Get Gallery Plug-in Settings
		$wpg2_option = get_option('wpg2_options');

		// Log into G2
		define("G2PARTINIT", "True");
		define("G2NOHEADERPAGE", "True");

		$ret = g2_login();

		if ($ret) {
			echo '<h2>' . __('Fatal G2 error', 'wpg2') . '</h2> ' . __("Here's the error from G2: ", 'wpg2') . $ret->getAsHtml();
			exit;
		}

		// Pass through WP URL / Blog Name
		$site_url = get_option('home');
		$site_name = get_option('blogname');
		GalleryUtilities::putRequestVariable('wpg2_returnurl', $site_url);
		GalleryUtilities::putRequestVariable('wpg2_returnname', $site_name);


		// Switch on Lightbox
		if ($wpg2_option['wpg2_enableg2lightbox']) {
			GalleryUtilities::putRequestVariable('wpg2_lightbox', "active");
			GalleryUtilities::putRequestVariable('wpg2_lightboxsize', $wpg2_option['g2_lightboximgsize']);
		}

		// Grab Gallery2 Output
		$g2data = GalleryEmbed::handleRequest();

		if ($g2data['isDone']) {
			exit; // G2 has already sent output (redirect or binary data)
		}

		// Theme Switcher Hook
		if(!empty($_COOKIE['wp_ts2_'.COOKIEHASH])) {

			if ( file_exists( TEMPLATEPATH . '/wpg2header.php') ) {
					$wpg2_option['g2_externalheader'] = "Yes";
				} else {
					$wpg2_option['g2_externalheader'] = "No";
				}

			if ( file_exists( TEMPLATEPATH . '/wpg2footer.php') ) {
				$wpg2_option['g2_externalfooter'] = "Yes";
			} else {
				$wpg2_option['g2_externalfooter'] = "No";
			}
		}

		// Should we Disable the Header output and instead allow the wpg2header to control the Output?
		if ( $wpg2_option['g2_externalheader']!="Yes" ) {
			get_header();
			//Include any plug-in header content set in the plugin options
			echo stripslashes($wpg2_option['g2_header']);
		} else
			include (TEMPLATEPATH.'/wpg2header.php');

		echo $g2data['bodyHtml'];	 //Display the gallery content

		//Close Gallery Connection
		GalleryEmbed::done();

		// Should we Disable the Header output and instead allow the wpg2footer to control the Output?
		if ($wpg2_option['g2_externalfooter']!="Yes" ) {
			//Include plug-in footer content
			echo stripslashes($wpg2_option['g2_footer']);
			//Include WP footer
			get_footer();
		} else
			include (TEMPLATEPATH.'/wpg2footer.php');
		exit;
	}
}

?>