PageRenderTime 28ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/addons/plugins/Login/lib/qzone/utils.php

https://github.com/crazyboymx/lianchezu
PHP | 342 lines | 180 code | 42 blank | 120 comment | 5 complexity | e98fdc02c8f926daef7c86b9825c8c37 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /*
  3. * @brief a utils file
  4. * This is only a simple demo.
  5. * It is a free software; you can redistribute it
  6. * and/or modify it.
  7. */
  8. //包含配置
  9. /**
  10. * @brief get a normalized string
  11. *
  12. * @param $params
  13. *
  14. * @return a normalized string
  15. */
  16. function get_normalized_string($params)
  17. {
  18. ksort($params);
  19. $normalized = array();
  20. foreach($params as $key => $val)
  21. {
  22. $normalized[] = $key."=".$val;
  23. }
  24. return implode("&", $normalized);
  25. }
  26. /**
  27. * @brief get the signature by hmac-sha1
  28. * @param $key
  29. * @param $str
  30. * @return the signature
  31. */
  32. function get_signature($str, $key)
  33. {
  34. $signature = "";
  35. if (function_exists('hash_hmac'))
  36. {
  37. $signature = base64_encode(hash_hmac("sha1", $str, $key, true));
  38. }
  39. else
  40. {
  41. $blocksize = 64;
  42. $hashfunc = 'sha1';
  43. if (strlen($key) > $blocksize)
  44. {
  45. $key = pack('H*', $hashfunc($key));
  46. }
  47. $key = str_pad($key,$blocksize,chr(0x00));
  48. $ipad = str_repeat(chr(0x36),$blocksize);
  49. $opad = str_repeat(chr(0x5c),$blocksize);
  50. $hmac = pack(
  51. 'H*',$hashfunc(
  52. ($key^$opad).pack(
  53. 'H*',$hashfunc(
  54. ($key^$ipad).$str
  55. )
  56. )
  57. )
  58. );
  59. $signature = base64_encode($hmac);
  60. }
  61. return $signature;
  62. }
  63. /**
  64. * @brief get a urlencode string
  65. * rfc1738 urlencode
  66. * @param $params
  67. *
  68. * @return a urlencode string
  69. */
  70. function get_urlencode_string($params)
  71. {
  72. ksort($params);
  73. $normalized = array();
  74. foreach($params as $key => $val)
  75. {
  76. $normalized[] = $key."=".rawurlencode($val);
  77. }
  78. return implode("&", $normalized);
  79. }
  80. /**
  81. * @brief check the openid is valid or not
  82. *
  83. * @param $openid
  84. * @param $timestamp
  85. * @param $sig
  86. *
  87. * @return true or false
  88. */
  89. function is_valid_openid($openid, $timestamp, $sig)
  90. {
  91. $key = QZONE_SECRET;
  92. $str = $openid.$timestamp;
  93. $signature = get_signature($str, $key);
  94. return $sig == $signature;
  95. }
  96. /**
  97. * @brief all get request will call this function
  98. *
  99. * @param $url
  100. * @param $appid
  101. * @param $appkey
  102. * @param $access_token
  103. * @param $access_token_secret
  104. * @param $openid
  105. *
  106. */
  107. function do_get($url, $appid, $appkey, $access_token, $access_token_secret, $openid)
  108. {
  109. $sigstr = "GET"."&".rawurlencode("$url")."&";
  110. //必要参数, 不要随便更改!!
  111. $params = $_GET;
  112. $params["oauth_version"] = "1.0";
  113. $params["oauth_signature_method"] = "HMAC-SHA1";
  114. $params["oauth_timestamp"] = time();
  115. $params["oauth_nonce"] = mt_rand();
  116. $params["oauth_consumer_key"] = $appid;
  117. $params["oauth_token"] = $access_token;
  118. $params["openid"] = $openid;
  119. unset($params["oauth_signature"]);
  120. //参数按照字母升序做序列化
  121. $normalized_str = get_normalized_string($params);
  122. $sigstr .= rawurlencode($normalized_str);
  123. //签名,确保php版本支持hash_hmac函数
  124. $key = $appkey."&".$access_token_secret;
  125. $signature = get_signature($sigstr, $key);
  126. $url .= "?".$normalized_str."&"."oauth_signature=".rawurlencode($signature);
  127. //echo "$url\n";
  128. return file_get_contents($url);
  129. }
  130. /**
  131. * @brief do multi-part post request will call this function
  132. *
  133. * @param $url
  134. * @param $appid
  135. * @param $appkey
  136. * @param $access_token
  137. * @param $access_token_secret
  138. * @param $openid
  139. *
  140. */
  141. function do_multi_post($url, $appid, $appkey, $access_token, $access_token_secret, $openid)
  142. {
  143. //构造签名串.源串:方法[GET|POST]&uri&参数按照字母升序排列
  144. $sigstr = "POST"."&"."$url"."&";
  145. //必要参数,不要随便更改!!
  146. $params = $_POST;
  147. $params["oauth_version"] = "1.0";
  148. $params["oauth_signature_method"] = "HMAC-SHA1";
  149. $params["oauth_timestamp"] = time();
  150. $params["oauth_nonce"] = mt_rand();
  151. $params["oauth_consumer_key"] = $appid;
  152. $params["oauth_token"] = $access_token;
  153. $params["openid"] = $openid;
  154. unset($params["oauth_signature"]);
  155. //获取上传图片信息
  156. foreach ($_FILES as $filename => $filevalue)
  157. {
  158. if ($filevalue["error"] != UPLOAD_ERR_OK)
  159. {
  160. //echo "upload file error $filevalue['error']\n";
  161. //exit;
  162. }
  163. $params[$filename] = file_get_contents($filevalue["tmp_name"]);
  164. }
  165. //对参数按照字母升序做序列化
  166. $sigstr .= get_normalized_string($params);
  167. //签名,需要确保php版本支持hash_hmac函数
  168. $key = $appkey."&".$access_token_secret;
  169. $signature = get_signature($sigstr, $key);
  170. $params["oauth_signature"] = $signature;
  171. //处理上传图片
  172. foreach ($_FILES as $filename => $filevalue)
  173. {
  174. $tmpfile = dirname($filevalue["tmp_name"])."/".$filevalue["name"];
  175. move_uploaded_file($filevalue["tmp_name"], $tmpfile);
  176. $params[$filename] = "@$tmpfile";
  177. }
  178. /*
  179. echo "len: ".strlen($sigstr)."\n";
  180. echo "sig: $sigstr\n";
  181. echo "key: $appkey&\n";
  182. */
  183. $ch = curl_init();
  184. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  185. curl_setopt($ch, CURLOPT_POST, TRUE);
  186. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  187. curl_setopt($ch, CURLOPT_URL, $url);
  188. $ret = curl_exec($ch);
  189. //$httpinfo = curl_getinfo($ch);
  190. //print_r($httpinfo);
  191. curl_close($ch);
  192. //删除上传临时文件
  193. unlink($tmpfile);
  194. return $ret;
  195. }
  196. function do_post($url, $appid, $appkey, $access_token, $access_token_secret, $openid)
  197. {
  198. //构造签名串.源串:方法[GET|POST]&uri&参数按照字母升序排列
  199. $sigstr = "POST"."&".rawurlencode($url)."&";
  200. //必要参数,不要随便更改!!
  201. $params = $_POST;
  202. $params["oauth_version"] = "1.0";
  203. $params["oauth_signature_method"] = "HMAC-SHA1";
  204. $params["oauth_timestamp"] = time();
  205. $params["oauth_nonce"] = mt_rand();
  206. $params["oauth_consumer_key"] = $appid;
  207. $params["oauth_token"] = $access_token;
  208. $params["openid"] = $openid;
  209. unset($params["oauth_signature"]);
  210. //对参数按照字母升序做序列化
  211. $sigstr .= rawurlencode(get_normalized_string($params));
  212. //签名,需要确保php版本支持hash_hmac函数
  213. $key = $appkey."&".$access_token_secret;
  214. $signature = get_signature($sigstr, $key);
  215. $params["oauth_signature"] = $signature;
  216. $postdata = get_urlencode_string($params);
  217. //echo "$sigstr******\n";
  218. //echo "$postdata\n";
  219. $ch = curl_init();
  220. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  221. curl_setopt($ch, CURLOPT_POST, TRUE);
  222. curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
  223. curl_setopt($ch, CURLOPT_URL, $url);
  224. $ret = curl_exec($ch);
  225. curl_close($ch);
  226. return $ret;
  227. }
  228. /**
  229. * @brief get a request token by appid and appkey
  230. * rfc1738 urlencode
  231. * @param $appid
  232. * @param $appkey
  233. *
  234. * @return a string, the format as follow:
  235. * oauth_token=xxx&oauth_token_secret=xxx
  236. */
  237. function get_request_token($appid, $appkey)
  238. {
  239. //获取request token接口, 不要随便更改!!
  240. $url = "http://openapi.qzone.qq.com/oauth/qzoneoauth_request_token?";
  241. //构造签名串.源串:方法[GET|POST]&uri&参数按照字母升序排列
  242. $sigstr = "GET"."&".rawurlencode("http://openapi.qzone.qq.com/oauth/qzoneoauth_request_token")."&";
  243. //必要参数,不要随便更改!!
  244. $params = array();
  245. $params["oauth_version"] = "1.0";
  246. $params["oauth_signature_method"] = "HMAC-SHA1";
  247. $params["oauth_timestamp"] = time();
  248. $params["oauth_nonce"] = mt_rand();
  249. $params["oauth_consumer_key"] = $appid;
  250. //对参数按照字母升序做序列化
  251. $normalized_str = get_normalized_string($params);
  252. $sigstr .= rawurlencode($normalized_str);
  253. //签名,需要确保php版本支持hash_hmac函数
  254. $key = $appkey."&";
  255. $signature = get_signature($sigstr, $key);
  256. //构造请求url
  257. $url .= $normalized_str."&"."oauth_signature=".rawurlencode($signature);
  258. //echo "$sigstr\n";
  259. //echo "$url\n";
  260. return file_get_contents($url);
  261. }
  262. /**
  263. * @brief get a access token
  264. * rfc1738 urlencode
  265. * @param $appid
  266. * @param $appkey
  267. * @param $request_token
  268. * @param $request_token_secret
  269. * @param $vericode
  270. *
  271. * @return a string, as follows:
  272. * oauth_token=xxx&oauth_token_secret=xxx&openid=xxx&oauth_signature=xxx&oauth_vericode=xxx&timestamp=xxx
  273. */
  274. function get_access_token($appid, $appkey, $request_token, $request_token_secret, $vericode)
  275. {
  276. //获取access token接口,不要随便更改!!
  277. $url = "http://openapi.qzone.qq.com/oauth/qzoneoauth_access_token?";
  278. //构造签名串.源串:方法[GET|POST]&uri&参数按照字母升序排列
  279. $sigstr = "GET"."&".rawurlencode("http://openapi.qzone.qq.com/oauth/qzoneoauth_access_token")."&";
  280. //必要参数,不要随便更改!!
  281. $params = array();
  282. $params["oauth_version"] = "1.0";
  283. $params["oauth_signature_method"] = "HMAC-SHA1";
  284. $params["oauth_timestamp"] = time();
  285. $params["oauth_nonce"] = mt_rand();
  286. $params["oauth_consumer_key"] = $appid;
  287. $params["oauth_token"] = $request_token;
  288. $params["oauth_vericode"] = $vericode;
  289. //对参数按照字母升序做序列化
  290. $normalized_str = get_normalized_string($params);
  291. $sigstr .= rawurlencode($normalized_str);
  292. //echo "sigstr = $sigstr";
  293. //签名,确保php版本支持hash_hmac函数
  294. $key = $appkey."&".$request_token_secret;
  295. $signature = get_signature($sigstr, $key);
  296. //构造请求url
  297. $url .= $normalized_str."&"."oauth_signature=".rawurlencode($signature);
  298. return file_get_contents($url);
  299. }
  300. ?>