OAuthUtil.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /** @package verysimple::Authentication */
  3. require_once "oauth/OAuthStore.php";
  4. require_once "oauth/OAuthRequester.php";
  5. require_once "verysimple/String/VerySimpleStringUtil.php";
  6. /**
  7. * A set of utility functions for working with OAuth
  8. *
  9. * @package verysimple::String
  10. * @author Jason Hinkle
  11. * @copyright 1997-2012 VerySimple, Inc.
  12. * @license http://www.gnu.org/licenses/lgpl.html LGPL
  13. * @version 1.0
  14. */
  15. class OAuthUtil
  16. {
  17. /**
  18. * Given a URL return an OAuth signed URL. This will handle creating a timestamp and nonce
  19. *
  20. * @param string $url the unsigned url
  21. * @param string $method request method GET, POST, PUT, DELETE
  22. * @param string $key oauth key
  23. * @param string $secret oauth secret
  24. * @param array $params querystring or post parameters
  25. * @param string $body the body contents of the request
  26. * @param string $signature_method method used for signature (default = 'HMAC_SHA1')
  27. */
  28. public static function SignUrl($url,$method,$key,$secret,$params=null,$body=null,$signature_method='HMAC_SHA1')
  29. {
  30. $options = array('consumer_key' => $key, 'consumer_secret' => $secret);
  31. $params = $params ? $params : array();
  32. OAuthStore::instance("2Leg", $options);
  33. // Obtain a request object for the request we want to make
  34. $request = new OAuthRequester($url, $method, $params, $body);
  35. $sig = $request->sign($key,null,'');
  36. $data = $request->signatureBaseString();
  37. $url = substr( urldecode($data . '&oauth_signature=' . $request->calculateDataSignature($data,$secret,'',$signature_method) ), strlen($method) + 1);
  38. $url = VerySimpleStringUtil::ReplaceFirst('&', '?', $url);
  39. return $url;
  40. }
  41. }
  42. ?>