pdo.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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: Wez Furlong <wez@php.net> |
  16. | Marcus Boerger <helly@php.net> |
  17. | Sterling Hughes <sterling@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. /* $Id$ */
  21. #ifdef HAVE_CONFIG_H
  22. #include "config.h"
  23. #endif
  24. #include <ctype.h>
  25. #include "php.h"
  26. #include "php_ini.h"
  27. #include "ext/standard/info.h"
  28. #include "php_pdo.h"
  29. #include "php_pdo_driver.h"
  30. #include "php_pdo_int.h"
  31. #include "zend_exceptions.h"
  32. static zend_class_entry *spl_ce_RuntimeException;
  33. ZEND_DECLARE_MODULE_GLOBALS(pdo)
  34. static PHP_GINIT_FUNCTION(pdo);
  35. /* True global resources - no need for thread safety here */
  36. /* the registry of PDO drivers */
  37. HashTable pdo_driver_hash;
  38. /* we use persistent resources for the driver connection stuff */
  39. static int le_ppdo;
  40. int php_pdo_list_entry(void)
  41. {
  42. return le_ppdo;
  43. }
  44. /* for exceptional circumstances */
  45. zend_class_entry *pdo_exception_ce;
  46. PDO_API zend_class_entry *php_pdo_get_dbh_ce(void)
  47. {
  48. return pdo_dbh_ce;
  49. }
  50. PDO_API zend_class_entry *php_pdo_get_exception(void)
  51. {
  52. return pdo_exception_ce;
  53. }
  54. PDO_API char *php_pdo_str_tolower_dup(const char *src, int len)
  55. {
  56. char *dest = emalloc(len + 1);
  57. zend_str_tolower_copy(dest, src, len);
  58. return dest;
  59. }
  60. PDO_API zend_class_entry *php_pdo_get_exception_base(int root TSRMLS_DC)
  61. {
  62. #if can_handle_soft_dependency_on_SPL && defined(HAVE_SPL) && ((PHP_MAJOR_VERSION > 5) || (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 1))
  63. if (!root) {
  64. if (!spl_ce_RuntimeException) {
  65. zend_class_entry **pce;
  66. if (zend_hash_find(CG(class_table), "runtimeexception", sizeof("RuntimeException"), (void **) &pce) == SUCCESS) {
  67. spl_ce_RuntimeException = *pce;
  68. return *pce;
  69. }
  70. } else {
  71. return spl_ce_RuntimeException;
  72. }
  73. }
  74. #endif
  75. #if (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 2)
  76. return zend_exception_get_default();
  77. #else
  78. return zend_exception_get_default(TSRMLS_C);
  79. #endif
  80. }
  81. zend_class_entry *pdo_dbh_ce, *pdo_dbstmt_ce, *pdo_row_ce;
  82. /* {{{ proto array pdo_drivers()
  83. Return array of available PDO drivers */
  84. PHP_FUNCTION(pdo_drivers)
  85. {
  86. HashPosition pos;
  87. pdo_driver_t **pdriver;
  88. if (zend_parse_parameters_none() == FAILURE) {
  89. return;
  90. }
  91. array_init(return_value);
  92. zend_hash_internal_pointer_reset_ex(&pdo_driver_hash, &pos);
  93. while (SUCCESS == zend_hash_get_current_data_ex(&pdo_driver_hash, (void**)&pdriver, &pos)) {
  94. add_next_index_stringl(return_value, (char*)(*pdriver)->driver_name, (*pdriver)->driver_name_len, 1);
  95. zend_hash_move_forward_ex(&pdo_driver_hash, &pos);
  96. }
  97. }
  98. /* }}} */
  99. /* {{{ arginfo */
  100. ZEND_BEGIN_ARG_INFO(arginfo_pdo_drivers, 0)
  101. ZEND_END_ARG_INFO()
  102. /* }}} */
  103. /* {{{ pdo_functions[] */
  104. const zend_function_entry pdo_functions[] = {
  105. PHP_FE(pdo_drivers, arginfo_pdo_drivers)
  106. PHP_FE_END
  107. };
  108. /* }}} */
  109. /* {{{ pdo_functions[] */
  110. #if ZEND_MODULE_API_NO >= 20050922
  111. static const zend_module_dep pdo_deps[] = {
  112. #ifdef HAVE_SPL
  113. ZEND_MOD_REQUIRED("spl")
  114. #endif
  115. ZEND_MOD_END
  116. };
  117. #endif
  118. /* }}} */
  119. /* {{{ pdo_module_entry */
  120. zend_module_entry pdo_module_entry = {
  121. #if ZEND_MODULE_API_NO >= 20050922
  122. STANDARD_MODULE_HEADER_EX, NULL,
  123. pdo_deps,
  124. #else
  125. STANDARD_MODULE_HEADER,
  126. #endif
  127. "PDO",
  128. pdo_functions,
  129. PHP_MINIT(pdo),
  130. PHP_MSHUTDOWN(pdo),
  131. NULL,
  132. NULL,
  133. PHP_MINFO(pdo),
  134. "1.0.4dev",
  135. PHP_MODULE_GLOBALS(pdo),
  136. PHP_GINIT(pdo),
  137. NULL,
  138. NULL,
  139. STANDARD_MODULE_PROPERTIES_EX
  140. };
  141. /* }}} */
  142. /* TODO: visit persistent handles: for each persistent statement handle,
  143. * remove bound parameter associations */
  144. #ifdef COMPILE_DL_PDO
  145. ZEND_GET_MODULE(pdo)
  146. #endif
  147. /* {{{ PHP_GINIT_FUNCTION */
  148. static PHP_GINIT_FUNCTION(pdo)
  149. {
  150. pdo_globals->global_value = 0;
  151. }
  152. /* }}} */
  153. PDO_API int php_pdo_register_driver(pdo_driver_t *driver)
  154. {
  155. if (driver->api_version != PDO_DRIVER_API) {
  156. zend_error(E_ERROR, "PDO: driver %s requires PDO API version %ld; this is PDO version %d",
  157. driver->driver_name, driver->api_version, PDO_DRIVER_API);
  158. return FAILURE;
  159. }
  160. if (!zend_hash_exists(&module_registry, "pdo", sizeof("pdo"))) {
  161. zend_error(E_ERROR, "You MUST load PDO before loading any PDO drivers");
  162. return FAILURE; /* NOTREACHED */
  163. }
  164. return zend_hash_add(&pdo_driver_hash, (char*)driver->driver_name, driver->driver_name_len,
  165. (void**)&driver, sizeof(pdo_driver_t *), NULL);
  166. }
  167. PDO_API void php_pdo_unregister_driver(pdo_driver_t *driver)
  168. {
  169. if (!zend_hash_exists(&module_registry, "pdo", sizeof("pdo"))) {
  170. return;
  171. }
  172. zend_hash_del(&pdo_driver_hash, (char*)driver->driver_name, driver->driver_name_len);
  173. }
  174. pdo_driver_t *pdo_find_driver(const char *name, int namelen)
  175. {
  176. pdo_driver_t **driver = NULL;
  177. zend_hash_find(&pdo_driver_hash, (char*)name, namelen, (void**)&driver);
  178. return driver ? *driver : NULL;
  179. }
  180. PDO_API int php_pdo_parse_data_source(const char *data_source,
  181. unsigned long data_source_len, struct pdo_data_src_parser *parsed,
  182. int nparams)
  183. {
  184. int i, j;
  185. int valstart = -1;
  186. int semi = -1;
  187. int optstart = 0;
  188. int nlen;
  189. int n_matches = 0;
  190. int n_semicolumns = 0;
  191. i = 0;
  192. while (i < data_source_len) {
  193. /* looking for NAME= */
  194. if (data_source[i] == '\0') {
  195. break;
  196. }
  197. if (data_source[i] != '=') {
  198. ++i;
  199. continue;
  200. }
  201. valstart = ++i;
  202. /* now we're looking for VALUE; or just VALUE<NUL> */
  203. semi = -1;
  204. n_semicolumns = 0;
  205. while (i < data_source_len) {
  206. if (data_source[i] == '\0') {
  207. semi = i++;
  208. break;
  209. }
  210. if (data_source[i] == ';') {
  211. if ((i + 1 >= data_source_len) || data_source[i+1] != ';') {
  212. semi = i++;
  213. break;
  214. } else {
  215. n_semicolumns++;
  216. i += 2;
  217. continue;
  218. }
  219. }
  220. ++i;
  221. }
  222. if (semi == -1) {
  223. semi = i;
  224. }
  225. /* find the entry in the array */
  226. nlen = valstart - optstart - 1;
  227. for (j = 0; j < nparams; j++) {
  228. if (0 == strncmp(data_source + optstart, parsed[j].optname, nlen) && parsed[j].optname[nlen] == '\0') {
  229. /* got a match */
  230. if (parsed[j].freeme) {
  231. efree(parsed[j].optval);
  232. }
  233. if (n_semicolumns == 0) {
  234. parsed[j].optval = estrndup(data_source + valstart, semi - valstart - n_semicolumns);
  235. } else {
  236. int vlen = semi - valstart;
  237. const char *orig_val = data_source + valstart;
  238. char *new_val = (char *) emalloc(vlen - n_semicolumns + 1);
  239. parsed[j].optval = new_val;
  240. while (vlen && *orig_val) {
  241. *new_val = *orig_val;
  242. new_val++;
  243. if (*orig_val == ';') {
  244. orig_val+=2;
  245. vlen-=2;
  246. } else {
  247. orig_val++;
  248. vlen--;
  249. }
  250. }
  251. *new_val = '\0';
  252. }
  253. parsed[j].freeme = 1;
  254. ++n_matches;
  255. break;
  256. }
  257. }
  258. while (i < data_source_len && isspace(data_source[i])) {
  259. i++;
  260. }
  261. optstart = i;
  262. }
  263. return n_matches;
  264. }
  265. static const char digit_vec[] = "0123456789";
  266. PDO_API char *php_pdo_int64_to_str(pdo_int64_t i64 TSRMLS_DC)
  267. {
  268. char buffer[65];
  269. char outbuf[65] = "";
  270. register char *p;
  271. long long_val;
  272. char *dst = outbuf;
  273. if (i64 < 0) {
  274. i64 = -i64;
  275. *dst++ = '-';
  276. }
  277. if (i64 == 0) {
  278. *dst++ = '0';
  279. *dst++ = '\0';
  280. return estrdup(outbuf);
  281. }
  282. p = &buffer[sizeof(buffer)-1];
  283. *p = '\0';
  284. while ((pdo_uint64_t)i64 > (pdo_uint64_t)LONG_MAX) {
  285. pdo_uint64_t quo = (pdo_uint64_t)i64 / (unsigned int)10;
  286. unsigned int rem = (unsigned int)(i64 - quo*10U);
  287. *--p = digit_vec[rem];
  288. i64 = (pdo_int64_t)quo;
  289. }
  290. long_val = (long)i64;
  291. while (long_val != 0) {
  292. long quo = long_val / 10;
  293. *--p = digit_vec[(unsigned int)(long_val - quo * 10)];
  294. long_val = quo;
  295. }
  296. while ((*dst++ = *p++) != 0)
  297. ;
  298. *dst = '\0';
  299. return estrdup(outbuf);
  300. }
  301. /* {{{ PHP_MINIT_FUNCTION */
  302. PHP_MINIT_FUNCTION(pdo)
  303. {
  304. zend_class_entry ce;
  305. spl_ce_RuntimeException = NULL;
  306. if (FAILURE == pdo_sqlstate_init_error_table()) {
  307. return FAILURE;
  308. }
  309. zend_hash_init(&pdo_driver_hash, 0, NULL, NULL, 1);
  310. le_ppdo = zend_register_list_destructors_ex(NULL, php_pdo_pdbh_dtor,
  311. "PDO persistent database", module_number);
  312. INIT_CLASS_ENTRY(ce, "PDOException", NULL);
  313. pdo_exception_ce = zend_register_internal_class_ex(&ce, php_pdo_get_exception_base(0 TSRMLS_CC), NULL TSRMLS_CC);
  314. zend_declare_property_null(pdo_exception_ce, "errorInfo", sizeof("errorInfo")-1, ZEND_ACC_PUBLIC TSRMLS_CC);
  315. pdo_dbh_init(TSRMLS_C);
  316. pdo_stmt_init(TSRMLS_C);
  317. return SUCCESS;
  318. }
  319. /* }}} */
  320. /* {{{ PHP_MSHUTDOWN_FUNCTION */
  321. PHP_MSHUTDOWN_FUNCTION(pdo)
  322. {
  323. zend_hash_destroy(&pdo_driver_hash);
  324. pdo_sqlstate_fini_error_table();
  325. return SUCCESS;
  326. }
  327. /* }}} */
  328. /* {{{ PHP_MINFO_FUNCTION */
  329. PHP_MINFO_FUNCTION(pdo)
  330. {
  331. HashPosition pos;
  332. char *drivers = NULL, *ldrivers = estrdup("");
  333. pdo_driver_t **pdriver;
  334. php_info_print_table_start();
  335. php_info_print_table_header(2, "PDO support", "enabled");
  336. zend_hash_internal_pointer_reset_ex(&pdo_driver_hash, &pos);
  337. while (SUCCESS == zend_hash_get_current_data_ex(&pdo_driver_hash, (void**)&pdriver, &pos)) {
  338. spprintf(&drivers, 0, "%s, %s", ldrivers, (*pdriver)->driver_name);
  339. zend_hash_move_forward_ex(&pdo_driver_hash, &pos);
  340. efree(ldrivers);
  341. ldrivers = drivers;
  342. }
  343. php_info_print_table_row(2, "PDO drivers", drivers ? drivers+2 : "");
  344. if (drivers) {
  345. efree(drivers);
  346. } else {
  347. efree(ldrivers);
  348. }
  349. php_info_print_table_end();
  350. }
  351. /* }}} */
  352. /*
  353. * Local variables:
  354. * tab-width: 4
  355. * c-basic-offset: 4
  356. * End:
  357. * vim600: noet sw=4 ts=4 fdm=marker
  358. * vim<600: noet sw=4 ts=4
  359. */