31
31
#include "mongoc-read-prefs-private.h"
32
32
#include "mongoc-bulk-operation-private.h"
33
33
#include "mongoc-write-concern-private.h"
34
+ #include "mongoc-uri-private.h"
34
35
#include "mongoc-trace.h"
35
36
36
37
@@ -1495,41 +1496,62 @@ void php_phongo_cursor_to_zval(zval *retval, php_phongo_cursor_t *cursor) /* {{{
1495
1496
/* }}} */
1496
1497
1497
1498
1498
- mongoc_client_t * php_phongo_make_mongo_client (const char * uri , zval * driverOptions TSRMLS_DC ) /* {{{ */
1499
+ mongoc_uri_t * php_phongo_make_uri (const char * uri_string , bson_t * options TSRMLS_DC ) /* {{{ */
1499
1500
{
1500
- zval * * tmp ;
1501
- php_stream_context * ctx ;
1502
- const char * mech ;
1503
- const mongoc_uri_t * muri ;
1504
- mongoc_client_t * client = mongoc_client_new (uri );
1505
-
1501
+ bson_iter_t iter ;
1502
+ mongoc_uri_t * uri ;
1506
1503
1507
- ENTRY ;
1504
+ uri = mongoc_uri_new (uri_string );
1505
+ MONGOC_DEBUG ("Connection string: '%s'" , uri_string );
1508
1506
1509
- if (driverOptions && zend_hash_find (Z_ARRVAL_P (driverOptions ), "debug" , strlen ("debug" ) + 1 , (void * * )& tmp ) == SUCCESS ) {
1510
- convert_to_string (* tmp );
1507
+ if (options && bson_iter_init (& iter , options )) {
1508
+ while (bson_iter_next (& iter )) {
1509
+ const char * key = bson_iter_key (& iter );
1511
1510
1512
- zend_alter_ini_entry_ex ((char * )PHONGO_DEBUG_INI , sizeof (PHONGO_DEBUG_INI ), Z_STRVAL_PP (tmp ), Z_STRLEN_PP (tmp ), PHP_INI_USER , PHP_INI_STAGE_RUNTIME , 0 TSRMLS_CC );
1513
- }
1514
-
1515
- MONGOC_DEBUG ("Creating Manager, phongo-%s[%s] - mongoc-%s, libbson-%s" , MONGODB_VERSION_S , MONGODB_STABILITY_S , MONGOC_VERSION_S , BSON_VERSION_S );
1516
- MONGOC_DEBUG ("Connecting to '%s'" , uri );
1517
-
1518
- if (!client ) {
1519
- RETURN (false);
1511
+ if (mongoc_uri_option_is_bool (key )) {
1512
+ mongoc_uri_set_option_as_bool (uri , key , bson_iter_as_bool (& iter ));
1513
+ }
1514
+ else if (mongoc_uri_option_is_int32 (key ) && BSON_ITER_HOLDS_INT32 (& iter )) {
1515
+ mongoc_uri_set_option_as_int32 (uri , key , bson_iter_int32 (& iter ));
1516
+ }
1517
+ else if (mongoc_uri_option_is_utf8 (key ) && BSON_ITER_HOLDS_UTF8 (& iter )) {
1518
+ mongoc_uri_set_option_as_utf8 (uri , key , bson_iter_utf8 (& iter , NULL ));
1519
+ }
1520
+ else if (BSON_ITER_HOLDS_ARRAY (& iter ) && !strcasecmp (key , "hosts" )) {
1521
+ bson_iter_t sub ;
1522
+
1523
+ bson_iter_recurse (& iter , & sub );
1524
+ while (bson_iter_next (& sub )) {
1525
+ if (BSON_ITER_HOLDS_UTF8 (& sub )) {
1526
+ mongoc_uri_parse_host (uri , bson_iter_utf8 (& sub , NULL ));
1527
+ }
1528
+ }
1529
+ }
1530
+ else if (BSON_ITER_HOLDS_UTF8 (& iter )) {
1531
+ const char * value = bson_iter_utf8 (& iter , NULL );
1532
+
1533
+ if (!strcasecmp (key , "username" )) {
1534
+ mongoc_uri_set_username (uri , value );
1535
+ } else if (!strcasecmp (key , "password" )) {
1536
+ mongoc_uri_set_password (uri , value );
1537
+ } else if (!strcasecmp (key , "database" )) {
1538
+ mongoc_uri_set_database (uri , value );
1539
+ } else if (!strcasecmp (key , "authsource" )) {
1540
+ mongoc_uri_set_auth_source (uri , value );
1541
+ }
1542
+ }
1543
+ }
1520
1544
}
1521
1545
1522
- if (driverOptions && zend_hash_find (Z_ARRVAL_P (driverOptions ), "context" , strlen ("context" ) + 1 , (void * * )& tmp ) == SUCCESS ) {
1523
- ctx = php_stream_context_from_zval (* tmp , 0 );
1524
- } else {
1525
- GET_DEFAULT_CONTEXT ();
1526
- }
1527
- muri = mongoc_client_get_uri (client );
1546
+ return uri ;
1547
+ } /* }}} */
1528
1548
1529
- if (driverOptions && mongoc_uri_get_ssl (muri )) {
1549
+ void php_phongo_populate_default_ssl_ctx (php_stream_context * ctx , zval * driverOptions ) /* {{{ */
1550
+ {
1551
+ zval * * tmp ;
1530
1552
1531
1553
#define SET_STRING_CTX (name ) \
1532
- if (php_array_exists(driverOptions, name)) { \
1554
+ if (driverOptions && php_array_exists(driverOptions, name)) { \
1533
1555
zval ztmp; \
1534
1556
zend_bool ctmp_free; \
1535
1557
int ctmp_len; \
@@ -1542,7 +1564,7 @@ mongoc_client_t *php_phongo_make_mongo_client(const char *uri, zval *driverOptio
1542
1564
#define SET_BOOL_CTX (name , defaultvalue ) \
1543
1565
{ \
1544
1566
zval ztmp; \
1545
- if (php_array_exists(driverOptions, name)) { \
1567
+ if (driverOptions && php_array_exists(driverOptions, name)) { \
1546
1568
ZVAL_BOOL(&ztmp, php_array_fetchl_bool(driverOptions, ZEND_STRL(name))); \
1547
1569
php_stream_context_set_option(ctx, "ssl", name, &ztmp); \
1548
1570
} \
@@ -1567,12 +1589,46 @@ mongoc_client_t *php_phongo_make_mongo_client(const char *uri, zval *driverOptio
1567
1589
SET_STRING_CTX ("ciphers" );
1568
1590
#undef SET_BOOL_CTX
1569
1591
#undef SET_STRING_CTX
1592
+ } /* }}} */
1593
+
1594
+ mongoc_client_t * php_phongo_make_mongo_client (const mongoc_uri_t * uri , zval * driverOptions TSRMLS_DC ) /* {{{ */
1595
+ {
1596
+ zval * * tmp ;
1597
+ php_stream_context * ctx ;
1598
+ const char * mech ;
1599
+ mongoc_client_t * client ;
1600
+
1601
+ ENTRY ;
1602
+
1603
+
1604
+ if (driverOptions && zend_hash_find (Z_ARRVAL_P (driverOptions ), "debug" , strlen ("debug" ) + 1 , (void * * )& tmp ) == SUCCESS ) {
1605
+ convert_to_string (* tmp );
1606
+
1607
+ zend_alter_ini_entry_ex ((char * )PHONGO_DEBUG_INI , sizeof (PHONGO_DEBUG_INI ), Z_STRVAL_PP (tmp ), Z_STRLEN_PP (tmp ), PHP_INI_USER , PHP_INI_STAGE_RUNTIME , 0 TSRMLS_CC );
1608
+ }
1609
+
1610
+ if (driverOptions && zend_hash_find (Z_ARRVAL_P (driverOptions ), "context" , strlen ("context" ) + 1 , (void * * )& tmp ) == SUCCESS ) {
1611
+ ctx = php_stream_context_from_zval (* tmp , 0 );
1612
+ } else {
1613
+ GET_DEFAULT_CONTEXT ();
1614
+ }
1615
+
1616
+ if (mongoc_uri_get_ssl (uri )) {
1617
+ php_phongo_populate_default_ssl_ctx (ctx , driverOptions );
1570
1618
}
1571
1619
1572
- mech = mongoc_uri_get_auth_mechanism (muri );
1620
+ MONGOC_DEBUG ("Creating Manager, phongo-%s[%s] - mongoc-%s, libbson-%s" , MONGODB_VERSION_S , MONGODB_STABILITY_S , MONGOC_VERSION_S , BSON_VERSION_S );
1621
+ client = mongoc_client_new_from_uri (uri );
1622
+
1623
+ if (!client ) {
1624
+ RETURN (false);
1625
+ }
1626
+
1627
+
1628
+ mech = mongoc_uri_get_auth_mechanism (uri );
1573
1629
1574
1630
/* Check if we are doing X509 auth, in which case extract the username (subject) from the cert if no username is provided */
1575
- if (mech && !strcasecmp (mech , "MONGODB-X509" ) && !mongoc_uri_get_username (muri )) {
1631
+ if (mech && !strcasecmp (mech , "MONGODB-X509" ) && !mongoc_uri_get_username (uri )) {
1576
1632
zval * * pem ;
1577
1633
1578
1634
if (SUCCESS == php_stream_context_get_option (ctx , "ssl" , "local_cert" , & pem )) {
0 commit comments