forked from geocoder-php/Geocoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoogleMapsBusiness.php
More file actions
98 lines (81 loc) · 2.78 KB
/
Copy pathGoogleMapsBusiness.php
File metadata and controls
98 lines (81 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
/**
* This file is part of the Geocoder package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Geocoder\Provider;
use Ivory\HttpAdapter\HttpAdapterInterface;
/**
* Google Maps for Business
* https://developers.google.com/maps/documentation/business/
*
* @author Jason Bouffard <jpb0104@gmail.com>
*/
class GoogleMapsBusiness extends GoogleMaps implements Provider
{
/**
* @var string
*/
private $clientId;
/**
* @var string
*/
private $privateKey;
/**
* @param HttpAdapterInterface $adapter An HTTP adapter.
* @param string $clientId Your Client ID.
* @param string $privateKey Your Private Key (optional).
* @param string $locale A locale (optional).
* @param string $region Region biasing (optional).
* @param bool $useSsl Whether to use an SSL connection (optional)
*/
public function __construct(HttpAdapterInterface $adapter, $clientId, $privateKey = null, $locale = null, $region = null, $useSsl = false)
{
parent::__construct($adapter, $locale, $region, $useSsl);
$this->clientId = $clientId;
$this->privateKey = $privateKey;
}
/**
* {@inheritDoc}
*/
public function getName()
{
return 'google_maps_business';
}
/**
* {@inheritDoc}
*/
protected function buildQuery($query)
{
$query = parent::buildQuery($query);
$query = sprintf('%s&client=%s', $query, $this->clientId);
if (null !== $this->privateKey) {
$query = $this->signQuery($query);
}
return $query;
}
/**
* Sign a URL with a given crypto key
* Note that this URL must be properly URL-encoded
* src: http://gmaps-samples.googlecode.com/svn/trunk/urlsigning/UrlSigner.php-source
*
* @param string $query Query to be signed
*
* @return string $query Query with signature appended.
*/
private function signQuery($query)
{
$url = parse_url($query);
$urlPartToSign = $url['path'] . '?' . $url['query'];
// Decode the private key into its binary format
$decodedKey = base64_decode(str_replace(array('-', '_'), array('+', '/'), $this->privateKey));
// Create a signature using the private key and the URL-encoded
// string using HMAC SHA1. This signature will be binary.
$signature = hash_hmac('sha1', $urlPartToSign, $decodedKey, true);
$encodedSignature = str_replace(array('+', '/'), array('-', '_'), base64_encode($signature));
return sprintf('%s&signature=%s', $query, $encodedSignature);
}
}