PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/firstrend/src/sdks/paipai/Util.php

http://ownerpress.googlecode.com/
PHP | 226 lines | 153 code | 23 blank | 50 comment | 17 complexity | e5ebef60dadbb44dd0db2cb2c655d591 MD5 | raw file
Possible License(s): Apache-2.0, AGPL-1.0, GPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. require_once 'Snoopy.class.php';
  3. class Util {
  4. static private $snoopy = NULL;
  5. /**
  6. * ???Snoopy
  7. */
  8. static public function instanceSnoopy() {
  9. if (self::$snoopy == NULL) {
  10. self::$snoopy = new Snoopy();
  11. }
  12. }
  13. //??cmdid
  14. /*
  15. *???http://api.paipai.com/item/addItem.xhtml??requestURLPath="/item/addItem.xhtml "????cmdid="item.addItem"?
  16. *???http://api.paipai.com/deal/getDeal.xhtml??requestURLPath="/deal/getDeal.xhtml "????cmdid="deal.getDeal"?
  17. */
  18. static public function createCmdid($requestURLPath)
  19. {
  20. if(strlen($requestURLPath)==0)return false;
  21. if($requestURLPath{0} != '/')return false;
  22. if(strpos($requestURLPath,'/')===false)return false;
  23. if(strpos($requestURLPath,'.')===false)return false;
  24. $pos_start = 1;
  25. $pos_end = strpos($requestURLPath,'.');
  26. $cmd = substr($requestURLPath,$pos_start,$pos_end-1);
  27. $cmd = str_replace('/','.',$cmd);
  28. return $cmd;
  29. }
  30. /**
  31. * ????
  32. * @param $paramArr?api????
  33. * @return $sign
  34. */
  35. static public function createSign ($paramArr,$cmdid='') {
  36. ksort($paramArr);
  37. $sign = $cmdid;
  38. foreach ($paramArr as $key => $val) {
  39. if ($key !='' && $val !='') {
  40. $sign .= $key.$val;
  41. }
  42. }
  43. $sign .= PAIPAI_API_SECRETKEY;
  44. $sign = md5($sign);
  45. return $sign;
  46. }
  47. /**
  48. * ???????
  49. * @param $paramArr?api????
  50. * @return $strParam
  51. */
  52. static public function createStrParam ($paramArr) {
  53. $strParam = '';
  54. foreach ($paramArr as $key => $val) {
  55. if ($key != '' && $val !='') {
  56. $strParam .= $key.'='.urlencode($val).'&';
  57. }
  58. }
  59. return $strParam;
  60. }
  61. /**
  62. * ?GET????api??
  63. * @param $paramArr?api????
  64. * @return $result
  65. */
  66. static public function getResult($paramArr,$requestURLPath='') {
  67. self::instanceSnoopy();
  68. //????
  69. $cmdid = self::createCmdid($requestURLPath);
  70. $sign = self::createSign($paramArr,$cmdid);
  71. $strParam = self::createStrParam($paramArr);
  72. $strParam .= 'sign='.$sign;
  73. $strParam = PAIPAI_API_URL.$requestURLPath.'?'.$strParam;
  74. //????
  75. self::$snoopy->fetch($strParam);
  76. //echo "<a href='".$strParam."' style='color:#fff' target='_blank'>??</a>";
  77. $result = self::$snoopy->results;
  78. //????
  79. return $result;
  80. }
  81. /**
  82. * ?POST????api??
  83. * @param $paramArr?api????
  84. * @return $result
  85. */
  86. static public function postResult($paramArr,$requestURLPath) {
  87. self::instanceSnoopy();
  88. //?????Snoopy????submit????????????urlencode???????????get??????????????urlencode??
  89. $cmdid = self::createCmdid($requestURLPath);
  90. $sign = self::createSign($paramArr,$cmdid);
  91. $paramArr['sign'] = $sign;
  92. $api_url = PAIPAI_API_URL . $requestURLPath . "?charset=";
  93. $api_url .= isset($paramArr['charset'])?$paramArr['charset']:'gbk';
  94. //????
  95. self::$snoopy->submit($api_url, $paramArr);
  96. $result = self::$snoopy->results;
  97. //????
  98. return $result;
  99. }
  100. /**
  101. * ?POST????api??????
  102. * @param $paramArr?api????
  103. * @param $imageArr????????????array('pic' => '/tmp/cs.jpg')??
  104. * @return $result
  105. */
  106. static public function postImageResult($paramArr,$requestURLPath,$imageArr) {
  107. self::instanceSnoopy();
  108. //????
  109. $cmdid = self::createCmdid($requestURLPath);
  110. $sign = self::createSign($paramArr,$cmdid);
  111. $paramArr['sign'] = $sign;
  112. $api_url = PAIPAI_API_URL . $requestURLPath . "?charset=";
  113. $api_url .= isset($paramArr['charset'])?$paramArr['charset']:'gbk';
  114. //????
  115. self::$snoopy->_submit_type = "multipart/form-data";
  116. self::$snoopy->submit($api_url,$paramArr,$imageArr);
  117. $result = self::$snoopy->results;
  118. //????
  119. return $result;
  120. }
  121. /**
  122. * ??xml
  123. */
  124. static public function getXmlData ($strXml) {
  125. //ADD BY ROGER???XML??????
  126. $strXml = preg_replace("/[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]/",'',$strXml);
  127. //?????
  128. $strXml = SBC_DBC($strXml,1);
  129. $pos = strpos($strXml, 'xml');
  130. if ($pos !== false) {
  131. $xmlCode=simplexml_load_string($strXml,'SimpleXMLElement', LIBXML_NOCDATA);
  132. $arrayCode=self::get_object_vars_final($xmlCode);
  133. return $arrayCode ;
  134. } else {
  135. return '';
  136. }
  137. }
  138. static private function get_object_vars_final($obj){
  139. if(is_object($obj)){
  140. $obj=get_object_vars($obj);
  141. }
  142. if(is_array($obj)){
  143. foreach ($obj as $key=>$value){
  144. $v = self::get_object_vars_final($value);
  145. //ADD BY ROGER??????????????
  146. if(is_array($v)&&(count($v)==0))$v=NULL;
  147. $obj[$key] = $v;
  148. }
  149. }
  150. return $obj;
  151. }
  152. }
  153. function SBC_DBC($str,$args2) { //??????????????????0,???????????1????????
  154. $DBC = array(
  155. '?' , '?' , '?' , '?' , '?' ,
  156. '?' , '?' , '?' , '?' , '?' ,
  157. '?' , '?' , '?' , '?' , '?' ,
  158. '?' , '?' , '?' , '?' , '?' ,
  159. '?' , '?' , '?' , '?' , '?' ,
  160. '?' , '?' , '?' , '?' , '?' ,
  161. '?' , '?' , '?' , '?' , '?' ,
  162. '?' , '?' , '?' , '?' , '?' ,
  163. '?' , '?' , '?' , '?' , '?' ,
  164. '?' , '?' , '?' , '?' , '?' ,
  165. '?' , '?' , '?' , '?' , '?' ,
  166. '?' , '?' , '?' , '?' , '?' ,
  167. '?' , '?' , '?' , '?' , '?' ,
  168. '?' , '?' , '?' , '?' , '?' ,
  169. '?' , '?' , '?' , '?' , '?' ,
  170. '?' , '?' , '?' , '?' , '?' ,
  171. '?' , '?' , '?' , '?' , '?' ,
  172. '?' , '?' , '?' , '?' , '?' ,
  173. '?' , '?' , '?'
  174. );
  175. $SBC = array( //??
  176. '0', '1', '2', '3', '4',
  177. '5', '6', '7', '8', '9',
  178. 'A', 'B', 'C', 'D', 'E',
  179. 'F', 'G', 'H', 'I', 'J',
  180. 'K', 'L', 'M', 'N', 'O',
  181. 'P', 'Q', 'R', 'S', 'T',
  182. 'U', 'V', 'W', 'X', 'Y',
  183. 'Z', 'a', 'b', 'c', 'd',
  184. 'e', 'f', 'g', 'h', 'i',
  185. 'j', 'k', 'l', 'm', 'n',
  186. 'o', 'p', 'q', 'r', 's',
  187. 't', 'u', 'v', 'w', 'x',
  188. 'y', 'z', '-', ' ', ':',
  189. '.', ',', '/', '%', '#',
  190. '!', '@', '&', '(', ')',
  191. '<', '>', '"', '\'','?',
  192. '[', ']', '{', '}', '\\',
  193. '|', '+', '=', '_', '^',
  194. '$', '~', '`'
  195. );
  196. if($args2==0)
  197. return str_replace($SBC,$DBC,$str); //?????
  198. if($args2==1)
  199. return str_replace($DBC,$SBC,$str); //?????
  200. else
  201. return false;
  202. }
  203. ?>