Your IP : 216.73.216.95


Current Path : /var/test/www/alh/catalog/controller/extension/module/
Upload File :
Current File : /var/test/www/alh/catalog/controller/extension/module/geo_ip_tools.php

<?php
//==============================================================================
// Geo IP Tools v303.2
// 
// Author: Clear Thinking, LLC
// E-mail: johnathan@getclearthinking.com
// Website: http://www.getclearthinking.com
// 
// All code within this file is copyright Clear Thinking, LLC.
// You may not copy or reuse code within this file without written permission.
//==============================================================================

class ControllerExtensionModuleGeoIpTools extends Model {
	private $type = 'module';
	private $name = 'geo_ip_tools';
	private $testing_mode;
	
	//==============================================================================
	// index()
	//==============================================================================
	public function index() {
		$settings = $this->getSettings();
		$this->testing_mode = $settings['testing_mode'];
		
		if (empty($settings['status'])) return;
		
		// Check user agents to ignore
		$ignore = empty($this->request->server['HTTP_USER_AGENT']);
		
		foreach (explode("\n", $settings['ignore_user_agents']) as $user_agent) {
			if (empty($user_agent) || empty($this->request->server['HTTP_USER_AGENT'])) {
				continue;
			}
			if (strpos(strtolower($this->request->server['HTTP_USER_AGENT']), trim(strtolower($user_agent))) !== false) {
				$ignore = true;
			}
		}
		
		// Check non-standard server variables that proxies use
		$nonstandard_server_variables = array(
			'HTTP_X_FORWARDED_FOR',
			'HTTP_CLIENT_IP',
			'HTTP_CF_CONNECTING_IP',
		);
		
		foreach ($nonstandard_server_variables as $nsv) {
			if (!empty($this->request->server[$nsv])) {
				$this->request->server['REMOTE_ADDR'] = $this->request->server[$nsv];
			}
		}
		
		// Remove extra IPs that some servers add
		$explode = explode(',', $this->request->server['REMOTE_ADDR']);
		$this->request->server['REMOTE_ADDR'] = $explode[0];
		
		// Check for IPs to ignore
		if (!empty($settings['ignore_ips'])) {
			$ips = array_filter(explode(',', str_replace(array("\n", ',,', ' '), array(',', ',', ''), $settings['ignore_ips'])));
			
			foreach ($ips as $range) {
				$range = explode('-', $range);
				if (empty($range[1])) $range[1] = $range[0];
				
				if (ip2long($this->request->server['REMOTE_ADDR']) >= ip2long($range[0]) && ip2long($this->request->server['REMOTE_ADDR']) <= ip2long($range[1])) {
					$ignore = true;
				}
			}
		}
		
		// Check IP and rules
		if (empty($this->request->server['REMOTE_ADDR']) || $ignore) {
			return '';
		}
		
		$this->logMessage("\n" . '------------------------------ Starting Test ' . date('Y-m-d G:i:s') . ' ------------------------------');
		
		$redirect = '';
		$this->load->model('extension/' . $this->type . '/' . $this->name);
		
		if (empty($this->session->data['geoip_data'])) {
			$location = $this->{'model_extension_'.$this->type.'_'.$this->name}->getLocation($this->request->server['REMOTE_ADDR']);
			$this->{'model_extension_'.$this->type.'_'.$this->name}->setLocation($location);
			$redirect = $this->{'model_extension_'.$this->type.'_'.$this->name}->checkLocation($location);
		} elseif ($settings['popup'] && empty($this->session->data['geoip_data']['location']['disable_popup'])) {
			$this->session->data['geoip_data']['location']['disable_popup'] = true;
			$data['location'] = $this->session->data['geoip_data']['location'];
			
			$data = array_merge($data, $this->load->language('account/address'));
			$data['geo_ip_tools_popup'] = $settings['popup_text_' . $this->session->data['language']];
			$data['geo_ip_tools_popup'] = str_replace('[ip]', $this->request->server['REMOTE_ADDR'], $data['geo_ip_tools_popup']);
			$data['geo_ip_tools_popup'] = html_entity_decode($data['geo_ip_tools_popup'], ENT_QUOTES, 'UTF-8');
			
			$this->load->model('localisation/country');
			$data['countries'] = $this->model_localisation_country->getCountries();
			
			$this->load->model('localisation/currency');
			$data['currencies'] = $this->model_localisation_currency->getCurrencies();
			$data['session_currency'] = $this->session->data['currency'];
			
			$this->load->model('localisation/language');
			$data['languages'] = $this->model_localisation_language->getLanguages();
			$data['session_language'] = $this->session->data['language'];
			
			foreach (array('country', 'zone', 'city', 'postcode', 'currency', 'language') as $field) {
				$data[$field . '_field'] = $settings[$field . '_field_' . $data['session_language']];
			}
		}
		
		// Check redirects
		if (!$redirect) {
			$redirect = $this->{'model_extension_'.$this->type.'_'.$this->name}->checkBlocksAndStoreRedirects();
		}
		
		if ($redirect) {
			header('Location: ' . str_replace('&amp;', '&', str_replace('*', substr($this->request->server['REQUEST_URI'], 1), $redirect)), true);
			exit();
		}
		
		// Output pop-up HTML if no redirect
		$output = '';
		
		if (!empty($data['geo_ip_tools_popup'])) {
			$template_file = 'catalog/view/theme/default/template/extension/' . $this->type . '/' . $this->name . '.twig';
			extract($data);
			
			ob_start();
			require(class_exists('VQMod') ? VQMod::modCheck(modification($template_file)) : modification($template_file));
			$output = ob_get_clean();
		}
		
		// Generate javascript for pop-up
		$javascript = '
			<meta name="viewport" content="width=device-width, initial-scale=1.0" />
			<script>
				function showPopup() {
					$("#modal-overlay, #geo-ip-tools-popup").show();
				}
		';
		
		if (!$settings['browser']) {
			$javascript .= "
				$(document).ready(function(){
					showPopup();
				});
			";
		} else {
			$javascript .= '
				$(document).ready(function(){
					if (navigator.geolocation) {
						navigator.geolocation.getCurrentPosition(setBrowserLocation, showPopup, {maximumAge: 60*60*1000});
					} else {
						showPopup();
					}
				});
				
				function setBrowserLocation(geocode) {
					$.get("index.php?route=extension/' . $this->type . '/' . $this->name . '/setLocation&geocode=" + geocode.coords.latitude + "," + geocode.coords.longitude);
				}
			';
		}
		
		return $output . $javascript . '</script>';
	}
	
	//==============================================================================
	// getZones()
	//==============================================================================
	public function getZones() {
		$this->load->model('localisation/zone');
		$zones = $this->model_localisation_zone->getZonesByCountryId($this->request->get['country_id']);
		echo json_encode($zones);
	}
	
	//==============================================================================
	// setLocation()
	//==============================================================================
	public function setLocation() {
		$this->load->model('extension/' . $this->type . '/' . $this->name);
		
		if (isset($this->request->get['geocode'])) {
			$location = $this->{'model_extension_'.$this->type.'_'.$this->name}->getBrowserLocation($this->request->get['geocode']);
		} else {
			$location = array(
				'country_id'	=> (isset($this->request->post['popup_country'])) ? $this->request->post['popup_country'] : '',
				'zone_id'		=> (isset($this->request->post['popup_zone'])) ? $this->request->post['popup_zone'] : '',
				'city'			=> (isset($this->request->post['popup_city'])) ? $this->request->post['popup_city'] : '',
				'postcode'		=> (isset($this->request->post['popup_postcode'])) ? $this->request->post['popup_postcode'] : '',
				'disable_popup'	=> true,
			);
		}
		
		$this->{'model_extension_'.$this->type.'_'.$this->name}->setLocation($location);
		$redirect = $this->{'model_extension_'.$this->type.'_'.$this->name}->checkLocation($location);
		
		if (!empty($this->request->post['popup_currency'])) {
			$this->session->data['currency'] = $this->request->post['popup_currency'];
		}
		if (!empty($this->request->post['popup_language'])) {
			$this->session->data['language'] = $this->request->post['popup_language'];
		}
		
		$config_url = $this->config->get('config_url');
		$popup_redirect = (!empty($this->request->post['popup_redirect'])) ? $this->request->post['popup_redirect'] : $config_url;
		
		if ($redirect == $config_url) {
			$redirect = $popup_redirect;
		}
		
		header('Location: ' . str_replace('&amp;', '&', str_replace('*', substr($popup_redirect, 1), $redirect)), true);
		exit();
	}
	
	//==============================================================================
	// Private functions
	//==============================================================================
	private function getSettings() {
		$code = (version_compare(VERSION, '3.0', '<') ? '' : $this->type . '_') . $this->name;
		
		$settings = array();
		$settings_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "setting WHERE `code` = '" . $this->db->escape($code) . "' ORDER BY `key` ASC");
		
		foreach ($settings_query->rows as $setting) {
			$value = $setting['value'];
			if ($setting['serialized']) {
				$value = (version_compare(VERSION, '2.1', '<')) ? unserialize($setting['value']) : json_decode($setting['value'], true);
			}
			$split_key = preg_split('/_(\d+)_?/', str_replace($code . '_', '', $setting['key']), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
			
				if (count($split_key) == 1)	$settings[$split_key[0]] = $value;
			elseif (count($split_key) == 2)	$settings[$split_key[0]][$split_key[1]] = $value;
			elseif (count($split_key) == 3)	$settings[$split_key[0]][$split_key[1]][$split_key[2]] = $value;
			elseif (count($split_key) == 4)	$settings[$split_key[0]][$split_key[1]][$split_key[2]][$split_key[3]] = $value;
			else 							$settings[$split_key[0]][$split_key[1]][$split_key[2]][$split_key[3]][$split_key[4]] = $value;
		}
		
		return $settings;
	}
	
	private function logMessage($message) {
		if ($this->testing_mode) {
			file_put_contents(DIR_LOGS . $this->name . '.messages', print_r($message, true) . "\n", FILE_APPEND|LOCK_EX);
		}
	}
}
?>