pdo_mysql.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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: George Schlossnagle <george@omniti.com> |
  16. | Johannes Schlueter <johannes@mysql.com> |
  17. +----------------------------------------------------------------------+
  18. */
  19. /* $Id$ */
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "php.h"
  24. #include "php_ini.h"
  25. #include "ext/standard/info.h"
  26. #include "pdo/php_pdo.h"
  27. #include "pdo/php_pdo_driver.h"
  28. #include "php_pdo_mysql.h"
  29. #include "php_pdo_mysql_int.h"
  30. #ifdef COMPILE_DL_PDO_MYSQL
  31. ZEND_GET_MODULE(pdo_mysql)
  32. #endif
  33. ZEND_DECLARE_MODULE_GLOBALS(pdo_mysql)
  34. /*
  35. The default socket location is sometimes defined by configure.
  36. With libmysql `mysql_config --socket` will fill PDO_MYSQL_UNIX_ADDR
  37. and the user can use --with-mysql-sock=SOCKET which will fill
  38. PDO_MYSQL_UNIX_ADDR. If both aren't set we're using mysqlnd and use
  39. /tmp/mysql.sock as default on *nix and NULL for Windows (default
  40. named pipe name is set in mysqlnd).
  41. */
  42. #ifndef PDO_MYSQL_UNIX_ADDR
  43. # ifdef PHP_MYSQL_UNIX_SOCK_ADDR
  44. # define PDO_MYSQL_UNIX_ADDR PHP_MYSQL_UNIX_SOCK_ADDR
  45. # else
  46. # if !PHP_WIN32
  47. # define PDO_MYSQL_UNIX_ADDR "/tmp/mysql.sock"
  48. # else
  49. # define PDO_MYSQL_UNIX_ADDR NULL
  50. # endif
  51. # endif
  52. #endif
  53. #ifdef PDO_USE_MYSQLND
  54. #include "ext/mysqlnd/mysqlnd_reverse_api.h"
  55. static MYSQLND * pdo_mysql_convert_zv_to_mysqlnd(zval * zv TSRMLS_DC)
  56. {
  57. if (Z_TYPE_P(zv) == IS_OBJECT && instanceof_function(Z_OBJCE_P(zv), php_pdo_get_dbh_ce() TSRMLS_CC)) {
  58. pdo_dbh_t * dbh = zend_object_store_get_object(zv TSRMLS_CC);
  59. if (!dbh) {
  60. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to retrieve handle from object store");
  61. return NULL;
  62. }
  63. if (dbh->driver != &pdo_mysql_driver) {
  64. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Provided PDO instance is not using MySQL but %s", dbh->driver->driver_name);
  65. return NULL;
  66. }
  67. return ((pdo_mysql_db_handle *)dbh->driver_data)->server;
  68. }
  69. return NULL;
  70. }
  71. static MYSQLND_REVERSE_API pdo_mysql_reverse_api = {
  72. &pdo_mysql_module_entry,
  73. pdo_mysql_convert_zv_to_mysqlnd
  74. };
  75. #endif
  76. /* {{{ PHP_INI_BEGIN
  77. */
  78. PHP_INI_BEGIN()
  79. #ifndef PHP_WIN32
  80. STD_PHP_INI_ENTRY("pdo_mysql.default_socket", PDO_MYSQL_UNIX_ADDR, PHP_INI_SYSTEM, OnUpdateStringUnempty, default_socket, zend_pdo_mysql_globals, pdo_mysql_globals)
  81. #endif
  82. #if PDO_DBG_ENABLED
  83. STD_PHP_INI_ENTRY("pdo_mysql.debug", NULL, PHP_INI_SYSTEM, OnUpdateString, debug, zend_pdo_mysql_globals, pdo_mysql_globals)
  84. #endif
  85. PHP_INI_END()
  86. /* }}} */
  87. /* true global environment */
  88. /* {{{ PHP_MINIT_FUNCTION
  89. */
  90. static PHP_MINIT_FUNCTION(pdo_mysql)
  91. {
  92. REGISTER_INI_ENTRIES();
  93. REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_USE_BUFFERED_QUERY", (long)PDO_MYSQL_ATTR_USE_BUFFERED_QUERY);
  94. REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_LOCAL_INFILE", (long)PDO_MYSQL_ATTR_LOCAL_INFILE);
  95. REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_INIT_COMMAND", (long)PDO_MYSQL_ATTR_INIT_COMMAND);
  96. #ifndef PDO_USE_MYSQLND
  97. REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_MAX_BUFFER_SIZE", (long)PDO_MYSQL_ATTR_MAX_BUFFER_SIZE);
  98. REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_READ_DEFAULT_FILE", (long)PDO_MYSQL_ATTR_READ_DEFAULT_FILE);
  99. REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_READ_DEFAULT_GROUP", (long)PDO_MYSQL_ATTR_READ_DEFAULT_GROUP);
  100. #endif
  101. REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_COMPRESS", (long)PDO_MYSQL_ATTR_COMPRESS);
  102. REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_DIRECT_QUERY", (long)PDO_MYSQL_ATTR_DIRECT_QUERY);
  103. REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_FOUND_ROWS", (long)PDO_MYSQL_ATTR_FOUND_ROWS);
  104. REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_IGNORE_SPACE", (long)PDO_MYSQL_ATTR_IGNORE_SPACE);
  105. REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_SSL_KEY", (long)PDO_MYSQL_ATTR_SSL_KEY);
  106. REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_SSL_CERT", (long)PDO_MYSQL_ATTR_SSL_CERT);
  107. REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_SSL_CA", (long)PDO_MYSQL_ATTR_SSL_CA);
  108. REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_SSL_CAPATH", (long)PDO_MYSQL_ATTR_SSL_CAPATH);
  109. REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_SSL_CIPHER", (long)PDO_MYSQL_ATTR_SSL_CIPHER);
  110. #if MYSQL_VERSION_ID > 50605 || defined(PDO_USE_MYSQLND)
  111. REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_SERVER_PUBLIC_KEY", (long)PDO_MYSQL_ATTR_SERVER_PUBLIC_KEY);
  112. #endif
  113. REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_MULTI_STATEMENTS", (long)PDO_MYSQL_ATTR_MULTI_STATEMENTS);
  114. #ifdef PDO_USE_MYSQLND
  115. mysqlnd_reverse_api_register_api(&pdo_mysql_reverse_api TSRMLS_CC);
  116. #endif
  117. return php_pdo_register_driver(&pdo_mysql_driver);
  118. }
  119. /* }}} */
  120. /* {{{ PHP_MSHUTDOWN_FUNCTION
  121. */
  122. static PHP_MSHUTDOWN_FUNCTION(pdo_mysql)
  123. {
  124. php_pdo_unregister_driver(&pdo_mysql_driver);
  125. #if PDO_USE_MYSQLND
  126. UNREGISTER_INI_ENTRIES();
  127. #endif
  128. return SUCCESS;
  129. }
  130. /* }}} */
  131. /* {{{ PHP_MINFO_FUNCTION
  132. */
  133. static PHP_MINFO_FUNCTION(pdo_mysql)
  134. {
  135. php_info_print_table_start();
  136. php_info_print_table_header(2, "PDO Driver for MySQL", "enabled");
  137. php_info_print_table_row(2, "Client API version", mysql_get_client_info());
  138. php_info_print_table_end();
  139. #ifndef PHP_WIN32
  140. DISPLAY_INI_ENTRIES();
  141. #endif
  142. }
  143. /* }}} */
  144. #if PDO_USE_MYSQLND && PDO_DBG_ENABLED
  145. /* {{{ PHP_RINIT_FUNCTION
  146. */
  147. static PHP_RINIT_FUNCTION(pdo_mysql)
  148. {
  149. if (PDO_MYSQL_G(debug)) {
  150. MYSQLND_DEBUG *dbg = mysqlnd_debug_init(mysqlnd_debug_std_no_trace_funcs TSRMLS_CC);
  151. if (!dbg) {
  152. return FAILURE;
  153. }
  154. dbg->m->set_mode(dbg, PDO_MYSQL_G(debug));
  155. PDO_MYSQL_G(dbg) = dbg;
  156. }
  157. return SUCCESS;
  158. }
  159. /* }}} */
  160. /* {{{ PHP_RSHUTDOWN_FUNCTION
  161. */
  162. static PHP_RSHUTDOWN_FUNCTION(pdo_mysql)
  163. {
  164. MYSQLND_DEBUG *dbg = PDO_MYSQL_G(dbg);
  165. PDO_DBG_ENTER("RSHUTDOWN");
  166. if (dbg) {
  167. dbg->m->close(dbg);
  168. dbg->m->free_handle(dbg);
  169. PDO_MYSQL_G(dbg) = NULL;
  170. }
  171. return SUCCESS;
  172. }
  173. /* }}} */
  174. #endif
  175. /* {{{ PHP_GINIT_FUNCTION
  176. */
  177. static PHP_GINIT_FUNCTION(pdo_mysql)
  178. {
  179. #ifndef PHP_WIN32
  180. pdo_mysql_globals->default_socket = NULL;
  181. #endif
  182. #if PDO_DBG_ENABLED
  183. pdo_mysql_globals->debug = NULL; /* The actual string */
  184. pdo_mysql_globals->dbg = NULL; /* The DBG object*/
  185. #endif
  186. }
  187. /* }}} */
  188. /* {{{ pdo_mysql_functions[] */
  189. const zend_function_entry pdo_mysql_functions[] = {
  190. PHP_FE_END
  191. };
  192. /* }}} */
  193. /* {{{ pdo_mysql_deps[] */
  194. #if ZEND_MODULE_API_NO >= 20050922
  195. static const zend_module_dep pdo_mysql_deps[] = {
  196. ZEND_MOD_REQUIRED("pdo")
  197. #ifdef PDO_USE_MYSQLND
  198. ZEND_MOD_REQUIRED("mysqlnd")
  199. #endif
  200. ZEND_MOD_END
  201. };
  202. #endif
  203. /* }}} */
  204. /* {{{ pdo_mysql_module_entry */
  205. zend_module_entry pdo_mysql_module_entry = {
  206. STANDARD_MODULE_HEADER_EX, NULL,
  207. pdo_mysql_deps,
  208. "pdo_mysql",
  209. pdo_mysql_functions,
  210. PHP_MINIT(pdo_mysql),
  211. PHP_MSHUTDOWN(pdo_mysql),
  212. #if PDO_USE_MYSQLND && PDO_DBG_ENABLED
  213. PHP_RINIT(pdo_mysql),
  214. PHP_RSHUTDOWN(pdo_mysql),
  215. #else
  216. NULL,
  217. NULL,
  218. #endif
  219. PHP_MINFO(pdo_mysql),
  220. "1.0.2",
  221. PHP_MODULE_GLOBALS(pdo_mysql),
  222. PHP_GINIT(pdo_mysql),
  223. NULL,
  224. NULL,
  225. STANDARD_MODULE_PROPERTIES_EX
  226. };
  227. /* }}} */
  228. /*
  229. * Local variables:
  230. * tab-width: 4
  231. * c-basic-offset: 4
  232. * End:
  233. * vim600: noet sw=4 ts=4 fdm=marker
  234. * vim<600: noet sw=4 ts=4
  235. */