编写php的extension实例
来源:互联网 发布:英菲克网络机顶盒好吗 编辑:程序博客网 时间:2024/06/13 14:57
一、说明
二、实际操作
PHP_ARG_ENABLE(icbc, whether to enable ICBC support,
[ --enable-icbc Enable ICBC support])
if test "$PHP_ICBC" = "yes"; then
AC_DEFINE(HAVE_ICBC, 1, [Whether you have ICBC])
if test -f ./libicbcapi.a; then
PHP_ADD_LIBRARY_WITH_PATH(icbcapi, ./, ICBCAPI_SHARED_LIBADD)
PHP_SUBST(ICBCAPI_SHARED_LIBADD)
AC_MSG_RESULT(checking for libicbcapi.a is OK)
else
AC_MSG_RESULT(libicbcapi.a not found)
AC_MSG_ERROR(Please make sure the libicbcapi.a is in current directory)
fi
PHP_NEW_EXTENSION(icbc, icbc.c, $ext_shared)
fi
第三行判断是否要启用icbc扩展,
第五行判断ICBC的静态库是否在当前目录(phpdir/ext/icbc)下
第六、七行将ICBC的静态库加入到编译环境中
#ifndef PHP_ICBC_H
#define PHP_ICBC_H
extern zend_module_entry icbc_module_entry;
#define phpext_icbc_ptr &icbc_module_entry
#ifdef PHP_WIN32
#define PHP_ICBC_API __declspec(dllexport)
#else
#define PHP_ICBC_API
#endif
#ifdef ZTS
#include "TSRM.h"
#endif
PHP_MINIT_FUNCTION(icbc);
PHP_MSHUTDOWN_FUNCTION(icbc);
PHP_RINIT_FUNCTION(icbc);
PHP_RSHUTDOWN_FUNCTION(icbc);
PHP_MINFO_FUNCTION(icbc);
/*Modify youself here*/
PHP_FUNCTION(icbc_sign);
PHP_FUNCTION(icbc_vsign);
PHP_FUNCTION(icbc_getCertID);
/****End of Self control section***/
#ifdef ZTS
#define ICBC_G(v) TSRMG(icbc_globals_id, zend_icbc_globals *, v)
#else
#define ICBC_G(v) (icbc_globals.v)
#endif
#endif /* PHP_ICBC_H */
涉及到我们也就
PHP_FUNCTION(icbc_sign);
PHP_FUNCTION(icbc_vsign);
PHP_FUNCTION(icbc_getCertID);
其他的都是PHP各个状态的入口函数声明
4.编写这三个函数的实现:
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_icbc.h"
#include "icbcapi.h"
static int le_icbc;
zend_function_entry icbc_functions[] = {
PHP_FE(icbc_sign,NULL)
PHP_FE(icbc_vsign,NULL)
PHP_FE(icbc_getCertID,NULL)
{NULL, NULL, NULL} /* Must be the last line in icbc_functions[] */
};
zend_module_entry icbc_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"icbc",
icbc_functions,
PHP_MINIT(icbc),
PHP_MSHUTDOWN(icbc),
PHP_RINIT(icbc), /* Replace with NULL if there's nothing to do at request start */
PHP_RSHUTDOWN(icbc), /* Replace with NULL if there's nothing to do at request end */
PHP_MINFO(icbc),
#if ZEND_MODULE_API_NO >= 20010901
"0.1", /* Replace with version number for your extension */
#endif
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_ICBC
ZEND_GET_MODULE(icbc)
#endif
PHP_MINIT_FUNCTION(icbc)
{
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(icbc)
{
return SUCCESS;
}
PHP_RINIT_FUNCTION(icbc)
{
return SUCCESS;
}
PHP_RSHUTDOWN_FUNCTION(icbc)
{
return SUCCESS;
}
PHP_MINFO_FUNCTION(icbc)
{
php_info_print_table_start();
php_info_print_table_header(2, "icbc support", "enabled");
php_info_print_table_end();
}
PHP_FUNCTION(icbc_sign)
{
char* src;
int srclen;
char* pkey;
int keylen;
char* keypass;
int keypasslen;
char* signedbuf;
int signedbuflen;
FILE* fp;
char key[2000];
int rcc;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,"sss",&src,&srclen,&pkey,&keylen,&keypass,&keypasslen) == FAILURE){
return;
}
fp = fopen(pkey,"rb");
if(fp == NULL)
{
return;
}
fseek(fp,2,SEEK_SET);
fread((void*)key,609,1,fp);
fclose(fp);
if(rcc = sign(src,srclen,key,607,keypass,&signedbuf,&signedbuflen) >= 0){
base64enc(signedbuf,signedbuflen,&signedbuf,&signedbuflen);
src = estrndup(signedbuf,signedbuflen);
if(signedbuf != NULL) infosec_free(signedbuf);
RETURN_STRING(src,1);
}else{
RETURN_LONG(rcc);
}
}PHP_FUNCTION(icbc_vsign)
{
char* src;
int srclen;
char* cert;
int certlen;
char* vsignedbuf;
int vsignedbuflen;
FILE* fp;
char vcert[2000];
int rcc;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,"sss",&src,&srclen,&cert,&certlen,&vsignedbuf,&vsignedbuflen) == FAILURE){
return;
}
fp = fopen(cert,"rb");
if(fp == NULL)
{
return;
}
fread((void*)vcert,1525,1,fp);
fclose(fp);
base64dec(vsignedbuf,vsignedbuflen,&vsignedbuf,&vsignedbuflen);
if(rcc = verifySign(src,srclen,vcert,1525,vsignedbuf,vsignedbuflen) >= 0){
if(vsignedbuf != NULL) infosec_free(vsignedbuf);
RETURN_TRUE;
}else{
if(vsignedbuf != NULL) infosec_free(vsignedbuf);
RETURN_LONG(rcc);
}
}
PHP_FUNCTION(icbc_getCertID)
{
char* arg;
char* certid;
int arg_len,certidlen;
FILE* fp;
char cert[2000];
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,"s", &arg,&arg_len) == FAILURE){
return;
}
fp = fopen(arg,"rb");
if(fp == NULL)
{
return;
}
fread((void*)cert,1525,1,fp);
fclose(fp);
if(!getCertID(cert,1525,&certid,&certidlen))
{
arg = estrndup(certid,certidlen);
if(certid != NULL) infosec_free(certid);
RETURN_STRING(arg,1);
}else{
return;
}
}
$(LIBTOOL) --mode=link $(CC) $(COMMON_FLAGS) $(CFLAGS_CLEAN) $(EXTRA_CFLAGS) $(LDFLAGS) -o $@ -export-dynamic -avoid-version -prefer-pic -module -rpath $(phplibdir) $(EXTRA_LDFLAGS) $(shared_objects_icbc) $(ICBCAPI_SHARED_LIBADD)
改好Makefile后就可以执行
make
如果一切顺利的话会在modules中得到我们的icbc.so,将我们的icbc.so拷贝到/usr/local/lib/php/extensions目录下,然后在php.ini中确认extensions_dir的值是/usr/local/lib/php/extensions,然后加入这句话
extension=icbc.so
重启apache后,就可以在php中直接调用这三个函数了
6.测试程序,要将测试的证书和key文件放到php测试文件的当前目录
<?php
$realpath = dirname(__FILE__);
$key = $realpath."/user.key";
$cert = $realpath."/user.crt";
$src = "zzz";
$passwd = "12345678";
echo "The Cert file information is ";
echo icbc_getCertID($cert);
echo "<br>";
$b64sdata = icbc_sign($src,$key,$passwd);
echo "The string ".$src." encrypt by icbc api is ".$b64sdata."(base64 encoded)<br>";
echo "Now we check it weather is correct....<br>";
if(icbc_vsign("zzz",$cert,$b64sdata)){
echo "The signtrue to ".$src." is right!!<br>";
}else{
echo "The signtrue to ".$src." is wrong!!<br>";
exit();
}
?>
- 编写php的extension实例
- 使用C/C++编写PHP Extension
- PHP Extension的开发基础
- PHP Extension的开发基础
- 为python编写c/c++ 的extension
- X Windows Extension的编写和加载
- PHP Extension
- php extension
- delphi 编写的com 对象 用php调用的实例
- [转]php + xapian extension的安装
- [转]php + xapian extension的安装
- [转]php + xapian extension的安装
- php的sphinx extension安装及测试
- PHP 编写XML DOM 实例
- PHP学习实例—5(日历类的编写)
- PHP调用VC编写的COM组件实例
- PHP编写一个限制实例化次数的类
- PHP调用VC编写的COM组件实例
- escape() 方法:
- 小谈MySQL字符集
- 程序员谈如何掌握计算机专业英语
- Jdk1.5中的新特性 --泛型 (详细版)
- [ASP.NET2.0]Global.asax实现在线人数统计和站点访问总量
- 编写php的extension实例
- 进度0.001%
- 技术
- 剑客与刀客--java vs .net
- php代码转义方法
- 世界历史50大著名统治者
- JNDI简介
- OPENQUERY
- 使用控件的RenderControl()方法导出Excel