Skip to content

fix: PHP 8 Compatibility #148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .github/workflows/integrate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ jobs:
- PHP: "7.4"
OS: "ubuntu-latest"
ZTS: true
- PHP: "8.0"
OS: "ubuntu-latest"
ZTS: true
- PHP: "8.1"
OS: "ubuntu-latest"
ZTS: true
- PHP: "8.2"
OS: "ubuntu-latest"
ZTS: true
- PHP: "8.3"
OS: "ubuntu-latest"
ZTS: true
- PHP: "8.4"
OS: "ubuntu-latest"
ZTS: true
env:
GITHUB: "yes"
enable_debug: "yes"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ beast.log_level支持参数:

通过测试环境:
<pre><code>
Nginx + Fastcgi + (PHP-5.2.x ~ PHP-7.1.x)
Nginx + Fastcgi + (PHP-5.2.x ~ PHP-8.4.x)
</code></pre>

------------------------------
Expand Down
130 changes: 73 additions & 57 deletions beast.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ int beast_is_root = 0;
int beast_pid = -1;

/* True global resources - no need for thread safety here */
static zend_op_array* (*old_compile_file)(zend_file_handle*, int TSRMLS_DC);
static zend_op_array* (*old_compile_file)(zend_file_handle*, int);

static int le_beast;
static int max_cache_size = DEFAULT_CACHE_SIZE;
Expand All @@ -107,11 +107,11 @@ static int beast_debug_mode = 0;
* Every user visible function must have an entry in beast_functions[].
*/
zend_function_entry beast_functions[] = {
PHP_FE(beast_encode_file, NULL)
PHP_FE(beast_avail_cache, NULL)
PHP_FE(beast_support_filesize, NULL)
PHP_FE(beast_file_expire, NULL)
PHP_FE(beast_clean_cache, NULL)
PHP_FE(beast_encode_file, arginfo_beast_encode_file)
PHP_FE(beast_avail_cache, arginfo_beast_avail_cache)
PHP_FE(beast_support_filesize, arginfo_beast_support_filesize)
PHP_FE(beast_file_expire, arginfo_beast_file_expire)
PHP_FE(beast_clean_cache, arginfo_beast_clean_cache)
{NULL, NULL, NULL} /* Must be the last line in beast_functions[] */
};
/* }}} */
Expand Down Expand Up @@ -175,34 +175,41 @@ static int big_endian()
}


int filter_code_comments(char *filename, zval *retval TSRMLS_DC)
int filter_code_comments(char *filename, zval *retval)
{
zend_lex_state original_lex_state;
zend_file_handle file_handle = {0};

#if PHP_API_VERSION > 20090626

php_output_start_default(TSRMLS_C);
php_output_start_default();

file_handle.type = ZEND_HANDLE_FILENAME;
file_handle.filename = filename;
file_handle.free_filename = 0;
#if PHP_API_VERSION > 20200930
file_handle.filename =
zend_string_init((char *)filename, strlen(filename), 0);
#else
file_handle.filename = filename;
#endif
#if PHP_API_VERSION < 20210902
file_handle.free_filename = 0;
#endif
file_handle.opened_path = NULL;

zend_save_lexical_state(&original_lex_state TSRMLS_CC);
if (open_file_for_scanning(&file_handle TSRMLS_CC) == FAILURE) {
zend_restore_lexical_state(&original_lex_state TSRMLS_CC);
php_output_end(TSRMLS_C);
zend_save_lexical_state(&original_lex_state);
if (open_file_for_scanning(&file_handle) == FAILURE) {
zend_restore_lexical_state(&original_lex_state);
php_output_end();
return -1;
}

zend_strip(TSRMLS_C);
zend_strip();

zend_destroy_file_handle(&file_handle TSRMLS_CC);
zend_restore_lexical_state(&original_lex_state TSRMLS_CC);
zend_destroy_file_handle(&file_handle);
zend_restore_lexical_state(&original_lex_state);

php_output_get_contents(retval TSRMLS_CC);
php_output_discard(TSRMLS_C);
php_output_get_contents(retval);
php_output_discard();

#else

Expand All @@ -211,21 +218,21 @@ int filter_code_comments(char *filename, zval *retval TSRMLS_DC)
file_handle.free_filename = 0;
file_handle.opened_path = NULL;

zend_save_lexical_state(&original_lex_state TSRMLS_CC);
if (open_file_for_scanning(&file_handle TSRMLS_CC) == FAILURE) {
zend_restore_lexical_state(&original_lex_state TSRMLS_CC);
zend_save_lexical_state(&original_lex_state);
if (open_file_for_scanning(&file_handle) == FAILURE) {
zend_restore_lexical_state(&original_lex_state);
return -1;
}

php_start_ob_buffer(NULL, 0, 1 TSRMLS_CC);
php_start_ob_buffer(NULL, 0, 1);

zend_strip(TSRMLS_C);
zend_strip();

zend_destroy_file_handle(&file_handle TSRMLS_CC);
zend_restore_lexical_state(&original_lex_state TSRMLS_CC);
zend_destroy_file_handle(&file_handle);
zend_restore_lexical_state(&original_lex_state);

php_ob_get_buffer(retval TSRMLS_CC);
php_end_ob_buffer(0, 0 TSRMLS_CC);
php_ob_get_buffer(retval);
php_end_ob_buffer(0, 0);

#endif

Expand Down Expand Up @@ -253,7 +260,7 @@ struct beast_ops *beast_get_encrypt_algo(int type)

int encrypt_file(const char *inputfile,
const char *outputfile, int expire,
int encrypt_type TSRMLS_DC)
int encrypt_type)
{
php_stream *output_stream = NULL;
zval codes;
Expand All @@ -263,8 +270,8 @@ int encrypt_file(const char *inputfile,
struct beast_ops *encrypt_ops = beast_get_encrypt_algo(encrypt_type);

/* Get php codes from script file */
if (filter_code_comments((char *)inputfile, &codes TSRMLS_CC) == -1) {
php_error_docref(NULL TSRMLS_CC, E_ERROR,
if (filter_code_comments((char *)inputfile, &codes) == -1) {
php_error_docref(NULL, E_ERROR,
"Unable get codes from php file `%s'", inputfile);
return -1;
}
Expand All @@ -286,6 +293,9 @@ int encrypt_file(const char *inputfile,

/* Open output file */
#if ZEND_MODULE_API_NO >= 20151012
#if PHP_API_VERSION > 20200930
#define IGNORE_URL_WIN 0
#endif
output_stream = php_stream_open_wrapper((char *)outputfile, "w+",
IGNORE_URL_WIN|REPORT_ERRORS, NULL);
#else
Expand All @@ -294,7 +304,7 @@ int encrypt_file(const char *inputfile,
#endif

if (!output_stream) {
php_error_docref(NULL TSRMLS_CC, E_ERROR,
php_error_docref(NULL, E_ERROR,
"Unable to open file `%s'", outputfile);
goto failed;
}
Expand All @@ -318,7 +328,7 @@ int encrypt_file(const char *inputfile,
php_stream_write(output_stream, (const char *)&dumptype, INT_SIZE);

if (encrypt_ops->encrypt(inbuf, inlen, &outbuf, &outlen) == -1) {
php_error_docref(NULL TSRMLS_CC, E_ERROR,
php_error_docref(NULL, E_ERROR,
"Unable to encrypt file `%s'", outputfile);
goto failed;
}
Expand Down Expand Up @@ -350,7 +360,7 @@ int encrypt_file(const char *inputfile,

int decrypt_file(const char *filename, int stream,
char **retbuf, int *retlen, int *free_buffer,
struct beast_ops **ret_encrypt TSRMLS_DC)
struct beast_ops **ret_encrypt)
{
struct stat stat_ssb;
cache_key_t findkey;
Expand Down Expand Up @@ -581,7 +591,7 @@ int beast_super_mkdir(char *path)
* CGI compile file
*/
zend_op_array *
cgi_compile_file(zend_file_handle *h, int type TSRMLS_DC)
cgi_compile_file(zend_file_handle *h, int type)
{
#if ZEND_MODULE_API_NO >= 20151012
zend_string *opened_path;
Expand All @@ -596,21 +606,27 @@ cgi_compile_file(zend_file_handle *h, int type TSRMLS_DC)
struct beast_ops *ops = NULL;
int destroy_file_handler = 0;

#if PHP_API_VERSION > 20200930
const char *filename = ZSTR_VAL(h->filename);
#else
const char *filename = h->filename;
#endif

#if 0
fp = zend_fopen(h->filename, &opened_path TSRMLS_CC);
fp = zend_fopen(h->filename, &opened_path);
#else
fp = fopen(h->filename, "rb");
fp = fopen(filename, "rb");
#endif
if (fp != NULL) {
fd = fileno(fp);
} else {
goto final;
}

retval = decrypt_file(h->filename, fd, &buf, &size,
&free_buffer, &ops TSRMLS_CC);
retval = decrypt_file(filename, fd, &buf, &size,
&free_buffer, &ops);
if (retval == -2) {
php_error_docref(NULL TSRMLS_CC, E_ERROR,
php_error_docref(NULL, E_ERROR,
"This program was expired, please contact administrator");
return NULL;
}
Expand All @@ -619,7 +635,7 @@ cgi_compile_file(zend_file_handle *h, int type TSRMLS_DC)
#if BEAST_EXECUTE_NORMAL_SCRIPT
goto final;
#else
php_error_docref(NULL TSRMLS_CC, E_ERROR,
php_error_docref(NULL, E_ERROR,
"Not allow execute normal PHP script");
return NULL;
#endif
Expand Down Expand Up @@ -690,7 +706,7 @@ cgi_compile_file(zend_file_handle *h, int type TSRMLS_DC)
default_file_handler->destroy(default_file_handler);
}

return old_compile_file(h, type TSRMLS_CC);
return old_compile_file(h, type);
}


Expand Down Expand Up @@ -1298,13 +1314,13 @@ PHP_MINIT_FUNCTION(beast)
}

if (validate_networkcard() == -1) {
php_error_docref(NULL TSRMLS_CC, E_ERROR,
php_error_docref(NULL, E_ERROR,
"Not allow run at this computer");
return FAILURE;
}

if ((encrypt_file_header_length + INT_SIZE * 2) > HEADER_MAX_SIZE) {
php_error_docref(NULL TSRMLS_CC, E_ERROR,
php_error_docref(NULL, E_ERROR,
"Header size overflow max size `%d'", HEADER_MAX_SIZE);
return FAILURE;
}
Expand All @@ -1329,13 +1345,13 @@ PHP_MINIT_FUNCTION(beast)
}

if (beast_cache_init(max_cache_size) == -1) {
php_error_docref(NULL TSRMLS_CC,
php_error_docref(NULL,
E_ERROR, "Unable initialize cache for beast");
return FAILURE;
}

if (beast_log_init(beast_log_file, log_level) == -1) {
php_error_docref(NULL TSRMLS_CC,
php_error_docref(NULL,
E_ERROR, "Unable open log file for beast");
return FAILURE;
}
Expand All @@ -1346,13 +1362,13 @@ PHP_MINIT_FUNCTION(beast)

pwd = getpwnam((const char *)beast_log_user);
if (!pwd) {
php_error_docref(NULL TSRMLS_CC,
php_error_docref(NULL,
E_ERROR, "Unable get user passwd information");
return FAILURE;
}

if (beast_log_chown(pwd->pw_uid, pwd->pw_gid) != 0) {
php_error_docref(NULL TSRMLS_CC,
php_error_docref(NULL,
E_ERROR, "Unable change log file owner");
return FAILURE;
}
Expand Down Expand Up @@ -1458,8 +1474,8 @@ PHP_FUNCTION(beast_file_expire)

zend_string *input_file;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S",
&input_file TSRMLS_CC) == FAILURE)
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S",
&input_file) == FAILURE)
{
RETURN_FALSE;
}
Expand All @@ -1469,8 +1485,8 @@ PHP_FUNCTION(beast_file_expire)

#else

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
&file, &file_len TSRMLS_CC) == FAILURE)
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s",
&file, &file_len) == FAILURE)
{
RETURN_FALSE;
}
Expand Down Expand Up @@ -1499,7 +1515,7 @@ PHP_FUNCTION(beast_file_expire)
}

if (expire > 0) {
string = (char *)php_format_date(format, strlen(format), expire, 1 TSRMLS_CC);
string = (char *)php_format_date(format, strlen(format), expire, 1);
BEAST_RETURN_STRING(string, 0);
} else {
BEAST_RETURN_STRING("+Infinity", 1);
Expand All @@ -1526,7 +1542,7 @@ PHP_FUNCTION(beast_encode_file)

zend_string *in, *out;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "SS|ll",
if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS|ll",
&in, &out, &expire, &encrypt_type) == FAILURE)
{
RETURN_FALSE;
Expand All @@ -1539,9 +1555,9 @@ PHP_FUNCTION(beast_encode_file)

#else

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|ll",
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss|ll",
&input, &input_len, &output, &output_len,
&expire, &encrypt_type TSRMLS_CC) == FAILURE)
&expire, &encrypt_type) == FAILURE)
{
RETURN_FALSE;
}
Expand All @@ -1555,7 +1571,7 @@ PHP_FUNCTION(beast_encode_file)
}

ret = encrypt_file(input, output,
(int)expire, (int)encrypt_type TSRMLS_CC);
(int)expire, (int)encrypt_type);
if (ret == -1) {
RETURN_FALSE;
}
Expand Down
20 changes: 20 additions & 0 deletions php_beast.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,26 @@ ZEND_END_MODULE_GLOBALS(beast)
#define BEAST_G(v) (beast_globals.v)
#endif

ZEND_BEGIN_ARG_INFO(arginfo_beast_encode_file, 0)
ZEND_ARG_INFO(0, input)
ZEND_ARG_INFO(0, output)
ZEND_ARG_INFO(0, expire)
ZEND_ARG_INFO(0, encrypt_type)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(arginfo_beast_avail_cache, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(arginfo_beast_support_filesize, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(arginfo_beast_file_expire, 0)
ZEND_ARG_INFO(0, file)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(arginfo_beast_clean_cache, 0)
ZEND_END_ARG_INFO()

#endif /* PHP_BEAST_H */


Expand Down
Loading