#include "zend.h"
#include "zend_API.h"
#include "php.h"
#include "php_ini.h"
#include "ibase.h"
#define min(a,b) ((a)<(b)?(a):(b))
#ifdef PHP_WIN32
#define LL_LIT(lit) lit ## I64
#else
#define LL_LIT(lit) lit ## ll
#endif
#ifdef ZTS
# include <pthread.h>
static void ***tsrm_ls;
pthread_mutex_t mtx_res = PTHREAD_MUTEX_INITIALIZER;
#define LOCK() do { pthread_mutex_lock(&mtx_res); } while (0)
#define UNLOCK() do { pthread_mutex_unlock(&mtx_res); } while (0)
#else
#define LOCK()
#define UNLOCK()
#endif
#ifdef PHP_EMBED
# include "php_main.h"
# include "sapi/embed/php_embed.h"
static void __attribute__((constructor)) init()
{
php_embed_init(0, NULL PTSRMLS_CC);
}
static void __attribute__((destructor)) fini()
{
php_embed_shutdown(TSRMLS_C);
}
#endif
void exec_php(BLOBCALLBACK b, PARAMDSC *res, ISC_SHORT *init)
{
int result, remaining = b->blob_total_length, i = 0;
char *code = pemalloc(remaining+1, 1);
ISC_USHORT read;
for (code[remaining] = '\0'; remaining > 0; remaining -= read)
b->blob_get_segment(b->blob_handle, &code[i++<<16],min(0x10000,remaining), &read);
LOCK();
switch (init && *init) {
default:
#ifdef PHP_EMBED
php_request_shutdown(NULL);
if (FAILURE == (result = php_request_startup(TSRMLS_C))) {
break;
}
case 0:
#endif
zend_first_try {
result = zend_eval_stringl(code, b->blob_total_length, NULL, "Firebird Embedded PHP engine" TSRMLS_CC);
} zend_end_try();
}
UNLOCK();
free(code);
res->dsc_dtype = dtype_long;
*(ISC_LONG*)res->dsc_address = (result == SUCCESS);
}
static ISC_INT64 const scales[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 100000000, 1000000000,
1000000000, LL_LIT(10000000000),LL_LIT(100000000000),LL_LIT(10000000000000),LL_LIT(100000000000000),
LL_LIT(1000000000000000),LL_LIT(1000000000000000),LL_LIT(1000000000000000000) };
static void call_php(char *name, PARAMDSC *r, int argc, PARAMDSC **argv)
{
do {
zval callback, args[4], *argp[4], return_value;
PARAMVARY *res = (PARAMVARY*)r->dsc_address;
int i;
INIT_ZVAL(callback);
ZVAL_STRING(&callback,name,0);
LOCK();
if (!zend_is_callable(&callback, 0, NULL TSRMLS_CC)) {
break;
}
UNLOCK();
for (i = 0; i < argc; ++i) {
INIT_ZVAL(args[i]);
argp[i] = &args[i];
if (argv[i]->dsc_flags & DSC_null) {
ZVAL_NULL(argp[i]);
continue;
}
switch (argv[i]->dsc_dtype) {
ISC_INT64 l;
struct tm t;
char const *fmt;
char d[64];
case dtype_cstring:
ZVAL_STRING(argp[i], (char*)argv[i]->dsc_address,0);
break;
case dtype_text:
ZVAL_STRINGL(argp[i], (char*)argv[i]->dsc_address, argv[i]->dsc_length,0);
break;
case dtype_varying:
ZVAL_STRINGL(argp[i], ((PARAMVARY*)argv[i]->dsc_address)->vary_string,
((PARAMVARY*)argv[i]->dsc_address)->vary_length,0);
break;
case dtype_short:
if (argv[i]->dsc_scale == 0) {
ZVAL_LONG(argp[i], *(short*)argv[i]->dsc_address);
} else {
ZVAL_DOUBLE(argp[i],
((double)*(short*)argv[i]->dsc_address)/scales[-argv[i]->dsc_scale]);
}
break;
case dtype_long:
if (argv[i]->dsc_scale == 0) {
ZVAL_LONG(argp[i], *(ISC_LONG*)argv[i]->dsc_address);
} else {
ZVAL_DOUBLE(argp[i],
((double)*(ISC_LONG*)argv[i]->dsc_address)/scales[-argv[i]->dsc_scale]);
}
break;
case dtype_int64:
l = *(ISC_INT64*)argv[i]->dsc_address;
if (argv[i]->dsc_scale == 0 && l <= LONG_MAX && l >= LONG_MIN) {
ZVAL_LONG(argp[i], (long)l);
} else {
ZVAL_DOUBLE(argp[i], ((double)l)/scales[-argv[i]->dsc_scale]);
}
break;
case dtype_real:
ZVAL_DOUBLE(argp[i], *(float*)argv[i]->dsc_address);
break;
case dtype_double:
ZVAL_DOUBLE(argp[i], *(double*)argv[i]->dsc_address);
break;
case dtype_sql_date:
isc_decode_sql_date((ISC_DATE*)argv[i]->dsc_address, &t);
ZVAL_STRINGL(argp[i], d, strftime(d, sizeof(d), INI_STR("ibase.dateformat"), &t),1);
break;
case dtype_sql_time:
isc_decode_sql_time((ISC_TIME*)argv[i]->dsc_address, &t);
ZVAL_STRINGL(argp[i], d, strftime(d, sizeof(d), INI_STR("ibase.timeformat"), &t),1);
break;
case dtype_timestamp:
isc_decode_timestamp((ISC_TIMESTAMP*)argv[i]->dsc_address, &t);
ZVAL_STRINGL(argp[i], d, strftime(d, sizeof(d), INI_STR("ibase.timestampformat"), &t),1);
break;
}
}
LOCK();
if (FAILURE == call_user_function(EG(function_table), NULL,
&callback, &return_value, argc, argp TSRMLS_CC)) {
UNLOCK();
break;
}
UNLOCK();
for (i = 0; i < argc; ++i) {
switch (argv[i]->dsc_dtype) {
case dtype_sql_date:
case dtype_sql_time:
case dtype_timestamp:
zval_dtor(argp[i]);
}
}
switch (Z_TYPE(return_value)) {
case IS_LONG:
r->dsc_dtype = dtype_long;
*(long*)r->dsc_address = Z_LVAL(return_value);
r->dsc_length = sizeof(long);
break;
case IS_DOUBLE:
r->dsc_dtype = dtype_double;
*(double*)r->dsc_address = Z_DVAL(return_value);
r->dsc_length = sizeof(double);
break;
case IS_NULL:
r->dsc_flags |= DSC_null;
break;
default:
convert_to_string(&return_value);
case IS_STRING:
r->dsc_dtype = dtype_varying;
memcpy(res->vary_string, Z_STRVAL(return_value),
(res->vary_length = min(r->dsc_length-2,Z_STRLEN(return_value))));
r->dsc_length = res->vary_length+2;
break;
}
zval_dtor(&return_value);
return;
} while (0);
LOCK();
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error calling function '%s' from database", name);
UNLOCK();
}
void udf_call_php1(char *name, PARAMDSC *r, PARAMDSC *arg1)
{
PARAMDSC *args[1] = { arg1 };
call_php(name, r, 1, args);
}
void udf_call_php2(char *name, PARAMDSC *r, PARAMDSC *arg1, PARAMDSC *arg2)
{
PARAMDSC *args[2] = { arg1, arg2 };
call_php(name, r, 2, args);
}
void udf_call_php3(char *name, PARAMDSC *r, PARAMDSC *arg1, PARAMDSC *arg2, PARAMDSC *arg3)
{
PARAMDSC *args[3] = { arg1, arg2, arg3 };
call_php(name, r, 3, args);
}
void udf_call_php4(char *name, PARAMDSC *r, PARAMDSC *arg1, PARAMDSC *arg2, PARAMDSC *arg3,
PARAMDSC *arg4)
{
PARAMDSC *args[4] = { arg1, arg2, arg3, arg4 };
call_php(name, r, 4, args);
}
void udf_call_php5(char *name, PARAMDSC *r, PARAMDSC *arg1, PARAMDSC *arg2, PARAMDSC *arg3,
PARAMDSC *arg4, PARAMDSC *arg5)
{
PARAMDSC *args[5] = { arg1, arg2, arg3, arg4, arg5 };
call_php(name, r, 5, args);
}
void udf_call_php6(char *name, PARAMDSC *r, PARAMDSC *arg1, PARAMDSC *arg2, PARAMDSC *arg3,
PARAMDSC *arg4, PARAMDSC *arg5, PARAMDSC *arg6)
{
PARAMDSC *args[6] = { arg1, arg2, arg3, arg4, arg5, arg6 };
call_php(name, r, 6, args);
}
void udf_call_php7(char *name, PARAMDSC *r, PARAMDSC *arg1, PARAMDSC *arg2, PARAMDSC *arg3,
PARAMDSC *arg4, PARAMDSC *arg5, PARAMDSC *arg6, PARAMDSC *arg7)
{
PARAMDSC *args[7] = { arg1, arg2, arg3, arg4, arg5, arg6, arg7 };
call_php(name, r, 7, args);
}
void udf_call_php8(char *name, PARAMDSC *r, PARAMDSC *arg1, PARAMDSC *arg2, PARAMDSC *arg3,
PARAMDSC *arg4, PARAMDSC *arg5, PARAMDSC *arg6, PARAMDSC *arg7, PARAMDSC *arg8)
{
PARAMDSC *args[8] = { arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8 };
call_php(name, r, 8, args);
}