forked from php/php-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php_mssql.c
2285 lines (1976 loc) · 63.9 KB
/
php_mssql.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2014 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Frank M. Kromann <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef COMPILE_DL_MSSQL
#define HAVE_MSSQL 1
#endif
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_globals.h"
#include "ext/standard/php_standard.h"
#include "ext/standard/info.h"
#include "php_mssql.h"
#include "php_ini.h"
#if HAVE_MSSQL
#define SAFE_STRING(s) ((s)?(s):"")
#define MSSQL_ASSOC 1<<0
#define MSSQL_NUM 1<<1
#define MSSQL_BOTH (MSSQL_ASSOC|MSSQL_NUM)
static int le_result, le_link, le_plink, le_statement;
static void php_mssql_get_column_content_with_type(mssql_link *mssql_ptr,int offset,zval *result, int column_type TSRMLS_DC);
static void php_mssql_get_column_content_without_type(mssql_link *mssql_ptr,int offset,zval *result, int column_type TSRMLS_DC);
static void _mssql_bind_hash_dtor(void *data);
/* {{{ arginfo */
ZEND_BEGIN_ARG_INFO_EX(arginfo_mssql_connect, 0, 0, 0)
ZEND_ARG_INFO(0, servername)
ZEND_ARG_INFO(0, username)
ZEND_ARG_INFO(0, password)
ZEND_ARG_INFO(0, newlink)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mssql_close, 0, 0, 0)
ZEND_ARG_INFO(0, link_identifier)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mssql_select_db, 0, 0, 1)
ZEND_ARG_INFO(0, database_name)
ZEND_ARG_INFO(0, link_identifier)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mssql_query, 0, 0, 1)
ZEND_ARG_INFO(0, query)
ZEND_ARG_INFO(0, link_identifier)
ZEND_ARG_INFO(0, batch_size)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mssql_fetch_batch, 0, 0, 1)
ZEND_ARG_INFO(0, result)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mssql_rows_affected, 0, 0, 1)
ZEND_ARG_INFO(0, link_identifier)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_mssql_get_last_message, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mssql_fetch_field, 0, 0, 1)
ZEND_ARG_INFO(0, result)
ZEND_ARG_INFO(0, field_offset)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mssql_fetch_array, 0, 0, 1)
ZEND_ARG_INFO(0, result)
ZEND_ARG_INFO(0, result_type)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mssql_fetch_assoc, 0, 0, 1)
ZEND_ARG_INFO(0, result_id)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mssql_field_length, 0, 0, 1)
ZEND_ARG_INFO(0, result)
ZEND_ARG_INFO(0, offset)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mssql_data_seek, 0, 0, 2)
ZEND_ARG_INFO(0, result_identifier)
ZEND_ARG_INFO(0, row_number)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mssql_result, 0, 0, 3)
ZEND_ARG_INFO(0, result)
ZEND_ARG_INFO(0, row)
ZEND_ARG_INFO(0, field)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mssql_min_error_severity, 0, 0, 1)
ZEND_ARG_INFO(0, severity)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mssql_init, 0, 0, 1)
ZEND_ARG_INFO(0, sp_name)
ZEND_ARG_INFO(0, link_identifier)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mssql_bind, 0, 0, 4)
ZEND_ARG_INFO(0, stmt)
ZEND_ARG_INFO(0, param_name)
ZEND_ARG_INFO(1, var)
ZEND_ARG_INFO(0, type)
ZEND_ARG_INFO(0, is_output)
ZEND_ARG_INFO(0, is_null)
ZEND_ARG_INFO(0, maxlen)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mssql_execute, 0, 0, 1)
ZEND_ARG_INFO(0, stmt)
ZEND_ARG_INFO(0, skip_results)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mssql_free_statement, 0, 0, 1)
ZEND_ARG_INFO(0, stmt)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mssql_guid_string, 0, 0, 1)
ZEND_ARG_INFO(0, binary)
ZEND_ARG_INFO(0, short_format)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ mssql_functions
*/
const zend_function_entry mssql_functions[] = {
PHP_FE(mssql_connect, arginfo_mssql_connect)
PHP_FE(mssql_pconnect, arginfo_mssql_connect)
PHP_FE(mssql_close, arginfo_mssql_close)
PHP_FE(mssql_select_db, arginfo_mssql_select_db)
PHP_FE(mssql_query, arginfo_mssql_query)
PHP_FE(mssql_fetch_batch, arginfo_mssql_fetch_batch)
PHP_FE(mssql_rows_affected, arginfo_mssql_rows_affected)
PHP_FE(mssql_free_result, arginfo_mssql_fetch_batch)
PHP_FE(mssql_get_last_message, arginfo_mssql_get_last_message)
PHP_FE(mssql_num_rows, arginfo_mssql_fetch_batch)
PHP_FE(mssql_num_fields, arginfo_mssql_fetch_batch)
PHP_FE(mssql_fetch_field, arginfo_mssql_fetch_field)
PHP_FE(mssql_fetch_row, arginfo_mssql_fetch_batch)
PHP_FE(mssql_fetch_array, arginfo_mssql_fetch_array)
PHP_FE(mssql_fetch_assoc, arginfo_mssql_fetch_assoc)
PHP_FE(mssql_fetch_object, arginfo_mssql_fetch_batch)
PHP_FE(mssql_field_length, arginfo_mssql_field_length)
PHP_FE(mssql_field_name, arginfo_mssql_field_length)
PHP_FE(mssql_field_type, arginfo_mssql_field_length)
PHP_FE(mssql_data_seek, arginfo_mssql_data_seek)
PHP_FE(mssql_field_seek, arginfo_mssql_fetch_field)
PHP_FE(mssql_result, arginfo_mssql_result)
PHP_FE(mssql_next_result, arginfo_mssql_fetch_assoc)
PHP_FE(mssql_min_error_severity, arginfo_mssql_min_error_severity)
PHP_FE(mssql_min_message_severity, arginfo_mssql_min_error_severity)
PHP_FE(mssql_init, arginfo_mssql_init)
PHP_FE(mssql_bind, arginfo_mssql_bind)
PHP_FE(mssql_execute, arginfo_mssql_execute)
PHP_FE(mssql_free_statement, arginfo_mssql_free_statement)
PHP_FE(mssql_guid_string, arginfo_mssql_guid_string)
PHP_FE_END
};
/* }}} */
ZEND_DECLARE_MODULE_GLOBALS(mssql)
static PHP_GINIT_FUNCTION(mssql);
/* {{{ mssql_module_entry
*/
zend_module_entry mssql_module_entry =
{
STANDARD_MODULE_HEADER,
"mssql",
mssql_functions,
PHP_MINIT(mssql),
PHP_MSHUTDOWN(mssql),
PHP_RINIT(mssql),
PHP_RSHUTDOWN(mssql),
PHP_MINFO(mssql),
NO_VERSION_YET,
PHP_MODULE_GLOBALS(mssql),
PHP_GINIT(mssql),
NULL,
NULL,
STANDARD_MODULE_PROPERTIES_EX
};
/* }}} */
#ifdef COMPILE_DL_MSSQL
ZEND_GET_MODULE(mssql)
#endif
#define CHECK_LINK(link) { if (link==-1) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "A link to the server could not be established"); RETURN_FALSE; } }
/* {{{ PHP_INI_DISP
*/
static PHP_INI_DISP(display_text_size)
{
char *value;
TSRMLS_FETCH();
if (type == PHP_INI_DISPLAY_ORIG && ini_entry->modified) {
value = ini_entry->orig_value;
} else if (ini_entry->value) {
value = ini_entry->value;
} else {
value = NULL;
}
if (atoi(value) == -1) {
PUTS("Server default");
} else {
php_printf("%s", value);
}
}
/* }}} */
/* {{{ PHP_INI
*/
PHP_INI_BEGIN()
STD_PHP_INI_BOOLEAN("mssql.allow_persistent", "1", PHP_INI_SYSTEM, OnUpdateBool, allow_persistent, zend_mssql_globals, mssql_globals)
STD_PHP_INI_ENTRY_EX("mssql.max_persistent", "-1", PHP_INI_SYSTEM, OnUpdateLong, max_persistent, zend_mssql_globals, mssql_globals, display_link_numbers)
STD_PHP_INI_ENTRY_EX("mssql.max_links", "-1", PHP_INI_SYSTEM, OnUpdateLong, max_links, zend_mssql_globals, mssql_globals, display_link_numbers)
STD_PHP_INI_ENTRY_EX("mssql.min_error_severity", "10", PHP_INI_ALL, OnUpdateLong, cfg_min_error_severity, zend_mssql_globals, mssql_globals, display_link_numbers)
STD_PHP_INI_ENTRY_EX("mssql.min_message_severity", "10", PHP_INI_ALL, OnUpdateLong, cfg_min_message_severity, zend_mssql_globals, mssql_globals, display_link_numbers)
/*
mssql.compatAbility_mode (with typo) was used for relatively long time.
Unless it is fixed the old version is also kept for compatibility reasons.
*/
STD_PHP_INI_BOOLEAN("mssql.compatability_mode", "0", PHP_INI_ALL, OnUpdateBool, compatibility_mode, zend_mssql_globals, mssql_globals)
STD_PHP_INI_BOOLEAN("mssql.compatibility_mode", "0", PHP_INI_ALL, OnUpdateBool, compatibility_mode, zend_mssql_globals, mssql_globals)
STD_PHP_INI_ENTRY_EX("mssql.connect_timeout", "5", PHP_INI_ALL, OnUpdateLong, connect_timeout, zend_mssql_globals, mssql_globals, display_link_numbers)
STD_PHP_INI_ENTRY_EX("mssql.timeout", "60", PHP_INI_ALL, OnUpdateLong, timeout, zend_mssql_globals, mssql_globals, display_link_numbers)
STD_PHP_INI_ENTRY_EX("mssql.textsize", "-1", PHP_INI_ALL, OnUpdateLong, textsize, zend_mssql_globals, mssql_globals, display_text_size)
STD_PHP_INI_ENTRY_EX("mssql.textlimit", "-1", PHP_INI_ALL, OnUpdateLong, textlimit, zend_mssql_globals, mssql_globals, display_text_size)
STD_PHP_INI_ENTRY_EX("mssql.batchsize", "0", PHP_INI_ALL, OnUpdateLong, batchsize, zend_mssql_globals, mssql_globals, display_link_numbers)
STD_PHP_INI_BOOLEAN("mssql.datetimeconvert", "1", PHP_INI_ALL, OnUpdateBool, datetimeconvert, zend_mssql_globals, mssql_globals)
STD_PHP_INI_BOOLEAN("mssql.secure_connection", "0", PHP_INI_SYSTEM, OnUpdateBool, secure_connection, zend_mssql_globals, mssql_globals)
STD_PHP_INI_ENTRY_EX("mssql.max_procs", "-1", PHP_INI_ALL, OnUpdateLong, max_procs, zend_mssql_globals, mssql_globals, display_link_numbers)
#ifdef HAVE_FREETDS
STD_PHP_INI_ENTRY("mssql.charset", "", PHP_INI_ALL, OnUpdateString, charset, zend_mssql_globals, mssql_globals)
#endif
PHP_INI_END()
/* }}} */
/* error handler */
static int php_mssql_error_handler(DBPROCESS *dbproc, int severity, int dberr, int oserr, char *dberrstr, char *oserrstr)
{
TSRMLS_FETCH();
if (severity >= MS_SQL_G(min_error_severity)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s (severity %d)", dberrstr, severity);
}
return INT_CANCEL;
}
/* {{{ php_mssql_message_handler
*/
/* message handler */
static int php_mssql_message_handler(DBPROCESS *dbproc, DBINT msgno,int msgstate, int severity,char *msgtext,char *srvname, char *procname,DBUSMALLINT line)
{
TSRMLS_FETCH();
if (severity >= MS_SQL_G(min_message_severity)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "message: %s (severity %d)", msgtext, severity);
}
if (MS_SQL_G(server_message)) {
STR_FREE(MS_SQL_G(server_message));
MS_SQL_G(server_message) = NULL;
}
MS_SQL_G(server_message) = estrdup(msgtext);
return 0;
}
/* }}} */
/* {{{ _clean_invalid_results
*/
static int _clean_invalid_results(zend_rsrc_list_entry *le TSRMLS_DC)
{
if (Z_TYPE_P(le) == le_result) {
mssql_link *mssql_ptr = ((mssql_result *) le->ptr)->mssql_ptr;
if (!mssql_ptr->valid) {
return 1;
}
}
return 0;
}
/* }}} */
/* {{{ _free_result
*/
static void _free_result(mssql_result *result, int free_fields)
{
int i,j;
if (result->data) {
for (i=0; i<result->num_rows; i++) {
if (result->data[i]) {
for (j=0; j<result->num_fields; j++) {
zval_dtor(&result->data[i][j]);
}
efree(result->data[i]);
}
}
efree(result->data);
result->data = NULL;
result->blocks_initialized = 0;
}
if (free_fields && result->fields) {
for (i=0; i<result->num_fields; i++) {
STR_FREE(result->fields[i].name);
STR_FREE(result->fields[i].column_source);
}
efree(result->fields);
}
}
/* }}} */
/* {{{ _free_mssql_statement
*/
static void _free_mssql_statement(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
mssql_statement *statement = (mssql_statement *)rsrc->ptr;
if (statement->binds) {
zend_hash_destroy(statement->binds);
efree(statement->binds);
}
efree(statement);
}
/* }}} */
/* {{{ _free_mssql_result
*/
static void _free_mssql_result(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
mssql_result *result = (mssql_result *)rsrc->ptr;
_free_result(result, 1);
dbcancel(result->mssql_ptr->link);
efree(result);
}
/* }}} */
/* {{{ php_mssql_set_defaullt_link
*/
static void php_mssql_set_default_link(int id TSRMLS_DC)
{
if (MS_SQL_G(default_link)!=-1) {
zend_list_delete(MS_SQL_G(default_link));
}
MS_SQL_G(default_link) = id;
zend_list_addref(id);
}
/* }}} */
/* {{{ _close_mssql_link
*/
static void _close_mssql_link(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
mssql_link *mssql_ptr = (mssql_link *)rsrc->ptr;
mssql_ptr->valid = 0;
zend_hash_apply(&EG(regular_list),(apply_func_t) _clean_invalid_results TSRMLS_CC);
dbclose(mssql_ptr->link);
dbfreelogin(mssql_ptr->login);
efree(mssql_ptr);
MS_SQL_G(num_links)--;
}
/* }}} */
/* {{{ _close_mssql_plink
*/
static void _close_mssql_plink(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
mssql_link *mssql_ptr = (mssql_link *)rsrc->ptr;
dbclose(mssql_ptr->link);
dbfreelogin(mssql_ptr->login);
free(mssql_ptr);
MS_SQL_G(num_persistent)--;
MS_SQL_G(num_links)--;
}
/* }}} */
/* {{{ _mssql_bind_hash_dtor
*/
static void _mssql_bind_hash_dtor(void *data)
{
mssql_bind *bind= (mssql_bind *) data;
zval_ptr_dtor(&(bind->zval));
}
/* }}} */
/* {{{ PHP_GINIT_FUNCTION
*/
static PHP_GINIT_FUNCTION(mssql)
{
long compatibility_mode;
mssql_globals->num_persistent = 0;
mssql_globals->get_column_content = php_mssql_get_column_content_with_type;
if (cfg_get_int("mssql.compatibility_mode", &compatibility_mode) == SUCCESS) {
if (compatibility_mode) {
mssql_globals->get_column_content = php_mssql_get_column_content_without_type;
}
}
}
/* }}} */
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(mssql)
{
REGISTER_INI_ENTRIES();
le_statement = zend_register_list_destructors_ex(_free_mssql_statement, NULL, "mssql statement", module_number);
le_result = zend_register_list_destructors_ex(_free_mssql_result, NULL, "mssql result", module_number);
le_link = zend_register_list_destructors_ex(_close_mssql_link, NULL, "mssql link", module_number);
le_plink = zend_register_list_destructors_ex(NULL, _close_mssql_plink, "mssql link persistent", module_number);
Z_TYPE(mssql_module_entry) = type;
if (dbinit()==FAIL) {
return FAILURE;
}
/* BEGIN MSSQL data types for mssql_bind */
REGISTER_INT_CONSTANT("MSSQL_ASSOC", MSSQL_ASSOC, CONST_CS | CONST_PERSISTENT);
REGISTER_INT_CONSTANT("MSSQL_NUM", MSSQL_NUM, CONST_CS | CONST_PERSISTENT);
REGISTER_INT_CONSTANT("MSSQL_BOTH", MSSQL_BOTH, CONST_CS | CONST_PERSISTENT);
REGISTER_INT_CONSTANT("SQLTEXT",SQLTEXT, CONST_CS | CONST_PERSISTENT);
REGISTER_INT_CONSTANT("SQLVARCHAR",SQLVARCHAR, CONST_CS | CONST_PERSISTENT);
REGISTER_INT_CONSTANT("SQLCHAR",SQLCHAR, CONST_CS | CONST_PERSISTENT);
REGISTER_INT_CONSTANT("SQLINT1",SQLINT1, CONST_CS | CONST_PERSISTENT);
REGISTER_INT_CONSTANT("SQLINT2",SQLINT2, CONST_CS | CONST_PERSISTENT);
REGISTER_INT_CONSTANT("SQLINT4",SQLINT4, CONST_CS | CONST_PERSISTENT);
REGISTER_INT_CONSTANT("SQLBIT",SQLBIT, CONST_CS | CONST_PERSISTENT);
REGISTER_INT_CONSTANT("SQLFLT4",SQLFLT4, CONST_CS | CONST_PERSISTENT);
REGISTER_INT_CONSTANT("SQLFLT8",SQLFLT8, CONST_CS | CONST_PERSISTENT);
REGISTER_INT_CONSTANT("SQLFLTN",SQLFLTN, CONST_CS | CONST_PERSISTENT);
/* END MSSQL data types for mssql_bind */
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(mssql)
{
UNREGISTER_INI_ENTRIES();
#ifndef HAVE_FREETDS
dbwinexit();
#else
dbexit();
#endif
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(mssql)
{
MS_SQL_G(default_link) = -1;
MS_SQL_G(num_links) = MS_SQL_G(num_persistent);
MS_SQL_G(appname) = estrndup("PHP 5", 5);
MS_SQL_G(server_message) = NULL;
MS_SQL_G(min_error_severity) = MS_SQL_G(cfg_min_error_severity);
MS_SQL_G(min_message_severity) = MS_SQL_G(cfg_min_message_severity);
if (MS_SQL_G(connect_timeout) < 1) MS_SQL_G(connect_timeout) = 1;
if (MS_SQL_G(timeout) < 0) MS_SQL_G(timeout) = 60;
if (MS_SQL_G(max_procs) != -1) {
dbsetmaxprocs((TDS_SHORT)MS_SQL_G(max_procs));
}
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(mssql)
{
STR_FREE(MS_SQL_G(appname));
MS_SQL_G(appname) = NULL;
if (MS_SQL_G(server_message)) {
STR_FREE(MS_SQL_G(server_message));
MS_SQL_G(server_message) = NULL;
}
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(mssql)
{
char buf[32];
php_info_print_table_start();
php_info_print_table_header(2, "MSSQL Support", "enabled");
snprintf(buf, sizeof(buf), "%ld", MS_SQL_G(num_persistent));
php_info_print_table_row(2, "Active Persistent Links", buf);
snprintf(buf, sizeof(buf), "%ld", MS_SQL_G(num_links));
php_info_print_table_row(2, "Active Links", buf);
php_info_print_table_row(2, "Library version", MSSQL_VERSION);
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
/* }}} */
/* {{{ php_mssql_do_connect
*/
static void php_mssql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
{
char *host = NULL, *user = NULL, *passwd = NULL;
php_size_t host_len = 0, user_len = 0, passwd_len = 0;
zend_bool new_link = 0;
char *hashed_details;
int hashed_details_length;
mssql_link mssql, *mssql_ptr;
char buffer[40];
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|SSSb", &host, &host_len, &user, &user_len, &passwd, &passwd_len, &new_link) == FAILURE) {
return;
}
/* Limit strings to 255 chars to prevent overflow issues in underlying libraries */
if(host_len>255) {
host[255] = '\0';
}
if(user_len>255) {
user[255] = '\0';
}
if(passwd_len>255) {
passwd[255] = '\0';
}
switch(ZEND_NUM_ARGS())
{
case 0:
/* defaults */
hashed_details_length=5+3;
hashed_details = (char *) emalloc(hashed_details_length+1);
strcpy(hashed_details, "mssql___");
break;
case 1:
hashed_details_length = spprintf(&hashed_details, 0, "mssql_%s__", host);
break;
case 2:
hashed_details_length = spprintf(&hashed_details, 0, "mssql_%s_%s_", host, user);
break;
case 3:
case 4:
hashed_details_length = spprintf(&hashed_details, 0, "mssql_%s_%s_%s", host, user, passwd);
break;
}
if (hashed_details == NULL) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Out of memory");
RETURN_FALSE;
}
dbsetlogintime(MS_SQL_G(connect_timeout));
dbsettime(MS_SQL_G(timeout));
/* set a DBLOGIN record */
if ((mssql.login = dblogin()) == NULL) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to allocate login record");
RETURN_FALSE;
}
DBERRHANDLE(mssql.login, (EHANDLEFUNC) php_mssql_error_handler);
DBMSGHANDLE(mssql.login, (MHANDLEFUNC) php_mssql_message_handler);
#ifndef HAVE_FREETDS
if (MS_SQL_G(secure_connection)){
DBSETLSECURE(mssql.login);
}
else {
#endif
if (user) {
DBSETLUSER(mssql.login,user);
}
if (passwd) {
DBSETLPWD(mssql.login,passwd);
}
#ifndef HAVE_FREETDS
}
#endif
#ifdef HAVE_FREETDS
if (MS_SQL_G(charset) && strlen(MS_SQL_G(charset))) {
DBSETLCHARSET(mssql.login, MS_SQL_G(charset));
}
#endif
DBSETLAPP(mssql.login,MS_SQL_G(appname));
mssql.valid = 1;
#ifndef HAVE_FREETDS
DBSETLVERSION(mssql.login, DBVER60);
#endif
/* DBSETLTIME(mssql.login, TIMEOUT_INFINITE); */
if (!MS_SQL_G(allow_persistent)) {
persistent=0;
}
if (persistent) {
zend_rsrc_list_entry *le;
/* try to find if we already have this link in our persistent list */
if (new_link || zend_hash_find(&EG(persistent_list), hashed_details, hashed_details_length + 1, (void **) &le)==FAILURE) { /* we don't */
zend_rsrc_list_entry new_le;
if (MS_SQL_G(max_links) != -1 && MS_SQL_G(num_links) >= MS_SQL_G(max_links)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Too many open links (%ld)", MS_SQL_G(num_links));
efree(hashed_details);
dbfreelogin(mssql.login);
RETURN_FALSE;
}
if (MS_SQL_G(max_persistent) != -1 && MS_SQL_G(num_persistent) >= MS_SQL_G(max_persistent)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Too many open persistent links (%ld)", MS_SQL_G(num_persistent));
efree(hashed_details);
dbfreelogin(mssql.login);
RETURN_FALSE;
}
/* create the link */
if ((mssql.link = dbopen(mssql.login, host)) == FAIL) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to connect to server: %s", (host == NULL ? "" : host));
efree(hashed_details);
dbfreelogin(mssql.login);
RETURN_FALSE;
}
if (DBSETOPT(mssql.link, DBBUFFER, "2")==FAIL) {
efree(hashed_details);
dbfreelogin(mssql.login);
dbclose(mssql.link);
RETURN_FALSE;
}
#ifndef HAVE_FREETDS
if (MS_SQL_G(textlimit) != -1) {
snprintf(buffer, sizeof(buffer), "%li", MS_SQL_G(textlimit));
if (DBSETOPT(mssql.link, DBTEXTLIMIT, buffer)==FAIL) {
efree(hashed_details);
dbfreelogin(mssql.login);
dbclose(mssql.link);
RETURN_FALSE;
}
}
#endif
if (MS_SQL_G(textsize) != -1) {
snprintf(buffer, sizeof(buffer), "SET TEXTSIZE %li", MS_SQL_G(textsize));
dbcmd(mssql.link, buffer);
dbsqlexec(mssql.link);
dbresults(mssql.link);
}
/* hash it up */
mssql_ptr = (mssql_link *) malloc(sizeof(mssql_link));
if (!mssql_ptr) {
efree(hashed_details);
dbfreelogin(mssql.login);
dbclose(mssql.link);
RETURN_FALSE;
}
memcpy(mssql_ptr, &mssql, sizeof(mssql_link));
Z_TYPE(new_le) = le_plink;
new_le.ptr = mssql_ptr;
if (zend_hash_update(&EG(persistent_list), hashed_details, hashed_details_length + 1, &new_le, sizeof(zend_rsrc_list_entry), NULL)==FAILURE) {
free(mssql_ptr);
efree(hashed_details);
dbfreelogin(mssql.login);
dbclose(mssql.link);
RETURN_FALSE;
}
MS_SQL_G(num_persistent)++;
MS_SQL_G(num_links)++;
} else { /* we do */
if (Z_TYPE_P(le) != le_plink) {
#if BROKEN_MSSQL_PCONNECTS
log_error("PHP/MS SQL: Hashed persistent link is not a MS SQL link!",php_rqst->server);
#endif
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Hashed persistent link is not a MS SQL link!");
efree(hashed_details);
RETURN_FALSE;
}
mssql_ptr = (mssql_link *) le->ptr;
/* test that the link hasn't died */
if (DBDEAD(mssql_ptr->link) == TRUE) {
dbclose(mssql_ptr->link);
#if BROKEN_MSSQL_PCONNECTS
log_error("PHP/MS SQL: Persistent link died, trying to reconnect...",php_rqst->server);
#endif
if ((mssql_ptr->link=dbopen(mssql_ptr->login,host))==NULL) {
#if BROKEN_MSSQL_PCONNECTS
log_error("PHP/MS SQL: Unable to reconnect!",php_rqst->server);
#endif
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Link to server lost, unable to reconnect");
zend_hash_del(&EG(persistent_list), hashed_details, hashed_details_length+1);
efree(hashed_details);
dbfreelogin(mssql_ptr->login);
RETURN_FALSE;
}
#if BROKEN_MSSQL_PCONNECTS
log_error("PHP/MS SQL: Reconnect successful!",php_rqst->server);
#endif
if (DBSETOPT(mssql_ptr->link, DBBUFFER, "2")==FAIL) {
#if BROKEN_MSSQL_PCONNECTS
log_error("PHP/MS SQL: Unable to set required options",php_rqst->server);
#endif
zend_hash_del(&EG(persistent_list), hashed_details, hashed_details_length + 1);
efree(hashed_details);
dbfreelogin(mssql_ptr->login);
dbclose(mssql_ptr->link);
RETURN_FALSE;
}
}
}
ZEND_REGISTER_RESOURCE(return_value, mssql_ptr, le_plink);
} else { /* non persistent */
zend_rsrc_list_entry *index_ptr, new_index_ptr;
/* first we check the hash for the hashed_details key. if it exists,
* it should point us to the right offset where the actual mssql link sits.
* if it doesn't, open a new mssql link, add it to the resource list,
* and add a pointer to it with hashed_details as the key.
*/
if (!new_link && zend_hash_find(&EG(regular_list), hashed_details, hashed_details_length + 1,(void **) &index_ptr)==SUCCESS) {
int type,link;
void *ptr;
if (Z_TYPE_P(index_ptr) != le_index_ptr) {
efree(hashed_details);
dbfreelogin(mssql.login);
RETURN_FALSE;
}
link = (int) index_ptr->ptr;
ptr = zend_list_find(link,&type); /* check if the link is still there */
if (ptr && (type==le_link || type==le_plink)) {
zend_list_addref(link);
Z_IVAL_P(return_value) = link;
php_mssql_set_default_link(link TSRMLS_CC);
Z_TYPE_P(return_value) = IS_RESOURCE;
dbfreelogin(mssql.login);
efree(hashed_details);
return;
} else {
zend_hash_del(&EG(regular_list), hashed_details, hashed_details_length + 1);
}
}
if (MS_SQL_G(max_links) != -1 && MS_SQL_G(num_links) >= MS_SQL_G(max_links)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Too many open links (%ld)", MS_SQL_G(num_links));
efree(hashed_details);
dbfreelogin(mssql.login);
RETURN_FALSE;
}
if ((mssql.link=dbopen(mssql.login, host))==NULL) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to connect to server: %s", (host == NULL ? "" : host));
efree(hashed_details);
dbfreelogin(mssql.login);
RETURN_FALSE;
}
if (DBSETOPT(mssql.link, DBBUFFER,"2")==FAIL) {
efree(hashed_details);
dbfreelogin(mssql.login);
dbclose(mssql.link);
RETURN_FALSE;
}
#ifndef HAVE_FREETDS
if (MS_SQL_G(textlimit) != -1) {
snprintf(buffer, sizeof(buffer), "%li", MS_SQL_G(textlimit));
if (DBSETOPT(mssql.link, DBTEXTLIMIT, buffer)==FAIL) {
efree(hashed_details);
dbfreelogin(mssql.login);
dbclose(mssql.link);
RETURN_FALSE;
}
}
#endif
if (MS_SQL_G(textsize) != -1) {
snprintf(buffer, sizeof(buffer), "SET TEXTSIZE %li", MS_SQL_G(textsize));
dbcmd(mssql.link, buffer);
dbsqlexec(mssql.link);
dbresults(mssql.link);
}
/* add it to the list */
mssql_ptr = (mssql_link *) emalloc(sizeof(mssql_link));
memcpy(mssql_ptr, &mssql, sizeof(mssql_link));
ZEND_REGISTER_RESOURCE(return_value, mssql_ptr, le_link);
/* add it to the hash */
new_index_ptr.ptr = (void *) Z_IVAL_P(return_value);
Z_TYPE(new_index_ptr) = le_index_ptr;
if (zend_hash_update(&EG(regular_list), hashed_details, hashed_details_length + 1,(void *) &new_index_ptr, sizeof(zend_rsrc_list_entry),NULL)==FAILURE) {
efree(hashed_details);
RETURN_FALSE;
}
MS_SQL_G(num_links)++;
}
efree(hashed_details);
php_mssql_set_default_link(Z_IVAL_P(return_value) TSRMLS_CC);
}
/* }}} */
/* {{{ php_mssql_get_default_link
*/
static int php_mssql_get_default_link(INTERNAL_FUNCTION_PARAMETERS)
{
if (MS_SQL_G(default_link)==-1) { /* no link opened yet, implicitly open one */
ht = 0;
php_mssql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU,0);
}
return MS_SQL_G(default_link);
}
/* }}} */
/* {{{ proto int mssql_connect([string servername [, string username [, string password [, bool new_link]]]])
Establishes a connection to a MS-SQL server */
PHP_FUNCTION(mssql_connect)
{
php_mssql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU,0);
}
/* }}} */
/* {{{ proto int mssql_pconnect([string servername [, string username [, string password [, bool new_link]]]])
Establishes a persistent connection to a MS-SQL server */
PHP_FUNCTION(mssql_pconnect)
{
php_mssql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU,1);
}
/* }}} */
/* {{{ proto bool mssql_close([resource conn_id])
Closes a connection to a MS-SQL server */
PHP_FUNCTION(mssql_close)
{
zval *mssql_link_index = NULL;
int id = -1;
mssql_link *mssql_ptr;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mssql_link_index) == FAILURE) {
return;
}
if (mssql_link_index == NULL) {
id = php_mssql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
CHECK_LINK(id);
}
ZEND_FETCH_RESOURCE2(mssql_ptr, mssql_link *, &mssql_link_index, id, "MS SQL-Link", le_link, le_plink);
if (mssql_link_index) {
zend_list_delete(Z_RESVAL_P(mssql_link_index));
} else {
zend_list_delete(id);
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool mssql_select_db(string database_name [, resource conn_id])
Select a MS-SQL database */
PHP_FUNCTION(mssql_select_db)
{
char *db;
zval *mssql_link_index = NULL;
php_size_t db_len;
int id = -1;
mssql_link *mssql_ptr;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S|r", &db, &db_len, &mssql_link_index) == FAILURE) {
return;
}
if (mssql_link_index == NULL) {
id = php_mssql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
CHECK_LINK(id);
}
ZEND_FETCH_RESOURCE2(mssql_ptr, mssql_link *, &mssql_link_index, id, "MS SQL-Link", le_link, le_plink);
if (dbuse(mssql_ptr->link, db)==FAIL) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to select database: %s", db);
RETURN_FALSE;
} else {
RETURN_TRUE;
}
}
/* }}} */
/* {{{ php_mssql_get_column_content_with_type
*/
static void php_mssql_get_column_content_with_type(mssql_link *mssql_ptr,int offset,zval *result, int column_type TSRMLS_DC)
{
if (dbdata(mssql_ptr->link,offset) == NULL && dbdatlen(mssql_ptr->link,offset) == 0) {
ZVAL_NULL(result);
return;
}
switch (column_type)
{
case SQLBIT:
case SQLINT1:
case SQLINT2:
case SQLINT4:
case SQLINTN: {
ZVAL_INT(result, (long) anyintcol(offset));
break;
}
case SQLCHAR:
case SQLVARCHAR:
case SQLTEXT: {
int length;
char *data = charcol(offset);
length=dbdatlen(mssql_ptr->link,offset);
#if ilia_0
while (length>0 && data[length-1] == ' ') { /* nuke trailing whitespace */
length--;
}
#endif
ZVAL_STRINGL(result, data, length, 1);
break;
}
case SQLFLT4:
ZVAL_DOUBLE(result, (double) floatcol4(offset));
break;
case SQLMONEY:
case SQLMONEY4:
case SQLMONEYN: {
DBFLT8 res_buf;
dbconvert(NULL, column_type, dbdata(mssql_ptr->link,offset), 8, SQLFLT8, (LPBYTE)&res_buf, -1);
ZVAL_DOUBLE(result, res_buf);
}
break;
case SQLFLT8:
ZVAL_DOUBLE(result, (double) floatcol8(offset));
break;
#ifdef SQLUNIQUE
case SQLUNIQUE: {
#else
case 36: { /* FreeTDS hack */
#endif
char *data = charcol(offset);
/* uniqueidentifier is a 16-byte binary number */
ZVAL_STRINGL(result, data, 16, 1);
}
break;
case SQLVARBINARY:
case SQLBINARY:
case SQLIMAGE: {
int res_length = dbdatlen(mssql_ptr->link, offset);
if (!res_length) {
ZVAL_NULL(result);
} else {
ZVAL_STRINGL(result, (char *)dbdata(mssql_ptr->link, offset), res_length, 1);
}
}