This commit is contained in:
hailin 2025-07-30 22:55:24 +08:00
parent ecbfced114
commit 210d32db06
3 changed files with 8 additions and 10 deletions

View File

@ -1,4 +1,3 @@
/* 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 support])

View File

@ -1,4 +1,3 @@
/* dec_interceptor.c */
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
@ -6,7 +5,7 @@
#include <time.h>
#include <limits.h>
/* 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;
@ -17,7 +16,7 @@ zend_op_array *hook_compile_file(zend_file_handle *file_handle, int type)
if (f) {
fprintf(f, "[%ld] compile_file: %s\n",
(long)time(NULL),
file_handle->filename ?: "(null)");
file_handle->filename ? file_handle->filename : "(null)");
fclose(f);
}
return prev_compile_file(file_handle, type);
@ -30,7 +29,7 @@ zend_op_array *hook_compile_string(zval *source_string, const char *filename)
if (f) {
fprintf(f, "[%ld] compile_string: %s\n",
(long)time(NULL),
filename ?: "(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");
@ -42,13 +41,13 @@ zend_op_array *hook_compile_string(zval *source_string, const char *filename)
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;
/* log init */
/* 初始化日志 */
FILE *f = fopen("/tmp/dec_interceptor.log", "a");
if (f) {
fprintf(f, "[%ld] MINIT: chain hooks registered\n", (long)time(NULL));
@ -59,6 +58,7 @@ PHP_MINIT_FUNCTION(dec_interceptor)
PHP_MSHUTDOWN_FUNCTION(dec_interceptor)
{
/* 恢复原始指针 */
zend_compile_file = prev_compile_file;
zend_compile_string = prev_compile_string;
return SUCCESS;

View File

@ -1,4 +1,3 @@
/* php_dec_interceptor.h */
#ifndef PHP_DEC_INTERCEPTOR_H
#define PHP_DEC_INTERCEPTOR_H
@ -10,11 +9,11 @@
extern zend_module_entry dec_interceptor_module_entry;
#define phpext_dec_interceptor_ptr &dec_interceptor_module_entry
/* 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);
/* 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);