Your IP : 216.73.216.170


Current Path : /var/www/iplanru/data/www/i-plan.ru/plugins/system/mobiletemplate/
Upload File :
Current File : /var/www/iplanru/data/www/i-plan.ru/plugins/system/mobiletemplate/mobiletemplate.php

<?php
/**
 * @copyright	Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */


// no direct access
defined('_JEXEC') or die('Restricted access');

jimport( 'joomla.plugin.plugin' );

class  plgSystemMobileTemplate extends JPlugin
{
	public function onAfterInitialise()
	{
		$app = JFactory::getApplication();

		if($app->isAdmin()){
			return;
		}
		
		
		$mobile_style=$this->params->get('mobile_style','iPhone|iPod|BlackBerry|Pre|Palm|Googlebot-Mobile|mobi|Safari Mobile|Windows Mobile|Android|Opera Mini|mobile');
		//$mobile_style = 'iPhone|iPod|BlackBerry|Pre|Palm|Googlebot-Mobile|mobi|Safari Mobile|Windows Mobile|Android|Opera Mini|mobile';
        $mobile_array = explode('|',$mobile_style);
        $is =false;
        $filter = $this->params->get('filter',array());
        $prip=$this->params->get('mob_ip_en','0');
        $prus=$this->params->get('mob_us_en','0');
        
        
        if ($prus=="1")
        {
			foreach ($mobile_array as $mobile) {
				if (preg_match("/$mobile/i",$_SERVER['HTTP_USER_AGENT'])) {
					$is =true;
					break;
				}
			}			
			foreach($filter as $f_word){
				if (preg_match("/$f_word/i",$_SERVER['HTTP_USER_AGENT'])) {
					$is =false;
				}
			}
		}
		
		
		if ($prip=="1")
		{
			$iplist = explode("\r\n",$this->params->get('mob_ip',''));
			if (count($iplist)<>0)
			{
				foreach($iplist as $ip)
				{
					if(trim($ip)==$_SERVER['REMOTE_ADDR'])
					{
						$is=true;
						break;
					}
				}
			}
		}
		
		
		
		
		if ($is)
		{
		
			$template_name = $this->params->get('template_name','mobiletemplate');

			$db = JFactory::getDbo();
			$query = $db->getQuery(true);
			$query->select($db->qn('template'));
			$query->select($db->qn('params'));
			$query->from($db->qn('#__template_styles'));
			$query->where($db->qn('client_id'). ' = 0');
			$query->where($db->qn('template'). ' = "'.$template_name.'"');
			$query->order($db->qn('id'));
			$db->setQuery( $query );
			$row = $db->loadObject();
			if(!$row || empty($row->template)){
				return;
			}

			if(is_dir(JPATH_THEMES."/".$row->template)){
				$app->setTemplate($row->template, (new JRegistry($row->params)));
			}
		}
	}
	
	
	
	public function onContentPrepare($context, &$article, &$params, $page = 0){
		$app = JFactory::getApplication();
		if ( $app->isAdmin()) {
			return;
		}
		
		//find the {IfMobile} and {EndIfMobile} pair and see if there is work to do
		$matches = array();
		preg_match_all('/.*?{IfMobile.*?}(?s).*?(?-s){EndIfMobile}.*?/im', $article->text, $matches, PREG_OFFSET_CAPTURE);
		if(!empty($matches) && !empty($matches[0])){
			//get the mobile parameters
			$isMobile = $app->getUserState('cmobile.ismobile', false);
			$isDevice = $app->getUserState('cmobile.isdevice', false);
			$mobileDevice = new JRegistry($app->getUserState('cmobile.device', '{}'));

			$replacePositionShift = 0;
			foreach($matches[0] as $i => $match){
				$originalText = $match[0];

				$replacePosition = $match[1];
				$replaceLength = strlen($originalText);
				
				$mobileParts = $nonMobileParts = $conditions = array();
				preg_match('/{IfMobile(.*?)}(.*?)(?:{Else.*?Mobile(?:.*?)}|{EndIfMobile})/is', $originalText, $mobileParts, PREG_OFFSET_CAPTURE);
				//TODO: ElseIf statemenets
				preg_match('/{ElseMobile}(.*?){EndIfMobile}/is', $originalText, $nonMobileParts, PREG_OFFSET_CAPTURE);
				
				$statementPos = $mobileParts[0][1];
				$statementLength = (strpos($originalText, '{EndIfMobile}') + strlen('{EndIfMobile}')) - $statementPos;
					
				$newText = '';
				$done = false;
				if($isMobile || $isDevice){
					//its a mobile device, get the mobile part of the content
					$content = $mobileParts[2][0];
					$contentPosition = $mobileParts[2][1];
					$replace = false;
					
					//check if there are conditions, and see if they are met before replacing
					if(isset($mobileParts[1][0]) && trim($mobileParts[1][0]) != ''){
						//mobile condition
						$conditions = explode(',', trim($mobileParts[1][0]));
						
						if(trim($conditions[0]) === 'isdevice' && $isDevice){
							$replace = true;
						}
						//first part is the device
						elseif($isMobile && $mobileDevice->get('name') === trim($conditions[0])){
							//conditions are met, replace the text
							$replace = true;
						}
					}elseif($isMobile){
						//no condition, just mobile enabled
						$replace = true;
					}
					if($replace){
						//replace the text
						$newText = $content;
						$done = true;
					}
					
				}
				if((!$isMobile && !$isDevice) || !$done){
					//not a mobile device, or conditions not met. Get the non mobile part of the content
					if(!empty($nonMobileParts)){
						$content = $nonMobileParts[1][0];
						$contentPosition = $nonMobileParts[1][1];
						
						$newText = $content;
					}
				}
				
				//replace the newText in the original content
				$originalText = substr_replace($originalText, $newText, $statementPos, $statementLength);
				
				$article->text = substr_replace($article->text, $originalText, ($replacePosition - $replacePositionShift), $replaceLength);
				
				//keep track of the changing positions after replacing content
				$replacePositionShift += ($replaceLength - strlen($originalText));
			}

		}
	}
}