1/*
2  +----------------------------------------------------------------------+
3  | PHP Version 7                                                        |
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  |         Frank M. Kromann <frank@kromann.info>                        |
17  +----------------------------------------------------------------------+
18*/
19
20/* $Id$ */
21
22#ifdef HAVE_CONFIG_H
23# include "config.h"
24#endif
25
26#include "php.h"
27#include "php_ini.h"
28#include "ext/standard/info.h"
29#include "pdo/php_pdo.h"
30#include "pdo/php_pdo_driver.h"
31#include "php_pdo_dblib.h"
32#include "php_pdo_dblib_int.h"
33#include "zend_exceptions.h"
34
35ZEND_DECLARE_MODULE_GLOBALS(dblib)
36static PHP_GINIT_FUNCTION(dblib);
37
38const zend_function_entry pdo_dblib_functions[] = {
39	PHP_FE_END
40};
41
42static const zend_module_dep pdo_dblib_deps[] = {
43	ZEND_MOD_REQUIRED("pdo")
44	ZEND_MOD_END
45};
46
47#if PDO_DBLIB_IS_MSSQL
48zend_module_entry pdo_mssql_module_entry = {
49#else
50zend_module_entry pdo_dblib_module_entry = {
51#endif
52	STANDARD_MODULE_HEADER_EX, NULL,
53	pdo_dblib_deps,
54#if PDO_DBLIB_IS_MSSQL
55	"pdo_mssql",
56#elif defined(PHP_WIN32)
57	"pdo_sybase",
58#else
59	"pdo_dblib",
60#endif
61	pdo_dblib_functions,
62	PHP_MINIT(pdo_dblib),
63	PHP_MSHUTDOWN(pdo_dblib),
64	NULL,
65	PHP_RSHUTDOWN(pdo_dblib),
66	PHP_MINFO(pdo_dblib),
67	PHP_PDO_DBLIB_VERSION,
68	PHP_MODULE_GLOBALS(dblib),
69	PHP_GINIT(dblib),
70	NULL,
71	NULL,
72	STANDARD_MODULE_PROPERTIES_EX
73};
74
75#if defined(COMPILE_DL_PDO_DBLIB) || defined(COMPILE_DL_PDO_MSSQL)
76#if PDO_DBLIB_IS_MSSQL
77ZEND_GET_MODULE(pdo_mssql)
78#else
79ZEND_GET_MODULE(pdo_dblib)
80#endif
81#endif
82
83int pdo_dblib_error_handler(DBPROCESS *dbproc, int severity, int dberr,
84	int oserr, char *dberrstr, char *oserrstr)
85{
86	pdo_dblib_err *einfo;
87	char *state = "HY000";
88
89	if(dbproc) {
90		einfo = (pdo_dblib_err*)dbgetuserdata(dbproc);
91		if (!einfo) einfo = &DBLIB_G(err);
92	} else {
93		einfo = &DBLIB_G(err);
94	}
95
96	einfo->severity = severity;
97	einfo->oserr = oserr;
98	einfo->dberr = dberr;
99
100	if (einfo->oserrstr) {
101		efree(einfo->oserrstr);
102	}
103	if (einfo->dberrstr) {
104		efree(einfo->dberrstr);
105	}
106	if (oserrstr) {
107		einfo->oserrstr = estrdup(oserrstr);
108	} else {
109		einfo->oserrstr = NULL;
110	}
111	if (dberrstr) {
112		einfo->dberrstr = estrdup(dberrstr);
113	} else {
114		einfo->dberrstr = NULL;
115	}
116
117	switch (dberr) {
118		case SYBESEOF:
119		case SYBEFCON:	state = "01002"; break;
120		case SYBEMEM:	state = "HY001"; break;
121		case SYBEPWD:	state = "28000"; break;
122	}
123	strcpy(einfo->sqlstate, state);
124
125	return INT_CANCEL;
126}
127
128int pdo_dblib_msg_handler(DBPROCESS *dbproc, DBINT msgno, int msgstate,
129	int severity, char *msgtext, char *srvname, char *procname, DBUSMALLINT line)
130{
131	pdo_dblib_err *einfo;
132
133	if (severity) {
134		einfo = (pdo_dblib_err*)dbgetuserdata(dbproc);
135		if (!einfo) {
136			einfo = &DBLIB_G(err);
137		}
138
139		if (einfo->lastmsg) {
140			efree(einfo->lastmsg);
141		}
142
143		einfo->lastmsg = estrdup(msgtext);
144	}
145
146	return 0;
147}
148
149static PHP_GINIT_FUNCTION(dblib)
150{
151	memset(dblib_globals, 0, sizeof(*dblib_globals));
152	dblib_globals->err.sqlstate = dblib_globals->sqlstate;
153}
154
155PHP_RSHUTDOWN_FUNCTION(pdo_dblib)
156{
157	if (DBLIB_G(err).oserrstr) {
158		efree(DBLIB_G(err).oserrstr);
159		DBLIB_G(err).oserrstr = NULL;
160	}
161	if (DBLIB_G(err).dberrstr) {
162		efree(DBLIB_G(err).dberrstr);
163		DBLIB_G(err).dberrstr = NULL;
164	}
165	if (DBLIB_G(err).lastmsg) {
166		efree(DBLIB_G(err).lastmsg);
167		DBLIB_G(err).lastmsg = NULL;
168	}
169	return SUCCESS;
170}
171
172PHP_MINIT_FUNCTION(pdo_dblib)
173{
174	REGISTER_PDO_CLASS_CONST_LONG("DBLIB_ATTR_CONNECTION_TIMEOUT", (long) PDO_DBLIB_ATTR_CONNECTION_TIMEOUT);
175	REGISTER_PDO_CLASS_CONST_LONG("DBLIB_ATTR_QUERY_TIMEOUT", (long) PDO_DBLIB_ATTR_QUERY_TIMEOUT);
176
177	if (FAIL == dbinit()) {
178		return FAILURE;
179	}
180
181	if (FAILURE == php_pdo_register_driver(&pdo_dblib_driver)) {
182		return FAILURE;
183	}
184
185#if !PHP_DBLIB_IS_MSSQL
186	dberrhandle((EHANDLEFUNC) pdo_dblib_error_handler);
187	dbmsghandle((MHANDLEFUNC) pdo_dblib_msg_handler);
188#endif
189
190	return SUCCESS;
191}
192
193PHP_MSHUTDOWN_FUNCTION(pdo_dblib)
194{
195	php_pdo_unregister_driver(&pdo_dblib_driver);
196	dbexit();
197	return SUCCESS;
198}
199
200PHP_MINFO_FUNCTION(pdo_dblib)
201{
202	php_info_print_table_start();
203	php_info_print_table_header(2, "PDO Driver for "
204#if PDO_DBLIB_IS_MSSQL
205		"MSSQL"
206#elif defined(PHP_WIN32)
207		"FreeTDS/Sybase/MSSQL"
208#else
209		"FreeTDS/Sybase"
210#endif
211		" DB-lib", "enabled");
212	php_info_print_table_row(2, "Flavour", PDO_DBLIB_FLAVOUR);
213	php_info_print_table_end();
214}
215
216