PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/wordpress/wp-content/plugins/wp-connect/OAuth/qq_OAuth.php

http://ownerpress.googlecode.com/
PHP | 328 lines | 210 code | 29 blank | 89 comment | 23 complexity | c5d077b4697ac26209815b65eb0597a9 MD5 | raw file
Possible License(s): Apache-2.0, AGPL-1.0, GPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * ???????
  4. *
  5. * @package sae
  6. * @author Easy Chen
  7. * @version 1.0
  8. */
  9. class qqClient
  10. {
  11. // ????
  12. function __construct( $akey , $skey , $accecss_token , $accecss_token_secret )
  13. {
  14. $this->oauth = new qqOAuth( $akey , $skey , $accecss_token , $accecss_token_secret );
  15. }
  16. function get_ip()
  17. {
  18. if ($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"]) {
  19. $ip = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];
  20. } elseif ($HTTP_SERVER_VARS["HTTP_CLIENT_IP"]) {
  21. $ip = $HTTP_SERVER_VARS["HTTP_CLIENT_IP"];
  22. } elseif ($HTTP_SERVER_VARS["REMOTE_ADDR"]) {
  23. $ip = $HTTP_SERVER_VARS["REMOTE_ADDR"];
  24. } elseif (getenv("HTTP_X_FORWARDED_FOR")) {
  25. $ip = getenv("HTTP_X_FORWARDED_FOR");
  26. } elseif (getenv("HTTP_CLIENT_IP")) {
  27. $ip = getenv("HTTP_CLIENT_IP");
  28. } elseif (getenv("REMOTE_ADDR")) {
  29. $ip = getenv("REMOTE_ADDR");
  30. } else {
  31. $ip = "Unknown";
  32. }
  33. return $ip;
  34. }
  35. // ???????
  36. function show_user( $name )
  37. {
  38. $params = array();
  39. $params['format'] = 'json';
  40. $params['name'] = $name;
  41. return $this->oauth->get( 'http://open.t.qq.com/api/user/other_info' , $params );
  42. }
  43. // ?????????
  44. function user_timeline( $page = 0, $count = 20, $name )
  45. {
  46. $params = array();
  47. $params['format'] = 'json';
  48. $params['name'] = $name;
  49. $params['reqnum'] = $count;
  50. $params['pageflag'] = $page;
  51. return $this->oauth->get('http://open.t.qq.com/api/statuses/user_timeline', $params );
  52. }
  53. // ????????
  54. function followers( $count = 20 , $name )
  55. {
  56. $params = array();
  57. $params['format'] = 'json';
  58. $params['name'] = $name;
  59. $params['reqnum'] = $count;
  60. return $this->oauth->get( 'http://open.t.qq.com/api/friends/user_fanslist' , $params );
  61. }
  62. // ????(???????????)
  63. function update( $text, $value = '' )
  64. {
  65. $params = array();
  66. $params['format'] = 'json';
  67. $params['content'] = $text;
  68. $params['clientip'] = $this -> get_ip();
  69. if ($value[0] == "image" && $value[1]) {
  70. $params['pic'] = $value[1];
  71. return $this -> oauth -> post('http://open.t.qq.com/api/t/add_pic', $params, true);
  72. } elseif ($value[0] == "video") {
  73. $params['url'] = $value[1];
  74. return $this -> oauth -> post('http://open.t.qq.com/api/t/add_video', $params);
  75. } elseif ($value[0] == "music") {
  76. $params['author'] = $value[1];
  77. $params['title'] = $value[2];
  78. $params['url'] = $value[3];
  79. return $this -> oauth -> post('http://open.t.qq.com/api/t/add_music', $params);
  80. } else {
  81. return $this -> oauth -> post('http://open.t.qq.com/api/t/add' , $params);
  82. }
  83. }
  84. // ???????????
  85. function comment( $sid , $text )
  86. {
  87. $params = array();
  88. $params['format'] = 'json';
  89. $params['content'] = $text;
  90. $params['reid'] = $sid;
  91. $params['clientip'] = $this -> get_ip();
  92. return $this -> oauth -> post('http://open.t.qq.com/api/t/comment' , $params);
  93. }
  94. // ??????
  95. function getvideoinfo( $url )
  96. {
  97. $params = array();
  98. $params['format'] = 'json';
  99. $params['url'] = $url;
  100. return $this->oauth->post('http://open.t.qq.com/api/t/getvideoinfo', $params);
  101. }
  102. // ??????
  103. function verify_credentials()
  104. {
  105. $params = array();
  106. $params['format'] = 'json';
  107. return $this->oauth->get( 'http://open.t.qq.com/api/user/info', $params );
  108. }
  109. // ????ID????????
  110. function get_list( $ids )
  111. {
  112. $params = array();
  113. $params['format'] = 'json';
  114. $params['ids'] = $ids;
  115. return $this->oauth->get( 'http://open.t.qq.com/api/t/list', $params );
  116. }
  117. // ???????????
  118. function user_timeline_ids( $page = 0, $count = 20, $name )
  119. {
  120. $params = array();
  121. $params['format'] = 'json';
  122. $params['name'] = $name;
  123. $params['reqnum'] = $count;
  124. $params['pageflag'] = $page;
  125. $params['type'] = 3;
  126. return $this->oauth->get( 'http://open.t.qq.com/api/statuses/user_timeline_ids', $params );
  127. }
  128. }
  129. /**
  130. * ???? OAuth ???
  131. *
  132. * @package sae
  133. * @author Easy Chen
  134. * @version 1.0
  135. */
  136. class qqOAuth {
  137. // Contains the last HTTP status code returned.
  138. public $http_code;
  139. // Contains the last API call.
  140. public $url;
  141. // Set up the API root URL.
  142. public $host = "https://open.t.qq.com/cgi-bin/";
  143. // Set timeout default.
  144. public $timeout = 30;
  145. // Set connect timeout.
  146. public $connecttimeout = 30;
  147. // Verify SSL Cert.
  148. public $ssl_verifypeer = FALSE;
  149. // Respons format.
  150. public $format = 'json';
  151. // Decode returned json data.
  152. public $decode_json = TRUE;
  153. // Contains the last HTTP headers returned.
  154. public $http_info;
  155. // Set the useragnet.
  156. public $useragent = 'qqOAuth v0.0.1';
  157. /* Immediately retry the API call if the response was not successful. */
  158. //public $retry = TRUE;
  159. /**
  160. *Set API URLS
  161. */
  162. function accessTokenURL() { return 'https://open.t.qq.com/cgi-bin/access_token'; }
  163. function authenticateURL() { return 'https://open.t.qq.com/cgi-bin/authenticate'; }
  164. function authorizeURL() { return 'https://open.t.qq.com/cgi-bin/authorize'; }
  165. function requestTokenURL() { return 'https://open.t.qq.com/cgi-bin/request_token'; }
  166. /**
  167. * Debug helpers
  168. */
  169. function lastStatusCode() { return $this->http_status; }
  170. function lastAPICall() { return $this->last_api_call; }
  171. /**
  172. * construct WeiboOAuth object
  173. */
  174. function __construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) {
  175. $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
  176. $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
  177. if (!empty($oauth_token) && !empty($oauth_token_secret)) {
  178. $this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);
  179. } else {
  180. $this->token = NULL;
  181. }
  182. }
  183. /**
  184. * Get a request_token from Weibo
  185. *
  186. * @return array a key/value array containing oauth_token and oauth_token_secret
  187. */
  188. function getRequestToken($oauth_callback = NULL) {
  189. $parameters = array();
  190. if (!empty($oauth_callback)) {
  191. $parameters['oauth_callback'] = $oauth_callback;
  192. }
  193. $request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters);
  194. $token = OAuthUtil::parse_parameters($request);
  195. $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
  196. return $token;
  197. }
  198. /**
  199. * Get the authorize URL
  200. *
  201. * @return string
  202. */
  203. function getAuthorizeURL($token, $sign_in_with_Weibo = TRUE , $url) {
  204. if (is_array($token)) {
  205. $token = $token['oauth_token'];
  206. }
  207. if (empty($sign_in_with_Weibo)) {
  208. return $this->authorizeURL() . "?oauth_token={$token}&oauth_callback=" . urlencode($url);
  209. } else {
  210. return $this->authenticateURL() . "?oauth_token={$token}&oauth_callback=". urlencode($url);
  211. }
  212. }
  213. /**
  214. * Exchange the request token and secret for an access token and
  215. * secret, to sign API calls.
  216. *
  217. * @return array array("oauth_token" => the access token,
  218. * "oauth_token_secret" => the access secret)
  219. */
  220. function getAccessToken($oauth_verifier = FALSE, $oauth_token = false) {
  221. $parameters = array();
  222. if (!empty($oauth_verifier)) {
  223. $parameters['oauth_verifier'] = $oauth_verifier;
  224. }
  225. $request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters);
  226. $token = OAuthUtil::parse_parameters($request);
  227. $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
  228. return $token;
  229. }
  230. /**
  231. * GET wrappwer for oAuthRequest.
  232. *
  233. * @return mixed
  234. */
  235. function get($url, $parameters = array()) {
  236. $response = $this->oAuthRequest($url, 'GET', $parameters);
  237. if ($this->format === 'json' && $this->decode_json) {
  238. return json_decode($response, true);
  239. }
  240. return $response;
  241. }
  242. /**
  243. * POST wreapper for oAuthRequest.
  244. *
  245. * @return mixed
  246. */
  247. function post($url, $parameters = array() , $multi = false) {
  248. $response = $this->oAuthRequest($url, 'POST', $parameters , $multi );
  249. if ($this->format === 'json' && $this->decode_json) {
  250. return json_decode($response, true);
  251. }
  252. return $response;
  253. }
  254. /**
  255. * DELTE wrapper for oAuthReqeust.
  256. *
  257. * @return mixed
  258. */
  259. function delete($url, $parameters = array()) {
  260. $response = $this->oAuthRequest($url, 'DELETE', $parameters);
  261. if ($this->format === 'json' && $this->decode_json) {
  262. return json_decode($response, true);
  263. }
  264. return $response;
  265. }
  266. /**
  267. * Format and sign an OAuth / API request
  268. *
  269. * @return string
  270. */
  271. function oAuthRequest($url, $method, $parameters , $multi = false) {
  272. // echo $url ;
  273. $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
  274. $request->sign_request($this->sha1_method, $this->consumer, $this->token);
  275. switch ($method) {
  276. case 'GET':
  277. //echo $request->to_url();
  278. return $this->http($request->to_url(), 'GET');
  279. default:
  280. return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata($multi) , $multi );
  281. }
  282. }
  283. /**
  284. * Make an HTTP request
  285. *
  286. * @return string API results
  287. */
  288. function http($url, $method, $postfields = null , $multi = false) {
  289. $params = array(
  290. "method" => $method,
  291. "timeout" => $this -> timeout,
  292. "user-agent" => $this -> useragent,
  293. "sslverify" => $this -> ssl_verifypeer,
  294. "body" => $postfields,
  295. "headers" => ($multi) ? array("Content-Type" => "multipart/form-data; boundary=" . OAuthUtil :: $boundary , "Expect: ") : ''
  296. );
  297. return class_http($url, $params);
  298. }
  299. }