/weibo/user_band/modules/baidu_login/lib/sdk/BaiduUtils.class.php

https://github.com/guoxiangke/Vp · PHP · 220 lines · 115 code · 11 blank · 94 comment · 12 complexity · 47ba84e95922dfe3d364c4c8717a2d8b MD5 · raw file

  1. <?php
  2. /***************************************************************************
  3. *
  4. * Copyright (c) 2008 Baidu.com, Inc. All Rights Reserved
  5. *
  6. **************************************************************************/
  7. /**
  8. * Utils functions
  9. *
  10. * @package BaiduOpenAPI
  11. * @author zhujt(zhujianting@baidu.com)
  12. * @version $Revision: 1.10 $
  13. **/
  14. class BaiduUtils
  15. {
  16. /**
  17. * Generate a signature using the application secret key.
  18. *
  19. * The only two entities that know your secret key are you and Baidu,
  20. * according to the Terms of Service. Since nobody else can generate
  21. * the signature, you can rely on it to verify that the information
  22. * came from Baidu.
  23. *
  24. * @param array $params an array of all Baidu-sent parameters,
  25. * NOT INCLUDING the signature itself
  26. * @param string $secret your app's secret key
  27. * @return a hash to be checked against the signature provided by Baidu
  28. */
  29. public static function generate_sig($params, $secret)
  30. {
  31. $str = '';
  32. ksort($params);
  33. //Note: make sure that the signature parameter is not already included in $params.
  34. foreach ($params as $k => $v) {
  35. $str .= "$k=$v";
  36. }
  37. $str .= $secret;
  38. return md5($str);
  39. }
  40. public static function no_magic_quotes($val)
  41. {
  42. if (get_magic_quotes_gpc()) {
  43. return stripslashes($val);
  44. } else {
  45. return $val;
  46. }
  47. }
  48. /**
  49. * @brief urlencode a variable recursively, array keys and object property names will not be
  50. * encoded, so you would better use ASCII to define the array key name or object property name.
  51. *
  52. * @param [in] mixed $var
  53. * @return mixed, with the same variable type
  54. * @retval
  55. * @see
  56. * @note
  57. * @author zhujt
  58. * @date 2009/06/01 14:33:21
  59. **/
  60. public static function urlencode_recursive($var)
  61. {
  62. if (is_array($var)) {
  63. return array_map(array('BaiduUtils', 'urlencode_recursive'), $var);
  64. } elseif (is_object($var)) {
  65. $rvar = null;
  66. foreach ($var as $key => $val) {
  67. $rvar->{$key} = self::urlencode_recursive($val);
  68. }
  69. return $rvar;
  70. } elseif (is_string($var)) {
  71. return urlencode($var);
  72. } else {
  73. return $var;
  74. }
  75. }
  76. /**
  77. * @brief urldecode a variable recursively, array keys and object property names will not be
  78. * decoded, so you would better use ASCII to define the array key name or object property name.
  79. *
  80. * @param [in] mixed $var
  81. * @return mixed, with the same variable type
  82. **/
  83. public static function urldecode_recursive($var)
  84. {
  85. if (is_array($var)) {
  86. return array_map(array('BaiduUtils', 'urldecode_recursive'), $var);
  87. } elseif (is_object($var)) {
  88. $rvar = null;
  89. foreach ($var as $key => $val) {
  90. $rvar->{$key} = self::urldecode_recursive($val);
  91. }
  92. return $rvar;
  93. } elseif (is_string($var)) {
  94. return urldecode($var);
  95. } else {
  96. return $var;
  97. }
  98. }
  99. /**
  100. * @brief base64_encode a variable recursively, array keys and object property names will not be
  101. * encoded, so you would better use ASCII to define the array key name or object property name.
  102. *
  103. * @param [in] mixed $var
  104. * @return mixed, with the same variable type
  105. **/
  106. public static function base64_encode_recursive($var)
  107. {
  108. if (is_array($var)) {
  109. return array_map(array('BaiduUtils', 'base64_encode_recursive'), $var);
  110. } elseif (is_object($var)) {
  111. $rvar = null;
  112. foreach ($var as $key => $val) {
  113. $rvar->{$key} = self::base64_encode_recursive($val);
  114. }
  115. return $rvar;
  116. } elseif (is_string($var)) {
  117. return base64_encode($var);
  118. } else {
  119. return $var;
  120. }
  121. }
  122. /**
  123. * @brief base64_decode a variable recursively, array keys and object property names will not be
  124. * decoded, so you would better use ASCII to define the array key name or object property name.
  125. *
  126. * @param [in] mixed $var
  127. * @return mixed, with the same variable type
  128. **/
  129. public static function base64_decode_recursive($var)
  130. {
  131. if (is_array($var)) {
  132. return array_map(array('BaiduUtils', 'base64_decode_recursive'), $var);
  133. } elseif (is_object($var)) {
  134. $rvar = null;
  135. foreach ($var as $key => $val) {
  136. $rvar->{$key} = self::base64_decode_recursive($val);
  137. }
  138. return $rvar;
  139. } elseif (is_string($var)) {
  140. return base64_decode($var);
  141. } else {
  142. return $var;
  143. }
  144. }
  145. /**
  146. * @brief Encode the GBK format var into json format.
  147. *
  148. * @param [in] mixed $var The value being encoded. Can be any type except a resource.
  149. * @return json format string.
  150. * @note The standard json_encode & json_decode needs all strings be in ASCII or UTF-8 format,
  151. * but most of the time, we use GBK format strings and the standard ones will not work properly,
  152. * by base64_encoded the strings we can change them to ASCII format and let the json_encode &
  153. * json_decode functions work.
  154. **/
  155. public static function json_encode($var)
  156. {
  157. return json_encode(self::base64_encode_recursive($var));
  158. }
  159. /**
  160. * @brief Decode the GBK format var from json format.
  161. *
  162. * @param [in] string $json json formated string
  163. * @param [in] bool $assoc When TRUE, returned objects will be converted into associative arrays.
  164. * @return mixed, associated array with values be urldecoded
  165. * @note The standard json_encode & json_decode needs all strings be in ASCII or UTF-8 format,
  166. * but most of the time, we use GBK format strings and the standard ones will not work properly,
  167. * by base64_encoded the strings we can change them to ASCII format and let the json_encode &
  168. * json_decode functions work.
  169. **/
  170. public static function json_decode($json, $assoc = false)
  171. {
  172. return self::base64_decode_recursive(json_decode($json, $assoc));
  173. }
  174. /**
  175. * @brief Convert string or array to requested character encoding
  176. *
  177. * @param mix $var variable to be converted
  178. * @param string $in_charset The input charset.
  179. * @param string $out_charset The output charset
  180. * @return mix The array with all of the values in it noslashed
  181. * @retval The array with all of the values in it noslashed
  182. * @see http://cn2.php.net/manual/en/function.iconv.php
  183. * @note
  184. * @author zhujt
  185. * @date 2009/03/16 12:17:09
  186. **/
  187. public static function iconv_recursive($var, $in_charset = 'UTF-8', $out_charset = 'GBK')
  188. {
  189. if (is_array($var)) {
  190. $rvar = array();
  191. foreach ($var as $key => $val) {
  192. $rvar[$key] = self::iconv_recursive($val, $in_charset, $out_charset);
  193. }
  194. return $rvar;
  195. } elseif (is_object($var)) {
  196. $rvar = null;
  197. foreach ($var as $key => $val) {
  198. $rvar->{$key} = self::iconv_recursive($val, $in_charset, $out_charset);
  199. }
  200. return $rvar;
  201. } elseif (is_string($var)) {
  202. return iconv($in_charset, $out_charset, $var);
  203. } else {
  204. return $var;
  205. }
  206. }
  207. }
  208. /* vim: set ts=4 sw=4 sts=4 tw=100 noet: */
  209. ?>