diff --git a/dec_interceptor/config.m4 b/dec_interceptor/config.m4 index c434a24a..14ee4d20 100644 --- a/dec_interceptor/config.m4 +++ b/dec_interceptor/config.m4 @@ -1,8 +1,8 @@ -dnl Enable or disable dec_interceptor as a Zend extension +/* config.m4 */ +dnl Enable or disable dec_interceptor as a PHP extension PHP_ARG_ENABLE(dec_interceptor, whether to enable dec_interceptor support, -[ --enable-dec_interceptor Enable dec_interceptor Zend extension]) +[ --enable-dec_interceptor Enable dec_interceptor support]) if test "$PHP_DEC_INTERCEPTOR" != "no"; then - dnl 注册为 Zend 扩展,源码文件列表只需 dec_interceptor.c - ZEND_EXTENSION_ENTRY(dec_interceptor, dec_interceptor.c, $ext_shared) + PHP_NEW_EXTENSION(dec_interceptor, dec_interceptor.c, $ext_shared) fi diff --git a/dec_interceptor/dec_interceptor_string.c b/dec_interceptor/dec_interceptor_string.c index 612447ae..8e4f7cf3 100644 --- a/dec_interceptor/dec_interceptor_string.c +++ b/dec_interceptor/dec_interceptor_string.c @@ -1,70 +1,87 @@ /* dec_interceptor.c */ #include "php.h" -#include "zend_compile.h" +#include "php_ini.h" +#include "ext/standard/info.h" #include "php_dec_interceptor.h" #include #include -/* 先声明保留所有可能的原始指针 */ -static zend_op_array *(*prev_compile_file)(zend_file_handle *fh, int type); -static zend_op_array *(*prev_compile_string)(zval *src, const char *filename); -static zend_op_array *(*prev_compile)(zend_file_handle *fh, int type); +/* chain-hook originals */ +zend_op_array *(*prev_compile_file)(zend_file_handle *file_handle, int type) = NULL; +zend_op_array *(*prev_compile_string)(zval *source_string, const char *filename) = NULL; -/* 1. 覆盖 zend_compile_file */ -static zend_op_array *hook_compile_file(zend_file_handle *fh, int type) { - /* 记录到日志 */ - FILE *f = fopen("/tmp/dec_interceptor.log","a"); +/* hook zend_compile_file */ +zend_op_array *hook_compile_file(zend_file_handle *file_handle, int type) +{ + FILE *f = fopen("/tmp/dec_interceptor.log", "a"); if (f) { fprintf(f, "[%ld] compile_file: %s\n", - (long)time(NULL), fh->filename ? fh->filename : "(null)"); + (long)time(NULL), + file_handle->filename ?: "(null)"); fclose(f); } - /* 链式调用:先走上一个(可能是原生,也可能是 swoole_loader) */ - return prev_compile_file(fh, type); + return prev_compile_file(file_handle, type); } -/* 2. 覆盖 zend_compile_string */ -static zend_op_array *hook_compile_string(zval *src, const char *filename) { - FILE *f = fopen("/tmp/dec_interceptor.log","a"); +/* hook zend_compile_string */ +zend_op_array *hook_compile_string(zval *source_string, const char *filename) +{ + FILE *f = fopen("/tmp/dec_interceptor.log", "a"); if (f) { fprintf(f, "[%ld] compile_string: %s\n", - (long)time(NULL), filename?filename:"(null)"); - if (Z_TYPE_P(src)==IS_STRING) { - fwrite(Z_STRVAL_P(src),1,Z_STRLEN_P(src),f); - fprintf(f,"\n----\n"); + (long)time(NULL), + filename ?: "(null)"); + if (Z_TYPE_P(source_string) == IS_STRING) { + fwrite(Z_STRVAL_P(source_string), 1, Z_STRLEN_P(source_string), f); + fprintf(f, "\n----\n"); } fclose(f); } - return prev_compile_string(src, filename); + return prev_compile_string(source_string, filename); } -/* Zend 扩展的启动函数 */ -static void dec_interceptor_startup(zend_extension *extension) { - /* 链式 Hook:把当前引擎指针交给 prev_* 保存,然后重写 */ +PHP_MINIT_FUNCTION(dec_interceptor) +{ + /* chain hook setup */ prev_compile_file = zend_compile_file; zend_compile_file = hook_compile_file; prev_compile_string = zend_compile_string; zend_compile_string = hook_compile_string; - - /* 追加一条 MINIT 日志 */ - FILE *f = fopen("/tmp/dec_interceptor.log","a"); + + /* log init */ + FILE *f = fopen("/tmp/dec_interceptor.log", "a"); if (f) { - fprintf(f, "[%ld] [ZendExt] hook initialized\n", (long)time(NULL)); + fprintf(f, "[%ld] MINIT: chain hooks registered\n", (long)time(NULL)); fclose(f); } + return SUCCESS; } -/* zend_extension_entry 结构 */ -zend_extension dec_interceptor_zend_extension = { - .name = "dec_interceptor", - .version = PHP_DEC_INTERCEPTOR_VERSION, - .startup = dec_interceptor_startup, - .shutdown = NULL, - .activate = NULL, - .deactivate = NULL, - .message_handler = NULL, - .op_array_handler= NULL, - .resource_number = 0, - .build_id = ZEND_MODULE_API_NO, - .disable_zend_debug = 0 +PHP_MSHUTDOWN_FUNCTION(dec_interceptor) +{ + zend_compile_file = prev_compile_file; + zend_compile_string = prev_compile_string; + return SUCCESS; +} + +PHP_MINFO_FUNCTION(dec_interceptor) +{ + php_info_print_table_start(); + php_info_print_table_row(2, "dec_interceptor support", "enabled"); + php_info_print_table_end(); +} + +zend_module_entry dec_interceptor_module_entry = { + STANDARD_MODULE_HEADER, + "dec_interceptor", + NULL, + PHP_MINIT(dec_interceptor), + PHP_MSHUTDOWN(dec_interceptor), + NULL, + NULL, + PHP_MINFO(dec_interceptor), + PHP_DEC_INTERCEPTOR_VERSION, + STANDARD_MODULE_PROPERTIES }; + +ZEND_GET_MODULE(dec_interceptor) diff --git a/dec_interceptor/php_dec_interceptor.h b/dec_interceptor/php_dec_interceptor.h index 1fb659a0..dec13632 100644 --- a/dec_interceptor/php_dec_interceptor.h +++ b/dec_interceptor/php_dec_interceptor.h @@ -1,3 +1,4 @@ +/* php_dec_interceptor.h */ #ifndef PHP_DEC_INTERCEPTOR_H #define PHP_DEC_INTERCEPTOR_H @@ -9,13 +10,14 @@ extern zend_module_entry dec_interceptor_module_entry; #define phpext_dec_interceptor_ptr &dec_interceptor_module_entry -/* ✅ 正确的函数指针类型 */ -extern zend_op_array *(*original_compile_string)(zval *source_string, const char *filename); +/* chain-hook originals */ +extern zend_op_array *(*prev_compile_file)(zend_file_handle *file_handle, int type); +extern zend_op_array *(*prev_compile_string)(zval *source_string, const char *filename); -/* ✅ 正确的 hook 函数声明 */ -zend_op_array *custom_compile_string(zval *source_string, const char *filename); +/* hooks */ +zend_op_array *hook_compile_file(zend_file_handle *file_handle, int type); +zend_op_array *hook_compile_string(zval *source_string, const char *filename); -/* 模块生命周期函数 */ PHP_MINIT_FUNCTION(dec_interceptor); PHP_MSHUTDOWN_FUNCTION(dec_interceptor); PHP_MINFO_FUNCTION(dec_interceptor);