php_ibase_udf.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Ard Biesheuvel <a.k.biesheuvel@ewi.tudelft.nl> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /**
  19. * This UDF library adds the ability to call PHP functions from SQL
  20. * statements. Because of SQL's strong typing, you will have to declare
  21. * an external function for every combination { output type, #args } that
  22. * your application requires.
  23. *
  24. * Declare the functions like this:
  25. *
  26. * DECLARE EXTERNAL FUNCTION CALL_PHP1
  27. * CSTRING(xx),
  28. * <return type> BY DESCRIPTOR,
  29. * INTEGER BY DESCRIPTOR
  30. * RETURNS PARAMETER 2
  31. * ENTRY_POINT 'udf_call_php1' MODULE_NAME 'php_ibase_udf'
  32. *
  33. * DECLARE EXTERNAL FUNCTION CALL_PHP2
  34. * CSTRING(xx),
  35. * <return type> BY DESCRIPTOR,
  36. * INTEGER BY DESCRIPTOR,
  37. * INTEGER BY DESCRIPTOR
  38. * RETURNS PARAMETER 2
  39. * ENTRY_POINT 'udf_call_php2' MODULE_NAME 'php_ibase_udf'
  40. *
  41. * ... and so on. [for up to 8 input arguments]
  42. *
  43. * The first input parameter contains the name of the PHP function you want
  44. * to call. The second argument is the result. (omit this argument when calling
  45. * the function) The return type of the function is the declared type of the
  46. * result. The value returned from the PHP function being called will
  47. * automatically be converted if necessary.
  48. * The arguments should have their types declared as well, but you're free
  49. * to pass arguments of other types instead. They will be converted to the
  50. * best matching PHP type before being passed to the PHP function.
  51. *
  52. * The declared functions can be called from SQL like:
  53. *
  54. * SELECT * FROM <table> WHERE CALL_PHP1('soundex',<field>) NOT LIKE ?
  55. * or
  56. * UPDATE <table> SET <field> = CALL_PHP1('ucwords',<field>)
  57. *
  58. * Additionally, there's a function 'exec_php' which allows the contents
  59. * of text BLOB fields to be parsed and executed by PHP. This is most useful
  60. * for declaring functions that can then be called with CALL_PHPx.
  61. *
  62. * DECLARE EXTERNAL FUNCTION EXEC_PHP
  63. * BLOB,
  64. * INTEGER BY DESCRIPTOR,
  65. * SMALLINT
  66. * RETURNS PARAMETER 2
  67. * ENTRY_POINT 'exec_php' MODULE_NAME 'php_ibase_udf'
  68. *
  69. * The function will return 1 if execution succeeded and 0 if an error
  70. * occurred. The result that is returned from the executed PHP code is
  71. * ignored. You can pass a non-zero value as second argument to force
  72. * the embedded PHP engine to re-initialise.
  73. *
  74. * There are several ways to build this library, depending on which way the
  75. * database is accessed. If you're using the classic server on a local
  76. * connection, you should compile the library like this:
  77. *
  78. * gcc -shared `php-config --includes` `php-config --ldflags` \
  79. * `php-config --libs` -o php_ibase_udf.so php_ibase_udf.c
  80. *
  81. * If you connect to the classic server by TCP/IP, you should build the
  82. * PHP embedded static library and link against that.
  83. *
  84. * gcc -shared `php-config --includes` `php-config --ldflags` \
  85. * `php-config --libs` -o php_ibase_udf.so php_ibase_udf.c \
  86. * /usr/lib/libphp5.a
  87. *
  88. * If you use the super server, you should also link against the embedded
  89. * library, but be sure to enable thread safety, as the super server is
  90. * multi-threaded. After building, copy the resulting file to the folder
  91. * where your database expects to find its UDFs.
  92. */
  93. #include "zend.h"
  94. #include "zend_API.h"
  95. #include "php.h"
  96. #include "php_ini.h"
  97. #include "ibase.h"
  98. #define min(a,b) ((a)<(b)?(a):(b))
  99. #ifdef PHP_WIN32
  100. #define LL_LIT(lit) lit ## I64
  101. #else
  102. #define LL_LIT(lit) lit ## ll
  103. #endif
  104. #ifdef ZTS
  105. # include <pthread.h>
  106. static void ***tsrm_ls;
  107. pthread_mutex_t mtx_res = PTHREAD_MUTEX_INITIALIZER;
  108. #define LOCK() do { pthread_mutex_lock(&mtx_res); } while (0)
  109. #define UNLOCK() do { pthread_mutex_unlock(&mtx_res); } while (0)
  110. #else
  111. #define LOCK()
  112. #define UNLOCK()
  113. #endif
  114. #ifdef PHP_EMBED
  115. # include "php_main.h"
  116. # include "sapi/embed/php_embed.h"
  117. static void __attribute__((constructor)) init()
  118. {
  119. php_embed_init(0, NULL PTSRMLS_CC);
  120. }
  121. static void __attribute__((destructor)) fini()
  122. {
  123. php_embed_shutdown(TSRMLS_C);
  124. }
  125. #endif
  126. /**
  127. * Gets the contents of the BLOB b and offers it to Zend for parsing/execution
  128. */
  129. void exec_php(BLOBCALLBACK b, PARAMDSC *res, ISC_SHORT *init)
  130. {
  131. int result, remaining = b->blob_total_length, i = 0;
  132. char *code = pemalloc(remaining+1, 1);
  133. ISC_USHORT read;
  134. for (code[remaining] = '\0'; remaining > 0; remaining -= read)
  135. b->blob_get_segment(b->blob_handle, &code[i++<<16],min(0x10000,remaining), &read);
  136. LOCK();
  137. switch (init && *init) {
  138. default:
  139. #ifdef PHP_EMBED
  140. php_request_shutdown(NULL);
  141. if (FAILURE == (result = php_request_startup(TSRMLS_C))) {
  142. break;
  143. }
  144. case 0:
  145. #endif
  146. /* feed it to the parser */
  147. zend_first_try {
  148. result = zend_eval_stringl(code, b->blob_total_length, NULL, "Firebird Embedded PHP engine" TSRMLS_CC);
  149. } zend_end_try();
  150. }
  151. UNLOCK();
  152. free(code);
  153. res->dsc_dtype = dtype_long;
  154. *(ISC_LONG*)res->dsc_address = (result == SUCCESS);
  155. }
  156. static ISC_INT64 const scales[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 100000000, 1000000000,
  157. 1000000000, LL_LIT(10000000000),LL_LIT(100000000000),LL_LIT(10000000000000),LL_LIT(100000000000000),
  158. LL_LIT(1000000000000000),LL_LIT(1000000000000000),LL_LIT(1000000000000000000) };
  159. static void call_php(char *name, PARAMDSC *r, int argc, PARAMDSC **argv)
  160. {
  161. do {
  162. zval callback, args[4], *argp[4], return_value;
  163. PARAMVARY *res = (PARAMVARY*)r->dsc_address;
  164. int i;
  165. INIT_ZVAL(callback);
  166. ZVAL_STRING(&callback,name,0);
  167. LOCK();
  168. /* check if the requested function exists */
  169. if (!zend_is_callable(&callback, 0, NULL TSRMLS_CC)) {
  170. break;
  171. }
  172. UNLOCK();
  173. /* create the argument array */
  174. for (i = 0; i < argc; ++i) {
  175. INIT_ZVAL(args[i]);
  176. argp[i] = &args[i];
  177. /* test arg for null */
  178. if (argv[i]->dsc_flags & DSC_null) {
  179. ZVAL_NULL(argp[i]);
  180. continue;
  181. }
  182. switch (argv[i]->dsc_dtype) {
  183. ISC_INT64 l;
  184. struct tm t;
  185. char const *fmt;
  186. char d[64];
  187. case dtype_cstring:
  188. ZVAL_STRING(argp[i], (char*)argv[i]->dsc_address,0);
  189. break;
  190. case dtype_text:
  191. ZVAL_STRINGL(argp[i], (char*)argv[i]->dsc_address, argv[i]->dsc_length,0);
  192. break;
  193. case dtype_varying:
  194. ZVAL_STRINGL(argp[i], ((PARAMVARY*)argv[i]->dsc_address)->vary_string,
  195. ((PARAMVARY*)argv[i]->dsc_address)->vary_length,0);
  196. break;
  197. case dtype_short:
  198. if (argv[i]->dsc_scale == 0) {
  199. ZVAL_LONG(argp[i], *(short*)argv[i]->dsc_address);
  200. } else {
  201. ZVAL_DOUBLE(argp[i],
  202. ((double)*(short*)argv[i]->dsc_address)/scales[-argv[i]->dsc_scale]);
  203. }
  204. break;
  205. case dtype_long:
  206. if (argv[i]->dsc_scale == 0) {
  207. ZVAL_LONG(argp[i], *(ISC_LONG*)argv[i]->dsc_address);
  208. } else {
  209. ZVAL_DOUBLE(argp[i],
  210. ((double)*(ISC_LONG*)argv[i]->dsc_address)/scales[-argv[i]->dsc_scale]);
  211. }
  212. break;
  213. case dtype_int64:
  214. l = *(ISC_INT64*)argv[i]->dsc_address;
  215. if (argv[i]->dsc_scale == 0 && l <= LONG_MAX && l >= LONG_MIN) {
  216. ZVAL_LONG(argp[i], (long)l);
  217. } else {
  218. ZVAL_DOUBLE(argp[i], ((double)l)/scales[-argv[i]->dsc_scale]);
  219. }
  220. break;
  221. case dtype_real:
  222. ZVAL_DOUBLE(argp[i], *(float*)argv[i]->dsc_address);
  223. break;
  224. case dtype_double:
  225. ZVAL_DOUBLE(argp[i], *(double*)argv[i]->dsc_address);
  226. break;
  227. case dtype_sql_date:
  228. isc_decode_sql_date((ISC_DATE*)argv[i]->dsc_address, &t);
  229. ZVAL_STRINGL(argp[i], d, strftime(d, sizeof(d), INI_STR("ibase.dateformat"), &t),1);
  230. break;
  231. case dtype_sql_time:
  232. isc_decode_sql_time((ISC_TIME*)argv[i]->dsc_address, &t);
  233. ZVAL_STRINGL(argp[i], d, strftime(d, sizeof(d), INI_STR("ibase.timeformat"), &t),1);
  234. break;
  235. case dtype_timestamp:
  236. isc_decode_timestamp((ISC_TIMESTAMP*)argv[i]->dsc_address, &t);
  237. ZVAL_STRINGL(argp[i], d, strftime(d, sizeof(d), INI_STR("ibase.timestampformat"), &t),1);
  238. break;
  239. }
  240. }
  241. LOCK();
  242. /* now call the function */
  243. if (FAILURE == call_user_function(EG(function_table), NULL,
  244. &callback, &return_value, argc, argp TSRMLS_CC)) {
  245. UNLOCK();
  246. break;
  247. }
  248. UNLOCK();
  249. for (i = 0; i < argc; ++i) {
  250. switch (argv[i]->dsc_dtype) {
  251. case dtype_sql_date:
  252. case dtype_sql_time:
  253. case dtype_timestamp:
  254. zval_dtor(argp[i]);
  255. }
  256. }
  257. /* return whatever type we got back from the callback: let DB handle conversion */
  258. switch (Z_TYPE(return_value)) {
  259. case IS_LONG:
  260. r->dsc_dtype = dtype_long;
  261. *(long*)r->dsc_address = Z_LVAL(return_value);
  262. r->dsc_length = sizeof(long);
  263. break;
  264. case IS_DOUBLE:
  265. r->dsc_dtype = dtype_double;
  266. *(double*)r->dsc_address = Z_DVAL(return_value);
  267. r->dsc_length = sizeof(double);
  268. break;
  269. case IS_NULL:
  270. r->dsc_flags |= DSC_null;
  271. break;
  272. default:
  273. convert_to_string(&return_value);
  274. case IS_STRING:
  275. r->dsc_dtype = dtype_varying;
  276. memcpy(res->vary_string, Z_STRVAL(return_value),
  277. (res->vary_length = min(r->dsc_length-2,Z_STRLEN(return_value))));
  278. r->dsc_length = res->vary_length+2;
  279. break;
  280. }
  281. zval_dtor(&return_value);
  282. return;
  283. } while (0);
  284. /**
  285. * If we end up here, we should report an error back to the DB engine, but
  286. * that's not possible. We can however report it back to PHP.
  287. */
  288. LOCK();
  289. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error calling function '%s' from database", name);
  290. UNLOCK();
  291. }
  292. /* Entry points for the DB engine */
  293. void udf_call_php1(char *name, PARAMDSC *r, PARAMDSC *arg1)
  294. {
  295. PARAMDSC *args[1] = { arg1 };
  296. call_php(name, r, 1, args);
  297. }
  298. void udf_call_php2(char *name, PARAMDSC *r, PARAMDSC *arg1, PARAMDSC *arg2)
  299. {
  300. PARAMDSC *args[2] = { arg1, arg2 };
  301. call_php(name, r, 2, args);
  302. }
  303. void udf_call_php3(char *name, PARAMDSC *r, PARAMDSC *arg1, PARAMDSC *arg2, PARAMDSC *arg3)
  304. {
  305. PARAMDSC *args[3] = { arg1, arg2, arg3 };
  306. call_php(name, r, 3, args);
  307. }
  308. void udf_call_php4(char *name, PARAMDSC *r, PARAMDSC *arg1, PARAMDSC *arg2, PARAMDSC *arg3,
  309. PARAMDSC *arg4)
  310. {
  311. PARAMDSC *args[4] = { arg1, arg2, arg3, arg4 };
  312. call_php(name, r, 4, args);
  313. }
  314. void udf_call_php5(char *name, PARAMDSC *r, PARAMDSC *arg1, PARAMDSC *arg2, PARAMDSC *arg3,
  315. PARAMDSC *arg4, PARAMDSC *arg5)
  316. {
  317. PARAMDSC *args[5] = { arg1, arg2, arg3, arg4, arg5 };
  318. call_php(name, r, 5, args);
  319. }
  320. void udf_call_php6(char *name, PARAMDSC *r, PARAMDSC *arg1, PARAMDSC *arg2, PARAMDSC *arg3,
  321. PARAMDSC *arg4, PARAMDSC *arg5, PARAMDSC *arg6)
  322. {
  323. PARAMDSC *args[6] = { arg1, arg2, arg3, arg4, arg5, arg6 };
  324. call_php(name, r, 6, args);
  325. }
  326. void udf_call_php7(char *name, PARAMDSC *r, PARAMDSC *arg1, PARAMDSC *arg2, PARAMDSC *arg3,
  327. PARAMDSC *arg4, PARAMDSC *arg5, PARAMDSC *arg6, PARAMDSC *arg7)
  328. {
  329. PARAMDSC *args[7] = { arg1, arg2, arg3, arg4, arg5, arg6, arg7 };
  330. call_php(name, r, 7, args);
  331. }
  332. void udf_call_php8(char *name, PARAMDSC *r, PARAMDSC *arg1, PARAMDSC *arg2, PARAMDSC *arg3,
  333. PARAMDSC *arg4, PARAMDSC *arg5, PARAMDSC *arg6, PARAMDSC *arg7, PARAMDSC *arg8)
  334. {
  335. PARAMDSC *args[8] = { arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8 };
  336. call_php(name, r, 8, args);
  337. }