GeoIP/copy_this/core/d3geoip.php

146 lignes
4.6 KiB
PHP
Brut Vue normale Historique

2009-12-15 09:52:22 +01:00
<?php
class d3GeoIP extends oxI18n
{
/**
* Current class name
*
* @var string
*/
protected $_sClassName = 'd3geoip';
/**
* Class constructor, initiates parent constructor (parent::oxI18n()).
*
* @return null
*/
public function __construct()
{
parent::__construct();
$this->init('d3geoip');
}
public function getUserLocationCountryObject()
{
if (!$this->oCountry)
{
$sIP = $_SERVER['REMOTE_ADDR'];
2009-12-15 10:05:46 +01:00
// $sIP = '62.4.77.32'; // Deutschland
// $sIP = '62.4.77.48'; // Deutschland
// $sIP = '41.188.100.127'; // Mauretanien
// $sIP = '4.18.40.144'; // US
2009-12-15 09:52:22 +01:00
$iIPNum = $this->_getNumIp($sIP);
$sISOAlpha = $this->LoadByIPNum($iIPNum);
$this->oCountry = $this->getCountryObject($sISOAlpha);
}
return $this->oCountry;
}
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()
{
$oCountry = $this->getUserLocationCountryObject();
2009-12-15 10:05:46 +01:00
$this->performShopSwitch();
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
{
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()
{
$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
{
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()
{
$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();
if (!$this->getSession()->getVar('d3isSetLang') && $oCountry->getFieldData('d3geoiplang') > -1)
$sLangId = $oCountry->getFieldData('d3geoiplang');
else
$sLangId = '';
header("Location: ".$oNewConf->getShopHomeUrl($sLangId));
exit();
}
}
public function getShopUrls()
{
$oShoplist = oxNew( 'oxshoplist' );
$oShoplist->getList();
$aShopUrls = array();
foreach ( $oShoplist as $sId => $oShop ) {
$aShopUrls[$sId] = $this->getConfig()->getShopConfVar( 'sMallShopURL', $sId );
}
return $aShopUrls;
}
2009-12-15 09:52:22 +01:00
}