Skip to content

Commit d06a2ec

Browse files
committed
兼容 PHP5 PHP7
1 parent af8c246 commit d06a2ec

File tree

4 files changed

+227
-48
lines changed

4 files changed

+227
-48
lines changed

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Ant PHP Extension
2+
3+
PHP 扩展, 用于 PHP-FPM、FastCGI 模式下突破 `disabled_functions`
4+
5+
### 原理
6+
7+
加载该扩展后, 可使用函数 `antsystem` 执行命令(相当于 `system` 别名)
8+
9+
eg:
10+
11+
```php
12+
<?php antsystem('whoami'); ?>
13+
```
14+
15+
### php.ini 样例
16+
17+
```
18+
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
19+
```
20+
21+
### 编译
22+
23+
```bash
24+
$ cd ant_php_extension/
25+
$ phpize && ./configure && make
26+
```
27+
28+
编译完成后当前目录下的 `ant.so` 则是需要的文件
29+
30+
### 安装到本机
31+
32+
```bash
33+
$ cd ant_php_extension/
34+
$ phpize && ./configure && make
35+
$ make install
36+
37+
# 测试
38+
$ php -d enable_dl=On ant.php
39+
```
40+
41+
### 相关链接
42+
43+
* [AntSword](github.com/antswordproject/antsword)

ant.c

Lines changed: 154 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
+----------------------------------------------------------------------+
3-
| PHP Version 7 |
3+
| PHP Version 5, 7 |
44
+----------------------------------------------------------------------+
55
| Copyright (c) 1997-2018 The PHP Group |
66
+----------------------------------------------------------------------+
@@ -12,7 +12,7 @@
1212
| obtain it through the world-wide-web, please send a note to |
1313
| [email protected] so we can mail you a copy immediately. |
1414
+----------------------------------------------------------------------+
15-
| Author: |
15+
| Author: github.com/antswordproject |
1616
+----------------------------------------------------------------------+
1717
*/
1818

@@ -25,6 +25,7 @@
2525
#include "php.h"
2626
#include "php_ini.h"
2727
#include "ext/standard/info.h"
28+
#include "ext/standard/exec.h"
2829
#include "php_ant.h"
2930

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

38+
/* {{{ ext/standard/basic_functions.c for exec.c */
39+
ZEND_BEGIN_ARG_INFO_EX(arginfo_system, 0, 0, 1)
40+
ZEND_ARG_INFO(0, command)
41+
ZEND_ARG_INFO(1, return_value)
42+
ZEND_END_ARG_INFO()
43+
/* }}} */
44+
45+
/* {{{ ant_functions[]
46+
*
47+
* Every user visible function must have an entry in ant_functions[].
48+
*/
49+
const zend_function_entry ant_functions[] = {
50+
PHP_FE(confirm_ant_compiled, NULL) /* For testing, remove later. */
51+
PHP_FE(antsystem, arginfo_system)
52+
PHP_FE_END /* Must be the last line in ant_functions[] */
53+
};
54+
/* }}} */
55+
56+
/* {{{ ant_module_entry
57+
*/
58+
zend_module_entry ant_module_entry = {
59+
#if ZEND_MODULE_API_NO >= 20010901
60+
STANDARD_MODULE_HEADER,
61+
#endif
62+
"ant",
63+
ant_functions,
64+
PHP_MINIT(ant),
65+
PHP_MSHUTDOWN(ant),
66+
PHP_RINIT(ant), /* Replace with NULL if there's nothing to do at request start */
67+
PHP_RSHUTDOWN(ant), /* Replace with NULL if there's nothing to do at request end */
68+
PHP_MINFO(ant),
69+
#if ZEND_MODULE_API_NO >= 20010901
70+
PHP_ANT_VERSION,
71+
#endif
72+
STANDARD_MODULE_PROPERTIES
73+
};
74+
/* }}} */
75+
3776
/* {{{ PHP_INI
3877
*/
3978
/* Remove comments and fill if you need to have entries in php.ini
@@ -53,18 +92,113 @@ PHP_INI_END()
5392
Return a string to confirm that the module is compiled in */
5493
PHP_FUNCTION(confirm_ant_compiled)
5594
{
56-
char *arg = NULL;
57-
size_t arg_len, len;
58-
zend_string *strg;
95+
#if PHP_MAJOR_VERSION == 7
96+
char *arg = NULL;
97+
size_t arg_len, len;
98+
zend_string *strg;
5999

60-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &arg, &arg_len) == FAILURE) {
61-
return;
62-
}
100+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &arg, &arg_len) == FAILURE) {
101+
return;
102+
}
63103

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

66-
RETURN_STR(strg);
67-
}
106+
RETURN_STR(strg);
107+
108+
#elif PHP_MAJOR_VERSION == 5
109+
char *arg = NULL;
110+
int arg_len, len;
111+
char *strg;
112+
113+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
114+
return;
115+
}
116+
117+
len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "ant", arg);
118+
RETURN_STRINGL(strg, len, 0);
119+
#else
120+
#endif
121+
};
122+
/* }}} */
123+
124+
125+
static void ant_php_exec_ex(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{ */
126+
{
127+
char *cmd;
128+
zval *ret_code=NULL, *ret_array=NULL;
129+
int ret;
130+
#if PHP_MAJOR_VERSION == 5
131+
int cmd_len;
132+
133+
if (mode) {
134+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|z/", &cmd, &cmd_len, &ret_code) == FAILURE) {
135+
RETURN_FALSE;
136+
}
137+
} else {
138+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|z/z/", &cmd, &cmd_len, &ret_array, &ret_code) == FAILURE) {
139+
RETURN_FALSE;
140+
}
141+
}
142+
if (!cmd_len) {
143+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot execute a blank command");
144+
RETURN_FALSE;
145+
}
146+
if (!ret_array) {
147+
ret = php_exec(mode, cmd, NULL, return_value TSRMLS_CC);
148+
} else {
149+
if (Z_TYPE_P(ret_array) != IS_ARRAY) {
150+
zval_dtor(ret_array);
151+
array_init(ret_array);
152+
}
153+
ret = php_exec(2, cmd, ret_array, return_value TSRMLS_CC);
154+
}
155+
#elif PHP_MAJOR_VERSION == 7
156+
157+
size_t cmd_len;
158+
159+
ZEND_PARSE_PARAMETERS_START(1, (mode ? 2 : 3))
160+
Z_PARAM_STRING(cmd, cmd_len)
161+
Z_PARAM_OPTIONAL
162+
if (!mode) {
163+
Z_PARAM_ZVAL_DEREF(ret_array)
164+
}
165+
Z_PARAM_ZVAL_DEREF(ret_code)
166+
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
167+
168+
if (!cmd_len) {
169+
php_error_docref(NULL, E_WARNING, "Cannot execute a blank command");
170+
RETURN_FALSE;
171+
}
172+
if (strlen(cmd) != cmd_len) {
173+
php_error_docref(NULL, E_WARNING, "NULL byte detected. Possible attack");
174+
RETURN_FALSE;
175+
}
176+
if (!ret_array) {
177+
ret = php_exec(mode, cmd, NULL, return_value);
178+
} else {
179+
if (Z_TYPE_P(ret_array) != IS_ARRAY) {
180+
zval_ptr_dtor(ret_array);
181+
array_init(ret_array);
182+
} else if (Z_REFCOUNT_P(ret_array) > 1) {
183+
zval_ptr_dtor(ret_array);
184+
ZVAL_ARR(ret_array, zend_array_dup(Z_ARR_P(ret_array)));
185+
}
186+
ret = php_exec(2, cmd, ret_array, return_value);
187+
}
188+
#endif
189+
if (ret_code) {
190+
zval_ptr_dtor(ret_code);
191+
ZVAL_LONG(ret_code, ret);
192+
}
193+
};
194+
/* }}} */
195+
196+
/* {{{ proto int antsystem(string command [, int &return_value])
197+
Execute an external program and display output */
198+
PHP_FUNCTION(antsystem)
199+
{
200+
ant_php_exec_ex(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
201+
};
68202
/* }}} */
69203
/* The previous line is meant for vim and emacs, so it can correctly fold and
70204
unfold functions in source code. See the corresponding marks just before
@@ -111,11 +245,13 @@ PHP_MSHUTDOWN_FUNCTION(ant)
111245
*/
112246
PHP_RINIT_FUNCTION(ant)
113247
{
248+
#if PHP_MAJOR_VERSION == 7
114249
#if defined(COMPILE_DL_ANT) && defined(ZTS)
115-
ZEND_TSRMLS_CACHE_UPDATE();
250+
ZEND_TSRMLS_CACHE_UPDATE();
251+
#endif
116252
#endif
117253
return SUCCESS;
118-
}
254+
};
119255
/* }}} */
120256

121257
/* Remove if there's nothing to do at request end */
@@ -124,7 +260,7 @@ PHP_RINIT_FUNCTION(ant)
124260
PHP_RSHUTDOWN_FUNCTION(ant)
125261
{
126262
return SUCCESS;
127-
}
263+
};
128264
/* }}} */
129265

130266
/* {{{ PHP_MINFO_FUNCTION
@@ -138,39 +274,17 @@ PHP_MINFO_FUNCTION(ant)
138274
/* Remove comments if you have entries in php.ini
139275
DISPLAY_INI_ENTRIES();
140276
*/
141-
}
142-
/* }}} */
143-
144-
/* {{{ ant_functions[]
145-
*
146-
* Every user visible function must have an entry in ant_functions[].
147-
*/
148-
const zend_function_entry ant_functions[] = {
149-
PHP_FE(confirm_ant_compiled, NULL) /* For testing, remove later. */
150-
PHP_FE_END /* Must be the last line in ant_functions[] */
151-
};
152-
/* }}} */
153-
154-
/* {{{ ant_module_entry
155-
*/
156-
zend_module_entry ant_module_entry = {
157-
STANDARD_MODULE_HEADER,
158-
"ant",
159-
ant_functions,
160-
PHP_MINIT(ant),
161-
PHP_MSHUTDOWN(ant),
162-
PHP_RINIT(ant), /* Replace with NULL if there's nothing to do at request start */
163-
PHP_RSHUTDOWN(ant), /* Replace with NULL if there's nothing to do at request end */
164-
PHP_MINFO(ant),
165-
PHP_ANT_VERSION,
166-
STANDARD_MODULE_PROPERTIES
167277
};
168278
/* }}} */
169279

170280
#ifdef COMPILE_DL_ANT
281+
#if PHP_MAJOR_VERSION == 7
282+
171283
#ifdef ZTS
172284
ZEND_TSRMLS_CACHE_DEFINE()
173285
#endif
286+
287+
#endif /* PHP_MAJOR_VERSION */
174288
ZEND_GET_MODULE(ant)
175289
#endif
176290

config.m4

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ dnl without editing.
77

88
dnl If your extension references something external, use with:
99

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

1414
dnl Otherwise use enable:
1515

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

2020
if test "$PHP_ANT" != "no"; then
@@ -77,5 +77,6 @@ if test "$PHP_ANT" != "no"; then
7777
dnl
7878
dnl PHP_SUBST(ANT_SHARED_LIBADD)
7979

80-
PHP_NEW_EXTENSION(ant, ant.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
80+
dnl PHP_NEW_EXTENSION(ant, ant.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
81+
PHP_NEW_EXTENSION(ant, ant.c, $ext_shared)
8182
fi

php_ant.h

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
+----------------------------------------------------------------------+
3-
| PHP Version 7 |
3+
| PHP Version 5, 7 |
44
+----------------------------------------------------------------------+
55
| Copyright (c) 1997-2018 The PHP Group |
66
+----------------------------------------------------------------------+
@@ -12,7 +12,7 @@
1212
| obtain it through the world-wide-web, please send a note to |
1313
| [email protected] so we can mail you a copy immediately. |
1414
+----------------------------------------------------------------------+
15-
| Author: |
15+
| Author: github.com/antswordproject |
1616
+----------------------------------------------------------------------+
1717
*/
1818

@@ -38,6 +38,15 @@ extern zend_module_entry ant_module_entry;
3838
#include "TSRM.h"
3939
#endif
4040

41+
PHP_MINIT_FUNCTION(ant);
42+
PHP_MSHUTDOWN_FUNCTION(ant);
43+
PHP_RINIT_FUNCTION(ant);
44+
PHP_RSHUTDOWN_FUNCTION(ant);
45+
PHP_MINFO_FUNCTION(ant);
46+
47+
PHP_FUNCTION(confirm_ant_compiled);
48+
PHP_FUNCTION(antsystem); /* php system */
49+
4150
/*
4251
Declare any global variables you may need between the BEGIN
4352
and END macros here:
@@ -47,7 +56,7 @@ ZEND_BEGIN_MODULE_GLOBALS(ant)
4756
char *global_string;
4857
ZEND_END_MODULE_GLOBALS(ant)
4958
*/
50-
59+
#if PHP_MAJOR_VERSION == 7
5160
/* Always refer to the globals in your function as ANT_G(variable).
5261
You are encouraged to rename these macros something shorter, see
5362
examples in any other php module directory.
@@ -58,6 +67,18 @@ ZEND_END_MODULE_GLOBALS(ant)
5867
ZEND_TSRMLS_CACHE_EXTERN()
5968
#endif
6069

70+
#elif PHP_MAJOR_VERSION == 5
71+
72+
#ifdef ZTS
73+
#define ANT_G(v) TSRMG(ant_globals_id, zend_ant_globals *, v)
74+
#else
75+
#define ANT_G(v) (ant_globals.v)
76+
#endif
77+
78+
#else /* PHP_MAJOR_VERSION */
79+
80+
#endif /* PHP_MAJOR_VERSION */
81+
6182
#endif /* PHP_ANT_H */
6283

6384
/*

0 commit comments

Comments
 (0)