dba.c 32 KB

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