]> granicus.if.org Git - php/commitdiff
@- Added CyberCash support. (Evan)
authorEvan Klinger <evan@php.net>
Tue, 7 Dec 1999 00:56:25 +0000 (00:56 +0000)
committerEvan Klinger <evan@php.net>
Tue, 7 Dec 1999 00:56:25 +0000 (00:56 +0000)
CyberCash support.

ext/cybercash/Makefile.am [new file with mode: 0644]
ext/cybercash/config.h.stub [new file with mode: 0644]
ext/cybercash/config.m4 [new file with mode: 0644]
ext/cybercash/cybercash.c [new file with mode: 0644]
ext/cybercash/cybercash.h [new file with mode: 0644]
ext/cybercash/cyberlib.php [new file with mode: 0644]
ext/cybercash/test.php [new file with mode: 0644]

diff --git a/ext/cybercash/Makefile.am b/ext/cybercash/Makefile.am
new file mode 100644 (file)
index 0000000..486524e
--- /dev/null
@@ -0,0 +1,2 @@
+noinst_LTLIBRARIES=libphpext_cybercash.la
+libphpext_cybercash_la_SOURCES=cybercash.c
diff --git a/ext/cybercash/config.h.stub b/ext/cybercash/config.h.stub
new file mode 100644 (file)
index 0000000..fb5502d
--- /dev/null
@@ -0,0 +1 @@
+#define HAVE_MCK 0
diff --git a/ext/cybercash/config.m4 b/ext/cybercash/config.m4
new file mode 100644 (file)
index 0000000..110472f
--- /dev/null
@@ -0,0 +1,26 @@
+dnl config.m4 for extension CyberCash
+dnl don't forget to call PHP_EXTENSION(cybercash)
+
+AC_MSG_CHECKING(for CyberCash support)
+AC_ARG_WITH(cybercash,
+[  --with-cybercash[=DIR]  Include CyberCash support.  DIR is the CyberCash MCK 
+                         install directory.],
+[
+  if test "$withval" != "no"; then
+      test -f $withval/mckcrypt.h && MCK_DIR="$withval"
+      test -f $withval/c-api/mckcrypt.h && MCK_DIR="$withval/c-api"
+      if test -n "$MCK_DIR"; then
+       AC_MSG_RESULT(yes)
+       PHP_EXTENSION(cybercash)
+       LIBS="$LIBS -L$withval/lib"
+       AC_ADD_LIBRARY_WITH_PATH(mckcrypto, $MCK_DIR/lib)
+       AC_ADD_INCLUDE($MCK_DIR)
+       AC_DEFINE(HAVE_MCK)
+      else
+        AC_MSG_ERROR(Please reinstall the CyberCash MCK - I cannot find mckcrypt.h)
+        AC_MSG_RESULT(no)
+      fi
+fi
+],[
+  AC_MSG_RESULT(no)
+])
diff --git a/ext/cybercash/cybercash.c b/ext/cybercash/cybercash.c
new file mode 100644 (file)
index 0000000..02335f5
--- /dev/null
@@ -0,0 +1,197 @@
+/*
+   +----------------------------------------------------------------------+
+   | PHP HTML Embedded Scripting Language Version 3.0                     |
+   +----------------------------------------------------------------------+
+   | Copyright (c) 1997,1998 PHP Development Team (See Credits file)      |
+   +----------------------------------------------------------------------+
+   | This program is free software; you can redistribute it and/or modify |
+   | it under the terms of one of the following licenses:                 |
+   |                                                                      |
+   |  A) the GNU General Public License as published by the Free Software |
+   |     Foundation; either version 2 of the License, or (at your option) |
+   |     any later version.                                               |
+   |                                                                      |
+   |  B) the PHP License as published by the PHP Development Team and     |
+   |     included in the distribution in the file: LICENSE                |
+   |                                                                      |
+   | This program is distributed in the hope that it will be useful,      |
+   | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
+   | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
+   | GNU General Public License for more details.                         |
+   |                                                                      |
+   | You should have received a copy of both licenses referred to here.   |
+   | If you did not, or have any questions about PHP licensing, please    |
+   | contact core@php.net.                                                |
+   +----------------------------------------------------------------------+
+   | Authors: Evan Klinger <evan715@sirius.com>                           |
+   |          Timothy Whitfield <timothy@ametro.net>                      |
+   +----------------------------------------------------------------------+
+ */
+
+#include "php.h"
+
+#if HAVE_MCK
+#include "cybercash.h"
+#include "mckcrypt.h"
+
+function_entry cybercash_functions[] = {
+       PHP_FE(cybercash_encr, NULL)
+       PHP_FE(cybercash_decr, NULL)
+       PHP_FE(cybercash_base64_encode, NULL)
+       PHP_FE(cybercash_base64_decode, NULL)
+       {0}
+  };
+
+zend_module_entry cybercash_module_entry = {
+       "CyberCash",
+       cybercash_functions,
+       NULL,NULL,
+       NULL,NULL,
+       NULL,
+       STANDARD_MODULE_PROPERTIES,
+};
+
+PHP_FUNCTION(cybercash_encr)
+{
+       pval **wmk, **sk, **inbuff;
+       unsigned char *outbuff, *macbuff;
+       unsigned int outAlloc, outLth;
+       long errcode;
+  
+       if(ARG_COUNT(ht) != 3 || getParametersEx(3,&wmk,&sk,&inbuff) == FAILURE) {
+               WRONG_PARAM_COUNT;
+       }
+
+       convert_to_string_ex(wmk);
+       convert_to_string_ex(sk);
+       convert_to_string_ex(inbuff);
+
+       outAlloc = (*inbuff)->value.str.len + 10;
+
+  outbuff = (unsigned char *)emalloc(outAlloc);
+  macbuff = (unsigned char *)emalloc(20);
+
+  errcode = mck_encr((*wmk)->value.str.val,(*sk)->value.str.val,
+                       (*inbuff)->value.str.len+1,
+                       (*inbuff)->value.str.val,
+                       outAlloc,
+                       outbuff,
+                       &outLth,
+                       macbuff);
+
+       array_init(return_value);
+
+       add_assoc_long(return_value,"errcode",errcode);
+
+       if(!errcode)
+       {
+               add_assoc_stringl(return_value,"outbuff",outbuff,outLth,0);
+               add_assoc_long(return_value,"outLth",outLth);
+               add_assoc_stringl(return_value,"macbuff",macbuff,20,0);
+       }
+       else
+       {
+               efree(outbuff);
+               efree(macbuff);
+       }
+}
+
+PHP_FUNCTION(cybercash_decr)
+{
+  pval **wmk,**sk,**inbuff;
+  unsigned char *outbuff, *macbuff;
+  unsigned int outAlloc, outLth;
+  long errcode;
+  
+
+  if(ARG_COUNT(ht) != 3 || getParametersEx(3,&wmk,&sk,&inbuff) == FAILURE)
+  {
+    WRONG_PARAM_COUNT;
+  }
+
+  convert_to_string_ex(wmk);
+  convert_to_string_ex(sk);
+  convert_to_string_ex(inbuff);
+
+  outAlloc=(*inbuff)->value.str.len;
+
+  outbuff=(unsigned char *)emalloc(outAlloc);
+  macbuff=(unsigned char *)emalloc(20);
+
+  errcode=mck_decr((*wmk)->value.str.val,
+                   (*sk)->value.str.val,
+                   (*inbuff)->value.str.len,
+                   (*inbuff)->value.str.val,
+                   outAlloc,
+                   outbuff,
+                   &outLth,
+                   macbuff);
+
+       array_init(return_value);
+
+       add_assoc_long(return_value,"errcode",errcode);
+
+       if(!errcode) {
+               add_assoc_stringl(return_value,"outbuff",outbuff,outLth,0);
+               add_assoc_long(return_value,"outLth",outLth);
+               add_assoc_stringl(return_value,"macbuff",macbuff,20,0);
+       }
+       else
+       {
+               efree(outbuff);
+               efree(macbuff);
+       }
+}
+
+PHP_FUNCTION(cybercash_base64_encode)
+{
+  pval **inbuff;
+  char *outbuff;
+  long ret_length;
+
+  if(ARG_COUNT(ht) != 1 ||
+     getParametersEx(1,&inbuff) == FAILURE)
+  {
+    WRONG_PARAM_COUNT;
+  }
+
+  convert_to_string_ex(inbuff);
+
+  outbuff=(char *)emalloc(
+    base64_enc_size((unsigned int)(*inbuff)->value.str.len));
+  
+  ret_length=base64_encode(outbuff,
+     (*inbuff)->value.str.val,(*inbuff)->value.str.len);
+
+  return_value->value.str.val=outbuff;
+  return_value->value.str.len=ret_length;
+  return_value->type=IS_STRING;
+  
+}
+
+PHP_FUNCTION(cybercash_base64_decode)
+{
+  pval **inbuff;
+  char *outbuff;
+  long ret_length;
+
+  if(ARG_COUNT(ht) != 1 ||
+     getParametersEx(1,&inbuff) == FAILURE)
+  {
+    WRONG_PARAM_COUNT;
+  }
+
+  convert_to_string_ex(inbuff);
+
+  outbuff=(char *)emalloc(
+    base64_dec_size((unsigned int)(*inbuff)->value.str.len));
+  
+  ret_length=base64_decode(outbuff,
+     (*inbuff)->value.str.val,(*inbuff)->value.str.len);
+
+  return_value->value.str.val=outbuff;
+  return_value->value.str.len=ret_length;
+  return_value->type=IS_STRING;
+  
+}
+#endif
diff --git a/ext/cybercash/cybercash.h b/ext/cybercash/cybercash.h
new file mode 100644 (file)
index 0000000..27bba3e
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+   +----------------------------------------------------------------------+
+   | PHP HTML Embedded Scripting Language Version 3.0                     |
+   +----------------------------------------------------------------------+
+   | Copyright (c) 1997,1998 PHP Development Team (See Credits file)      |
+   +----------------------------------------------------------------------+
+   | This program is free software; you can redistribute it and/or modify |
+   | it under the terms of one of the following licenses:                 |
+   |                                                                      |
+   |  A) the GNU General Public License as published by the Free Software |
+   |     Foundation; either version 2 of the License, or (at your option) |
+   |     any later version.                                               |
+   |                                                                      |
+   |  B) the PHP License as published by the PHP Development Team and     |
+   |     included in the distribution in the file: LICENSE                |
+   |                                                                      |
+   | This program is distributed in the hope that it will be useful,      |
+   | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
+   | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
+   | GNU General Public License for more details.                         |
+   |                                                                      |
+   | You should have received a copy of both licenses referred to here.   |
+   | If you did not, or have any questions about PHP licensing, please    |
+   | contact core@php.net.                                                |
+   +----------------------------------------------------------------------+
+   | Authors: Evan Klinger <evan715@sirius.com>                           |
+   +----------------------------------------------------------------------+
+ */
+
+#ifndef _CYBERCASH_H
+#define _CYBERCASH_H
+
+
+#if HAVE_MCK
+
+#if PHP_API_VERSION < 19990421
+#define zend_module_entry cybercash_module_entry
+#include "modules.h"
+#include "internal_functions.h"
+#endif
+
+extern zend_module_entry cybercash_module_entry;
+#define cybercash_module_ptr &cybercash_module_entry
+
+PHP_FUNCTION(cybercash_encr);
+PHP_FUNCTION(cybercash_decr);
+PHP_FUNCTION(cybercash_base64_encode);
+PHP_FUNCTION(cybercash_base64_decode);
+
+#else
+#define cybercash_module_ptr NULL
+#endif
+
+#define phpext_cybercash_ptr cybercash_module_ptr
+
+#endif
diff --git a/ext/cybercash/cyberlib.php b/ext/cybercash/cyberlib.php
new file mode 100644 (file)
index 0000000..ef1b1de
--- /dev/null
@@ -0,0 +1,213 @@
+<?php
+/*********************************************************************
+ *    Cyberlib - (C) American Metrocomm Internet Services            *
+ *           by Timothy Whitfield <timothy@ametro.net>               *
+ *                                                                   *
+ *    PHP Cybercash API - This requires that php3_mckcrypt.c be      *
+ *    compiled in along with libmckcrypt from the Cybercash          *
+ *    MCK 3.2.0.3                                                    *
+ *                                                                   *
+ *    This is an attempt to duplicate the cybercash API for PHP3     *
+ *    users.                                                         *
+ *********************************************************************
+ *  This does not require merchant_conf for reasons that any file    *
+ *  can be accessed by the web server can be accessed by any cgi.    *
+ *********************************************************************
+ */
+
+ function SendCC2_1Server($merchant,$merchant_key,
+                          $url,$operation,$CCNVList)
+ {
+   /* We need to make the url. */
+   $url=$url."cr21api.cgi/".$operation;
+
+   return SendCCServer($merchant,$merchant_key,$url,$CCNVList); 
+ }
+ function SendCCServer($merchant,$merchant_key,$url,$CCNVList)
+ {
+   /* Break the url into parts. */
+   $url=parse_url($url);
+   
+   /* Turn the name value pairs into a url encoded message. */
+   $pairs="";
+   $done=0;
+   $more=list($key,$val)=each($CCNVList);
+   while(!$done)
+   {
+     $pairs.=chop($key)."=".urlencode(chop($val));
+     
+     $more=list($key,$val)=each($CCNVList);
+     if($more)
+     {
+       $pairs.="&";
+     }
+     else
+     {
+       $done=1;
+     }
+   }
+
+   $encrypted_pairs=CCEncrypt($merchant_key,$pairs);
+   $pairs_length=strlen($encrypted_pairs);
+   $message=sprintf("POST %s/%s HTTP/1.0\r\n",$url["path"],$merchant);
+   $message.=sprintf("User-Agent: CCMCK-%s\r\n","3.2.0.3");
+   $message.="Content-type: application/x-www-form-urlencoded\r\n";
+   $message.=sprintf("Content-length: %d\r\n",$pairs_length);
+   $message.="\r\n";
+   $message.=$encrypted_pairs."\r\n";
+   $response=CCSocketSend($merchant_key,$url["host"],$url["port"],$message);
+   return $response;
+ }
+
+ function CCEncrypt($merchant_key,$pairs)
+ {
+   $session_key=sprintf("%s #%ld",date("D M j H:i:s Y"),getmypid());
+   $enc_msg=mck_encr($merchant_key,$session_key,$pairs);
+   $pairs=mck_base64_encode($enc_msg["outbuff"]);
+   $mac=mck_base64_encode($enc_msg["macbuff"]);
+   
+   /* This adds the information needed to decrypt. */
+   $encrypted_pairs="message=".urlencode($pairs)."&";
+   $encrypted_pairs.="session-key=".urlencode($session_key)."&";
+   $encrypted_pairs.="mac=".urlencode($mac);
+   
+   
+   return $encrypted_pairs;
+ }
+ function CCSocketSend($merchant_key,$host,$port,$message)
+ {
+   if(!$port)
+   {
+     $port="80";
+   }
+   /*This opens the port. */
+   $fd=fsockopen($host,$port);
+
+   /* Minor error checking. */
+   if(!$fd)
+   {
+     $vars["MStatus"]="failure-hard";
+     $vars["MErrMsg"]="Error contacting credit processor.";
+
+     return $vars;
+   }
+
+   /*This sends the message. */
+   fputs($fd,$message);
+   
+   /* We read the header in and parse at the same time. */
+
+   /* Retrieve header. */
+   $i=0;
+   $response="";
+   while(!feof($fd) && $response != "\n")
+   {
+     $response="";
+     $more="";
+     while(!feof($fd) && $more != "\n")
+     {
+       $more=fgetc($fd);
+       if($more != "\n" || $response=="")
+       {
+         if($more != "\r")
+         {
+           $response.=$more;
+         }
+       }
+     }
+     $header[$i++]=$response;
+   }
+
+   /* We will now get the message. */
+   $message="";
+   while(!feof($fd))
+   {
+     $message.=fgets($fd,50);
+   }
+   /* It should be ok to close the socket now. */
+   fclose($fd);
+   
+   /* This set of variables is encoded/encrypted. */
+   $vars=CCGetVars($message);
+   $vars["message"]=mck_base64_decode($vars["message"]);
+   $vars["mac"]=mck_base64_decode($vars["mac"]);
+   
+   /* Check for errors with the request/decryption. */
+   /* message is base64 and encrypted. */
+   $dec_msg=mck_decr($merchant_key,$vars["session-key"],
+                     $vars["message"]);
+
+   if($dec_msg["errcode"])
+   {
+     $vars["MStatus"]="failure-hard";
+     $vars["MErrMsg"]="Response non-decodable.";
+     return $vars;
+   }
+
+   if($dec_msg["macbuff"] != $vars["mac"])
+   {
+     $vars["MStatus"]="failure-hard";
+     $vars["MErrMsg"]="Signitures do not match.";
+     return $vars;
+   }
+   
+   /* We will have to parse again to get more info. */
+   $vars=CCGetVars($dec_msg["outbuff"]);
+   
+   return $vars;
+ }
+ function CCGetVars($message)
+ {
+   /* Retrieve variables. 
+      This function retrieves variables in var/value key pairs.
+      So that $myvar["var"]==value
+   */
+   
+   /* Need to find these variables too. */
+   $cx=0;
+   $response="";
+   $more="";
+   $message.="\n";
+   $msg_len=strlen($message);
+
+   while($cx<=$msg_len)
+   {
+     $more="";
+     $varname="";
+     $varval="";
+     while($cx<=$msg_len && $more != "&" && $more != "\n")
+     {
+       $more=substr($message,$cx,1);
+       $cx++;
+       if($more != "&" && $more != "=" && $more != "\n")
+       {
+         $response=$response.$more;
+       }
+       if($more=="=")
+       {
+         $varname=$response;
+         $response="";
+       }
+       if($more=="&" || $more=="\n")
+       {
+         $varval=$response;
+         $response="";
+       }
+     }
+     
+     if($varname != "")
+     {
+       $cybervar[$varname]=urldecode($varval);
+     }
+   }
+  
+   return $cybervar;
+ }
+?>
diff --git a/ext/cybercash/test.php b/ext/cybercash/test.php
new file mode 100644 (file)
index 0000000..efa034f
--- /dev/null
@@ -0,0 +1,26 @@
+<?php
+ require "cyberlib.php3";
+
+ $merchant=""; /* Your merchant ID goes here. */
+ $merchant_key=""; /* Your merchant key goes here. */
+ $payment_url="http://cr.cybercash.com/cgi-bin/";
+ $auth_type="mauthonly";
+ $response=SendCC2_1Server($merchant,$merchant_key,$payment_url,
+                $auth_type,array("Order-ID" => "2342322",
+                "Amount" => "usd 11.50",
+                "Card-Number" => "4111111111111111",
+                "Card-Address" => "1600 Pennsylvania Avenue",
+                "Card-City" => "Washington",
+                "Card-State" => "DC",
+                "Card-Zip" => "20500",
+                "Card-Country" => "USA",
+                "Card-Exp" => "12/99",
+                "Card-Name" => "Bill Clinton"));
+
+  while(list($key,$val)=each($response))
+  {
+    echo $key."=".$val."<br>";
+  }
+
+?>