Skip to content

Instantly share code, notes, and snippets.

@leesbian
Created November 1, 2016 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leesbian/0324f8b85787a7c0eecd90fa846408bb to your computer and use it in GitHub Desktop.
Save leesbian/0324f8b85787a7c0eecd90fa846408bb to your computer and use it in GitHub Desktop.
How not to bootstrap Magento
// ...
// Regular index.php stuff above here
// default site
$mage_run_code = 'base';
$currentzone = 'UK';
$geo_country_code = 'GB';
if(isset($_SERVER['HTTP_X_STORE'])) {
$currentzone = $_SERVER['HTTP_X_STORE'];
$geo_country_code = $_SERVER['HTTP_X_STORE'];
}
if (isset($_SERVER['HTTP_CF_IPCOUNTRY'])) {
$geo_country_code = $_SERVER['HTTP_CF_IPCOUNTRY'];
} elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$geo_country_code = @geoip_country_code_by_name($_SERVER['HTTP_X_FORWARDED_FOR']);
} else {
$geo_country_code = apache_note("GEOIP_COUNTRY_CODE");
}
if (strlen($geo_country_code)==0 || $geo_country_code == '--') $geo_country_code = 'GB';
// bypass all this if in admin
if (!indexIsAdmin()) {
if (isset($_COOKIE['frontend'])) {
try {
$session_id = $_COOKIE['frontend'];
if (is_file('var/session/sess_'.$session_id)) {
$session_data = file_get_contents('var/session/sess_'.$session_id);
$szpattern = '#"shipping_zone";s:(?:[0-9]):"([A-Z]+)"#s';
preg_match($szpattern, $session_data, $szmatches);
if (is_array($szmatches) && isset($szmatches[1])) {
$session_shipping_zone = $szmatches[1];
}
}
} catch (Exception $e) {
}
}
// available sites, hard code for speed
$websites = array(
'UK'=>'base',
'EU'=>'eur_website',
'US'=>'usd_website',
'ROW'=>'row_website',
);
// countries for each site, not listed go to base
$uk_countries = array('GB', 'IM');
$euro_countries = array('AD','AT','BE','BG','CY','CZ','DE','DK','EE','ES','FI','FR','GR','HR','HU','IE','IT','LT','LU','LV','MT','NL','PL','PT','RO','SE','SI','SK');
$usd_countries = array('US','PR','UM');
if (array_search($geo_country_code, $uk_countries)!==false) {
$countryToRegion = 'UK';
} elseif (array_search($geo_country_code, $euro_countries)!==false) {
$countryToRegion = 'EU';
} elseif (array_search($geo_country_code, $usd_countries)!==false) {
$countryToRegion = 'US';
} else {
$countryToRegion = 'ROW';
}
// get current zone from url or session, else use geoip
$changedZone = false;
if (isset($_GET['shipto']) && isset($websites[$_GET['shipto']])) { // User wants to change the currency
$currentzone = $_GET['shipto'];
$mage_run_code = $websites[$currentzone];
setcookie('changed_zone',$currentzone,strtotime( '+8 hours' ),'/');
}
elseif(isset($_COOKIE['changed_zone']))
{
$currentzone = $_COOKIE['changed_zone'];
$mage_run_code = $websites[$currentzone];
}
else { // Max Mind
if (isset($_COOKIE['maxmind'])) { // We already have a Max Mind request
$currentzone = $_COOKIE['maxmind'];
} else { // New Maxmind request and save cookie
$ip = $_SERVER['REMOTE_ADDR'];
$address = 'https://geoip.maxmind.com/geoip/v2.1/country/' . $ip;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $address);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, 'secret');
$json = curl_exec($ch);
curl_close($ch);
$data = json_decode($json, true);
if(is_array($data)) {
if(isset($data['error'])) {
$country2letters = 'GB';
} else {
$country2letters = strtoupper($data['registered_country']['iso_code']);
}
} else {
$country2letters = 'GB';
}
$session_shipping_zone = $country2letters;
$geo_country_code = $country2letters;
$currentzone = '';
if($currentzone == ''){
// else use geoip
if (array_search($geo_country_code, $uk_countries)!==false) {
$currentzone = 'UK';
} elseif (array_search($geo_country_code, $euro_countries)!==false) {
$currentzone = 'EU';
} elseif (array_search($geo_country_code, $usd_countries)!==false) {
$currentzone = 'US';
} else {
$currentzone = 'ROW';
}
}
setcookie('maxmind',$currentzone,strtotime( '+8 hours' ),'/');
}
$mage_run_code = $websites[$currentzone];
}
}
Mage::run($mage_run_code, 'website');
if (!indexIsAdmin()) {
Mage::getSingleton('core/session')->setShippingZone($currentzone);
Mage::getSingleton('core/session')->setDetectedCountry($geo_country_code);
Mage::getSingleton('core/session')->setChangedZone($session_user_changed_zone);
if (!Mage::helper('customer')->isLoggedIn()) {
if ($customer = Mage::getSingleton('customer/session')) {
$customerId = $customer->getCustomerId();
if (is_numeric($customerId) && $customerId>0) {
Mage::getSingleton('customer/session')->unsetAll();
Mage::getModel('persistent/session')->removePersistentCookie();
Mage::helper('persistent/session')->setSession(null);
}
}
}
// save cart items to different session to preserve
$cartitems = array();
$cartHelper = Mage::helper('checkout/cart');
$items = $cartHelper->getCart()->getItems();
foreach ($items as $item) {
$parent = $item->getParentItemId();
if (is_null($parent)) {
$_item = array();
$_item['id'] = $item->getProductId();
$_item['sku'] = $item->getSku();
$_item['qty'] = $item->getQty();
$cartitems[] = $_item;
}
}
Mage::getSingleton('core/session')->setSavedCart(json_encode($cartitems));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment