Hi,
I have an early extension that I'd like to guide me to the finish.
Having knowledge of C, let alone PHP architecture and extension developers do not run the streets, I am compelled to turn to you.
This is added to PHP, a function to rename other functions (native or not).
Until then, it works more or less, however, I can not rename some functions in particular (include, require, include_once, require_once, etc..).
The need for this extension is really thought, it's not just a crazy idea for the experiment, it is really necessary to my project.
Here is my current code:
config.m4
dnl config.m4 for extension rename
PHP_ARG_ENABLE(rename, whether to enable rename support,
[ --enable-rename Enable rename support])
if test "$PHP_RENAME" != "no"; then
PHP_NEW_EXTENSION(rename, rename.c, $ext_shared)
fi
rename.c
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_rename.h"
static int le_rename;
ZEND_BEGIN_ARG_INFO_EX(arginfo_function_rename, 0, 0, 2)
ZEND_ARG_INFO(0, orig_func)
ZEND_ARG_INFO(0, new_func)
ZEND_END_ARG_INFO()
/* {{{ rename_functions[]
*
* Every user visible function must have an entry in rename_functions[].
*/
const zend_function_entry rename_functions[] = {
PHP_FE(function_rename, arginfo_function_rename)
{NULL,NULL,NULL}
};
/* }}} */
/* {{{ rename_module_entry
*/
zend_module_entry rename_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"rename",
rename_functions,
PHP_MINIT(rename),
PHP_MSHUTDOWN(rename),
NULL,
NULL,
PHP_MINFO(rename),
#if ZEND_MODULE_API_NO >= 20010901
"0.1",
#endif
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_RENAME
ZEND_GET_MODULE(rename)
#endif
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(rename)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(rename)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(comuto)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(comuto)
{
return SUCCESS;
}
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(rename)
{
php_info_print_table_start();
php_info_print_table_header(2, "rename support", "enabled");
php_info_print_table_end();
}
/* }}} */
/* {{{ proto string function_rename(string orig_func, string new_func)
*/
PHP_FUNCTION(function_rename)
{
char *orig_func, *new_func, *orig_lower, *new_lower;
int orig_func_len, new_func_len, i;
zend_function *func;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &orig_func, &orig_func_len, &new_func, &new_func_len) == FAILURE) {
return;
}
// Normalize the source function name to lower case.
orig_lower = malloc(orig_func_len+1);
for (i = 0; i < orig_func_len; i ++) {
orig_lower[i] = tolower(orig_func[i]);
}
orig_lower[i] = 0;
// Normalize the destination function name to lower case.
new_lower = malloc(new_func_len + 1);
for (i = 0; i < new_func_len; i++) {
new_lower[i] = tolower(new_func[i]);
}
new_lower[i] = 0;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &orig_func, &orig_func_len, &new_func, &new_func_len) == FAILURE) {
return;
}
if(zend_hash_find(EG(function_table), orig_lower, orig_func_len + 1, (void **) &func) == FAILURE) {
zend_error(E_WARNING, "cannot find %s()", orig_func);
RETURN_FALSE;
}
if(zend_hash_exists(EG(function_table), new_lower, new_func_len + 1)) {
zend_error(E_WARNING, "%s() already exists", new_func);
RETURN_FALSE;
}
if(zend_hash_add(EG(function_table), new_lower, new_func_len + 1, func, sizeof(zend_function), NULL) == FAILURE) {
zend_error(E_WARNING, "cannot rename %s()", orig_func);
RETURN_FALSE;
}
if(zend_hash_del(EG(function_table), orig_lower, orig_func_len + 1) == FAILURE) {
zend_error(E_WARNING, "cannot rename %s()", orig_func);
RETURN_FALSE;
}
RETURN_TRUE;
}
php_rename.h
#ifndef PHP_RENAME_H
#define PHP_RENAME_H
extern zend_module_entry rename_module_entry;
#define phpext_rename_ptr &rename_module_entry
#ifdef PHP_WIN32
# define PHP_RENAME_API __declspec(dllexport)
#elif defined(__GNUC__) && __GNUC__ >= 4
# define PHP_RENAME_API __attribute__ ((visibility("default")))
#else
# define PHP_RENAME_API
#endif
#ifdef ZTS
#include "TSRM.h"
#endif
PHP_MINIT_FUNCTION(rename);
PHP_MSHUTDOWN_FUNCTION(rename);
PHP_RINIT_FUNCTION(rename);
PHP_RSHUTDOWN_FUNCTION(rename);
PHP_MINFO_FUNCTION(rename);
PHP_FUNCTION(function_rename);
#ifdef ZTS
#define RENAME_G(v) TSRMG(rename_globals_id, zend_rename_globals *, v)
#else
#define RENAME_G(v) (rename_globals.v)
#endif
#endif /* PHP_RENAME_H */
Can You help me, please? ๐