RequestValidator.php 935 B

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