1 : <?php
2 :
3 : class Services_Twilio_RequestValidator
4 : {
5 :
6 : protected $AuthToken;
7 :
8 : function __construct($token)
9 : {
10 1 : $this->AuthToken = $token;
11 1 : }
12 :
13 : public function computeSignature($url, $data = array())
14 : {
15 : // sort the array by keys
16 1 : ksort($data);
17 :
18 : // append them to the data string in order
19 : // with no delimiters
20 1 : foreach($data as $key => $value)
21 1 : $url .= "$key$value";
22 :
23 : // This function calculates the HMAC hash of the data with the key
24 : // passed in
25 : // Note: hash_hmac requires PHP 5 >= 5.1.2 or PECL hash:1.1-1.5
26 : // Or http://pear.php.net/package/Crypt_HMAC/
27 1 : return base64_encode(hash_hmac("sha1", $url, $this->AuthToken, true));
28 : }
29 :
30 : public function validate($expectedSignature, $url, $data = array())
31 : {
32 1 : return $this->computeSignature($url, $data)
33 1 : == $expectedSignature;
34 : }
35 :
|