PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/phpshop1/WEB-INF/modules/checkout/lib/ps_cyberlib.inc

http://phpshop.googlecode.com/
PHP | 215 lines | 146 code | 34 blank | 35 comment | 30 complexity | 5166a7b4714cafa782adf756c1c63fe6 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /*********************************************************************
  3. * Cyberlib - (C) American Metrocomm Internet Services *
  4. * by Timothy Whitfield <timothy@ametro.net> *
  5. * *
  6. * PHP Cybercash API - This requires that CyberCash support be *
  7. * compiled. *
  8. * *
  9. * *
  10. * This is an attempt to duplicate the cybercash API for PHP *
  11. * users. *
  12. *********************************************************************
  13. * This does not require merchant_conf for reasons that any file *
  14. * can be accessed by the web server can be accessed by any cgi. *
  15. *********************************************************************
  16. */
  17. function SendCC2_1Server($merchant,$merchant_key,
  18. $url,$operation,$CCNVList)
  19. {
  20. /* We need to make the url. */
  21. $url=$url."cr21api.cgi/".$operation;
  22. return SendCCServer($merchant,$merchant_key,$url,$CCNVList);
  23. }
  24. function SendCCServer($merchant,$merchant_key,$url,$CCNVList)
  25. {
  26. /* Break the url into parts. */
  27. $url=parse_url($url);
  28. /* Turn the name value pairs into a url encoded message. */
  29. $pairs="";
  30. $done=0;
  31. $more=list($key,$val)=each($CCNVList);
  32. while(!$done)
  33. {
  34. $pairs.=chop($key)."=".urlencode(chop($val));
  35. $more=list($key,$val)=each($CCNVList);
  36. if($more)
  37. {
  38. $pairs.="&";
  39. }
  40. else
  41. {
  42. $done=1;
  43. }
  44. }
  45. $encrypted_pairs=CCEncrypt($merchant_key,$pairs);
  46. $pairs_length=strlen($encrypted_pairs);
  47. $message=sprintf("POST %s/%s HTTP/1.0\r\n",$url["path"],$merchant);
  48. $message.=sprintf("User-Agent: CCMCK-%s\r\n","3.2.0.5");
  49. $message.="Content-type: application/x-www-form-urlencoded\r\n";
  50. $message.=sprintf("Content-length: %d\r\n",$pairs_length);
  51. $message.="\r\n";
  52. $message.=$encrypted_pairs."\r\n";
  53. $response=CCSocketSend($merchant_key,$url["host"],isset($url["port"])?$url["port"]:"",$message);
  54. return $response;
  55. }
  56. function CCEncrypt($merchant_key,$pairs)
  57. {
  58. $session_key=sprintf("%s #%ld",date("D M j H:i:s Y"),getmypid());
  59. $enc_msg=cybercash_encr($merchant_key,$session_key,$pairs);
  60. $pairs=cybercash_base64_encode($enc_msg["outbuff"]);
  61. $mac=cybercash_base64_encode($enc_msg["macbuff"]);
  62. /* This adds the information needed to decrypt. */
  63. $encrypted_pairs="message=".urlencode($pairs)."&";
  64. $encrypted_pairs.="session-key=".urlencode($session_key)."&";
  65. $encrypted_pairs.="mac=".urlencode($mac);
  66. return $encrypted_pairs;
  67. }
  68. function CCSocketSend($merchant_key,$host,$port,$message)
  69. {
  70. if(!$port)
  71. {
  72. $port="80";
  73. }
  74. /*This opens the port. */
  75. $fd=fsockopen($host,$port);
  76. /* Minor error checking. */
  77. if(!$fd)
  78. {
  79. $vars["MStatus"]="failure-hard";
  80. $vars["MErrMsg"]="Error contacting credit processor.";
  81. return $vars;
  82. }
  83. /*This sends the message. */
  84. fputs($fd,$message);
  85. /* We read the header in and parse at the same time. */
  86. /* Retrieve header. */
  87. $i=0;
  88. $response="";
  89. while(!feof($fd) && $response != "\n")
  90. {
  91. $response="";
  92. $more="";
  93. while(!feof($fd) && $more != "\n")
  94. {
  95. $more=fgetc($fd);
  96. if($more != "\n" || $response=="")
  97. {
  98. if($more != "\r")
  99. {
  100. $response.=$more;
  101. }
  102. }
  103. }
  104. $header[$i++]=$response;
  105. }
  106. /* We will now get the message. */
  107. $message="";
  108. while(!feof($fd))
  109. {
  110. $message.=fgets($fd,50);
  111. }
  112. /* It should be ok to close the socket now. */
  113. fclose($fd);
  114. /* This set of variables is encoded/encrypted. */
  115. $vars=CCGetVars($message);
  116. $vars["message"]=cybercash_base64_decode($vars["message"]);
  117. $vars["mac"]=cybercash_base64_decode($vars["mac"]);
  118. /* Check for errors with the request/decryption. */
  119. /* message is base64 and encrypted. */
  120. $dec_msg=cybercash_decr($merchant_key,$vars["session-key"],
  121. $vars["message"]);
  122. if($dec_msg["errcode"])
  123. {
  124. $vars["MStatus"]="failure-hard";
  125. $vars["MErrMsg"]="Response non-decodable.";
  126. return $vars;
  127. }
  128. if($dec_msg["macbuff"] != $vars["mac"])
  129. {
  130. $vars["MStatus"]="failure-hard";
  131. $vars["MErrMsg"]="Signitures do not match.";
  132. return $vars;
  133. }
  134. /* We will have to parse again to get more info. */
  135. $vars=CCGetVars($dec_msg["outbuff"]);
  136. return $vars;
  137. }
  138. function CCGetVars($message)
  139. {
  140. /* Retrieve variables.
  141. This function retrieves variables in var/value key pairs.
  142. So that $myvar["var"]==value
  143. */
  144. /* Need to find these variables too. */
  145. $cx=0;
  146. $response="";
  147. $more="";
  148. $message.="\n";
  149. $msg_len=strlen($message);
  150. while($cx<=$msg_len)
  151. {
  152. $more="";
  153. $varname="";
  154. $varval="";
  155. while($cx<=$msg_len && $more != "&" && $more != "\n")
  156. {
  157. $more=substr($message,$cx,1);
  158. $cx++;
  159. if($more != "&" && $more != "=" && $more != "\n")
  160. {
  161. $response=$response.$more;
  162. }
  163. if($more=="=")
  164. {
  165. $varname=$response;
  166. $response="";
  167. }
  168. if($more=="&" || $more=="\n")
  169. {
  170. $varval=$response;
  171. $response="";
  172. }
  173. }
  174. if($varname != "")
  175. {
  176. $cybervar[$varname]=urldecode($varval);
  177. }
  178. }
  179. return $cybervar;
  180. }
  181. ?>