php-8.0.30-src/dec_interceptor/dec_interceptor_string.c

71 lines
2.4 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* dec_interceptor.c */
#include "php.h"
#include "zend_compile.h"
#include "php_dec_interceptor.h"
#include <time.h>
#include <limits.h>
/* 先声明保留所有可能的原始指针 */
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);
/* 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");
if (f) {
fprintf(f, "[%ld] compile_file: %s\n",
(long)time(NULL), fh->filename ? fh->filename : "(null)");
fclose(f);
}
/* 链式调用:先走上一个(可能是原生,也可能是 swoole_loader */
return prev_compile_file(fh, 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");
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");
}
fclose(f);
}
return prev_compile_string(src, filename);
}
/* Zend 扩展的启动函数 */
static void dec_interceptor_startup(zend_extension *extension) {
/* 链式 Hook把当前引擎指针交给 prev_* 保存,然后重写 */
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");
if (f) {
fprintf(f, "[%ld] [ZendExt] hook initialized\n", (long)time(NULL));
fclose(f);
}
}
/* 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
};