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
35/* Cache of the server supported datatypes, initialized in handle_factory */
36zval* pdo_dblib_datatypes;
37
38static int dblib_fetch_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info)
39{
40	pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
41	pdo_dblib_err *einfo = &H->err;
42	pdo_dblib_stmt *S = NULL;
43	char *message;
44	char *msg;
45
46	if (stmt) {
47		S = (pdo_dblib_stmt*)stmt->driver_data;
48		einfo = &S->err;
49	}
50
51	if (einfo->dberr == SYBESMSG && einfo->lastmsg) {
52		msg = einfo->lastmsg;
53	} else if (einfo->dberr == SYBESMSG && DBLIB_G(err).lastmsg) {
54		msg = DBLIB_G(err).lastmsg;
55		DBLIB_G(err).lastmsg = NULL;
56	} else {
57		msg = einfo->dberrstr;
58	}
59
60	spprintf(&message, 0, "%s [%d] (severity %d) [%s]",
61		msg, einfo->dberr, einfo->severity, stmt ? stmt->active_query_string : "");
62
63	add_next_index_long(info, einfo->dberr);
64	add_next_index_string(info, message);
65	efree(message);
66	add_next_index_long(info, einfo->oserr);
67	add_next_index_long(info, einfo->severity);
68	if (einfo->oserrstr) {
69		add_next_index_string(info, einfo->oserrstr);
70	}
71
72	return 1;
73}
74
75
76static int dblib_handle_closer(pdo_dbh_t *dbh)
77{
78	pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
79
80	if (H) {
81		if (H->link) {
82			dbclose(H->link);
83			H->link = NULL;
84		}
85		if (H->login) {
86			dbfreelogin(H->login);
87			H->login = NULL;
88		}
89		pefree(H, dbh->is_persistent);
90		dbh->driver_data = NULL;
91	}
92	return 0;
93}
94
95static int dblib_handle_preparer(pdo_dbh_t *dbh, const char *sql, size_t sql_len, pdo_stmt_t *stmt, zval *driver_options)
96{
97	pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
98	pdo_dblib_stmt *S = ecalloc(1, sizeof(*S));
99
100	S->H = H;
101	stmt->driver_data = S;
102	stmt->methods = &dblib_stmt_methods;
103	stmt->supports_placeholders = PDO_PLACEHOLDER_NONE;
104	S->computed_column_name_count = 0;
105	S->err.sqlstate = stmt->error_code;
106
107	return 1;
108}
109
110static zend_long dblib_handle_doer(pdo_dbh_t *dbh, const char *sql, size_t sql_len)
111{
112	pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
113	RETCODE ret, resret;
114
115	dbsetuserdata(H->link, (BYTE*)&H->err);
116
117	if (FAIL == dbcmd(H->link, sql)) {
118		return -1;
119	}
120
121	if (FAIL == dbsqlexec(H->link)) {
122		return -1;
123	}
124
125	resret = dbresults(H->link);
126
127	if (resret == FAIL) {
128		return -1;
129	}
130
131	ret = dbnextrow(H->link);
132	if (ret == FAIL) {
133		return -1;
134	}
135
136	if (dbnumcols(H->link) <= 0) {
137		return DBCOUNT(H->link);
138	}
139
140	/* throw away any rows it might have returned */
141	dbcanquery(H->link);
142
143	return DBCOUNT(H->link);
144}
145
146static int dblib_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, size_t unquotedlen, char **quoted, size_t *quotedlen, enum pdo_param_type paramtype)
147{
148
149	int useBinaryEncoding = 0;
150	const char * hex = "0123456789abcdef";
151	int i;
152	char * q;
153	*quotedlen = 0;
154
155	/*
156	 * Detect quoted length and if we should use binary encoding
157	 */
158	for(i=0;i<unquotedlen;i++) {
159		if( 32 > unquoted[i] || 127 < unquoted[i] ) {
160			useBinaryEncoding = 1;
161			break;
162		}
163		if(unquoted[i] == '\'') ++*quotedlen;
164		++*quotedlen;
165	}
166
167	if(useBinaryEncoding) {
168		/*
169		 * Binary safe quoting
170		 * Will implicitly convert for all data types except Text, DateTime & SmallDateTime
171		 *
172		 */
173		*quotedlen = (unquotedlen * 2) + 2; /* 2 chars per byte +2 for "0x" prefix */
174		q = *quoted = emalloc(*quotedlen+1); /* Add byte for terminal null */
175
176		*q++ = '0';
177		*q++ = 'x';
178		for (i=0;i<unquotedlen;i++) {
179			*q++ = hex[ (*unquoted>>4)&0xF];
180			*q++ = hex[ (*unquoted++)&0xF];
181		}
182	} else {
183		/* Alpha/Numeric Quoting */
184		*quotedlen += 2; /* +2 for opening, closing quotes */
185		q  = *quoted = emalloc(*quotedlen+1); /* Add byte for terminal null */
186		*q++ = '\'';
187
188		for (i=0;i<unquotedlen;i++) {
189			if (unquoted[i] == '\'') {
190				*q++ = '\'';
191				*q++ = '\'';
192			} else {
193				*q++ = unquoted[i];
194			}
195		}
196		*q++ = '\'';
197	}
198
199	*q = 0;
200
201	return 1;
202}
203
204static int pdo_dblib_transaction_cmd(const char *cmd, pdo_dbh_t *dbh)
205{
206	pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
207
208	if (FAIL == dbcmd(H->link, cmd)) {
209		return 0;
210	}
211
212	if (FAIL == dbsqlexec(H->link)) {
213		return 0;
214	}
215
216	return 1;
217}
218
219static int dblib_handle_begin(pdo_dbh_t *dbh)
220{
221	return pdo_dblib_transaction_cmd("BEGIN TRANSACTION", dbh);
222}
223
224static int dblib_handle_commit(pdo_dbh_t *dbh)
225{
226	return pdo_dblib_transaction_cmd("COMMIT TRANSACTION", dbh);
227}
228
229static int dblib_handle_rollback(pdo_dbh_t *dbh)
230{
231	return pdo_dblib_transaction_cmd("ROLLBACK TRANSACTION", dbh);
232}
233
234char *dblib_handle_last_id(pdo_dbh_t *dbh, const char *name, size_t *len)
235{
236	pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
237
238	RETCODE ret;
239	char *id = NULL;
240
241	/*
242	 * Would use scope_identity() but it's not implemented on Sybase
243	 */
244
245	if (FAIL == dbcmd(H->link, "SELECT @@IDENTITY")) {
246		return NULL;
247	}
248
249	if (FAIL == dbsqlexec(H->link)) {
250		return NULL;
251	}
252
253	ret = dbresults(H->link);
254	if (ret == FAIL || ret == NO_MORE_RESULTS) {
255		dbcancel(H->link);
256		return NULL;
257	}
258
259	ret = dbnextrow(H->link);
260
261	if (ret == FAIL || ret == NO_MORE_ROWS) {
262		dbcancel(H->link);
263		return NULL;
264	}
265
266	if (dbdatlen(H->link, 1) == 0) {
267		dbcancel(H->link);
268		return NULL;
269	}
270
271	id = emalloc(32);
272	*len = dbconvert(NULL, (dbcoltype(H->link, 1)) , (dbdata(H->link, 1)) , (dbdatlen(H->link, 1)), SQLCHAR, (BYTE *)id, (DBINT)-1);
273
274	dbcancel(H->link);
275	return id;
276}
277
278static int dblib_set_attr(pdo_dbh_t *dbh, zend_long attr, zval *val)
279{
280	switch(attr) {
281		case PDO_ATTR_TIMEOUT:
282			return 0;
283		default:
284			return 1;
285	}
286
287}
288
289static int dblib_get_attribute(pdo_dbh_t *dbh, zend_long attr, zval *return_value)
290{
291	/* dblib_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; */
292	return 0;
293}
294
295static struct pdo_dbh_methods dblib_methods = {
296	dblib_handle_closer,
297	dblib_handle_preparer,
298	dblib_handle_doer,
299	dblib_handle_quoter,
300	dblib_handle_begin, /* begin */
301	dblib_handle_commit, /* commit */
302	dblib_handle_rollback, /* rollback */
303	dblib_set_attr, /*set attr */
304	dblib_handle_last_id, /* last insert id */
305	dblib_fetch_error, /* fetch error */
306	dblib_get_attribute, /* get attr */
307	NULL, /* check liveness */
308	NULL, /* get driver methods */
309	NULL, /* request shutdown */
310	NULL  /* in transaction */
311};
312
313static int pdo_dblib_handle_factory(pdo_dbh_t *dbh, zval *driver_options)
314{
315	pdo_dblib_db_handle *H;
316	int i, nvars, nvers, ret = 0;
317
318	const pdo_dblib_keyval tdsver[] = {
319		 {"4.2",DBVERSION_42}
320		,{"4.6",DBVERSION_46}
321		,{"5.0",DBVERSION_70} /* FIXME: This does not work with Sybase, but environ will */
322		,{"6.0",DBVERSION_70}
323		,{"7.0",DBVERSION_70}
324#ifdef DBVERSION_71
325		,{"7.1",DBVERSION_71}
326#endif
327#ifdef DBVERSION_72
328		,{"7.2",DBVERSION_72}
329		,{"8.0",DBVERSION_72}
330#endif
331		,{"10.0",DBVERSION_100}
332		,{"auto",0} /* Only works with FreeTDS. Other drivers will bork */
333
334	};
335
336	struct pdo_data_src_parser vars[] = {
337		{ "charset",	NULL,	0 }
338		,{ "appname",	"PHP " PDO_DBLIB_FLAVOUR,	0 }
339		,{ "host",		"127.0.0.1", 0 }
340		,{ "dbname",	NULL,	0 }
341		,{ "secure",	NULL,	0 } /* DBSETLSECURE */
342		,{ "version",	NULL,	0 } /* DBSETLVERSION */
343	};
344
345	nvars = sizeof(vars)/sizeof(vars[0]);
346	nvers = sizeof(tdsver)/sizeof(tdsver[0]);
347
348	php_pdo_parse_data_source(dbh->data_source, dbh->data_source_len, vars, nvars);
349
350	if (driver_options) {
351		int connect_timeout = pdo_attr_lval(driver_options, PDO_DBLIB_ATTR_CONNECTION_TIMEOUT, -1);
352		int query_timeout = pdo_attr_lval(driver_options, PDO_DBLIB_ATTR_QUERY_TIMEOUT, -1);
353		int timeout = pdo_attr_lval(driver_options, PDO_ATTR_TIMEOUT, 30);
354
355		if (connect_timeout == -1) {
356			connect_timeout = timeout;
357		}
358		if (query_timeout == -1) {
359			query_timeout = timeout;
360		}
361
362		dbsetlogintime(connect_timeout); /* Connection/Login Timeout */
363		dbsettime(query_timeout); /* Statement Timeout */
364	}
365
366	H = pecalloc(1, sizeof(*H), dbh->is_persistent);
367	H->login = dblogin();
368	H->err.sqlstate = dbh->error_code;
369
370	if (!H->login) {
371		goto cleanup;
372	}
373
374	DBERRHANDLE(H->login, (EHANDLEFUNC) pdo_dblib_error_handler);
375	DBMSGHANDLE(H->login, (MHANDLEFUNC) pdo_dblib_msg_handler);
376
377	if(vars[5].optval) {
378		for(i=0;i<nvers;i++) {
379			if(strcmp(vars[5].optval,tdsver[i].key) == 0) {
380				if(FAIL==dbsetlversion(H->login, tdsver[i].value)) {
381					pdo_raise_impl_error(dbh, NULL, "HY000", "PDO_DBLIB: Failed to set version specified in connection string.");
382					goto cleanup;
383				}
384				break;
385			}
386		}
387
388		if (i==nvers) {
389			printf("Invalid version '%s'\n", vars[5].optval);
390			pdo_raise_impl_error(dbh, NULL, "HY000", "PDO_DBLIB: Invalid version specified in connection string.");
391			goto cleanup; /* unknown version specified */
392		}
393	}
394
395	if (dbh->username) {
396		if(FAIL == DBSETLUSER(H->login, dbh->username)) {
397			goto cleanup;
398		}
399	}
400
401	if (dbh->password) {
402		if(FAIL == DBSETLPWD(H->login, dbh->password)) {
403			goto cleanup;
404		}
405	}
406
407#if !PHP_DBLIB_IS_MSSQL
408	if (vars[0].optval) {
409		DBSETLCHARSET(H->login, vars[0].optval);
410	}
411#endif
412
413	DBSETLAPP(H->login, vars[1].optval);
414
415/* DBSETLDBNAME is only available in FreeTDS 0.92 or above */
416#ifdef DBSETLDBNAME
417	if (vars[3].optval) {
418		if(FAIL == DBSETLDBNAME(H->login, vars[3].optval)) goto cleanup;
419	}
420#endif
421
422	H->link = dbopen(H->login, vars[2].optval);
423
424	if (!H->link) {
425		goto cleanup;
426	}
427
428/*
429 * FreeTDS < 0.92 does not support the DBSETLDBNAME option
430 * Send use database here after login (Will not work with SQL Azure)
431 */
432#ifndef DBSETLDBNAME
433	if (vars[3].optval) {
434		if(FAIL == dbuse(H->link, vars[3].optval)) goto cleanup;
435	}
436#endif
437
438#if PHP_DBLIB_IS_MSSQL
439	/* dblib do not return more than this length from text/image */
440	DBSETOPT(H->link, DBTEXTLIMIT, "2147483647");
441#endif
442
443	/* limit text/image from network */
444	DBSETOPT(H->link, DBTEXTSIZE, "2147483647");
445
446	/* allow double quoted indentifiers */
447	DBSETOPT(H->link, DBQUOTEDIDENT, "1");
448
449	ret = 1;
450	dbh->max_escaped_char_length = 2;
451	dbh->alloc_own_columns = 1;
452
453cleanup:
454	for (i = 0; i < nvars; i++) {
455		if (vars[i].freeme) {
456			efree(vars[i].optval);
457		}
458	}
459
460	dbh->methods = &dblib_methods;
461	dbh->driver_data = H;
462
463	if (!ret) {
464		zend_throw_exception_ex(php_pdo_get_exception(), DBLIB_G(err).dberr,
465			"SQLSTATE[%s] %s (severity %d)",
466			DBLIB_G(err).sqlstate,
467			DBLIB_G(err).dberrstr,
468			DBLIB_G(err).severity);
469	}
470
471	return ret;
472}
473
474pdo_driver_t pdo_dblib_driver = {
475#if PDO_DBLIB_IS_MSSQL
476	PDO_DRIVER_HEADER(mssql),
477#elif defined(PHP_WIN32)
478#define PDO_DBLIB_IS_SYBASE
479	PDO_DRIVER_HEADER(sybase),
480#else
481	PDO_DRIVER_HEADER(dblib),
482#endif
483	pdo_dblib_handle_factory
484};
485
486