@@ -997,11 +997,11 @@ int php_phongo_peer_verify(php_stream *stream, X509 *cert, const char *hostname,
997
997
zval * * verify_peer_name ;
998
998
999
999
/* This option is available since PHP 5.6.0 */
1000
- if (php_stream_context_get_option (stream -> context , "ssl" , "verify_peer_name" , & verify_peer_name ) == SUCCESS && zend_is_true (* verify_peer_name )) {
1000
+ if (php_stream_context_get_option (PHP_STREAM_CONTEXT ( stream ) , "ssl" , "verify_peer_name" , & verify_peer_name ) == SUCCESS && zend_is_true (* verify_peer_name )) {
1001
1001
zval * * zhost = NULL ;
1002
1002
const char * peer ;
1003
1003
1004
- if (php_stream_context_get_option (stream -> context , "ssl" , "peer_name" , & zhost ) == SUCCESS ) {
1004
+ if (php_stream_context_get_option (PHP_STREAM_CONTEXT ( stream ) , "ssl" , "peer_name" , & zhost ) == SUCCESS ) {
1005
1005
convert_to_string_ex (zhost );
1006
1006
peer = Z_STRVAL_PP (zhost );
1007
1007
} else {
@@ -1018,23 +1018,35 @@ int php_phongo_peer_verify(php_stream *stream, X509 *cert, const char *hostname,
1018
1018
}
1019
1019
#endif
1020
1020
1021
- #ifdef PHONGO_TODO_SSL
1022
1021
bool php_phongo_ssl_verify (php_stream * stream , const char * hostname , bson_error_t * error TSRMLS_DC )
1023
1022
{
1023
+ #if PHP_VERSION_ID >= 70000
1024
+ zval * zcert ;
1025
+ zval * verify_expiry ;
1026
+ #else
1024
1027
zval * * zcert ;
1025
1028
zval * * verify_expiry ;
1029
+ #endif
1026
1030
X509 * cert ;
1027
1031
1028
- if (!stream -> context ) {
1032
+ if (!PHP_STREAM_CONTEXT ( stream ) ) {
1029
1033
return true;
1030
1034
}
1031
1035
1032
- if (!(php_stream_context_get_option (stream -> context , "ssl" , "peer_certificate" , & zcert ) == SUCCESS && Z_TYPE_PP (zcert ) == IS_RESOURCE )) {
1036
+ #if PHP_VERSION_ID >= 70000
1037
+ if (!((zcert = php_stream_context_get_option (PHP_STREAM_CONTEXT (stream ), "ssl" , "peer_certificate" )) != NULL && Z_TYPE_P (zcert ) == IS_RESOURCE )) {
1038
+ #else
1039
+ if (!(php_stream_context_get_option (PHP_STREAM_CONTEXT (stream ), "ssl" , "peer_certificate" , & zcert ) == SUCCESS && Z_TYPE_PP (zcert ) == IS_RESOURCE )) {
1040
+ #endif
1033
1041
bson_set_error (error , MONGOC_ERROR_STREAM , MONGOC_ERROR_STREAM_CONNECT , "Could not capture certificate of %s" , hostname );
1034
1042
return false;
1035
1043
}
1036
1044
1045
+ #if PHP_VERSION_ID >= 70000
1046
+ cert = (X509 * )x509_from_zval (zcert TSRMLS_CC );
1047
+ #else
1037
1048
cert = (X509 * )x509_from_zval (* zcert TSRMLS_CC );
1049
+ #endif
1038
1050
if (!cert ) {
1039
1051
bson_set_error (error , MONGOC_ERROR_STREAM , MONGOC_ERROR_STREAM_CONNECT , "Could not get certificate of %s" , hostname );
1040
1052
return false;
@@ -1046,7 +1058,11 @@ bool php_phongo_ssl_verify(php_stream *stream, const char *hostname, bson_error_
1046
1058
}
1047
1059
#endif
1048
1060
1049
- if (php_stream_context_get_option (stream -> context , "ssl" , "verify_expiry" , & verify_expiry ) == SUCCESS && zend_is_true (* verify_expiry )) {
1061
+ #if PHP_VERSION_ID >= 70000
1062
+ if ((verify_expiry = php_stream_context_get_option (PHP_STREAM_CONTEXT (stream ), "ssl" , "verify_expiry" )) != NULL && zend_is_true (verify_expiry )) {
1063
+ #else
1064
+ if (php_stream_context_get_option (PHP_STREAM_CONTEXT (stream ), "ssl" , "verify_expiry" , & verify_expiry ) == SUCCESS && zend_is_true (* verify_expiry )) {
1065
+ #endif
1050
1066
time_t current = time (NULL );
1051
1067
time_t valid_from = php_mongo_asn1_time_to_time_t (X509_get_notBefore (cert ) TSRMLS_CC );
1052
1068
time_t valid_until = php_mongo_asn1_time_to_time_t (X509_get_notAfter (cert ) TSRMLS_CC );
@@ -1063,7 +1079,6 @@ bool php_phongo_ssl_verify(php_stream *stream, const char *hostname, bson_error_
1063
1079
1064
1080
return true;
1065
1081
}
1066
- #endif
1067
1082
1068
1083
mongoc_stream_t * phongo_stream_initiator (const mongoc_uri_t * uri , const mongoc_host_list_t * host , void * user_data , bson_error_t * error ) /* {{{ */
1069
1084
{
@@ -1136,18 +1151,17 @@ mongoc_stream_t* phongo_stream_initiator(const mongoc_uri_t *uri, const mongoc_h
1136
1151
efree (uniqid );
1137
1152
1138
1153
if (mongoc_uri_get_ssl (uri )) {
1139
- #ifdef PHONGO_TODO_SSL
1140
1154
zend_error_handling error_handling ;
1141
1155
1142
1156
zend_replace_error_handling (EH_THROW , php_phongo_sslconnectionexception_ce , & error_handling TSRMLS_CC );
1143
1157
1144
1158
MONGOC_DEBUG ("Enabling SSL" );
1145
1159
1146
1160
/* Capture the server certificate so we can do further verification */
1147
- if (stream -> context ) {
1161
+ if (PHP_STREAM_CONTEXT ( stream ) ) {
1148
1162
zval capture ;
1149
1163
ZVAL_BOOL (& capture , 1 );
1150
- php_stream_context_set_option (stream -> context , "ssl" , "capture_peer_cert" , & capture );
1164
+ php_stream_context_set_option (PHP_STREAM_CONTEXT ( stream ) , "ssl" , "capture_peer_cert" , & capture );
1151
1165
}
1152
1166
1153
1167
if (php_stream_xport_crypto_setup (stream , PHONGO_CRYPTO_METHOD , NULL TSRMLS_CC ) < 0 ) {
@@ -1174,7 +1188,6 @@ mongoc_stream_t* phongo_stream_initiator(const mongoc_uri_t *uri, const mongoc_h
1174
1188
}
1175
1189
1176
1190
zend_restore_error_handling (& error_handling TSRMLS_CC );
1177
- #endif
1178
1191
}
1179
1192
efree (dsn );
1180
1193
@@ -1552,9 +1565,37 @@ static mongoc_uri_t *php_phongo_make_uri(const char *uri_string, bson_t *options
1552
1565
return uri ;
1553
1566
} /* }}} */
1554
1567
1555
- #ifdef PHONGO_TODO_SSL
1556
1568
void php_phongo_populate_default_ssl_ctx (php_stream_context * ctx , zval * driverOptions ) /* {{{ */
1557
1569
{
1570
+ #if PHP_VERSION_ID >= 70000
1571
+ zval * tmp ;
1572
+
1573
+ #define SET_STRING_CTX (name ) \
1574
+ if (driverOptions && php_array_exists(driverOptions, name)) { \
1575
+ zval ztmp; \
1576
+ zend_bool ctmp_free; \
1577
+ int ctmp_len; \
1578
+ char *ctmp; \
1579
+ ctmp = php_array_fetchl_string(driverOptions, name, sizeof(name)-1, &ctmp_len, &ctmp_free); \
1580
+ ZVAL_STRING(&ztmp, ctmp); \
1581
+ if (ctmp_free) { \
1582
+ str_efree(ctmp); \
1583
+ } \
1584
+ php_stream_context_set_option(ctx, "ssl", name, &ztmp); \
1585
+ }
1586
+ #define SET_BOOL_CTX (name , defaultvalue ) \
1587
+ { \
1588
+ zval ztmp; \
1589
+ if (driverOptions && php_array_exists(driverOptions, name)) { \
1590
+ ZVAL_BOOL(&ztmp, php_array_fetchl_bool(driverOptions, ZEND_STRL(name))); \
1591
+ php_stream_context_set_option(ctx, "ssl", name, &ztmp); \
1592
+ } \
1593
+ else if ((tmp = php_stream_context_get_option(ctx, "ssl", name)) == NULL) { \
1594
+ ZVAL_BOOL(&ztmp, defaultvalue); \
1595
+ php_stream_context_set_option(ctx, "ssl", name, &ztmp); \
1596
+ } \
1597
+ }
1598
+ #else
1558
1599
zval * * tmp ;
1559
1600
1560
1601
#define SET_STRING_CTX (name ) \
@@ -1567,7 +1608,6 @@ void php_phongo_populate_default_ssl_ctx(php_stream_context *ctx, zval *driverOp
1567
1608
ZVAL_STRING(&ztmp, ctmp, ctmp_free); \
1568
1609
php_stream_context_set_option(ctx, "ssl", name, &ztmp); \
1569
1610
}
1570
-
1571
1611
#define SET_BOOL_CTX (name , defaultvalue ) \
1572
1612
{ \
1573
1613
zval ztmp; \
@@ -1580,6 +1620,7 @@ void php_phongo_populate_default_ssl_ctx(php_stream_context *ctx, zval *driverOp
1580
1620
php_stream_context_set_option(ctx, "ssl", name, &ztmp); \
1581
1621
} \
1582
1622
}
1623
+ #endif
1583
1624
1584
1625
SET_BOOL_CTX ("verify_peer" , 1 );
1585
1626
SET_BOOL_CTX ("verify_peer_name" , 1 );
@@ -1597,7 +1638,6 @@ void php_phongo_populate_default_ssl_ctx(php_stream_context *ctx, zval *driverOp
1597
1638
#undef SET_BOOL_CTX
1598
1639
#undef SET_STRING_CTX
1599
1640
} /* }}} */
1600
- #endif
1601
1641
1602
1642
static bool php_phongo_apply_rp_options_to_client (mongoc_client_t * client , bson_t * options TSRMLS_DC ) /* {{{ */
1603
1643
{
@@ -1821,19 +1861,20 @@ static mongoc_client_t *php_phongo_make_mongo_client(const mongoc_uri_t *uri, zv
1821
1861
}
1822
1862
#endif
1823
1863
1824
- #ifdef PHONGO_TODO_STREAM
1864
+ #if PHP_VERSION_ID >= 70000
1865
+ if (driverOptions && (tmp = zend_hash_str_find (Z_ARRVAL_P (driverOptions ), "context" , sizeof ("context" )- 1 )) != NULL ) {
1866
+ ctx = php_stream_context_from_zval (tmp , 0 );
1867
+ #else
1825
1868
if (driverOptions && zend_hash_find (Z_ARRVAL_P (driverOptions ), "context" , strlen ("context" ) + 1 , (void * * )& tmp ) == SUCCESS ) {
1826
1869
ctx = php_stream_context_from_zval (* tmp , 0 );
1870
+ #endif
1827
1871
} else {
1828
1872
GET_DEFAULT_CONTEXT ();
1829
1873
}
1830
- #endif
1831
1874
1832
- #ifdef PHONGO_TODO_SSL
1833
1875
if (mongoc_uri_get_ssl (uri )) {
1834
1876
php_phongo_populate_default_ssl_ctx (ctx , driverOptions );
1835
1877
}
1836
- #endif
1837
1878
1838
1879
#ifdef HAVE_SYSTEM_LIBMONGOC
1839
1880
mongoc_version = mongoc_get_version ();
@@ -1866,23 +1907,33 @@ static mongoc_client_t *php_phongo_make_mongo_client(const mongoc_uri_t *uri, zv
1866
1907
mech = mongoc_uri_get_auth_mechanism (uri );
1867
1908
1868
1909
/* Check if we are doing X509 auth, in which case extract the username (subject) from the cert if no username is provided */
1869
- #ifdef PHONGO_TODO_SSL
1870
1910
if (mech && !strcasecmp (mech , "MONGODB-X509" ) && !mongoc_uri_get_username (uri )) {
1911
+ #if PHP_VERSION_ID >= 70000
1912
+ zval * pem ;
1913
+ #else
1871
1914
zval * * pem ;
1915
+ #endif
1872
1916
1917
+ #if PHP_VERSION_ID >= 70000
1918
+ if ((pem = php_stream_context_get_option (ctx , "ssl" , "local_cert" )) != NULL ) {
1919
+ #else
1873
1920
if (SUCCESS == php_stream_context_get_option (ctx , "ssl" , "local_cert" , & pem )) {
1921
+ #endif
1874
1922
char filename [MAXPATHLEN ];
1875
1923
1924
+ #if PHP_VERSION_ID >= 70000
1925
+ if (VCWD_REALPATH (zval_get_string (pem )-> val , filename )) {
1926
+ #else
1876
1927
convert_to_string_ex (pem );
1877
1928
if (VCWD_REALPATH (Z_STRVAL_PP (pem ), filename )) {
1929
+ #endif
1878
1930
mongoc_ssl_opt_t ssl_options ;
1879
1931
1880
1932
ssl_options .pem_file = filename ;
1881
1933
mongoc_client_set_ssl_opts (client , & ssl_options );
1882
1934
}
1883
1935
}
1884
1936
}
1885
- #endif
1886
1937
1887
1938
mongoc_client_set_stream_initiator (client , phongo_stream_initiator , ctx );
1888
1939
0 commit comments