PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/doc/????/youyan/youyan-social-comment-system.2.3.4/youyan-social-comment-system/opent.php

http://ownerpress.googlecode.com/
PHP | 336 lines | 189 code | 21 blank | 126 comment | 28 complexity | 3658a08a24cebcbcd9978a4c5d9f84c0 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. * @param
  5. * @return
  6. * @author tuguska
  7. */
  8. require_once 'OAuth.php';
  9. class MBOpenTOAuth {
  10. public $host = 'http://open.t.qq.com/';
  11. public $timeout = 30;
  12. public $connectTimeout = 30;
  13. public $sslVerifypeer = FALSE;
  14. public $format = MB_RETURN_FORMAT;
  15. public $decodeJson = TRUE;
  16. public $httpInfo;
  17. public $userAgent = 'oauth test';
  18. public $decode_json = FALSE;
  19. function accessTokenURL() { return 'https://open.t.qq.com/cgi-bin/access_token'; }
  20. //function authenticateURL() { return 'http://open.t.qq.com/cgi-bin/authenticate'; }
  21. function authenticateURL() { return 'http://open.t.qq.com/oauth_html/loginnew.php'; }
  22. function authorizeURL() { return 'http://open.t.qq.com/cgi-bin/authorize'; }
  23. function mobelURL() { return 'https://open.t.qq.com/oauth_html/mobel.php'; }
  24. function requestTokenURL() { return 'https://open.t.qq.com/cgi-bin/request_token'; }
  25. function lastStatusCode() { return $this->http_status; }
  26. function __construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) {
  27. $this->sha1_method = new TencentOAuthSignatureMethod_HMAC_SHA1();
  28. $this->consumer = new TencentOAuthConsumer($consumer_key, $consumer_secret);
  29. if (!empty($oauth_token) && !empty($oauth_token_secret)) {
  30. $this->token = new TencentOAuthConsumer($oauth_token, $oauth_token_secret);
  31. } else {
  32. $this->token = NULL;
  33. }
  34. }
  35. /**
  36. * oauth?????????
  37. * ???? oauth_token ?oauth_token_secret?key/value??
  38. */
  39. function getRequestToken($oauth_callback = NULL) {
  40. $parameters = array();
  41. if (!empty($oauth_callback)) {
  42. $parameters['oauth_callback'] = $oauth_callback;
  43. }
  44. $request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters);
  45. $token = TencentOAuthUtil::parse_parameters($request);
  46. $this->token = new TencentOAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
  47. return $token;
  48. }
  49. /**
  50. * ????url
  51. * @return string
  52. */
  53. function getmobelURL($token, $signInWithWeibo = TRUE , $url='') {
  54. if (is_array($token)) {
  55. $token = $token['oauth_token'];
  56. }
  57. if (empty($signInWithWeibo)) {
  58. return $this->mobelURL() . "?oauth_token={$token}";
  59. } else {
  60. return $this->mobelURL() . "?oauth_token={$token}";
  61. }
  62. }
  63. /**
  64. * ????url
  65. * @return string
  66. */
  67. function getAuthorizeURL($token, $signInWithWeibo = TRUE , $url='') {
  68. if (is_array($token)) {
  69. $token = $token['oauth_token'];
  70. }
  71. if (empty($signInWithWeibo)) {
  72. return $this->authorizeURL() . "?oauth_token={$token}";
  73. } else {
  74. return $this->authenticateURL() . "?oauth_token={$token}";
  75. }
  76. }
  77. /**
  78. * ????
  79. * Exchange the request token and secret for an access token and
  80. * secret, to sign API calls.
  81. *
  82. * @return array array("oauth_token" => the access token,
  83. * "oauth_token_secret" => the access secret)
  84. */
  85. function getAccessToken($oauth_verifier = FALSE, $oauth_token = false) {
  86. $parameters = array();
  87. if (!empty($oauth_verifier)) {
  88. $parameters['oauth_verifier'] = $oauth_verifier;
  89. }
  90. $request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters);
  91. $token = TencentOAuthUtil::parse_parameters($request);
  92. $this->token = new TencentOAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
  93. return $token;
  94. }
  95. function jsonDecode($response, $assoc=true) {
  96. //#echo $response;
  97. $response = preg_replace('/[^\x20-\xff]*/', "", $response);
  98. $jsonArr = json_decode($response, $assoc);
  99. if(!is_array($jsonArr))
  100. {
  101. throw new Exception('????!');
  102. }
  103. $ret = $jsonArr["ret"];
  104. $msg = $jsonArr["msg"];
  105. /**
  106. *Ret=0 ????
  107. *Ret=1 ????
  108. *Ret=2 ????
  109. *Ret=3 ????
  110. *Ret=4 ???????
  111. */
  112. switch ($ret) {
  113. case 0:
  114. return $jsonArr;;
  115. break;
  116. case 1:
  117. throw new Exception('????!');
  118. break;
  119. case 2:
  120. throw new Exception('????!');
  121. break;
  122. case 3:
  123. //throw new Exception('????!');
  124. break;
  125. default:
  126. $errcode = $jsonArr["errcode"];
  127. if(isset($errcode)) //????????
  128. {
  129. //throw new Exception("????");
  130. //var_dump($errcode);
  131. break;
  132. //require_once MB_COMM_DIR.'/api_errcode.class.php';
  133. //$msg = ApiErrCode::getMsg($errcode);
  134. }
  135. throw new Exception('???????!');
  136. break;
  137. }
  138. }
  139. /**
  140. * ?????get??.
  141. * @return mixed
  142. */
  143. function get($url, $parameters) {
  144. $response = $this->oAuthRequest($url, 'GET', $parameters);
  145. if (MB_RETURN_FORMAT === 'json') {
  146. return $this->jsonDecode($response, true);
  147. }
  148. return $response;
  149. }
  150. /**
  151. * ?????post??.
  152. * @return mixed
  153. */
  154. function post($url, $parameters = array() , $multi = false) {
  155. #echo 33;
  156. $response = $this->oAuthRequest($url, 'POST', $parameters , $multi );
  157. #echo 44;
  158. #echo MB_RETURN_FORMAT;
  159. #echo $response;
  160. if (MB_RETURN_FORMAT === 'json') {
  161. #echo 'here1';
  162. #var_dump($response);
  163. return $this->jsonDecode($response, true);
  164. }
  165. #echo 'here2';
  166. return $response;
  167. }
  168. /**
  169. * DELTE wrapper for oAuthReqeust.
  170. * @return mixed
  171. */
  172. function delete($url, $parameters = array()) {
  173. $response = $this->oAuthRequest($url, 'DELETE', $parameters);
  174. if (MB_RETURN_FORMAT === 'json') {
  175. return $this->jsonDecode($response, true);
  176. }
  177. return $response;
  178. }
  179. /**
  180. * ????????
  181. * @return string
  182. */
  183. function oAuthRequest($url, $method, $parameters , $multi = false) {
  184. #echo 55;
  185. if (strrpos($url, 'http://') !== 0 && strrpos($url, 'https://') !== 0) {
  186. $url = "{$this->host}{$url}.{$this->format}";
  187. }
  188. #echo 66;
  189. $request = TencentOAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
  190. #echo 77;
  191. $request->sign_request($this->sha1_method, $this->consumer, $this->token);
  192. #echo 88;
  193. switch ($method) {
  194. case 'GET':
  195. return $this->http($request->to_url(), 'GET');
  196. default:
  197. #echo 99;
  198. return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata($multi) , $multi );
  199. }
  200. }
  201. function http($url, $method, $postfields = NULL , $multi = false){
  202. //$https = 0;
  203. //?????https??
  204. if(strrpos($url, 'https://')===0){
  205. $port = 443;
  206. $version = '1.1';
  207. $host = 'ssl://'.MB_API_HOST;
  208. }else{
  209. $port = 80;
  210. $version = '1.0';
  211. $host = MB_API_HOST;
  212. }
  213. $header = "$method $url HTTP/$version\r\n";
  214. $header .= "Host: ".MB_API_HOST."\r\n";
  215. if($multi){
  216. #echo 'here3';
  217. $header .= "Content-Type: multipart/form-data; boundary=" . TencentOAuthUtil::$boundary . "\r\n";
  218. }else{
  219. $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
  220. }
  221. if(strtolower($method) == 'post' ){
  222. $header .= "Content-Length: ".strlen($postfields)."\r\n";
  223. $header .= "Connection: Close\r\n\r\n";
  224. $header .= $postfields;
  225. }else{
  226. $header .= "Connection: Close\r\n\r\n";
  227. }
  228. $ret = '';
  229. $fp = fsockopen($host,$port,$errno,$errstr,30);
  230. if(!$fp){
  231. $error = '??sock????';
  232. throw new Exception($error);
  233. }else{
  234. #echo 't1';
  235. fwrite ($fp, $header);
  236. while (!feof($fp)) {
  237. $ret .= fgets($fp, 4096);
  238. }
  239. #echo 't2';
  240. fclose($fp);
  241. if(strrpos($ret,'Transfer-Encoding: chunked')){
  242. #echo 't3';
  243. $info = @split("\r\n\r\n",$ret);
  244. $response = @split("\r\n",$info[1]);
  245. $t = array_slice($response,1,-1);
  246. $returnInfo = implode('',$t);
  247. }else{
  248. #echo 't4';
  249. #var_dump($ret);
  250. $response = @split("\r\n\r\n",$ret);
  251. $returnInfo = $response[1];
  252. }
  253. //#var_dump($returnInfo);
  254. //??utf-8??
  255. return iconv("utf-8","utf-8//ignore",$returnInfo);
  256. }
  257. }
  258. /*
  259. ??curl??????,??????????
  260. function http($url, $method, $postfields = NULL , $multi = false){
  261. $this->http_info = array();
  262. $ci = curl_init();
  263. curl_setopt($ci, CURLOPT_USERAGENT, $this->userAgent);
  264. curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
  265. curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
  266. curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
  267. curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->sslVerifypeer);
  268. curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
  269. curl_setopt($ci, CURLOPT_HEADER, FALSE);
  270. switch ($method) {
  271. case 'POST':
  272. curl_setopt($ci, CURLOPT_POST, TRUE);
  273. if (!empty($postfields)) {
  274. curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
  275. }
  276. break;
  277. case 'DELETE':
  278. curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
  279. if (!empty($postfields)) {
  280. $url = "{$url}?{$postfields}";
  281. }
  282. }
  283. $header_array = array();
  284. $header_array2=array();
  285. if( $multi )
  286. $header_array2 = array("Content-Type: multipart/form-data; boundary=" . OAuthUtil::$boundary , "Expect: ");
  287. foreach($header_array as $k => $v)
  288. array_push($header_array2,$k.': '.$v);
  289. curl_setopt($ci, CURLOPT_HTTPHEADER, $header_array2 );
  290. curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE );
  291. curl_setopt($ci, CURLOPT_URL, $url);
  292. $response = curl_exec($ci);
  293. $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
  294. $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
  295. $this->url = $url;
  296. print_r($response);
  297. curl_close ($ci);
  298. return $response;
  299. }*/
  300. function getHeader($ch, $header) {
  301. $i = strpos($header, ':');
  302. if (!empty($i)) {
  303. $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
  304. $value = trim(substr($header, $i + 2));
  305. $this->http_header[$key] = $value;
  306. }
  307. return strlen($header);
  308. }
  309. }