Skip to content

Instantly share code, notes, and snippets.

@racklin
Created November 17, 2015 04:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save racklin/8a49443e5922b8fa22d0 to your computer and use it in GitHub Desktop.
Save racklin/8a49443e5922b8fa22d0 to your computer and use it in GitHub Desktop.
simple php extension using pcre
PHP_ARG_ENABLE(test1, whether to enable my extension,
[ --enable-test1 Enable my extension])
if test "$PHP_TEST1" = "yes"; then
AC_DEFINE(HAVE_TEST1, 1, [Whether you have my extension])
PHP_NEW_EXTENSION(test1, test1.c, $ext_shared)
PHP_ADD_EXTENSION_DEP(test1, pcre)
fi
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2014 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 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_01.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: |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifndef PHP_TEST1_H
#define PHP_TEST1_H
extern zend_module_entry test1_module_entry;
#define phpext_test1_ptr &test1_module_entry
#define PHP_TEST1_VERSION "0.1.0" /* Replace with version number for your extension */
#ifdef PHP_WIN32
# define PHP_TEST1_API __declspec(dllexport)
#elif defined(__GNUC__) && __GNUC__ >= 4
# define PHP_TEST1_API __attribute__ ((visibility("default")))
#else
# define PHP_TEST1_API
#endif
#ifdef ZTS
#include "TSRM.h"
#endif
PHP_MINIT_FUNCTION(test1);
PHP_MSHUTDOWN_FUNCTION(test1);
PHP_RINIT_FUNCTION(test1);
PHP_RSHUTDOWN_FUNCTION(test1);
PHP_MINFO_FUNCTION(test1);
PHP_FUNCTION(confirm_test1_compiled); /* For testing, remove later. */
/*
Declare any global variables you may need between the BEGIN
and END macros here:
ZEND_BEGIN_MODULE_GLOBALS(test1)
long global_value;
char *global_string;
ZEND_END_MODULE_GLOBALS(test1)
*/
/* In every utility function you add that needs to use variables
in php_test1_globals, call TSRMLS_FETCH(); after declaring other
variables used by that function, or better yet, pass in TSRMLS_CC
after the last function argument and declare your utility function
with TSRMLS_DC after the last declared argument. Always refer to
the globals in your function as TEST1_G(variable). You are
encouraged to rename these macros something shorter, see
examples in any other php module directory.
*/
#ifdef ZTS
#define TEST1_G(v) TSRMG(test1_globals_id, zend_test1_globals *, v)
#else
#define TEST1_G(v) (test1_globals.v)
#endif
#endif /* PHP_TEST1_H */
/*
* 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
*/
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2014 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 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_01.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: |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "ext/pcre/php_pcre.h"
#include "php_test1.h"
/* If you declare any globals in php_test1.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(test1)
*/
/* True global resources - no need for thread safety here */
static int le_test1;
/* {{{ test1_functions[]
*
* Every user visible function must have an entry in test1_functions[].
*/
const zend_function_entry test1_functions[] = {
PHP_FE(confirm_test1_compiled, NULL) /* For testing, remove later. */
PHP_FE_END /* Must be the last line in test1_functions[] */
};
/* }}} */
/* {{{ test1_module_entry
*/
zend_module_entry test1_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"test1",
test1_functions,
PHP_MINIT(test1),
PHP_MSHUTDOWN(test1),
PHP_RINIT(test1), /* Replace with NULL if there's nothing to do at request start */
PHP_RSHUTDOWN(test1), /* Replace with NULL if there's nothing to do at request end */
PHP_MINFO(test1),
#if ZEND_MODULE_API_NO >= 20010901
PHP_TEST1_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_TEST1
ZEND_GET_MODULE(test1)
#endif
/* {{{ PHP_INI
*/
/* Remove comments and fill if you need to have entries in php.ini
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("test1.global_value", "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_test1_globals, test1_globals)
STD_PHP_INI_ENTRY("test1.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_test1_globals, test1_globals)
PHP_INI_END()
*/
/* }}} */
/* {{{ php_test1_init_globals
*/
/* Uncomment this function if you have INI entries
static void php_test1_init_globals(zend_test1_globals *test1_globals)
{
test1_globals->global_value = 0;
test1_globals->global_string = NULL;
}
*/
/* }}} */
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(test1)
{
/* If you have INI entries, uncomment these lines
REGISTER_INI_ENTRIES();
*/
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(test1)
{
/* uncomment this line if you have INI entries
UNREGISTER_INI_ENTRIES();
*/
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(test1)
{
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(test1)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(test1)
{
php_info_print_table_start();
php_info_print_table_header(2, "test1 support", "enabled");
php_info_print_table_end();
/* Remove comments if you have entries in php.ini
DISPLAY_INI_ENTRIES();
*/
}
/* }}} */
/* Remove the following function when you have successfully modified config.m4
so that your module can be compiled into PHP, it exists only for testing
purposes. */
/* Every user-visible function in PHP should document itself in the source */
/* {{{ proto string confirm_test1_compiled(string arg)
Return a string to confirm that the module is compiled in */
PHP_FUNCTION(confirm_test1_compiled)
{
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.", "test1", arg);
zval *regex;
pcre_cache_entry *pce;
if ((pce = pcre_get_compiled_regex_cache(Z_STRVAL_P(regex), Z_STRLEN_P(regex) TSRMLS_CC)) == NULL) {
}
RETURN_STRINGL(strg, len, 0);
}
/* }}} */
/* 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
function definition, where the functions purpose is also documented. Please
follow this convention for the convenience of others editing your code.
*/
/*
* 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
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment