pdo.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. | Author: Wez Furlong <wez@php.net> |
  14. | Marcus Boerger <helly@php.net> |
  15. | Sterling Hughes <sterling@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. #include <ctype.h>
  22. #include "php.h"
  23. #include "php_ini.h"
  24. #include "ext/standard/info.h"
  25. #include "php_pdo.h"
  26. #include "php_pdo_driver.h"
  27. #include "php_pdo_int.h"
  28. #include "zend_exceptions.h"
  29. #include "ext/spl/spl_exceptions.h"
  30. #include "pdo_arginfo.h"
  31. zend_class_entry *pdo_dbh_ce, *pdo_dbstmt_ce, *pdo_row_ce;
  32. /* for exceptional circumstances */
  33. zend_class_entry *pdo_exception_ce;
  34. /* True global resources - no need for thread safety here */
  35. /* the registry of PDO drivers */
  36. HashTable pdo_driver_hash;
  37. /* we use persistent resources for the driver connection stuff */
  38. static int le_ppdo;
  39. int php_pdo_list_entry(void) /* {{{ */
  40. {
  41. return le_ppdo;
  42. }
  43. /* }}} */
  44. PDO_API zend_class_entry *php_pdo_get_dbh_ce(void) /* {{{ */
  45. {
  46. return pdo_dbh_ce;
  47. }
  48. /* }}} */
  49. PDO_API zend_class_entry *php_pdo_get_exception(void) /* {{{ */
  50. {
  51. return pdo_exception_ce;
  52. }
  53. /* }}} */
  54. /* {{{ Return array of available PDO drivers */
  55. PHP_FUNCTION(pdo_drivers)
  56. {
  57. pdo_driver_t *pdriver;
  58. ZEND_PARSE_PARAMETERS_NONE();
  59. array_init(return_value);
  60. ZEND_HASH_FOREACH_PTR(&pdo_driver_hash, pdriver) {
  61. add_next_index_stringl(return_value, pdriver->driver_name, pdriver->driver_name_len);
  62. } ZEND_HASH_FOREACH_END();
  63. }
  64. /* }}} */
  65. /* {{{ pdo_functions[] */
  66. static const zend_module_dep pdo_deps[] = {
  67. ZEND_MOD_REQUIRED("spl")
  68. ZEND_MOD_END
  69. };
  70. /* }}} */
  71. /* {{{ pdo_module_entry */
  72. zend_module_entry pdo_module_entry = {
  73. STANDARD_MODULE_HEADER_EX, NULL,
  74. pdo_deps,
  75. "PDO",
  76. ext_functions,
  77. PHP_MINIT(pdo),
  78. PHP_MSHUTDOWN(pdo),
  79. NULL,
  80. NULL,
  81. PHP_MINFO(pdo),
  82. PHP_PDO_VERSION,
  83. STANDARD_MODULE_PROPERTIES
  84. };
  85. /* }}} */
  86. /* TODO: visit persistent handles: for each persistent statement handle,
  87. * remove bound parameter associations */
  88. #ifdef COMPILE_DL_PDO
  89. ZEND_GET_MODULE(pdo)
  90. #endif
  91. PDO_API zend_result php_pdo_register_driver(const pdo_driver_t *driver) /* {{{ */
  92. {
  93. if (driver->api_version != PDO_DRIVER_API) {
  94. zend_error(E_ERROR, "PDO: driver %s requires PDO API version " ZEND_ULONG_FMT "; this is PDO version %d",
  95. driver->driver_name, driver->api_version, PDO_DRIVER_API);
  96. return FAILURE;
  97. }
  98. if (!zend_hash_str_exists(&module_registry, "pdo", sizeof("pdo") - 1)) {
  99. zend_error(E_ERROR, "You MUST load PDO before loading any PDO drivers");
  100. return FAILURE; /* NOTREACHED */
  101. }
  102. return zend_hash_str_add_ptr(&pdo_driver_hash, driver->driver_name, driver->driver_name_len, (void*)driver) != NULL ? SUCCESS : FAILURE;
  103. }
  104. /* }}} */
  105. PDO_API void php_pdo_unregister_driver(const pdo_driver_t *driver) /* {{{ */
  106. {
  107. if (!zend_hash_str_exists(&module_registry, "pdo", sizeof("pdo") - 1)) {
  108. return;
  109. }
  110. zend_hash_str_del(&pdo_driver_hash, driver->driver_name, driver->driver_name_len);
  111. }
  112. /* }}} */
  113. pdo_driver_t *pdo_find_driver(const char *name, int namelen) /* {{{ */
  114. {
  115. return zend_hash_str_find_ptr(&pdo_driver_hash, name, namelen);
  116. }
  117. /* }}} */
  118. PDO_API int php_pdo_parse_data_source(const char *data_source, zend_ulong data_source_len, struct pdo_data_src_parser *parsed, int nparams) /* {{{ */
  119. {
  120. zend_ulong i;
  121. int j;
  122. int valstart = -1;
  123. int semi = -1;
  124. int optstart = 0;
  125. int nlen;
  126. int n_matches = 0;
  127. int n_semicolumns = 0;
  128. i = 0;
  129. while (i < data_source_len) {
  130. /* looking for NAME= */
  131. if (data_source[i] == '\0') {
  132. break;
  133. }
  134. if (data_source[i] != '=') {
  135. ++i;
  136. continue;
  137. }
  138. valstart = ++i;
  139. /* now we're looking for VALUE; or just VALUE<NUL> */
  140. semi = -1;
  141. n_semicolumns = 0;
  142. while (i < data_source_len) {
  143. if (data_source[i] == '\0') {
  144. semi = i++;
  145. break;
  146. }
  147. if (data_source[i] == ';') {
  148. if ((i + 1 >= data_source_len) || data_source[i+1] != ';') {
  149. semi = i++;
  150. break;
  151. } else {
  152. n_semicolumns++;
  153. i += 2;
  154. continue;
  155. }
  156. }
  157. ++i;
  158. }
  159. if (semi == -1) {
  160. semi = i;
  161. }
  162. /* find the entry in the array */
  163. nlen = valstart - optstart - 1;
  164. for (j = 0; j < nparams; j++) {
  165. if (0 == strncmp(data_source + optstart, parsed[j].optname, nlen) && parsed[j].optname[nlen] == '\0') {
  166. /* got a match */
  167. if (parsed[j].freeme) {
  168. efree(parsed[j].optval);
  169. }
  170. if (n_semicolumns == 0) {
  171. parsed[j].optval = estrndup(data_source + valstart, semi - valstart - n_semicolumns);
  172. } else {
  173. int vlen = semi - valstart;
  174. const char *orig_val = data_source + valstart;
  175. char *new_val = (char *) emalloc(vlen - n_semicolumns + 1);
  176. parsed[j].optval = new_val;
  177. while (vlen && *orig_val) {
  178. *new_val = *orig_val;
  179. new_val++;
  180. if (*orig_val == ';') {
  181. orig_val+=2;
  182. vlen-=2;
  183. } else {
  184. orig_val++;
  185. vlen--;
  186. }
  187. }
  188. *new_val = '\0';
  189. }
  190. parsed[j].freeme = 1;
  191. ++n_matches;
  192. break;
  193. }
  194. }
  195. while (i < data_source_len && isspace(data_source[i])) {
  196. i++;
  197. }
  198. optstart = i;
  199. }
  200. return n_matches;
  201. }
  202. /* }}} */
  203. /* {{{ PHP_MINIT_FUNCTION */
  204. PHP_MINIT_FUNCTION(pdo)
  205. {
  206. pdo_sqlstate_init_error_table();
  207. zend_hash_init(&pdo_driver_hash, 0, NULL, NULL, 1);
  208. le_ppdo = zend_register_list_destructors_ex(NULL, php_pdo_pdbh_dtor,
  209. "PDO persistent database", module_number);
  210. pdo_exception_ce = register_class_PDOException(spl_ce_RuntimeException);
  211. pdo_dbh_init();
  212. pdo_stmt_init();
  213. return SUCCESS;
  214. }
  215. /* }}} */
  216. /* {{{ PHP_MSHUTDOWN_FUNCTION */
  217. PHP_MSHUTDOWN_FUNCTION(pdo)
  218. {
  219. zend_hash_destroy(&pdo_driver_hash);
  220. pdo_sqlstate_fini_error_table();
  221. return SUCCESS;
  222. }
  223. /* }}} */
  224. /* {{{ PHP_MINFO_FUNCTION */
  225. PHP_MINFO_FUNCTION(pdo)
  226. {
  227. char *drivers = NULL, *ldrivers = estrdup("");
  228. pdo_driver_t *pdriver;
  229. php_info_print_table_start();
  230. php_info_print_table_header(2, "PDO support", "enabled");
  231. ZEND_HASH_FOREACH_PTR(&pdo_driver_hash, pdriver) {
  232. spprintf(&drivers, 0, "%s, %s", ldrivers, pdriver->driver_name);
  233. efree(ldrivers);
  234. ldrivers = drivers;
  235. } ZEND_HASH_FOREACH_END();
  236. php_info_print_table_row(2, "PDO drivers", drivers ? drivers + 2 : "");
  237. if (drivers) {
  238. efree(drivers);
  239. } else {
  240. efree(ldrivers);
  241. }
  242. php_info_print_table_end();
  243. }
  244. /* }}} */