GeoIP/copy_this/core/d3geoip.php

202 lines
7.0 KiB
PHP
Raw Normal View History

2009-12-15 09:52:22 +01:00
<?php
class d3GeoIP extends oxI18n
{
/**
* Current class name
*
* @var string
*/
protected $_sClassName = 'd3geoip';
private $_sModId = 'd3_geoip';
2009-12-15 09:52:22 +01:00
/**
* Class constructor, initiates parent constructor (parent::oxI18n()).
*
* @return null
*/
public function __construct()
{
parent::__construct();
$this->init('d3geoip');
}
public function getUserLocationCountryObject($sIP = false)
2009-12-15 09:52:22 +01:00
{
if (!$this->oCountry)
{
if (!$sIP)
$sIP = $this->getIP();
2009-12-15 09:52:22 +01:00
$iIPNum = $this->_getNumIp($sIP);
$sISOAlpha = $this->LoadByIPNum($iIPNum);
if (!$sISOAlpha)
{
$this->_getLog()->setLog('error', __CLASS__, __FUNCTION__, __LINE__, 'get ISO by IP failed', $sIP);
$this->oCountry = oxNew('oxcountry');
}
else
{
$this->_getLog()->setLog('info', __CLASS__, __FUNCTION__, __LINE__, 'get ISO by IP', $sIP." => ".$sISOAlpha);
$this->oCountry = $this->getCountryObject($sISOAlpha);
}
2009-12-15 09:52:22 +01:00
}
return $this->oCountry;
}
public function getIP()
{
if ($this->_getConfig()->getValue('blUseTestIp') && $this->_getConfig()->getValue('sTestIp'))
return $this->_getConfig()->getValue('sTestIp');
else
return $_SERVER['REMOTE_ADDR'];
}
2009-12-15 09:52:22 +01:00
protected function _getNumIp($sIP)
{
$aIP = explode('.',$sIP);
$iIP = ($aIP[0] * 16777216) + ($aIP[1] * 65536) + ($aIP[2] * 256) + ($aIP[3] * 1);
return $iIP;
}
public function LoadByIPNum($iIPNum)
{
$sSelect = "SELECT d3iso FROM ".$this->_sClassName." WHERE d3startipnum <= '$iIPNum' AND d3endipnum >= '$iIPNum'";
return oxDb::getDb()->getOne($sSelect);
}
public function getCountryObject($sISOAlpha)
{
$oCountry = &oxNew('oxcountry');
2009-12-15 10:05:46 +01:00
$sSelect = "SELECT oxid FROM ".$oCountry->getViewName()." WHERE OXISOALPHA2 = '".$sISOAlpha."' AND OXACTIVE = '1'";
2009-12-15 09:52:22 +01:00
$oCountry->load(oxDb::getDb()->getOne($sSelect));
return $oCountry;
}
2009-12-15 10:05:46 +01:00
/*
2009-12-15 09:52:22 +01:00
public function setUserCountry()
{
if (!$this->getUser()) {
$oCountry = $this->getUserLocationCountryObject();
$oUser = &oxNew('oxuser');
$oUser->oxuser__oxcountryid = oxNew('oxfield');
$oUser->oxuser__oxcountryid->setValue($oCountry->getId());
$this->setUser($oUser);
}
return;
}
2009-12-15 10:05:46 +01:00
*/
2009-12-15 09:52:22 +01:00
public function setCountryLanguage()
{
$this->performURLSwitch();
2009-12-15 10:05:46 +01:00
$this->performShopSwitch();
if (!$this->_getConfig()->getFieldData('oxactive') || !$this->_getConfig()->getValue('blChangeLang'))
return;
$oCountry = $this->getUserLocationCountryObject();
2009-12-15 10:05:46 +01:00
if (!$this->isAdmin() && oxUtils::getInstance()->isSearchEngine() === false && $this->getSession()->getVar('d3isSetLang') === null && $oCountry->getId() && $oCountry->getFieldData('d3geoiplang') > -1)
2009-12-15 09:52:22 +01:00
{
$this->_getLog()->setLog('info', __CLASS__, __FUNCTION__, __LINE__, 'set language', $this->getIP().' => '.$oCountry->getFieldData('d3geoiplang'));
2009-12-15 10:05:46 +01:00
oxLang::getInstance()->setTplLanguage((int) $oCountry->getFieldData('d3geoiplang'));
oxLang::getInstance()->setBaseLanguage((int) $oCountry->getFieldData('d3geoiplang'));
2009-12-15 09:52:22 +01:00
$this->getSession()->setVar('d3isSetLang', true);
}
}
public function setCountryCurrency()
{
if (!$this->_getConfig()->getFieldData('oxactive') || !$this->_getConfig()->getValue('blChangeCurr'))
return;
2009-12-15 09:52:22 +01:00
$oCountry = $this->getUserLocationCountryObject();
2009-12-15 10:05:46 +01:00
if (!$this->isAdmin() && oxUtils::getInstance()->isSearchEngine() === false && !$this->getSession()->getVar('d3isSetCurr') && $oCountry->getId() && $oCountry->getFieldData('d3geoipcur') > -1)
2009-12-15 09:52:22 +01:00
{
$this->_getLog()->setLog('info', __CLASS__, __FUNCTION__, __LINE__, 'set currency', $this->getIP().' => '.$oCountry->getFieldData('d3geoipcur'));
2009-12-15 10:05:46 +01:00
$this->getConfig()->setActShopCurrency((int) $oCountry->getFieldData('d3geoipcur'));
2009-12-15 09:52:22 +01:00
$this->getSession()->setVar('d3isSetCurr', true);
}
}
2009-12-15 10:05:46 +01:00
public function performShopSwitch()
{
if (!$this->_getConfig()->getFieldData('oxactive') || !$this->_getConfig()->getValue('blChangeShop'))
return;
2009-12-15 10:05:46 +01:00
$oCountry = $this->getUserLocationCountryObject();
$iNewShop = $oCountry->getFieldData('d3geoipshop');
$aShopLinks = $this->getShopUrls();
if (!$this->isAdmin() && oxUtils::getInstance()->isSearchEngine() === false && $oCountry->getId() && $this->getConfig()->isMall() && $iNewShop > -1 && $iNewShop != $this->getConfig()->getShopId())
{
$oNewConf = new oxConfig();
$oNewConf->setShopId($iNewShop);
$oNewConf->init();
$this->getConfig()->onShopChange();
2009-12-15 10:05:46 +01:00
if (!$this->getSession()->getVar('d3isSetLang') && $oCountry->getFieldData('d3geoiplang') > -1)
$sLangId = $oCountry->getFieldData('d3geoiplang');
else
$sLangId = '';
2009-12-15 10:05:46 +01:00
$this->_getLog()->setLog('info', __CLASS__, __FUNCTION__, __LINE__, 'change shop', $this->getIP().' => '.$oNewConf->getShopHomeUrl($sLangId));
header("Location: ".$oNewConf->getShopHomeUrl($sLangId));
exit();
}
}
public function performURLSwitch()
{
if (!$this->_getConfig()->getFieldData('oxactive') || !$this->_getConfig()->getValue('blChangeURL'))
return;
$oCountry = $this->getUserLocationCountryObject();
if (!$this->isAdmin() && oxUtils::getInstance()->isSearchEngine() === false && $oCountry->getId() && $oCountry->getFieldData('d3geoipurl') && strlen(trim($oCountry->getFieldData('d3geoipurl'))) > 0)
{
$sNewUrl = $oCountry->getFieldData('d3geoipurl');
2009-12-15 10:05:46 +01:00
$this->_getLog()->setLog('info', __CLASS__, __FUNCTION__, __LINE__, 'change url', $this->getIP().' => '.$oCountry->getFieldData('d3geoipurl'));
header("Location: ".$sNewUrl);
exit();
2009-12-15 10:05:46 +01:00
}
}
public function getShopUrls()
{
$oShoplist = oxNew( 'oxshoplist' );
$oShoplist->getList();
$aShopUrls = array();
foreach ( $oShoplist as $sId => $oShop ) {
$aShopUrls[$sId] = $this->getConfig()->getShopConfVar( 'sMallShopURL', $sId );
}
return $aShopUrls;
}
protected function _getConfig()
{
return d3_cfg_mod::get($this->_sModId);
}
protected function _getLog()
{
if (!$this->oD3Log)
$this->oD3Log = $this->_getConfig()->getLogInstance();
return $this->oD3Log;
}
2009-12-15 09:52:22 +01:00
}