Skip to content

Commit

Permalink
兼容 PHP5 PHP7
Browse files Browse the repository at this point in the history
  • Loading branch information
Medicean committed Mar 25, 2019
1 parent af8c246 commit d06a2ec
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 48 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Ant PHP Extension

PHP 扩展, 用于 PHP-FPM、FastCGI 模式下突破 `disabled_functions`

### 原理

加载该扩展后, 可使用函数 `antsystem` 执行命令(相当于 `system` 别名)

eg:

```php
<?php antsystem('whoami'); ?>
```

### php.ini 样例

```
disable_functions = pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,exec,shell_exec,popen,proc_open,passthru,symlink,link,syslog,imap_open,ld,mail,system
```

### 编译

```bash
$ cd ant_php_extension/
$ phpize && ./configure && make
```

编译完成后当前目录下的 `ant.so` 则是需要的文件

### 安装到本机

```bash
$ cd ant_php_extension/
$ phpize && ./configure && make
$ make install

# 测试
$ php -d enable_dl=On ant.php
```

### 相关链接

* [AntSword](github.com/antswordproject/antsword)
194 changes: 154 additions & 40 deletions ant.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
| PHP Version 5, 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2018 The PHP Group |
+----------------------------------------------------------------------+
Expand All @@ -12,7 +12,7 @@
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: |
| Author: github.com/antswordproject |
+----------------------------------------------------------------------+
*/

Expand All @@ -25,6 +25,7 @@
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "ext/standard/exec.h"
#include "php_ant.h"

/* If you declare any globals in php_ant.h uncomment this:
Expand All @@ -34,6 +35,44 @@ ZEND_DECLARE_MODULE_GLOBALS(ant)
/* True global resources - no need for thread safety here */
static int le_ant;

/* {{{ ext/standard/basic_functions.c for exec.c */
ZEND_BEGIN_ARG_INFO_EX(arginfo_system, 0, 0, 1)
ZEND_ARG_INFO(0, command)
ZEND_ARG_INFO(1, return_value)
ZEND_END_ARG_INFO()
/* }}} */

/* {{{ ant_functions[]
*
* Every user visible function must have an entry in ant_functions[].
*/
const zend_function_entry ant_functions[] = {
PHP_FE(confirm_ant_compiled, NULL) /* For testing, remove later. */
PHP_FE(antsystem, arginfo_system)
PHP_FE_END /* Must be the last line in ant_functions[] */
};
/* }}} */

/* {{{ ant_module_entry
*/
zend_module_entry ant_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"ant",
ant_functions,
PHP_MINIT(ant),
PHP_MSHUTDOWN(ant),
PHP_RINIT(ant), /* Replace with NULL if there's nothing to do at request start */
PHP_RSHUTDOWN(ant), /* Replace with NULL if there's nothing to do at request end */
PHP_MINFO(ant),
#if ZEND_MODULE_API_NO >= 20010901
PHP_ANT_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
/* }}} */

/* {{{ PHP_INI
*/
/* Remove comments and fill if you need to have entries in php.ini
Expand All @@ -53,18 +92,113 @@ PHP_INI_END()
Return a string to confirm that the module is compiled in */
PHP_FUNCTION(confirm_ant_compiled)
{
char *arg = NULL;
size_t arg_len, len;
zend_string *strg;
#if PHP_MAJOR_VERSION == 7
char *arg = NULL;
size_t arg_len, len;
zend_string *strg;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &arg, &arg_len) == FAILURE) {
return;
}
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &arg, &arg_len) == FAILURE) {
return;
}

strg = strpprintf(0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "ant", arg);
strg = strpprintf(0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "ant", arg);

RETURN_STR(strg);
}
RETURN_STR(strg);

#elif PHP_MAJOR_VERSION == 5
char *arg = NULL;
int arg_len, len;
char *strg;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
return;
}

len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "ant", arg);
RETURN_STRINGL(strg, len, 0);
#else
#endif
};
/* }}} */


static void ant_php_exec_ex(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{ */
{
char *cmd;
zval *ret_code=NULL, *ret_array=NULL;
int ret;
#if PHP_MAJOR_VERSION == 5
int cmd_len;

if (mode) {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|z/", &cmd, &cmd_len, &ret_code) == FAILURE) {
RETURN_FALSE;
}
} else {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|z/z/", &cmd, &cmd_len, &ret_array, &ret_code) == FAILURE) {
RETURN_FALSE;
}
}
if (!cmd_len) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot execute a blank command");
RETURN_FALSE;
}
if (!ret_array) {
ret = php_exec(mode, cmd, NULL, return_value TSRMLS_CC);
} else {
if (Z_TYPE_P(ret_array) != IS_ARRAY) {
zval_dtor(ret_array);
array_init(ret_array);
}
ret = php_exec(2, cmd, ret_array, return_value TSRMLS_CC);
}
#elif PHP_MAJOR_VERSION == 7

size_t cmd_len;

ZEND_PARSE_PARAMETERS_START(1, (mode ? 2 : 3))
Z_PARAM_STRING(cmd, cmd_len)
Z_PARAM_OPTIONAL
if (!mode) {
Z_PARAM_ZVAL_DEREF(ret_array)
}
Z_PARAM_ZVAL_DEREF(ret_code)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);

if (!cmd_len) {
php_error_docref(NULL, E_WARNING, "Cannot execute a blank command");
RETURN_FALSE;
}
if (strlen(cmd) != cmd_len) {
php_error_docref(NULL, E_WARNING, "NULL byte detected. Possible attack");
RETURN_FALSE;
}
if (!ret_array) {
ret = php_exec(mode, cmd, NULL, return_value);
} else {
if (Z_TYPE_P(ret_array) != IS_ARRAY) {
zval_ptr_dtor(ret_array);
array_init(ret_array);
} else if (Z_REFCOUNT_P(ret_array) > 1) {
zval_ptr_dtor(ret_array);
ZVAL_ARR(ret_array, zend_array_dup(Z_ARR_P(ret_array)));
}
ret = php_exec(2, cmd, ret_array, return_value);
}
#endif
if (ret_code) {
zval_ptr_dtor(ret_code);
ZVAL_LONG(ret_code, ret);
}
};
/* }}} */

/* {{{ proto int antsystem(string command [, int &return_value])
Execute an external program and display output */
PHP_FUNCTION(antsystem)
{
ant_php_exec_ex(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
};
/* }}} */
/* The previous line is meant for vim and emacs, so it can correctly fold and
unfold functions in source code. See the corresponding marks just before
Expand Down Expand Up @@ -111,11 +245,13 @@ PHP_MSHUTDOWN_FUNCTION(ant)
*/
PHP_RINIT_FUNCTION(ant)
{
#if PHP_MAJOR_VERSION == 7
#if defined(COMPILE_DL_ANT) && defined(ZTS)
ZEND_TSRMLS_CACHE_UPDATE();
ZEND_TSRMLS_CACHE_UPDATE();
#endif
#endif
return SUCCESS;
}
};
/* }}} */

/* Remove if there's nothing to do at request end */
Expand All @@ -124,7 +260,7 @@ PHP_RINIT_FUNCTION(ant)
PHP_RSHUTDOWN_FUNCTION(ant)
{
return SUCCESS;
}
};
/* }}} */

/* {{{ PHP_MINFO_FUNCTION
Expand All @@ -138,39 +274,17 @@ PHP_MINFO_FUNCTION(ant)
/* Remove comments if you have entries in php.ini
DISPLAY_INI_ENTRIES();
*/
}
/* }}} */

/* {{{ ant_functions[]
*
* Every user visible function must have an entry in ant_functions[].
*/
const zend_function_entry ant_functions[] = {
PHP_FE(confirm_ant_compiled, NULL) /* For testing, remove later. */
PHP_FE_END /* Must be the last line in ant_functions[] */
};
/* }}} */

/* {{{ ant_module_entry
*/
zend_module_entry ant_module_entry = {
STANDARD_MODULE_HEADER,
"ant",
ant_functions,
PHP_MINIT(ant),
PHP_MSHUTDOWN(ant),
PHP_RINIT(ant), /* Replace with NULL if there's nothing to do at request start */
PHP_RSHUTDOWN(ant), /* Replace with NULL if there's nothing to do at request end */
PHP_MINFO(ant),
PHP_ANT_VERSION,
STANDARD_MODULE_PROPERTIES
};
/* }}} */

#ifdef COMPILE_DL_ANT
#if PHP_MAJOR_VERSION == 7

#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
#endif

#endif /* PHP_MAJOR_VERSION */
ZEND_GET_MODULE(ant)
#endif

Expand Down
11 changes: 6 additions & 5 deletions config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ dnl without editing.

dnl If your extension references something external, use with:

dnl PHP_ARG_WITH(ant, for ant support,
dnl Make sure that the comment is aligned:
dnl [ --with-ant Include ant support])
PHP_ARG_WITH(ant, for ant support,
Make sure that the comment is aligned:
[ --with-ant Include ant support])

dnl Otherwise use enable:

PHP_ARG_ENABLE(ant, whether to enable ant support,
dnl Make sure that the comment is aligned:
Make sure that the comment is aligned:
[ --enable-ant Enable ant support])

if test "$PHP_ANT" != "no"; then
Expand Down Expand Up @@ -77,5 +77,6 @@ if test "$PHP_ANT" != "no"; then
dnl
dnl PHP_SUBST(ANT_SHARED_LIBADD)

PHP_NEW_EXTENSION(ant, ant.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
dnl PHP_NEW_EXTENSION(ant, ant.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
PHP_NEW_EXTENSION(ant, ant.c, $ext_shared)
fi
27 changes: 24 additions & 3 deletions php_ant.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
| PHP Version 5, 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2018 The PHP Group |
+----------------------------------------------------------------------+
Expand All @@ -12,7 +12,7 @@
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: |
| Author: github.com/antswordproject |
+----------------------------------------------------------------------+
*/

Expand All @@ -38,6 +38,15 @@ extern zend_module_entry ant_module_entry;
#include "TSRM.h"
#endif

PHP_MINIT_FUNCTION(ant);
PHP_MSHUTDOWN_FUNCTION(ant);
PHP_RINIT_FUNCTION(ant);
PHP_RSHUTDOWN_FUNCTION(ant);
PHP_MINFO_FUNCTION(ant);

PHP_FUNCTION(confirm_ant_compiled);
PHP_FUNCTION(antsystem); /* php system */

/*
Declare any global variables you may need between the BEGIN
and END macros here:
Expand All @@ -47,7 +56,7 @@ ZEND_BEGIN_MODULE_GLOBALS(ant)
char *global_string;
ZEND_END_MODULE_GLOBALS(ant)
*/

#if PHP_MAJOR_VERSION == 7
/* Always refer to the globals in your function as ANT_G(variable).
You are encouraged to rename these macros something shorter, see
examples in any other php module directory.
Expand All @@ -58,6 +67,18 @@ ZEND_END_MODULE_GLOBALS(ant)
ZEND_TSRMLS_CACHE_EXTERN()
#endif

#elif PHP_MAJOR_VERSION == 5

#ifdef ZTS
#define ANT_G(v) TSRMG(ant_globals_id, zend_ant_globals *, v)
#else
#define ANT_G(v) (ant_globals.v)
#endif

#else /* PHP_MAJOR_VERSION */

#endif /* PHP_MAJOR_VERSION */

#endif /* PHP_ANT_H */

/*
Expand Down

0 comments on commit d06a2ec

Please sign in to comment.