forked from php/php-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphp_sybase_ct.c
2244 lines (1908 loc) · 67.8 KB
/
php_sybase_ct.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-2012 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. |
+----------------------------------------------------------------------+
| Authors: Zeev Suraski <[email protected]> |
| Tom May <[email protected]> |
| Timm Friebe <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_sybase_ct.h"
#include "ext/standard/php_standard.h"
#include "ext/standard/info.h"
#include "php_globals.h"
#include "php_ini.h"
/* True globals, no need for thread safety */
static int le_link, le_plink, le_result;
#if HAVE_SYBASE_CT
ZEND_DECLARE_MODULE_GLOBALS(sybase)
static PHP_GINIT_FUNCTION(sybase);
static PHP_GSHUTDOWN_FUNCTION(sybase);
/* {{{ arginfo */
ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_connect, 0, 0, 0)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, user)
ZEND_ARG_INFO(0, password)
ZEND_ARG_INFO(0, charset)
ZEND_ARG_INFO(0, appname)
ZEND_ARG_INFO(0, new)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_pconnect, 0, 0, 0)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, user)
ZEND_ARG_INFO(0, password)
ZEND_ARG_INFO(0, charset)
ZEND_ARG_INFO(0, appname)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_close, 0, 0, 0)
ZEND_ARG_INFO(0, link_id)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_select_db, 0, 0, 1)
ZEND_ARG_INFO(0, database)
ZEND_ARG_INFO(0, link_id)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_query, 0, 0, 1)
ZEND_ARG_INFO(0, query)
ZEND_ARG_INFO(0, link_id)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_unbuffered_query, 0, 0, 1)
ZEND_ARG_INFO(0, query)
ZEND_ARG_INFO(0, link_id)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_free_result, 0, 0, 1)
ZEND_ARG_INFO(0, result)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_get_last_message, 0, 0, 1)
ZEND_ARG_INFO(0, d)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_num_rows, 0, 0, 1)
ZEND_ARG_INFO(0, result)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_num_fields, 0, 0, 1)
ZEND_ARG_INFO(0, result)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_fetch_row, 0, 0, 1)
ZEND_ARG_INFO(0, result)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_fetch_object, 0, 0, 1)
ZEND_ARG_INFO(0, result)
ZEND_ARG_INFO(0, object)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_fetch_array, 0, 0, 1)
ZEND_ARG_INFO(0, result)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_fetch_assoc, 0, 0, 1)
ZEND_ARG_INFO(0, result)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_data_seek, 0, 0, 2)
ZEND_ARG_INFO(0, result)
ZEND_ARG_INFO(0, offset)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_fetch_field, 0, 0, 1)
ZEND_ARG_INFO(0, result)
ZEND_ARG_INFO(0, offset)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_field_seek, 0, 0, 2)
ZEND_ARG_INFO(0, result)
ZEND_ARG_INFO(0, offset)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_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_sybase_affected_rows, 0, 0, 0)
ZEND_ARG_INFO(0, link_id)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_min_client_severity, 0, 0, 1)
ZEND_ARG_INFO(0, severity)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_min_server_severity, 0, 0, 1)
ZEND_ARG_INFO(0, severity)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_deadlock_retry_count, 0, 0, 1)
ZEND_ARG_INFO(0, retry_count)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_sybase_set_message_handler, 0, 0, 1)
ZEND_ARG_INFO(0, error_func)
ZEND_ARG_INFO(0, connection)
ZEND_END_ARG_INFO()
/* }}} */
const zend_function_entry sybase_functions[] = {
PHP_FE(sybase_connect, arginfo_sybase_connect)
PHP_FE(sybase_pconnect, arginfo_sybase_pconnect)
PHP_FE(sybase_close, arginfo_sybase_close)
PHP_FE(sybase_select_db, arginfo_sybase_select_db)
PHP_FE(sybase_query, arginfo_sybase_query)
PHP_FE(sybase_unbuffered_query, arginfo_sybase_unbuffered_query)
PHP_FE(sybase_free_result, arginfo_sybase_free_result)
PHP_FE(sybase_get_last_message, arginfo_sybase_get_last_message)
PHP_FE(sybase_num_rows, arginfo_sybase_num_rows)
PHP_FE(sybase_num_fields, arginfo_sybase_num_fields)
PHP_FE(sybase_fetch_row, arginfo_sybase_fetch_row)
PHP_FE(sybase_fetch_array, arginfo_sybase_fetch_array)
PHP_FE(sybase_fetch_assoc, arginfo_sybase_fetch_assoc)
PHP_FE(sybase_fetch_object, arginfo_sybase_fetch_object)
PHP_FE(sybase_data_seek, arginfo_sybase_data_seek)
PHP_FE(sybase_fetch_field, arginfo_sybase_fetch_field)
PHP_FE(sybase_field_seek, arginfo_sybase_field_seek)
PHP_FE(sybase_result, arginfo_sybase_result)
PHP_FE(sybase_affected_rows, arginfo_sybase_affected_rows)
PHP_FE(sybase_min_client_severity, arginfo_sybase_min_client_severity)
PHP_FE(sybase_min_server_severity, arginfo_sybase_min_server_severity)
PHP_FE(sybase_set_message_handler, arginfo_sybase_set_message_handler)
PHP_FE(sybase_deadlock_retry_count, arginfo_sybase_deadlock_retry_count)
#if !defined(PHP_WIN32) && !defined(HAVE_MSSQL)
PHP_FALIAS(mssql_connect, sybase_connect, arginfo_sybase_connect)
PHP_FALIAS(mssql_pconnect, sybase_pconnect, arginfo_sybase_pconnect)
PHP_FALIAS(mssql_close, sybase_close, arginfo_sybase_close)
PHP_FALIAS(mssql_select_db, sybase_select_db, arginfo_sybase_select_db)
PHP_FALIAS(mssql_query, sybase_query, arginfo_sybase_query)
PHP_FALIAS(mssql_unbuffered_query, sybase_unbuffered_query, arginfo_sybase_unbuffered_query)
PHP_FALIAS(mssql_free_result, sybase_free_result, arginfo_sybase_free_result)
PHP_FALIAS(mssql_get_last_message, sybase_get_last_message, arginfo_sybase_get_last_message)
PHP_FALIAS(mssql_num_rows, sybase_num_rows, arginfo_sybase_num_rows)
PHP_FALIAS(mssql_num_fields, sybase_num_fields, arginfo_sybase_num_fields)
PHP_FALIAS(mssql_fetch_row, sybase_fetch_row, arginfo_sybase_fetch_row)
PHP_FALIAS(mssql_fetch_array, sybase_fetch_array, arginfo_sybase_fetch_array)
PHP_FALIAS(mssql_fetch_assoc, sybase_fetch_assoc, arginfo_sybase_fetch_assoc)
PHP_FALIAS(mssql_fetch_object, sybase_fetch_object, arginfo_sybase_fetch_object)
PHP_FALIAS(mssql_data_seek, sybase_data_seek, arginfo_sybase_data_seek)
PHP_FALIAS(mssql_fetch_field, sybase_fetch_field, arginfo_sybase_fetch_field)
PHP_FALIAS(mssql_field_seek, sybase_field_seek, arginfo_sybase_field_seek)
PHP_FALIAS(mssql_result, sybase_result, arginfo_sybase_result)
PHP_FALIAS(mssql_affected_rows, sybase_affected_rows, arginfo_sybase_affected_rows)
PHP_FALIAS(mssql_min_client_severity, sybase_min_client_severity, arginfo_sybase_min_client_severity)
PHP_FALIAS(mssql_min_server_severity, sybase_min_server_severity, arginfo_sybase_min_server_severity)
PHP_FALIAS(mssql_set_message_handler, sybase_set_message_handler, arginfo_sybase_set_message_handler)
PHP_FALIAS(mssql_deadlock_retry_count, sybase_deadlock_retry_count, arginfo_sybase_deadlock_retry_count)
#endif
PHP_FE_END
};
zend_module_entry sybase_module_entry = {
STANDARD_MODULE_HEADER,
"sybase_ct",
sybase_functions,
PHP_MINIT(sybase),
PHP_MSHUTDOWN(sybase),
PHP_RINIT(sybase),
PHP_RSHUTDOWN(sybase),
PHP_MINFO(sybase),
NO_VERSION_YET,
PHP_MODULE_GLOBALS(sybase),
PHP_GINIT(sybase),
PHP_GSHUTDOWN(sybase),
NULL,
STANDARD_MODULE_PROPERTIES_EX
};
/* static CS_CONTEXT *context; */
#ifdef COMPILE_DL_SYBASE_CT
ZEND_GET_MODULE(sybase)
#endif
ZEND_DECLARE_MODULE_GLOBALS(sybase)
#define CHECK_LINK(link) { if (link==-1) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: A link to the server could not be established"); RETURN_FALSE; } }
static int _clean_invalid_results(zend_rsrc_list_entry *le TSRMLS_DC)
{
if (Z_TYPE_P(le) == le_result) {
sybase_link *sybase_ptr = ((sybase_result *) le->ptr)->sybase_ptr;
if (!sybase_ptr->valid) {
return 1;
}
}
return 0;
}
#define efree_n(x) { efree(x); x = NULL; }
#define efree_if(x) if (x) efree_n(x)
#ifdef PHP_SYBASE_DEBUG
#define FREE_SYBASE_RESULT(result) \
if (result) { \
fprintf(stderr, "_free_sybase_result(%p) called from line #%d\n", result, __LINE__); \
fflush(stderr); \
_free_sybase_result(result); \
result = NULL; \
}
#else
#define FREE_SYBASE_RESULT(result) \
if (result) { \
_free_sybase_result(result); \
result = NULL; \
}
#endif
static void _free_sybase_result(sybase_result *result)
{
int i, j;
if (result->data) {
for (i = 0; i < (result->store ? result->num_rows : MIN(1, result->num_rows)); i++) {
for (j=0; j<result->num_fields; j++) {
zval_dtor(&result->data[i][j]);
}
efree(result->data[i]);
}
efree(result->data);
}
if (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);
}
if (result->tmp_buffer) {
for (i=0; i<result->num_fields; i++) {
efree(result->tmp_buffer[i]);
}
efree(result->tmp_buffer);
}
efree_if(result->lengths);
efree_if(result->indicators);
efree_if(result->datafmt);
efree_if(result->numerics);
efree_if(result->types);
efree(result);
}
/* Forward declaration */
static int php_sybase_finish_results (sybase_result *result TSRMLS_DC);
static void php_free_sybase_result(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
sybase_result *result = (sybase_result *)rsrc->ptr;
/* Check to see if we've read all rows */
if (result->sybase_ptr && result->sybase_ptr->active_result_index) {
if (result->sybase_ptr->cmd) {
ct_cancel(NULL, result->sybase_ptr->cmd, CS_CANCEL_ALL);
}
php_sybase_finish_results(result TSRMLS_CC);
}
FREE_SYBASE_RESULT(result);
}
static void _close_sybase_link(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
sybase_link *sybase_ptr = (sybase_link *)rsrc->ptr;
CS_INT con_status;
sybase_ptr->valid = 0;
if (sybase_ptr->callback_name != NULL) {
zval_ptr_dtor(&sybase_ptr->callback_name);
sybase_ptr->callback_name= NULL;
}
zend_hash_apply(&EG(regular_list), (apply_func_t) _clean_invalid_results TSRMLS_CC);
/* Non-persistent connections will always be connected or we wouldn't
* get here, but since we want to check the death status anyway
* we might as well double-check the connect status.
*/
if (ct_con_props(sybase_ptr->connection, CS_GET, CS_CON_STATUS,
&con_status, CS_UNUSED, NULL)!=CS_SUCCEED) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Unable to get connection status on close");
/* Assume the worst. */
con_status = CS_CONSTAT_CONNECTED | CS_CONSTAT_DEAD;
}
if (con_status & CS_CONSTAT_CONNECTED) {
if ((con_status & CS_CONSTAT_DEAD) || ct_close(sybase_ptr->connection, CS_UNUSED)!=CS_SUCCEED) {
ct_close(sybase_ptr->connection, CS_FORCE_CLOSE);
}
}
ct_cmd_drop(sybase_ptr->cmd);
ct_con_drop(sybase_ptr->connection);
efree(sybase_ptr);
SybCtG(num_links)--;
}
static void _close_sybase_plink(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
sybase_link *sybase_ptr = (sybase_link *)rsrc->ptr;
CS_INT con_status;
/* Persistent connections may have been closed before a failed
* reopen attempt.
*/
if (ct_con_props(sybase_ptr->connection, CS_GET, CS_CON_STATUS,
&con_status, CS_UNUSED, NULL)!=CS_SUCCEED) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Unable to get connection status on close");
/* Assume the worst. */
con_status = CS_CONSTAT_CONNECTED | CS_CONSTAT_DEAD;
}
if (con_status & CS_CONSTAT_CONNECTED) {
if ((con_status & CS_CONSTAT_DEAD) || ct_close(sybase_ptr->connection, CS_UNUSED)!=CS_SUCCEED) {
ct_close(sybase_ptr->connection, CS_FORCE_CLOSE);
}
}
ct_con_drop(sybase_ptr->connection);
free(sybase_ptr);
SybCtG(num_persistent)--;
SybCtG(num_links)--;
}
static CS_RETCODE CS_PUBLIC _client_message_handler(CS_CONTEXT *context, CS_CONNECTION *connection, CS_CLIENTMSG *errmsg)
{
TSRMLS_FETCH();
if (CS_SEVERITY(errmsg->msgnumber) >= SybCtG(min_client_severity)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Client message: %s (severity %ld)", errmsg->msgstring, (long)CS_SEVERITY(errmsg->msgnumber));
}
STR_FREE(SybCtG(server_message));
SybCtG(server_message) = estrdup(errmsg->msgstring);
/* If this is a timeout message, return CS_FAIL to cancel the
* operation and mark the connection as dead.
*/
if (CS_SEVERITY(errmsg->msgnumber) == CS_SV_RETRY_FAIL &&
CS_NUMBER(errmsg->msgnumber) == 63 &&
CS_ORIGIN(errmsg->msgnumber) == 2 &&
CS_LAYER(errmsg->msgnumber) == 1)
{
return CS_FAIL;
}
return CS_SUCCEED;
}
static int _call_message_handler(zval *callback_name, CS_SERVERMSG *srvmsg TSRMLS_DC)
{
int handled = 0;
zval *msgnumber, *severity, *state, *line, *text, *retval = NULL;
zval **args[5];
/* Border case - empty fcall */
if (NULL == callback_name) return 0;
/* Build arguments */
MAKE_STD_ZVAL(msgnumber);
ZVAL_LONG(msgnumber, srvmsg->msgnumber);
args[0] = &msgnumber;
MAKE_STD_ZVAL(severity);
ZVAL_LONG(severity, srvmsg->severity);
args[1] = &severity;
MAKE_STD_ZVAL(state);
ZVAL_LONG(state, srvmsg->state);
args[2] = &state;
MAKE_STD_ZVAL(line);
ZVAL_LONG(line, srvmsg->line);
args[3] = &line;
MAKE_STD_ZVAL(text);
ZVAL_STRING(text, srvmsg->text, 1);
args[4] = &text;
if (call_user_function_ex(EG(function_table), NULL, callback_name, &retval, 5, args, 0, NULL TSRMLS_CC) == FAILURE) {
zval expr_copy;
int use_copy;
zend_make_printable_zval(callback_name, &expr_copy, &use_copy);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Cannot call the messagehandler %s", Z_STRVAL(expr_copy));
zval_dtor(&expr_copy);
}
if (retval) {
handled = ((Z_TYPE_P(retval) != IS_BOOL) || (Z_BVAL_P(retval) != 0));
zval_ptr_dtor(&retval);
} else {
handled = 0;
}
zval_ptr_dtor(&msgnumber);
zval_ptr_dtor(&severity);
zval_ptr_dtor(&state);
zval_ptr_dtor(&line);
zval_ptr_dtor(&text);
return handled;
}
static CS_RETCODE CS_PUBLIC _server_message_handler(CS_CONTEXT *context, CS_CONNECTION *connection, CS_SERVERMSG *srvmsg)
{
sybase_link *sybase;
int handled = 0;
TSRMLS_FETCH();
/* Remember the last server message in any case */
STR_FREE(SybCtG(server_message));
SybCtG(server_message) = estrdup(srvmsg->text);
/* Retrieve sybase link */
if (ct_con_props(connection, CS_GET, CS_USERDATA, &sybase, CS_SIZEOF(sybase), NULL) != CS_SUCCEED) {
sybase = NULL;
}
/* If this is a deadlock message, set the connection's deadlock flag
* so we will retry the request. Sorry about the bare constant here,
* but it's not defined anywhere and it's a "well-known" number.
*/
if (sybase && (srvmsg->msgnumber == 1205)) {
sybase->deadlock = 1;
}
/* Check mininum server severity level */
if (srvmsg->severity < SybCtG(min_server_severity)) {
return CS_SUCCEED;
}
/* Call global message handler */
handled = handled | _call_message_handler(SybCtG(callback_name), srvmsg TSRMLS_CC);
/* Call link specific message handler */
if (sybase) {
handled = handled | _call_message_handler(sybase->callback_name, srvmsg TSRMLS_CC);
}
/* Spit out a warning if neither of them has handled this message */
if (!handled) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Server message: %s (severity %ld, procedure %s)",
srvmsg->text, (long)srvmsg->severity, ((srvmsg->proclen>0) ? srvmsg->proc : "N/A"));
}
return CS_SUCCEED;
}
PHP_INI_BEGIN()
STD_PHP_INI_BOOLEAN("sybct.allow_persistent", "1", PHP_INI_SYSTEM, OnUpdateLong, allow_persistent, zend_sybase_globals, sybase_globals)
STD_PHP_INI_ENTRY_EX("sybct.max_persistent", "-1", PHP_INI_SYSTEM, OnUpdateLong, max_persistent, zend_sybase_globals, sybase_globals, display_link_numbers)
STD_PHP_INI_ENTRY_EX("sybct.max_links", "-1", PHP_INI_SYSTEM, OnUpdateLong, max_links, zend_sybase_globals, sybase_globals, display_link_numbers)
STD_PHP_INI_ENTRY("sybct.min_server_severity", "10", PHP_INI_ALL, OnUpdateLong, min_server_severity, zend_sybase_globals, sybase_globals)
STD_PHP_INI_ENTRY("sybct.min_client_severity", "10", PHP_INI_ALL, OnUpdateLong, min_client_severity, zend_sybase_globals, sybase_globals)
STD_PHP_INI_ENTRY("sybct.login_timeout", "-1", PHP_INI_ALL, OnUpdateLong, login_timeout, zend_sybase_globals, sybase_globals)
STD_PHP_INI_ENTRY("sybct.hostname", NULL, PHP_INI_ALL, OnUpdateString, hostname, zend_sybase_globals, sybase_globals)
STD_PHP_INI_ENTRY_EX("sybct.deadlock_retry_count", "0", PHP_INI_ALL, OnUpdateLong, deadlock_retry_count, zend_sybase_globals, sybase_globals, display_link_numbers)
PHP_INI_END()
static PHP_GINIT_FUNCTION(sybase)
{
long opt;
if (cs_ctx_alloc(CTLIB_VERSION, &sybase_globals->context)!=CS_SUCCEED || ct_init(sybase_globals->context, CTLIB_VERSION)!=CS_SUCCEED) {
return;
}
/* Initialize message handlers */
if (ct_callback(sybase_globals->context, NULL, CS_SET, CS_SERVERMSG_CB, (CS_VOID *)_server_message_handler)!=CS_SUCCEED) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Unable to set server message handler");
}
if (ct_callback(sybase_globals->context, NULL, CS_SET, CS_CLIENTMSG_CB, (CS_VOID *)_client_message_handler)!=CS_SUCCEED) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Unable to set client message handler");
}
/* Set datetime conversion format to "Nov 3 1998 8:06PM".
* This is the default format for the ct-lib that comes with
* Sybase ASE 11.5.1 for Solaris, but the Linux libraries that
* come with 11.0.3.3 default to "03/11/98" which is singularly
* useless. This levels the playing field for all platforms.
*/
{
CS_INT dt_convfmt = CS_DATES_SHORT;
if (cs_dt_info(sybase_globals->context, CS_SET, NULL, CS_DT_CONVFMT, CS_UNUSED, &dt_convfmt, sizeof(dt_convfmt), NULL)!=CS_SUCCEED) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Unable to set datetime conversion format");
}
}
/* Set the timeout, which is per context and can't be set with
* ct_con_props(), so set it globally from the config value if
* requested. The default is CS_NO_LIMIT.
*
* Note that despite some noise in the documentation about using
* signals to implement timeouts, they are actually implemented
* by using poll() or select() on Solaris and Linux.
*/
if (cfg_get_long("sybct.timeout", &opt)==SUCCESS) {
CS_INT cs_timeout = opt;
if (ct_config(sybase_globals->context, CS_SET, CS_TIMEOUT, &cs_timeout, CS_UNUSED, NULL)!=CS_SUCCEED) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Unable to update the timeout");
}
}
sybase_globals->num_persistent=0;
sybase_globals->callback_name = NULL;
}
static PHP_GSHUTDOWN_FUNCTION(sybase)
{
ct_exit(sybase_globals->context, CS_UNUSED);
cs_ctx_drop(sybase_globals->context);
}
PHP_MINIT_FUNCTION(sybase)
{
REGISTER_INI_ENTRIES();
le_link = zend_register_list_destructors_ex(_close_sybase_link, NULL, "sybase-ct link", module_number);
le_plink = zend_register_list_destructors_ex(NULL, _close_sybase_plink, "sybase-ct link persistent", module_number);
le_result = zend_register_list_destructors_ex(php_free_sybase_result, NULL, "sybase-ct result", module_number);
return SUCCESS;
}
PHP_RINIT_FUNCTION(sybase)
{
SybCtG(default_link)=-1;
SybCtG(num_links) = SybCtG(num_persistent);
SybCtG(appname) = estrndup("PHP " PHP_VERSION, sizeof("PHP " PHP_VERSION));
SybCtG(server_message) = STR_EMPTY_ALLOC();
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(sybase)
{
UNREGISTER_INI_ENTRIES();
#if 0
ct_exit(context, CS_UNUSED);
cs_ctx_drop(context);
#endif
return SUCCESS;
}
PHP_RSHUTDOWN_FUNCTION(sybase)
{
efree(SybCtG(appname));
SybCtG(appname) = NULL;
if (SybCtG(callback_name)) {
zval_ptr_dtor(&SybCtG(callback_name));
SybCtG(callback_name)= NULL;
}
STR_FREE(SybCtG(server_message));
SybCtG(server_message) = NULL;
return SUCCESS;
}
static int php_sybase_do_connect_internal(sybase_link *sybase, char *host, char *user, char *passwd, char *charset, char *appname TSRMLS_DC)
{
CS_LOCALE *tmp_locale;
long packetsize;
/* set a CS_CONNECTION record */
if (ct_con_alloc(SybCtG(context), &sybase->connection)!=CS_SUCCEED) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Unable to allocate connection record");
return 0;
}
/* Note - this saves a copy of sybase, not a pointer to it. */
if (ct_con_props(sybase->connection, CS_SET, CS_USERDATA, &sybase, CS_SIZEOF(sybase), NULL)!=CS_SUCCEED) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Unable to set userdata");
ct_con_drop(sybase->connection);
return 0;
}
if (user) {
ct_con_props(sybase->connection, CS_SET, CS_USERNAME, user, CS_NULLTERM, NULL);
}
if (passwd) {
ct_con_props(sybase->connection, CS_SET, CS_PASSWORD, passwd, CS_NULLTERM, NULL);
}
if (appname) {
ct_con_props(sybase->connection, CS_SET, CS_APPNAME, appname, CS_NULLTERM, NULL);
} else {
ct_con_props(sybase->connection, CS_SET, CS_APPNAME, SybCtG(appname), CS_NULLTERM, NULL);
}
if (SybCtG(hostname)) {
ct_con_props(sybase->connection, CS_SET, CS_HOSTNAME, SybCtG(hostname), CS_NULLTERM, NULL);
}
if (charset) {
if (cs_loc_alloc(SybCtG(context), &tmp_locale)!=CS_SUCCEED) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Unable to allocate locale information");
} else {
if (cs_locale(SybCtG(context), CS_SET, tmp_locale, CS_LC_ALL, NULL, CS_NULLTERM, NULL)!=CS_SUCCEED) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Unable to load default locale data");
} else {
if (cs_locale(SybCtG(context), CS_SET, tmp_locale, CS_SYB_CHARSET, charset, CS_NULLTERM, NULL)!=CS_SUCCEED) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Unable to update character set");
} else {
if (ct_con_props(sybase->connection, CS_SET, CS_LOC_PROP, tmp_locale, CS_UNUSED, NULL)!=CS_SUCCEED) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Unable to update connection properties");
}
}
}
}
}
if (cfg_get_long("sybct.packet_size", &packetsize) == SUCCESS) {
if (ct_con_props(sybase->connection, CS_SET, CS_PACKETSIZE, (CS_VOID *)&packetsize, CS_UNUSED, NULL) != CS_SUCCEED) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Unable to update connection packetsize");
}
}
/* Set the login timeout. Actually, the login timeout is per context
* and not per connection, but we will update the context here to
* allow for code such as the following:
*
* ini_set('sybct.login_timeout', $timeout);
* sybase_connect(...)
*
* Note that preceding calls to sybase_connect() will now use the
* updated value and not the default one!
*
* The default value for CS_LOGIN_TIMEOUT is 60 (1 minute).
*/
if (SybCtG(login_timeout) != -1) {
CS_INT cs_login_timeout = SybCtG(login_timeout);
if (ct_config(SybCtG(context), CS_SET, CS_LOGIN_TIMEOUT, &cs_login_timeout, CS_UNUSED, NULL)!=CS_SUCCEED) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Unable to update the login timeout");
}
}
sybase->valid = 1;
sybase->dead = 0;
sybase->active_result_index = 0;
sybase->callback_name = NULL;
/* create the link */
if (ct_connect(sybase->connection, host, CS_NULLTERM)!=CS_SUCCEED) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Unable to connect");
ct_con_drop(sybase->connection);
return 0;
}
if (ct_cmd_alloc(sybase->connection, &sybase->cmd)!=CS_SUCCEED) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Unable to allocate command record");
ct_close(sybase->connection, CS_UNUSED);
ct_con_drop(sybase->connection);
return 0;
}
return 1;
}
static void php_sybase_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
{
char *user = NULL, *passwd = NULL, *host = NULL, *charset = NULL, *appname = NULL;
char *hashed_details;
int hashed_details_length, len;
zend_bool new = 0;
sybase_link *sybase_ptr;
host= user= passwd= charset= appname= NULL;
if (persistent) {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!s!s!s!s!", &host, &len, &user, &len, &passwd, &len, &charset, &len, &appname, &len) == FAILURE) {
return;
}
} else {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!s!s!s!s!b", &host, &len, &user, &len, &passwd, &len, &charset, &len, &appname, &len, &new) == FAILURE) {
return;
}
}
hashed_details_length = spprintf(
&hashed_details,
0,
"sybase_%s_%s_%s_%s_%s",
host ? host : "",
user ? user : "",
passwd ? passwd : "",
charset ? charset : "",
appname ? appname : ""
);
if (!SybCtG(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 (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 (SybCtG(max_links)!=-1 && SybCtG(num_links)>=SybCtG(max_links)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Too many open links (%ld)", SybCtG(num_links));
efree(hashed_details);
RETURN_FALSE;
}
if (SybCtG(max_persistent)!=-1 && SybCtG(num_persistent)>=SybCtG(max_persistent)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Too many open persistent links (%ld)", SybCtG(num_persistent));
efree(hashed_details);
RETURN_FALSE;
}
sybase_ptr = (sybase_link *) malloc(sizeof(sybase_link));
if (!sybase_ptr) {
efree(hashed_details);
RETURN_FALSE;
}
if (!php_sybase_do_connect_internal(sybase_ptr, host, user, passwd, charset, appname TSRMLS_CC)) {
free(sybase_ptr);
efree(hashed_details);
RETURN_FALSE;
}
/* hash it up */
Z_TYPE(new_le) = le_plink;
new_le.ptr = sybase_ptr;
if (zend_hash_update(&EG(persistent_list), hashed_details, hashed_details_length+1, (void *) &new_le, sizeof(zend_rsrc_list_entry), NULL)==FAILURE) {
ct_close(sybase_ptr->connection, CS_UNUSED);
ct_con_drop(sybase_ptr->connection);
free(sybase_ptr);
efree(hashed_details);
RETURN_FALSE;
}
SybCtG(num_persistent)++;
SybCtG(num_links)++;
} else { /* we do */
CS_INT con_status;
if (Z_TYPE_P(le) != le_plink) {
efree(hashed_details);
RETURN_FALSE;
}
sybase_ptr = (sybase_link *) le->ptr;
/* If the link has died, close it and overwrite it with a new one. */
if (ct_con_props(sybase_ptr->connection, CS_GET, CS_CON_STATUS,
&con_status, CS_UNUSED, NULL)!=CS_SUCCEED) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Unable to get connection status");
efree(hashed_details);
RETURN_FALSE;
}
if (!(con_status & CS_CONSTAT_CONNECTED) || (con_status & CS_CONSTAT_DEAD) || sybase_ptr->dead) {
sybase_link sybase;
if (con_status & CS_CONSTAT_CONNECTED) {
ct_close(sybase_ptr->connection, CS_FORCE_CLOSE);
}
/* Create a new connection, then replace the old
* connection. If we fail to create a new connection,
* put the old one back so there will be a connection,
* even if it is a non-functional one. This is because
* code may still be holding an id for this connection
* so we can't free the CS_CONNECTION.
* (This is actually totally hokey, it would be better
* to just ct_con_drop() the connection and set
* sybase_ptr->connection to NULL, then test it for
* NULL before trying to use it elsewhere . . .)
*/
memcpy(&sybase, sybase_ptr, sizeof(sybase_link));
if (!php_sybase_do_connect_internal(sybase_ptr, host, user, passwd, charset, appname TSRMLS_CC)) {
memcpy(sybase_ptr, &sybase, sizeof(sybase_link));
efree(hashed_details);
RETURN_FALSE;
}
ct_con_drop(sybase.connection); /* drop old connection */
}
}
ZEND_REGISTER_RESOURCE(return_value, sybase_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 sybase link sits.
* if it doesn't, open a new sybase link, add it to the resource list,
* and add a pointer to it with hashed_details as the key.
*/
if (!new && 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);
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_LVAL_P(return_value) = SybCtG(default_link) = link;
Z_TYPE_P(return_value) = IS_RESOURCE;
efree(hashed_details);
return;
} else {
zend_hash_del(&EG(regular_list), hashed_details, hashed_details_length+1);
}
}
if (SybCtG(max_links)!=-1 && SybCtG(num_links)>=SybCtG(max_links)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: Too many open links (%ld)", SybCtG(num_links));
efree(hashed_details);
RETURN_FALSE;
}
sybase_ptr = (sybase_link *) emalloc(sizeof(sybase_link));
if (!php_sybase_do_connect_internal(sybase_ptr, host, user, passwd, charset, appname TSRMLS_CC)) {
efree(sybase_ptr);
efree(hashed_details);
RETURN_FALSE;
}
/* add it to the list */
ZEND_REGISTER_RESOURCE(return_value, sybase_ptr, le_link);
/* add it to the hash */
new_index_ptr.ptr = (void *) Z_LVAL_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) {
ct_close(sybase_ptr->connection, CS_UNUSED);
ct_con_drop(sybase_ptr->connection);
efree(sybase_ptr);
efree(hashed_details);
RETURN_FALSE;
}
SybCtG(num_links)++;
}
efree(hashed_details);
SybCtG(default_link)=Z_LVAL_P(return_value);
zend_list_addref(SybCtG(default_link));
}
static int php_sybase_get_default_link(INTERNAL_FUNCTION_PARAMETERS)
{
if (SybCtG(default_link)==-1) { /* no link opened yet, implicitly open one */
ht = 0;
php_sybase_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
}
return SybCtG(default_link);
}
/* {{{ proto int sybase_connect([string host [, string user [, string password [, string charset [, string appname [, bool new]]]]]])
Open Sybase server connection */
PHP_FUNCTION(sybase_connect)
{
php_sybase_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
}
/* }}} */
/* {{{ proto int sybase_pconnect([string host [, string user [, string password [, string charset [, string appname]]]]])
Open persistent Sybase connection */
PHP_FUNCTION(sybase_pconnect)
{
php_sybase_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
}
/* }}} */
inline static int php_sybase_connection_id(zval *sybase_link_index, int *id TSRMLS_DC)
{
if (NULL == sybase_link_index) {
if (-1 == SybCtG(default_link)) {
return FAILURE;
}
*id = SybCtG(default_link);
} else {
*id = -1; /* explicit resource number */
}
return SUCCESS;
}
/* {{{ proto bool sybase_close([resource link_id])
Close Sybase connection */
PHP_FUNCTION(sybase_close)
{
zval *sybase_link_index = NULL;
sybase_link *sybase_ptr;
int id;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &sybase_link_index) == FAILURE) {
return;
}
if (php_sybase_connection_id(sybase_link_index, &id TSRMLS_CC) == FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sybase: No connection to close");
RETURN_FALSE;
}
ZEND_FETCH_RESOURCE2(sybase_ptr, sybase_link *, &sybase_link_index, id, "Sybase-Link", le_link, le_plink);
if (id == -1) {
zend_list_delete(Z_RESVAL_P(sybase_link_index));
}
if (id != -1 || (sybase_link_index && Z_RESVAL_P(sybase_link_index) == SybCtG(default_link))) {
zend_list_delete(SybCtG(default_link));
SybCtG(default_link) = -1;
}
RETURN_TRUE;
}
/* }}} */
static int exec_cmd(sybase_link *sybase_ptr, char *cmdbuf)
{
CS_RETCODE retcode;
CS_INT restype;
int failure=0;
/* Fail if we already marked this connection dead. */
if (sybase_ptr->dead) {
return FAILURE;
}
/*
** Get a command handle, store the command string in it, and
** send it to the server.
*/
if (ct_command(sybase_ptr->cmd, CS_LANG_CMD, cmdbuf, CS_NULLTERM, CS_UNUSED)!=CS_SUCCEED) {