Skip to content

Instantly share code, notes, and snippets.

@zerolab
Created October 28, 2009 18:11
Show Gist options
  • Save zerolab/220671 to your computer and use it in GitHub Desktop.
Save zerolab/220671 to your computer and use it in GitHub Desktop.
Drupal block visibility depending on the location on the website and geolocation
<?php
// requires the ip2cc module (http://drupal.org/project/ip2cc)
if (!function_exists('drupal_match_path')) {
// copy of http://api.drupal.org/api/function/drupal_match_path/6
function drupal_match_path($path, $patterns) {
static $regexps;
if (!isset($regexps[$patterns])) {
$regexps[$patterns] = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote($patterns, '/')) .')$/';
}
return preg_match($regexps[$patterns], $path);
}
}
$path = drupal_get_path_alias($_GET['q']);
// pages must be separated by new line \n or \r\n
$pages = "page1\npage2/*\n";
$co = ip2cc_get_country($_SERVER['REMOTE_ADDR']);
// COUNTRY_CODE -- e.g. IT for Italy, BY for Belarus. See more at http://www.mathguide.de/info/tools/countrycode.html
if ($co->country_code == "COUNTRY_CODE" && drupal_match_path($path, $pages) ) {
return true;
}
return false;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment