cvs: pecl /philter EXPERIMENTAL config.m4 credits package.xml philter.c philter_base64.c philter_base64.h php_philter.h php_philter_mod.h
/philter/tests 001.phpt base64_001.phpt base64code.php
| From: | John Coggeshall | Date: | Sat, 06 Mar 2004 22:11:06 +0000 |
| Subject: | cvs: pecl /philter EXPERIMENTAL config.m4 credits package.xml philter.c philter_base64.c philter_base64.h php_philter.h php_philter_mod.h /philter/tests 001.phpt base64_001.phpt base64code.php |
||
| Groups: | php.pecl.cvs | ||
| Request: | Send a blank email to pecl-cvs+get-1011@lists.php.net to get a copy of this message | ||
john Sat Mar 6 17:11:06 2004 EDT
Added files:
/pecl/philter EXPERIMENTAL config.m4 credits package.xml philter.c
philter_base64.c philter_base64.h php_philter.h
php_philter_mod.h
/pecl/philter/tests 001.phpt base64_001.phpt base64code.php
Log:
Initial Check-in of the philter extension. See tests for an example of its
use.
http://cvs.php.net/co.php/pecl/philter/config.m4?r=1.1&p=1 Index: pecl/philter/config.m4 +++ pecl/philter/config.m4 dnl $Id: config.m4,v 1.1 2004/03/06 22:11:05 john Exp $ dnl config.m4 for extension philter dnl Comments in this file start with the string 'dnl'. dnl Remove where necessary. This file will not work dnl without editing. dnl If your extension references something external, use with: dnl PHP_ARG_WITH(philter, for philter support, dnl Make sure that the comment is aligned: dnl [ --with-philter Include philter support]) dnl Otherwise use enable: PHP_ARG_ENABLE(philter, whether to enable philter support, dnl Make sure that the comment is aligned: [ --enable-philter Enable philter support]) if test $PHP_PHILTER != "no"; then SEARCH_PATH="/usr/local /usr" SEARCH_FOR="/include/gcrypt.h" AC_MSG_CHECKING([for gcrypt library for RSA support]) for i in $SEARCH_PATH ; do if test -r $i/$SEARCH_FOR; then GCRYPT_DIR=$i AC_MSG_RESULT(found in $i) fi done if test -z "$GCRYPT_DIR"; then AC_MSG_RESULT([not found]) else PHP_ADD_INCLUDE($GCRYPT_DIR/include) LIBNAME=gcrypt LIBSYMBOL=gcry_check_version AC_DEFINE(HAVE_GCRYPTLIB, 1, [ ]) PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $GCRYPT_DIR/lib, PHILTER_SHARED_LIBADD) fi PHP_NEW_EXTENSION(philter, philter.c philter_base64.c, $ext_shared) fi http://cvs.php.net/co.php/pecl/philter/package.xml?r=1.1&p=1 Index: pecl/philter/package.xml +++ pecl/philter/package.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE package SYSTEM "../pear/package.dtd"> <package> <name>philter</name> <summary>A PHP Script Pre-processor and encoder</summary> <maintainers> <maintainer> <user>john</user> <name>John Coggeshall</name> <email>john@php.net</email> <role>lead</role> </maintainer> </maintainers> <description> Philter is an extension that allows you to encode PHP scripts, write pre-processors, or perform other manipulations on PHP scripts prior to them being compiled by the Zend engine. </description> <license>PHP</license> <release> <state>alpha</state> <version>.3dev</version> <date>2004-03-06</date> <notes> Initial PECL release </notes> <configureoptions> <configureoption name="enable-philter" default="autodetect"/> </configureoptions> <filelist> <file role="src" name="config.m4"/> <file role="src" name="philter.c"/> <file role="src" name="philter_base64.c"/> <file role="src" name="philter_base64.h"/> <file role="src" name="php_philter_mod.h"/> <file role="src" name="php_philter.h"/> <file role="doc" name="CREDITS"/> <file role="doc" name="EXPERIMENTAL"/> <file role="test" name="tests/001.phpt"/> <file role="test" name="tests/base64_001.phpt"/> <file role="test" name="tests/base64code.php"/> </filelist> <deps> <dep type="php" rel="ge" version="5" /> </deps> </release> </package> <!-- vim:et:ts=1:sw=1 --> http://cvs.php.net/co.php/pecl/philter/philter.c?r=1.1&p=1 Index: pecl/philter/philter.c +++ pecl/philter/philter.c /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2003 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.0 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_0.txt. | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: John Coggeshall <john@php.net> | +----------------------------------------------------------------------+ */ /* $Id: philter.c,v 1.1 2004/03/06 22:11:06 john Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "php.h" #include "php_ini.h" #include "ext/standard/info.h" #include "ext/standard/md5.h" #include "php_philter_mod.h" #include "php_philter.h" #include "Zend/zend_compile.h" #include "Zend/zend_hash.h" #include "philter_base64.h" ZEND_DECLARE_MODULE_GLOBALS(philter); static philter_module *philter_modules[MAX_MODULES +1] = { &philter_base64, NULL }; PHP_INI_BEGIN() PHP_INI_END() PHPAPI int php_philter_register_module(philter_module *ptr) { int i; for(i = 0; i < MAX_MODULES; i++) { if(!philter_modules[i]) { philter_modules[i] = ptr; return SUCCESS; } } return FAILURE; } static philter_module *_php_find_philter_module(char *name TSRMLS_DC) { philter_module *ret = NULL; philter_module **mod; int i; for(i = 0, mod = philter_modules; i < MAX_MODULES; i++, mod++) { if(*mod && !strcasecmp(name, (*mod)->module_info.name)) { ret = *mod; break; } } return ret; } static size_t philter_stream_reader(void *handle, char *buf, size_t len TSRMLS_DC) { size_t bytes = (PHILTER_G(index) + len > PHILTER_G(decoded_len)) ? PHILTER_G(decoded_len) - PHILTER_G(index) : len; memcpy(buf, &PHILTER_G(decoded)[PHILTER_G(index)], bytes); PHILTER_G(index) += bytes; return bytes; } static void philter_stream_closer(void *handle TSRMLS_DC) { old_stream_closer(handle TSRMLS_CC); efree(PHILTER_G(script)); PHILTER_G(script_len) = 0; PHILTER_G(index) = 0; PHILTER_G(decoded_len) = 0; PHILTER_G(decoded) = NULL; PHILTER_G(script) = NULL; } static void _php_philter_call_modules(TSRMLS_D) { philter_module_header *mod; philter_module *module; int i; int header_size, script_size; PHILTER_G(base_header) = (philter_base_header *)PHILTER_G(script); header_size = sizeof(philter_base_header) + (sizeof(philter_module_header) * PHILTER_G(base_header)->num_philters); script_size = PHILTER_G(script_len) - header_size; if(PHILTER_G(base_header)->num_philters) { mod = (philter_module_header *)((char *)PHILTER_G(script) + header_size - sizeof(philter_module_header)); for(i = 0; i < PHILTER_G(base_header)->num_philters; i++) { mod = (philter_module_header *)((char *)PHILTER_G(script) + sizeof(philter_base_header)); if((module = _php_find_philter_module(mod->name TSRMLS_CC)) != NULL) { if(module->decode) { char *script, *newscript; int new_len; script = ((char *)PHILTER_G(script) + header_size); newscript = module->decode(script, script_size, &new_len TSRMLS_CC); PHILTER_G(script) = erealloc(PHILTER_G(script), header_size + new_len + 1); memcpy(((char *)PHILTER_G(script) + header_size), newscript, new_len); PHILTER_G(script)[header_size + new_len] = '\0'; script_size = new_len; efree(newscript); } else { zend_error(E_NOTICE, "Module %s provided no decoding routine", module->module_info.name); } } mod--; } } PHILTER_G(decoded) = ((char *)PHILTER_G(script) + header_size); PHILTER_G(decoded_len) = script_size; } zend_op_array *philter_compile(zend_file_handle *file_handle, int type TSRMLS_DC) { int i = 0; size_t bytes; PHP_MD5_CTX context; unsigned char digest[16]; zend_stream_fixup(file_handle TSRMLS_CC); old_stream_reader = file_handle->handle.stream.reader; old_stream_closer = file_handle->handle.stream.closer; file_handle->handle.stream.reader = philter_stream_reader; file_handle->handle.stream.closer = philter_stream_closer; PHILTER_G(script) = emalloc(PHILTER_BUFSIZE); for(i = 2; (bytes = old_stream_reader(file_handle->handle.stream.handle, &PHILTER_G(script)[PHILTER_G(index)], PHILTER_BUFSIZE TSRMLS_CC)) > 0; i++) { PHILTER_G(script_len) += bytes; if(bytes == PHILTER_BUFSIZE) { PHILTER_G(script) = erealloc(PHILTER_G(script), PHILTER_BUFSIZE * i); PHILTER_G(index) += bytes; } } PHILTER_G(script_len) += bytes; if(!strncmp(PHILTER_G(script), PHILTER_IDENT, strlen(PHILTER_IDENT))) { char md5[33]; _php_philter_call_modules(TSRMLS_C); PHP_MD5Init(&context); PHP_MD5Update(&context, PHILTER_G(decoded), PHILTER_G(decoded_len)); PHP_MD5Final(digest, &context); make_digest(&md5, digest); if(strncmp(md5, PHILTER_G(base_header)->md5, 32)) { zend_error(E_ERROR, "Validation of philtered script '%s' failed, cannot execute.", file_handle->filename); return NULL; } } else { PHILTER_G(decoded) = PHILTER_G(script); PHILTER_G(decoded_len) = PHILTER_G(script_len); } PHILTER_G(index) = 0; return zend_compile_file_old(file_handle, type TSRMLS_CC); } static void php_philter_init_globals(zend_philter_globals *g) { g->script = NULL; g->script_len = 0; g->index = 0; } function_entry philter_functions[] = { PHP_FE(philter_encode, NULL) {NULL, NULL, NULL} }; zend_module_entry philter_module_entry = { #if ZEND_MODULE_API_NO >= 20010901 STANDARD_MODULE_HEADER, #endif "philter", philter_functions, PHP_MINIT(philter), PHP_MSHUTDOWN(philter), PHP_RINIT(philter), PHP_RSHUTDOWN(philter), PHP_MINFO(philter), #if ZEND_MODULE_API_NO >= 20010901 PHILTER_VERSION, #endif STANDARD_MODULE_PROPERTIES }; #ifdef COMPILE_DL_PHILTER ZEND_GET_MODULE(philter) #endif PHP_FUNCTION(philter_encode) { char *in, *out; int in_len, out_len; php_stream *stream; int len; char *data = NULL; zval *philter_data = NULL, *mod = NULL; PHP_MD5_CTX context; unsigned char digest[16]; philter_base_header header = {PHILTER_IDENT, PHILTER_VERSION}; philter_module *module; if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssz|z", &in, &in_len, &out, &out_len, &mod, &philter_data) == FAILURE) { return; } if (!(stream = php_stream_open_wrapper(in, "rb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL))) { RETURN_FALSE; } if ((len = php_stream_copy_to_mem(stream, &data, PHP_STREAM_COPY_ALL, 0)) == 0) { data = estrdup(""); } php_stream_close(stream); /* Get a hash of the script before we encode it for validation later */ PHP_MD5Init(&context); PHP_MD5Update(&context, data, len); PHP_MD5Final(digest, &context); make_digest(&header.md5, digest); if(Z_TYPE_P(mod) == IS_ARRAY) { header.num_philters = zend_hash_num_elements(HASH_OF(mod)); } else { header.num_philters = 1; } if((stream = php_stream_open_wrapper(out, "wb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL))) { HashTable *temp; _php_stream_write(stream, &header, sizeof(philter_base_header) TSRMLS_CC); /* Call modules to do their encoding of the data, writing the header for each in the order they were encoded. */ if(Z_TYPE_P(mod) == IS_ARRAY) { zval **mod_name = NULL; temp = HASH_OF(mod); for(zend_hash_internal_pointer_reset(temp); zend_hash_get_current_data(temp, (void **)&mod_name) == SUCCESS; zend_hash_move_forward(temp)) { if((module = _php_find_philter_module(Z_STRVAL_PP(mod_name) TSRMLS_CC)) != NULL) { int new_len; void *newdata; if(module->encode) { newdata = module->encode(data, len, philter_data, &new_len TSRMLS_CC); if(new_len >= 0) { _php_stream_write(stream, &module->module_info, sizeof(philter_module_header) TSRMLS_CC); efree(data); data = newdata; len = new_len; } else { efree(newdata); zend_error(E_NOTICE, "Encoding for module '%s' failed -- ignoring", Z_STRVAL_PP(mod_name)); } } else { zend_error(E_NOTICE, "Module %s provided no encoding routine", Z_STRVAL_PP(mod_name)); } } else { zend_error(E_NOTICE, "Could not find philter module %s", Z_STRVAL_PP(mod_name)); } } } else { convert_to_string(mod); if((module = _php_find_philter_module(Z_STRVAL_P(mod) TSRMLS_CC)) != NULL) { int new_len; void *newdata; if(module->encode) { newdata = module->encode(data, len, philter_data, &new_len TSRMLS_CC); if(new_len >= 0) { _php_stream_write(stream, &module->module_info, sizeof(philter_module_header) TSRMLS_CC); efree(data); data = newdata; len = new_len; } else { efree(newdata); zend_error(E_WARNING, "Encoding for module '%s' failed", Z_STRVAL_P(mod)); } } else { zend_error(E_WARNING, "Module %s provided no encoding routine", Z_STRVAL_P(mod)); } } else { zend_error(E_WARNING, "Could not find philter module %s", Z_STRVAL_P(mod)); } } _php_stream_write(stream, data, len TSRMLS_CC); php_stream_close(stream); RETVAL_TRUE; } else { RETVAL_FALSE; } efree(data); } PHP_MINIT_FUNCTION(philter) { ZEND_INIT_MODULE_GLOBALS(philter, php_philter_init_globals, NULL); REGISTER_INI_ENTRIES(); zend_compile_file_old = zend_compile_file; zend_compile_file = philter_compile; int i; for(i = 0; i < MAX_MODULES; i++) { if(philter_modules[i]) { if(philter_modules[i]->init) { philter_modules[i]->init(INIT_FUNC_ARGS_PASSTHRU); } } } return SUCCESS; } PHP_MSHUTDOWN_FUNCTION(philter) { zend_compile_file = zend_compile_file_old; return SUCCESS; } PHP_RINIT_FUNCTION(philter) { return SUCCESS; } PHP_RSHUTDOWN_FUNCTION(philter) { PHILTER_G(script) = NULL; PHILTER_G(index) = 0; PHILTER_G(script_len) = 0; PHILTER_G(decoded) = NULL; PHILTER_G(decoded_len) = 0; PHILTER_G(base_header) = NULL; return SUCCESS; } PHP_MINFO_FUNCTION(philter) { php_info_print_table_start(); php_info_print_table_header(2, "philter support", "enabled"); php_info_print_table_end(); int i; for(i = 0; i < MAX_MODULES; i++) { if(philter_modules[i]) { if(philter_modules[i]->info) { philter_modules[i]->info(ZEND_MODULE_INFO_FUNC_ARGS_PASSTHRU); } } } DISPLAY_INI_ENTRIES(); } http://cvs.php.net/co.php/pecl/philter/philter_base64.c?r=1.1&p=1 Index: pecl/philter/philter_base64.c +++ pecl/philter/philter_base64.c /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2003 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.0 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_0.txt. | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: John Coggeshall <john@php.net> | +----------------------------------------------------------------------+ */ /* $Id: philter_base64.c,v 1.1 2004/03/06 22:11:06 john Exp $ */ #include "php.h" #include "php_philter_mod.h" #include "philter_base64.h" philter_module philter_base64 = { {"BASE64", "base64", "0.1"}, php_philter_base64_encode, php_philter_base64_decode, NULL, NULL }; void *php_philter_base64_encode(void *script, int in_len, zval *data, int *out_len TSRMLS_DC) { return (char *)php_base64_encode((unsigned char *)script, in_len, out_len); } void *php_philter_base64_decode(void *input, int in_len, int *out_len TSRMLS_DC) { return (char *)php_base64_decode((unsigned char *)input, in_len, out_len); } http://cvs.php.net/co.php/pecl/philter/philter_base64.h?r=1.1&p=1 Index: pecl/philter/philter_base64.h +++ pecl/philter/philter_base64.h /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2003 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.0 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_0.txt. | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: John Coggeshall <john@php.net> | +----------------------------------------------------------------------+ */ #ifndef PHILTER_BASE64_H #define PHILTER_BASE64_H extern philter_module philter_base64; #ifndef philter_base64_ptr #define philter_base64_ptr &philter_base64; #endif void *php_philter_base64_encode(void *script, int in_len, zval *data, int *out_len TSRMLS_DC); void *php_philter_base64_decode(void *input, int in_len, int *out_len TSRMLS_DC); #endif http://cvs.php.net/co.php/pecl/philter/php_philter.h?r=1.1&p=1 Index: pecl/philter/php_philter.h +++ pecl/philter/php_philter.h /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2003 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.0 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_0.txt. | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: John Coggeshall <john@php.net> | +----------------------------------------------------------------------+ */ /* $Id: php_philter.h,v 1.1 2004/03/06 22:11:06 john Exp $ */ #ifndef PHP_PHILTER_H #define PHP_PHILTER_H #include "php_philter_mod.h" #ifdef PHP_WIN32 #define PHP_PHILTER_API __declspec(dllexport) #else #define PHP_PHILTER_API #endif #define PHILTER_BUFSIZE 8192 #define PHILTER_VERSION "0.3-dev" #define PHILTER_IDENT "PHILTER" #define MAX_MODULES 30 extern zend_module_entry philter_module_entry; #define phpext_philter_ptr &philter_module_entry ZEND_BEGIN_MODULE_GLOBALS(philter) philter_base_header *base_header; unsigned char *script; unsigned int index; size_t script_len; unsigned char *decoded; size_t decoded_len; ZEND_END_MODULE_GLOBALS(philter) size_t (*old_stream_reader)(void *, char *, size_t TSRMLS_DC); void (*old_stream_closer)(void * TSRMLS_DC); zend_op_array *(*zend_compile_file_old)(zend_file_handle *, int TSRMLS_DC); static size_t philter_stream_reader(void *handle, char *buf, size_t len TSRMLS_DC); static void philter_stream_closer(void *handle TSRMLS_DC); #ifdef ZTS #include "TSRM.h" #endif PHP_MINIT_FUNCTION(philter); PHP_MSHUTDOWN_FUNCTION(philter); PHP_RINIT_FUNCTION(philter); PHP_RSHUTDOWN_FUNCTION(philter); PHP_MINFO_FUNCTION(philter); PHP_FUNCTION(philter_encode); #ifdef ZTS #define PHILTER_G(v) TSRMG(philter_globals_id, zend_philter_globals *, v) #else #define PHILTER_G(v) (philter_globals.v) #endif #endif /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: noet sw=4 ts=4 fdm=marker * vim<600: noet sw=4 ts=4 */ http://cvs.php.net/co.php/pecl/philter/php_philter_mod.h?r=1.1&p=1 Index: pecl/philter/php_philter_mod.h +++ pecl/philter/php_philter_mod.h /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2003 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.0 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_0.txt. | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: John Coggeshall <john@php.net> | +----------------------------------------------------------------------+ */ #ifndef PHP_PHILTER_MOD_H #define PHP_PHILTER_MOD_H #define PHILTER_MAX_NAMELEN 26 #define PHILTER_MAX_VERLEN 26 #define PHILTER_MAX_IDENTLEN 11 #define PHILTER_MAX_FILEIDLEN 33 typedef struct _philter_base_header { char ident[PHILTER_MAX_IDENTLEN]; char version[PHILTER_MAX_VERLEN]; char md5[PHILTER_MAX_FILEIDLEN]; unsigned int num_philters; } philter_base_header; typedef struct _philter_module_header { char ident[PHILTER_MAX_IDENTLEN]; char name[PHILTER_MAX_NAMELEN]; char version[PHILTER_MAX_VERLEN]; } philter_module_header; typedef struct _philter_module { philter_module_header module_info; void *(*encode)(void *, int, zval *, int * TSRMLS_DC); void *(*decode)(void *, int, int * TSRMLS_DC); void (*init)(INIT_FUNC_ARGS); void (*info)(ZEND_MODULE_INFO_FUNC_ARGS); } philter_module; #endif http://cvs.php.net/co.php/pecl/philter/tests/001.phpt?r=1.1&p=1 Index: pecl/philter/tests/001.phpt +++ pecl/philter/tests/001.phpt --TEST-- Check for philter presence --SKIPIF-- <?php if (!extension_loaded("philter")) print "skip"; ?> --POST-- --GET-- --INI-- --FILE-- <?php echo "philter extension is available"; /* you can add regression tests for your extension here the output of your test code has to be equal to the text in the --EXPECT-- section below for the tests to pass, differences between the output and the expected text are interpreted as failure see php4/README.TESTING for further information on writing regression tests */ ?> --EXPECT-- philter extension is available http://cvs.php.net/co.php/pecl/philter/tests/base64_001.phpt?r=1.1&p=1 Index: pecl/philter/tests/base64_001.phpt +++ pecl/philter/tests/base64_001.phpt --TEST-- Check the base64 philter --SKIPIF-- <?php if (!extension_loaded("philter")) print "skip"; ?> --POST-- --GET-- --INI-- --FILE-- <?php philter_encode("ext/philter/tests/base64code.php", "ext/philter/tests/base64encoded.php", "base64"); include_once("ext/philter/tests/base64encoded.php"); unlink("ext/philter/tests/base64encoded.php"); ?> --EXPECT-- The base64 philter worked. http://cvs.php.net/co.php/pecl/philter/tests/base64code.php?r=1.1&p=1 Index: pecl/philter/tests/base64code.php +++ pecl/philter/tests/base64code.php <?php echo "The base64 philter worked."; ?>
http://cvs.php.net/co.php/pecl/philter/config.m4?r=1.1&p=1 Index: pecl/philter/config.m4 +++ pecl/philter/config.m4 dnl $Id: config.m4,v 1.1 2004/03/06 22:11:05 john Exp $ dnl config.m4 for extension philter dnl Comments in this file start with the string 'dnl'. dnl Remove where necessary. This file will not work dnl without editing. dnl If your extension references something external, use with: dnl PHP_ARG_WITH(philter, for philter support, dnl Make sure that the comment is aligned: dnl [ --with-philter Include philter support]) dnl Otherwise use enable: PHP_ARG_ENABLE(philter, whether to enable philter support, dnl Make sure that the comment is aligned: [ --enable-philter Enable philter support]) if test $PHP_PHILTER != "no"; then SEARCH_PATH="/usr/local /usr" SEARCH_FOR="/include/gcrypt.h" AC_MSG_CHECKING([for gcrypt library for RSA support]) for i in $SEARCH_PATH ; do if test -r $i/$SEARCH_FOR; then GCRYPT_DIR=$i AC_MSG_RESULT(found in $i) fi done if test -z "$GCRYPT_DIR"; then AC_MSG_RESULT([not found]) else PHP_ADD_INCLUDE($GCRYPT_DIR/include) LIBNAME=gcrypt LIBSYMBOL=gcry_check_version AC_DEFINE(HAVE_GCRYPTLIB, 1, [ ]) PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $GCRYPT_DIR/lib, PHILTER_SHARED_LIBADD) fi PHP_NEW_EXTENSION(philter, philter.c philter_base64.c, $ext_shared) fi http://cvs.php.net/co.php/pecl/philter/package.xml?r=1.1&p=1 Index: pecl/philter/package.xml +++ pecl/philter/package.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE package SYSTEM "../pear/package.dtd"> <package> <name>philter</name> <summary>A PHP Script Pre-processor and encoder</summary> <maintainers> <maintainer> <user>john</user> <name>John Coggeshall</name> <email>john@php.net</email> <role>lead</role> </maintainer> </maintainers> <description> Philter is an extension that allows you to encode PHP scripts, write pre-processors, or perform other manipulations on PHP scripts prior to them being compiled by the Zend engine. </description> <license>PHP</license> <release> <state>alpha</state> <version>.3dev</version> <date>2004-03-06</date> <notes> Initial PECL release </notes> <configureoptions> <configureoption name="enable-philter" default="autodetect"/> </configureoptions> <filelist> <file role="src" name="config.m4"/> <file role="src" name="philter.c"/> <file role="src" name="philter_base64.c"/> <file role="src" name="philter_base64.h"/> <file role="src" name="php_philter_mod.h"/> <file role="src" name="php_philter.h"/> <file role="doc" name="CREDITS"/> <file role="doc" name="EXPERIMENTAL"/> <file role="test" name="tests/001.phpt"/> <file role="test" name="tests/base64_001.phpt"/> <file role="test" name="tests/base64code.php"/> </filelist> <deps> <dep type="php" rel="ge" version="5" /> </deps> </release> </package> <!-- vim:et:ts=1:sw=1 --> http://cvs.php.net/co.php/pecl/philter/philter.c?r=1.1&p=1 Index: pecl/philter/philter.c +++ pecl/philter/philter.c /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2003 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.0 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_0.txt. | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: John Coggeshall <john@php.net> | +----------------------------------------------------------------------+ */ /* $Id: philter.c,v 1.1 2004/03/06 22:11:06 john Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "php.h" #include "php_ini.h" #include "ext/standard/info.h" #include "ext/standard/md5.h" #include "php_philter_mod.h" #include "php_philter.h" #include "Zend/zend_compile.h" #include "Zend/zend_hash.h" #include "philter_base64.h" ZEND_DECLARE_MODULE_GLOBALS(philter); static philter_module *philter_modules[MAX_MODULES +1] = { &philter_base64, NULL }; PHP_INI_BEGIN() PHP_INI_END() PHPAPI int php_philter_register_module(philter_module *ptr) { int i; for(i = 0; i < MAX_MODULES; i++) { if(!philter_modules[i]) { philter_modules[i] = ptr; return SUCCESS; } } return FAILURE; } static philter_module *_php_find_philter_module(char *name TSRMLS_DC) { philter_module *ret = NULL; philter_module **mod; int i; for(i = 0, mod = philter_modules; i < MAX_MODULES; i++, mod++) { if(*mod && !strcasecmp(name, (*mod)->module_info.name)) { ret = *mod; break; } } return ret; } static size_t philter_stream_reader(void *handle, char *buf, size_t len TSRMLS_DC) { size_t bytes = (PHILTER_G(index) + len > PHILTER_G(decoded_len)) ? PHILTER_G(decoded_len) - PHILTER_G(index) : len; memcpy(buf, &PHILTER_G(decoded)[PHILTER_G(index)], bytes); PHILTER_G(index) += bytes; return bytes; } static void philter_stream_closer(void *handle TSRMLS_DC) { old_stream_closer(handle TSRMLS_CC); efree(PHILTER_G(script)); PHILTER_G(script_len) = 0; PHILTER_G(index) = 0; PHILTER_G(decoded_len) = 0; PHILTER_G(decoded) = NULL; PHILTER_G(script) = NULL; } static void _php_philter_call_modules(TSRMLS_D) { philter_module_header *mod; philter_module *module; int i; int header_size, script_size; PHILTER_G(base_header) = (philter_base_header *)PHILTER_G(script); header_size = sizeof(philter_base_header) + (sizeof(philter_module_header) * PHILTER_G(base_header)->num_philters); script_size = PHILTER_G(script_len) - header_size; if(PHILTER_G(base_header)->num_philters) { mod = (philter_module_header *)((char *)PHILTER_G(script) + header_size - sizeof(philter_module_header)); for(i = 0; i < PHILTER_G(base_header)->num_philters; i++) { mod = (philter_module_header *)((char *)PHILTER_G(script) + sizeof(philter_base_header)); if((module = _php_find_philter_module(mod->name TSRMLS_CC)) != NULL) { if(module->decode) { char *script, *newscript; int new_len; script = ((char *)PHILTER_G(script) + header_size); newscript = module->decode(script, script_size, &new_len TSRMLS_CC); PHILTER_G(script) = erealloc(PHILTER_G(script), header_size + new_len + 1); memcpy(((char *)PHILTER_G(script) + header_size), newscript, new_len); PHILTER_G(script)[header_size + new_len] = '\0'; script_size = new_len; efree(newscript); } else { zend_error(E_NOTICE, "Module %s provided no decoding routine", module->module_info.name); } } mod--; } } PHILTER_G(decoded) = ((char *)PHILTER_G(script) + header_size); PHILTER_G(decoded_len) = script_size; } zend_op_array *philter_compile(zend_file_handle *file_handle, int type TSRMLS_DC) { int i = 0; size_t bytes; PHP_MD5_CTX context; unsigned char digest[16]; zend_stream_fixup(file_handle TSRMLS_CC); old_stream_reader = file_handle->handle.stream.reader; old_stream_closer = file_handle->handle.stream.closer; file_handle->handle.stream.reader = philter_stream_reader; file_handle->handle.stream.closer = philter_stream_closer; PHILTER_G(script) = emalloc(PHILTER_BUFSIZE); for(i = 2; (bytes = old_stream_reader(file_handle->handle.stream.handle, &PHILTER_G(script)[PHILTER_G(index)], PHILTER_BUFSIZE TSRMLS_CC)) > 0; i++) { PHILTER_G(script_len) += bytes; if(bytes == PHILTER_BUFSIZE) { PHILTER_G(script) = erealloc(PHILTER_G(script), PHILTER_BUFSIZE * i); PHILTER_G(index) += bytes; } } PHILTER_G(script_len) += bytes; if(!strncmp(PHILTER_G(script), PHILTER_IDENT, strlen(PHILTER_IDENT))) { char md5[33]; _php_philter_call_modules(TSRMLS_C); PHP_MD5Init(&context); PHP_MD5Update(&context, PHILTER_G(decoded), PHILTER_G(decoded_len)); PHP_MD5Final(digest, &context); make_digest(&md5, digest); if(strncmp(md5, PHILTER_G(base_header)->md5, 32)) { zend_error(E_ERROR, "Validation of philtered script '%s' failed, cannot execute.", file_handle->filename); return NULL; } } else { PHILTER_G(decoded) = PHILTER_G(script); PHILTER_G(decoded_len) = PHILTER_G(script_len); } PHILTER_G(index) = 0; return zend_compile_file_old(file_handle, type TSRMLS_CC); } static void php_philter_init_globals(zend_philter_globals *g) { g->script = NULL; g->script_len = 0; g->index = 0; } function_entry philter_functions[] = { PHP_FE(philter_encode, NULL) {NULL, NULL, NULL} }; zend_module_entry philter_module_entry = { #if ZEND_MODULE_API_NO >= 20010901 STANDARD_MODULE_HEADER, #endif "philter", philter_functions, PHP_MINIT(philter), PHP_MSHUTDOWN(philter), PHP_RINIT(philter), PHP_RSHUTDOWN(philter), PHP_MINFO(philter), #if ZEND_MODULE_API_NO >= 20010901 PHILTER_VERSION, #endif STANDARD_MODULE_PROPERTIES }; #ifdef COMPILE_DL_PHILTER ZEND_GET_MODULE(philter) #endif PHP_FUNCTION(philter_encode) { char *in, *out; int in_len, out_len; php_stream *stream; int len; char *data = NULL; zval *philter_data = NULL, *mod = NULL; PHP_MD5_CTX context; unsigned char digest[16]; philter_base_header header = {PHILTER_IDENT, PHILTER_VERSION}; philter_module *module; if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssz|z", &in, &in_len, &out, &out_len, &mod, &philter_data) == FAILURE) { return; } if (!(stream = php_stream_open_wrapper(in, "rb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL))) { RETURN_FALSE; } if ((len = php_stream_copy_to_mem(stream, &data, PHP_STREAM_COPY_ALL, 0)) == 0) { data = estrdup(""); } php_stream_close(stream); /* Get a hash of the script before we encode it for validation later */ PHP_MD5Init(&context); PHP_MD5Update(&context, data, len); PHP_MD5Final(digest, &context); make_digest(&header.md5, digest); if(Z_TYPE_P(mod) == IS_ARRAY) { header.num_philters = zend_hash_num_elements(HASH_OF(mod)); } else { header.num_philters = 1; } if((stream = php_stream_open_wrapper(out, "wb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL))) { HashTable *temp; _php_stream_write(stream, &header, sizeof(philter_base_header) TSRMLS_CC); /* Call modules to do their encoding of the data, writing the header for each in the order they were encoded. */ if(Z_TYPE_P(mod) == IS_ARRAY) { zval **mod_name = NULL; temp = HASH_OF(mod); for(zend_hash_internal_pointer_reset(temp); zend_hash_get_current_data(temp, (void **)&mod_name) == SUCCESS; zend_hash_move_forward(temp)) { if((module = _php_find_philter_module(Z_STRVAL_PP(mod_name) TSRMLS_CC)) != NULL) { int new_len; void *newdata; if(module->encode) { newdata = module->encode(data, len, philter_data, &new_len TSRMLS_CC); if(new_len >= 0) { _php_stream_write(stream, &module->module_info, sizeof(philter_module_header) TSRMLS_CC); efree(data); data = newdata; len = new_len; } else { efree(newdata); zend_error(E_NOTICE, "Encoding for module '%s' failed -- ignoring", Z_STRVAL_PP(mod_name)); } } else { zend_error(E_NOTICE, "Module %s provided no encoding routine", Z_STRVAL_PP(mod_name)); } } else { zend_error(E_NOTICE, "Could not find philter module %s", Z_STRVAL_PP(mod_name)); } } } else { convert_to_string(mod); if((module = _php_find_philter_module(Z_STRVAL_P(mod) TSRMLS_CC)) != NULL) { int new_len; void *newdata; if(module->encode) { newdata = module->encode(data, len, philter_data, &new_len TSRMLS_CC); if(new_len >= 0) { _php_stream_write(stream, &module->module_info, sizeof(philter_module_header) TSRMLS_CC); efree(data); data = newdata; len = new_len; } else { efree(newdata); zend_error(E_WARNING, "Encoding for module '%s' failed", Z_STRVAL_P(mod)); } } else { zend_error(E_WARNING, "Module %s provided no encoding routine", Z_STRVAL_P(mod)); } } else { zend_error(E_WARNING, "Could not find philter module %s", Z_STRVAL_P(mod)); } } _php_stream_write(stream, data, len TSRMLS_CC); php_stream_close(stream); RETVAL_TRUE; } else { RETVAL_FALSE; } efree(data); } PHP_MINIT_FUNCTION(philter) { ZEND_INIT_MODULE_GLOBALS(philter, php_philter_init_globals, NULL); REGISTER_INI_ENTRIES(); zend_compile_file_old = zend_compile_file; zend_compile_file = philter_compile; int i; for(i = 0; i < MAX_MODULES; i++) { if(philter_modules[i]) { if(philter_modules[i]->init) { philter_modules[i]->init(INIT_FUNC_ARGS_PASSTHRU); } } } return SUCCESS; } PHP_MSHUTDOWN_FUNCTION(philter) { zend_compile_file = zend_compile_file_old; return SUCCESS; } PHP_RINIT_FUNCTION(philter) { return SUCCESS; } PHP_RSHUTDOWN_FUNCTION(philter) { PHILTER_G(script) = NULL; PHILTER_G(index) = 0; PHILTER_G(script_len) = 0; PHILTER_G(decoded) = NULL; PHILTER_G(decoded_len) = 0; PHILTER_G(base_header) = NULL; return SUCCESS; } PHP_MINFO_FUNCTION(philter) { php_info_print_table_start(); php_info_print_table_header(2, "philter support", "enabled"); php_info_print_table_end(); int i; for(i = 0; i < MAX_MODULES; i++) { if(philter_modules[i]) { if(philter_modules[i]->info) { philter_modules[i]->info(ZEND_MODULE_INFO_FUNC_ARGS_PASSTHRU); } } } DISPLAY_INI_ENTRIES(); } http://cvs.php.net/co.php/pecl/philter/philter_base64.c?r=1.1&p=1 Index: pecl/philter/philter_base64.c +++ pecl/philter/philter_base64.c /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2003 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.0 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_0.txt. | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: John Coggeshall <john@php.net> | +----------------------------------------------------------------------+ */ /* $Id: philter_base64.c,v 1.1 2004/03/06 22:11:06 john Exp $ */ #include "php.h" #include "php_philter_mod.h" #include "philter_base64.h" philter_module philter_base64 = { {"BASE64", "base64", "0.1"}, php_philter_base64_encode, php_philter_base64_decode, NULL, NULL }; void *php_philter_base64_encode(void *script, int in_len, zval *data, int *out_len TSRMLS_DC) { return (char *)php_base64_encode((unsigned char *)script, in_len, out_len); } void *php_philter_base64_decode(void *input, int in_len, int *out_len TSRMLS_DC) { return (char *)php_base64_decode((unsigned char *)input, in_len, out_len); } http://cvs.php.net/co.php/pecl/philter/philter_base64.h?r=1.1&p=1 Index: pecl/philter/philter_base64.h +++ pecl/philter/philter_base64.h /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2003 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.0 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_0.txt. | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: John Coggeshall <john@php.net> | +----------------------------------------------------------------------+ */ #ifndef PHILTER_BASE64_H #define PHILTER_BASE64_H extern philter_module philter_base64; #ifndef philter_base64_ptr #define philter_base64_ptr &philter_base64; #endif void *php_philter_base64_encode(void *script, int in_len, zval *data, int *out_len TSRMLS_DC); void *php_philter_base64_decode(void *input, int in_len, int *out_len TSRMLS_DC); #endif http://cvs.php.net/co.php/pecl/philter/php_philter.h?r=1.1&p=1 Index: pecl/philter/php_philter.h +++ pecl/philter/php_philter.h /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2003 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.0 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_0.txt. | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: John Coggeshall <john@php.net> | +----------------------------------------------------------------------+ */ /* $Id: php_philter.h,v 1.1 2004/03/06 22:11:06 john Exp $ */ #ifndef PHP_PHILTER_H #define PHP_PHILTER_H #include "php_philter_mod.h" #ifdef PHP_WIN32 #define PHP_PHILTER_API __declspec(dllexport) #else #define PHP_PHILTER_API #endif #define PHILTER_BUFSIZE 8192 #define PHILTER_VERSION "0.3-dev" #define PHILTER_IDENT "PHILTER" #define MAX_MODULES 30 extern zend_module_entry philter_module_entry; #define phpext_philter_ptr &philter_module_entry ZEND_BEGIN_MODULE_GLOBALS(philter) philter_base_header *base_header; unsigned char *script; unsigned int index; size_t script_len; unsigned char *decoded; size_t decoded_len; ZEND_END_MODULE_GLOBALS(philter) size_t (*old_stream_reader)(void *, char *, size_t TSRMLS_DC); void (*old_stream_closer)(void * TSRMLS_DC); zend_op_array *(*zend_compile_file_old)(zend_file_handle *, int TSRMLS_DC); static size_t philter_stream_reader(void *handle, char *buf, size_t len TSRMLS_DC); static void philter_stream_closer(void *handle TSRMLS_DC); #ifdef ZTS #include "TSRM.h" #endif PHP_MINIT_FUNCTION(philter); PHP_MSHUTDOWN_FUNCTION(philter); PHP_RINIT_FUNCTION(philter); PHP_RSHUTDOWN_FUNCTION(philter); PHP_MINFO_FUNCTION(philter); PHP_FUNCTION(philter_encode); #ifdef ZTS #define PHILTER_G(v) TSRMG(philter_globals_id, zend_philter_globals *, v) #else #define PHILTER_G(v) (philter_globals.v) #endif #endif /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: noet sw=4 ts=4 fdm=marker * vim<600: noet sw=4 ts=4 */ http://cvs.php.net/co.php/pecl/philter/php_philter_mod.h?r=1.1&p=1 Index: pecl/philter/php_philter_mod.h +++ pecl/philter/php_philter_mod.h /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2003 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.0 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_0.txt. | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: John Coggeshall <john@php.net> | +----------------------------------------------------------------------+ */ #ifndef PHP_PHILTER_MOD_H #define PHP_PHILTER_MOD_H #define PHILTER_MAX_NAMELEN 26 #define PHILTER_MAX_VERLEN 26 #define PHILTER_MAX_IDENTLEN 11 #define PHILTER_MAX_FILEIDLEN 33 typedef struct _philter_base_header { char ident[PHILTER_MAX_IDENTLEN]; char version[PHILTER_MAX_VERLEN]; char md5[PHILTER_MAX_FILEIDLEN]; unsigned int num_philters; } philter_base_header; typedef struct _philter_module_header { char ident[PHILTER_MAX_IDENTLEN]; char name[PHILTER_MAX_NAMELEN]; char version[PHILTER_MAX_VERLEN]; } philter_module_header; typedef struct _philter_module { philter_module_header module_info; void *(*encode)(void *, int, zval *, int * TSRMLS_DC); void *(*decode)(void *, int, int * TSRMLS_DC); void (*init)(INIT_FUNC_ARGS); void (*info)(ZEND_MODULE_INFO_FUNC_ARGS); } philter_module; #endif http://cvs.php.net/co.php/pecl/philter/tests/001.phpt?r=1.1&p=1 Index: pecl/philter/tests/001.phpt +++ pecl/philter/tests/001.phpt --TEST-- Check for philter presence --SKIPIF-- <?php if (!extension_loaded("philter")) print "skip"; ?> --POST-- --GET-- --INI-- --FILE-- <?php echo "philter extension is available"; /* you can add regression tests for your extension here the output of your test code has to be equal to the text in the --EXPECT-- section below for the tests to pass, differences between the output and the expected text are interpreted as failure see php4/README.TESTING for further information on writing regression tests */ ?> --EXPECT-- philter extension is available http://cvs.php.net/co.php/pecl/philter/tests/base64_001.phpt?r=1.1&p=1 Index: pecl/philter/tests/base64_001.phpt +++ pecl/philter/tests/base64_001.phpt --TEST-- Check the base64 philter --SKIPIF-- <?php if (!extension_loaded("philter")) print "skip"; ?> --POST-- --GET-- --INI-- --FILE-- <?php philter_encode("ext/philter/tests/base64code.php", "ext/philter/tests/base64encoded.php", "base64"); include_once("ext/philter/tests/base64encoded.php"); unlink("ext/philter/tests/base64encoded.php"); ?> --EXPECT-- The base64 philter worked. http://cvs.php.net/co.php/pecl/philter/tests/base64code.php?r=1.1&p=1 Index: pecl/philter/tests/base64code.php +++ pecl/philter/tests/base64code.php <?php echo "The base64 philter worked."; ?>