<?php
/*
	Author: WPG2 Team
	Updated: 26/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 Series of Checks to Ensure WPG2 will function within the Wordpress/Gallery2 Environment
*
* @param NULL
* @return string $html The album tree HTML
*/
function wpg2_validate($wpg2_outputtype) {

	global $wp_db_version;

	// Initialisation
		$validate_err=0;
		$validate_warning=0;
		//Get Current Gallery2 Plug-in Options
		$wpg2_option = get_option('wpg2_options');
		$wpg2_g2path = get_option('wpg2_g2paths');

	// Do We Have Paths?
	if (empty($wpg2_g2path['g2_filepath'])) {
		$ret = wpg2_simple_find_paths();

		if (!$ret) {
			// Clean up Permalink Structure
			wpg2_trimpermalinkrules();
			// Verify WPG2 Internal g2_embeduri setting
			wpg2_template_page();
			$wpg2_g2path = get_option('wpg2_g2paths');
		}
	}

	// Test Wordpress Version
		$environment_html = '<tr><td>';
		$environment_html .=  __('Wordpress Compatible?' , 'wpg2') .' </td><td>';
		if ($wp_db_version >=$wpg2_option['g2_wpversion_low']) {
			$environment_html .= '<font color="green">' . __('Success', 'wpg2') . '</font>';
		} else {
			define("WPVERSIONERR", "True");
			$environment_html .= '<font color="red">' . __('Failed', 'wpg2') . '</font>';
			$validate_err=1;
		}

	// Test Gallery2 URL
		$environment_html .= '<tr><td>';
		$environment_html .=  __('URI to Gallery2 Ok?' , 'wpg2') .' </td><td>';
		if (!$validate_err) {
			$ret = wpg2_validateuri($wpg2_g2path['g2_url'].'embed.php');
			if (!$ret) {
				$environment_html .= '<font color="green">' . __('Success', 'wpg2') . '</font>';
			} else {
				define("G2EMBEDERR", "True");
				$validate_warning=1;
				$reterr = $ret;
				$environment_html .= '<font color="orange">' . __('Warning', 'wpg2') . '</font>';
			}
		} else $environment_html .= '<font color="red">' . __('Skipped Due to Validation Error', 'wpg2') . '<font>';
		$environment_html .= "</td></tr>";

	// Gallery Ok?
		$environment_html .= '<tr><td>';
		$environment_html .= __('Gallery2 can be located in G2 File Path?', 'wpg2') . '</td><td>';
		if (!$validate_err) {
			if (file_exists( $wpg2_g2path['g2_filepath'].'embed.php' ) )
				$environment_html .= '<font color="green">' . __('Success', 'wpg2');
			else {
				$environment_html .= '<font color="red">' . __('Failed', 'wpg2');
				define("G2PATHERR", "True");
				$validate_err=1;
			}
		} else $environment_html .= '<font color="red">' . __('Skipped Due to Validation Error', 'wpg2') . '<font>';
		$environment_html .= "</td></tr>";

	// Test Initialise G2
		$environment_html .= '<tr><td>';
		$environment_html .= __('Gallery2 can be initialised?', 'wpg2') . '</td><td>';
		if (!$validate_err) {
			if (!defined('G2INIT')) {
				$ret = g2_init();
				if (!$ret)
					$environment_html .= '<font color="green">' . __('Success', 'wpg2') . '</font>';
				else {
					define("G2INITERR", "True");
					$environment_html .= '<font color="red">' . __('Failed', 'wpg2') . '-><font>';
					$validate_err=1;
				}
			} else
					$environment_html .= '<font color="green">' . __('Success', 'wpg2') . '</font>';
		} else $environment_html .= '<font color="red">' . __('Skipped Due to Validation Error', 'wpg2') . '<font>';
		$environment_html .= "</td></tr>";

	// Test G2 Version
		$environment_html .= '<tr><td>';
		$environment_html .= __('Gallery2 Version Compatible?' , 'wpg2') . '</td><td>';
		if (!$validate_err) {
			list ($ret, $core) = GalleryCoreApi::loadPlugin('module', 'core', true);
	        $g2version = $core->getInstalledVersions();
			$g2version = substr($g2version['gallery'],0,3);
			if ($wpg2_option['g2_version_low'] <= $g2version)
				$environment_html .= '<font color="green">' . __('Success', 'wpg2') . '</font>';
			else {
				$environment_html .= '<font color="red">' . __('Failed', 'wpg2') . '</font>';
				define("G2VERERR", "True");
				$validate_err=1;
			}
		} else $environment_html .= '<font color="red">' . __('Skipped Due to Validation Error', 'wpg2') . '<font>';
		$environment_html .= "</td></tr>";


	// ImageBlock Active?
		$environment_html .= '<tr><td>';
		$environment_html .= __('Gallery2 Module: ImageBlock Active?' , 'wpg2') . '</td><td>';
		if (!$validate_err) {
			$g2_image_block_active = 0;
			$g2_moduleid = 'imageblock';
			list ($ret, $g2_modulestatus ) = GalleryCoreApi::fetchPluginStatus('module');
			if (!$ret) {
				if (isset($g2_modulestatus[$g2_moduleid]) && !empty($g2_modulestatus[$g2_moduleid]['active']) && $g2_modulestatus[$g2_moduleid]['active'])
					$environment_html .= '<font color="green">' . __('Success', 'wpg2') . '</font>';
				else {
					$environment_html .= '<font color="red">' . __('Failed', 'wpg2') . '</font>';
					define("G2IMAGEBLOCKERR", "True");
					$validate_err=1;
				}
			}
		} else $environment_html .= '<font color="red">' . __('Skipped Due to Validation Error', 'wpg2') . '<font>';
		$environment_html .= "</td></tr>";

	// ImageFrame Active?
		$environment_html .= '<tr><td>';
		$environment_html .=  __('Gallery2 Module: ImageFrame Active?' , 'wpg2') . '</td><td>';
		if (!$validate_err) {
			$g2_image_block_active = 0;
			$g2_moduleid = 'imageframe';
			if (!$ret) {
				if (isset($g2_modulestatus[$g2_moduleid]) && !empty($g2_modulestatus[$g2_moduleid]['active']) && $g2_modulestatus[$g2_moduleid]['active'])
					$environment_html .= '<font color="green">' . __('Success', 'wpg2') . '</font>';
				else {
					$environment_html .= '<font color="red">' . __('Failed', 'wpg2') . '</font>';
					define("G2IMAGEFRAMEERR", "True");
					$validate_err=1;
				}
			}
		} else $environment_html .= '<font color="red">' . __('Skipped Due to Validation Error', 'wpg2') . '<font>';
		$environment_html .= "</td></tr>";

	// WPG2 Page Output Type?
		$environment_html .= '<tr><td>';
		$environment_html .= __('WPG2 Output Page Type?', 'wpg2') . '</td>';
		if ( $wpg2_option['g2_embedpagetype'] == 'wordpress' ) {
			$environment_html .= '<td><font color="green">' . __('Wordpress', 'wpg2');
		} else {
			$environment_html .= '<td><font color="green">' . __('Gallery2', 'wpg2');
		}
		$environment_html .= "</td></tr>";

	// WPG2 / Gallery Rewrites Active?
		$environment_html .= '<tr><td>';
		$environment_html .= __('WPG2 Gallery2 Rewrites Status?', 'wpg2') . '</td>';
		if ($wpg2_option['g2_rewriteactive'] == 'active') {
			$environment_html .= '<td><font color="green">' . __('On', 'wpg2');
		} else {
			if ($wpg2_option['g2_rewriteactive'] == 'safe')
				$environment_html .= '<td><font color="green">' . __('Off [Safe]', 'wpg2');
			else
				$environment_html .= '<td><font color="green">' . __('Off', 'wpg2');
		}
		$environment_html .= "</td></tr>";


	// Custom Header?
		$environment_html .= '<tr><td>';
		$environment_html .= __('Embedded Page is using what Header type?', 'wpg2') . '</td>';
		if ( file_exists( TEMPLATEPATH . '/wpg2header.php') ) {
			$environment_html .= '<td><font color="green">' . __('WPG2HEADER', 'wpg2');
			$wpg2_option['g2_externalheader'] = "Yes";
		} else {
			define('WPSIMPLEHEADER',"True");
			$environment_html .= '<td><font color="green">' . __('Simple', 'wpg2');
			$wpg2_option['g2_externalheader'] = "No";
		}
		$environment_html .= "</td></tr>";


	// Custom Footer?
		$environment_html .= '<tr><td>';
		$environment_html .= __('Embedded Page is using what Footer type?', 'wpg2') . '</td>';
		if ( file_exists( TEMPLATEPATH . '/wpg2footer.php') ) {
			$environment_html .= '<td><font color="green">' . __('WPG2FOOTER', 'wpg2');
			$wpg2_option['g2_externalfooter'] = "Yes";
		} else {
			$environment_html .= '<td><font color="green">' . __('Simple', 'wpg2');
			$wpg2_option['g2_externalfooter'] = "No";
		}
		$environment_html .= "</td></tr>";

	// External WPG2 CSS
		$environment_html .= '<tr><td>';
		$environment_html .= __('Using Which WPG2 Stylesheet?', 'wpg2') . '</td>';
		if ( file_exists( TEMPLATEPATH . '/wpg2.css') ) {
			$environment_html .= '<td><font color="green">' . __('External', 'wpg2');
			$wpg2_option['g2_externalcss'] = "Yes";
		} else {
			$environment_html .= '<td><font color="green">' . __('Internal', 'wpg2');
			$wpg2_option['g2_externalcss'] = "";
		}
		$environment_html .= "</td></tr>";
		$environment_html .= "</table><br/>";

		if ($validate_err)
			$wpg2_option['g2_validated'] = "No";
		else {
			if (!$wpg2_option['g2_rewriteactive'] == 'active') {
				$g2_moduleid = 'rewrite';
				list ($ret, $g2_modulestatus ) = GalleryCoreApi::fetchPluginStatus('module');
				if (!$ret && isset($g2_modulestatus[$g2_moduleid]) && !empty($g2_modulestatus[$g2_moduleid]['active']) && $g2_modulestatus[$g2_moduleid]['active']) {
				// Deactivate Rewrite Module as Wordpress Permalink Array will be wrong.
					list ($ret, $ignored) = GalleryCoreApi::deactivatePlugin('module', 'rewrite');
					if (!$ret)
						define("G2REWRITEDEACTIVATED", "True");
				}
			}
			$wpg2_option['g2_validated'] = "Yes";
			// Close Gallery Connection
			GalleryEmbed::done();
		}

	// Update the Options
		update_option('wpg2_options', $wpg2_option);


	//Output Status
	if ($wpg2_outputtype != "silent") {

		echo '<div id="wpg2_status" class="wrap">';

		echo '<p class="submit"><a href="http://codex.gallery2.org/Integration:WPG2_Options_-_Validation">'. __('Help', 'wpg2').'</a></p>';

		if ($validate_err){
			echo '<h2>' . __('WPG2 Status: ', 'wpg2') . '<font color="red">' . __('Failed', 'wpg2') . '</font></h2>';
			echo "<table width='500' bordercolor='#242424' border=1 cellpadding=1 cellspacing=0 >";
			echo $environment_html;
		} else {
			echo '<h2>' . __('WPG2 Validation Check: ', 'wpg2') . '<font color="green">' . __('Valid', 'wpg2') . '</font><font size="3">'.' (<a href="'.$wpg2_g2path[g2_embeduri].'">'.'View Page &raquo;</a>)</font></h2>';
			?>
				<table width='500' bordercolor='#242424' border=1 cellpadding=1 cellspacing=0 >
				<?php echo $environment_html; ?>
			<div>
			<?php
		}


		if (defined('WPVERSIONERR')) {
			echo '<div id = "wpversion_form">';
			echo '<h2>' . __('Error: Wordpress Build is NOT Compatible?', 'wpg2').'</h2>'. __(' WPG2 ','wpg2').$wpg2_option['g2_wpg2version'];
			_e(' requires a minimum Wordpress build of ', 'wpg2');
			echo $wpg2_option['g2_wpversion_low'];
			_e (' and has detected your Wordpress build ', 'wpg2');
			echo $wp_db_version;
			_e (' does not meet the minimum build requirement.', 'wpg2');
			echo '<br /><br /></div>';
		}

		if (defined('G2EMBEDERR')) {
			echo '<div id = "g2embed_form">';
			echo '<h3>'. __('Warning: Possible invalid URL to Gallery2', 'wpg2').'</h3>'. __('Possible Validation Problem of Gallery2 URL ', 'wpg2').$reterr.'<br /><br />';
			echo '</div>';
		}

		if (defined('G2PATHERR')) {
			echo '<div id = "g2path_form">';
			echo '<h2>'. __('Error: embed.php cannot be located in the specified File Path', 'wpg2').'</h2>';
			_e('WPG2 plugin cannot locate the Gallery2 file ', 'wpg2');
			echo ($wpg2_g2path['g2_filepath']). __('embed.php', 'wpg2').'<br /><br />';
			echo '</div>';
		}


		if (defined('G2INITERR')) {
			echo '<div id = "g2init_form">';
			echo '<h2>'. __('Error: Gallery2 cannot be initialised', 'wpg2').'</h2>'. __('Check to ensure Gallery2 is correctly working and revalidate.', 'wpg2').'<br />';
			echo $ret.'<br /><br />';
			echo '</div>';
		}

		if (defined('G2VERERR')) {
			echo '<div id = "g2version_form">';
			echo '<h2>'. __('Error: Gallery2 version is incompatible','wpg2').'</h2>'. __('WPG2 plugin requires at least Gallery2 version ', 'wpg2');
			echo $wpg2_option['g2_version_low']. __(', Gallery2 reports you are running version ','wpg2');
			echo $g2version.'<br /><br />';
			echo '</div>';
		}

		if (defined('G2IMAGEBLOCKERR')) {
			echo '<div id = "g2imageblock_form">';
			echo '<h2>'. __('Error: Gallery2 Imageblock plugin is not activated','wpg2').'</h2>'. __('Ensure the Gallery2 ImageBlock is installed / activated via the Gallery2 Site Admin -> Plugin Panel.', 'wpg2').'<br /><br />';
			echo $ret;
			echo '</div>';
		}

		if (defined('G2IMAGEFRAMEERR')) {
			echo '<div id = "g2imageframe_form">';
			echo '<h2>'. __('Error: Gallery2 Imageframe plugin is not activated','wpg2').'</h2>'. __('Ensure the Gallery2 ImageFrame is installed / activated via the Gallery2 Site Admin -> Plugin Panel.', 'wpg2').'<br /><br />';
			echo $ret;
			echo '</div>';
		}

		if (defined('G2REWRITEDEACTIVATED')) {
			echo '<div id = "simpleheader_form">';
			echo '<h3>'. __('Warning: Gallery2 Rewrite Plugin was deactived','wpg2').'</h3>'. __('WPG2 has automatically deactivated the Gallery2 Rewrite Plugin, to reactive, enable Gallery2 Rewrites in the WPG2 Gallery2 Rewrites Tab.','wpg2').'<br /><br />';
			echo '</div>';
		}

		if (defined('WPSIMPLEHEADER')) {
			echo '<div id = "simpleheader_form">';
			echo '<h3>'. __('Warning: Using the Simple Header','wpg2').'</h3>'. __('WPG2 will generate your Wordpress header using the CSS Elements specified in the embedded page options. For some Wordpress themes this will incorrectly distort the output from Gallery2 and instead you may need to create your own WPG2 Header.','wpg2').'<br /><br />';
			echo '</div>';
		}

		echo '</div></div>';

	}

}

?>