Skip to content

Commit a627906

Browse files
jlirochonFabrice Bernhard
authored andcommitted
added support for root domain api keys (ie 'example.com' instead of 'www.local.dev.example.com'). As mentionned on gmap signup page, this is 'usually the best practice, as it will work for all subdomains and directories'
1 parent 37a1a6d commit a627906

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

lib/GMapClient.class.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,24 @@ public static function getAPIKeyByDomain($domain, $api_keys = null)
151151
$api_keys = sfConfig::get('app_google_maps_api_keys');
152152
}
153153

154+
// find root domain (www.local.dev.example.com -> example.com)
155+
$rootDomain = null;
156+
$tokens = explode('.', $domain);
157+
if (sizeof($tokens) > 2)
158+
{
159+
$rootDomain = implode('.', array_slice($tokens, -2, 2));
160+
}
161+
154162
if (is_array($api_keys) && array_key_exists($domain, $api_keys))
155163
{
164+
// exact domain match (ex: www.local.dev.example.com)
156165
$api_key = $api_keys[$domain];
157166
}
167+
elseif ($rootDomain !== null && is_array($api_keys) && array_key_exists($rootDomain, $api_keys))
168+
{
169+
// root domain match (ex: example.com)
170+
$api_key = $api_keys[$rootDomain];
171+
}
158172
else
159173
{
160174
if (is_array($api_keys) && array_key_exists('default', $api_keys))

0 commit comments

Comments
 (0)