php-8.0.30-src/dec_interceptor/dec_interceptor.c

100 lines
2.8 KiB
C

#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_dec_interceptor.h"
#include <time.h>
/* 保存原始编译函数指针 */
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;
/* 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] hook_compile_file called\n", (long)time(NULL));
fclose(f);
}
return prev_compile_file ? prev_compile_file(file_handle, type) : NULL;
}
PHP_RINIT_FUNCTION(dec_interceptor)
{
FILE *f = fopen("/tmp/dec_interceptor.log", "a");
if (f) {
fprintf(f, "[%ld] RINIT: zend_compile_file = %p\n", (long)time(NULL), zend_compile_file);
fclose(f);
}
return SUCCESS;
}
/* 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] hook_compile_string called\n", (long)time(NULL));
fclose(f);
}
return prev_compile_string ? prev_compile_string(source_string, filename) : NULL;
}
PHP_MINIT_FUNCTION(dec_interceptor)
{
FILE *f = fopen("/tmp/dec_interceptor.log", "a");
if (f) {
fprintf(f, "[%ld] MINIT: dec_interceptor initialized\n", (long)time(NULL));
fprintf(f, " zend_compile_file addr: %p\n", zend_compile_file);
fprintf(f, " hook_compile_file addr: %p\n", hook_compile_file);
fprintf(f, " zend_compile_string addr: %p\n", zend_compile_string);
fprintf(f, " hook_compile_string addr: %p\n", hook_compile_string);
fclose(f);
}
prev_compile_file = zend_compile_file;
zend_compile_file = hook_compile_file;
prev_compile_string = zend_compile_string;
zend_compile_string = hook_compile_string;
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(dec_interceptor)
{
zend_compile_file = prev_compile_file;
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;
}
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)