dba.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Sascha Schumann <sascha@schumann.cx> |
  14. | Marcus Boerger <helly@php.net> |
  15. +----------------------------------------------------------------------+
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. #include "config.h"
  19. #endif
  20. #include "php.h"
  21. #if HAVE_DBA
  22. #include "php_ini.h"
  23. #include <stdio.h>
  24. #include <fcntl.h>
  25. #ifdef HAVE_SYS_FILE_H
  26. #include <sys/file.h>
  27. #endif
  28. #include "php_dba.h"
  29. #include "ext/standard/info.h"
  30. #include "ext/standard/php_string.h"
  31. #include "ext/standard/flock_compat.h"
  32. #include "php_gdbm.h"
  33. #include "php_ndbm.h"
  34. #include "php_dbm.h"
  35. #include "php_cdb.h"
  36. #include "php_db1.h"
  37. #include "php_db2.h"
  38. #include "php_db3.h"
  39. #include "php_db4.h"
  40. #include "php_flatfile.h"
  41. #include "php_inifile.h"
  42. #include "php_qdbm.h"
  43. #include "php_tcadb.h"
  44. #include "php_lmdb.h"
  45. #include "dba_arginfo.h"
  46. PHP_MINIT_FUNCTION(dba);
  47. PHP_MSHUTDOWN_FUNCTION(dba);
  48. PHP_MINFO_FUNCTION(dba);
  49. ZEND_BEGIN_MODULE_GLOBALS(dba)
  50. char *default_handler;
  51. dba_handler *default_hptr;
  52. ZEND_END_MODULE_GLOBALS(dba)
  53. ZEND_DECLARE_MODULE_GLOBALS(dba)
  54. #define DBA_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(dba, v)
  55. static PHP_GINIT_FUNCTION(dba);
  56. zend_module_entry dba_module_entry = {
  57. STANDARD_MODULE_HEADER,
  58. "dba",
  59. ext_functions,
  60. PHP_MINIT(dba),
  61. PHP_MSHUTDOWN(dba),
  62. NULL,
  63. NULL,
  64. PHP_MINFO(dba),
  65. PHP_DBA_VERSION,
  66. PHP_MODULE_GLOBALS(dba),
  67. PHP_GINIT(dba),
  68. NULL,
  69. NULL,
  70. STANDARD_MODULE_PROPERTIES_EX
  71. };
  72. #ifdef COMPILE_DL_DBA
  73. #ifdef ZTS
  74. ZEND_TSRMLS_CACHE_DEFINE()
  75. #endif
  76. ZEND_GET_MODULE(dba)
  77. #endif
  78. /* {{{ macromania */
  79. #define DBA_ID_PARS \
  80. zval *id; \
  81. dba_info *info = NULL; \
  82. int ac = ZEND_NUM_ARGS()
  83. /* these are used to get the standard arguments */
  84. /* {{{ php_dba_myke_key */
  85. static size_t php_dba_make_key(zval *key, char **key_str, char **key_free)
  86. {
  87. if (Z_TYPE_P(key) == IS_ARRAY) {
  88. zval *group, *name;
  89. HashPosition pos;
  90. size_t len;
  91. if (zend_hash_num_elements(Z_ARRVAL_P(key)) != 2) {
  92. zend_argument_error(NULL, 1, "must have exactly two elements: \"key\" and \"name\"");
  93. return 0;
  94. }
  95. zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(key), &pos);
  96. group = zend_hash_get_current_data_ex(Z_ARRVAL_P(key), &pos);
  97. zend_hash_move_forward_ex(Z_ARRVAL_P(key), &pos);
  98. name = zend_hash_get_current_data_ex(Z_ARRVAL_P(key), &pos);
  99. convert_to_string(group);
  100. convert_to_string(name);
  101. if (Z_STRLEN_P(group) == 0) {
  102. *key_str = Z_STRVAL_P(name);
  103. *key_free = NULL;
  104. return Z_STRLEN_P(name);
  105. }
  106. len = spprintf(key_str, 0, "[%s]%s", Z_STRVAL_P(group), Z_STRVAL_P(name));
  107. *key_free = *key_str;
  108. return len;
  109. } else {
  110. zval tmp;
  111. size_t len;
  112. ZVAL_COPY(&tmp, key);
  113. convert_to_string(&tmp);
  114. len = Z_STRLEN(tmp);
  115. if (len) {
  116. *key_free = *key_str = estrndup(Z_STRVAL(tmp), Z_STRLEN(tmp));
  117. }
  118. zval_ptr_dtor(&tmp);
  119. return len;
  120. }
  121. }
  122. /* }}} */
  123. #define DBA_GET2 \
  124. zval *key; \
  125. char *key_str, *key_free; \
  126. size_t key_len; \
  127. if (zend_parse_parameters(ac, "zr", &key, &id) == FAILURE) { \
  128. RETURN_THROWS(); \
  129. } \
  130. if ((key_len = php_dba_make_key(key, &key_str, &key_free)) == 0) {\
  131. RETURN_FALSE; \
  132. }
  133. #define DBA_GET2_3 \
  134. zval *key; \
  135. char *key_str, *key_free; \
  136. size_t key_len; \
  137. zend_long skip = 0; \
  138. switch(ac) { \
  139. case 2: \
  140. if (zend_parse_parameters(ac, "zr", &key, &id) == FAILURE) { \
  141. RETURN_THROWS(); \
  142. } \
  143. break; \
  144. case 3: \
  145. if (zend_parse_parameters(ac, "zlr", &key, &skip, &id) == FAILURE) { \
  146. RETURN_THROWS(); \
  147. } \
  148. break; \
  149. default: \
  150. WRONG_PARAM_COUNT; \
  151. } \
  152. if ((key_len = php_dba_make_key(key, &key_str, &key_free)) == 0) {\
  153. RETURN_FALSE; \
  154. }
  155. #define DBA_FETCH_RESOURCE(info, id) \
  156. if ((info = (dba_info *)zend_fetch_resource2(Z_RES_P(id), "DBA identifier", le_db, le_pdb)) == NULL) { \
  157. RETURN_THROWS(); \
  158. }
  159. #define DBA_FETCH_RESOURCE_WITH_ID(info, id) \
  160. if ((info = (dba_info *)zend_fetch_resource2(Z_RES_P(id), "DBA identifier", le_db, le_pdb)) == NULL) { \
  161. DBA_ID_DONE; \
  162. RETURN_THROWS(); \
  163. }
  164. #define DBA_ID_GET2 DBA_ID_PARS; DBA_GET2; DBA_FETCH_RESOURCE_WITH_ID(info, id)
  165. #define DBA_ID_GET2_3 DBA_ID_PARS; DBA_GET2_3; DBA_FETCH_RESOURCE_WITH_ID(info, id)
  166. #define DBA_ID_DONE \
  167. if (key_free) efree(key_free)
  168. /* a DBA handler must have specific routines */
  169. #define DBA_NAMED_HND(alias, name, flags) \
  170. {\
  171. #alias, flags, dba_open_##name, dba_close_##name, dba_fetch_##name, dba_update_##name, \
  172. dba_exists_##name, dba_delete_##name, dba_firstkey_##name, dba_nextkey_##name, \
  173. dba_optimize_##name, dba_sync_##name, dba_info_##name \
  174. },
  175. #define DBA_HND(name, flags) DBA_NAMED_HND(name, name, flags)
  176. /* check whether the user has write access */
  177. #define DBA_WRITE_CHECK \
  178. if(info->mode != DBA_WRITER && info->mode != DBA_TRUNC && info->mode != DBA_CREAT) { \
  179. php_error_docref(NULL, E_WARNING, "You cannot perform a modification to a database without proper access"); \
  180. RETURN_FALSE; \
  181. }
  182. /* the same check, but with a call to DBA_ID_DONE before returning */
  183. #define DBA_WRITE_CHECK_WITH_ID \
  184. if(info->mode != DBA_WRITER && info->mode != DBA_TRUNC && info->mode != DBA_CREAT) { \
  185. php_error_docref(NULL, E_WARNING, "You cannot perform a modification to a database without proper access"); \
  186. DBA_ID_DONE; \
  187. RETURN_FALSE; \
  188. }
  189. /* }}} */
  190. /* {{{ globals */
  191. static dba_handler handler[] = {
  192. #if DBA_GDBM
  193. DBA_HND(gdbm, DBA_LOCK_EXT) /* Locking done in library if set */
  194. #endif
  195. #if DBA_DBM
  196. DBA_HND(dbm, DBA_LOCK_ALL) /* No lock in lib */
  197. #endif
  198. #if DBA_NDBM
  199. DBA_HND(ndbm, DBA_LOCK_ALL) /* Could be done in library: filemode = 0644 + S_ENFMT */
  200. #endif
  201. #if DBA_CDB
  202. DBA_HND(cdb, DBA_STREAM_OPEN|DBA_LOCK_ALL) /* No lock in lib */
  203. #endif
  204. #if DBA_CDB_BUILTIN
  205. DBA_NAMED_HND(cdb_make, cdb, DBA_STREAM_OPEN|DBA_LOCK_ALL) /* No lock in lib */
  206. #endif
  207. #if DBA_DB1
  208. DBA_HND(db1, DBA_LOCK_ALL) /* No lock in lib */
  209. #endif
  210. #if DBA_DB2
  211. DBA_HND(db2, DBA_LOCK_ALL) /* No lock in lib */
  212. #endif
  213. #if DBA_DB3
  214. DBA_HND(db3, DBA_LOCK_ALL) /* No lock in lib */
  215. #endif
  216. #if DBA_DB4
  217. DBA_HND(db4, DBA_LOCK_ALL) /* No lock in lib */
  218. #endif
  219. #if DBA_INIFILE
  220. DBA_HND(inifile, DBA_STREAM_OPEN|DBA_LOCK_ALL|DBA_CAST_AS_FD) /* No lock in lib */
  221. #endif
  222. #if DBA_FLATFILE
  223. DBA_HND(flatfile, DBA_STREAM_OPEN|DBA_LOCK_ALL|DBA_NO_APPEND) /* No lock in lib */
  224. #endif
  225. #if DBA_QDBM
  226. DBA_HND(qdbm, DBA_LOCK_EXT)
  227. #endif
  228. #if DBA_TCADB
  229. DBA_HND(tcadb, DBA_LOCK_ALL)
  230. #endif
  231. #if DBA_LMDB
  232. DBA_HND(lmdb, DBA_LOCK_EXT)
  233. #endif
  234. { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
  235. };
  236. #if DBA_FLATFILE
  237. #define DBA_DEFAULT "flatfile"
  238. #elif DBA_DB4
  239. #define DBA_DEFAULT "db4"
  240. #elif DBA_DB3
  241. #define DBA_DEFAULT "db3"
  242. #elif DBA_DB2
  243. #define DBA_DEFAULT "db2"
  244. #elif DBA_DB1
  245. #define DBA_DEFAULT "db1"
  246. #elif DBA_GDBM
  247. #define DBA_DEFAULT "gdbm"
  248. #elif DBA_NBBM
  249. #define DBA_DEFAULT "ndbm"
  250. #elif DBA_DBM
  251. #define DBA_DEFAULT "dbm"
  252. #elif DBA_QDBM
  253. #define DBA_DEFAULT "qdbm"
  254. #elif DBA_TCADB
  255. #define DBA_DEFAULT "tcadb"
  256. #elif DBA_LMDB
  257. #define DBA_DEFAULT "lmdb"
  258. #else
  259. #define DBA_DEFAULT ""
  260. #endif
  261. /* cdb/cdb_make and ini are no option here */
  262. static int le_db;
  263. static int le_pdb;
  264. /* }}} */
  265. /* {{{ dba_fetch_resource
  266. PHPAPI void dba_fetch_resource(dba_info **pinfo, zval **id)
  267. {
  268. dba_info *info;
  269. DBA_ID_FETCH
  270. *pinfo = info;
  271. }
  272. */
  273. /* }}} */
  274. /* {{{ dba_get_handler
  275. PHPAPI dba_handler *dba_get_handler(const char* handler_name)
  276. {
  277. dba_handler *hptr;
  278. for (hptr = handler; hptr->name && strcasecmp(hptr->name, handler_name); hptr++);
  279. return hptr;
  280. }
  281. */
  282. /* }}} */
  283. /* {{{ dba_close */
  284. static void dba_close(dba_info *info)
  285. {
  286. if (info->hnd) {
  287. info->hnd->close(info);
  288. }
  289. if (info->path) {
  290. pefree(info->path, info->flags&DBA_PERSISTENT);
  291. }
  292. if (info->fp && info->fp != info->lock.fp) {
  293. if (info->flags & DBA_PERSISTENT) {
  294. php_stream_pclose(info->fp);
  295. } else {
  296. php_stream_close(info->fp);
  297. }
  298. }
  299. if (info->lock.fp) {
  300. if (info->flags & DBA_PERSISTENT) {
  301. php_stream_pclose(info->lock.fp);
  302. } else {
  303. php_stream_close(info->lock.fp);
  304. }
  305. }
  306. if (info->lock.name) {
  307. pefree(info->lock.name, info->flags&DBA_PERSISTENT);
  308. }
  309. pefree(info, info->flags&DBA_PERSISTENT);
  310. }
  311. /* }}} */
  312. /* {{{ dba_close_rsrc */
  313. static void dba_close_rsrc(zend_resource *rsrc)
  314. {
  315. dba_info *info = (dba_info *)rsrc->ptr;
  316. dba_close(info);
  317. }
  318. /* }}} */
  319. /* {{{ dba_close_pe_rsrc_deleter */
  320. int dba_close_pe_rsrc_deleter(zval *el, void *pDba)
  321. {
  322. if (Z_RES_P(el)->ptr == pDba) {
  323. if (Z_DELREF_P(el) == 0) {
  324. return ZEND_HASH_APPLY_REMOVE;
  325. } else {
  326. return ZEND_HASH_APPLY_KEEP | ZEND_HASH_APPLY_STOP;
  327. }
  328. } else {
  329. return ZEND_HASH_APPLY_KEEP;
  330. }
  331. }
  332. /* }}} */
  333. /* {{{ dba_close_pe_rsrc */
  334. static void dba_close_pe_rsrc(zend_resource *rsrc)
  335. {
  336. dba_info *info = (dba_info *)rsrc->ptr;
  337. /* closes the resource by calling dba_close_rsrc() */
  338. zend_hash_apply_with_argument(&EG(persistent_list), dba_close_pe_rsrc_deleter, info);
  339. }
  340. /* }}} */
  341. /* {{{ PHP_INI */
  342. ZEND_INI_MH(OnUpdateDefaultHandler)
  343. {
  344. dba_handler *hptr;
  345. if (!ZSTR_LEN(new_value)) {
  346. DBA_G(default_hptr) = NULL;
  347. return OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
  348. }
  349. for (hptr = handler; hptr->name && strcasecmp(hptr->name, ZSTR_VAL(new_value)); hptr++);
  350. if (!hptr->name) {
  351. php_error_docref(NULL, E_WARNING, "No such handler: %s", ZSTR_VAL(new_value));
  352. return FAILURE;
  353. }
  354. DBA_G(default_hptr) = hptr;
  355. return OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
  356. }
  357. PHP_INI_BEGIN()
  358. STD_PHP_INI_ENTRY("dba.default_handler", DBA_DEFAULT, PHP_INI_ALL, OnUpdateDefaultHandler, default_handler, zend_dba_globals, dba_globals)
  359. PHP_INI_END()
  360. /* }}} */
  361. /* {{{ PHP_GINIT_FUNCTION */
  362. static PHP_GINIT_FUNCTION(dba)
  363. {
  364. #if defined(COMPILE_DL_DBA) && defined(ZTS)
  365. ZEND_TSRMLS_CACHE_UPDATE();
  366. #endif
  367. dba_globals->default_handler = "";
  368. dba_globals->default_hptr = NULL;
  369. }
  370. /* }}} */
  371. /* {{{ PHP_MINIT_FUNCTION */
  372. PHP_MINIT_FUNCTION(dba)
  373. {
  374. REGISTER_INI_ENTRIES();
  375. le_db = zend_register_list_destructors_ex(dba_close_rsrc, NULL, "dba", module_number);
  376. le_pdb = zend_register_list_destructors_ex(dba_close_pe_rsrc, dba_close_rsrc, "dba persistent", module_number);
  377. return SUCCESS;
  378. }
  379. /* }}} */
  380. /* {{{ PHP_MSHUTDOWN_FUNCTION */
  381. PHP_MSHUTDOWN_FUNCTION(dba)
  382. {
  383. UNREGISTER_INI_ENTRIES();
  384. return SUCCESS;
  385. }
  386. /* }}} */
  387. #include "zend_smart_str.h"
  388. /* {{{ PHP_MINFO_FUNCTION */
  389. PHP_MINFO_FUNCTION(dba)
  390. {
  391. dba_handler *hptr;
  392. smart_str handlers = {0};
  393. for(hptr = handler; hptr->name; hptr++) {
  394. smart_str_appends(&handlers, hptr->name);
  395. smart_str_appendc(&handlers, ' ');
  396. }
  397. php_info_print_table_start();
  398. php_info_print_table_row(2, "DBA support", "enabled");
  399. if (handlers.s) {
  400. smart_str_0(&handlers);
  401. php_info_print_table_row(2, "Supported handlers", ZSTR_VAL(handlers.s));
  402. smart_str_free(&handlers);
  403. } else {
  404. php_info_print_table_row(2, "Supported handlers", "none");
  405. }
  406. php_info_print_table_end();
  407. DISPLAY_INI_ENTRIES();
  408. }
  409. /* }}} */
  410. /* {{{ php_dba_update */
  411. static void php_dba_update(INTERNAL_FUNCTION_PARAMETERS, int mode)
  412. {
  413. size_t val_len;
  414. zval *id;
  415. dba_info *info = NULL;
  416. int ac = ZEND_NUM_ARGS();
  417. zval *key;
  418. char *val;
  419. char *key_str, *key_free;
  420. size_t key_len;
  421. if (zend_parse_parameters(ac, "zsr", &key, &val, &val_len, &id) == FAILURE) {
  422. RETURN_THROWS();
  423. }
  424. if ((key_len = php_dba_make_key(key, &key_str, &key_free)) == 0) {
  425. RETURN_FALSE;
  426. }
  427. DBA_FETCH_RESOURCE_WITH_ID(info, id);
  428. DBA_WRITE_CHECK_WITH_ID;
  429. if (info->hnd->update(info, key_str, key_len, val, val_len, mode) == SUCCESS) {
  430. DBA_ID_DONE;
  431. RETURN_TRUE;
  432. }
  433. DBA_ID_DONE;
  434. RETURN_FALSE;
  435. }
  436. /* }}} */
  437. #define FREENOW if(args) {int i; for (i=0; i<ac; i++) { zval_ptr_dtor(&args[i]); } efree(args);} if(key) efree(key)
  438. /* {{{ php_find_dbm */
  439. dba_info *php_dba_find(const char* path)
  440. {
  441. zend_resource *le;
  442. dba_info *info;
  443. zend_long numitems, i;
  444. numitems = zend_hash_next_free_element(&EG(regular_list));
  445. for (i=1; i<numitems; i++) {
  446. if ((le = zend_hash_index_find_ptr(&EG(regular_list), i)) == NULL) {
  447. continue;
  448. }
  449. if (le->type == le_db || le->type == le_pdb) {
  450. info = (dba_info *)(le->ptr);
  451. if (!strcmp(info->path, path)) {
  452. return (dba_info *)(le->ptr);
  453. }
  454. }
  455. }
  456. return NULL;
  457. }
  458. /* }}} */
  459. /* {{{ php_dba_open */
  460. static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, int persistent)
  461. {
  462. zval *args = NULL;
  463. int ac = ZEND_NUM_ARGS();
  464. dba_mode_t modenr;
  465. dba_info *info, *other;
  466. dba_handler *hptr;
  467. char *key = NULL, *error = NULL;
  468. size_t keylen = 0;
  469. int i;
  470. int lock_mode, lock_flag, lock_dbf = 0;
  471. char *file_mode;
  472. char mode[4], *pmode, *lock_file_mode = NULL;
  473. int persistent_flag = persistent ? STREAM_OPEN_PERSISTENT : 0;
  474. zend_string *opened_path = NULL;
  475. char *lock_name;
  476. #ifdef PHP_WIN32
  477. bool restarted = 0;
  478. bool need_creation = 0;
  479. #endif
  480. if (ac < 2) {
  481. WRONG_PARAM_COUNT;
  482. }
  483. /* we pass additional args to the respective handler */
  484. args = safe_emalloc(ac, sizeof(zval), 0);
  485. if (zend_get_parameters_array_ex(ac, args) != SUCCESS) {
  486. efree(args);
  487. WRONG_PARAM_COUNT;
  488. }
  489. /* we only take string arguments */
  490. for (i = 0; i < ac; i++) {
  491. ZVAL_STR(&args[i], zval_get_string(&args[i]));
  492. keylen += Z_STRLEN(args[i]);
  493. }
  494. /* Exception during string conversion */
  495. if (EG(exception)) {
  496. FREENOW;
  497. RETURN_THROWS();
  498. }
  499. if (persistent) {
  500. zend_resource *le;
  501. /* calculate hash */
  502. key = safe_emalloc(keylen, 1, 1);
  503. key[keylen] = '\0';
  504. keylen = 0;
  505. for(i = 0; i < ac; i++) {
  506. memcpy(key+keylen, Z_STRVAL(args[i]), Z_STRLEN(args[i]));
  507. keylen += Z_STRLEN(args[i]);
  508. }
  509. /* try to find if we already have this link in our persistent list */
  510. if ((le = zend_hash_str_find_ptr(&EG(persistent_list), key, keylen)) != NULL) {
  511. FREENOW;
  512. if (le->type != le_pdb) {
  513. RETURN_FALSE;
  514. }
  515. info = (dba_info *)le->ptr;
  516. GC_ADDREF(le);
  517. RETURN_RES(zend_register_resource(info, le_pdb));
  518. return;
  519. }
  520. }
  521. if (ac==2) {
  522. hptr = DBA_G(default_hptr);
  523. if (!hptr) {
  524. php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "No default handler selected");
  525. FREENOW;
  526. RETURN_FALSE;
  527. }
  528. } else {
  529. for (hptr = handler; hptr->name && strcasecmp(hptr->name, Z_STRVAL(args[2])); hptr++);
  530. }
  531. if (!hptr->name) {
  532. php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "No such handler: %s", Z_STRVAL(args[2]));
  533. FREENOW;
  534. RETURN_FALSE;
  535. }
  536. /* Check mode: [rwnc][fl]?t?
  537. * r: Read
  538. * w: Write
  539. * n: Create/Truncate
  540. * c: Create
  541. *
  542. * d: force lock on database file
  543. * l: force lock on lck file
  544. * -: ignore locking
  545. *
  546. * t: test open database, warning if locked
  547. */
  548. strlcpy(mode, Z_STRVAL(args[1]), sizeof(mode));
  549. pmode = &mode[0];
  550. if (pmode[0] && (pmode[1]=='d' || pmode[1]=='l' || pmode[1]=='-')) { /* force lock on db file or lck file or disable locking */
  551. switch (pmode[1]) {
  552. case 'd':
  553. lock_dbf = 1;
  554. if ((hptr->flags & DBA_LOCK_ALL) == 0) {
  555. lock_flag = (hptr->flags & DBA_LOCK_ALL);
  556. break;
  557. }
  558. ZEND_FALLTHROUGH;
  559. case 'l':
  560. lock_flag = DBA_LOCK_ALL;
  561. if ((hptr->flags & DBA_LOCK_ALL) == 0) {
  562. php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_NOTICE, "Handler %s does locking internally", hptr->name);
  563. }
  564. break;
  565. default:
  566. case '-':
  567. if ((hptr->flags & DBA_LOCK_ALL) == 0) {
  568. php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "Locking cannot be disabled for handler %s", hptr->name);
  569. FREENOW;
  570. RETURN_FALSE;
  571. }
  572. lock_flag = 0;
  573. break;
  574. }
  575. } else {
  576. lock_flag = (hptr->flags&DBA_LOCK_ALL);
  577. lock_dbf = 1;
  578. }
  579. switch (*pmode++) {
  580. case 'r':
  581. modenr = DBA_READER;
  582. lock_mode = (lock_flag & DBA_LOCK_READER) ? LOCK_SH : 0;
  583. file_mode = "r";
  584. break;
  585. case 'w':
  586. modenr = DBA_WRITER;
  587. lock_mode = (lock_flag & DBA_LOCK_WRITER) ? LOCK_EX : 0;
  588. file_mode = "r+b";
  589. break;
  590. case 'c': {
  591. #ifdef PHP_WIN32
  592. if (hptr->flags & (DBA_NO_APPEND|DBA_CAST_AS_FD)) {
  593. php_stream_statbuf ssb;
  594. need_creation = (SUCCESS != php_stream_stat_path(Z_STRVAL(args[0]), &ssb));
  595. }
  596. #endif
  597. modenr = DBA_CREAT;
  598. lock_mode = (lock_flag & DBA_LOCK_CREAT) ? LOCK_EX : 0;
  599. if (lock_mode) {
  600. if (lock_dbf) {
  601. /* the create/append check will be done on the lock
  602. * when the lib opens the file it is already created
  603. */
  604. file_mode = "r+b"; /* read & write, seek 0 */
  605. #ifdef PHP_WIN32
  606. if (!need_creation) {
  607. lock_file_mode = "r+b";
  608. } else
  609. #endif
  610. lock_file_mode = "a+b"; /* append */
  611. } else {
  612. #ifdef PHP_WIN32
  613. if (!need_creation) {
  614. file_mode = "r+b";
  615. } else
  616. #endif
  617. file_mode = "a+b"; /* append */
  618. lock_file_mode = "w+b"; /* create/truncate */
  619. }
  620. } else {
  621. #ifdef PHP_WIN32
  622. if (!need_creation) {
  623. file_mode = "r+b";
  624. } else
  625. #endif
  626. file_mode = "a+b";
  627. }
  628. /* In case of the 'a+b' append mode, the handler is responsible
  629. * to handle any rewind problems (see flatfile handler).
  630. */
  631. break;
  632. }
  633. case 'n':
  634. modenr = DBA_TRUNC;
  635. lock_mode = (lock_flag & DBA_LOCK_TRUNC) ? LOCK_EX : 0;
  636. file_mode = "w+b";
  637. break;
  638. default:
  639. php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "Illegal DBA mode");
  640. FREENOW;
  641. RETURN_FALSE;
  642. }
  643. if (!lock_file_mode) {
  644. lock_file_mode = file_mode;
  645. }
  646. if (*pmode=='d' || *pmode=='l' || *pmode=='-') {
  647. pmode++; /* done already - skip here */
  648. }
  649. if (*pmode=='t') {
  650. pmode++;
  651. if (!lock_flag) {
  652. php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "You cannot combine modifiers - (no lock) and t (test lock)");
  653. FREENOW;
  654. RETURN_FALSE;
  655. }
  656. if (!lock_mode) {
  657. if ((hptr->flags & DBA_LOCK_ALL) == 0) {
  658. php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "Handler %s uses its own locking which doesn't support mode modifier t (test lock)", hptr->name);
  659. FREENOW;
  660. RETURN_FALSE;
  661. } else {
  662. php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "Handler %s doesn't uses locking for this mode which makes modifier t (test lock) obsolete", hptr->name);
  663. FREENOW;
  664. RETURN_FALSE;
  665. }
  666. } else {
  667. lock_mode |= LOCK_NB; /* test =: non blocking */
  668. }
  669. }
  670. if (*pmode) {
  671. php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "Illegal DBA mode");
  672. FREENOW;
  673. RETURN_FALSE;
  674. }
  675. info = pemalloc(sizeof(dba_info), persistent);
  676. memset(info, 0, sizeof(dba_info));
  677. info->path = pestrdup(Z_STRVAL(args[0]), persistent);
  678. info->mode = modenr;
  679. info->argc = ac - 3;
  680. info->argv = args + 3;
  681. info->flags = (hptr->flags & ~DBA_LOCK_ALL) | (lock_flag & DBA_LOCK_ALL) | (persistent ? DBA_PERSISTENT : 0);
  682. info->lock.mode = lock_mode;
  683. /* if any open call is a locking call:
  684. * check if we already habe a locking call open that should block this call
  685. * the problem is some systems would allow read during write
  686. */
  687. if (hptr->flags & DBA_LOCK_ALL) {
  688. if ((other = php_dba_find(info->path)) != NULL) {
  689. if ( ( (lock_mode&LOCK_EX) && (other->lock.mode&(LOCK_EX|LOCK_SH)) )
  690. || ( (other->lock.mode&LOCK_EX) && (lock_mode&(LOCK_EX|LOCK_SH)) )
  691. ) {
  692. error = "Unable to establish lock (database file already open)"; /* force failure exit */
  693. }
  694. }
  695. }
  696. #ifdef PHP_WIN32
  697. restart:
  698. #endif
  699. if (!error && lock_mode) {
  700. if (lock_dbf) {
  701. lock_name = Z_STRVAL(args[0]);
  702. } else {
  703. spprintf(&lock_name, 0, "%s.lck", info->path);
  704. if (!strcmp(file_mode, "r")) {
  705. /* when in read only mode try to use existing .lck file first */
  706. /* do not log errors for .lck file while in read only mode on .lck file */
  707. lock_file_mode = "rb";
  708. info->lock.fp = php_stream_open_wrapper(lock_name, lock_file_mode, STREAM_MUST_SEEK|IGNORE_PATH|persistent_flag, &opened_path);
  709. }
  710. if (!info->lock.fp) {
  711. /* when not in read mode or failed to open .lck file read only. now try again in create(write) mode and log errors */
  712. lock_file_mode = "a+b";
  713. } else {
  714. if (opened_path) {
  715. info->lock.name = pestrndup(ZSTR_VAL(opened_path), ZSTR_LEN(opened_path), persistent);
  716. zend_string_release_ex(opened_path, 0);
  717. }
  718. }
  719. }
  720. if (!info->lock.fp) {
  721. info->lock.fp = php_stream_open_wrapper(lock_name, lock_file_mode, STREAM_MUST_SEEK|REPORT_ERRORS|IGNORE_PATH|persistent_flag, &opened_path);
  722. if (info->lock.fp) {
  723. if (lock_dbf) {
  724. /* replace the path info with the real path of the opened file */
  725. pefree(info->path, persistent);
  726. info->path = pestrndup(ZSTR_VAL(opened_path), ZSTR_LEN(opened_path), persistent);
  727. }
  728. /* now store the name of the lock */
  729. info->lock.name = pestrndup(ZSTR_VAL(opened_path), ZSTR_LEN(opened_path), persistent);
  730. zend_string_release_ex(opened_path, 0);
  731. }
  732. }
  733. if (!lock_dbf) {
  734. efree(lock_name);
  735. }
  736. if (!info->lock.fp) {
  737. dba_close(info);
  738. /* stream operation already wrote an error message */
  739. FREENOW;
  740. RETURN_FALSE;
  741. }
  742. if (!php_stream_supports_lock(info->lock.fp)) {
  743. error = "Stream does not support locking";
  744. }
  745. if (php_stream_lock(info->lock.fp, lock_mode)) {
  746. error = "Unable to establish lock"; /* force failure exit */
  747. }
  748. }
  749. /* centralised open stream for builtin */
  750. if (!error && (hptr->flags&DBA_STREAM_OPEN)==DBA_STREAM_OPEN) {
  751. if (info->lock.fp && lock_dbf) {
  752. info->fp = info->lock.fp; /* use the same stream for locking and database access */
  753. } else {
  754. info->fp = php_stream_open_wrapper(info->path, file_mode, STREAM_MUST_SEEK|REPORT_ERRORS|IGNORE_PATH|persistent_flag, NULL);
  755. }
  756. if (!info->fp) {
  757. dba_close(info);
  758. /* stream operation already wrote an error message */
  759. FREENOW;
  760. RETURN_FALSE;
  761. }
  762. if (hptr->flags & (DBA_NO_APPEND|DBA_CAST_AS_FD)) {
  763. /* Needed because some systems do not allow to write to the original
  764. * file contents with O_APPEND being set.
  765. */
  766. if (SUCCESS != php_stream_cast(info->fp, PHP_STREAM_AS_FD, (void*)&info->fd, 1)) {
  767. php_error_docref(NULL, E_WARNING, "Could not cast stream");
  768. dba_close(info);
  769. FREENOW;
  770. RETURN_FALSE;
  771. #ifdef F_SETFL
  772. } else if (modenr == DBA_CREAT) {
  773. int flags = fcntl(info->fd, F_GETFL);
  774. fcntl(info->fd, F_SETFL, flags & ~O_APPEND);
  775. #elif defined(PHP_WIN32)
  776. } else if (modenr == DBA_CREAT && need_creation && !restarted) {
  777. if (info->lock.fp != NULL) {
  778. php_stream_free(info->lock.fp, persistent ? PHP_STREAM_FREE_CLOSE_PERSISTENT : PHP_STREAM_FREE_CLOSE);
  779. }
  780. if (info->fp != info->lock.fp) {
  781. php_stream_free(info->fp, persistent ? PHP_STREAM_FREE_CLOSE_PERSISTENT : PHP_STREAM_FREE_CLOSE);
  782. }
  783. info->fp = NULL;
  784. info->lock.fp = NULL;
  785. info->fd = -1;
  786. pefree(info->lock.name, persistent);
  787. lock_file_mode = "r+b";
  788. restarted = 1;
  789. goto restart;
  790. #endif
  791. }
  792. }
  793. }
  794. if (error || hptr->open(info, &error) != SUCCESS) {
  795. dba_close(info);
  796. php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "Driver initialization failed for handler: %s%s%s", hptr->name, error?": ":"", error?error:"");
  797. FREENOW;
  798. RETURN_FALSE;
  799. }
  800. info->hnd = hptr;
  801. info->argc = 0;
  802. info->argv = NULL;
  803. if (persistent) {
  804. if (zend_register_persistent_resource(key, keylen, info, le_pdb) == NULL) {
  805. dba_close(info);
  806. php_error_docref2(NULL, Z_STRVAL(args[0]), Z_STRVAL(args[1]), E_WARNING, "Could not register persistent resource");
  807. FREENOW;
  808. RETURN_FALSE;
  809. }
  810. }
  811. RETVAL_RES(zend_register_resource(info, (persistent ? le_pdb : le_db)));
  812. FREENOW;
  813. }
  814. /* }}} */
  815. #undef FREENOW
  816. /* {{{ Opens path using the specified handler in mode persistently */
  817. PHP_FUNCTION(dba_popen)
  818. {
  819. php_dba_open(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  820. }
  821. /* }}} */
  822. /* {{{ Opens path using the specified handler in mode*/
  823. PHP_FUNCTION(dba_open)
  824. {
  825. php_dba_open(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  826. }
  827. /* }}} */
  828. /* {{{ Closes database */
  829. PHP_FUNCTION(dba_close)
  830. {
  831. zval *id;
  832. dba_info *info = NULL;
  833. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &id) == FAILURE) {
  834. RETURN_THROWS();
  835. }
  836. DBA_FETCH_RESOURCE(info, id);
  837. zend_list_close(Z_RES_P(id));
  838. }
  839. /* }}} */
  840. /* {{{ Checks, if the specified key exists */
  841. PHP_FUNCTION(dba_exists)
  842. {
  843. DBA_ID_GET2;
  844. if(info->hnd->exists(info, key_str, key_len) == SUCCESS) {
  845. DBA_ID_DONE;
  846. RETURN_TRUE;
  847. }
  848. DBA_ID_DONE;
  849. RETURN_FALSE;
  850. }
  851. /* }}} */
  852. /* {{{ Fetches the data associated with key */
  853. PHP_FUNCTION(dba_fetch)
  854. {
  855. char *val;
  856. size_t len = 0;
  857. DBA_ID_GET2_3;
  858. if (ac==3) {
  859. if (!strcmp(info->hnd->name, "cdb")) {
  860. if (skip < 0) {
  861. php_error_docref(NULL, E_NOTICE, "Handler %s accepts only skip values greater than or equal to zero, using skip=0", info->hnd->name);
  862. skip = 0;
  863. }
  864. } else if (!strcmp(info->hnd->name, "inifile")) {
  865. /* "-1" is comparable to 0 but allows a non restrictive
  866. * access which is faster. For example 'inifile' uses this
  867. * to allow faster access when the key was already found
  868. * using firstkey/nextkey. However explicitly setting the
  869. * value to 0 ensures the first value.
  870. */
  871. if (skip < -1) {
  872. php_error_docref(NULL, E_NOTICE, "Handler %s accepts only skip value -1 and greater, using skip=0", info->hnd->name);
  873. skip = 0;
  874. }
  875. } else {
  876. php_error_docref(NULL, E_NOTICE, "Handler %s does not support optional skip parameter, the value will be ignored", info->hnd->name);
  877. skip = 0;
  878. }
  879. } else {
  880. skip = 0;
  881. }
  882. if((val = info->hnd->fetch(info, key_str, key_len, skip, &len)) != NULL) {
  883. DBA_ID_DONE;
  884. RETVAL_STRINGL(val, len);
  885. efree(val);
  886. return;
  887. }
  888. DBA_ID_DONE;
  889. RETURN_FALSE;
  890. }
  891. /* }}} */
  892. /* {{{ Splits an inifile key into an array of the form array(0=>group,1=>value_name) but returns false if input is false or null */
  893. PHP_FUNCTION(dba_key_split)
  894. {
  895. zval *zkey;
  896. char *key, *name;
  897. size_t key_len;
  898. if (ZEND_NUM_ARGS() != 1) {
  899. WRONG_PARAM_COUNT;
  900. }
  901. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "z", &zkey) == SUCCESS) {
  902. if (Z_TYPE_P(zkey) == IS_NULL || (Z_TYPE_P(zkey) == IS_FALSE)) {
  903. RETURN_BOOL(0);
  904. }
  905. }
  906. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &key, &key_len) == FAILURE) {
  907. RETURN_THROWS();
  908. }
  909. array_init(return_value);
  910. if (key[0] == '[' && (name = strchr(key, ']')) != NULL) {
  911. add_next_index_stringl(return_value, key+1, name - (key + 1));
  912. add_next_index_stringl(return_value, name+1, key_len - (name - key + 1));
  913. } else {
  914. add_next_index_stringl(return_value, "", 0);
  915. add_next_index_stringl(return_value, key, key_len);
  916. }
  917. }
  918. /* }}} */
  919. /* {{{ Resets the internal key pointer and returns the first key */
  920. PHP_FUNCTION(dba_firstkey)
  921. {
  922. char *fkey;
  923. size_t len;
  924. zval *id;
  925. dba_info *info = NULL;
  926. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &id) == FAILURE) {
  927. RETURN_THROWS();
  928. }
  929. DBA_FETCH_RESOURCE(info, id);
  930. fkey = info->hnd->firstkey(info, &len);
  931. if (fkey) {
  932. RETVAL_STRINGL(fkey, len);
  933. efree(fkey);
  934. return;
  935. }
  936. RETURN_FALSE;
  937. }
  938. /* }}} */
  939. /* {{{ Returns the next key */
  940. PHP_FUNCTION(dba_nextkey)
  941. {
  942. char *nkey;
  943. size_t len;
  944. zval *id;
  945. dba_info *info = NULL;
  946. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &id) == FAILURE) {
  947. RETURN_THROWS();
  948. }
  949. DBA_FETCH_RESOURCE(info, id);
  950. nkey = info->hnd->nextkey(info, &len);
  951. if (nkey) {
  952. RETVAL_STRINGL(nkey, len);
  953. efree(nkey);
  954. return;
  955. }
  956. RETURN_FALSE;
  957. }
  958. /* }}} */
  959. /* {{{ Deletes the entry associated with key
  960. If inifile: remove all other key lines */
  961. PHP_FUNCTION(dba_delete)
  962. {
  963. DBA_ID_GET2;
  964. DBA_WRITE_CHECK_WITH_ID;
  965. if(info->hnd->delete(info, key_str, key_len) == SUCCESS)
  966. {
  967. DBA_ID_DONE;
  968. RETURN_TRUE;
  969. }
  970. DBA_ID_DONE;
  971. RETURN_FALSE;
  972. }
  973. /* }}} */
  974. /* {{{ If not inifile: Insert value as key, return false, if key exists already
  975. If inifile: Add vakue as key (next instance of key) */
  976. PHP_FUNCTION(dba_insert)
  977. {
  978. php_dba_update(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  979. }
  980. /* }}} */
  981. /* {{{ Inserts value as key, replaces key, if key exists already
  982. If inifile: remove all other key lines */
  983. PHP_FUNCTION(dba_replace)
  984. {
  985. php_dba_update(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  986. }
  987. /* }}} */
  988. /* {{{ Optimizes (e.g. clean up, vacuum) database */
  989. PHP_FUNCTION(dba_optimize)
  990. {
  991. zval *id;
  992. dba_info *info = NULL;
  993. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &id) == FAILURE) {
  994. RETURN_THROWS();
  995. }
  996. DBA_FETCH_RESOURCE(info, id);
  997. DBA_WRITE_CHECK;
  998. if (info->hnd->optimize(info) == SUCCESS) {
  999. RETURN_TRUE;
  1000. }
  1001. RETURN_FALSE;
  1002. }
  1003. /* }}} */
  1004. /* {{{ Synchronizes database */
  1005. PHP_FUNCTION(dba_sync)
  1006. {
  1007. zval *id;
  1008. dba_info *info = NULL;
  1009. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &id) == FAILURE) {
  1010. RETURN_THROWS();
  1011. }
  1012. DBA_FETCH_RESOURCE(info, id);
  1013. if (info->hnd->sync(info) == SUCCESS) {
  1014. RETURN_TRUE;
  1015. }
  1016. RETURN_FALSE;
  1017. }
  1018. /* }}} */
  1019. /* {{{ List configured database handlers */
  1020. PHP_FUNCTION(dba_handlers)
  1021. {
  1022. dba_handler *hptr;
  1023. bool full_info = 0;
  1024. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &full_info) == FAILURE) {
  1025. RETURN_THROWS();
  1026. }
  1027. array_init(return_value);
  1028. for(hptr = handler; hptr->name; hptr++) {
  1029. if (full_info) {
  1030. // TODO: avoid reallocation ???
  1031. char *str = hptr->info(hptr, NULL);
  1032. add_assoc_string(return_value, hptr->name, str);
  1033. efree(str);
  1034. } else {
  1035. add_next_index_string(return_value, hptr->name);
  1036. }
  1037. }
  1038. }
  1039. /* }}} */
  1040. /* {{{ List opened databases */
  1041. PHP_FUNCTION(dba_list)
  1042. {
  1043. zend_ulong numitems, i;
  1044. zend_resource *le;
  1045. dba_info *info;
  1046. if (zend_parse_parameters_none() == FAILURE) {
  1047. RETURN_THROWS();
  1048. }
  1049. array_init(return_value);
  1050. numitems = zend_hash_next_free_element(&EG(regular_list));
  1051. for (i=1; i<numitems; i++) {
  1052. if ((le = zend_hash_index_find_ptr(&EG(regular_list), i)) == NULL) {
  1053. continue;
  1054. }
  1055. if (le->type == le_db || le->type == le_pdb) {
  1056. info = (dba_info *)(le->ptr);
  1057. add_index_string(return_value, i, info->path);
  1058. }
  1059. }
  1060. }
  1061. /* }}} */
  1062. #endif /* HAVE_DBA */