<?php if(!defined('master_script')) die();

/**
 * Default settings
 *
 * @category   Module (seo-content)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-12-21
 */
		
	global $system_data, $page_info;
	
	if(!isset($module_settings['seo-content']['referer'])) $module_settings['seo-content']['referer'] = 1;
	if(!isset($module_settings['seo-content']['page name'])) $module_settings['seo-content']['page name'] = '';
	if(!isset($module_settings['seo-content']['referers'])) $module_settings['seo-content']['referers'] = '';
	if(!isset($module_settings['seo-content']['text'])) $module_settings['seo-content']['text'] = '';
	
	$module_settings['seo-content']['search engines'] = array(
		'bing' => 'Bing',
		'google' => 'Google',
		'yahoo' => 'Yahoo!'
	);

/**
 * Languages
 *
 * @category   Module (seo-content)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2010-07-28
 */

	$modules['seo-content']['key'] = 'seo-content';
	include_txt($modules['seo-content']['key'], $system_data['folder']['modules'] . $modules['seo-content']['key'] . '/languages/');
	include_txt($modules['seo-content']['key'], $system_data['folder']['settings'] . 'modules/' . $modules['seo-content']['key'] . '/languages/');

/**
 * Parse specified referers
 *
 * @category   Module (seo-content)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-12-21
 */

	$module_settings['seo-content']['referers'] = explode("\n", $module_settings['seo-content']['referers']);
	
	if(!is_array($module_settings['seo-content']['referers']))
		$module_settings['seo-content']['referers'] = array($module_settings['seo-content']['referers']);

/**
 * Functions
 *
 * @category   Module (seo-content)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-12-21
 */

	// Return search engine name
	if(!function_exists('get_se_hostname')) {
		function get_se_hostname() {
			global $module_settings;
			
			if(isset($_SESSION['seo_content_referer'])) $referer = $_SESSION['seo_content_referer'];
			if(isset($_SERVER['HTTP_REFERER'])) $referer = $_SERVER['HTTP_REFERER'];
			
			$search_engine = 'unknown';
			
			if(isset($referer)) {
				// Save referer string to cookie
				if(isset($_SERVER['HTTP_REFERER'])) $_SESSION['seo_content_referer'] = $_SERVER['HTTP_REFERER'];
				
				// Identify referer
				if($search_engine == 'unknown' && strpos(strtolower($referer), 'bing.')) $search_engine = 'bing';
				if($search_engine == 'unknown' && strpos(strtolower($referer), 'google.')) $search_engine = 'google';
				if($search_engine == 'unknown' && strpos(strtolower($referer), 'yahoo.')) $search_engine = 'yahoo';
				
				if($search_engine == 'unknown') foreach($module_settings['seo-content']['referers'] as $referer) {
					$referer = trim($referer);
					
					if(!empty($referer)) {
						$referer_parts = explode(',', $referer);
						$referer_url = isset($referer_parts[0]) ? trim($referer_parts[0]) : trim($referer_parts);
						$referer_name = isset($referer_parts[1]) ? trim($referer_parts[1]) : $referer_url;
						
						if(strpos(strtolower($referer), $referer_url)) {
							$module_settings['seo-content']['search engines'][$referer_url] = $referer_name;
							$search_engine = $referer_url;
						}
					}
				}
			}
			
			return $search_engine;
		}
	}

	// Return keywords
	if(!function_exists('get_referal_keywords')) {
		function get_referal_keywords() {
			if(isset($_SESSION['seo_content_referer'])) $referer = $_SESSION['seo_content_referer'];
			if(isset($_SERVER['HTTP_REFERER'])) $referer = $_SERVER['HTTP_REFERER'];

			$keywords = '';

			if(isset($referer)) {
				if(in_array(get_se_hostname(), array('bing', 'google'))) {
				    $keywords = substr($referer, strpos($referer, 'q='));
					$keywords = substr($keywords, 2);
					if(strpos($keywords, '&')) $keywords = substr($keywords, 0, strpos($keywords, '&'));
					$keywords = urldecode($keywords);
				}

				if(get_se_hostname() == 'yahoo') {
				    $keywords = substr($referer, strpos($referer, 'p='));
					$keywords = substr($keywords, 2);
					if(strpos($keywords, '&')) $keywords = substr($keywords, 0, strpos($keywords, '&'));
					$keywords = urldecode($keywords);
				}
			}
			
			return $keywords;
		}
	}

/**
 * Include contents
 *
 * @category   Module (seo-content)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-12-21
 */
	
	if(!isset($_SESSION['seo_content_referer']) && (get_se_hostname() != 'unknown' || $module_settings['seo-content']['referer'] == 0 || isset($_GET['backend']))) {
		// Parse dynamic tags in static content
		if($module_settings['seo-content']['referer'] == 1 && !isset($_GET['backend'])) {
			$module_settings['seo-content']['text'] = str_replace('%search_engine', $module_settings['seo-content']['search engines'][get_se_hostname()], $module_settings['seo-content']['text']);
			$module_settings['seo-content']['text'] = str_replace('%search_keywords', get_referal_keywords(), $module_settings['seo-content']['text']);
		}
		
		// Simulate active page data
		$this_page = $page_info;
		
		$page_info = get_page_info(trim($module_settings['seo-content']['page name'], '/'));
		$page_info->index = $system_data['folder']['pages'] . $page_info->alias . '/' . $system_data['file']['index page'];
		$page_info->href = $system_data['frontend']['href attribute'] . $page_info->alias;
		
		// Include module frontend template
		include_template('seo-content');
		
		// Restore real active page data
		$page_info = $this_page;
	} else {
		$module_settings['seo-content']['text'] = '';
		$module_settings['seo-content']['page name'] = '';
	}

?>