-
Notifications
You must be signed in to change notification settings - Fork 4
/
oci8.c
2544 lines (2158 loc) · 80.8 KB
/
oci8.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
/*
+----------------------------------------------------------------------+
| Copyright (c) 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: |
| https://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 |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Stig Sæther Bakken <ssb@php.net> |
| Thies C. Arntzen <thies@thieso.net> |
| Maxim Maletsky <maxim@maxim.cx> |
| |
| Collection support by Andy Sautins <asautins@veripost.net> |
| Temporary LOB support by David Benson <dbenson@mancala.com> |
| ZTS per process OCIPLogon by Harald Radi <harald.radi@nme.at> |
| |
| Redesigned by: Antony Dovgal <antony@zend.com> |
| Andi Gutmans <andi@php.net> |
| Wez Furlong <wez@omniti.com> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "ext/standard/info.h"
#include "php_ini.h"
#include "zend_attributes.h"
#include "zend_smart_str.h"
#ifdef HAVE_OCI8
/* Note to maintainers: config.m4 should/does check the minimum PHP version so
* the below checks are obsolete - but are kept 'just in case'. Also bump the
* minimum version in package.xml, as appropriate. */
/* PHP 5.2 is the minimum supported version for OCI8 2.0 */
#if PHP_MAJOR_VERSION < 5 || (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION <= 1)
#error Use PHP OCI8 1.4 for your version of PHP
#elif PHP_MAJOR_VERSION < 7
/* PHP 7 is the minimum supported version for OCI8 2.1 */
#error Use PHP OCI8 2.0 for your version of PHP
#elif PHP_MAJOR_VERSION < 8
/* PHP 8 is the minimum supported version for OCI8 3 */
#error Use PHP OCI8 2.2 for your version of PHP
#elif PHP_MAJOR_VERSION == 8 && PHP_MINOR_VERSION < 1
#error Use PHP OCI8 3.0 for your version of PHP
#elif PHP_MAJOR_VERSION == 8 && PHP_MINOR_VERSION < 2
#error Use PHP OCI8 3.2 for your version of PHP
#endif
#include "php_oci8.h"
#include "php_oci8_int.h"
#include "zend_hash.h"
ZEND_DECLARE_MODULE_GLOBALS(oci)
static PHP_GINIT_FUNCTION(oci);
static PHP_GSHUTDOWN_FUNCTION(oci);
/* Allow PHP 5.3 branch to be used in PECL for 5.x compatible builds */
#ifndef Z_ADDREF_P
#define Z_ADDREF_P(x) ZVAL_ADDREF(x)
#endif
/* For a user friendly message about environment setup */
#if defined(PHP_WIN32)
#define PHP_OCI8_LIB_PATH_MSG "PATH"
#elif defined(__APPLE__)
#define PHP_OCI8_LIB_PATH_MSG "DYLD_LIBRARY_PATH"
#elif defined(_AIX)
#define PHP_OCI8_LIB_PATH_MSG "LIBPATH"
#elif defined(__hpux)
#define PHP_OCI8_LIB_PATH_MSG "SHLIB_PATH"
#else
#define PHP_OCI8_LIB_PATH_MSG "LD_LIBRARY_PATH"
#endif
/* True globals, no need for thread safety */
int le_connection;
int le_pconnection;
int le_statement;
int le_descriptor;
int le_psessionpool;
int le_collection;
zend_class_entry *oci_lob_class_entry_ptr;
zend_class_entry *oci_coll_class_entry_ptr;
#ifndef SQLT_BFILEE
#define SQLT_BFILEE 114
#endif
#ifndef SQLT_CFILEE
#define SQLT_CFILEE 115
#endif
#ifdef ZTS
#define PHP_OCI_INIT_MODE (OCI_DEFAULT | OCI_OBJECT | OCI_THREADED | OCI_NO_MUTEX)
#else
#define PHP_OCI_INIT_MODE (OCI_DEFAULT | OCI_OBJECT)
#endif
/* {{{ static protos */
static void php_oci_connection_list_dtor (zend_resource *);
static void php_oci_pconnection_list_dtor (zend_resource *);
static void php_oci_pconnection_list_np_dtor (zend_resource *);
static void php_oci_statement_list_dtor (zend_resource *);
static void php_oci_descriptor_list_dtor (zend_resource *);
static void php_oci_spool_list_dtor(zend_resource *entry);
static void php_oci_collection_list_dtor (zend_resource *);
static int php_oci_persistent_helper(zval *zv);
static int php_oci_connection_ping(php_oci_connection *);
static int php_oci_connection_status(php_oci_connection *);
static int php_oci_connection_close(php_oci_connection *);
static void php_oci_spool_close(php_oci_spool *session_pool);
static OCIEnv *php_oci_create_env(ub2 charsetid);
static int php_oci_create_session(php_oci_connection *connection, php_oci_spool *session_pool, char *dbname, int dbname_len, char *username, int username_len, char *password, int password_len, char *new_password, int new_password_len, int session_mode);
static int php_oci_old_create_session(php_oci_connection *connection, char *dbname, int dbname_len, char *username, int username_len, char *password, int password_len, char *new_password, int new_password_len, int session_mode);
static php_oci_spool *php_oci_get_spool(char *username, int username_len, char *password, int password_len, char *dbname, int dbname_len, int charsetid, int mode);
static php_oci_spool *php_oci_create_spool(char *username, int username_len, char *password, int password_len, char *dbname, int dbname_len, zend_string *hash_key, int charsetid, int mode);
static sword php_oci_ping_init(php_oci_connection *connection, OCIError *errh);
/* }}} */
/* {{{ dynamically loadable module stuff */
#if defined(COMPILE_DL_OCI8) || defined(COMPILE_DL_OCI8_11G) || defined(COMPILE_DL_OCI8_12C) || defined(COMPILE_DL_OCI8_19)
ZEND_GET_MODULE(oci8)
#endif /* COMPILE_DL */
/* }}} */
#include "oci8_arginfo.h"
static PHP_INI_MH(OnUpdateOldCloseSemantics)
{
bool *p = (bool *) ZEND_INI_GET_ADDR();
*p = zend_ini_parse_bool(new_value);
if (*p) {
zend_error(E_DEPRECATED, "Directive oci8.old_oci_close_semantics is deprecated");
}
return SUCCESS;
}
/* {{{ extension definition structures */
zend_module_entry oci8_module_entry = {
STANDARD_MODULE_HEADER,
"oci8", /* extension name */
ext_functions, /* extension function list */
PHP_MINIT(oci), /* extension-wide startup function */
PHP_MSHUTDOWN(oci), /* extension-wide shutdown function */
PHP_RINIT(oci), /* per-request startup function */
PHP_RSHUTDOWN(oci), /* per-request shutdown function */
PHP_MINFO(oci), /* information function */
PHP_OCI8_VERSION,
PHP_MODULE_GLOBALS(oci), /* globals descriptor */
PHP_GINIT(oci), /* globals ctor */
PHP_GSHUTDOWN(oci), /* globals dtor */
NULL, /* post deactivate */
STANDARD_MODULE_PROPERTIES_EX
};
/* }}} */
/* {{{ PHP_INI */
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY( "oci8.max_persistent", "-1", PHP_INI_SYSTEM, OnUpdateLong, max_persistent, zend_oci_globals, oci_globals)
STD_PHP_INI_ENTRY( "oci8.persistent_timeout", "-1", PHP_INI_SYSTEM, OnUpdateLong, persistent_timeout, zend_oci_globals, oci_globals)
STD_PHP_INI_ENTRY( "oci8.ping_interval", "60", PHP_INI_SYSTEM, OnUpdateLong, ping_interval, zend_oci_globals, oci_globals)
STD_PHP_INI_BOOLEAN("oci8.privileged_connect", "0", PHP_INI_SYSTEM, OnUpdateBool, privileged_connect, zend_oci_globals, oci_globals)
STD_PHP_INI_ENTRY( "oci8.statement_cache_size", "20", PHP_INI_SYSTEM, OnUpdateLong, statement_cache_size, zend_oci_globals, oci_globals)
STD_PHP_INI_ENTRY( "oci8.default_prefetch", "100", PHP_INI_SYSTEM, OnUpdateLong, default_prefetch, zend_oci_globals, oci_globals)
STD_PHP_INI_BOOLEAN("oci8.old_oci_close_semantics", "0", PHP_INI_SYSTEM, OnUpdateOldCloseSemantics, old_oci_close_semantics,zend_oci_globals, oci_globals)
STD_PHP_INI_ENTRY( "oci8.connection_class", "", PHP_INI_ALL, OnUpdateString, connection_class, zend_oci_globals, oci_globals)
STD_PHP_INI_BOOLEAN("oci8.events", "0", PHP_INI_SYSTEM, OnUpdateBool, events, zend_oci_globals, oci_globals)
STD_PHP_INI_ENTRY( "oci8.prefetch_lob_size", "0", PHP_INI_SYSTEM, OnUpdateLong, prefetch_lob_size, zend_oci_globals, oci_globals)
PHP_INI_END()
/* }}} */
/* {{{ startup, shutdown and info functions */
/* {{{ php_oci_init_global_handles()
*
* Initialize global handles only when they are needed
*/
static void php_oci_init_global_handles(void)
{
sword errstatus;
sb4 ora_error_code = 0;
text tmp_buf[PHP_OCI_ERRBUF_LEN]; /* Use traditional smaller size: non-PL/SQL errors should fit and it keeps the stack smaller */
errstatus = OCIEnvNlsCreate(&OCI_G(env), PHP_OCI_INIT_MODE, 0, NULL, NULL, NULL, 0, NULL, 0, 0);
if (errstatus == OCI_ERROR) {
#ifdef HAVE_OCI_INSTANT_CLIENT
php_error_docref(NULL, E_WARNING, "OCIEnvNlsCreate() failed. There is something wrong with your system - please check that " PHP_OCI8_LIB_PATH_MSG " includes the directory with Oracle Instant Client libraries");
#else
php_error_docref(NULL, E_WARNING, "OCIEnvNlsCreate() failed. There is something wrong with your system - please check that ORACLE_HOME and " PHP_OCI8_LIB_PATH_MSG " are set and point to the right directories");
#endif
if (OCI_G(env)
&& OCIErrorGet(OCI_G(env), (ub4)1, NULL, &ora_error_code, tmp_buf, (ub4)PHP_OCI_ERRBUF_LEN, (ub4)OCI_HTYPE_ENV) == OCI_SUCCESS
&& *tmp_buf) {
php_error_docref(NULL, E_WARNING, "%s", tmp_buf);
}
OCI_G(env) = NULL;
OCI_G(err) = NULL;
return;
}
errstatus = OCIHandleAlloc (OCI_G(env), (dvoid **)&OCI_G(err), OCI_HTYPE_ERROR, 0, NULL);
if (errstatus != OCI_SUCCESS) {
OCIErrorGet(OCI_G(env), (ub4)1, NULL, &ora_error_code, tmp_buf, (ub4)PHP_OCI_ERRBUF_LEN, (ub4)OCI_HTYPE_ERROR);
if (ora_error_code) {
int tmp_buf_len = (int) strlen((char *)tmp_buf);
if (tmp_buf_len > 0 && tmp_buf[tmp_buf_len - 1] == '\n') {
tmp_buf[tmp_buf_len - 1] = '\0';
}
if (errstatus == OCI_SUCCESS_WITH_INFO) {
php_error_docref(NULL, E_WARNING, "Initialization error: OCI_SUCCESS_WITH_INFO: %s", tmp_buf);
} else {
php_error_docref(NULL, E_WARNING, "Initialization error: OCI_ERROR: %s", tmp_buf);
OCIHandleFree((dvoid *) OCI_G(env), OCI_HTYPE_ENV);
OCI_G(env) = NULL;
OCI_G(err) = NULL;
}
}
}
}
/* }}} */
/* {{{ PHP_GINIT_FUNCTION
*
* Zerofill globals during module init
*/
static PHP_GINIT_FUNCTION(oci)
{
memset(oci_globals, 0, sizeof(zend_oci_globals));
}
/* }}} */
/* {{{ PHP_GSHUTDOWN_FUNCTION
*
* Called for thread shutdown in ZTS, after module shutdown for non-ZTS
* Free global handles (if they were initialized before)
*/
static PHP_GSHUTDOWN_FUNCTION(oci)
{
if (oci_globals->err) {
oci_globals->in_call = 1;
OCIHandleFree((dvoid *) oci_globals->err, OCI_HTYPE_ERROR);
oci_globals->in_call = 0;
oci_globals->err = NULL;
}
if (oci_globals->env) {
oci_globals->in_call = 1;
OCIHandleFree((dvoid *) oci_globals->env, OCI_HTYPE_ENV);
oci_globals->in_call = 0;
oci_globals->env = NULL;
}
}
/* }}} */
PHP_MINIT_FUNCTION(oci)
{
REGISTER_INI_ENTRIES();
le_statement = zend_register_list_destructors_ex(php_oci_statement_list_dtor, NULL, "oci8 statement", module_number);
le_connection = zend_register_list_destructors_ex(php_oci_connection_list_dtor, NULL, "oci8 connection", module_number);
le_pconnection = zend_register_list_destructors_ex(php_oci_pconnection_list_np_dtor, php_oci_pconnection_list_dtor, "oci8 persistent connection", module_number);
le_psessionpool = zend_register_list_destructors_ex(NULL, php_oci_spool_list_dtor, "oci8 persistent session pool", module_number);
le_descriptor = zend_register_list_destructors_ex(php_oci_descriptor_list_dtor, NULL, "oci8 descriptor", module_number);
le_collection = zend_register_list_destructors_ex(php_oci_collection_list_dtor, NULL, "oci8 collection", module_number);
oci_lob_class_entry_ptr = register_class_OCILob();
oci_coll_class_entry_ptr = register_class_OCICollection();
register_oci8_symbols(module_number);
return SUCCESS;
}
PHP_RINIT_FUNCTION(oci)
{
OCI_G(num_links) = OCI_G(num_persistent);
OCI_G(errcode) = 0;
OCI_G(edition) = NULL;
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(oci)
{
OCI_G(shutdown) = 1;
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
PHP_RSHUTDOWN_FUNCTION(oci)
{
/* Check persistent connections and do the necessary actions if needed. If persistent_helper is
* unable to process a pconnection because of a refcount, the processing would happen from
* np-destructor which is called when refcount goes to zero - php_oci_pconnection_list_np_dtor
*/
zend_hash_apply(&EG(persistent_list), php_oci_persistent_helper);
if (OCI_G(edition)) {
efree(OCI_G(edition));
}
return SUCCESS;
}
PHP_MINFO_FUNCTION(oci)
{
char buf[32];
char ver[256];
php_info_print_table_start();
php_info_print_table_row(2, "OCI8 Support", "enabled");
#if defined(HAVE_OCI8_DTRACE)
php_info_print_table_row(2, "OCI8 DTrace Support", "enabled");
#else
php_info_print_table_row(2, "OCI8 DTrace Support", "disabled");
#endif
php_info_print_table_row(2, "OCI8 Version", PHP_OCI8_VERSION);
php_oci_client_get_version(ver, sizeof(ver));
php_info_print_table_row(2, "Oracle Run-time Client Library Version", ver);
snprintf(buf, sizeof(buf), "%d.%d", OCI_MAJOR_VERSION, OCI_MINOR_VERSION);
#if defined(HAVE_OCI_INSTANT_CLIENT)
php_info_print_table_row(2, "Oracle Compile-time Instant Client Version", buf);
#else
php_info_print_table_row(2, "Oracle Compile-time Version", buf);
#endif
#if !defined(PHP_WIN32) && !defined(HAVE_OCI_INSTANT_CLIENT)
#if defined(PHP_OCI8_DEF_DIR)
php_info_print_table_row(2, "Compile-time ORACLE_HOME", PHP_OCI8_DEF_DIR);
#endif
#if defined(PHP_OCI8_DEF_SHARED_LIBADD)
php_info_print_table_row(2, "Libraries Used", PHP_OCI8_DEF_SHARED_LIBADD);
#endif
#endif
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
php_info_print_table_start();
php_info_print_table_header(2, "Statistics", "");
snprintf(buf, sizeof(buf), ZEND_LONG_FMT, OCI_G(num_persistent));
php_info_print_table_row(2, "Active Persistent Connections", buf);
snprintf(buf, sizeof(buf), ZEND_LONG_FMT, OCI_G(num_links));
php_info_print_table_row(2, "Active Connections", buf);
php_info_print_table_end();
}
/* }}} */
/* {{{ list destructors */
/* {{{ php_oci_connection_list_dtor()
*
* Non-persistent connection destructor
*/
static void php_oci_connection_list_dtor(zend_resource *entry)
{
php_oci_connection *connection = (php_oci_connection *)entry->ptr;
if (connection) {
php_oci_connection_close(connection);
OCI_G(num_links)--;
}
}
/* }}} */
/* {{{ php_oci_pconnection_list_dtor()
*
* Persistent connection destructor
*/
static void php_oci_pconnection_list_dtor(zend_resource *entry)
{
php_oci_connection *connection = (php_oci_connection *)entry->ptr;
if (connection) {
php_oci_connection_close(connection);
OCI_G(num_persistent)--;
OCI_G(num_links)--;
}
}
/* }}} */
/* {{{ php_oci_pconnection_list_np_dtor()
*
* Non-Persistent destructor for persistent connection - This gets invoked when
* the refcount of this goes to zero in the regular list
*/
static void php_oci_pconnection_list_np_dtor(zend_resource *entry)
{
php_oci_connection *connection = (php_oci_connection *)entry->ptr;
zval *zvp;
zend_resource *le;
/*
* We cannot get connection as NULL or as a stub in this function. This is the function that
* turns a pconnection to a stub
*
* If oci_password_change() changed the password of a persistent connection, close the
* connection and remove it from the persistent connection cache. This means subsequent scripts
* will be prevented from being able to present the old (now invalid) password to a usable
* connection to the database; they must use the new password.
*
* Check for conditions that warrant removal of the hash entry
*/
if (!connection->is_open ||
connection->passwd_changed ||
(PG(connection_status) & PHP_CONNECTION_TIMEOUT) ||
OCI_G(in_call)) {
/* Remove the hash entry if present */
if (connection->hash_key) {
zvp = zend_hash_find(&EG(persistent_list), connection->hash_key);
le = zvp ? Z_RES_P(zvp) : NULL;
if (le != NULL && le->type == le_pconnection && le->ptr == connection) {
zend_hash_del(&EG(persistent_list), connection->hash_key);
}
else {
php_oci_connection_close(connection);
OCI_G(num_persistent)--;
}
}
#ifdef HAVE_OCI8_DTRACE
if (DTRACE_OCI8_CONNECT_P_DTOR_CLOSE_ENABLED()) {
DTRACE_OCI8_CONNECT_P_DTOR_CLOSE(connection);
}
#endif /* HAVE_OCI8_DTRACE */
} else {
/*
* Release the connection to underlying pool. We do this unconditionally so that
* out-of-scope pconnects are now consistent with oci_close and out-of-scope new connect
* semantics. With the PECL OCI 1.3.x extensions, we release pconnections when oci_close
* takes the refcount to zero.
*
* If oci_old_close_semantics is set, we artificially bump up the refcount and decremented
* only at request shutdown.
*/
php_oci_connection_release(connection);
#ifdef HAVE_OCI8_DTRACE
if (DTRACE_OCI8_CONNECT_P_DTOR_RELEASE_ENABLED()) {
DTRACE_OCI8_CONNECT_P_DTOR_RELEASE(connection);
}
#endif /* HAVE_OCI8_DTRACE */
}
}
/* }}} */
/* {{{ php_oci_statement_list_dtor()
*
* Statement destructor
*/
static void php_oci_statement_list_dtor(zend_resource *entry)
{
php_oci_statement *statement = (php_oci_statement *)entry->ptr;
php_oci_statement_free(statement);
}
/* }}} */
/* {{{ php_oci_descriptor_list_dtor()
*
* Descriptor destructor
*/
static void php_oci_descriptor_list_dtor(zend_resource *entry)
{
php_oci_descriptor *descriptor = (php_oci_descriptor *)entry->ptr;
php_oci_lob_free(descriptor);
}
/* }}} */
/* {{{ php_oci_collection_list_dtor()
*
* Collection destructor
*/
static void php_oci_collection_list_dtor(zend_resource *entry)
{
php_oci_collection *collection = (php_oci_collection *)entry->ptr;
php_oci_collection_close(collection);
}
/* }}} */
/* }}} */
/* {{{ Hash Destructors */
/* {{{ php_oci_define_hash_dtor()
*
* Define hash destructor
*/
void php_oci_define_hash_dtor(zval *data)
{
php_oci_define *define = (php_oci_define *) Z_PTR_P(data);
if (define->name) {
efree(define->name);
define->name = NULL;
}
zval_ptr_dtor(&define->val);
efree(define);
}
/* }}} */
/* {{{ php_oci_bind_hash_dtor()
*
* Bind hash destructor
*/
void php_oci_bind_hash_dtor(zval *data)
{
php_oci_bind *bind = (php_oci_bind *) Z_PTR_P(data);
if (!Z_ISUNDEF(bind->val)) {
zval_ptr_dtor(&bind->val);
ZVAL_UNDEF(&bind->val);
}
if (bind->array.elements) {
efree(bind->array.elements);
bind->array.elements = NULL;
}
if (bind->array.element_lengths) {
efree(bind->array.element_lengths);
bind->array.element_lengths = NULL;
}
if (bind->array.indicators) {
efree(bind->array.indicators);
bind->array.indicators = NULL;
}
efree(bind);
}
/* }}} */
/* {{{ php_oci_column_hash_dtor()
*
* Column hash destructor
*/
void php_oci_column_hash_dtor(zval *data)
{
php_oci_out_column *column = (php_oci_out_column *) Z_PTR_P(data);
if (column->stmtid) {
zend_list_close(column->stmtid);
}
if (column->descid) {
if (GC_REFCOUNT(column->descid) == 1)
zend_list_close(column->descid);
else {
GC_DELREF(column->descid);
}
}
if (column->data) {
efree(column->data);
}
if (column->name) {
efree(column->name);
}
efree(column);
}
/* }}} */
/* {{{ php_oci_descriptor_flush_hash_dtor()
*
* Flush descriptors on commit
*/
void php_oci_descriptor_flush_hash_dtor(zval *data)
{
php_oci_descriptor *descriptor = (php_oci_descriptor *) Z_PTR_P(data);
if (descriptor && descriptor->buffering == PHP_OCI_LOB_BUFFER_USED && (descriptor->type == OCI_DTYPE_LOB || descriptor->type == OCI_DTYPE_FILE)) {
php_oci_lob_flush(descriptor, OCI_LOB_BUFFER_FREE);
descriptor->buffering = PHP_OCI_LOB_BUFFER_ENABLED;
}
data = NULL;
}
/* }}} */
/* }}} */
/* {{{ php_oci_connection_descriptors_free()
*
* Free descriptors for a connection
*/
void php_oci_connection_descriptors_free(php_oci_connection *connection)
{
zend_hash_destroy(connection->descriptors);
efree(connection->descriptors);
connection->descriptors = NULL;
connection->descriptor_count = 0;
}
/* }}} */
/* {{{ php_oci_error()
*
* Fetch & print out error message if we get an error
* Returns an Oracle error number
*/
sb4 php_oci_error(OCIError *err_p, sword errstatus)
{
text errbuf[PHP_OCI_ERRBUF_LEN];
sb4 errcode = 0; /* Oracle error number */
switch (errstatus) {
case OCI_SUCCESS:
break;
case OCI_SUCCESS_WITH_INFO:
errcode = php_oci_fetch_errmsg(err_p, errbuf, sizeof(errbuf));
if (errcode) {
php_error_docref(NULL, E_WARNING, "OCI_SUCCESS_WITH_INFO: %s", errbuf);
} else {
php_error_docref(NULL, E_WARNING, "OCI_SUCCESS_WITH_INFO: failed to fetch error message");
}
break;
case OCI_NEED_DATA:
php_error_docref(NULL, E_WARNING, "OCI_NEED_DATA");
break;
case OCI_NO_DATA:
errcode = php_oci_fetch_errmsg(err_p, errbuf, sizeof(errbuf));
if (errcode) {
php_error_docref(NULL, E_WARNING, "%s", errbuf);
} else {
php_error_docref(NULL, E_WARNING, "OCI_NO_DATA: failed to fetch error message");
}
break;
case OCI_ERROR:
errcode = php_oci_fetch_errmsg(err_p, errbuf, sizeof(errbuf));
if (errcode) {
php_error_docref(NULL, E_WARNING, "%s", errbuf);
} else {
php_error_docref(NULL, E_WARNING, "Failed to fetch error message");
}
break;
case OCI_INVALID_HANDLE:
php_error_docref(NULL, E_WARNING, "OCI_INVALID_HANDLE");
break;
case OCI_STILL_EXECUTING:
php_error_docref(NULL, E_WARNING, "OCI_STILL_EXECUTING");
break;
case OCI_CONTINUE:
php_error_docref(NULL, E_WARNING, "OCI_CONTINUE");
break;
default:
php_error_docref(NULL, E_WARNING, "Unknown OCI error code: %d", errstatus);
break;
}
#ifdef HAVE_OCI8_DTRACE
if (DTRACE_OCI8_ERROR_ENABLED()) {
DTRACE_OCI8_ERROR((int)errstatus, (long)errcode);
}
#endif /* HAVE_OCI8_DTRACE */
return errcode;
}
/* }}} */
/* {{{ php_oci_fetch_errmsg()
*
* Fetch error message into the buffer from the error handle provided
*/
sb4 php_oci_fetch_errmsg(OCIError *error_handle, text *error_buf, size_t error_buf_size)
{
sb4 error_code = 0;
PHP_OCI_CALL(OCIErrorGet, (error_handle, (ub4)1, NULL, &error_code, error_buf, (ub4)error_buf_size, (ub4)OCI_HTYPE_ERROR));
if (error_code) {
int err_buf_len = (int) strlen((char *)error_buf);
if (err_buf_len && error_buf[err_buf_len - 1] == '\n') {
error_buf[err_buf_len - 1] = '\0';
}
}
return error_code;
}
/* }}} */
/* {{{ php_oci_fetch_sqltext_offset()
*
* Compute offset in the SQL statement
*/
int php_oci_fetch_sqltext_offset(php_oci_statement *statement, text **sqltext, ub2 *error_offset)
{
sword errstatus;
*sqltext = NULL;
*error_offset = 0;
PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)statement->stmt, OCI_HTYPE_STMT, (dvoid *) sqltext, (ub4 *)0, OCI_ATTR_STATEMENT, statement->err));
if (errstatus != OCI_SUCCESS) {
statement->errcode = php_oci_error(statement->err, errstatus);
PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
return 1;
}
PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)statement->stmt, OCI_HTYPE_STMT, (ub2 *)error_offset, (ub4 *)0, OCI_ATTR_PARSE_ERROR_OFFSET, statement->err));
if (errstatus != OCI_SUCCESS) {
statement->errcode = php_oci_error(statement->err, errstatus);
PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
return 1;
}
return 0;
}
/* }}} */
/* {{{ php_oci_do_connect()
*
* Connect wrapper
*/
void php_oci_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent, int exclusive)
{
php_oci_connection *connection;
char *username, *password;
char *dbname = NULL, *charset = NULL;
size_t username_len = 0, password_len = 0;
size_t dbname_len = 0, charset_len = 0;
zend_long session_mode = OCI_DEFAULT;
/* if a fourth parameter is handed over, it is the charset identifier (but is only used in Oracle 9i+) */
ZEND_PARSE_PARAMETERS_START(2, 5)
Z_PARAM_STRING(username, username_len)
Z_PARAM_STRING(password, password_len)
Z_PARAM_OPTIONAL
Z_PARAM_STRING_OR_NULL(dbname, dbname_len)
Z_PARAM_STRING(charset, charset_len)
Z_PARAM_LONG(session_mode)
ZEND_PARSE_PARAMETERS_END();
#ifdef HAVE_OCI8_DTRACE
if (DTRACE_OCI8_CONNECT_ENTRY_ENABLED()) {
DTRACE_OCI8_CONNECT_ENTRY(username, dbname, charset, session_mode, persistent, exclusive);
}
#endif /* HAVE_OCI8_DTRACE */
if (!charset_len) {
charset = NULL;
}
connection = php_oci_do_connect_ex(username, (int) username_len, password, (int) password_len, NULL, 0, dbname, (int) dbname_len, charset, session_mode, persistent, exclusive);
#ifdef HAVE_OCI8_DTRACE
if (DTRACE_OCI8_CONNECT_RETURN_ENABLED()) {
DTRACE_OCI8_CONNECT_RETURN(connection);
}
#endif /* HAVE_OCI8_DTRACE */
if (!connection) {
RETURN_FALSE;
}
RETURN_RES(connection->id);
}
/* }}} */
/* {{{ php_oci_do_connect_ex()
*
* The real connect function. Allocates all the resources needed, establishes the connection and
* returns the result handle (or NULL)
*/
php_oci_connection *php_oci_do_connect_ex(char *username, int username_len, char *password, int password_len, char *new_password, int new_password_len, char *dbname, int dbname_len, char *charset, zend_long session_mode, int persistent, int exclusive)
{
zval *zvp;
zend_resource *le = NULL;
zend_resource new_le;
php_oci_connection *connection = NULL;
smart_str hashed_details = {0};
time_t timestamp;
php_oci_spool *session_pool = NULL;
bool use_spool = 1; /* Default is to use client-side session pool */
bool ping_done = 0;
ub2 charsetid = 0;
ub2 charsetid_nls_lang = 0;
if (session_mode & ~(OCI_SYSOPER | OCI_SYSDBA | PHP_OCI_CRED_EXT)) {
php_error_docref(NULL, E_WARNING, "Invalid session mode specified (" ZEND_LONG_FMT ")", session_mode);
return NULL;
}
if (session_mode & (OCI_SYSOPER | OCI_SYSDBA | PHP_OCI_CRED_EXT)) {
if ((session_mode & OCI_SYSOPER) && (session_mode & OCI_SYSDBA)) {
php_error_docref(NULL, E_WARNING, "OCI_SYSDBA and OCI_SYSOPER cannot be used together");
return NULL;
}
if (session_mode & PHP_OCI_CRED_EXT) {
#ifdef PHP_WIN32
/* Disable external authentication on Windows as Impersonation is not yet handled.
* TODO: Re-enable this once OCI provides capability.
*/
php_error_docref(NULL, E_WARNING, "External Authentication is not supported on Windows");
return NULL;
#endif
if (username_len != 1 || username[0] != '/' || password_len != 0) {
php_error_docref(NULL, E_WARNING, "OCI_CRED_EXT can only be used with a username of \"/\" and a NULL password");
return NULL;
}
}
if (session_mode & (OCI_SYSOPER | OCI_SYSDBA)) {
/* Increase security by not caching privileged oci_pconnect() connections. The
* connection becomes equivalent to oci_connect() or oci_new_connect().
*/
persistent = 0;
if (!OCI_G(privileged_connect)) {
php_error_docref(NULL, E_WARNING, "Privileged connect is disabled. Enable oci8.privileged_connect to be able to connect as SYSOPER or SYSDBA");
return NULL;
}
}
}
/* Initialize global handles if they weren't initialized before */
if (OCI_G(env) == NULL) {
php_oci_init_global_handles();
if (OCI_G(env) == NULL) {
return NULL;
}
}
/* We cannot use the new session create logic (OCISessionGet from
* client-side session pool) when privileged connect or password change is attempted.
* TODO: Re-enable this when OCI provides support.
*/
if ((session_mode & (OCI_SYSOPER | OCI_SYSDBA)) || (new_password_len)) {
use_spool = 0;
}
smart_str_appendl_ex(&hashed_details, "oci8***", sizeof("oci8***") - 1, 0);
smart_str_appendl_ex(&hashed_details, username, username_len, 0);
smart_str_appendl_ex(&hashed_details, "**", sizeof("**") - 1, 0);
/* DRCP: connection_class is an attribute of a connection */
if (OCI_G(connection_class)){
smart_str_appendl_ex(&hashed_details, OCI_G(connection_class), strlen(OCI_G(connection_class)), 0);
}
smart_str_appendl_ex(&hashed_details, "**", sizeof("**") - 1, 0);
/* Add edition attribute to the hash */
if (OCI_G(edition)){
smart_str_appendl_ex(&hashed_details, OCI_G(edition), strlen(OCI_G(edition)), 0);
}
smart_str_appendl_ex(&hashed_details, "**", sizeof("**") - 1, 0);
if (password_len) {
zend_ulong password_hash;
password_hash = zend_hash_func(password, password_len);
smart_str_append_unsigned_ex(&hashed_details, password_hash, 0);
}
smart_str_appendl_ex(&hashed_details, "**", sizeof("**") - 1, 0);
if (dbname) {
smart_str_appendl_ex(&hashed_details, dbname, dbname_len, 0);
}
smart_str_appendl_ex(&hashed_details, "**", sizeof("**") - 1, 0);
if (charset && *charset) {
PHP_OCI_CALL_RETURN(charsetid, OCINlsCharSetNameToId, (OCI_G(env), (CONST oratext *)charset));
if (!charsetid) {
php_error_docref(NULL, E_WARNING, "Invalid character set name: %s", charset);
} else {
smart_str_append_unsigned_ex(&hashed_details, charsetid, 0);
}
}
/* use NLS_LANG if no or invalid charset specified */
if (!charsetid) {
size_t rsize = 0;
sword result;
PHP_OCI_CALL_RETURN(result, OCINlsEnvironmentVariableGet, (&charsetid_nls_lang, 0, OCI_NLS_CHARSET_ID, 0, &rsize));
if (result != OCI_SUCCESS) {
charsetid_nls_lang = 0;
}
smart_str_append_unsigned_ex(&hashed_details, charsetid_nls_lang, 0);
}
timestamp = time(NULL);
smart_str_append_unsigned_ex(&hashed_details, session_mode, 0);
if (persistent) {
smart_str_appendl_ex(&hashed_details, "pc", sizeof("pc") - 1, 0);
}
smart_str_0(&hashed_details);
/* make it lowercase */
zend_str_tolower(ZSTR_VAL(hashed_details.s), ZSTR_LEN(hashed_details.s));
if (!exclusive && !new_password) {
bool found = 0;
if (persistent && ((zvp = zend_hash_find(&EG(persistent_list), hashed_details.s))) != NULL) {
zend_resource *le = Z_RES_P(zvp);
found = 1;
/* found */
if (le->type == le_pconnection) {
connection = (php_oci_connection *)le->ptr;
}
} else if (!persistent && ((zvp = zend_hash_find(&EG(regular_list), hashed_details.s)) != NULL)) {
le = Z_RES_P(zvp);
found = 1;
if (le->type == le_index_ptr) {
zend_resource *ptr;
ptr = (zend_resource *) le->ptr;
if (ptr && (ptr->type == le_connection)) {
connection = (php_oci_connection *)ptr->ptr;
}
}
}
#ifdef HAVE_OCI8_DTRACE
if (DTRACE_OCI8_CONNECT_LOOKUP_ENABLED()) {
DTRACE_OCI8_CONNECT_LOOKUP(connection, connection && connection->is_stub ? 1 : 0);
}
#endif /* HAVE_OCI8_DTRACE */
/* If we got a pconnection stub, then 'load'(OCISessionGet) the real connection from its
* private spool A connection is a stub if it is only a cached structure and the real
* connection is released to its underlying private session pool. We currently do not have
* stub support for non-persistent conns.
*
* TODO: put in negative code for non-persistent stubs
*/
if (connection && connection->is_persistent && connection->is_stub) {
if (php_oci_create_session(connection, NULL, dbname, dbname_len, username, username_len, password, password_len, new_password, new_password_len, (int) session_mode)) {
smart_str_free(&hashed_details);
zend_hash_del(&EG(persistent_list), connection->hash_key);
return NULL;
}
/* We do the ping in php_oci_create_session, no need to ping again below */
ping_done = 1;
}
if (connection) {
if (connection->is_open) {
/* found an open connection. now ping it */
if (connection->is_persistent) {
/* Check connection liveness in the following order:
* 1) always check OCI_ATTR_SERVER_STATUS
* 2) see if it's time to ping it
* 3) ping it if needed
*/
if (php_oci_connection_status(connection)) {
/* Only ping if:
*
* 1) next_ping > 0, which means that ping_interval is not -1 (aka "Off")
*
* 2) current_timestamp > next_ping, which means "it's time to check if it's
* still alive"
*/
if (!ping_done && (*(connection->next_pingp) > 0) && (timestamp >= *(connection->next_pingp)) && !php_oci_connection_ping(connection)) {
/* server died */
} else {
php_oci_connection *tmp = (php_oci_connection *) NULL;
zval *tmp_val = (zval *) NULL;
/* okay, the connection is open and the server is still alive */
connection->used_this_request = 1;
if (connection->id) {
tmp_val = zend_hash_index_find(&EG(regular_list), connection->id->handle);
if ((tmp_val != NULL) && (Z_TYPE_P(tmp_val) == IS_RESOURCE)) {
tmp = Z_RES_VAL_P(tmp_val);
}