Skip to content

Commit

Permalink
first php7 version
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarbonneaux committed Feb 20, 2021
1 parent a7b7d00 commit 378055d
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 54 deletions.
24 changes: 12 additions & 12 deletions GetPathDir.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@

// Retrieves the pathpath from a pathname.
//
// Returns: SUCCESS if the basepath is present and successfully copied to the p_base_path buffer
// FAILURE_NULL_ARGUMENT if any arguments are NULL
// FAILURE_INVALID_ARGUMENTS if either buffer size is less than 1
// FAILURE_BUFFER_TOO_SMALL if the p_basepath buffer is too small
// FAILURE_INVALID_PATH if the p_pathname doesn't have a path (e.g. C:, calc.exe, ?qwa)
// FAILURE_API_CALL if there is an error from the underlying API calls
// Returns: MCA_SUCCESS if the basepath is present and successfully copied to the p_base_path buffer
// MCA_FAILURE_NULL_ARGUMENT if any arguments are NULL
// MCA_FAILURE_INVALID_ARGUMENTS if either buffer size is less than 1
// MCA_FAILURE_BUFFER_TOO_SMALL if the p_basepath buffer is too small
// MCA_FAILURE_INVALID_PATH if the p_pathname doesn't have a path (e.g. C:, calc.exe, ?qwa)
// MCA_FAILURE_API_CALL if there is an error from the underlying API calls
int get_base_path_from_pathname( const char* const p_pathname,
size_t pathname_size,
char* const p_basepath,
Expand All @@ -37,28 +37,28 @@ int get_base_path_from_pathname( const char* const p_pathname,
int return_code;

// Parameter Validation
if( p_pathname == NULL || p_basepath == NULL ) { return FAILURE_NULL_ARGUMENT; }
if( pathname_size < 1 || basepath_size < 1 ) { return FAILURE_INVALID_ARGUMENTS; }
if( p_pathname == NULL || p_basepath == NULL ) { return MCA_FAILURE_NULL_ARGUMENT; }
if( pathname_size < 1 || basepath_size < 1 ) { return MCA_FAILURE_INVALID_ARGUMENTS; }

// Returns a pointer to the last occurrence of \ in p_pathname or NULL if it is not found
p_end_of_path = strrchr( p_pathname, '/' );
if( p_end_of_path == NULL )
{
// There is no path part
return FAILURE_INVALID_PATH;
return MCA_FAILURE_INVALID_PATH;
}
else
{
path_length = (size_t)( p_end_of_path - p_pathname + 1 );

// Do some sanity checks on the length
if( path_length < 1 ) { return FAILURE_INVALID_PATH; }
if( ( path_length + 1 ) > basepath_size ) { return FAILURE_BUFFER_TOO_SMALL; }
if( path_length < 1 ) { return MCA_FAILURE_INVALID_PATH; }
if( ( path_length + 1 ) > basepath_size ) { return MCA_FAILURE_BUFFER_TOO_SMALL; }

// Copy the base path into the out variable
strncpy(p_basepath, p_pathname, path_length);
p_basepath[path_length] = '\0';
}

return SUCCESS;
return MCA_SUCCESS;
}
12 changes: 6 additions & 6 deletions GetPathDir.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
#include <stdio.h>
#include <stdlib.h>

#define FAILURE_NULL_ARGUMENT 1
#define FAILURE_INVALID_ARGUMENTS 2
#define FAILURE_INVALID_PATH 3
#define FAILURE_BUFFER_TOO_SMALL 4
#define FAILURE_API_CALL 5
#define SUCCESS 0
#define MCA_FAILURE_NULL_ARGUMENT 1
#define MCA_FAILURE_INVALID_ARGUMENTS 2
#define MCA_FAILURE_INVALID_PATH 3
#define MCA_FAILURE_BUFFER_TOO_SMALL 4
#define MCA_FAILURE_API_CALL 5
#define MCA_SUCCESS 0
extern int get_base_path_from_pathname( const char* const p_pathname,
size_t pathname_size,
char* const p_basepath,
Expand Down
4 changes: 2 additions & 2 deletions Makefile.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MODULE_NAME=@MODULE_NAME@
MODULE_OBJS=$(MODULE_NAME).o php_embeded.o GetPathDir.o
CFLAGS=-fPIC -I@ZABBIX_INC_DIR@ @CFLAGS@
MODULE_OBJS=php_embeded.o GetPathDir.o $(MODULE_NAME).o
CFLAGS=-fPIC -I@ZABBIX_INC_DIR@ @CFLAGS@ -Wno-cpp
LDFLAGS=@LDFLAGS@
LIBS=@LIBS@
CC=gcc
Expand Down
3 changes: 2 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ AC_SUBST([LDFLAGS])
AC_SUBST([LIBS])
AC_SUBST([prefix])

AC_OUTPUT(zbx_php_config.h Makefile)
AC_CONFIG_HEADER(zbx_php_config.h)
AC_OUTPUT(Makefile)

echo ""
echo " Zabbix PHP laodable module install diretory = ${prefix}"
Expand Down
11 changes: 7 additions & 4 deletions m4/ax_lib_php.m4
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ AC_HELP_STRING([--with-php_lib@<:@=DIR@:>@], [PHP lib subdirectory if different
PHP_VERSION=""
dnl
dnl Check PHP Embeded libraries (libphp5)
dnl Check PHP Embeded libraries (libphp)
dnl
if test "$want_php" = "yes"; then
Expand All @@ -106,8 +106,11 @@ AC_HELP_STRING([--with-php_lib@<:@=DIR@:>@], [PHP lib subdirectory if different
LDFLAGS="${LDFLAGS} ${PHP_LDFLAGS}"
CFLAGS="${CFLAGS} ${PHP_CFLAGS}"
AC_CHECK_LIB(php5, php_embed_init,[ PHP_LIBS="-lphp5" ],[ AC_MSG_ERROR([Not found php embeded library]) ])
AC_CHECK_LIB(php5, php_embed_init,[ PHP_LIBS="-lphp5" ], [
AC_CHECK_LIB(php7, php_embed_init,[ PHP_LIBS="-lphp7" ], [ AC_MSG_ERROR([Not found php embeded library]) ])
])
dnl AC_CHECK_LIB(php7, php_embed_init,[ PHP_LIBS="-lphp7" ], [ AC_MSG_ERROR([Not found php embeded library]) ]))
AC_CHECK_LIB(php7, zend_signal_startup,[ AC_DEFINE(HAVE_ZEND_SIGNAL_STARTUP,[1],[zend_signal_startup are available]) ])
LIBS="${_save_php_libs}"
LDFLAGS="${_save_php_ldflags}"
CFLAGS="${_save_php_cflags}"
Expand All @@ -117,7 +120,7 @@ AC_HELP_STRING([--with-php_lib@<:@=DIR@:>@], [PHP lib subdirectory if different
PHP_VERSION=`$PHP_CONFIG --version`
AC_DEFINE(HAVE_PHP,1,[Define to 1 if PHP libraries are available])
AC_DEFINE(HAVE_PHP,[1],[Define to 1 if PHP libraries are available])
CFLAGS="${CFLAGS} ${PHP_CFLAGS}"
found_php="yes"
Expand Down
36 changes: 23 additions & 13 deletions php_embeded.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@
* https://www.amazon.fr/Advanced-PHP-Programming-George-Schlossnagle/dp/0672325616
*/

/* upgrading to php7
* https://wiki.php.net/phpng-upgrading
* https://www.programmersought.com/article/42361486581/
*/

#include "php_embeded.h"
#include "ext/standard/php_standard.h"

Expand All @@ -81,27 +86,22 @@
#error "Need PHP version >= 5.5 to compile this file"
#endif


/******************************************************************************
* *
* Function: php_embed_eval_string *
* *
* Purpose: eval php script string transmit in code. *
* *
******************************************************************************/
zval *php_embed_eval_string(char *code, zval *retval_ptr, char *string_name TSRMLS_DC)
int php_embed_eval_string(char *code, zval *retval, char *string_name TSRMLS_DC)
{
zval * retval;
ALLOC_INIT_ZVAL(retval);

zend_try {
if (SUCCESS == zend_eval_string(code, retval, string_name TSRMLS_CC))
return retval;
if (SUCCESS == zend_eval_string(code, retval, string_name TSRMLS_CC)) return SUCCESS;
} zend_catch {

} zend_end_try();

return retval;
return FAILURE;
}


Expand All @@ -112,10 +112,9 @@ zval *php_embed_eval_string(char *code, zval *retval_ptr, char *string_name TSRM
* Purpose: execute filename script. *
* *
******************************************************************************/
zval * php_embed_execute(char *filename TSRMLS_DC)
int php_embed_execute(char *filename, zval *retval TSRMLS_DC)
{
int ret = 0;
zval *retval = NULL;
zend_file_handle zfd;

zfd.type = ZEND_HANDLE_FILENAME;
Expand All @@ -125,9 +124,9 @@ zval * php_embed_execute(char *filename TSRMLS_DC)
zfd.opened_path = NULL;
zend_try {
if (SUCCESS == zend_execute_scripts(ZEND_REQUIRE TSRMLS_CC, &retval, 1, &zfd))
if (retval) return retval;
return SUCCESS;
} zend_end_try();
return retval;
return FAILURE;
}


Expand All @@ -151,7 +150,7 @@ static const zend_function_entry my_additional_functions[] = {
*/
int php_embed_minit(const char* hardcoded_ini PTSRMLS_DC)
{
#ifdef ZTS
#if defined(ZTS) && PHP_VERSION_ID < 70400
void ***tsrm_ls = NULL;
#endif
int ini_entries_len = 0;
Expand All @@ -168,11 +167,22 @@ int php_embed_minit(const char* hardcoded_ini PTSRMLS_DC)
#endif

#ifdef ZTS
#if PHP_VERSION_ID >= 70400
php_tsrm_startup();
#else
tsrm_startup(1, 1, 0, NULL);
tsrm_ls = ts_resource(0);
*ptsrm_ls = tsrm_ls;
#endif
#endif

#if PHP_VERSION_ID >= 70000 && defined(ZEND_SIGNALS)
#if HAVE_ZEND_SIGNAL_STARTUP
zend_signal_startup();
#elif defined(ZTS)
#error PHP is built with thread safety and broken signals.
#endif
#endif
if (hardcoded_ini!=NULL)
{
ini_entries_len = strlen(hardcoded_ini);
Expand Down
17 changes: 15 additions & 2 deletions php_embeded.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,22 @@
#include "TSRM.h"
#include "zend_exceptions.h"

#if PHP_VERSION_ID >= 70000
#define PTSRMLS_DC
#endif

/* php 8 */
#ifndef TSRMLS_CC
#define TSRMLS_CC
#define TSRMLS_DC
#define TSRMLS_D void
#define TSRMLS_C
#endif

int php_embed_minit(const char* hardcoded_ini PTSRMLS_DC);
int php_embed_rinit(TSRMLS_D);
zval * php_embed_eval_string(char *str, zval *retval_ptr, char *string_name TSRMLS_DC);
zval * php_embed_execute(char *filename TSRMLS_DC);
int php_embed_eval_string(char *code, zval *retval, char *string_name TSRMLS_DC);
int php_embed_execute(char *filename, zval *retval TSRMLS_DC);
void php_embed_mshutdown(TSRMLS_D);
void php_embed_rshutdown(TSRMLS_D);

Expand Down
Loading

0 comments on commit 378055d

Please sign in to comment.