Your IP : 216.73.216.247


Current Path : /home/nicholsonsmith/public_html/plugins/system/mojoomdisclaimer/
Upload File :
Current File : /home/nicholsonsmith/public_html/plugins/system/mojoomdisclaimer/mojoomdisclaimer.php

<?php
/**
 * MOJoomDisclaimer Extension for Joomla By MO'WebCreation
 * 
 * 
 * @copyright	Copyright (C) 2014 MO'WebCreation. Mickaël Gerard André Outhier.  All rights reserved.
 * @license		GNU General Public License version 2. Please check the "read me" file provided in the package
 * 
 * @Website 	http://www.mo-web-creation.com/
 * @Support 	https://github.com/MOWebCreation
 *
 */

// No direct access
defined('_JEXEC') or die;

jimport('joomla.plugin.plugin');

class plgSystemMojoomdisclaimer extends JPlugin {
	
	private $doc = FALSE;
	private $session = FALSE;
	private $is_site = FALSE;
	private $moplg_params = FALSE;
	private $jmenu = FALSE;
	private $invok = FALSE;
	
	private function isAssigned() {
		
		$assignment = (string) JArrayHelper::getValue($this->moplg_params, 'assignment');
		
		if(!preg_match('#SeG#', $assignment)) {
			//by default all items links are assigned
			return TRUE;
		} else {
			
			$assignment_segs = explode('SeG', $assignment);
			$assignment_select = (int) $assignment_segs[0];
			$assignment_items = explode(',', $assignment_segs[1]);
			
			if($assignment_select == 0) { return TRUE; } //all items are assigned 
			elseif($assignment_select == -2) {return FALSE; } //no item selected
			elseif($assignment_select == 1) {return in_array($this->jmenu->getActive()->id, $assignment_items); }
			elseif($assignment_select == -1) {return !in_array($this->jmenu->getActive()->id, $assignment_items); }
			else { return TRUE; }
		}
	}
	
	private function varsCombine($url = '', $vars = '') {
		//adding url get vars to an existing url
		if(preg_match('#\?#', $url)) { return $url . '&' . $vars; }
		else { return $url . '?' . $vars; }
	}
	
	public function __construct(&$subject, $config = array()) {
        parent::__construct($subject, $config);
		
		$this->doc = JFactory::getDocument();
		$this->session = JFactory::getSession();
		$this->is_site = JFactory::getApplication()->getName() == 'site';
		$this->moplg_params = json_decode(JPluginHelper::getPlugin('system','mojoomdisclaimer')->params, TRUE);
		$this->jmenu = JMenu::getInstance('site');
	}
	
	public function onBeforeCompileHead() {
		
		$current_token = (int) $this->session->get('disclaimToken');
		
		if(JRequest::getInt('disclaimKey') > 0) { $request_key = (string) JRequest::getVar('disclaimKey');}
		else { $request_key = 'null'; }

		if($current_token != 1 AND $this->is_site == TRUE AND $this->isAssigned() == TRUE) {
			
			/*** All requested conditions are present for invoke the plugin functions ***/
						
			if(JRequest::getInt('disclaimToken') == 1 AND $request_key == $this->session->get('disclaimTokenKey')) { //VALIDED
				$this->session->set('disclaimToken', 1);	
				$this->invok = FALSE;	
				
			} else {//ASKING
				 $disclaim_key = rand(1,5).time();
				 $this->session->set('disclaimTokenKey', $disclaim_key);
				 			
				 //Set the forward url called by clicking on the "enter link"
				 $valided_redirect = (int) JArrayHelper::getValue($this->moplg_params, 'validedredirect');
				 
				 if($valided_redirect == 0) { //same page
				 	
					$disclaim_referer = (string) $this->session->get('disclaimReferer');
					$this->session->set('disclaimReferer', NULL);
					
				 	if($this->moplg_params['disclaimtype'] == 1 AND strlen($disclaim_referer) > 0) {
						$valided_url = $disclaim_referer;
					} else { 
						$valided_url = $_SERVER['REQUEST_URI'];
					}
				 	
					//if the URL contain vars
					$valided_url = $this->varsCombine($valided_url, 'disclaimToken=1&disclaimKey='.$disclaim_key);
					
				 } else { //other page
			 		$valided_link = $this->jmenu->getItem((int) $this->moplg_params['validedurl']);
					if(is_object($valided_link)) { 

						$valided_url = html_entity_decode($valided_link->flink);

						//if the URL contain vars
						$valided_url = $this->varsCombine($valided_url, 'disclaimToken=1&disclaimKey='.$disclaim_key);

					}
					else { $valided_url = JUri::base() . '?disclaimToken=1&disclaimKey='.$disclaim_key; }
				 }
				 
				 //Set the forward url called by clicking on the "exit link"
				 $exit_link = (string) $this->moplg_params['exiturl'];
				 if(!empty($exit_link)) { $exit_url = $exit_link;}
				 else { $exit_url = 'http://www.google.com/'; }

				 if($this->moplg_params['disclaimtype'] == 1) {//User is redirected to disclaimer page
				 
				 	/** store the full current URL in session
					/* this data will be used if the user is redirected in the arrival page by clicking in the enter link */
					$this->session->set('disclaimReferer', $_SERVER['REQUEST_URI']);
					
					//be SEF... don't redirect the robots :-)
					$jbrowser =& JBrowser::getInstance();
					$robot_sef = (int) JArrayHelper::getValue($this->moplg_params, 'robotsef');
					if($jbrowser->isRobot() == TRUE AND $robot_sef == 0) { return FALSE; }
										
					//Set the url where the user will be redirected
					$asking_link = $this->jmenu->getItem((int) $this->moplg_params['askingurl']);
					if(is_object($asking_link)) {
						
						if($this->jmenu->getActive()->id != $asking_link->id) {
							JFactory::getApplication()->redirect(html_entity_decode($asking_link->flink)); die();
						}
						
						//we are on the disclaimer page
						$this->doc->addScriptDeclaration('window.MODisclaimer = {token: false, disclaimtype: 1}; //object for MOJoomDisclaimer');
					 	$this->doc->addScriptDeclaration('window.MODisclaimer.url = "'.$valided_url.'";');
					 	$this->doc->addScriptDeclaration('window.MODisclaimer.exitUrl = "'.$exit_url.'";');
						
						
					} else { JFactory::getApplication()->enqueueMessage(JText::_('PLG_SYSTEM_MOJOOMDISCLAIMER_ERROR_ASKINK_LINK') , 'error'); }
					
					$this->invok = TRUE;
				 
				 } else {
				 	 //disclaimer displayed in modal popup
					 JHtmlBehavior::modal();
					 $this->doc->addStyleDeclaration('#mo-disclaim-modal-wrapper, #sbox-btn-close{display: none;}');
					 $modal_object = '{token: false,disclaimtype: 0,rwd: '.(int) $this->moplg_params['rwd'].',overlay:'. (float) $this->moplg_params['overlay'].'}';
					 
					 $this->doc->addScriptDeclaration('window.MODisclaimer = '.$modal_object.'; //object for the modal disclaimer by MOJoomDisclaimer');
					 $this->doc->addScriptDeclaration('window.MODisclaimer.url = "'.$valided_url.'";');
					 $this->doc->addScriptDeclaration('window.MODisclaimer.exitUrl = "'.$exit_url.'";');

					 //jQuery : load OR not load ??
					 if($this->moplg_params['jquery'] != 0) { JHtmlJquery::framework(TRUE); }
					 $this->invok = TRUE;
				 }
			}
		} else { 
			$this->invok = FALSE;
		}
		      
    }
	
	
	public function onAfterRender() {
		
		if($this->invok == TRUE) {

			$admin_base_path = str_ireplace('plugins\system\mojoomdisclaimer', 'administrator', dirname(__FILE__));

			$lang = &JFactory::getLanguage();
			$load = $lang->load('plg_system_mojoomdisclaimer', $admin_base_path, $lang->getTag(), TRUE);

			//Set the HTML of the disclaimer				
			$enter_text = JText::_('PLG_SYSTEM_MOJOOMDISCLAIMER_ENTER_LINK');
			$exit_text = JText::_('PLG_SYSTEM_MOJOOMDISCLAIMER_EXIT_LINK');

			$html = '<div id="mo-disclaimer-content">'.preg_replace(array('#<script#','#/script>#'), '', $this->moplg_params['content']).'</div>'.PHP_EOL;
			$html.= '<div id="mo-disclaimer-links"><a href="#" id="mo-disclaim-enter" title="'.$enter_text.'">'.$enter_text.'</a>    <a href="#" id="mo-disclaim-exit" title="'.$exit_text.'">'.$exit_text.'</a></div>';
			
			if($this->moplg_params['disclaimtype'] == 1) {
				//Disclaimer is loaded in a page
			
				$preg_patterns = array('#{MODisclaimerDisplay}#', '#</body>#');
				$preg_replacements = array($html, 
										  '<script type="text/javascript" src="'.JUri::base().'plugins/system/mojoomdisclaimer/mojoomdisclaimer.js"></script>'.PHP_EOL.'</body>');
					
				JResponse::setBody(preg_replace($preg_patterns, $preg_replacements, JResponse::getBody()));
				
			} else {
				//Adding the HTML and the JAVASCRIPT code necessary for a modal display
				
				JResponse::setBody(str_ireplace('</body>', '<div id="mo-disclaim-modal-wrapper"><div id="mo-disclaim-modal-inner">'.$html.'</div></div>'.PHP_EOL.'<script type="text/javascript" src="'.JUri::base().'plugins/system/mojoomdisclaimer/mojoomdisclaimer.js"></script>'.PHP_EOL.'</body>', JResponse::getBody()));
			}
			
		}
    }
}