pdo_mysql.c 7.9 KB

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