11/*
22 +----------------------------------------------------------------------+
3- | PHP Version 7 |
3+ | PHP Version 5, 7 |
44 +----------------------------------------------------------------------+
55 | Copyright (c) 1997-2018 The PHP Group |
66 +----------------------------------------------------------------------+
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
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 */
3536static 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 */
5493PHP_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 */
112246PHP_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)
124260PHP_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
172284ZEND_TSRMLS_CACHE_DEFINE ()
173285#endif
286+
287+ #endif /* PHP_MAJOR_VERSION */
174288ZEND_GET_MODULE (ant )
175289#endif
176290
0 commit comments