forked from php/pecl-database-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php_mysql.c
2821 lines (2486 loc) · 79.9 KB
/
php_mysql.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 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2015 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]> |
| Zak Greant <[email protected]> |
| Georg Richter <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
/* TODO:
*
* ? Safe mode implementation
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "php.h"
#include "php_globals.h"
#include "ext/standard/info.h"
#include "ext/standard/php_string.h"
#include "ext/standard/basic_functions.h"
#include "zend_exceptions.h"
#if HAVE_MYSQL
#ifdef PHP_WIN32
# include <winsock2.h>
# define signal(a, b) NULL
#elif defined(NETWARE)
# include <sys/socket.h>
# define signal(a, b) NULL
#else
# if HAVE_SIGNAL_H
# include <signal.h>
# endif
# if HAVE_SYS_TYPES_H
# include <sys/types.h>
# endif
# include <netdb.h>
# include <netinet/in.h>
# if HAVE_ARPA_INET_H
# include <arpa/inet.h>
# endif
#endif
#include "php_ini.h"
#include "php_mysql_structs.h"
#include "php_mysql.h"
/* PHP 7.3 compatibility macro */
#ifndef GC_ADDREF
# define GC_ADDREF(ref) ++GC_REFCOUNT(ref)
#endif
/* True globals, no need for thread safety */
static int le_result, le_link, le_plink;
#ifdef HAVE_MYSQL_REAL_CONNECT
# ifdef HAVE_ERRMSG_H
# include <errmsg.h>
# endif
#endif
#define SAFE_STRING(s) ((s)?(s):"")
#if MYSQL_VERSION_ID > 32199 || defined(MYSQL_USE_MYSQLND)
# define mysql_row_length_type zend_ulong
# define HAVE_MYSQL_ERRNO
#else
# define mysql_row_length_type unsigned int
# ifdef mysql_errno
# define HAVE_MYSQL_ERRNO
# endif
#endif
#if MYSQL_VERSION_ID >= 32032 || defined(MYSQL_USE_MYSQLND)
#define HAVE_GETINFO_FUNCS
#endif
#if MYSQL_VERSION_ID > 32133 || defined(FIELD_TYPE_TINY)
#define MYSQL_HAS_TINY
#endif
#if MYSQL_VERSION_ID >= 32200
#define MYSQL_HAS_YEAR
#endif
#define MYSQL_ASSOC 1<<0
#define MYSQL_NUM 1<<1
#define MYSQL_BOTH (MYSQL_ASSOC|MYSQL_NUM)
#define MYSQL_USE_RESULT 0
#define MYSQL_STORE_RESULT 1
#if MYSQL_VERSION_ID < 32224
#define PHP_MYSQL_VALID_RESULT(mysql) \
(mysql_num_fields(mysql)>0)
#else
#define PHP_MYSQL_VALID_RESULT(mysql) \
(mysql_field_count(mysql)>0)
#endif
ZEND_DECLARE_MODULE_GLOBALS(mysql)
static PHP_GINIT_FUNCTION(mysql);
typedef struct _php_mysql_conn {
MYSQL *conn;
zend_resource *active_result_res;
int multi_query;
} php_mysql_conn;
#if MYSQL_VERSION_ID >= 40101
#define MYSQL_DISABLE_MQ if (mysql->multi_query) { \
mysql_set_server_option(mysql->conn, MYSQL_OPTION_MULTI_STATEMENTS_OFF); \
mysql->multi_query = 0; \
}
#else
#define MYSQL_DISABLE_MQ
#endif
/* {{{ arginfo */
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_connect, 0, 0, 0)
ZEND_ARG_INFO(0, hostname)
ZEND_ARG_INFO(0, username)
ZEND_ARG_INFO(0, password)
ZEND_ARG_INFO(0, new)
ZEND_ARG_INFO(0, flags)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_pconnect, 0, 0, 0)
ZEND_ARG_INFO(0, hostname)
ZEND_ARG_INFO(0, username)
ZEND_ARG_INFO(0, password)
ZEND_ARG_INFO(0, flags)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo__optional_mysql_link, 0, 0, 0)
ZEND_ARG_INFO(0, link_identifier)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_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(arginfo__void_mysql_arg, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_set_charset, 0, 0, 1)
ZEND_ARG_INFO(0, charset_name)
ZEND_ARG_INFO(0, link_identifier)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_query, 0, 0, 1)
ZEND_ARG_INFO(0, query)
ZEND_ARG_INFO(0, link_identifier)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_db_query, 0, 0, 2)
ZEND_ARG_INFO(0, database_name)
ZEND_ARG_INFO(0, query)
ZEND_ARG_INFO(0, link_identifier)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_list_fields, 0, 0, 2)
ZEND_ARG_INFO(0, database_name)
ZEND_ARG_INFO(0, table_name)
ZEND_ARG_INFO(0, link_identifier)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_escape_string, 0, 0, 1)
ZEND_ARG_INFO(0, string)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_real_escape_string, 0, 0, 1)
ZEND_ARG_INFO(0, string)
ZEND_ARG_INFO(0, link_identifier)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_result, 0, 0, 2)
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__result_mysql_arg, 0, 0, 1)
ZEND_ARG_INFO(0, result)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_fetch_object, 0, 0, 1)
ZEND_ARG_INFO(0, result)
ZEND_ARG_INFO(0, class_name)
ZEND_ARG_INFO(0, ctor_params)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_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_mysql_data_seek, 0, 0, 2)
ZEND_ARG_INFO(0, result)
ZEND_ARG_INFO(0, row_number)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_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_mysql_field_seek, 0, 0, 2)
ZEND_ARG_INFO(0, result)
ZEND_ARG_INFO(0, field_offset)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_field_name, 0, 0, 2)
ZEND_ARG_INFO(0, result)
ZEND_ARG_INFO(0, field_index)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ mysql_functions[]
*/
static const zend_function_entry mysql_functions[] = {
PHP_FE(mysql_connect, arginfo_mysql_connect)
PHP_FE(mysql_pconnect, arginfo_mysql_pconnect)
PHP_FE(mysql_close, arginfo__optional_mysql_link)
PHP_FE(mysql_select_db, arginfo_mysql_select_db)
#ifndef NETWARE /* The below two functions not supported on NetWare */
#if MYSQL_VERSION_ID < 40000
PHP_DEP_FE(mysql_create_db, arginfo_mysql_select_db)
PHP_DEP_FE(mysql_drop_db, arginfo_mysql_select_db)
#endif
#endif /* NETWARE */
PHP_FE(mysql_query, arginfo_mysql_query)
PHP_FE(mysql_unbuffered_query, arginfo_mysql_query)
PHP_DEP_FE(mysql_db_query, arginfo_mysql_db_query)
PHP_DEP_FE(mysql_list_dbs, arginfo__optional_mysql_link)
PHP_DEP_FE(mysql_list_tables, arginfo_mysql_select_db)
PHP_FE(mysql_list_fields, arginfo_mysql_list_fields)
PHP_FE(mysql_list_processes, arginfo__optional_mysql_link)
PHP_FE(mysql_error, arginfo__optional_mysql_link)
#ifdef HAVE_MYSQL_ERRNO
PHP_FE(mysql_errno, arginfo__optional_mysql_link)
#endif
PHP_FE(mysql_affected_rows, arginfo__optional_mysql_link)
PHP_FE(mysql_insert_id, arginfo__optional_mysql_link)
PHP_FE(mysql_result, arginfo_mysql_result)
PHP_FE(mysql_num_rows, arginfo__result_mysql_arg)
PHP_FE(mysql_num_fields, arginfo__result_mysql_arg)
PHP_FE(mysql_fetch_row, arginfo__result_mysql_arg)
PHP_FE(mysql_fetch_array, arginfo_mysql_fetch_array)
PHP_FE(mysql_fetch_assoc, arginfo__result_mysql_arg)
PHP_FE(mysql_fetch_object, arginfo_mysql_fetch_object)
PHP_FE(mysql_data_seek, arginfo_mysql_data_seek)
PHP_FE(mysql_fetch_lengths, arginfo__result_mysql_arg)
PHP_FE(mysql_fetch_field, arginfo_mysql_fetch_field)
PHP_FE(mysql_field_seek, arginfo_mysql_field_seek)
PHP_FE(mysql_free_result, arginfo__result_mysql_arg)
PHP_FE(mysql_field_name, arginfo_mysql_field_name)
PHP_FE(mysql_field_table, arginfo_mysql_field_seek)
PHP_FE(mysql_field_len, arginfo_mysql_field_seek)
PHP_FE(mysql_field_type, arginfo_mysql_field_seek)
PHP_FE(mysql_field_flags, arginfo_mysql_field_seek)
PHP_FE(mysql_escape_string, arginfo_mysql_escape_string)
PHP_FE(mysql_real_escape_string, arginfo_mysql_real_escape_string)
PHP_FE(mysql_stat, arginfo__optional_mysql_link)
PHP_FE(mysql_thread_id, arginfo__optional_mysql_link)
PHP_FE(mysql_client_encoding, arginfo__optional_mysql_link)
PHP_FE(mysql_ping, arginfo__optional_mysql_link)
#ifdef HAVE_GETINFO_FUNCS
PHP_FE(mysql_get_client_info, arginfo__void_mysql_arg)
PHP_FE(mysql_get_host_info, arginfo__optional_mysql_link)
PHP_FE(mysql_get_proto_info, arginfo__optional_mysql_link)
PHP_FE(mysql_get_server_info, arginfo__optional_mysql_link)
#endif
PHP_FE(mysql_info, arginfo__optional_mysql_link)
#ifdef MYSQL_HAS_SET_CHARSET
PHP_FE(mysql_set_charset, arginfo_mysql_set_charset)
#endif
/* for downwards compatibility */
PHP_DEP_FALIAS(mysql, mysql_db_query, arginfo_mysql_db_query)
PHP_DEP_FALIAS(mysql_fieldname, mysql_field_name, arginfo_mysql_field_name)
PHP_DEP_FALIAS(mysql_fieldtable, mysql_field_table, arginfo_mysql_field_seek)
PHP_DEP_FALIAS(mysql_fieldlen, mysql_field_len, arginfo_mysql_field_seek)
PHP_DEP_FALIAS(mysql_fieldtype, mysql_field_type, arginfo_mysql_field_seek)
PHP_DEP_FALIAS(mysql_fieldflags, mysql_field_flags, arginfo_mysql_field_seek)
PHP_DEP_FALIAS(mysql_selectdb, mysql_select_db, arginfo_mysql_select_db)
#ifndef NETWARE /* The below two functions not supported on NetWare */
#if MYSQL_VERSION_ID < 40000
PHP_DEP_FALIAS(mysql_createdb, mysql_create_db, arginfo_mysql_select_db)
PHP_DEP_FALIAS(mysql_dropdb, mysql_drop_db, arginfo_mysql_select_db)
#endif
#endif /* NETWARE */
PHP_DEP_FALIAS(mysql_freeresult, mysql_free_result, arginfo__result_mysql_arg)
PHP_DEP_FALIAS(mysql_numfields, mysql_num_fields, arginfo__result_mysql_arg)
PHP_DEP_FALIAS(mysql_numrows, mysql_num_rows, arginfo__result_mysql_arg)
PHP_DEP_FALIAS(mysql_listdbs, mysql_list_dbs, arginfo__optional_mysql_link)
PHP_DEP_FALIAS(mysql_listtables,mysql_list_tables, arginfo_mysql_select_db)
PHP_DEP_FALIAS(mysql_listfields, mysql_list_fields, arginfo_mysql_list_fields)
PHP_FALIAS(mysql_db_name, mysql_result, arginfo_mysql_result)
PHP_DEP_FALIAS(mysql_dbname, mysql_result, arginfo_mysql_result)
PHP_FALIAS(mysql_tablename, mysql_result, arginfo_mysql_result)
PHP_FALIAS(mysql_table_name, mysql_result, arginfo_mysql_result)
PHP_FE_END
};
/* }}} */
/* Dependancies */
static const zend_module_dep mysql_deps[] = {
#if defined(MYSQL_USE_MYSQLND)
ZEND_MOD_REQUIRED("mysqlnd")
#endif
ZEND_MOD_END
};
/* {{{ mysql_module_entry
*/
zend_module_entry mysql_module_entry = {
#if ZEND_MODULE_API_NO >= 20050922
STANDARD_MODULE_HEADER_EX, NULL,
mysql_deps,
#elif ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"mysql",
mysql_functions,
ZEND_MODULE_STARTUP_N(mysql),
PHP_MSHUTDOWN(mysql),
PHP_RINIT(mysql),
PHP_RSHUTDOWN(mysql),
PHP_MINFO(mysql),
PHP_MYSQL_VERSION,
PHP_MODULE_GLOBALS(mysql),
PHP_GINIT(mysql),
NULL,
NULL,
STANDARD_MODULE_PROPERTIES_EX
};
/* }}} */
#ifdef COMPILE_DL_MYSQL
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE();
#endif
ZEND_GET_MODULE(mysql)
#endif
void timeout(int sig);
#define CHECK_LINK(link) { if (link == NULL) { php_error_docref(NULL, E_WARNING, "A link to the server could not be established"); RETURN_FALSE; } }
#if defined(MYSQL_USE_MYSQLND)
#define PHPMY_UNBUFFERED_QUERY_CHECK() \
{ \
if (mysql->active_result_res) { \
do { \
MYSQL_RES *_mysql_result; \
_mysql_result = (MYSQL_RES *)mysql->active_result_res->ptr; \
if (_mysql_result && \
mysql->active_result_res->type == le_result) { \
if (mysql_result_is_unbuffered(_mysql_result) && \
!mysql_eof(_mysql_result)) { \
php_error_docref(NULL, E_NOTICE, \
"Function called without first fetching all " \
"rows from a previous unbuffered query"); \
} \
zend_list_close(mysql->active_result_res); \
mysql->active_result_res = NULL; \
} \
} while(0); \
} \
}
#else
#define PHPMY_UNBUFFERED_QUERY_CHECK() \
{ \
if (mysql->active_result_res) { \
do { \
MYSQL_RES *mysql_result; \
mysql_result = (MYSQL_RES *) mysql->active_result_res->ptr; \
if (mysql_result && \
mysql->active_result_res->type == le_result) { \
if (!mysql_eof(mysql_result)) { \
php_error_docref(NULL, E_NOTICE, \
"Function called without first fetching " \
"all rows from a previous unbuffered query"); \
while (mysql_fetch_row(mysql_result)); \
} \
zend_list_close(mysql->active_result_res); \
mysql->active_result_res = NULL; \
} \
} while(0); \
} \
}
#endif
/* {{{ _free_mysql_result
* This wrapper is required since mysql_free_result() returns an integer, and
* thus, cannot be used directly
*/
static void _free_mysql_result(zend_resource *rsrc)
{
MYSQL_RES *mysql_result = (MYSQL_RES *)rsrc->ptr;
mysql_free_result(mysql_result);
MySG(result_allocated)--;
}
/* }}} */
/* {{{ php_mysql_set_default_link
*/
static void php_mysql_set_default_link(zend_resource *link)
{
if (MySG(default_link) != NULL) {
zend_list_delete(MySG(default_link));
}
GC_ADDREF(link);
MySG(default_link) = link;
}
/* }}} */
/* {{{ php_mysql_select_db
*/
static int php_mysql_select_db(php_mysql_conn *mysql, char *db)
{
PHPMY_UNBUFFERED_QUERY_CHECK();
if (mysql_select_db(mysql->conn, db) != 0) {
return 0;
} else {
return 1;
}
}
/* }}} */
/* {{{ _close_mysql_link
*/
static void _close_mysql_link(zend_resource *rsrc)
{
php_mysql_conn *link = (php_mysql_conn *)rsrc->ptr;
void (*handler)(int);
handler = signal(SIGPIPE, SIG_IGN);
mysql_close(link->conn);
signal(SIGPIPE, handler);
efree(link);
MySG(num_links)--;
}
/* }}} */
/* {{{ _close_mysql_plink
*/
static void _close_mysql_plink(zend_resource *rsrc)
{
php_mysql_conn *link = (php_mysql_conn *)rsrc->ptr;
void (*handler) (int);
handler = signal(SIGPIPE, SIG_IGN);
mysql_close(link->conn);
signal(SIGPIPE, handler);
free(link);
MySG(num_persistent)--;
MySG(num_links)--;
}
/* }}} */
/* {{{ PHP_INI_MH
*/
static PHP_INI_MH(OnMySQLPort)
{
if (new_value != NULL) { /* default port */
MySG(default_port) = atoi(ZSTR_VAL(new_value));
} else {
MySG(default_port) = -1;
}
return SUCCESS;
}
/* }}} */
/* {{{ PHP_INI */
PHP_INI_BEGIN()
STD_PHP_INI_BOOLEAN("mysql.allow_persistent", "1", PHP_INI_SYSTEM, OnUpdateLong, allow_persistent, zend_mysql_globals, mysql_globals)
STD_PHP_INI_ENTRY_EX("mysql.max_persistent", "-1", PHP_INI_SYSTEM, OnUpdateLong, max_persistent, zend_mysql_globals, mysql_globals, display_link_numbers)
STD_PHP_INI_ENTRY_EX("mysql.max_links", "-1", PHP_INI_SYSTEM, OnUpdateLong, max_links, zend_mysql_globals, mysql_globals, display_link_numbers)
STD_PHP_INI_ENTRY("mysql.default_host", NULL, PHP_INI_ALL, OnUpdateString, default_host, zend_mysql_globals, mysql_globals)
STD_PHP_INI_ENTRY("mysql.default_user", NULL, PHP_INI_ALL, OnUpdateString, default_user, zend_mysql_globals, mysql_globals)
STD_PHP_INI_ENTRY("mysql.default_password", NULL, PHP_INI_ALL, OnUpdateString, default_password, zend_mysql_globals, mysql_globals)
PHP_INI_ENTRY("mysql.default_port", NULL, PHP_INI_ALL, OnMySQLPort)
#ifdef MYSQL_UNIX_ADDR
STD_PHP_INI_ENTRY("mysql.default_socket", MYSQL_UNIX_ADDR,PHP_INI_ALL,OnUpdateStringUnempty, default_socket, zend_mysql_globals, mysql_globals)
#else
STD_PHP_INI_ENTRY("mysql.default_socket", NULL, PHP_INI_ALL, OnUpdateStringUnempty, default_socket, zend_mysql_globals, mysql_globals)
#endif
STD_PHP_INI_ENTRY("mysql.connect_timeout", "60", PHP_INI_ALL, OnUpdateLong, connect_timeout, zend_mysql_globals, mysql_globals)
STD_PHP_INI_BOOLEAN("mysql.trace_mode", "0", PHP_INI_ALL, OnUpdateLong, trace_mode, zend_mysql_globals, mysql_globals)
STD_PHP_INI_BOOLEAN("mysql.allow_local_infile", "1", PHP_INI_SYSTEM, OnUpdateLong, allow_local_infile, zend_mysql_globals, mysql_globals)
PHP_INI_END()
/* }}} */
/* {{{ PHP_GINIT_FUNCTION
*/
static PHP_GINIT_FUNCTION(mysql)
{
#if defined(COMPILE_DL_MYSQL) && defined(ZTS)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
mysql_globals->num_persistent = 0;
mysql_globals->default_socket = NULL;
mysql_globals->default_host = NULL;
mysql_globals->default_user = NULL;
mysql_globals->default_password = NULL;
mysql_globals->connect_errno = 0;
mysql_globals->connect_error = NULL;
mysql_globals->connect_timeout = 0;
mysql_globals->trace_mode = 0;
mysql_globals->allow_local_infile = 1;
mysql_globals->result_allocated = 0;
}
/* }}} */
#ifdef MYSQL_USE_MYSQLND
#include "ext/mysqlnd/mysqlnd_reverse_api.h"
static MYSQLND *mysql_convert_zv_to_mysqlnd(zval *zv)
{
php_mysql_conn *mysql;
if (Z_TYPE_P(zv) != IS_RESOURCE) {
/* Might be nicer to check resource type, too, but ext/mysql is the only one using resources so emitting an error is not to bad, while usually this hook should be silent */
return NULL;
}
if (!(mysql = (php_mysql_conn *)zend_fetch_resource2(Z_RES_P(zv), "MySQL-Link", le_link, le_plink))) {
return NULL;
}
return mysql->conn;
}
static MYSQLND_REVERSE_API mysql_reverse_api = {
&mysql_module_entry,
mysql_convert_zv_to_mysqlnd
};
#endif
/* {{{ PHP_MINIT_FUNCTION
*/
ZEND_MODULE_STARTUP_D(mysql)
{
REGISTER_INI_ENTRIES();
le_result = zend_register_list_destructors_ex(_free_mysql_result, NULL, "mysql result", module_number);
le_link = zend_register_list_destructors_ex(_close_mysql_link, NULL, "mysql link", module_number);
le_plink = zend_register_list_destructors_ex(NULL, _close_mysql_plink, "mysql link persistent", module_number);
mysql_module_entry.type = type;
REGISTER_LONG_CONSTANT("MYSQL_ASSOC", MYSQL_ASSOC, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MYSQL_NUM", MYSQL_NUM, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MYSQL_BOTH", MYSQL_BOTH, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MYSQL_CLIENT_COMPRESS", CLIENT_COMPRESS, CONST_CS | CONST_PERSISTENT);
#if MYSQL_VERSION_ID >= 40000
REGISTER_LONG_CONSTANT("MYSQL_CLIENT_SSL", CLIENT_SSL, CONST_CS | CONST_PERSISTENT);
#endif
REGISTER_LONG_CONSTANT("MYSQL_CLIENT_INTERACTIVE", CLIENT_INTERACTIVE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MYSQL_CLIENT_IGNORE_SPACE", CLIENT_IGNORE_SPACE, CONST_CS | CONST_PERSISTENT);
#ifndef MYSQL_USE_MYSQLND
#if MYSQL_VERSION_ID >= 40000
if (mysql_server_init(0, NULL, NULL)) {
return FAILURE;
}
#endif
#endif
#ifdef MYSQL_USE_MYSQLND
mysqlnd_reverse_api_register_api(&mysql_reverse_api);
#endif
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(mysql)
{
#ifndef MYSQL_USE_MYSQLND
#if MYSQL_VERSION_ID >= 40000
#ifdef PHP_WIN32
unsigned long client_ver = mysql_get_client_version();
/*
Can't call mysql_server_end() multiple times prior to 5.0.46 on Windows.
PHP bug#41350 MySQL bug#25621
*/
if ((client_ver >= 50046 && client_ver < 50100) || client_ver > 50122) {
mysql_server_end();
}
#else
mysql_server_end();
#endif
#endif
#endif
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(mysql)
{
#if !defined(MYSQL_USE_MYSQLND) && defined(ZTS) && MYSQL_VERSION_ID >= 40000
if (mysql_thread_init()) {
return FAILURE;
}
#endif
MySG(default_link) = NULL;
MySG(num_links) = MySG(num_persistent);
/* Reset connect error/errno on every request */
MySG(connect_error) = NULL;
MySG(connect_errno) =0;
MySG(result_allocated) = 0;
return SUCCESS;
}
/* }}} */
#if defined(A0) && defined(MYSQL_USE_MYSQLND)
static int php_mysql_persistent_helper(zval *el)
{
zend_resource *le = (zend_resource *)Z_PTR_P(el);
if (le->type == le_plink) {
mysqlnd_end_psession(((php_mysql_conn *) le->ptr)->conn);
}
return ZEND_HASH_APPLY_KEEP;
} /* }}} */
#endif
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(mysql)
{
#if !defined(MYSQL_USE_MYSQLND) && defined(ZTS) && MYSQL_VERSION_ID >= 40000
mysql_thread_end();
#endif
if (MySG(trace_mode)) {
if (MySG(result_allocated)){
php_error_docref("function.mysql-free-result", E_WARNING, ZEND_LONG_FMT " result set(s) not freed. Use mysql_free_result to free result sets which were requested using mysql_query()", MySG(result_allocated));
}
}
if (MySG(connect_error)!=NULL) {
efree(MySG(connect_error));
}
#if defined(A0) && defined(MYSQL_USE_MYSQLND)
zend_hash_apply(&EG(persistent_list), (apply_func_t) php_mysql_persistent_helper);
#endif
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(mysql)
{
char buf[32];
php_info_print_table_start();
php_info_print_table_header(2, "MySQL Support", "enabled");
snprintf(buf, sizeof(buf), ZEND_LONG_FMT, MySG(num_persistent));
php_info_print_table_row(2, "Active Persistent Links", buf);
snprintf(buf, sizeof(buf), ZEND_LONG_FMT, MySG(num_links));
php_info_print_table_row(2, "Active Links", buf);
php_info_print_table_row(2, "Client API version", mysql_get_client_info());
#if !defined (PHP_WIN32) && !defined (NETWARE) && !defined(MYSQL_USE_MYSQLND)
php_info_print_table_row(2, "MYSQL_MODULE_TYPE", PHP_MYSQL_TYPE);
php_info_print_table_row(2, "MYSQL_SOCKET", MYSQL_UNIX_ADDR);
php_info_print_table_row(2, "MYSQL_INCLUDE", PHP_MYSQL_INCLUDE);
php_info_print_table_row(2, "MYSQL_LIBS", PHP_MYSQL_LIBS);
#endif
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
/* }}} */
/* {{{ php_mysql_do_connect
*/
#define MYSQL_DO_CONNECT_CLEANUP() \
if (free_host) { \
efree(host); \
}
#define MYSQL_DO_CONNECT_RETURN_FALSE() \
MYSQL_DO_CONNECT_CLEANUP(); \
RETURN_FALSE;
#ifdef MYSQL_USE_MYSQLND
#define MYSQL_PORT 0
#endif
static void php_mysql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
{
char *user = NULL, *passwd = NULL;
char *host_and_port = NULL, *socket = NULL;
char *tmp = NULL, *host = NULL;
size_t user_len = 0, passwd_len = 0, host_len = 0;
int port = MYSQL_PORT;
zend_long client_flags = 0;
php_mysql_conn *mysql = NULL;
#if MYSQL_VERSION_ID <= 32230
void (*handler) (int);
#endif
zend_long connect_timeout;
zend_string *hashed_details = NULL;
zend_bool free_host = 0, new_link = 0;
php_error_docref(NULL,
E_DEPRECATED,
"The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead");
#if !defined(MYSQL_USE_MYSQLND)
if ((MYSQL_VERSION_ID / 100) != (mysql_get_client_version() / 100)) {
php_error_docref(NULL, E_WARNING,
"Headers and client library minor version mismatch. Headers:%d Library:%ld",
MYSQL_VERSION_ID, mysql_get_client_version());
}
#endif
connect_timeout = MySG(connect_timeout);
socket = MySG(default_socket);
if (MySG(default_port) < 0) {
#if !defined(PHP_WIN32) && !defined(NETWARE)
struct servent *serv_ptr;
char *env;
MySG(default_port) = MYSQL_PORT;
if ((serv_ptr = getservbyname("mysql", "tcp"))) {
MySG(default_port) = (uint)ntohs((ushort)serv_ptr->s_port);
}
if ((env = getenv("MYSQL_TCP_PORT"))) {
MySG(default_port) = (uint)atoi(env);
}
#else
MySG(default_port) = MYSQL_PORT;
#endif
}
#if PHP_API_VERSION < 20160731
if (PG(sql_safe_mode)) {
#else
if (0) {
#endif
if (ZEND_NUM_ARGS()>0) {
php_error_docref(NULL, E_NOTICE, "SQL safe mode in effect - ignoring host/user/password information");
}
user = php_get_current_user();
hashed_details = zend_string_alloc(sizeof("mysql___") + strlen(user) - 1, 0);
snprintf(ZSTR_VAL(hashed_details), ZSTR_LEN(hashed_details) + 1, "mysql__%s_", user);
client_flags = CLIENT_INTERACTIVE;
} else {
/* mysql_pconnect does not support new_link parameter */
if (persistent) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s!s!s!l", &host_and_port, &host_len,
&user, &user_len, &passwd, &passwd_len,
&client_flags)==FAILURE) {
return;
}
} else {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s!s!s!bl", &host_and_port, &host_len,
&user, &user_len, &passwd, &passwd_len,
&new_link, &client_flags)==FAILURE) {
return;
}
}
if (!host_and_port) {
host_and_port = MySG(default_host);
}
if (!user) {
user = MySG(default_user);
}
if (!passwd) {
passwd = MySG(default_password);
passwd_len = passwd? strlen(passwd):0;
}
/* disable local infile option for open_basedir */
#if PHP_API_VERSION < 20100412
if (((PG(open_basedir) && PG(open_basedir)[0] != '\0') || PG(safe_mode)) && (client_flags & CLIENT_LOCAL_FILES)) {
#else
if ((PG(open_basedir) && PG(open_basedir)[0] != '\0') && (client_flags & CLIENT_LOCAL_FILES)) {
#endif
client_flags ^= CLIENT_LOCAL_FILES;
}
#ifdef CLIENT_MULTI_RESULTS
client_flags |= CLIENT_MULTI_RESULTS; /* compatibility with 5.2, see bug#50416 */
#endif
#ifdef CLIENT_MULTI_STATEMENTS
client_flags &= ~CLIENT_MULTI_STATEMENTS; /* don't allow multi_queries via connect parameter */
#endif
hashed_details = zend_string_alloc(sizeof("mysql____") + (host_and_port? strlen(host_and_port) : 0)
+ (user? strlen(user) : 0) + (passwd? strlen(passwd) : 0) + MAX_LENGTH_OF_LONG - 1, 0);
ZSTR_LEN(hashed_details) = snprintf(ZSTR_VAL(hashed_details), ZSTR_LEN(hashed_details) + 1, "mysql_%s_%s_%s_" ZEND_LONG_FMT, SAFE_STRING(host_and_port), SAFE_STRING(user), SAFE_STRING(passwd), client_flags);
}
/* We cannot use mysql_port anymore in windows, need to use
* mysql_real_connect() to set the port.
*/
if (host_and_port && (tmp = strchr(host_and_port, ':'))) {
host = estrndup(host_and_port, tmp-host_and_port);
free_host = 1;
tmp++;
if (tmp[0] != '/') {
port = atoi(tmp);
if ((tmp=strchr(tmp, ':'))) {
tmp++;
socket=tmp;
}
} else {
socket = tmp;
}
} else {
host = host_and_port;
port = MySG(default_port);
}
#if MYSQL_VERSION_ID < 32200
mysql_port = port;
#endif
if (!MySG(allow_persistent)) {
persistent = 0;
}
if (persistent) {
zend_resource *le;
#if PHP_VERSION_ID < 70300
zend_string *phashed;
#endif
/* try to find if we already have this link in our persistent list */
if ((le = zend_hash_find_ptr(&EG(persistent_list), hashed_details)) == NULL) { /* we don't */
#if PHP_VERSION_ID < 70300
zval new_le;
#endif
if (MySG(max_links) != -1 && MySG(num_links) >= MySG(max_links)) {
php_error_docref(NULL, E_WARNING, "Too many open links (" ZEND_LONG_FMT ")", MySG(num_links));
zend_string_release(hashed_details);
MYSQL_DO_CONNECT_RETURN_FALSE();
}
if (MySG(max_persistent) != -1 && MySG(num_persistent) >= MySG(max_persistent)) {
php_error_docref(NULL, E_WARNING, "Too many open persistent links (" ZEND_LONG_FMT ")", MySG(num_persistent));
zend_string_release(hashed_details);
MYSQL_DO_CONNECT_RETURN_FALSE();
}
/* create the link */
mysql = (php_mysql_conn *)malloc(sizeof(php_mysql_conn));
if (!mysql) {
php_error_docref(NULL, E_ERROR,
"Out of memory while allocating memory for a persistent link");
}
mysql->active_result_res = NULL;
#ifdef CLIENT_MULTI_STATEMENTS
mysql->multi_query = client_flags & CLIENT_MULTI_STATEMENTS? 1:0;
#else
mysql->multi_query = 0;
#endif
#ifndef MYSQL_USE_MYSQLND
mysql->conn = mysql_init(NULL);
#elif PHP_VERSION_ID >= 80100
mysql->conn = mysqlnd_init(MYSQLND_CLIENT_NO_FLAG, persistent);
#else
mysql->conn = mysqlnd_init(MYSQLND_CLIENT_KNOWS_RSET_COPY_DATA, persistent);
#endif
if (connect_timeout != -1) {
mysql_options(mysql->conn, MYSQL_OPT_CONNECT_TIMEOUT, (const char *)&connect_timeout);
}
#ifndef MYSQL_USE_MYSQLND
if (mysql_real_connect(mysql->conn, host, user, passwd, NULL, port, socket, client_flags)==NULL)
#elif PHP_VERSION_ID >= 80100
if (mysqlnd_connect(mysql->conn, host, user, passwd, passwd_len, NULL, 0, port, socket, client_flags, MYSQLND_CLIENT_NO_FLAG) == NULL)
#else
if (mysqlnd_connect(mysql->conn, host, user, passwd, passwd_len, NULL, 0, port, socket, client_flags, MYSQLND_CLIENT_KNOWS_RSET_COPY_DATA) == NULL)
#endif
{
/* Populate connect error globals so that the error functions can read them */
if (MySG(connect_error) != NULL) {
efree(MySG(connect_error));
}
MySG(connect_error) = estrdup(mysql_error(mysql->conn));
php_error_docref(NULL, E_WARNING, "%s", MySG(connect_error));
#if defined(HAVE_MYSQL_ERRNO)
MySG(connect_errno) = mysql_errno(mysql->conn);
#endif
free(mysql);
zend_string_release(hashed_details);
MYSQL_DO_CONNECT_RETURN_FALSE();
}
mysql_options(mysql->conn, MYSQL_OPT_LOCAL_INFILE, (char *)&MySG(allow_local_infile));
#if PHP_VERSION_ID < 70300
/* hash it up */
ZVAL_NEW_PERSISTENT_RES(&new_le, -1, mysql, le_plink);
/* avoid bogus memleak report */
phashed = zend_string_init(ZSTR_VAL(hashed_details), ZSTR_LEN(hashed_details), 1);
if (zend_hash_update(&EG(persistent_list), phashed, &new_le) == NULL) {
zend_string_release(phashed);
#else
if (zend_register_persistent_resource(ZSTR_VAL(hashed_details), ZSTR_LEN(hashed_details), mysql, le_plink) == NULL) {
#endif
free(mysql);
zend_string_release(hashed_details);
MYSQL_DO_CONNECT_RETURN_FALSE();
}
#if PHP_VERSION_ID < 70300
zend_string_release(phashed);
#endif
MySG(num_persistent)++;
MySG(num_links)++;
} else { /* The link is in our list of persistent connections */
if (le->type != le_plink) {
MYSQL_DO_CONNECT_RETURN_FALSE();
}
mysql = (php_mysql_conn *)le->ptr;
mysql->active_result_res = NULL;
#ifdef CLIENT_MULTI_STATEMENTS
mysql->multi_query = client_flags & CLIENT_MULTI_STATEMENTS? 1:0;
#else
mysql->multi_query = 0;
#endif
/* ensure that the link did not die */
#if defined(A0) && MYSQL_USE_MYSQLND
mysqlnd_end_psession(mysql->conn);
#endif
if (mysql_ping(mysql->conn)) {
if (mysql_errno(mysql->conn) == 2006) {
#ifndef MYSQL_USE_MYSQLND
if (mysql_real_connect(mysql->conn, host, user, passwd, NULL, port, socket, client_flags)==NULL)
#elif PHP_VERSION_ID >= 80100
if (mysqlnd_connect(mysql->conn, host, user, passwd, passwd_len, NULL, 0, port, socket, client_flags, MYSQLND_CLIENT_NO_FLAG) == NULL)
#else
if (mysqlnd_connect(mysql->conn, host, user, passwd, passwd_len, NULL, 0, port, socket, client_flags, MYSQLND_CLIENT_KNOWS_RSET_COPY_DATA) == NULL)
#endif
{
php_error_docref(NULL, E_WARNING, "Link to server lost, unable to reconnect");
zend_hash_del(&EG(persistent_list), hashed_details);
zend_string_release(hashed_details);
MYSQL_DO_CONNECT_RETURN_FALSE();
}
mysql_options(mysql->conn, MYSQL_OPT_LOCAL_INFILE, (char *)&MySG(allow_local_infile));
}
} else {
#ifdef MYSQL_USE_MYSQLND
mysqlnd_restart_psession(mysql->conn);
#endif
}
}
ZVAL_RES(return_value, zend_register_resource(mysql, le_plink));
} else { /* non persistent */
zend_resource *index_ptr;
zval 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 mysql link sits.
* if it doesn't, open a new mysql link, add it to the resource list,