<?php
defined( '_JEXEC' ) or die( 'Restricted access' );

class plgSystemPDAbot extends JPlugin
{
	function plgSystemPDAbot(&$subject,$config)
	{
		parent::__construct($subject,$config);
	}
	function CheckAgent()
	{
		$pdas = array(
		'acer',			'alcatel',		'audiovox',		'avantgo',		'blackberry',
		'blazer',		'cdm',			'digital paths','elaine',		'epoc',
		'ericsson',		'handspring',	'iemobile',		'kyocera',		'lg',
		'midp',			'mmp',			'mobile',		'motorola',		'nec',
		'nokia',		'o2',			'openwave',		'opera mini',	'operamini',
		'opwv',			'palm',			'panasonic',	'pda',			'phone',
		'playstation portable','pocket','psp',			'qci',			'sagem',
		'sanyo',		'samsung',		'sec',			'sendo',		'sharp',
		'smartphone',	'sonyericsson',	'symbian',		'telit',		'tsm',
		'up-browser',	'up.browser',	'up.link',		'vodafone',		'wap',
		'windows ce',	'xiino'
		);

		$accept = isset($_SERVER['HTTP_ACCEPT'])?strtolower($_SERVER['HTTP_ACCEPT']):'';
		if(((strpos($accept,'text/vnd.wap.wml')>0)||
			(strpos($accept,'application/vnd.wap.xhtml+xml')>0))||
			isset($_SERVER['HTTP_X_WAP_PROFILE'])||
			isset($_SERVER['HTTP_PROFILE'])||
			isset($_SERVER['X-OperaMini-Features'])||
			isset($_SERVER['UA-pixels']) )
		return true;

		if(isset($_SERVER['HTTP_USER_AGENT']))
		{
			$agent = strtolower($_SERVER['HTTP_USER_AGENT']);
			foreach($pdas as $browser)
				if(false!==strpos($agent,$browser))
					return true;
		}
		return false;
	}
	function onAfterInitialise()
	{
		global $mainframe;
		if($mainframe->isAdmin()) return;
		$plugin=&JPluginHelper::getPlugin('system','pdabot');
		$botParams = new JParameter($plugin->params);
		$botParams->def( 'useragent', 1 );
		$botParams->def( 'subdomain', 1 );
		$botParams->def( 'subdomainname', 'pda' );
		$botParams->def( 'pdatemplate', 'pda' );
		$botParams->def( 'gzip', 0 );
		$botParams->def( 'homepage', '' );

		$GLOBALS['pdabot']['params']=$botParams;
		$GLOBALS['pdabot']['version']='J100';
		$GLOBALS['ispda']=false;

		$subdomain=$botParams->get('subdomainname').'.';
		if( $botParams->get( 'subdomain' ) &&
			substr($_SERVER['HTTP_HOST'],0,strlen($subdomain))==$subdomain )
		{
			$GLOBALS['ispda']=true;
		}

		if( $botParams->get( 'useragent' ) && plgSystemPDAbot::CheckAgent() )
		{	
			$GLOBALS['ispda']=true;
		}
	
		if( $GLOBALS['ispda'] )
		{
			$template=$botParams->get('pdatemplate');
			if($template)
				$mainframe->setTemplate($template);
			$config =& JFactory::getConfig();
			$config->setValue('config.gzip',$botParams->get('gzip'));
			$homepage=$botParams->get('homepage');
			if($homepage && ($_SERVER['REQUEST_URI']=='/' || $_SERVER['REQUEST_URI']=='/index.php') )
			{
				$GLOBALS['pdabot']['home']=1;
				$_SERVER['REQUEST_URI']=$path.'/'.$homepage;
				if(substr($homepage,0,10)=='index.php?')
				{
					$_SERVER['QUERY_STRING']=substr($homepage,10);
					parse_str($_SERVER['QUERY_STRING'],$_REQUEST);
				}
			}
		}
	}
	function onAfterRender()
	{
		global $mainframe;
		if($mainframe->isAdmin()) return;
		if(!$GLOBALS['ispda']) return;
		$plugin=&JPluginHelper::getPlugin('system','pdabot');
		$botParams = new JParameter($plugin->params);
		$botParams->def( 'removeimg', 0 );
		$botParams->def( 'removeiframe', 0 );
		$botParams->def( 'removeobject', 0 );
		$botParams->def( 'removeapplet', 0 );
		$botParams->def( 'removeembed', 0 );
		$botParams->def( 'removescript', 0 );

		$is_remove_img   =$botParams->get( 'removeimg' );
		$is_remove_iframe=$botParams->get( 'removeiframe' );
		$is_remove_object=$botParams->get( 'removeobject' );
		$is_remove_applet=$botParams->get( 'removeapplet' );
		$is_remove_embed =$botParams->get( 'removeembed' );
		$is_remove_script=$botParams->get( 'removescript' );
		
		if(	$is_remove_img   ||
			$is_remove_iframe||
			$is_remove_object||
			$is_remove_applet||
			$is_remove_embed ||
			$is_remove_script )
		{
			$body = JResponse::getBody();
			if( $is_remove_img )
				$body = preg_replace( '|<img\s[^>]+>|is',       '', $body );
			if( $is_remove_iframe ) {
				$body = preg_replace( '|<iframe\s[^>]+ />|is',  '', $body );
				$body = preg_replace( '|<iframe.+</iframe>|is', '', $body );
			}
			if( $is_remove_object ) {
				$body = preg_replace( '|<object\s[^>]+ />|is',  '', $body );
				$body = preg_replace( '|<object\s.+</object>|is','',$body );
			}
			if( $is_remove_embed ) {
				$body = preg_replace( '|<embed\s[^>]+ />|is',   '', $body );
				$body = preg_replace( '|<embed.+</embed>|is',   '', $body );
			}
			if( $is_remove_applet ) {
				$body = preg_replace( '|<applet\s[^>]+ />|is',  '', $body );
				$body = preg_replace( '|<applet\s.+</applet>|is','',$body );
			}
			if( $is_remove_script ) {
				$body = preg_replace( '|<script\s[^>]+ />|is',  '', $body );
				$body = preg_replace( '|<script\s.+</script>|is','',$body );
			}
			JResponse::setBody($body);
		}
	}
}
?>