GeoIP/copy_this/modules/d3/d3geoip/models/d3geoip.php

291 lines
8.8 KiB
PHP
Raw Normal View History

2013-04-22 11:21:42 +02:00
<?php
2013-04-22 11:40:11 +02:00
/**
* This Software is the property of Data Development and is protected
* by copyright law - it is NOT Freeware.
*
* Any unauthorized use of this software without a valid license
* is a violation of the license agreement and will be prosecuted by
* civil and criminal law.
*
* http://www.shopmodule.com
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <ds@shopmodule.com>
* @link http://www.oxidmodule.com
*/
2012-02-15 14:17:42 +01:00
2013-04-22 11:21:42 +02:00
class d3GeoIP extends oxI18n
{
protected $_sClassName = 'd3geoip';
private $_sModId = 'd3_geoip';
2013-04-22 11:40:11 +02:00
public $oCountry;
public $oD3Log;
2013-04-22 11:21:42 +02:00
/**
* Class constructor, initiates parent constructor (parent::oxI18n()).
*/
public function __construct()
{
parent::__construct();
$this->init('d3geoip');
}
/**
* get oxcountry object by given IP address (optional)
*
* @param string $sIP optional
2013-04-22 11:40:11 +02:00
* @return oxcountry
2013-04-22 11:21:42 +02:00
*/
2013-04-22 11:40:11 +02:00
public function getUserLocationCountryObject($sIP = null)
2013-04-22 11:21:42 +02:00
{
if (!$this->oCountry)
{
if (!$sIP)
2013-04-22 11:40:11 +02:00
{
2013-04-22 11:21:42 +02:00
$sIP = $this->getIP();
2013-04-22 11:40:11 +02:00
}
2013-04-22 11:21:42 +02:00
$iIPNum = $this->_getNumIp($sIP);
$sISOAlpha = $this->LoadByIPNum($iIPNum);
if (!$sISOAlpha)
{
2013-04-22 11:40:11 +02:00
$this->_getLog()->log('error', __CLASS__, __FUNCTION__, __LINE__, 'get ISO by IP failed', $sIP);
2012-02-15 14:17:42 +01:00
$this->oCountry = $this->getCountryFallBackObject();
2013-04-22 11:21:42 +02:00
}
else
{
2013-04-22 11:40:11 +02:00
$this->_getLog()->log('info', __CLASS__, __FUNCTION__, __LINE__, 'get ISO by IP', $sIP." => ".$sISOAlpha);
2013-04-22 11:21:42 +02:00
$this->oCountry = $this->getCountryObject($sISOAlpha);
}
}
return $this->oCountry;
}
/**
* get IP address from client or set test IP address
*
* @return string
*/
public function getIP()
{
if ($this->_getModConfig()->getValue('blUseTestIp') && $this->_getModConfig()->getValue('sTestIp'))
2013-04-22 11:40:11 +02:00
{
2013-04-22 11:21:42 +02:00
return $this->_getModConfig()->getValue('sTestIp');
2013-04-22 11:40:11 +02:00
}
2013-04-22 11:25:27 +02:00
elseif ($this->_getModConfig()->getValue('blUseTestCountry') && $this->_getModConfig()->getValue('sTestCountryIp'))
2013-04-22 11:40:11 +02:00
{
2013-04-22 11:25:27 +02:00
return $this->_getModConfig()->getValue('sTestCountryIp');
2013-04-22 11:40:11 +02:00
}
2013-04-22 11:21:42 +02:00
else
2013-04-22 11:40:11 +02:00
{
2013-04-22 11:21:42 +02:00
return $_SERVER['REMOTE_ADDR'];
2013-04-22 11:40:11 +02:00
}
2013-04-22 11:21:42 +02:00
}
/**
* get IP number by IP address
*
* @param string $sIP IP address
* @return int IP number
*/
protected function _getNumIp($sIP)
{
$aIP = explode('.',$sIP);
$iIP = ($aIP[0] * 16777216) + ($aIP[1] * 65536) + ($aIP[2] * 256) + ($aIP[3] * 1);
return $iIP;
}
/**
* get ISO alpha 2 ID by IP number
*
* @param int $iIPNum IP number
* @return string
*/
public function LoadByIPNum($iIPNum)
{
$sSelect = "SELECT d3iso FROM ".$this->_sClassName." WHERE d3startipnum <= '$iIPNum' AND d3endipnum >= '$iIPNum'";
return oxDb::getDb()->getOne($sSelect);
}
/**
* get oxcountry object by ISO alpha 2 ID
*
* @param string $sISOAlpha
2013-04-22 11:40:11 +02:00
* @return oxcountry
2013-04-22 11:21:42 +02:00
*/
public function getCountryObject($sISOAlpha)
{
2012-02-15 14:17:42 +01:00
$oCountry = oxNew('oxcountry');
2013-04-22 11:21:42 +02:00
$sSelect = "SELECT oxid FROM ".$oCountry->getViewName()." WHERE OXISOALPHA2 = '".$sISOAlpha."' AND OXACTIVE = '1'";
2013-04-22 11:25:27 +02:00
2013-04-22 11:21:42 +02:00
$oCountry->load(oxDb::getDb()->getOne($sSelect));
return $oCountry;
}
/**
* get oxcountry object for fallback, if set
*
* @return object
*/
2012-02-15 14:17:42 +01:00
public function getCountryFallBackObject()
2012-02-15 14:15:28 +01:00
{
2012-02-15 14:17:42 +01:00
$oCountry = oxNew('oxcountry');
2013-04-22 11:21:42 +02:00
if ($this->_getModConfig()->getValue('blUseFallback') && $this->_getModConfig()->getValue('sFallbackCountryId'))
2012-02-15 14:17:42 +01:00
{
2013-04-22 11:21:42 +02:00
$oCountry->Load($this->_getModConfig()->getValue('sFallbackCountryId'));
2012-02-15 14:17:42 +01:00
}
2013-04-22 11:21:42 +02:00
2012-02-15 14:15:28 +01:00
return $oCountry;
}
2013-04-22 11:21:42 +02:00
/**
* check module active state and set user country specific language
*
*/
public function setCountryLanguage()
{
$this->performURLSwitch();
$this->performShopSwitch();
2013-04-22 11:40:11 +02:00
if (!$this->_getModConfig()->isActive() || !$this->_getModConfig()->getValue('blChangeLang'))
2013-04-22 11:21:42 +02:00
return;
$oCountry = $this->getUserLocationCountryObject();
2013-04-22 11:40:11 +02:00
if (!$this->isAdmin() && oxRegistry::getUtils()->isSearchEngine() === false && oxRegistry::getSession()->getVariable('d3isSetLang') === null && $oCountry->getId() && $oCountry->getFieldData('d3geoiplang') > -1)
2013-04-22 11:21:42 +02:00
{
2013-04-22 11:40:11 +02:00
$this->_getLog()->log('info', __CLASS__, __FUNCTION__, __LINE__, 'set language', $this->getIP().' => '.$oCountry->getFieldData('d3geoiplang'));
oxRegistry::getLang()->setTplLanguage((int) $oCountry->getFieldData('d3geoiplang'));
oxRegistry::getLang()->setBaseLanguage((int) $oCountry->getFieldData('d3geoiplang'));
oxRegistry::getSession()->setVariable('d3isSetLang', true);
2013-04-22 11:21:42 +02:00
}
}
/**
* check module active state and set user country specific currency
*
*/
public function setCountryCurrency()
{
2013-04-22 11:40:11 +02:00
if (!$this->_getModConfig()->isActive() || !$this->_getModConfig()->getValue('blChangeCurr'))
2013-04-22 11:21:42 +02:00
return;
$oCountry = $this->getUserLocationCountryObject();
2013-04-22 11:40:11 +02:00
if (!$this->isAdmin() && oxRegistry::getUtils()->isSearchEngine() === false && !oxRegistry::getSession()->getVariable('d3isSetCurr') && $oCountry->getId() && $oCountry->getFieldData('d3geoipcur') > -1)
2013-04-22 11:21:42 +02:00
{
2013-04-22 11:40:11 +02:00
$this->_getLog()->log('info', __CLASS__, __FUNCTION__, __LINE__, 'set currency', $this->getIP().' => '.$oCountry->getFieldData('d3geoipcur'));
oxRegistry::getConfig()->setActShopCurrency((int) $oCountry->getFieldData('d3geoipcur'));
oxRegistry::getSession()->setVariable('d3isSetCurr', true);
2013-04-22 11:21:42 +02:00
}
}
/**
* check module active state and perform switching to user country specific shop (EE only)
*
*/
public function performShopSwitch()
{
2013-04-22 11:40:11 +02:00
if (!$this->_getModConfig()->isActive() || !$this->_getModConfig()->getValue('blChangeShop'))
2013-04-22 11:21:42 +02:00
return;
$oCountry = $this->getUserLocationCountryObject();
$iNewShop = $oCountry->getFieldData('d3geoipshop');
2013-04-22 11:40:11 +02:00
if (!$this->isAdmin() && oxRegistry::getUtils()->isSearchEngine() === false && $oCountry->getId() && $this->getConfig()->isMall() && $iNewShop > -1 && $iNewShop != $this->getConfig()->getShopId())
2013-04-22 11:21:42 +02:00
{
$oNewConf = new oxConfig();
$oNewConf->setShopId($iNewShop);
$oNewConf->init();
$this->getConfig()->onShopChange();
2013-04-22 11:40:11 +02:00
if (!oxRegistry::getSession()->getVariable('d3isSetLang') && $oCountry->getFieldData('d3geoiplang') > -1)
{
2013-04-22 11:21:42 +02:00
$sLangId = $oCountry->getFieldData('d3geoiplang');
2013-04-22 11:40:11 +02:00
}
2013-04-22 11:21:42 +02:00
else
2013-04-22 11:40:11 +02:00
{
2013-04-22 11:21:42 +02:00
$sLangId = '';
2013-04-22 11:40:11 +02:00
}
2013-04-22 11:21:42 +02:00
2013-04-22 11:40:11 +02:00
$this->_getLog()->log('info', __CLASS__, __FUNCTION__, __LINE__, 'change shop', $this->getIP().' => '.$oNewConf->getShopHomeUrl($sLangId));
2013-04-22 11:21:42 +02:00
header("Location: ".$oNewConf->getShopHomeUrl($sLangId));
exit();
}
}
/**
* check module active state and perform switching to user country specific url
*
*/
public function performURLSwitch()
{
2013-04-22 11:40:11 +02:00
if (!$this->_getModConfig()->isActive() || !$this->_getModConfig()->getValue('blChangeURL'))
{
2013-04-22 11:21:42 +02:00
return;
2013-04-22 11:40:11 +02:00
}
2013-04-22 11:21:42 +02:00
$oCountry = $this->getUserLocationCountryObject();
2013-04-22 11:40:11 +02:00
if (!$this->isAdmin() && oxRegistry::getUtils()->isSearchEngine() === false && $oCountry->getId() && $oCountry->getFieldData('d3geoipurl') && strlen(trim($oCountry->getFieldData('d3geoipurl'))) > 0)
2013-04-22 11:21:42 +02:00
{
$sNewUrl = $oCountry->getFieldData('d3geoipurl');
2013-04-22 11:40:11 +02:00
$this->_getLog()->log('info', __CLASS__, __FUNCTION__, __LINE__, 'change url', $this->getIP().' => '.$oCountry->getFieldData('d3geoipurl'));
2013-04-22 11:21:42 +02:00
header("Location: ".$sNewUrl);
exit();
}
}
/**
* get all shop urls
*
* @return array
*/
public function getShopUrls()
{
$oShoplist = oxNew( 'oxshoplist' );
$oShoplist->getList();
$aShopUrls = array();
2013-04-22 11:40:11 +02:00
foreach ( $oShoplist as $sId => $oShop )
{
2013-04-22 11:21:42 +02:00
$aShopUrls[$sId] = $this->getConfig()->getShopConfVar( 'sMallShopURL', $sId );
}
return $aShopUrls;
}
/**
* get modcfg instance
*
2013-04-22 11:40:11 +02:00
* @return d3_cfg_mod
2013-04-22 11:21:42 +02:00
*/
protected function _getModConfig()
{
return d3_cfg_mod::get($this->_sModId);
}
/**
* get d3log instance
*
2013-04-22 11:40:11 +02:00
* @return d3log
2013-04-22 11:21:42 +02:00
*/
protected function _getLog()
{
if (!$this->oD3Log)
2013-04-22 11:40:11 +02:00
{
$this->oD3Log = $this->_getModConfig()->getLog();
}
2013-04-22 11:21:42 +02:00
return $this->oD3Log;
}
2009-12-15 09:52:22 +01:00
}