This commit is contained in:
hailin 2025-07-30 23:00:18 +08:00
parent 210d32db06
commit 78791de7e9
2 changed files with 20 additions and 25 deletions

View File

@ -3,9 +3,8 @@
#include "ext/standard/info.h" #include "ext/standard/info.h"
#include "php_dec_interceptor.h" #include "php_dec_interceptor.h"
#include <time.h> #include <time.h>
#include <limits.h>
/* 链式保存原始指针 */ /* 保存原始编译函数指针 */
zend_op_array *(*prev_compile_file)(zend_file_handle *file_handle, int type) = NULL; 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; zend_op_array *(*prev_compile_string)(zval *source_string, const char *filename) = NULL;
@ -14,12 +13,10 @@ zend_op_array *hook_compile_file(zend_file_handle *file_handle, int type)
{ {
FILE *f = fopen("/tmp/dec_interceptor.log", "a"); FILE *f = fopen("/tmp/dec_interceptor.log", "a");
if (f) { if (f) {
fprintf(f, "[%ld] compile_file: %s\n", fprintf(f, "[%ld] hook_compile_file called\n", (long)time(NULL));
(long)time(NULL),
file_handle->filename ? file_handle->filename : "(null)");
fclose(f); fclose(f);
} }
return prev_compile_file(file_handle, type); return prev_compile_file ? prev_compile_file(file_handle, type) : NULL;
} }
/* hook zend_compile_string */ /* hook zend_compile_string */
@ -27,40 +24,40 @@ zend_op_array *hook_compile_string(zval *source_string, const char *filename)
{ {
FILE *f = fopen("/tmp/dec_interceptor.log", "a"); FILE *f = fopen("/tmp/dec_interceptor.log", "a");
if (f) { if (f) {
fprintf(f, "[%ld] compile_string: %s\n", fprintf(f, "[%ld] hook_compile_string called\n", (long)time(NULL));
(long)time(NULL),
filename ? 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); fclose(f);
} }
return prev_compile_string(source_string, filename); return prev_compile_string ? prev_compile_string(source_string, filename) : NULL;
} }
PHP_MINIT_FUNCTION(dec_interceptor) PHP_MINIT_FUNCTION(dec_interceptor)
{ {
/* 注册链式钩子 */ prev_compile_file = zend_compile_file;
prev_compile_file = zend_compile_file; zend_compile_file = hook_compile_file;
zend_compile_file = hook_compile_file;
prev_compile_string = zend_compile_string; prev_compile_string = zend_compile_string;
zend_compile_string = hook_compile_string; zend_compile_string = hook_compile_string;
/* 初始化日志 */
FILE *f = fopen("/tmp/dec_interceptor.log", "a"); FILE *f = fopen("/tmp/dec_interceptor.log", "a");
if (f) { if (f) {
fprintf(f, "[%ld] MINIT: chain hooks registered\n", (long)time(NULL)); fprintf(f, "[%ld] MINIT: dec_interceptor initialized\n", (long)time(NULL));
fclose(f); fclose(f);
} }
return SUCCESS; return SUCCESS;
} }
PHP_MSHUTDOWN_FUNCTION(dec_interceptor) PHP_MSHUTDOWN_FUNCTION(dec_interceptor)
{ {
/* 恢复原始指针 */ zend_compile_file = prev_compile_file;
zend_compile_file = prev_compile_file;
zend_compile_string = prev_compile_string; zend_compile_string = prev_compile_string;
FILE *f = fopen("/tmp/dec_interceptor.log", "a");
if (f) {
fprintf(f, "[%ld] MSHUTDOWN: dec_interceptor shutdown\n", (long)time(NULL));
fclose(f);
}
return SUCCESS; return SUCCESS;
} }
@ -71,14 +68,14 @@ PHP_MINFO_FUNCTION(dec_interceptor)
php_info_print_table_end(); php_info_print_table_end();
} }
/* 模块定义 */
zend_module_entry dec_interceptor_module_entry = { zend_module_entry dec_interceptor_module_entry = {
STANDARD_MODULE_HEADER, STANDARD_MODULE_HEADER,
"dec_interceptor", "dec_interceptor",
NULL, NULL,
PHP_MINIT(dec_interceptor), PHP_MINIT(dec_interceptor),
PHP_MSHUTDOWN(dec_interceptor), PHP_MSHUTDOWN(dec_interceptor),
NULL, NULL, NULL,
NULL,
PHP_MINFO(dec_interceptor), PHP_MINFO(dec_interceptor),
PHP_DEC_INTERCEPTOR_VERSION, PHP_DEC_INTERCEPTOR_VERSION,
STANDARD_MODULE_PROPERTIES STANDARD_MODULE_PROPERTIES

View File

@ -9,11 +9,9 @@
extern zend_module_entry dec_interceptor_module_entry; extern zend_module_entry dec_interceptor_module_entry;
#define phpext_dec_interceptor_ptr &dec_interceptor_module_entry #define phpext_dec_interceptor_ptr &dec_interceptor_module_entry
/* 链式保存原始指针 */
extern zend_op_array *(*prev_compile_file)(zend_file_handle *file_handle, int type); 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); extern zend_op_array *(*prev_compile_string)(zval *source_string, const char *filename);
/* 钩子函数声明 */
zend_op_array *hook_compile_file(zend_file_handle *file_handle, int type); 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); zend_op_array *hook_compile_string(zval *source_string, const char *filename);