1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 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    | Authors: Stig Sæther Bakken <ssb@php.net>                            |
16    |          Andreas Karajannis <Andreas.Karajannis@gmd.de>              |
17    |          Frank M. Kromann <frank@kromann.info>  Support for DB/2 CLI |
18    |          Kevin N. Shallow <kshallow@tampabay.rr.com>                 |
19    |          Daniel R. Kalowsky <kalowsky@php.net>                       |
20    +----------------------------------------------------------------------+
21 */
22 
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 
27 #include "php.h"
28 #include "php_globals.h"
29 
30 #include "ext/standard/info.h"
31 #include "ext/standard/php_string.h"
32 #include "ext/standard/php_standard.h"
33 
34 #include "php_odbc.h"
35 #include "php_odbc_includes.h"
36 #include "php_globals.h"
37 
38 #if HAVE_UODBC
39 
40 #include <fcntl.h>
41 #include "ext/standard/head.h"
42 #include "php_ini.h"
43 
44 #ifdef PHP_WIN32
45 #include <winsock2.h>
46 
47 #define ODBC_TYPE "Win32"
48 #define PHP_ODBC_TYPE ODBC_TYPE
49 
50 #endif
51 
52 /*
53  * not defined elsewhere
54  */
55 
56 #ifndef TRUE
57 #define TRUE 1
58 #define FALSE 0
59 #endif
60 
61 void odbc_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent);
62 
63 static int le_result, le_conn, le_pconn;
64 
65 #define SAFE_SQL_NTS(n) ((SQLSMALLINT) ((n)?(SQL_NTS):0))
66 
67 /* {{{ arginfo */
68 ZEND_BEGIN_ARG_INFO(arginfo_odbc_close_all, 0)
69 ZEND_END_ARG_INFO()
70 
71 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_binmode, 0, 0, 2)
72 	ZEND_ARG_INFO(0, result_id)
73 	ZEND_ARG_INFO(0, mode)
74 ZEND_END_ARG_INFO()
75 
76 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_longreadlen, 0, 0, 2)
77 	ZEND_ARG_INFO(0, result_id)
78 	ZEND_ARG_INFO(0, length)
79 ZEND_END_ARG_INFO()
80 
81 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_prepare, 0, 0, 2)
82 	ZEND_ARG_INFO(0, connection_id)
83 	ZEND_ARG_INFO(0, query)
84 ZEND_END_ARG_INFO()
85 
86 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_execute, 0, 0, 1)
87 	ZEND_ARG_INFO(0, result_id)
88 	ZEND_ARG_INFO(0, parameters_array)
89 ZEND_END_ARG_INFO()
90 
91 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_cursor, 0, 0, 1)
92 	ZEND_ARG_INFO(0, result_id)
93 ZEND_END_ARG_INFO()
94 
95 #ifdef HAVE_SQLDATASOURCES
96 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_data_source, 0, 0, 2)
97 	ZEND_ARG_INFO(0, connection_id)
98 	ZEND_ARG_INFO(0, fetch_type)
99 ZEND_END_ARG_INFO()
100 #endif
101 
102 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_exec, 0, 0, 2)
103 	ZEND_ARG_INFO(0, connection_id)
104 	ZEND_ARG_INFO(0, query)
105 	ZEND_ARG_INFO(0, flags)
106 ZEND_END_ARG_INFO()
107 
108 #ifdef PHP_ODBC_HAVE_FETCH_HASH
109 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_fetch_object, 0, 0, 1)
110 	ZEND_ARG_INFO(0, result)
111 	ZEND_ARG_INFO(0, rownumber)
112 ZEND_END_ARG_INFO()
113 
114 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_fetch_array, 0, 0, 1)
115 	ZEND_ARG_INFO(0, result)
116 	ZEND_ARG_INFO(0, rownumber)
117 ZEND_END_ARG_INFO()
118 #endif
119 
120 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_fetch_into, 0, 0, 2)
121 	ZEND_ARG_INFO(0, result_id)
122 	ZEND_ARG_INFO(1, result_array)
123 	ZEND_ARG_INFO(0, rownumber)
124 ZEND_END_ARG_INFO()
125 
126 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_fetch_row, 0, 0, 1)
127 	ZEND_ARG_INFO(0, result_id)
128 	ZEND_ARG_INFO(0, row_number)
129 ZEND_END_ARG_INFO()
130 
131 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_result, 0, 0, 2)
132 	ZEND_ARG_INFO(0, result_id)
133 	ZEND_ARG_INFO(0, field)
134 ZEND_END_ARG_INFO()
135 
136 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_result_all, 0, 0, 1)
137 	ZEND_ARG_INFO(0, result_id)
138 	ZEND_ARG_INFO(0, format)
139 ZEND_END_ARG_INFO()
140 
141 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_free_result, 0, 0, 1)
142 	ZEND_ARG_INFO(0, result_id)
143 ZEND_END_ARG_INFO()
144 
145 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_connect, 0, 0, 3)
146 	ZEND_ARG_INFO(0, dsn)
147 	ZEND_ARG_INFO(0, user)
148 	ZEND_ARG_INFO(0, password)
149 	ZEND_ARG_INFO(0, cursor_option)
150 ZEND_END_ARG_INFO()
151 
152 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_pconnect, 0, 0, 3)
153 	ZEND_ARG_INFO(0, dsn)
154 	ZEND_ARG_INFO(0, user)
155 	ZEND_ARG_INFO(0, password)
156 	ZEND_ARG_INFO(0, cursor_option)
157 ZEND_END_ARG_INFO()
158 
159 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_close, 0, 0, 1)
160 	ZEND_ARG_INFO(0, connection_id)
161 ZEND_END_ARG_INFO()
162 
163 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_num_rows, 0, 0, 1)
164 	ZEND_ARG_INFO(0, result_id)
165 ZEND_END_ARG_INFO()
166 
167 #if !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30)
168 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_next_result, 0, 0, 1)
169 	ZEND_ARG_INFO(0, result_id)
170 ZEND_END_ARG_INFO()
171 #endif
172 
173 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_num_fields, 0, 0, 1)
174 	ZEND_ARG_INFO(0, result_id)
175 ZEND_END_ARG_INFO()
176 
177 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_field_name, 0, 0, 2)
178 	ZEND_ARG_INFO(0, result_id)
179 	ZEND_ARG_INFO(0, field_number)
180 ZEND_END_ARG_INFO()
181 
182 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_field_type, 0, 0, 2)
183 	ZEND_ARG_INFO(0, result_id)
184 	ZEND_ARG_INFO(0, field_number)
185 ZEND_END_ARG_INFO()
186 
187 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_field_len, 0, 0, 2)
188 	ZEND_ARG_INFO(0, result_id)
189 	ZEND_ARG_INFO(0, field_number)
190 ZEND_END_ARG_INFO()
191 
192 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_field_scale, 0, 0, 2)
193 	ZEND_ARG_INFO(0, result_id)
194 	ZEND_ARG_INFO(0, field_number)
195 ZEND_END_ARG_INFO()
196 
197 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_field_num, 0, 0, 2)
198 	ZEND_ARG_INFO(0, result_id)
199 	ZEND_ARG_INFO(0, field_name)
200 ZEND_END_ARG_INFO()
201 
202 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_autocommit, 0, 0, 1)
203 	ZEND_ARG_INFO(0, connection_id)
204 	ZEND_ARG_INFO(0, onoff)
205 ZEND_END_ARG_INFO()
206 
207 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_commit, 0, 0, 1)
208 	ZEND_ARG_INFO(0, connection_id)
209 ZEND_END_ARG_INFO()
210 
211 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_rollback, 0, 0, 1)
212 	ZEND_ARG_INFO(0, connection_id)
213 ZEND_END_ARG_INFO()
214 
215 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_error, 0, 0, 0)
216 	ZEND_ARG_INFO(0, connection_id)
217 ZEND_END_ARG_INFO()
218 
219 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_errormsg, 0, 0, 0)
220 	ZEND_ARG_INFO(0, connection_id)
221 ZEND_END_ARG_INFO()
222 
223 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_setoption, 0, 0, 4)
224 	ZEND_ARG_INFO(0, conn_id)
225 	ZEND_ARG_INFO(0, which)
226 	ZEND_ARG_INFO(0, option)
227 	ZEND_ARG_INFO(0, value)
228 ZEND_END_ARG_INFO()
229 
230 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_tables, 0, 0, 1)
231 	ZEND_ARG_INFO(0, connection_id)
232 	ZEND_ARG_INFO(0, qualifier)
233 	ZEND_ARG_INFO(0, owner)
234 	ZEND_ARG_INFO(0, name)
235 	ZEND_ARG_INFO(0, table_types)
236 ZEND_END_ARG_INFO()
237 
238 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_columns, 0, 0, 1)
239 	ZEND_ARG_INFO(0, connection_id)
240 	ZEND_ARG_INFO(0, qualifier)
241 	ZEND_ARG_INFO(0, owner)
242 	ZEND_ARG_INFO(0, table_name)
243 	ZEND_ARG_INFO(0, column_name)
244 ZEND_END_ARG_INFO()
245 
246 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_gettypeinfo, 0, 0, 1)
247 	ZEND_ARG_INFO(0, connection_id)
248 	ZEND_ARG_INFO(0, data_type)
249 ZEND_END_ARG_INFO()
250 
251 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_primarykeys, 0, 0, 4)
252 	ZEND_ARG_INFO(0, connection_id)
253 	ZEND_ARG_INFO(0, qualifier)
254 	ZEND_ARG_INFO(0, owner)
255 	ZEND_ARG_INFO(0, table)
256 ZEND_END_ARG_INFO()
257 
258 #if !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30) && !defined(HAVE_SOLID_35)
259 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_procedurecolumns, 0, 0, 1)
260 	ZEND_ARG_INFO(0, connection_id)
261 	ZEND_ARG_INFO(0, qualifier)
262 	ZEND_ARG_INFO(0, owner)
263 	ZEND_ARG_INFO(0, proc)
264 	ZEND_ARG_INFO(0, column)
265 ZEND_END_ARG_INFO()
266 
267 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_procedures, 0, 0, 1)
268 	ZEND_ARG_INFO(0, connection_id)
269 	ZEND_ARG_INFO(0, qualifier)
270 	ZEND_ARG_INFO(0, owner)
271 	ZEND_ARG_INFO(0, name)
272 ZEND_END_ARG_INFO()
273 
274 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_foreignkeys, 0, 0, 7)
275 	ZEND_ARG_INFO(0, connection_id)
276 	ZEND_ARG_INFO(0, pk_qualifier)
277 	ZEND_ARG_INFO(0, pk_owner)
278 	ZEND_ARG_INFO(0, pk_table)
279 	ZEND_ARG_INFO(0, fk_qualifier)
280 	ZEND_ARG_INFO(0, fk_owner)
281 	ZEND_ARG_INFO(0, fk_table)
282 ZEND_END_ARG_INFO()
283 #endif
284 
285 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_specialcolumns, 0, 0, 7)
286 	ZEND_ARG_INFO(0, connection_id)
287 	ZEND_ARG_INFO(0, type)
288 	ZEND_ARG_INFO(0, qualifier)
289 	ZEND_ARG_INFO(0, owner)
290 	ZEND_ARG_INFO(0, table)
291 	ZEND_ARG_INFO(0, scope)
292 	ZEND_ARG_INFO(0, nullable)
293 ZEND_END_ARG_INFO()
294 
295 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_statistics, 0, 0, 6)
296 	ZEND_ARG_INFO(0, connection_id)
297 	ZEND_ARG_INFO(0, qualifier)
298 	ZEND_ARG_INFO(0, owner)
299 	ZEND_ARG_INFO(0, name)
300 	ZEND_ARG_INFO(0, unique)
301 	ZEND_ARG_INFO(0, accuracy)
302 ZEND_END_ARG_INFO()
303 
304 #if !defined(HAVE_DBMAKER) && !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30) &&!defined(HAVE_SOLID_35)
305 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_tableprivileges, 0, 0, 4)
306 	ZEND_ARG_INFO(0, connection_id)
307 	ZEND_ARG_INFO(0, qualifier)
308 	ZEND_ARG_INFO(0, owner)
309 	ZEND_ARG_INFO(0, name)
310 ZEND_END_ARG_INFO()
311 
312 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_columnprivileges, 0, 0, 5)
313 	ZEND_ARG_INFO(0, connection_id)
314 	ZEND_ARG_INFO(0, catalog)
315 	ZEND_ARG_INFO(0, schema)
316 	ZEND_ARG_INFO(0, table)
317 	ZEND_ARG_INFO(0, column)
318 ZEND_END_ARG_INFO()
319 #endif
320 /* }}} */
321 
322 /* {{{ odbc_functions[]
323  */
324 static const zend_function_entry odbc_functions[] = {
325 	PHP_FE(odbc_autocommit, arginfo_odbc_autocommit)
326 	PHP_FE(odbc_binmode, arginfo_odbc_binmode)
327 	PHP_FE(odbc_close, arginfo_odbc_close)
328 	PHP_FE(odbc_close_all, arginfo_odbc_close_all)
329 	PHP_FE(odbc_columns, arginfo_odbc_columns)
330 	PHP_FE(odbc_commit, arginfo_odbc_commit)
331 	PHP_FE(odbc_connect, arginfo_odbc_connect)
332 	PHP_FE(odbc_cursor, arginfo_odbc_cursor)
333 #ifdef HAVE_SQLDATASOURCES
334 	PHP_FE(odbc_data_source, arginfo_odbc_data_source)
335 #endif
336 	PHP_FE(odbc_execute, arginfo_odbc_execute)
337 	PHP_FE(odbc_error, arginfo_odbc_error)
338 	PHP_FE(odbc_errormsg, arginfo_odbc_errormsg)
339 	PHP_FE(odbc_exec, arginfo_odbc_exec)
340 #ifdef PHP_ODBC_HAVE_FETCH_HASH
341 	PHP_FE(odbc_fetch_array, arginfo_odbc_fetch_array)
342 	PHP_FE(odbc_fetch_object, arginfo_odbc_fetch_object)
343 #endif
344 	PHP_FE(odbc_fetch_row, arginfo_odbc_fetch_row)
345 	PHP_FE(odbc_fetch_into, arginfo_odbc_fetch_into)
346 	PHP_FE(odbc_field_len, arginfo_odbc_field_len)
347 	PHP_FE(odbc_field_scale, arginfo_odbc_field_scale)
348 	PHP_FE(odbc_field_name, arginfo_odbc_field_name)
349 	PHP_FE(odbc_field_type, arginfo_odbc_field_type)
350 	PHP_FE(odbc_field_num, arginfo_odbc_field_num)
351 	PHP_FE(odbc_free_result, arginfo_odbc_free_result)
352 	PHP_FE(odbc_gettypeinfo, arginfo_odbc_gettypeinfo)
353 	PHP_FE(odbc_longreadlen, arginfo_odbc_longreadlen)
354 #if !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30)
355 	PHP_FE(odbc_next_result, arginfo_odbc_next_result)
356 #endif
357 	PHP_FE(odbc_num_fields, arginfo_odbc_num_fields)
358 	PHP_FE(odbc_num_rows, arginfo_odbc_num_rows)
359 	PHP_FE(odbc_pconnect, arginfo_odbc_pconnect)
360 	PHP_FE(odbc_prepare, arginfo_odbc_prepare)
361 	PHP_FE(odbc_result, arginfo_odbc_result)
362 	PHP_FE(odbc_result_all, arginfo_odbc_result_all)
363 	PHP_FE(odbc_rollback, arginfo_odbc_rollback)
364 	PHP_FE(odbc_setoption, arginfo_odbc_setoption)
365 	PHP_FE(odbc_specialcolumns, arginfo_odbc_specialcolumns)
366 	PHP_FE(odbc_statistics, arginfo_odbc_statistics)
367 	PHP_FE(odbc_tables, arginfo_odbc_tables)
368 	PHP_FE(odbc_primarykeys, arginfo_odbc_primarykeys)
369 #if !defined(HAVE_DBMAKER) && !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30) &&!defined(HAVE_SOLID_35)    /* not supported now */
370 	PHP_FE(odbc_columnprivileges, arginfo_odbc_columnprivileges)
371 	PHP_FE(odbc_tableprivileges, arginfo_odbc_tableprivileges)
372 #endif
373 #if !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30) && !defined(HAVE_SOLID_35) /* not supported */
374 	PHP_FE(odbc_foreignkeys, arginfo_odbc_foreignkeys)
375 	PHP_FE(odbc_procedures, arginfo_odbc_procedures)
376 	PHP_FE(odbc_procedurecolumns, arginfo_odbc_procedurecolumns)
377 #endif
378 	PHP_FALIAS(odbc_do, odbc_exec, arginfo_odbc_exec)
379 	PHP_FALIAS(odbc_field_precision, odbc_field_len, arginfo_odbc_field_len)
380 	PHP_FE_END
381 };
382 /* }}} */
383 
384 PHP_ODBC_API ZEND_DECLARE_MODULE_GLOBALS(odbc)
385 static PHP_GINIT_FUNCTION(odbc);
386 
387 /* {{{ odbc_module_entry
388  */
389 zend_module_entry odbc_module_entry = {
390 	STANDARD_MODULE_HEADER,
391 	"odbc",
392 	odbc_functions,
393 	PHP_MINIT(odbc),
394 	PHP_MSHUTDOWN(odbc),
395 	PHP_RINIT(odbc),
396 	PHP_RSHUTDOWN(odbc),
397 	PHP_MINFO(odbc),
398 	PHP_ODBC_VERSION,
399 	PHP_MODULE_GLOBALS(odbc),
400 	PHP_GINIT(odbc),
401 	NULL,
402 	NULL,
403 	STANDARD_MODULE_PROPERTIES_EX
404 };
405 /* }}} */
406 
407 #ifdef COMPILE_DL_ODBC
408 #ifdef ZTS
409 ZEND_TSRMLS_CACHE_DEFINE()
410 #endif
411 ZEND_GET_MODULE(odbc)
412 #endif
413 
414 /* {{{ _free_odbc_result
415  */
_free_odbc_result(zend_resource *rsrc)416 static void _free_odbc_result(zend_resource *rsrc)
417 {
418 	odbc_result *res = (odbc_result *)rsrc->ptr;
419 	int i;
420 
421 	if (res) {
422 		if (res->values) {
423 			for(i = 0; i < res->numcols; i++) {
424 				if (res->values[i].value)
425 					efree(res->values[i].value);
426 			}
427 			efree(res->values);
428 			res->values = NULL;
429 		}
430 		/* If aborted via timer expiration, don't try to call any unixODBC function */
431 		if (res->stmt && !(PG(connection_status) & PHP_CONNECTION_TIMEOUT)) {
432 #if defined(HAVE_SOLID) || defined(HAVE_SOLID_30) || defined(HAVE_SOLID_35)
433 			SQLTransact(res->conn_ptr->henv, res->conn_ptr->hdbc,
434 						(SQLUSMALLINT) SQL_COMMIT);
435 #endif
436 			SQLFreeStmt(res->stmt,SQL_DROP);
437 			/* We don't want the connection to be closed after the last statement has been closed
438 			 * Connections will be closed on shutdown
439 			 * zend_list_delete(res->conn_ptr->id);
440 			 */
441 		}
442 		if (res->param_info) {
443 			efree(res->param_info);
444 		}
445 		efree(res);
446 	}
447 }
448 /* }}} */
449 
450 /* {{{ safe_odbc_disconnect
451  * disconnect, and if it fails, then issue a rollback for any pending transaction (lurcher)
452  */
safe_odbc_disconnect( void *handle )453 static void safe_odbc_disconnect( void *handle )
454 {
455 	int ret;
456 
457 	ret = SQLDisconnect( handle );
458 	if ( ret == SQL_ERROR )
459 	{
460 		SQLTransact( NULL, handle, SQL_ROLLBACK );
461 		SQLDisconnect( handle );
462 	}
463 }
464 /* }}} */
465 
466 /* {{{ _close_odbc_conn
467  */
_close_odbc_conn(zend_resource *rsrc)468 static void _close_odbc_conn(zend_resource *rsrc)
469 {
470 	zend_resource *p;
471 	odbc_result *res;
472 
473 	odbc_connection *conn = (odbc_connection *)rsrc->ptr;
474 
475 	ZEND_HASH_FOREACH_PTR(&EG(regular_list), p) {
476 		if (p->ptr && (p->type == le_result)) {
477 			res = (odbc_result *)p->ptr;
478 			if (res->conn_ptr == conn) {
479 				zend_list_close(p);
480 			}
481 		}
482 	} ZEND_HASH_FOREACH_END();
483 
484 	/* If aborted via timer expiration, don't try to call any unixODBC function */
485 	if (!(PG(connection_status) & PHP_CONNECTION_TIMEOUT)) {
486 		safe_odbc_disconnect(conn->hdbc);
487 		SQLFreeConnect(conn->hdbc);
488 		SQLFreeEnv(conn->henv);
489 	}
490 	efree(conn);
491 	ODBCG(num_links)--;
492 }
493 /* }}} */
494 
495 /* {{{ void _close_odbc_pconn
496  */
_close_odbc_pconn(zend_resource *rsrc)497 static void _close_odbc_pconn(zend_resource *rsrc)
498 {
499 	zend_resource *p;
500 	odbc_result *res;
501 	odbc_connection *conn = (odbc_connection *)rsrc->ptr;
502 
503 	ZEND_HASH_FOREACH_PTR(&EG(regular_list), p) {
504 		if (p->ptr && (p->type == le_result)) {
505 			res = (odbc_result *)p->ptr;
506 			if (res->conn_ptr == conn) {
507 				zend_list_close(p);
508 			}
509 		}
510 	} ZEND_HASH_FOREACH_END();
511 
512 	/* If aborted via timer expiration, don't try to call any unixODBC function */
513 	if (!(PG(connection_status) & PHP_CONNECTION_TIMEOUT)) {
514 		safe_odbc_disconnect(conn->hdbc);
515 		SQLFreeConnect(conn->hdbc);
516 		SQLFreeEnv(conn->henv);
517 	}
518 	free(conn);
519 
520 	ODBCG(num_links)--;
521 	ODBCG(num_persistent)--;
522 }
523 /* }}} */
524 
525 /* {{{ PHP_INI_DISP(display_link_nums)
526  */
PHP_INI_DISPnull527 static PHP_INI_DISP(display_link_nums)
528 {
529 	char *value;
530 
531 	if (type == PHP_INI_DISPLAY_ORIG && ini_entry->modified) {
532 		value = ZSTR_VAL(ini_entry->orig_value);
533 	} else if (ini_entry->value) {
534 		value = ZSTR_VAL(ini_entry->value);
535 	} else {
536 		value = NULL;
537 	}
538 
539 	if (value) {
540 		if (atoi(value) == -1) {
541 			PUTS("Unlimited");
542 		} else {
543 			php_printf("%s", value);
544 		}
545 	}
546 }
547 /* }}} */
548 
549 /* {{{ PHP_INI_DISP(display_defPW)
550  */
PHP_INI_DISPnull551 static PHP_INI_DISP(display_defPW)
552 {
553 	char *value;
554 
555 	if (type == PHP_INI_DISPLAY_ORIG && ini_entry->modified) {
556 		value = ZSTR_VAL(ini_entry->orig_value);
557 	} else if (ini_entry->value) {
558 		value = ZSTR_VAL(ini_entry->value);
559 	} else {
560 		value = NULL;
561 	}
562 
563 	if (value) {
564 #if PHP_DEBUG
565 		php_printf("%s", value);
566 #else
567 		PUTS("********");
568 #endif
569 	} else {
570 		if (PG(html_errors)) {
571 			PUTS("<i>no value</i>");
572 		} else {
573 			PUTS("no value");
574 		}
575 	}
576 }
577 /* }}} */
578 
579 /* {{{ PHP_INI_DISP(display_binmode)
580  */
PHP_INI_DISPnull581 static PHP_INI_DISP(display_binmode)
582 {
583 	char *value;
584 
585 	if (type == PHP_INI_DISPLAY_ORIG && ini_entry->modified) {
586 		value = ZSTR_VAL(ini_entry->orig_value);
587 	} else if (ini_entry->value) {
588 		value = ZSTR_VAL(ini_entry->value);
589 	} else {
590 		value = NULL;
591 	}
592 
593 	if (value) {
594 		switch(atoi(value)) {
595 			case 0:
596 				PUTS("passthru");
597 				break;
598 			case 1:
599 				PUTS("return as is");
600 				break;
601 			case 2:
602 				PUTS("return as char");
603 				break;
604 		}
605 	}
606 }
607 /* }}} */
608 
609 /* {{{ PHP_INI_DISP(display_lrl)
610  */
PHP_INI_DISPnull611 static PHP_INI_DISP(display_lrl)
612 {
613 	char *value;
614 
615 	if (type == PHP_INI_DISPLAY_ORIG && ini_entry->modified) {
616 		value = ZSTR_VAL(ini_entry->orig_value);
617 	} else if (ini_entry->value) {
618 		value = ZSTR_VAL(ini_entry->value);
619 	} else {
620 		value = NULL;
621 	}
622 
623 	if (value) {
624 		if (atoi(value) <= 0) {
625 			PUTS("Passthru");
626 		} else {
627 			php_printf("return up to %s bytes", value);
628 		}
629 	}
630 }
631 /* }}} */
632 
633 
634 /* {{{ PHP_INI_DISP(display_cursortype)
635  */
PHP_INI_DISPnull636 static PHP_INI_DISP(display_cursortype)
637 {
638 	char *value;
639 
640 	if (type == PHP_INI_DISPLAY_ORIG && ini_entry->modified) {
641 		value = ZSTR_VAL(ini_entry->orig_value);
642 	} else if (ini_entry->value) {
643 		value = ZSTR_VAL(ini_entry->value);
644 	} else {
645 		value = NULL;
646 	}
647 
648 	if (value) {
649 		switch (atoi (value))
650 		  {
651 		    case SQL_CURSOR_FORWARD_ONLY:
652 				PUTS ("Forward Only cursor");
653 				break;
654 
655 			case SQL_CURSOR_STATIC:
656 			    PUTS ("Static cursor");
657 				break;
658 
659 			case SQL_CURSOR_KEYSET_DRIVEN:
660 				PUTS ("Keyset driven cursor");
661 				break;
662 
663 			case SQL_CURSOR_DYNAMIC:
664 				PUTS ("Dynamic cursor");
665 				break;
666 
667 			default:
668 				php_printf("Unknown cursor model %s", value);
669 				break;
670 		  }
671 	}
672 }
673 
674 /* }}} */
675 
676 /* {{{ PHP_INI_BEGIN
677  */
678 PHP_INI_BEGIN()
679 	STD_PHP_INI_BOOLEAN("odbc.allow_persistent", "1", PHP_INI_SYSTEM, OnUpdateLong,
680 			allow_persistent, zend_odbc_globals, odbc_globals)
681 	STD_PHP_INI_ENTRY_EX("odbc.max_persistent",  "-1", PHP_INI_SYSTEM, OnUpdateLong,
682 			max_persistent, zend_odbc_globals, odbc_globals, display_link_nums)
683 	STD_PHP_INI_ENTRY_EX("odbc.max_links", "-1", PHP_INI_SYSTEM, OnUpdateLong,
684 			max_links, zend_odbc_globals, odbc_globals, display_link_nums)
685 	STD_PHP_INI_ENTRY("odbc.default_db", NULL, PHP_INI_ALL, OnUpdateString,
686 			defDB, zend_odbc_globals, odbc_globals)
687 	STD_PHP_INI_ENTRY("odbc.default_user", NULL, PHP_INI_ALL, OnUpdateString,
688 			defUser, zend_odbc_globals, odbc_globals)
689 	STD_PHP_INI_ENTRY_EX("odbc.default_pw", NULL, PHP_INI_ALL, OnUpdateString,
690 			defPW, zend_odbc_globals, odbc_globals, display_defPW)
691 	STD_PHP_INI_ENTRY_EX("odbc.defaultlrl", "4096", PHP_INI_ALL, OnUpdateLong,
692 			defaultlrl, zend_odbc_globals, odbc_globals, display_lrl)
693 	STD_PHP_INI_ENTRY_EX("odbc.defaultbinmode", "1", PHP_INI_ALL, OnUpdateLong,
694 			defaultbinmode, zend_odbc_globals, odbc_globals, display_binmode)
695 	STD_PHP_INI_BOOLEAN("odbc.check_persistent", "1", PHP_INI_SYSTEM, OnUpdateLong,
696 			check_persistent, zend_odbc_globals, odbc_globals)
697 	STD_PHP_INI_ENTRY_EX("odbc.default_cursortype", "3", PHP_INI_ALL, OnUpdateLong,
698 			default_cursortype, zend_odbc_globals, odbc_globals, display_cursortype)
699 PHP_INI_END()
700 /* }}} */
701 
PHP_GINIT_FUNCTIONnull702 static PHP_GINIT_FUNCTION(odbc)
703 {
704 #if defined(COMPILE_DL_ODBC) && defined(ZTS)
705 	ZEND_TSRMLS_CACHE_UPDATE();
706 #endif
707 	odbc_globals->num_persistent = 0;
708 }
709 
710 /* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTIONnull711 PHP_MINIT_FUNCTION(odbc)
712 {
713 #ifdef SQLANY_BUG
714 	ODBC_SQL_CONN_T foobar;
715 	RETCODE rc;
716 #endif
717 
718 	REGISTER_INI_ENTRIES();
719 	le_result = zend_register_list_destructors_ex(_free_odbc_result, NULL, "odbc result", module_number);
720 	le_conn = zend_register_list_destructors_ex(_close_odbc_conn, NULL, "odbc link", module_number);
721 	le_pconn = zend_register_list_destructors_ex(NULL, _close_odbc_pconn, "odbc link persistent", module_number);
722 	odbc_module_entry.type = type;
723 
724 	REGISTER_STRING_CONSTANT("ODBC_TYPE", PHP_ODBC_TYPE, CONST_CS | CONST_PERSISTENT);
725 	REGISTER_LONG_CONSTANT("ODBC_BINMODE_PASSTHRU", 0, CONST_CS | CONST_PERSISTENT);
726 	REGISTER_LONG_CONSTANT("ODBC_BINMODE_RETURN", 1, CONST_CS | CONST_PERSISTENT);
727 	REGISTER_LONG_CONSTANT("ODBC_BINMODE_CONVERT", 2, CONST_CS | CONST_PERSISTENT);
728 	/* Define Constants for options
729 	   these Constants are defined in <sqlext.h>
730 	*/
731 	REGISTER_LONG_CONSTANT("SQL_ODBC_CURSORS", SQL_ODBC_CURSORS, CONST_PERSISTENT | CONST_CS);
732 	REGISTER_LONG_CONSTANT("SQL_CUR_USE_DRIVER", SQL_CUR_USE_DRIVER, CONST_PERSISTENT | CONST_CS);
733 	REGISTER_LONG_CONSTANT("SQL_CUR_USE_IF_NEEDED", SQL_CUR_USE_IF_NEEDED, CONST_PERSISTENT | CONST_CS);
734 	REGISTER_LONG_CONSTANT("SQL_CUR_USE_ODBC", SQL_CUR_USE_ODBC, CONST_PERSISTENT | CONST_CS);
735 
736 
737 	REGISTER_LONG_CONSTANT("SQL_CONCURRENCY", SQL_CONCURRENCY, CONST_PERSISTENT | CONST_CS);
738 	REGISTER_LONG_CONSTANT("SQL_CONCUR_READ_ONLY", SQL_CONCUR_READ_ONLY, CONST_PERSISTENT | CONST_CS);
739 	REGISTER_LONG_CONSTANT("SQL_CONCUR_LOCK", SQL_CONCUR_LOCK, CONST_PERSISTENT | CONST_CS);
740 	REGISTER_LONG_CONSTANT("SQL_CONCUR_ROWVER", SQL_CONCUR_ROWVER, CONST_PERSISTENT | CONST_CS);
741 	REGISTER_LONG_CONSTANT("SQL_CONCUR_VALUES", SQL_CONCUR_VALUES, CONST_PERSISTENT | CONST_CS);
742 
743 	REGISTER_LONG_CONSTANT("SQL_CURSOR_TYPE", SQL_CURSOR_TYPE, CONST_PERSISTENT | CONST_CS);
744 	REGISTER_LONG_CONSTANT("SQL_CURSOR_FORWARD_ONLY", SQL_CURSOR_FORWARD_ONLY, CONST_PERSISTENT | CONST_CS);
745 	REGISTER_LONG_CONSTANT("SQL_CURSOR_KEYSET_DRIVEN", SQL_CURSOR_KEYSET_DRIVEN, CONST_PERSISTENT | CONST_CS);
746 	REGISTER_LONG_CONSTANT("SQL_CURSOR_DYNAMIC", SQL_CURSOR_DYNAMIC, CONST_PERSISTENT | CONST_CS);
747 	REGISTER_LONG_CONSTANT("SQL_CURSOR_STATIC", SQL_CURSOR_STATIC, CONST_PERSISTENT | CONST_CS);
748 
749 	REGISTER_LONG_CONSTANT("SQL_KEYSET_SIZE", SQL_KEYSET_SIZE, CONST_PERSISTENT | CONST_CS);
750 
751 	/* these are for the Data Source type */
752 	REGISTER_LONG_CONSTANT("SQL_FETCH_FIRST", SQL_FETCH_FIRST, CONST_PERSISTENT | CONST_CS);
753 	REGISTER_LONG_CONSTANT("SQL_FETCH_NEXT", SQL_FETCH_NEXT, CONST_PERSISTENT | CONST_CS);
754 
755 	/*
756 	 * register the standard data types
757 	 */
758 	REGISTER_LONG_CONSTANT("SQL_CHAR", SQL_CHAR, CONST_PERSISTENT | CONST_CS);
759 	REGISTER_LONG_CONSTANT("SQL_VARCHAR", SQL_VARCHAR, CONST_PERSISTENT | CONST_CS);
760 	REGISTER_LONG_CONSTANT("SQL_LONGVARCHAR", SQL_LONGVARCHAR, CONST_PERSISTENT | CONST_CS);
761 	REGISTER_LONG_CONSTANT("SQL_DECIMAL", SQL_DECIMAL, CONST_PERSISTENT | CONST_CS);
762 	REGISTER_LONG_CONSTANT("SQL_NUMERIC", SQL_NUMERIC, CONST_PERSISTENT | CONST_CS);
763 	REGISTER_LONG_CONSTANT("SQL_BIT", SQL_BIT, CONST_PERSISTENT | CONST_CS);
764 	REGISTER_LONG_CONSTANT("SQL_TINYINT", SQL_TINYINT, CONST_PERSISTENT | CONST_CS);
765 	REGISTER_LONG_CONSTANT("SQL_SMALLINT", SQL_SMALLINT, CONST_PERSISTENT | CONST_CS);
766 	REGISTER_LONG_CONSTANT("SQL_INTEGER", SQL_INTEGER, CONST_PERSISTENT | CONST_CS);
767 	REGISTER_LONG_CONSTANT("SQL_BIGINT", SQL_BIGINT, CONST_PERSISTENT | CONST_CS);
768 	REGISTER_LONG_CONSTANT("SQL_REAL", SQL_REAL, CONST_PERSISTENT | CONST_CS);
769 	REGISTER_LONG_CONSTANT("SQL_FLOAT", SQL_FLOAT, CONST_PERSISTENT | CONST_CS);
770 	REGISTER_LONG_CONSTANT("SQL_DOUBLE", SQL_DOUBLE, CONST_PERSISTENT | CONST_CS);
771 	REGISTER_LONG_CONSTANT("SQL_BINARY", SQL_BINARY, CONST_PERSISTENT | CONST_CS);
772 	REGISTER_LONG_CONSTANT("SQL_VARBINARY", SQL_VARBINARY, CONST_PERSISTENT | CONST_CS);
773 	REGISTER_LONG_CONSTANT("SQL_LONGVARBINARY", SQL_LONGVARBINARY, CONST_PERSISTENT | CONST_CS);
774 	REGISTER_LONG_CONSTANT("SQL_DATE", SQL_DATE, CONST_PERSISTENT | CONST_CS);
775 	REGISTER_LONG_CONSTANT("SQL_TIME", SQL_TIME, CONST_PERSISTENT | CONST_CS);
776 	REGISTER_LONG_CONSTANT("SQL_TIMESTAMP", SQL_TIMESTAMP, CONST_PERSISTENT | CONST_CS);
777 #if defined(ODBCVER) && (ODBCVER >= 0x0300)
778 	REGISTER_LONG_CONSTANT("SQL_TYPE_DATE", SQL_TYPE_DATE, CONST_PERSISTENT | CONST_CS);
779 	REGISTER_LONG_CONSTANT("SQL_TYPE_TIME", SQL_TYPE_TIME, CONST_PERSISTENT | CONST_CS);
780 	REGISTER_LONG_CONSTANT("SQL_TYPE_TIMESTAMP", SQL_TYPE_TIMESTAMP, CONST_PERSISTENT | CONST_CS);
781 	REGISTER_LONG_CONSTANT("SQL_WCHAR", SQL_WCHAR, CONST_PERSISTENT | CONST_CS);
782 	REGISTER_LONG_CONSTANT("SQL_WVARCHAR", SQL_WVARCHAR, CONST_PERSISTENT | CONST_CS);
783 	REGISTER_LONG_CONSTANT("SQL_WLONGVARCHAR", SQL_WLONGVARCHAR, CONST_PERSISTENT | CONST_CS);
784 
785 	/*
786 	 * SQLSpecialColumns values
787 	 */
788 	REGISTER_LONG_CONSTANT("SQL_BEST_ROWID", SQL_BEST_ROWID, CONST_PERSISTENT | CONST_CS);
789 	REGISTER_LONG_CONSTANT("SQL_ROWVER", SQL_ROWVER, CONST_PERSISTENT | CONST_CS);
790 	REGISTER_LONG_CONSTANT("SQL_SCOPE_CURROW", SQL_SCOPE_CURROW, CONST_PERSISTENT | CONST_CS);
791 	REGISTER_LONG_CONSTANT("SQL_SCOPE_TRANSACTION", SQL_SCOPE_TRANSACTION, CONST_PERSISTENT | CONST_CS);
792 	REGISTER_LONG_CONSTANT("SQL_SCOPE_SESSION", SQL_SCOPE_SESSION, CONST_PERSISTENT | CONST_CS);
793 	REGISTER_LONG_CONSTANT("SQL_NO_NULLS", SQL_NO_NULLS, CONST_PERSISTENT | CONST_CS);
794 	REGISTER_LONG_CONSTANT("SQL_NULLABLE", SQL_NULLABLE, CONST_PERSISTENT | CONST_CS);
795 
796 	/*
797 	 * SQLStatistics values
798 	 */
799 	REGISTER_LONG_CONSTANT("SQL_INDEX_UNIQUE", SQL_INDEX_UNIQUE, CONST_PERSISTENT | CONST_CS);
800 	REGISTER_LONG_CONSTANT("SQL_INDEX_ALL", SQL_INDEX_ALL, CONST_PERSISTENT | CONST_CS);
801 	REGISTER_LONG_CONSTANT("SQL_ENSURE", SQL_ENSURE, CONST_PERSISTENT | CONST_CS);
802 	REGISTER_LONG_CONSTANT("SQL_QUICK", SQL_QUICK, CONST_PERSISTENT | CONST_CS);
803 #endif
804 
805 #if defined(HAVE_IBMDB2) && defined(_AIX)
806 	/* atexit() handler in the DB2/AIX library segfaults in PHP CLI */
807 	/* DB2NOEXITLIST env variable prevents DB2 from invoking atexit() */
808 	putenv("DB2NOEXITLIST=TRUE");
809 #endif
810 
811 	return SUCCESS;
812 }
813 /* }}} */
814 
815 /* {{{ PHP_RINIT_FUNCTION */
PHP_RINIT_FUNCTIONnull816 PHP_RINIT_FUNCTION(odbc)
817 {
818 	ODBCG(defConn) = -1;
819 	ODBCG(num_links) = ODBCG(num_persistent);
820 	memset(ODBCG(laststate), '\0', 6);
821 	memset(ODBCG(lasterrormsg), '\0', SQL_MAX_MESSAGE_LENGTH);
822 	return SUCCESS;
823 }
824 /* }}} */
825 
826 /* {{{ PHP_RSHUTDOWN_FUNCTION */
PHP_RSHUTDOWN_FUNCTIONnull827 PHP_RSHUTDOWN_FUNCTION(odbc)
828 {
829 	return SUCCESS;
830 }
831 /* }}} */
832 
833 /* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTIONnull834 PHP_MSHUTDOWN_FUNCTION(odbc)
835 {
836 	UNREGISTER_INI_ENTRIES();
837 	return SUCCESS;
838 }
839 /* }}} */
840 
841 /* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTIONnull842 PHP_MINFO_FUNCTION(odbc)
843 {
844 	char buf[32];
845 
846 	php_info_print_table_start();
847 	php_info_print_table_header(2, "ODBC Support", "enabled");
848 	snprintf(buf, sizeof(buf), ZEND_LONG_FMT, ODBCG(num_persistent));
849 	php_info_print_table_row(2, "Active Persistent Links", buf);
850 	snprintf(buf, sizeof(buf), ZEND_LONG_FMT, ODBCG(num_links));
851 	php_info_print_table_row(2, "Active Links", buf);
852 	php_info_print_table_row(2, "ODBC library", PHP_ODBC_TYPE);
853 #ifdef ODBCVER
854 	snprintf(buf, sizeof(buf), "0x%0.4x", ODBCVER);
855 	php_info_print_table_row(2, "ODBCVER", buf);
856 #endif
857 #ifndef PHP_WIN32
858 	php_info_print_table_row(2, "ODBC_CFLAGS", PHP_ODBC_CFLAGS);
859 	php_info_print_table_row(2, "ODBC_LFLAGS", PHP_ODBC_LFLAGS);
860 	php_info_print_table_row(2, "ODBC_LIBS", PHP_ODBC_LIBS);
861 #endif
862 	php_info_print_table_end();
863 
864 	DISPLAY_INI_ENTRIES();
865 
866 }
867 /* }}} */
868 
869 /* {{{ odbc_sql_error */
odbc_sql_errornull870 void odbc_sql_error(ODBC_SQL_ERROR_PARAMS)
871 {
872 	SQLINTEGER	error;        /* Not used */
873 	SQLSMALLINT	errormsgsize; /* Not used */
874 	RETCODE rc;
875 	ODBC_SQL_ENV_T henv;
876 	ODBC_SQL_CONN_T conn;
877 
878 	if (conn_resource) {
879 		henv = conn_resource->henv;
880 		conn = conn_resource->hdbc;
881 	} else {
882 		henv = SQL_NULL_HENV;
883 		conn = SQL_NULL_HDBC;
884 	}
885 
886 	/* This leads to an endless loop in many drivers!
887 	 *
888 	   while(henv != SQL_NULL_HENV){
889 		do {
890 	 */
891 	rc = SQLError(henv, conn, stmt, ODBCG(laststate), &error, ODBCG(lasterrormsg), sizeof(ODBCG(lasterrormsg))-1, &errormsgsize);
892 	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
893 		snprintf(ODBCG(laststate), sizeof(ODBCG(laststate)), "HY000");
894 		snprintf(ODBCG(lasterrormsg), sizeof(ODBCG(lasterrormsg)), "Failed to fetch error message");
895 	}
896 	if (conn_resource) {
897 		memcpy(conn_resource->laststate, ODBCG(laststate), sizeof(ODBCG(laststate)));
898 		memcpy(conn_resource->lasterrormsg, ODBCG(lasterrormsg), sizeof(ODBCG(lasterrormsg)));
899 	}
900 	if (func) {
901 		php_error_docref(NULL, E_WARNING, "SQL error: %s, SQL state %s in %s", ODBCG(lasterrormsg), ODBCG(laststate), func);
902 	} else {
903 		php_error_docref(NULL, E_WARNING, "SQL error: %s, SQL state %s", ODBCG(lasterrormsg), ODBCG(laststate));
904 	}
905 	/*
906 		} while (SQL_SUCCEEDED(rc));
907 	}
908 	*/
909 }
910 /* }}} */
911 
912 /* {{{ php_odbc_fetch_attribs */
php_odbc_fetch_attribs(INTERNAL_FUNCTION_PARAMETERS, int mode)913 void php_odbc_fetch_attribs(INTERNAL_FUNCTION_PARAMETERS, int mode)
914 {
915 	odbc_result *result;
916 	zval *pv_res;
917 	zend_long flag;
918 
919 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &pv_res, &flag) == FAILURE) {
920 		return;
921 	}
922 
923 	if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
924 		RETURN_FALSE;
925 	}
926 
927 	if (mode) {
928 		result->longreadlen = flag;
929 	} else {
930 		result->binmode = flag;
931 	}
932 
933 	RETURN_TRUE;
934 }
935 /* }}} */
936 
937 /* {{{ odbc_bindcols */
odbc_bindcols(odbc_result *result)938 int odbc_bindcols(odbc_result *result)
939 {
940 	RETCODE rc;
941 	int i;
942 	SQLSMALLINT 	colnamelen; /* Not used */
943 	SQLLEN      	displaysize;
944 	SQLUSMALLINT	colfieldid;
945 	int		charextraalloc;
946 
947 	result->values = (odbc_result_value *) safe_emalloc(sizeof(odbc_result_value), result->numcols, 0);
948 
949 	result->longreadlen = ODBCG(defaultlrl);
950 	result->binmode = ODBCG(defaultbinmode);
951 
952 	for(i = 0; i < result->numcols; i++) {
953 		charextraalloc = 0;
954 		colfieldid = SQL_COLUMN_DISPLAY_SIZE;
955 
956 		rc = PHP_ODBC_SQLCOLATTRIBUTE(result->stmt, (SQLUSMALLINT)(i+1), PHP_ODBC_SQL_DESC_NAME,
957 				result->values[i].name, sizeof(result->values[i].name), &colnamelen, 0);
958 		result->values[i].coltype = 0;
959 		rc = PHP_ODBC_SQLCOLATTRIBUTE(result->stmt, (SQLUSMALLINT)(i+1), SQL_COLUMN_TYPE,
960 				NULL, 0, NULL, &result->values[i].coltype);
961 
962 		/* Don't bind LONG / BINARY columns, so that fetch behaviour can
963 		 * be controlled by odbc_binmode() / odbc_longreadlen()
964 		 */
965 
966 		switch(result->values[i].coltype) {
967 			case SQL_BINARY:
968 			case SQL_VARBINARY:
969 			case SQL_LONGVARBINARY:
970 			case SQL_LONGVARCHAR:
971 #if defined(ODBCVER) && (ODBCVER >= 0x0300)
972 			case SQL_WLONGVARCHAR:
973 #endif
974 				result->values[i].value = NULL;
975 				break;
976 
977 #ifdef HAVE_ADABAS
978 			case SQL_TIMESTAMP:
979 				result->values[i].value = (char *)emalloc(27);
980 				SQLBindCol(result->stmt, (SQLUSMALLINT)(i+1), SQL_C_CHAR, result->values[i].value,
981 							27, &result->values[i].vallen);
982 				break;
983 #endif /* HAVE_ADABAS */
984 			case SQL_CHAR:
985 			case SQL_VARCHAR:
986 #if defined(ODBCVER) && (ODBCVER >= 0x0300)
987 			case SQL_WCHAR:
988 			case SQL_WVARCHAR:
989 				colfieldid = SQL_DESC_OCTET_LENGTH;
990 #else
991 				charextraalloc = 1;
992 #endif
993 			default:
994 				rc = PHP_ODBC_SQLCOLATTRIBUTE(result->stmt, (SQLUSMALLINT)(i+1), colfieldid,
995 								NULL, 0, NULL, &displaysize);
996 				if (rc != SQL_SUCCESS) {
997 					displaysize = 0;
998 				}
999 #if defined(ODBCVER) && (ODBCVER >= 0x0300)
1000 				if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO && colfieldid == SQL_DESC_OCTET_LENGTH) {
1001 					SQLINTEGER err;
1002 					SQLCHAR errtxt[128];
1003 					SQLCHAR state[6];
1004 
1005 					memset(errtxt, '\0', 128);
1006 					memset(state, '\0', 6);
1007 
1008 					if (SQL_SUCCESS == SQLGetDiagRec(SQL_HANDLE_STMT, result->stmt, 1, state, &err, errtxt, 128, NULL)) {
1009 						errtxt[127] = '\0';
1010 						state[5] = '\0';
1011 						php_error_docref(NULL, E_WARNING, "SQLColAttribute can't handle SQL_DESC_OCTET_LENGTH: [%s] %s", state, errtxt);
1012 					}
1013 					 /* This is  a quirk for ODBC 2.0 compatibility for broken driver implementations.
1014 					  */
1015 					charextraalloc = 1;
1016 					rc = SQLColAttributes(result->stmt, (SQLUSMALLINT)(i+1), SQL_COLUMN_DISPLAY_SIZE,
1017 								NULL, 0, NULL, &displaysize);
1018 					if (rc != SQL_SUCCESS) {
1019 						displaysize = 0;
1020 					}
1021 				}
1022 
1023 				/* Workaround for drivers that report NVARCHAR(MAX) columns as SQL_WVARCHAR with size 0 (bug #69975) */
1024 				if (result->values[i].coltype == SQL_WVARCHAR && displaysize == 0) {
1025 					result->values[i].coltype = SQL_WLONGVARCHAR;
1026 					result->values[i].value = NULL;
1027 					break;
1028 				}
1029 #endif
1030 				/* Workaround for drivers that report VARCHAR(MAX) columns as SQL_VARCHAR (bug #73725) */
1031 				if (SQL_VARCHAR == result->values[i].coltype && displaysize == 0) {
1032 					result->values[i].coltype = SQL_LONGVARCHAR;
1033 					result->values[i].value = NULL;
1034 					break;
1035 				}
1036 
1037 				/* Workaround for Oracle ODBC Driver bug (#50162) when fetching TIMESTAMP column */
1038 				if (result->values[i].coltype == SQL_TIMESTAMP) {
1039 					displaysize += 3;
1040 				}
1041 
1042 				if (charextraalloc) {
1043 					/* Since we don't know the exact # of bytes, allocate extra */
1044 					displaysize *= 4;
1045 				}
1046 				result->values[i].value = (char *)emalloc(displaysize + 1);
1047 				rc = SQLBindCol(result->stmt, (SQLUSMALLINT)(i+1), SQL_C_CHAR, result->values[i].value,
1048 							displaysize + 1, &result->values[i].vallen);
1049 				break;
1050 		}
1051 	}
1052 	return 1;
1053 }
1054 /* }}} */
1055 
1056 /* {{{ odbc_transact */
odbc_transact(INTERNAL_FUNCTION_PARAMETERS, int type)1057 void odbc_transact(INTERNAL_FUNCTION_PARAMETERS, int type)
1058 {
1059 	odbc_connection *conn;
1060 	RETCODE rc;
1061 	zval *pv_conn;
1062 
1063 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pv_conn) == FAILURE) {
1064 		return;
1065 	}
1066 
1067 	if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
1068 		RETURN_FALSE;
1069 	}
1070 
1071 	rc = SQLTransact(conn->henv, conn->hdbc, (SQLUSMALLINT)((type)?SQL_COMMIT:SQL_ROLLBACK));
1072 	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
1073 		odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLTransact");
1074 		RETURN_FALSE;
1075 	}
1076 
1077 	RETURN_TRUE;
1078 }
1079 /* }}} */
1080 
1081 /* {{{ _close_pconn_with_res */
_close_pconn_with_res(zend_resource *le, zend_resource *res)1082 static int _close_pconn_with_res(zend_resource *le, zend_resource *res)
1083 {
1084 	if (le->type == le_pconn && (((odbc_connection *)(le->ptr))->res == res)){
1085 		return 1;
1086 	}else{
1087 		return 0;
1088 	}
1089 }
1090 /* }}} */
1091 
1092 /* {{{ odbc_column_lengths */
odbc_column_lengths(INTERNAL_FUNCTION_PARAMETERS, int type)1093 void odbc_column_lengths(INTERNAL_FUNCTION_PARAMETERS, int type)
1094 {
1095 	odbc_result *result;
1096 #if defined(HAVE_SOLID) || defined(HAVE_SOLID_30)
1097 	/* this seems to be necessary for Solid2.3 ( tested by
1098 	 * tammy@synchronis.com) and Solid 3.0 (tested by eric@terra.telemediair.nl)
1099 	 * Solid does not seem to declare a SQLINTEGER, but it does declare a
1100 	 * SQL_INTEGER which does not work (despite being the same type as a SDWORD.
1101 	 * Solid 3.5 does not have this issue.
1102 	 */
1103 	SDWORD len;
1104 #else
1105 	SQLLEN len;
1106 #endif
1107 	zval *pv_res;
1108 	zend_long pv_num;
1109 
1110 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &pv_res, &pv_num) == FAILURE) {
1111 		return;
1112 	}
1113 
1114 	if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
1115 		RETURN_FALSE;
1116 	}
1117 
1118 	if (result->numcols == 0) {
1119 		php_error_docref(NULL, E_WARNING, "No tuples available at this result index");
1120 		RETURN_FALSE;
1121 	}
1122 
1123 	if (pv_num > result->numcols) {
1124 		php_error_docref(NULL, E_WARNING, "Field index larger than number of fields");
1125 		RETURN_FALSE;
1126 	}
1127 
1128 	if (pv_num < 1) {
1129 		php_error_docref(NULL, E_WARNING, "Field numbering starts at 1");
1130 		RETURN_FALSE;
1131 	}
1132 
1133 	PHP_ODBC_SQLCOLATTRIBUTE(result->stmt, (SQLUSMALLINT)pv_num, (SQLUSMALLINT) (type?SQL_COLUMN_SCALE:SQL_COLUMN_PRECISION), NULL, 0, NULL, &len);
1134 
1135 	RETURN_LONG(len);
1136 }
1137 /* }}} */
1138 
1139 /* Main User Functions */
1140 
1141 /* {{{ proto void odbc_close_all(void)
1142    Close all ODBC connections */
PHP_FUNCTIONnull1143 PHP_FUNCTION(odbc_close_all)
1144 {
1145 	zend_resource *p;
1146 
1147 	if (zend_parse_parameters_none() == FAILURE) {
1148 		return;
1149 	}
1150 
1151 	/* Loop through list and close all statements */
1152 	ZEND_HASH_FOREACH_PTR(&EG(regular_list), p) {
1153 		if (p->ptr && (p->type == le_result)) {
1154 			zend_list_close(p);
1155 		}
1156 	} ZEND_HASH_FOREACH_END();
1157 
1158 	/* Second loop through list, now close all connections */
1159 	ZEND_HASH_FOREACH_PTR(&EG(regular_list), p) {
1160 		if (p->ptr) {
1161 			if (p->type == le_conn){
1162 				zend_list_close(p);
1163 			} else if (p->type == le_pconn){
1164 				zend_list_close(p);
1165 				/* Delete the persistent connection */
1166 				zend_hash_apply_with_argument(&EG(persistent_list),
1167 					(apply_func_arg_t) _close_pconn_with_res, (void *)p);
1168 			}
1169 		}
1170 	} ZEND_HASH_FOREACH_END();
1171 }
1172 /* }}} */
1173 
1174 /* {{{ proto bool odbc_binmode(int result_id, int mode)
1175    Handle binary column data */
PHP_FUNCTIONnull1176 PHP_FUNCTION(odbc_binmode)
1177 {
1178 	php_odbc_fetch_attribs(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
1179 }
1180 /* }}} */
1181 
1182 /* {{{ proto bool odbc_longreadlen(int result_id, int length)
1183    Handle LONG columns */
PHP_FUNCTIONnull1184 PHP_FUNCTION(odbc_longreadlen)
1185 {
1186 	php_odbc_fetch_attribs(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
1187 }
1188 /* }}} */
1189 
1190 /* {{{ proto resource odbc_prepare(resource connection_id, string query)
1191    Prepares a statement for execution */
PHP_FUNCTIONnull1192 PHP_FUNCTION(odbc_prepare)
1193 {
1194 	zval *pv_conn;
1195 	char *query;
1196 	size_t query_len;
1197 	odbc_result *result = NULL;
1198 	odbc_connection *conn;
1199 	RETCODE rc;
1200 	int i;
1201 #ifdef HAVE_SQL_EXTENDED_FETCH
1202 	SQLUINTEGER      scrollopts;
1203 #endif
1204 
1205 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs", &pv_conn, &query, &query_len) == FAILURE) {
1206 		return;
1207 	}
1208 
1209 	if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
1210 		RETURN_FALSE;
1211 	}
1212 
1213 	result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
1214 
1215 	result->numparams = 0;
1216 	result->param_info = NULL;
1217 
1218 	rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
1219 	if (rc == SQL_INVALID_HANDLE) {
1220 		efree(result);
1221 		php_error_docref(NULL, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
1222 		RETURN_FALSE;
1223 	}
1224 
1225 	if (rc == SQL_ERROR) {
1226 		odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
1227 		efree(result);
1228 		RETURN_FALSE;
1229 	}
1230 
1231 #ifdef HAVE_SQL_EXTENDED_FETCH
1232 	/* Solid doesn't have ExtendedFetch, if DriverManager is used, get Info,
1233 	   whether Driver supports ExtendedFetch */
1234 	rc = SQLGetInfo(conn->hdbc, SQL_FETCH_DIRECTION, (void *) &scrollopts, sizeof(scrollopts), NULL);
1235 	if (rc == SQL_SUCCESS) {
1236 		if ((result->fetch_abs = (scrollopts & SQL_FD_FETCH_ABSOLUTE))) {
1237 			/* Try to set CURSOR_TYPE to dynamic. Driver will replace this with other
1238 			   type if not possible.
1239 			*/
1240 			SQLSetStmtOption(result->stmt, SQL_CURSOR_TYPE, ODBCG(default_cursortype));
1241 		}
1242 	} else {
1243 		result->fetch_abs = 0;
1244 	}
1245 #endif
1246 
1247 	rc = SQLPrepare(result->stmt, query, SQL_NTS);
1248 	switch (rc) {
1249 		case SQL_SUCCESS:
1250 			break;
1251 		case SQL_SUCCESS_WITH_INFO:
1252 			odbc_sql_error(conn, result->stmt, "SQLPrepare");
1253 			break;
1254 		default:
1255 			odbc_sql_error(conn, result->stmt, "SQLPrepare");
1256 			RETURN_FALSE;
1257 	}
1258 
1259 	SQLNumParams(result->stmt, &(result->numparams));
1260 	SQLNumResultCols(result->stmt, &(result->numcols));
1261 
1262 	if (result->numcols > 0) {
1263 		if (!odbc_bindcols(result)) {
1264 			efree(result);
1265 			RETURN_FALSE;
1266 		}
1267 	} else {
1268 		result->values = NULL;
1269 	}
1270 	Z_ADDREF_P(pv_conn);
1271 	result->conn_ptr = conn;
1272 	result->fetched = 0;
1273 
1274 	result->param_info = (odbc_param_info *) safe_emalloc(sizeof(odbc_param_info), result->numparams, 0);
1275 	for (i=0;i<result->numparams;i++) {
1276 		rc = SQLDescribeParam(result->stmt, (SQLUSMALLINT)(i+1), &result->param_info[i].sqltype, &result->param_info[i].precision,
1277 													&result->param_info[i].scale, &result->param_info[i].nullable);
1278 		if (rc == SQL_ERROR) {
1279 			odbc_sql_error(result->conn_ptr, result->stmt, "SQLDescribeParameter");
1280 			SQLFreeStmt(result->stmt, SQL_RESET_PARAMS);
1281 			efree(result->param_info);
1282 			efree(result);
1283 			RETURN_FALSE;
1284 		}
1285 	}
1286 
1287 	RETURN_RES(zend_register_resource(result, le_result));
1288 }
1289 /* }}} */
1290 
1291 /*
1292  * Execute prepared SQL statement. Supports only input parameters.
1293  */
1294 
1295 /* {{{ proto bool odbc_execute(resource result_id [, array parameters_array])
1296    Execute a prepared statement */
PHP_FUNCTIONnull1297 PHP_FUNCTION(odbc_execute)
1298 {
1299 	zval *pv_res, *pv_param_arr, *tmp;
1300 	typedef struct params_t {
1301 		SQLLEN vallen;
1302 		int fp;
1303 	} params_t;
1304 	params_t *params = NULL;
1305 	char *filename;
1306 	unsigned char otype;
1307 	SQLSMALLINT ctype;
1308    	odbc_result *result;
1309 	int numArgs = ZEND_NUM_ARGS(), i, ne;
1310 	RETCODE rc;
1311 
1312 	if (zend_parse_parameters(numArgs, "r|a/", &pv_res, &pv_param_arr) == FAILURE) {
1313 		return;
1314 	}
1315 
1316 	if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
1317 		RETURN_FALSE;
1318 	}
1319 
1320 	/* XXX check for already bound parameters*/
1321 	if (result->numparams > 0 && numArgs == 1) {
1322 		php_error_docref(NULL, E_WARNING, "No parameters to SQL statement given");
1323 		RETURN_FALSE;
1324 	}
1325 
1326 	if (result->numparams > 0) {
1327 		if ((ne = zend_hash_num_elements(Z_ARRVAL_P(pv_param_arr))) < result->numparams) {
1328 			php_error_docref(NULL, E_WARNING,"Not enough parameters (%d should be %d) given", ne, result->numparams);
1329 			RETURN_FALSE;
1330 		}
1331 
1332 		zend_hash_internal_pointer_reset(Z_ARRVAL_P(pv_param_arr));
1333 		params = (params_t *)safe_emalloc(sizeof(params_t), result->numparams, 0);
1334 		for(i = 0; i < result->numparams; i++) {
1335 			params[i].fp = -1;
1336 		}
1337 
1338 		for(i = 1; i <= result->numparams; i++) {
1339 			if ((tmp = zend_hash_get_current_data(Z_ARRVAL_P(pv_param_arr))) == NULL) {
1340 				php_error_docref(NULL, E_WARNING,"Error getting parameter");
1341 				SQLFreeStmt(result->stmt,SQL_RESET_PARAMS);
1342 				for (i = 0; i < result->numparams; i++) {
1343 					if (params[i].fp != -1) {
1344 						close(params[i].fp);
1345 					}
1346 				}
1347 				efree(params);
1348 				RETURN_FALSE;
1349 			}
1350 
1351 			otype = Z_TYPE_P(tmp);
1352 			if (!try_convert_to_string(tmp)) {
1353 				SQLFreeStmt(result->stmt, SQL_RESET_PARAMS);
1354 				for (i = 0; i < result->numparams; i++) {
1355 					if (params[i].fp != -1) {
1356 						close(params[i].fp);
1357 					}
1358 				}
1359 				efree(params);
1360 				RETURN_FALSE;
1361 			}
1362 
1363 			params[i-1].vallen = Z_STRLEN_P(tmp);
1364 			params[i-1].fp = -1;
1365 
1366 			if (IS_SQL_BINARY(result->param_info[i-1].sqltype)) {
1367 				ctype = SQL_C_BINARY;
1368 			} else {
1369 				ctype = SQL_C_CHAR;
1370 			}
1371 
1372 			if (Z_STRLEN_P(tmp) > 2 &&
1373 				Z_STRVAL_P(tmp)[0] == '\'' &&
1374 				Z_STRVAL_P(tmp)[Z_STRLEN_P(tmp) - 1] == '\'') {
1375 
1376 				if (CHECK_ZVAL_NULL_PATH(tmp)) {
1377 					RETURN_FALSE;
1378 				}
1379 				filename = estrndup(&Z_STRVAL_P(tmp)[1], Z_STRLEN_P(tmp) - 2);
1380 				filename[strlen(filename)] = '\0';
1381 
1382 				/* Check the basedir */
1383 				if (php_check_open_basedir(filename)) {
1384 					efree(filename);
1385 					SQLFreeStmt(result->stmt, SQL_RESET_PARAMS);
1386 					for (i = 0; i < result->numparams; i++) {
1387 						if (params[i].fp != -1) {
1388 							close(params[i].fp);
1389 						}
1390 					}
1391 					efree(params);
1392 					RETURN_FALSE;
1393 				}
1394 
1395 				if ((params[i-1].fp = open(filename,O_RDONLY)) == -1) {
1396 					php_error_docref(NULL, E_WARNING,"Can't open file %s", filename);
1397 					SQLFreeStmt(result->stmt, SQL_RESET_PARAMS);
1398 					for (i = 0; i < result->numparams; i++) {
1399 						if (params[i].fp != -1) {
1400 							close(params[i].fp);
1401 						}
1402 					}
1403 					efree(params);
1404 					efree(filename);
1405 					RETURN_FALSE;
1406 				}
1407 
1408 				efree(filename);
1409 
1410 				params[i-1].vallen = SQL_LEN_DATA_AT_EXEC(0);
1411 
1412 				rc = SQLBindParameter(result->stmt, (SQLUSMALLINT)i, SQL_PARAM_INPUT,
1413 									  ctype, result->param_info[i-1].sqltype, result->param_info[i-1].precision, result->param_info[i-1].scale,
1414 									  (void *)(intptr_t)params[i-1].fp, 0,
1415 									  &params[i-1].vallen);
1416 			} else {
1417 #ifdef HAVE_DBMAKER
1418 				precision = params[i-1].vallen;
1419 #endif
1420 				if (otype == IS_NULL) {
1421 					params[i-1].vallen = SQL_NULL_DATA;
1422 				}
1423 
1424 				rc = SQLBindParameter(result->stmt, (SQLUSMALLINT)i, SQL_PARAM_INPUT,
1425 									  ctype, result->param_info[i-1].sqltype, result->param_info[i-1].precision, result->param_info[i-1].scale,
1426 									  Z_STRVAL_P(tmp), 0,
1427 									  &params[i-1].vallen);
1428 			}
1429 			if (rc == SQL_ERROR) {
1430 				odbc_sql_error(result->conn_ptr, result->stmt, "SQLBindParameter");
1431 				SQLFreeStmt(result->stmt, SQL_RESET_PARAMS);
1432 				for (i = 0; i < result->numparams; i++) {
1433 					if (params[i].fp != -1) {
1434 						close(params[i].fp);
1435 					}
1436 				}
1437 				efree(params);
1438 				RETURN_FALSE;
1439 			}
1440 			zend_hash_move_forward(Z_ARRVAL_P(pv_param_arr));
1441 		}
1442 	}
1443 	/* Close cursor, needed for doing multiple selects */
1444 	rc = SQLFreeStmt(result->stmt, SQL_CLOSE);
1445 
1446 	if (rc == SQL_ERROR) {
1447 		odbc_sql_error(result->conn_ptr, result->stmt, "SQLFreeStmt");
1448 	}
1449 
1450 	rc = SQLExecute(result->stmt);
1451 
1452 	result->fetched = 0;
1453 	if (rc == SQL_NEED_DATA) {
1454 		char buf[4096];
1455 		int fp, nbytes;
1456 		while (rc == SQL_NEED_DATA) {
1457 			rc = SQLParamData(result->stmt, (void*)&fp);
1458 			if (rc == SQL_NEED_DATA) {
1459 				while ((nbytes = read(fp, &buf, 4096)) > 0) {
1460 					SQLPutData(result->stmt, (void*)&buf, nbytes);
1461 				}
1462 			}
1463 		}
1464 	} else {
1465 		switch (rc) {
1466 			case SQL_SUCCESS:
1467 				break;
1468 			case SQL_NO_DATA_FOUND:
1469 			case SQL_SUCCESS_WITH_INFO:
1470 				odbc_sql_error(result->conn_ptr, result->stmt, "SQLExecute");
1471 				break;
1472 			default:
1473 				odbc_sql_error(result->conn_ptr, result->stmt, "SQLExecute");
1474 				RETVAL_FALSE;
1475 		}
1476 	}
1477 
1478 	if (result->numparams > 0) {
1479 		SQLFreeStmt(result->stmt, SQL_RESET_PARAMS);
1480 		for(i = 0; i < result->numparams; i++) {
1481 			if (params[i].fp != -1) {
1482 				close(params[i].fp);
1483 			}
1484 		}
1485 		efree(params);
1486 	}
1487 
1488 	if (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO || rc == SQL_NO_DATA_FOUND) {
1489 		RETVAL_TRUE;
1490 	}
1491 
1492 	if (result->numcols == 0) {
1493 		SQLNumResultCols(result->stmt, &(result->numcols));
1494 
1495 		if (result->numcols > 0) {
1496 			if (!odbc_bindcols(result)) {
1497 				efree(result);
1498 				RETVAL_FALSE;
1499 			}
1500 		} else {
1501 			result->values = NULL;
1502 		}
1503 	}
1504 }
1505 /* }}} */
1506 
1507 /* {{{ proto string odbc_cursor(resource result_id)
1508    Get cursor name */
PHP_FUNCTIONnull1509 PHP_FUNCTION(odbc_cursor)
1510 {
1511 	zval *pv_res;
1512 	SQLUSMALLINT max_len;
1513 	SQLSMALLINT len;
1514 	char *cursorname;
1515    	odbc_result *result;
1516 	RETCODE rc;
1517 
1518 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pv_res) == FAILURE) {
1519 		return;
1520 	}
1521 
1522 	if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
1523 		RETURN_FALSE;
1524 	}
1525 
1526 	rc = SQLGetInfo(result->conn_ptr->hdbc,SQL_MAX_CURSOR_NAME_LEN, (void *)&max_len,sizeof(max_len),&len);
1527 	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
1528 		RETURN_FALSE;
1529 	}
1530 
1531 	if (max_len > 0) {
1532 		cursorname = emalloc(max_len + 1);
1533 		rc = SQLGetCursorName(result->stmt,cursorname,(SQLSMALLINT)max_len,&len);
1534 		if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
1535 			char        state[6];     /* Not used */
1536 	 		SQLINTEGER  error;        /* Not used */
1537 			char        errormsg[SQL_MAX_MESSAGE_LENGTH];
1538 			SQLSMALLINT errormsgsize; /* Not used */
1539 
1540 			SQLError( result->conn_ptr->henv, result->conn_ptr->hdbc,
1541 						result->stmt, state, &error, errormsg,
1542 						sizeof(errormsg)-1, &errormsgsize);
1543 			if (!strncmp(state,"S1015",5)) {
1544 				snprintf(cursorname, max_len+1, "php_curs_" ZEND_ULONG_FMT, (zend_ulong)result->stmt);
1545 				if (SQLSetCursorName(result->stmt,cursorname,SQL_NTS) != SQL_SUCCESS) {
1546 					odbc_sql_error(result->conn_ptr, result->stmt, "SQLSetCursorName");
1547 					RETVAL_FALSE;
1548 				} else {
1549 					RETVAL_STRING(cursorname);
1550 				}
1551 			} else {
1552 				php_error_docref(NULL, E_WARNING, "SQL error: %s, SQL state %s", errormsg, state);
1553 				RETVAL_FALSE;
1554 			}
1555 		} else {
1556 			RETVAL_STRING(cursorname);
1557 		}
1558 		efree(cursorname);
1559 	} else {
1560 		RETVAL_FALSE;
1561 	}
1562 }
1563 /* }}} */
1564 
1565 #ifdef HAVE_SQLDATASOURCES
1566 /* {{{ proto array odbc_data_source(resource connection_id, int fetch_type)
1567    Return information about the currently connected data source */
PHP_FUNCTIONnull1568 PHP_FUNCTION(odbc_data_source)
1569 {
1570 	zval *zv_conn;
1571 	zend_long zv_fetch_type;
1572 	RETCODE rc = 0; /* assume all is good */
1573 	odbc_connection *conn;
1574 	UCHAR server_name[100], desc[200];
1575 	SQLSMALLINT len1=0, len2=0, fetch_type;
1576 
1577 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &zv_conn, &zv_fetch_type) == FAILURE) {
1578 		return;
1579 	}
1580 
1581 	fetch_type = (SQLSMALLINT) zv_fetch_type;
1582 
1583 	if (!(fetch_type == SQL_FETCH_FIRST || fetch_type == SQL_FETCH_NEXT)) {
1584 		php_error_docref(NULL, E_WARNING, "Invalid fetch type (%d)", fetch_type);
1585 		RETURN_FALSE;
1586 	}
1587 
1588 	if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(zv_conn), "ODBC-Link", le_conn, le_pconn))) {
1589 		RETURN_FALSE;
1590 	}
1591 
1592 	/* now we have the "connection" lets call the DataSource object */
1593 	rc = SQLDataSources(conn->henv,
1594 			fetch_type,
1595 			server_name,
1596 			(SQLSMALLINT)sizeof(server_name),
1597 			&len1,
1598 			desc,
1599 			(SQLSMALLINT)sizeof(desc),
1600 			&len2);
1601 
1602 	if (SQL_NO_DATA == rc) {
1603 		/* System has no data sources, no error. Signal it by returning NULL,
1604 			not false. */
1605 		RETURN_NULL();
1606 	} else if (rc != SQL_SUCCESS) {
1607 		/* ummm.... he did it */
1608 		odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLDataSources");
1609 		RETURN_FALSE;
1610 	}
1611 
1612 	if (len1 == 0 || len2 == 0) {
1613 		/* we have a non-valid entry... so stop the looping */
1614 		RETURN_FALSE;
1615 	}
1616 
1617 	array_init(return_value);
1618 
1619 	add_assoc_string_ex(return_value, "server", sizeof("server")-1, server_name);
1620 	add_assoc_string_ex(return_value, "description", sizeof("description")-1, desc);
1621 
1622 }
1623 /* }}} */
1624 #endif /* HAVE_SQLDATASOURCES */
1625 
1626 /* {{{ proto resource odbc_exec(resource connection_id, string query [, int flags])
1627    Prepare and execute an SQL statement */
1628 /* XXX Use flags */
PHP_FUNCTIONnull1629 PHP_FUNCTION(odbc_exec)
1630 {
1631 	zval *pv_conn;
1632 	zend_long pv_flags;
1633 	char *query;
1634 	size_t query_len;
1635 	odbc_result *result = NULL;
1636 	odbc_connection *conn;
1637 	RETCODE rc;
1638 #ifdef HAVE_SQL_EXTENDED_FETCH
1639 	SQLUINTEGER      scrollopts;
1640 #endif
1641 
1642 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs|l", &pv_conn, &query, &query_len, &pv_flags) == FAILURE) {
1643 		return;
1644 	}
1645 
1646 	if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
1647 		RETURN_FALSE;
1648 	}
1649 
1650 	result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
1651 
1652 	rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
1653 	if (rc == SQL_INVALID_HANDLE) {
1654 		php_error_docref(NULL, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
1655 		efree(result);
1656 		RETURN_FALSE;
1657 	}
1658 
1659 	if (rc == SQL_ERROR) {
1660 		odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
1661 		efree(result);
1662 		RETURN_FALSE;
1663 	}
1664 
1665 #ifdef HAVE_SQL_EXTENDED_FETCH
1666 	/* Solid doesn't have ExtendedFetch, if DriverManager is used, get Info,
1667 	   whether Driver supports ExtendedFetch */
1668 	rc = SQLGetInfo(conn->hdbc, SQL_FETCH_DIRECTION, (void *) &scrollopts, sizeof(scrollopts), NULL);
1669 	if (rc == SQL_SUCCESS) {
1670 		if ((result->fetch_abs = (scrollopts & SQL_FD_FETCH_ABSOLUTE))) {
1671 			/* Try to set CURSOR_TYPE to dynamic. Driver will replace this with other
1672 			   type if not possible.
1673 			 */
1674 			SQLSetStmtOption(result->stmt, SQL_CURSOR_TYPE, ODBCG(default_cursortype));
1675 		}
1676 	} else {
1677 		result->fetch_abs = 0;
1678 	}
1679 #endif
1680 
1681 	rc = SQLExecDirect(result->stmt, query, SQL_NTS);
1682 	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO && rc != SQL_NO_DATA_FOUND) {
1683 		/* XXX FIXME we should really check out SQLSTATE with SQLError
1684 		 * in case rc is SQL_SUCCESS_WITH_INFO here.
1685 		 */
1686 		odbc_sql_error(conn, result->stmt, "SQLExecDirect");
1687 		SQLFreeStmt(result->stmt, SQL_DROP);
1688 		efree(result);
1689 		RETURN_FALSE;
1690 	}
1691 
1692 	SQLNumResultCols(result->stmt, &(result->numcols));
1693 
1694 	/* For insert, update etc. cols == 0 */
1695 	if (result->numcols > 0) {
1696 		if (!odbc_bindcols(result)) {
1697 			efree(result);
1698 			RETURN_FALSE;
1699 		}
1700 	} else {
1701 		result->values = NULL;
1702 	}
1703 	Z_ADDREF_P(pv_conn);
1704 	result->conn_ptr = conn;
1705 	result->fetched = 0;
1706 	RETURN_RES(zend_register_resource(result, le_result));
1707 }
1708 /* }}} */
1709 
1710 #ifdef PHP_ODBC_HAVE_FETCH_HASH
1711 #define ODBC_NUM  1
1712 #define ODBC_OBJECT  2
1713 
1714 /* {{{ php_odbc_fetch_hash */
php_odbc_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, int result_type)1715 static void php_odbc_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, int result_type)
1716 {
1717 	int i;
1718 	odbc_result *result;
1719 	RETCODE rc;
1720 	SQLSMALLINT sql_c_type;
1721 	char *buf = NULL;
1722 #ifdef HAVE_SQL_EXTENDED_FETCH
1723 	SQLULEN crow;
1724 	SQLUSMALLINT RowStatus[1];
1725 	SQLLEN rownum;
1726 	zval *pv_res, tmp;
1727 	zend_long pv_row = -1;
1728 
1729 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &pv_res, &pv_row) == FAILURE) {
1730 		return;
1731 	}
1732 
1733 	rownum = pv_row;
1734 #else
1735 	zval *pv_res, tmp;
1736 
1737 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pv_res) == FAILURE) {
1738 		return;
1739 	}
1740 #endif
1741 
1742 	if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
1743 		RETURN_FALSE;
1744 	}
1745 
1746 	if (result->numcols == 0) {
1747 		php_error_docref(NULL, E_WARNING, "No tuples available at this result index");
1748 		RETURN_FALSE;
1749 	}
1750 
1751 #ifdef HAVE_SQL_EXTENDED_FETCH
1752 	if (result->fetch_abs) {
1753 		if (rownum > 0) {
1754 			rc = SQLExtendedFetch(result->stmt,SQL_FETCH_ABSOLUTE,rownum,&crow,RowStatus);
1755 		} else {
1756 			rc = SQLExtendedFetch(result->stmt,SQL_FETCH_NEXT,1,&crow,RowStatus);
1757 		}
1758 	} else
1759 #endif
1760 	rc = SQLFetch(result->stmt);
1761 
1762 	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
1763 		RETURN_FALSE;
1764 	}
1765 
1766 	array_init(return_value);
1767 
1768 #ifdef HAVE_SQL_EXTENDED_FETCH
1769 	if (rownum > 0 && result->fetch_abs)
1770 		result->fetched = rownum;
1771 	else
1772 #endif
1773 		result->fetched++;
1774 
1775 	for(i = 0; i < result->numcols; i++) {
1776 		sql_c_type = SQL_C_CHAR;
1777 
1778 		switch(result->values[i].coltype) {
1779 			case SQL_BINARY:
1780 			case SQL_VARBINARY:
1781 			case SQL_LONGVARBINARY:
1782 				if (result->binmode <= 0) {
1783 					ZVAL_EMPTY_STRING(&tmp);
1784 					break;
1785 				}
1786 				if (result->binmode == 1) {
1787 					sql_c_type = SQL_C_BINARY;
1788 				}
1789 			case SQL_LONGVARCHAR:
1790 #if defined(ODBCVER) && (ODBCVER >= 0x0300)
1791 			case SQL_WLONGVARCHAR:
1792 #endif
1793 				if (IS_SQL_LONG(result->values[i].coltype) && result->longreadlen <= 0) {
1794 					ZVAL_EMPTY_STRING(&tmp);
1795 					break;
1796 				}
1797 				if (buf == NULL) {
1798 					buf = emalloc(result->longreadlen + 1);
1799 				}
1800 
1801 				rc = SQLGetData(result->stmt, (SQLUSMALLINT)(i + 1), sql_c_type, buf, result->longreadlen + 1, &result->values[i].vallen);
1802 
1803 				if (rc == SQL_ERROR) {
1804 					odbc_sql_error(result->conn_ptr, result->stmt, "SQLGetData");
1805 					efree(buf);
1806 					RETURN_FALSE;
1807 				}
1808 
1809 				if (rc == SQL_SUCCESS_WITH_INFO) {
1810 					ZVAL_STRINGL(&tmp, buf, result->longreadlen);
1811 				} else if (rc != SQL_SUCCESS) {
1812 					php_error_docref(NULL, E_WARNING, "Cannot get data of column #%d (retcode %u)", i + 1, rc);
1813 					ZVAL_FALSE(&tmp);
1814 				} else if (result->values[i].vallen == SQL_NULL_DATA) {
1815 					ZVAL_NULL(&tmp);
1816 					break;
1817 				} else {
1818 					ZVAL_STRINGL(&tmp, buf, result->values[i].vallen);
1819 				}
1820 				break;
1821 
1822 			default:
1823 				if (result->values[i].vallen == SQL_NULL_DATA) {
1824 					ZVAL_NULL(&tmp);
1825 					break;
1826 				}
1827 				ZVAL_STRINGL(&tmp, result->values[i].value, result->values[i].vallen);
1828 				break;
1829 		}
1830 
1831 		if (result_type & ODBC_NUM) {
1832 			zend_hash_index_update(Z_ARRVAL_P(return_value), i, &tmp);
1833 		} else {
1834 			if (!*(result->values[i].name) && Z_TYPE(tmp) == IS_STRING) {
1835 				zend_hash_update(Z_ARRVAL_P(return_value), Z_STR(tmp), &tmp);
1836 			} else {
1837 				zend_hash_str_update(Z_ARRVAL_P(return_value), result->values[i].name, strlen(result->values[i].name), &tmp);
1838 			}
1839 		}
1840 	}
1841 	if (buf) {
1842 		efree(buf);
1843 	}
1844 }
1845 /* }}} */
1846 
1847 
1848 /* {{{ proto object odbc_fetch_object(resource result [, int rownumber])
1849    Fetch a result row as an object */
PHP_FUNCTIONnull1850 PHP_FUNCTION(odbc_fetch_object)
1851 {
1852 	php_odbc_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, ODBC_OBJECT);
1853 	if (Z_TYPE_P(return_value) == IS_ARRAY) {
1854 		object_and_properties_init(return_value, ZEND_STANDARD_CLASS_DEF_PTR, Z_ARRVAL_P(return_value));
1855 	}
1856 }
1857 /* }}} */
1858 
1859 /* {{{ proto array odbc_fetch_array(resource result [, int rownumber])
1860    Fetch a result row as an associative array */
PHP_FUNCTIONnull1861 PHP_FUNCTION(odbc_fetch_array)
1862 {
1863 	php_odbc_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, ODBC_OBJECT);
1864 }
1865 /* }}} */
1866 #endif
1867 
1868 /* {{{ proto int odbc_fetch_into(resource result_id, array &result_array [, int rownumber])
1869    Fetch one result row into an array */
PHP_FUNCTIONnull1870 PHP_FUNCTION(odbc_fetch_into)
1871 {
1872 	int i;
1873 	odbc_result *result;
1874 	RETCODE rc;
1875 	SQLSMALLINT sql_c_type;
1876 	char *buf = NULL;
1877 	zval *pv_res, *pv_res_arr, tmp;
1878 #ifdef HAVE_SQL_EXTENDED_FETCH
1879 	zend_long pv_row = 0;
1880 	SQLULEN crow;
1881 	SQLUSMALLINT RowStatus[1];
1882 	SQLLEN rownum = -1;
1883 #endif /* HAVE_SQL_EXTENDED_FETCH */
1884 
1885 #ifdef HAVE_SQL_EXTENDED_FETCH
1886 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz|l", &pv_res, &pv_res_arr, &pv_row) == FAILURE) {
1887 		return;
1888 	}
1889 
1890 	rownum = pv_row;
1891 #else
1892 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz", &pv_res, &pv_res_arr) == FAILURE) {
1893 		return;
1894 	}
1895 #endif /* HAVE_SQL_EXTENDED_FETCH */
1896 
1897 	if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
1898 		RETURN_FALSE;
1899 	}
1900 
1901 	if (result->numcols == 0) {
1902 		php_error_docref(NULL, E_WARNING, "No tuples available at this result index");
1903 		RETURN_FALSE;
1904 	}
1905 
1906 	pv_res_arr = zend_try_array_init(pv_res_arr);
1907 	if (!pv_res_arr) {
1908 		return;
1909 	}
1910 
1911 #ifdef HAVE_SQL_EXTENDED_FETCH
1912 	if (result->fetch_abs) {
1913 		if (rownum > 0) {
1914 			rc = SQLExtendedFetch(result->stmt,SQL_FETCH_ABSOLUTE,rownum,&crow,RowStatus);
1915 		} else {
1916 			rc = SQLExtendedFetch(result->stmt,SQL_FETCH_NEXT,1,&crow,RowStatus);
1917 		}
1918 	} else
1919 #endif
1920 		rc = SQLFetch(result->stmt);
1921 
1922 	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
1923 		RETURN_FALSE;
1924 	}
1925 
1926 #ifdef HAVE_SQL_EXTENDED_FETCH
1927 	if (rownum > 0 && result->fetch_abs)
1928 		result->fetched = rownum;
1929 	else
1930 #endif
1931 		result->fetched++;
1932 
1933 	for(i = 0; i < result->numcols; i++) {
1934 		sql_c_type = SQL_C_CHAR;
1935 
1936 		switch(result->values[i].coltype) {
1937 			case SQL_BINARY:
1938 			case SQL_VARBINARY:
1939 			case SQL_LONGVARBINARY:
1940 				if (result->binmode <= 0) {
1941 					ZVAL_EMPTY_STRING(&tmp);
1942 					break;
1943 				}
1944 				if (result->binmode == 1) sql_c_type = SQL_C_BINARY;
1945 
1946 			case SQL_LONGVARCHAR:
1947 #if defined(ODBCVER) && (ODBCVER >= 0x0300)
1948 			case SQL_WLONGVARCHAR:
1949 #endif
1950 				if (IS_SQL_LONG(result->values[i].coltype) && result->longreadlen <= 0) {
1951 					ZVAL_EMPTY_STRING(&tmp);
1952 					break;
1953 				}
1954 
1955 				if (buf == NULL) {
1956 					buf = emalloc(result->longreadlen + 1);
1957 				}
1958 				rc = SQLGetData(result->stmt, (SQLUSMALLINT)(i + 1),sql_c_type, buf, result->longreadlen + 1, &result->values[i].vallen);
1959 
1960 				if (rc == SQL_ERROR) {
1961 					odbc_sql_error(result->conn_ptr, result->stmt, "SQLGetData");
1962 					efree(buf);
1963 					RETURN_FALSE;
1964 				}
1965 				if (rc == SQL_SUCCESS_WITH_INFO) {
1966 					ZVAL_STRINGL(&tmp, buf, result->longreadlen);
1967 				} else if (rc != SQL_SUCCESS) {
1968 					php_error_docref(NULL, E_WARNING, "Cannot get data of column #%d (retcode %u)", i + 1, rc);
1969 					ZVAL_FALSE(&tmp);
1970 				} else if (result->values[i].vallen == SQL_NULL_DATA) {
1971 					ZVAL_NULL(&tmp);
1972 					break;
1973 				} else {
1974 					ZVAL_STRINGL(&tmp, buf, result->values[i].vallen);
1975 				}
1976 				break;
1977 
1978 			default:
1979 				if (result->values[i].vallen == SQL_NULL_DATA) {
1980 					ZVAL_NULL(&tmp);
1981 					break;
1982 				}
1983 				ZVAL_STRINGL(&tmp, result->values[i].value, result->values[i].vallen);
1984 				break;
1985 		}
1986 		zend_hash_index_update(Z_ARRVAL_P(pv_res_arr), i, &tmp);
1987 	}
1988 	if (buf) efree(buf);
1989 	RETURN_LONG(result->numcols);
1990 }
1991 /* }}} */
1992 
1993 /* {{{ proto bool solid_fetch_prev(resource result_id)
1994    */
1995 #if defined(HAVE_SOLID) || defined(HAVE_SOLID_30) || defined(HAVE_SOLID_35)
PHP_FUNCTIONnull1996 PHP_FUNCTION(solid_fetch_prev)
1997 {
1998 	odbc_result *result;
1999 	RETCODE rc;
2000 	zval *pv_res;
2001 
2002 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pv_res) == FAILURE) {
2003 		return;
2004 	}
2005 
2006 	if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
2007 		RETURN_FALSE;
2008 	}
2009 	if (result->numcols == 0) {
2010 		php_error_docref(NULL, E_WARNING, "No tuples available at this result index");
2011 		RETURN_FALSE;
2012 	}
2013 	rc = SQLFetchPrev(result->stmt);
2014 
2015 	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
2016 		RETURN_FALSE;
2017 	}
2018 
2019 	if (result->fetched > 1) {
2020 		result->fetched--;
2021 	}
2022 
2023 	RETURN_TRUE;
2024 }
2025 #endif
2026 /* }}} */
2027 
2028 /* {{{ proto bool odbc_fetch_row(resource result_id [, int row_number])
2029    Fetch a row */
PHP_FUNCTIONnull2030 PHP_FUNCTION(odbc_fetch_row)
2031 {
2032 	SQLLEN rownum;
2033 	odbc_result *result;
2034 	RETCODE rc;
2035 	zval *pv_res;
2036 	zend_long pv_row = 1;
2037 #ifdef HAVE_SQL_EXTENDED_FETCH
2038 	SQLULEN crow;
2039 	SQLUSMALLINT RowStatus[1];
2040 #endif
2041 
2042 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &pv_res, &pv_row) == FAILURE) {
2043 		return;
2044 	}
2045 
2046 	rownum = pv_row;
2047 
2048 	if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
2049 		RETURN_FALSE;
2050 	}
2051 
2052 	if (result->numcols == 0) {
2053 		php_error_docref(NULL, E_WARNING, "No tuples available at this result index");
2054 		RETURN_FALSE;
2055 	}
2056 
2057 #ifdef HAVE_SQL_EXTENDED_FETCH
2058 	if (result->fetch_abs) {
2059 		if (ZEND_NUM_ARGS() > 1) {
2060 			rc = SQLExtendedFetch(result->stmt,SQL_FETCH_ABSOLUTE,rownum,&crow,RowStatus);
2061 		} else {
2062 			rc = SQLExtendedFetch(result->stmt,SQL_FETCH_NEXT,1,&crow,RowStatus);
2063 		}
2064 	} else
2065 #endif
2066 		rc = SQLFetch(result->stmt);
2067 
2068 	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
2069 		RETURN_FALSE;
2070 	}
2071 
2072 	if (ZEND_NUM_ARGS() > 1) {
2073 		result->fetched = rownum;
2074 	} else {
2075 		result->fetched++;
2076 	}
2077 
2078 	RETURN_TRUE;
2079 }
2080 /* }}} */
2081 
2082 /* {{{ proto mixed odbc_result(resource result_id, mixed field)
2083    Get result data */
PHP_FUNCTIONnull2084 PHP_FUNCTION(odbc_result)
2085 {
2086 	char *field;
2087 	zend_string *field_str;
2088 	int field_ind;
2089 	SQLSMALLINT sql_c_type = SQL_C_CHAR;
2090 	odbc_result *result;
2091 	int i = 0;
2092 	RETCODE rc;
2093 	SQLLEN	fieldsize;
2094 	zval *pv_res, *pv_field;
2095 #ifdef HAVE_SQL_EXTENDED_FETCH
2096 	SQLULEN crow;
2097 	SQLUSMALLINT RowStatus[1];
2098 #endif
2099 
2100 	field_ind = -1;
2101 	field = NULL;
2102 
2103 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz", &pv_res, &pv_field) == FAILURE) {
2104 		return;
2105 	}
2106 
2107 	if (Z_TYPE_P(pv_field) == IS_STRING) {
2108 		field = Z_STRVAL_P(pv_field);
2109 	} else {
2110 		convert_to_long_ex(pv_field);
2111 		field_ind = Z_LVAL_P(pv_field) - 1;
2112 	}
2113 
2114 	if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
2115 		RETURN_FALSE;
2116 	}
2117 
2118 	if ((result->numcols == 0)) {
2119 		php_error_docref(NULL, E_WARNING, "No tuples available at this result index");
2120 		RETURN_FALSE;
2121 	}
2122 
2123 	/* get field index if the field parameter was a string */
2124 	if (field != NULL) {
2125 		if (result->values == NULL) {
2126 			php_error_docref(NULL, E_WARNING, "Result set contains no data");
2127 			RETURN_FALSE;
2128 		}
2129 
2130 		for(i = 0; i < result->numcols; i++) {
2131 			if (!strcasecmp(result->values[i].name, field)) {
2132 				field_ind = i;
2133 				break;
2134 			}
2135 		}
2136 
2137 		if (field_ind < 0) {
2138 			php_error_docref(NULL, E_WARNING, "Field %s not found", field);
2139 			RETURN_FALSE;
2140 		}
2141 	} else {
2142 		/* check for limits of field_ind if the field parameter was an int */
2143 		if (field_ind >= result->numcols || field_ind < 0) {
2144 			php_error_docref(NULL, E_WARNING, "Field index is larger than the number of fields");
2145 			RETURN_FALSE;
2146 		}
2147 	}
2148 
2149 	if (result->fetched == 0) {
2150 		/* User forgot to call odbc_fetch_row(), or wants to reload the results, do it now */
2151 #ifdef HAVE_SQL_EXTENDED_FETCH
2152 		if (result->fetch_abs)
2153 			rc = SQLExtendedFetch(result->stmt, SQL_FETCH_NEXT, 1, &crow,RowStatus);
2154 		else
2155 #endif
2156 			rc = SQLFetch(result->stmt);
2157 
2158 		if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
2159 			RETURN_FALSE;
2160 		}
2161 
2162 		result->fetched++;
2163 	}
2164 
2165 	switch(result->values[field_ind].coltype) {
2166 		case SQL_BINARY:
2167 		case SQL_VARBINARY:
2168 		case SQL_LONGVARBINARY:
2169 			if (result->binmode <= 1) {
2170 				sql_c_type = SQL_C_BINARY;
2171 			}
2172 			if (result->binmode <= 0) {
2173 				break;
2174 			}
2175 		case SQL_LONGVARCHAR:
2176 #if defined(ODBCVER) && (ODBCVER >= 0x0300)
2177 		case SQL_WLONGVARCHAR:
2178 #endif
2179 			if (IS_SQL_LONG(result->values[field_ind].coltype)) {
2180 				if (result->longreadlen <= 0) {
2181 				   break;
2182 				} else {
2183 				   fieldsize = result->longreadlen;
2184 				}
2185 			} else {
2186 			   PHP_ODBC_SQLCOLATTRIBUTE(result->stmt, (SQLUSMALLINT)(field_ind + 1),
2187 					   			(SQLUSMALLINT)((sql_c_type == SQL_C_BINARY) ? SQL_COLUMN_LENGTH :
2188 					   			SQL_COLUMN_DISPLAY_SIZE),
2189 					   			NULL, 0, NULL, &fieldsize);
2190 			}
2191 			/* For char data, the length of the returned string will be longreadlen - 1 */
2192 			fieldsize = (result->longreadlen <= 0) ? 4096 : result->longreadlen;
2193 			field_str = zend_string_alloc(fieldsize, 0);
2194 
2195 		/* SQLGetData will truncate CHAR data to fieldsize - 1 bytes and append \0.
2196 		 * For binary data it is truncated to fieldsize bytes.
2197 		 */
2198 			rc = SQLGetData(result->stmt, (SQLUSMALLINT)(field_ind + 1), sql_c_type,
2199 							ZSTR_VAL(field_str), fieldsize, &result->values[field_ind].vallen);
2200 
2201 			if (rc == SQL_ERROR) {
2202 				odbc_sql_error(result->conn_ptr, result->stmt, "SQLGetData");
2203 				zend_string_efree(field_str);
2204 				RETURN_FALSE;
2205 			}
2206 
2207 			if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
2208 				zend_string_efree(field_str);
2209 				php_error_docref(NULL, E_WARNING, "Cannot get data of column #%d (retcode %u)", field_ind + 1, rc);
2210 				RETURN_FALSE;
2211 			} else if (result->values[field_ind].vallen == SQL_NULL_DATA) {
2212 				zend_string_efree(field_str);
2213 				RETURN_NULL();
2214 			}
2215 			/* Reduce fieldlen by 1 if we have char data. One day we might
2216 			   have binary strings... */
2217 			if ((result->values[field_ind].coltype == SQL_LONGVARCHAR)
2218 #if defined(ODBCVER) && (ODBCVER >= 0x0300)
2219 			    || (result->values[field_ind].coltype == SQL_WLONGVARCHAR)
2220 #endif
2221 			) {
2222 				fieldsize -= 1;
2223 			}
2224 			/* Don't duplicate result, saves one emalloc.
2225 			   For SQL_SUCCESS, the length is in vallen.
2226 			 */
2227 			if (rc != SQL_SUCCESS_WITH_INFO) {
2228 				field_str = zend_string_truncate(field_str, result->values[field_ind].vallen, 0);
2229 			}
2230 			ZSTR_VAL(field_str)[ZSTR_LEN(field_str)] = '\0';
2231 			RETURN_NEW_STR(field_str);
2232 			break;
2233 
2234 		default:
2235 			if (result->values[field_ind].vallen == SQL_NULL_DATA) {
2236 				RETURN_NULL();
2237 			} else {
2238 				RETURN_STRINGL(result->values[field_ind].value, result->values[field_ind].vallen);
2239 			}
2240 			break;
2241 	}
2242 
2243 /* If we come here, output unbound LONG and/or BINARY column data to the client */
2244 
2245 	/* We emalloc 1 byte more for SQL_C_CHAR (trailing \0) */
2246 	fieldsize = (sql_c_type == SQL_C_CHAR) ? 4096 : 4095;
2247 	field = emalloc(fieldsize);
2248 
2249 	/* Call SQLGetData() until SQL_SUCCESS is returned */
2250 	while(1) {
2251 		rc = SQLGetData(result->stmt, (SQLUSMALLINT)(field_ind + 1),sql_c_type, field, fieldsize, &result->values[field_ind].vallen);
2252 
2253 		if (rc == SQL_ERROR) {
2254 			odbc_sql_error(result->conn_ptr, result->stmt, "SQLGetData");
2255 			efree(field);
2256 			RETURN_FALSE;
2257 		}
2258 
2259 		if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
2260 			php_error_docref(NULL, E_WARNING, "Cannot get data of column #%d (retcode %u)", field_ind + 1, rc);
2261 			efree(field);
2262 			RETURN_FALSE;
2263 		}
2264 
2265 		if (result->values[field_ind].vallen == SQL_NULL_DATA) {
2266 			efree(field);
2267 			RETURN_NULL();
2268 		}
2269 		/* chop the trailing \0 by outputting only 4095 bytes */
2270 		PHPWRITE(field,(rc == SQL_SUCCESS_WITH_INFO) ? 4095 : result->values[field_ind].vallen);
2271 
2272 		if (rc == SQL_SUCCESS) { /* no more data avail */
2273 			efree(field);
2274 			RETURN_TRUE;
2275 		}
2276 	}
2277 	RETURN_TRUE;
2278 }
2279 /* }}} */
2280 
2281 /* {{{ proto int odbc_result_all(resource result_id [, string format])
2282    Print result as HTML table */
PHP_FUNCTIONnull2283 PHP_FUNCTION(odbc_result_all)
2284 {
2285 	char *buf = NULL;
2286 	odbc_result *result;
2287 	RETCODE rc;
2288 	zval *pv_res;
2289 	char *pv_format = NULL;
2290 	size_t i, pv_format_len = 0;
2291 	SQLSMALLINT sql_c_type;
2292 #ifdef HAVE_SQL_EXTENDED_FETCH
2293 	SQLULEN crow;
2294 	SQLUSMALLINT RowStatus[1];
2295 #endif
2296 
2297 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|s", &pv_res, &pv_format, &pv_format_len) == FAILURE) {
2298 		return;
2299 	}
2300 
2301 	if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
2302 		RETURN_FALSE;
2303 	}
2304 
2305 	if (result->numcols == 0) {
2306 		php_error_docref(NULL, E_WARNING, "No tuples available at this result index");
2307 		RETURN_FALSE;
2308 	}
2309 #ifdef HAVE_SQL_EXTENDED_FETCH
2310 	if (result->fetch_abs)
2311 		rc = SQLExtendedFetch(result->stmt,SQL_FETCH_NEXT,1,&crow,RowStatus);
2312 	else
2313 #endif
2314 		rc = SQLFetch(result->stmt);
2315 
2316 	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
2317 		php_printf("<h2>No rows found</h2>\n");
2318 		RETURN_LONG(0);
2319 	}
2320 
2321 	/* Start table tag */
2322 	if (ZEND_NUM_ARGS() == 1) {
2323 		php_printf("<table><tr>");
2324 	} else {
2325 		php_printf("<table %s ><tr>", pv_format);
2326 	}
2327 
2328 	for (i = 0; i < result->numcols; i++) {
2329 		php_printf("<th>%s</th>", result->values[i].name);
2330 	}
2331 
2332 	php_printf("</tr>\n");
2333 
2334 	while(rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO) {
2335 		result->fetched++;
2336 		php_printf("<tr>");
2337 		for(i = 0; i < result->numcols; i++) {
2338 			sql_c_type = SQL_C_CHAR;
2339 			switch(result->values[i].coltype) {
2340 				case SQL_BINARY:
2341 				case SQL_VARBINARY:
2342 				case SQL_LONGVARBINARY:
2343 					if (result->binmode <= 0) {
2344 						php_printf("<td>Not printable</td>");
2345 						break;
2346 					}
2347 					if (result->binmode <= 1) sql_c_type = SQL_C_BINARY;
2348 				case SQL_LONGVARCHAR:
2349 #if defined(ODBCVER) && (ODBCVER >= 0x0300)
2350 				case SQL_WLONGVARCHAR:
2351 #endif
2352 					if (IS_SQL_LONG(result->values[i].coltype) &&
2353 						result->longreadlen <= 0) {
2354 						php_printf("<td>Not printable</td>");
2355 						break;
2356 					}
2357 
2358 					if (buf == NULL) {
2359 						buf = emalloc(result->longreadlen);
2360 					}
2361 
2362 					rc = SQLGetData(result->stmt, (SQLUSMALLINT)(i + 1),sql_c_type, buf, result->longreadlen, &result->values[i].vallen);
2363 
2364 					php_printf("<td>");
2365 
2366 					if (rc == SQL_ERROR) {
2367 						odbc_sql_error(result->conn_ptr, result->stmt, "SQLGetData");
2368 						php_printf("</td></tr></table>");
2369 						efree(buf);
2370 						RETURN_FALSE;
2371 					}
2372 					if (rc == SQL_SUCCESS_WITH_INFO) {
2373 						PHPWRITE(buf, result->longreadlen);
2374 					} else if (rc != SQL_SUCCESS) {
2375 						php_printf("</td></tr></table>");
2376 						php_error_docref(NULL, E_WARNING, "Cannot get data of column #%zu (retcode %u)", i + 1, rc);
2377 						efree(buf);
2378 						RETURN_FALSE;
2379 					} else if (result->values[i].vallen == SQL_NULL_DATA) {
2380 						php_printf("<td>NULL</td>");
2381 						break;
2382 					} else {
2383 						PHPWRITE(buf, result->values[i].vallen);
2384 					}
2385 					php_printf("</td>");
2386 					break;
2387 				default:
2388 					if (result->values[i].vallen == SQL_NULL_DATA) {
2389 						php_printf("<td>NULL</td>");
2390 					} else {
2391 						php_printf("<td>%s</td>", result->values[i].value);
2392 					}
2393 					break;
2394 			}
2395 		}
2396    		php_printf("</tr>\n");
2397 
2398 #ifdef HAVE_SQL_EXTENDED_FETCH
2399 		if (result->fetch_abs)
2400 			rc = SQLExtendedFetch(result->stmt,SQL_FETCH_NEXT,1,&crow,RowStatus);
2401 		else
2402 #endif
2403 			rc = SQLFetch(result->stmt);
2404 	}
2405 	php_printf("</table>\n");
2406 	if (buf) efree(buf);
2407 	RETURN_LONG(result->fetched);
2408 }
2409 /* }}} */
2410 
2411 /* {{{ proto bool odbc_free_result(resource result_id)
2412    Free resources associated with a result */
PHP_FUNCTIONnull2413 PHP_FUNCTION(odbc_free_result)
2414 {
2415 	zval *pv_res;
2416 	odbc_result *result;
2417 	int i;
2418 
2419 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pv_res) == FAILURE) {
2420 		return;
2421 	}
2422 
2423 	if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
2424 		RETURN_FALSE;
2425 	}
2426 
2427 	if (result->values) {
2428 		for (i = 0; i < result->numcols; i++) {
2429 			if (result->values[i].value) {
2430 				efree(result->values[i].value);
2431 			}
2432 		}
2433 		efree(result->values);
2434 		result->values = NULL;
2435 	}
2436 
2437 	zend_list_close(Z_RES_P(pv_res));
2438 
2439 	RETURN_TRUE;
2440 }
2441 /* }}} */
2442 
2443 /* {{{ proto resource odbc_connect(string DSN, string user, string password [, int cursor_option])
2444    Connect to a datasource */
PHP_FUNCTIONnull2445 PHP_FUNCTION(odbc_connect)
2446 {
2447 	odbc_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
2448 }
2449 /* }}} */
2450 
2451 /* {{{ proto resource odbc_pconnect(string DSN, string user, string password [, int cursor_option])
2452    Establish a persistent connection to a datasource */
PHP_FUNCTIONnull2453 PHP_FUNCTION(odbc_pconnect)
2454 {
2455 	odbc_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
2456 }
2457 /* }}} */
2458 
2459 /* {{{ odbc_sqlconnect */
odbc_sqlconnect(odbc_connection **conn, char *db, char *uid, char *pwd, int cur_opt, int persistent)2460 int odbc_sqlconnect(odbc_connection **conn, char *db, char *uid, char *pwd, int cur_opt, int persistent)
2461 {
2462 	RETCODE rc;
2463 
2464 	*conn = (odbc_connection *)pemalloc(sizeof(odbc_connection), persistent);
2465 	memset(*conn, 0, sizeof(odbc_connection));
2466 	(*conn)->persistent = persistent;
2467 	SQLAllocEnv(&((*conn)->henv));
2468 	SQLAllocConnect((*conn)->henv, &((*conn)->hdbc));
2469 
2470 #if defined(HAVE_SOLID) || defined(HAVE_SOLID_30)
2471 	SQLSetConnectOption((*conn)->hdbc, SQL_TRANSLATE_OPTION,
2472 			SQL_SOLID_XLATOPT_NOCNV);
2473 #endif
2474 #ifdef HAVE_OPENLINK
2475 	{
2476 		char dsnbuf[1024];
2477 		short dsnbuflen;
2478 
2479 		rc = SQLDriverConnect((*conn)->hdbc, NULL, db, SQL_NTS,	dsnbuf, sizeof(dsnbuf) - 1, &dsnbuflen, SQL_DRIVER_NOPROMPT);
2480 	}
2481 #else
2482 	if (cur_opt != SQL_CUR_DEFAULT) {
2483 		rc = SQLSetConnectOption((*conn)->hdbc, SQL_ODBC_CURSORS, cur_opt);
2484 		if (rc != SQL_SUCCESS) {  /* && rc != SQL_SUCCESS_WITH_INFO ? */
2485 			odbc_sql_error(*conn, SQL_NULL_HSTMT, "SQLSetConnectOption");
2486 			SQLFreeConnect((*conn)->hdbc);
2487 			pefree(*conn, persistent);
2488 			return FALSE;
2489 		}
2490 	}
2491 /*  Possible fix for bug #10250
2492  *  Needs testing on UnixODBC < 2.0.5 though. */
2493 #if defined(HAVE_EMPRESS) || defined(HAVE_UNIXODBC) || defined(PHP_WIN32) || defined (HAVE_IODBC)
2494 /* *  Uncomment the line above, and comment line below to fully test
2495  * #ifdef HAVE_EMPRESS */
2496 	{
2497 		int     direct = 0;
2498 		char    dsnbuf[1024];
2499 		short   dsnbuflen;
2500 		char    *ldb = 0;
2501 		int		ldb_len = 0;
2502 
2503 		if (strstr((char*)db, ";")) {
2504 			direct = 1;
2505 			if (uid && !strstr ((char*)db, "uid") && !strstr((char*)db, "UID")) {
2506 				spprintf(&ldb, 0, "%s;UID=%s;PWD=%s", db, uid, pwd);
2507 			} else {
2508 				ldb_len = strlen(db)+1;
2509 				ldb = (char*) emalloc(ldb_len);
2510 				memcpy(ldb, db, ldb_len);
2511 			}
2512 		}
2513 
2514 		if (direct) {
2515 			rc = SQLDriverConnect((*conn)->hdbc, NULL, ldb, strlen(ldb), dsnbuf, sizeof(dsnbuf) - 1, &dsnbuflen, SQL_DRIVER_NOPROMPT);
2516 		} else {
2517 			rc = SQLConnect((*conn)->hdbc, db, SQL_NTS, uid, SQL_NTS, pwd, SQL_NTS);
2518 		}
2519 
2520 		if (ldb) {
2521 			efree(ldb);
2522 		}
2523 	}
2524 #else
2525 	rc = SQLConnect((*conn)->hdbc, db, SQL_NTS, uid, SQL_NTS, pwd, SQL_NTS);
2526 #endif
2527 #endif
2528 	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
2529 		odbc_sql_error(*conn, SQL_NULL_HSTMT, "SQLConnect");
2530 		SQLFreeConnect((*conn)->hdbc);
2531 		pefree((*conn), persistent);
2532 		return FALSE;
2533 	}
2534 /*	(*conn)->open = 1;*/
2535 	return TRUE;
2536 }
2537 /* }}} */
2538 
2539 /* Persistent connections: two list-types le_pconn, le_conn and a plist
2540  * where hashed connection info is stored together with index pointer to
2541  * the actual link of type le_pconn in the list. Only persistent
2542  * connections get hashed up. Normal connections use existing pconnections.
2543  * Maybe this has to change with regard to transactions on pconnections?
2544  * Possibly set autocommit to on on request shutdown.
2545  *
2546  * We do have to hash non-persistent connections, and reuse connections.
2547  * In the case where two connects were being made, without closing the first
2548  * connect, access violations were occurring.  This is because some of the
2549  * "globals" in this module should actually be per-connection variables.  I
2550  * simply fixed things to get them working for now.  Shane
2551  */
2552 /* {{{ odbc_do_connect */
odbc_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)2553 void odbc_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
2554 {
2555 	char *db, *uid, *pwd;
2556 	size_t db_len, uid_len, pwd_len;
2557 	zend_long pv_opt = SQL_CUR_DEFAULT;
2558 	odbc_connection *db_conn;
2559 	char *hashed_details;
2560 	int hashed_len, cur_opt;
2561 
2562 	/*  Now an optional 4th parameter specifying the cursor type
2563 	 *  defaulting to the cursors default
2564 	 */
2565 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "sss|l", &db, &db_len, &uid, &uid_len, &pwd, &pwd_len, &pv_opt) == FAILURE) {
2566 		return;
2567 	}
2568 
2569 	cur_opt = pv_opt;
2570 
2571 	if (ZEND_NUM_ARGS() > 3) {
2572 		/* Confirm the cur_opt range */
2573 		if (! (cur_opt == SQL_CUR_USE_IF_NEEDED ||
2574 			cur_opt == SQL_CUR_USE_ODBC ||
2575 			cur_opt == SQL_CUR_USE_DRIVER ||
2576 			cur_opt == SQL_CUR_DEFAULT) ) {
2577 			php_error_docref(NULL, E_WARNING, "Invalid Cursor type (%d)", cur_opt);
2578 			RETURN_FALSE;
2579 		}
2580 	}
2581 
2582 	if (ODBCG(allow_persistent) <= 0) {
2583 		persistent = 0;
2584 	}
2585 
2586 	hashed_len = spprintf(&hashed_details, 0, "%s_%s_%s_%s_%d", ODBC_TYPE, db, uid, pwd, cur_opt);
2587 
2588 	/* FIXME the idea of checking to see if our connection is already persistent
2589 		is good, but it adds a lot of overhead to non-persistent connections.  We
2590 		should look and see if we can fix that somehow */
2591 	/* try to find if we already have this link in our persistent list,
2592 	 * no matter if it is to be persistent or not
2593 	 */
2594 
2595 try_and_get_another_connection:
2596 
2597 	if (persistent) {
2598 		zend_resource *le;
2599 
2600 		/* the link is not in the persistent list */
2601 		if ((le = zend_hash_str_find_ptr(&EG(persistent_list), hashed_details, hashed_len)) == NULL) {
2602 			if (ODBCG(max_links) != -1 && ODBCG(num_links) >= ODBCG(max_links)) {
2603 				php_error_docref(NULL, E_WARNING, "Too many open links (%ld)", ODBCG(num_links));
2604 				efree(hashed_details);
2605 				RETURN_FALSE;
2606 			}
2607 			if (ODBCG(max_persistent) != -1 && ODBCG(num_persistent) >= ODBCG(max_persistent)) {
2608 				php_error_docref(NULL, E_WARNING,"Too many open persistent links (%ld)", ODBCG(num_persistent));
2609 				efree(hashed_details);
2610 				RETURN_FALSE;
2611 			}
2612 
2613 			if (!odbc_sqlconnect(&db_conn, db, uid, pwd, cur_opt, 1)) {
2614 				efree(hashed_details);
2615 				RETURN_FALSE;
2616 			}
2617 
2618 			if (zend_register_persistent_resource(hashed_details, hashed_len, db_conn, le_pconn) == NULL) {
2619 				free(db_conn);
2620 				efree(hashed_details);
2621 				RETURN_FALSE;
2622 			}
2623 			ODBCG(num_persistent)++;
2624 			ODBCG(num_links)++;
2625 			db_conn->res = zend_register_resource(db_conn, le_pconn);
2626 			RETVAL_RES(db_conn->res);
2627 		} else { /* found connection */
2628 			if (le->type != le_pconn) {
2629 				RETURN_FALSE;
2630 			}
2631 			/*
2632 			 * check to see if the connection is still valid
2633 			 */
2634 			db_conn = (odbc_connection *)le->ptr;
2635 
2636 			/*
2637 			 * check to see if the connection is still in place (lurcher)
2638 			 */
2639 			if(ODBCG(check_persistent)){
2640 				RETCODE ret;
2641 				UCHAR d_name[32];
2642 				SQLSMALLINT len;
2643 
2644 				ret = SQLGetInfo(db_conn->hdbc,
2645 					SQL_DATA_SOURCE_READ_ONLY,
2646 					d_name, sizeof(d_name), &len);
2647 
2648 				if(ret != SQL_SUCCESS || len == 0) {
2649 					zend_hash_str_del(&EG(persistent_list), hashed_details, hashed_len);
2650 					/* Commented out to fix a possible double closure error
2651 					 * when working with persistent connections as submitted by
2652 					 * bug #15758
2653 					 *
2654 					 * safe_odbc_disconnect(db_conn->hdbc);
2655 					 * SQLFreeConnect(db_conn->hdbc);
2656 					 */
2657 					goto try_and_get_another_connection;
2658 				}
2659 			}
2660 		}
2661 		db_conn->res = zend_register_resource(db_conn, le_pconn);
2662 		RETVAL_RES(db_conn->res);
2663 	} else { /* non persistent */
2664 		zend_resource *index_ptr, new_index_ptr;
2665 
2666 		if ((index_ptr = zend_hash_str_find_ptr(&EG(regular_list), hashed_details, hashed_len)) != NULL) {
2667 			zend_ulong conn_id;
2668 			zend_resource *p;
2669 
2670 			if (index_ptr->type != le_index_ptr) {
2671 				RETURN_FALSE;
2672 			}
2673 			conn_id = (zend_ulong)index_ptr->ptr;
2674 			p = zend_hash_index_find_ptr(&EG(regular_list), conn_id);   /* check if the connection is still there */
2675 
2676 			if (p && p->ptr && (p->type == le_conn || p->type == le_pconn)) {
2677 				GC_ADDREF(p);
2678 				RETVAL_RES(p);
2679 				efree(hashed_details);
2680 				return;
2681 			} else {
2682 				zend_hash_str_del(&EG(regular_list), hashed_details, hashed_len);
2683 			}
2684 		}
2685 		if (ODBCG(max_links) != -1 && ODBCG(num_links) >= ODBCG(max_links)) {
2686 			php_error_docref(NULL, E_WARNING,"Too many open connections (%ld)",ODBCG(num_links));
2687 			efree(hashed_details);
2688 			RETURN_FALSE;
2689 		}
2690 
2691 		if (!odbc_sqlconnect(&db_conn, db, uid, pwd, cur_opt, 0)) {
2692 			efree(hashed_details);
2693 			RETURN_FALSE;
2694 		}
2695 		db_conn->res = zend_register_resource(db_conn, le_conn);
2696 		RETVAL_RES(db_conn->res);
2697 		new_index_ptr.ptr = (void *)(zend_uintptr_t)Z_RES_HANDLE_P(return_value);
2698 		new_index_ptr.type = le_index_ptr;
2699 
2700 		zend_hash_str_update_mem(&EG(regular_list), hashed_details, hashed_len, (void *) &new_index_ptr,
2701 				   sizeof(zend_resource));
2702 		ODBCG(num_links)++;
2703 	}
2704 	efree(hashed_details);
2705 }
2706 /* }}} */
2707 
2708 /* {{{ proto void odbc_close(resource connection_id)
2709    Close an ODBC connection */
PHP_FUNCTIONnull2710 PHP_FUNCTION(odbc_close)
2711 {
2712 	zval *pv_conn;
2713 	zend_resource *p;
2714 	odbc_connection *conn;
2715 	odbc_result *res;
2716 	int is_pconn = 0;
2717 
2718 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pv_conn) == FAILURE) {
2719 		return;
2720 	}
2721 
2722 	if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
2723 		RETURN_FALSE;
2724 	}
2725 
2726 	if (Z_RES_P(pv_conn)->type == le_pconn) {
2727 		is_pconn = 1;
2728 	}
2729 
2730 	ZEND_HASH_FOREACH_PTR(&EG(regular_list), p) {
2731 		if (p->ptr && (p->type == le_result)) {
2732 			res = (odbc_result *)p->ptr;
2733 			if (res->conn_ptr == conn) {
2734 				zend_list_close(p);
2735 			}
2736 		}
2737 	} ZEND_HASH_FOREACH_END();
2738 
2739 	zend_list_close(Z_RES_P(pv_conn));
2740 
2741 	if(is_pconn){
2742 		zend_hash_apply_with_argument(&EG(persistent_list),	(apply_func_arg_t) _close_pconn_with_res, (void *) Z_RES_P(pv_conn));
2743 	}
2744 }
2745 /* }}} */
2746 
2747 /* {{{ proto int odbc_num_rows(resource result_id)
2748    Get number of rows in a result */
PHP_FUNCTIONnull2749 PHP_FUNCTION(odbc_num_rows)
2750 {
2751 	odbc_result *result;
2752 	SQLLEN rows;
2753 	zval *pv_res;
2754 
2755 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pv_res) == FAILURE) {
2756 		return;
2757 	}
2758 
2759 	if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
2760 		RETURN_FALSE;
2761 	}
2762 
2763 	SQLRowCount(result->stmt, &rows);
2764 	RETURN_LONG(rows);
2765 }
2766 /* }}} */
2767 
2768 #if !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30)
2769 /* {{{ proto bool odbc_next_result(resource result_id)
2770    Checks if multiple results are available */
PHP_FUNCTIONnull2771 PHP_FUNCTION(odbc_next_result)
2772 {
2773 	odbc_result *result;
2774 	zval *pv_res;
2775 	int rc, i;
2776 
2777 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pv_res) == FAILURE) {
2778 		return;
2779 	}
2780 
2781 	if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
2782 		RETURN_FALSE;
2783 	}
2784 
2785 	if (result->values) {
2786 		for(i = 0; i < result->numcols; i++) {
2787 			if (result->values[i].value) {
2788 				efree(result->values[i].value);
2789 			}
2790 		}
2791 		efree(result->values);
2792 		result->values = NULL;
2793 		result->numcols = 0;
2794 	}
2795 
2796 	result->fetched = 0;
2797 	rc = SQLMoreResults(result->stmt);
2798 	if (rc == SQL_SUCCESS_WITH_INFO || rc == SQL_SUCCESS) {
2799 		rc = SQLFreeStmt(result->stmt, SQL_UNBIND);
2800 		SQLNumParams(result->stmt, &(result->numparams));
2801 		SQLNumResultCols(result->stmt, &(result->numcols));
2802 
2803 		if (result->numcols > 0) {
2804 			if (!odbc_bindcols(result)) {
2805 				efree(result);
2806 				RETVAL_FALSE;
2807 			}
2808 		} else {
2809 			result->values = NULL;
2810 		}
2811 		RETURN_TRUE;
2812 	} else if (rc == SQL_NO_DATA_FOUND) {
2813 		RETURN_FALSE;
2814 	} else {
2815 		odbc_sql_error(result->conn_ptr, result->stmt, "SQLMoreResults");
2816 		RETURN_FALSE;
2817 	}
2818 }
2819 /* }}} */
2820 #endif
2821 
2822 /* {{{ proto int odbc_num_fields(resource result_id)
2823    Get number of columns in a result */
PHP_FUNCTIONnull2824 PHP_FUNCTION(odbc_num_fields)
2825 {
2826 	odbc_result *result;
2827 	zval *pv_res;
2828 
2829 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pv_res) == FAILURE) {
2830 		return;
2831 	}
2832 
2833 	if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
2834 		RETURN_FALSE;
2835 	}
2836 
2837 	RETURN_LONG(result->numcols);
2838 }
2839 /* }}} */
2840 
2841 /* {{{ proto string odbc_field_name(resource result_id, int field_number)
2842    Get a column name */
PHP_FUNCTIONnull2843 PHP_FUNCTION(odbc_field_name)
2844 {
2845 	odbc_result *result;
2846 	zval *pv_res;
2847 	zend_long pv_num;
2848 
2849 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &pv_res, &pv_num) == FAILURE) {
2850 		return;
2851 	}
2852 
2853 	if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
2854 		RETURN_FALSE;
2855 	}
2856 
2857 	if (result->numcols == 0) {
2858 		php_error_docref(NULL, E_WARNING, "No tuples available at this result index");
2859 		RETURN_FALSE;
2860 	}
2861 
2862 	if (pv_num > result->numcols) {
2863 		php_error_docref(NULL, E_WARNING, "Field index larger than number of fields");
2864 		RETURN_FALSE;
2865 	}
2866 
2867 	if (pv_num < 1) {
2868 		php_error_docref(NULL, E_WARNING, "Field numbering starts at 1");
2869 		RETURN_FALSE;
2870 	}
2871 
2872 	RETURN_STRING(result->values[pv_num - 1].name);
2873 }
2874 /* }}} */
2875 
2876 /* {{{ proto string odbc_field_type(resource result_id, int field_number)
2877    Get the datatype of a column */
PHP_FUNCTIONnull2878 PHP_FUNCTION(odbc_field_type)
2879 {
2880 	odbc_result	*result;
2881 	char    	tmp[32];
2882 	SQLSMALLINT	tmplen;
2883 	zval		*pv_res;
2884 	zend_long		pv_num;
2885 
2886 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &pv_res, &pv_num) == FAILURE) {
2887 		return;
2888 	}
2889 
2890 	if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
2891 		RETURN_FALSE;
2892 	}
2893 
2894 	if (result->numcols == 0) {
2895 		php_error_docref(NULL, E_WARNING, "No tuples available at this result index");
2896 		RETURN_FALSE;
2897 	}
2898 
2899 	if (pv_num > result->numcols) {
2900 		php_error_docref(NULL, E_WARNING, "Field index larger than number of fields");
2901 		RETURN_FALSE;
2902 	}
2903 
2904 	if (pv_num < 1) {
2905 		php_error_docref(NULL, E_WARNING, "Field numbering starts at 1");
2906 		RETURN_FALSE;
2907 	}
2908 
2909 	PHP_ODBC_SQLCOLATTRIBUTE(result->stmt, (SQLUSMALLINT)pv_num, SQL_COLUMN_TYPE_NAME, tmp, 31, &tmplen, NULL);
2910 	RETURN_STRING(tmp)
2911 }
2912 /* }}} */
2913 
2914 /* {{{ proto int odbc_field_len(resource result_id, int field_number)
2915    Get the length (precision) of a column */
PHP_FUNCTIONnull2916 PHP_FUNCTION(odbc_field_len)
2917 {
2918 	odbc_column_lengths(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
2919 }
2920 /* }}} */
2921 
2922 /* {{{ proto int odbc_field_scale(resource result_id, int field_number)
2923    Get the scale of a column */
PHP_FUNCTIONnull2924 PHP_FUNCTION(odbc_field_scale)
2925 {
2926 	odbc_column_lengths(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
2927 }
2928 /* }}} */
2929 
2930 /* {{{ proto int odbc_field_num(resource result_id, string field_name)
2931    Return column number */
PHP_FUNCTIONnull2932 PHP_FUNCTION(odbc_field_num)
2933 {
2934 	char *fname;
2935 	size_t i, field_ind, fname_len;
2936 	odbc_result *result;
2937 	zval *pv_res;
2938 
2939 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs", &pv_res, &fname, &fname_len) == FAILURE) {
2940 		return;
2941 	}
2942 
2943 	if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
2944 		RETURN_FALSE;
2945 	}
2946 
2947 	if (result->numcols == 0) {
2948 		php_error_docref(NULL, E_WARNING, "No tuples available at this result index");
2949 		RETURN_FALSE;
2950 	}
2951 
2952 	field_ind = -1;
2953 	for(i = 0; i < result->numcols; i++) {
2954 		if (strcasecmp(result->values[i].name, fname) == 0) {
2955 			field_ind = i + 1;
2956 		}
2957 	}
2958 
2959 	if (field_ind == -1) {
2960 		RETURN_FALSE;
2961 	}
2962 	RETURN_LONG(field_ind);
2963 }
2964 /* }}} */
2965 
2966 /* {{{ proto mixed odbc_autocommit(resource connection_id [, int OnOff])
2967    Toggle autocommit mode or get status */
2968 /* There can be problems with pconnections!*/
PHP_FUNCTIONnull2969 PHP_FUNCTION(odbc_autocommit)
2970 {
2971 	odbc_connection *conn;
2972 	RETCODE rc;
2973 	zval *pv_conn;
2974 	zend_long pv_onoff = 0;
2975 
2976 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &pv_conn, &pv_onoff) == FAILURE) {
2977 		return;
2978 	}
2979 
2980 	if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
2981 		RETURN_FALSE;
2982 	}
2983 
2984 	if (ZEND_NUM_ARGS() > 1) {
2985 		rc = SQLSetConnectOption(conn->hdbc, SQL_AUTOCOMMIT, (pv_onoff) ? SQL_AUTOCOMMIT_ON : SQL_AUTOCOMMIT_OFF);
2986 		if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
2987 			odbc_sql_error(conn, SQL_NULL_HSTMT, "Set autocommit");
2988 			RETURN_FALSE;
2989 		}
2990 		RETVAL_TRUE;
2991 	} else {
2992 		SQLINTEGER status;
2993 
2994 		rc = SQLGetConnectOption(conn->hdbc, SQL_AUTOCOMMIT, (PTR)&status);
2995 		if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
2996 			odbc_sql_error(conn, SQL_NULL_HSTMT, "Get commit status");
2997 			RETURN_FALSE;
2998 		}
2999 		RETVAL_LONG((zend_long)status);
3000 	}
3001 }
3002 /* }}} */
3003 
3004 /* {{{ proto bool odbc_commit(resource connection_id)
3005    Commit an ODBC transaction */
PHP_FUNCTIONnull3006 PHP_FUNCTION(odbc_commit)
3007 {
3008 	odbc_transact(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
3009 }
3010 /* }}} */
3011 
3012 /* {{{ proto bool odbc_rollback(resource connection_id)
3013    Rollback a transaction */
PHP_FUNCTIONnull3014 PHP_FUNCTION(odbc_rollback)
3015 {
3016 	odbc_transact(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
3017 }
3018 /* }}} */
3019 
3020 /* {{{ php_odbc_lasterror */
php_odbc_lasterror(INTERNAL_FUNCTION_PARAMETERS, int mode)3021 static void php_odbc_lasterror(INTERNAL_FUNCTION_PARAMETERS, int mode)
3022 {
3023 	odbc_connection *conn;
3024 	zval *pv_handle;
3025 	char *ret;
3026 
3027 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|r", &pv_handle) == FAILURE) {
3028 		return;
3029 	}
3030 
3031 	if (ZEND_NUM_ARGS() == 1) {
3032 		if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_handle), "ODBC-Link", le_conn, le_pconn))) {
3033 			RETURN_FALSE;
3034 		}
3035 		if (mode == 0) {
3036 			ret = conn->laststate;
3037 		} else {
3038 			ret = conn->lasterrormsg;
3039 		}
3040 	} else {
3041 		if (mode == 0) {
3042 			ret = ODBCG(laststate);
3043 		} else {
3044 			ret = ODBCG(lasterrormsg);
3045 		}
3046 	}
3047 
3048 	RETURN_STRING(ret);
3049 }
3050 /* }}} */
3051 
3052 /* {{{ proto string odbc_error([resource connection_id])
3053    Get the last error code */
PHP_FUNCTIONnull3054 PHP_FUNCTION(odbc_error)
3055 {
3056 	php_odbc_lasterror(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
3057 }
3058 /* }}} */
3059 
3060 /* {{{ proto string odbc_errormsg([resource connection_id])
3061    Get the last error message */
PHP_FUNCTIONnull3062 PHP_FUNCTION(odbc_errormsg)
3063 {
3064 	php_odbc_lasterror(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
3065 }
3066 /* }}} */
3067 
3068 /* {{{ proto bool odbc_setoption(resource conn_id|result_id, int which, int option, int value)
3069    Sets connection or statement options */
3070 /* This one has to be used carefully. We can't allow to set connection options for
3071    persistent connections. I think that SetStmtOption is of little use, since most
3072    of those can only be specified before preparing/executing statements.
3073    On the other hand, they can be made connection wide default through SetConnectOption
3074    - but will be overridden by calls to SetStmtOption() in odbc_prepare/odbc_do
3075 */
PHP_FUNCTIONnull3076 PHP_FUNCTION(odbc_setoption)
3077 {
3078 	odbc_connection *conn;
3079 	odbc_result	*result;
3080 	RETCODE rc;
3081 	zval *pv_handle;
3082 	zend_long pv_which, pv_opt, pv_val;
3083 
3084 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rlll", &pv_handle, &pv_which, &pv_opt, &pv_val) == FAILURE) {
3085 		return;
3086 	}
3087 
3088 	switch (pv_which) {
3089 		case 1:		/* SQLSetConnectOption */
3090 			if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_handle), "ODBC-Link", le_conn, le_pconn))) {
3091 				RETURN_FALSE;
3092 			}
3093 
3094 			if (conn->persistent) {
3095 				php_error_docref(NULL, E_WARNING, "Unable to set option for persistent connection");
3096 				RETURN_FALSE;
3097 			}
3098 			rc = SQLSetConnectOption(conn->hdbc, (unsigned short) pv_opt, pv_val);
3099 			if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
3100 				odbc_sql_error(conn, SQL_NULL_HSTMT, "SetConnectOption");
3101 				RETURN_FALSE;
3102 			}
3103 			break;
3104 		case 2:		/* SQLSetStmtOption */
3105 			if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_handle), "ODBC result", le_result)) == NULL) {
3106 				RETURN_FALSE;
3107 			}
3108 
3109 			rc = SQLSetStmtOption(result->stmt, (unsigned short) pv_opt, pv_val);
3110 
3111 			if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
3112 				odbc_sql_error(result->conn_ptr, result->stmt, "SetStmtOption");
3113 				RETURN_FALSE;
3114 			}
3115 			break;
3116 		default:
3117 			php_error_docref(NULL, E_WARNING, "Unknown option type");
3118 			RETURN_FALSE;
3119 			break;
3120 	}
3121 
3122 	RETURN_TRUE;
3123 }
3124 /* }}} */
3125 
3126 /*
3127  * metadata functions
3128  */
3129 
3130 /* {{{ proto resource odbc_tables(resource connection_id [, string qualifier [, string owner [, string name [, string table_types]]]])
3131    Call the SQLTables function */
PHP_FUNCTIONnull3132 PHP_FUNCTION(odbc_tables)
3133 {
3134 	zval *pv_conn;
3135 	odbc_result   *result = NULL;
3136 	odbc_connection *conn;
3137 	char *cat = NULL, *schema = NULL, *table = NULL, *type = NULL;
3138 	size_t cat_len = 0, schema_len = 0, table_len = 0, type_len = 0;
3139 	RETCODE rc;
3140 
3141 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|s!sss", &pv_conn, &cat, &cat_len, &schema, &schema_len,
3142 		&table, &table_len, &type, &type_len) == FAILURE) {
3143 		return;
3144 	}
3145 
3146 	if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
3147 		RETURN_FALSE;
3148 	}
3149 
3150 	result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
3151 
3152 	rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
3153 	if (rc == SQL_INVALID_HANDLE) {
3154 		efree(result);
3155 		php_error_docref(NULL, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
3156 		RETURN_FALSE;
3157 	}
3158 
3159 	if (rc == SQL_ERROR) {
3160 		odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
3161 		efree(result);
3162 		RETURN_FALSE;
3163 	}
3164 
3165 	/* This hack is needed to access table information in Access databases (fmk) */
3166 	if (table && table_len && schema && schema_len == 0) {
3167 		schema = NULL;
3168 	}
3169 
3170 	rc = SQLTables(result->stmt,
3171 			cat, SAFE_SQL_NTS(cat),
3172 			schema,	SAFE_SQL_NTS(schema),
3173 			table, SAFE_SQL_NTS(table),
3174 			type, SAFE_SQL_NTS(type));
3175 
3176 	if (rc == SQL_ERROR) {
3177 		odbc_sql_error(conn, result->stmt, "SQLTables");
3178 		efree(result);
3179 		RETURN_FALSE;
3180 	}
3181 
3182 	result->numparams = 0;
3183 	SQLNumResultCols(result->stmt, &(result->numcols));
3184 
3185 	if (result->numcols > 0) {
3186 		if (!odbc_bindcols(result)) {
3187 			efree(result);
3188 			RETURN_FALSE;
3189 		}
3190 	} else {
3191 		result->values = NULL;
3192 	}
3193 	result->conn_ptr = conn;
3194 	result->fetched = 0;
3195 	RETURN_RES(zend_register_resource(result, le_result));
3196 }
3197 /* }}} */
3198 
3199 /* {{{ proto resource odbc_columns(resource connection_id [, string qualifier [, string owner [, string table_name [, string column_name]]]])
3200    Returns a result identifier that can be used to fetch a list of column names in specified tables */
PHP_FUNCTIONnull3201 PHP_FUNCTION(odbc_columns)
3202 {
3203 	zval *pv_conn;
3204 	odbc_result *result = NULL;
3205 	odbc_connection *conn;
3206 	char *cat = NULL, *schema = NULL, *table = NULL, *column = NULL;
3207 	size_t cat_len = 0, schema_len = 0, table_len = 0, column_len = 0;
3208 	RETCODE rc;
3209 
3210 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|s!sss", &pv_conn, &cat, &cat_len, &schema, &schema_len,
3211 		&table, &table_len, &column, &column_len) == FAILURE) {
3212 		return;
3213 	}
3214 
3215 	if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
3216 		RETURN_FALSE;
3217 	}
3218 
3219 	result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
3220 
3221 	rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
3222 	if (rc == SQL_INVALID_HANDLE) {
3223 		efree(result);
3224 		php_error_docref(NULL, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
3225 		RETURN_FALSE;
3226 	}
3227 
3228 	if (rc == SQL_ERROR) {
3229 		odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
3230 		efree(result);
3231 		RETURN_FALSE;
3232 	}
3233 
3234 	/*
3235 	 * Needed to make MS Access happy
3236 	 */
3237 	if (table && table_len && schema && schema_len == 0) {
3238 		schema = NULL;
3239 	}
3240 
3241 	rc = SQLColumns(result->stmt,
3242 			cat, (SQLSMALLINT) cat_len,
3243 			schema, (SQLSMALLINT) schema_len,
3244 			table, (SQLSMALLINT) table_len,
3245 			column, (SQLSMALLINT) column_len);
3246 
3247 	if (rc == SQL_ERROR) {
3248 		odbc_sql_error(conn, result->stmt, "SQLColumns");
3249 		efree(result);
3250 		RETURN_FALSE;
3251 	}
3252 
3253 	result->numparams = 0;
3254 	SQLNumResultCols(result->stmt, &(result->numcols));
3255 
3256 	if (result->numcols > 0) {
3257 		if (!odbc_bindcols(result)) {
3258 			efree(result);
3259 			RETURN_FALSE;
3260 		}
3261