PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/new88li/openid/2345/lib/Tuan2345Auth.php

http://phpfor.googlecode.com/
PHP | 254 lines | 171 code | 21 blank | 62 comment | 20 complexity | e20805831cf640d7dcac44737dba8e6e MD5 | raw file
  1. <?php
  2. /**
  3. * Tuan2345Auth v1.0.0.1
  4. * @version 1.0.0.1
  5. * @modifytime 2011-03-11
  6. * @author xiongxiaoming
  7. * @contact xiongxm.9991.com@gmail.com
  8. * @example
  9. * // GET request token
  10. * $auth = new Tuan2345Auth();
  11. * $requestToken = $auth->getRequestToken();
  12. * $authorizeURL = $auth->getAuthorizeURL($requestToken,"http://example.com/callback.php")
  13. * header("Location:$authorizeURL");
  14. * // GET access token
  15. * $h = new Tuan2345Auth($request_token,$oauth_token_secret);
  16. * $accessToken = $h->getAccessToken($oauth_verifier) ;
  17. */
  18. include_once("config.php");
  19. include_once("OAuth.php");
  20. class Tuan2345Auth
  21. { /*{{{*/
  22. public $http_code;
  23. public $url;
  24. public $server_domain="api.tuan.2345.com";
  25. public $host = null;
  26. public $timeout = 10;
  27. public $connecttimeout = 30;
  28. public $ssl_verifypeer = FALSE;
  29. public $format = 'json';
  30. public $decode_json = TRUE;
  31. public $http_info;
  32. public $useragent = 'Tuan2345Auth v1.0.0.1 ';
  33. function __construct($oauth_token = NULL, $oauth_token_secret = NULL)
  34. { /*{{{*/
  35. $this->host = "http://".$this->server_domain."/" ;
  36. $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
  37. $this->consumer = new OAuthConsumer(APP_KEY, APP_SECRET);
  38. if (!empty($oauth_token) && !empty($oauth_token_secret))
  39. {
  40. $this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);
  41. }
  42. else
  43. {
  44. $this->token = NULL;
  45. }
  46. } /*}}}*/
  47. /**
  48. * Get a request_token
  49. *
  50. * @return array a key/value array containing oauth_token and oauth_token_secret
  51. */
  52. function getRequestToken($oauth_callback = NULL)
  53. { /*{{{*/
  54. $parameters = array();
  55. if (!empty($oauth_callback)) {
  56. $parameters['oauth_callback'] = $oauth_callback;
  57. }
  58. $request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters);
  59. $token = OAuthUtil::parse_parameters($request);
  60. $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
  61. return $token;
  62. } /*}}}*/
  63. /**
  64. * Get the authorize URL
  65. *
  66. * @return string
  67. */
  68. function getAuthorizeURL($token,$callbackurl)
  69. { /*{{{*/
  70. if (is_array($token))
  71. $token = $token['oauth_token'];
  72. return $this->authorizeURL() . "?oauth_token={$token}&oauth_callback=" . urlencode($callbackurl);
  73. } /*}}}*/
  74. /**
  75. * Exchange the request token and secret for an access token and
  76. * secret, to sign API calls.
  77. *
  78. * @return array array("oauth_token" => the access token,
  79. * "oauth_token_secret" => the access secret)
  80. */
  81. function getAccessToken($oauth_verifier = FALSE, $oauth_token = false)
  82. { /*{{{*/
  83. $parameters = array();
  84. if (!empty($oauth_verifier)) {
  85. $parameters['oauth_verifier'] = $oauth_verifier;
  86. }
  87. $request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters);
  88. $token = OAuthUtil::parse_parameters($request);
  89. $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
  90. return $token;
  91. } /*}}}*/
  92. function get($url, $parameters = array())
  93. { /*{{{*/
  94. $response = $this->oAuthRequest($url, 'GET', $parameters);
  95. if ($this->format === 'json' && $this->decode_json)
  96. {
  97. return json_decode($response, true);
  98. }
  99. return $response;
  100. } /*}}}*/
  101. function post($url, $parameters = array() , $multi = false)
  102. { /*{{{*/
  103. $response = $this->oAuthRequest($url, 'POST', $parameters , $multi );
  104. if ($this->format === 'json' && $this->decode_json) {
  105. return json_decode($response, true);
  106. }
  107. return $response;
  108. } /*}}}*/
  109. function delete($url, $parameters = array())
  110. { /*{{{*/
  111. $response = $this->oAuthRequest($url, 'DELETE', $parameters);
  112. if ($this->format === 'json' && $this->decode_json) {
  113. return json_decode($response, true);
  114. }
  115. return $response;
  116. } /*}}}*/
  117. /**
  118. * Format and sign an OAuth / API request
  119. *
  120. * @return string
  121. */
  122. function oAuthRequest($url, $method, $parameters , $multi = false)
  123. { /*{{{*/
  124. if (strrpos($url, 'http://') !== 0 && strrpos($url, 'http://') !== 0)
  125. {
  126. $url = "{$this->host}{$url}.{$this->format}";
  127. }
  128. // echo $url ;
  129. $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
  130. $request->sign_request($this->sha1_method, $this->consumer, $this->token);
  131. // echo "<pre>";
  132. // var_dump($request);
  133. switch ($method)
  134. {
  135. case 'GET':
  136. //echo $request->to_url(); die('xiongxiaoming');
  137. return $this->http($request->to_url(), 'GET');
  138. default:
  139. return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata($multi) , $multi );
  140. }
  141. } /*}}}*/
  142. function http($url, $method, $postfields = NULL , $multi = false)
  143. { /*{{{*/
  144. $this->http_info = array();
  145. $ci = curl_init();
  146. /* Curl settings */
  147. curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
  148. curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
  149. curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
  150. curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
  151. curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
  152. curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
  153. curl_setopt($ci, CURLOPT_HEADER, FALSE);
  154. switch ($method) {
  155. case 'POST':
  156. curl_setopt($ci, CURLOPT_POST, TRUE);
  157. if (!empty($postfields)) {
  158. curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
  159. //echo "=====post data======\r\n";
  160. //echo $postfields;
  161. }
  162. break;
  163. case 'DELETE':
  164. curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
  165. if (!empty($postfields)) {
  166. $url = "{$url}?{$postfields}";
  167. }
  168. }
  169. $header_array = array();
  170. /*
  171. /////////////
  172. $header_array["FetchUrl"] = $url;
  173. $header_array['TimeStamp'] = date('Y-m-d H:i:s');
  174. $header_array['AccessKey'] = SAE_ACCESSKEY;
  175. $content="FetchUrl";
  176. $content.=$header_array["FetchUrl"];
  177. $content.="TimeStamp";
  178. $content.=$header_array['TimeStamp'];
  179. $content.="AccessKey";
  180. $content.=$header_array['AccessKey'];
  181. $header_array['Signature'] = base64_encode(hash_hmac('sha256',$content, SAE_SECRETKEY ,true));
  182. ////////////
  183. */
  184. $header_array2=array();
  185. if( $multi )
  186. $header_array2 = array("Content-Type: multipart/form-data; boundary=" . OAuthUtil::$boundary , "Expect: ");
  187. foreach($header_array as $k => $v)
  188. array_push($header_array2,$k.': '.$v);
  189. curl_setopt($ci, CURLOPT_HTTPHEADER, $header_array2 );
  190. curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE );
  191. curl_setopt($ci, CURLOPT_URL, $url);
  192. $response = curl_exec($ci);
  193. $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
  194. $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
  195. $this->url = $url;
  196. // echo "<pre>";
  197. // echo '=====info====='."\r\n";
  198. // print_r( curl_getinfo($ci) );
  199. curl_close ($ci);
  200. return $response;
  201. } /*}}}*/
  202. function getHeader($ch, $header)
  203. { /*{{{*/
  204. $i = strpos($header, ':');
  205. if (!empty($i))
  206. {
  207. $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
  208. $value = trim(substr($header, $i + 2));
  209. $this->http_header[$key] = $value;
  210. }
  211. return strlen($header);
  212. } /*}}}*/
  213. function accessTokenURL()
  214. { /*{{{*/
  215. return 'http://'.$this->server_domain.'/oauth/access_token.php';
  216. } /*}}}*/
  217. function authorizeURL()
  218. {/*{{{*/
  219. return 'http://'.$this->server_domain.'/oauth/authorize.php';
  220. } /*}}}*/
  221. function requestTokenURL()
  222. { /*{{{*/
  223. return 'http://'.$this->server_domain.'/oauth/request_token.php';
  224. } /*}}}*/
  225. function lastStatusCode()
  226. { /*{{{*/
  227. return $this->http_status;
  228. } /*}}}*/
  229. function lastAPICall()
  230. { /*{{{*/
  231. return $this->last_api_call;
  232. } /*}}}*/
  233. } /*}}}*/