<?php


$mobile_browser = 0;
$container = $_SERVER['HTTP_USER_AGENT'];

$useragents = array (

'Blazer' ,
'Palm' ,
'Handspring' ,
'Nokia' ,
'Kyocera',
'Samsung' ,
'Motorola' ,
'Smartphone',
'Windows CE' ,
'Blackberry' ,
'WAP' ,
'SonyEricsson',
'PlayStation Portable',
'LG',
'MMP',
'Presto',
'iPhone',
'Mobile',
'OPWV',
'Symbian',
'EPOC'

);

foreach ( $useragents as $useragent ) {

   if(strstr($container,$useragent)) {
      $mobile_browser = 1;
   }
 
}

if(strstr($container,'iPad')) {
   $mobile_browser = 0;
}

class __shaun_gethere {
	/*
	 creates the admin control panel for the plugin
	 */
	function create_acp(){
		if(current_user_can('switch_themes')){
			$acp	= $this->args['dir'].'/'.'admin/index.php';
			if(file_exists($acp) AND !$this->fatal){
				include($acp);
				new __shaun_gethere_acp($this->args);
			}
		}
	}
	
	
	/*
	 create the html for admin notice 
	 */
	function notice(){
		echo '<div id="message" class="'.$this->mode.' fade"><p><b>Premium Lead Generation Mobile:</b> '.$this->error.'</p></div>';
	}


	/*
	 creates the admin notice in acp
	 */
	function error_admin($error, $mode='error'){
		if(is_admin() AND $_REQUEST['page'] != $this->args['slug']){
			$this->error	= $error;
			$this->mode		= ($mode != 'error') ? 'updated' : 'error';
			add_action('admin_notices', array($this, 'notice'));
		}
	}
	
	
	/*
	 returns the list of all available themes
	  */
	function get_themelist(){
		$dir		= $this->args['dir'].'/'.'themes'.'/';
		$list		= glob($dir.'*', GLOB_ONLYDIR);
		$theme		= $this->args['options']['theme'];
		$ret		= array();
		
		// return list of themes
		foreach($list as $value){
			$ret[]= str_replace($dir, '', $value);				
		}
		return $ret;
	}
	
	
	/*
	 creates variables for use throughout the plugin
	  */
	function add_vars(){
		define('DS', '/');		
		$this->args		= array(
				'name'		=> 'Premium Lead Generation',
				'slug'		=> '__getthere',
				'ver'		=> '1.0',
				'dir'		=> dirname(__FILE__),
				'dirname'	=> str_replace('/','',str_replace(basename( __FILE__),'',plugin_basename(__FILE__))),
				'options'	=> get_option('__shaun__gt_settings'),
				'themecolor'	=> get_option('plg_alt_stylesheet')
			);

		$this->args		= array_merge($this->args,  array(
				'themes'	=> $this->get_themelist(),

				'url'		=> WP_TEMPLATE_URL.'/'.$this->args['dirname'],
				'optout'	=> get_bloginfo('wpurl').'/?'.$this->args['slug'].'=optout',
				'optoutnret'    => get_bloginfo('wpurl').'/?'.$this->args['slug'].'=optout&return='.$_SERVER['REQUEST_URI'],
				'theme'		=> WP_TEMPLATE_URL.'/'.$this->args['dirname'].'/'.'themes',
				'turl'		=> WP_TEMPLATE_URL.'/'.$this->args['dirname'].'/'.'themes'.'/'.$this->args['options']['theme'],
				'turl2'		=> get_bloginfo('template_url').'/functions/mobile/themes/'.$this->args['themecolor'],
				'murl'		=> get_bloginfo('template_url'),
				'blogname'	=> get_bloginfo('name'),
				'phone'		=> get_option('plg_phone_number'),
				'contactimg'	=> get_option('plg_sidebar_contact_img'),
				'topbartext'	=> get_option('plg_top_bar_text'),
				'formheading'	=> get_option('plg_form_heading'),
				'formadd'	=> get_option('plg_form_add'),
				'email'		=> get_option('plg_email_address'),
				'sidebarshow'	=> get_option('plg_sidebar_contact_img_show'),
				'ccemail'	=> get_option('plg_ccemail_address')

			));
	}
	
	
	/*
	 insert hooks used by the plugin
	  */
	 function add_hooks(){
	 	add_action('init', array($this, 'create_acp'));
	 }
	 
	 
	 /*
	 places hooks to include template
	  */
	 function hook_landing(){
		extract($this->args);
		$theme		= $dir.'/'.'themes/'.$this->args['themecolor'].'/index.php';
		
		if(file_exists($theme)){
			$opt	= $this->args;
			add_action('template_redirect', array($this, 'show_landing'));
		}
	}
	
	
	/*
	 actual include function for template
	  */
	function show_landing($template){
		extract($this->args);
		$theme		= $dir.'/'.'themes/'.$this->args['themecolor'].'/index.php';
		include($theme);
		if($break){
			exit();
		}
	}
	 
	 
	/*
	 magic takes place here :p
	 */
	function start($cond, $args=null, $break=true){
		// disable execution if admin page
		if(is_admin()){
			return;
		}
	
		// extract all options
		if(is_array($args)){
			extract($args);
		}
		if(is_array($this->args)){
			extract($this->args);
		}

		// insert cookie
		if($_GET[$slug] == 'optout'){
		        setcookie($slug, true);
			// create redirect url
			$url_wp	= get_bloginfo('wpurl');
			$url_ret= 'http://'.$_SERVER['HTTP_HOST'].$_GET['return'];
			$url_red= get_bloginfo('wpurl');

			// redirect and die
			header("Location: ".$url_red);
			die();
		}

		// insert cookie
		if($_GET[$slug] == 'mobi'){
	   		setcookie($slug, true);
			// create redirect url
			$url_wp2= $this->args['options']['mobilelink'];

			// redirect and die
			header("Location: ".$url_wp2);
			die();
		}
		
		// look for cookies
		$cookie	= $_COOKIE[$slug];
		if(isset($cookie)){
			return;
		}
		
		// eval the code
		if(eval("return $cond;")){
			$this->args['break'] = $break;
			$this->hook_landing();
		}
	}


	/*
	 default call when class is initialized
	  */
	function __construct(){	
		$this->add_vars();			// creates variables for use in plugin
		$this->add_hooks();			// inserts required hooks
	}
}

// loader tag
function __gethere($cond, $args=null, $break=true){
	$class	= new __shaun_gethere;
	$class->start($cond, $args, $break);
}


__gethere('$mobile_browser > 0', array('mobile_browser' => $mobile_browser));
//__gethere('$cond==1', array('cond' => 1));

?>