Skip to content

Commit 5076fb0

Browse files
committed
Merge pull request #376
2 parents 0fa2246 + 9717d5c commit 5076fb0

File tree

8 files changed

+314
-119
lines changed

8 files changed

+314
-119
lines changed

php_phongo.c

Lines changed: 249 additions & 104 deletions
Large diffs are not rendered by default.

php_phongo.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ ZEND_BEGIN_MODULE_GLOBALS(mongodb)
4444
char *debug;
4545
FILE *debug_fd;
4646
bson_mem_vtable_t bsonMemVTable;
47+
HashTable clients;
4748
ZEND_END_MODULE_GLOBALS(mongodb)
4849

4950
#if PHP_VERSION_ID >= 70000
@@ -134,7 +135,7 @@ void php_phongo_read_preference_to_zval(zval *retval, const mongoc_read_prefs_t
134135
void php_phongo_write_concern_to_zval(zval *retval, const mongoc_write_concern_t *write_concern);
135136
void php_phongo_cursor_to_zval(zval *retval, const mongoc_cursor_t *cursor);
136137

137-
bool phongo_manager_init(php_phongo_manager_t *manager, const char *uri_string, bson_t *bson_options, zval *driverOptions TSRMLS_DC);
138+
void phongo_manager_init(php_phongo_manager_t *manager, const char *uri_string, zval *options, zval *driverOptions TSRMLS_DC);
138139
void php_phongo_objectid_new_from_oid(zval *object, const bson_oid_t *oid TSRMLS_DC);
139140
void php_phongo_cursor_id_new_from_id(zval *object, int64_t cursorid TSRMLS_DC);
140141
void php_phongo_new_utcdatetime_from_epoch(zval *object, int64_t msec_since_epoch TSRMLS_DC);

php_phongo_structs.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ typedef struct {
8181
typedef struct {
8282
PHONGO_ZEND_OBJECT_PRE
8383
mongoc_client_t *client;
84-
char *pem_file;
8584
PHONGO_ZEND_OBJECT_POST
8685
} php_phongo_manager_t;
8786

src/MongoDB/Manager.c

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,55 @@ PHONGO_API zend_class_entry *php_phongo_manager_ce;
5151

5252
zend_object_handlers php_phongo_handler_manager;
5353

54+
/* Checks if driverOptions contains a stream context resource in the "context"
55+
* key and incorporates any of its SSL options into the base array that did not
56+
* already exist (i.e. array union). The "context" key is then unset from the
57+
* base array.
58+
*
59+
* This handles the merging of any legacy SSL context options and also makes
60+
* driverOptions suitable for serialization by removing the resource zval. */
61+
static bool php_phongo_manager_merge_context_options(zval *zdriverOptions TSRMLS_DC)
62+
{
63+
php_stream_context *context;
64+
zval *zcontext, *zcontextOptions;
65+
66+
if (!php_array_existsc(zdriverOptions, "context")) {
67+
return true;
68+
}
69+
70+
zcontext = php_array_fetchc(zdriverOptions, "context");
71+
context = php_stream_context_from_zval(zcontext, 1);
72+
73+
if (!context) {
74+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "\"context\" driver option is not a valid Stream-Context resource");
75+
return false;
76+
}
77+
78+
#if PHP_VERSION_ID >= 70000
79+
zcontextOptions = php_array_fetchc_array(&context->options, "ssl");
80+
#else
81+
zcontextOptions = php_array_fetchc_array(context->options, "ssl");
82+
#endif
83+
84+
if (!zcontextOptions) {
85+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Stream-Context resource does not contain \"ssl\" options array");
86+
return false;
87+
}
88+
89+
/* Perform array union (see: add_function() in zend_operators.c) */
90+
#if PHP_VERSION_ID >= 70000
91+
zend_hash_merge(Z_ARRVAL_P(zdriverOptions), Z_ARRVAL_P(zcontextOptions), zval_add_ref, 0);
92+
#else
93+
{
94+
zval *tmp;
95+
zend_hash_merge(Z_ARRVAL_P(zdriverOptions), Z_ARRVAL_P(zcontextOptions), (void (*)(void *pData)) zval_add_ref, (void *) &tmp, sizeof(zval *), 0);
96+
}
97+
#endif
98+
99+
php_array_unsetc(zdriverOptions, "context");
100+
return true;
101+
}
102+
54103
/* {{{ proto void Manager::__construct([string $uri = "mongodb://127.0.0.1/"[, array $options = array()[, array $driverOptions = array()]]])
55104
Constructs a new Manager */
56105
PHP_METHOD(Manager, __construct)
@@ -60,26 +109,27 @@ PHP_METHOD(Manager, __construct)
60109
char *uri_string = NULL;
61110
phongo_zpp_char_len uri_string_len = 0;
62111
zval *options = NULL;
63-
bson_t bson_options = BSON_INITIALIZER;
64112
zval *driverOptions = NULL;
65113
SUPPRESS_UNUSED_WARNING(return_value) SUPPRESS_UNUSED_WARNING(return_value_ptr) SUPPRESS_UNUSED_WARNING(return_value_used)
66114

67115

68116
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling TSRMLS_CC);
69117
intern = Z_MANAGER_OBJ_P(getThis());
70118

119+
/* Separate the driverOptions zval, since we may end up modifying it in
120+
* php_phongo_manager_merge_context_options() below. */
71121
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!a!a!", &uri_string, &uri_string_len, &options, &driverOptions) == FAILURE) {
72122
zend_restore_error_handling(&error_handling TSRMLS_CC);
73123
return;
74124
}
75125
zend_restore_error_handling(&error_handling TSRMLS_CC);
76126

77-
if (options) {
78-
phongo_zval_to_bson(options, PHONGO_BSON_NONE, &bson_options, NULL TSRMLS_CC);
127+
if (driverOptions && !php_phongo_manager_merge_context_options(driverOptions TSRMLS_CC)) {
128+
/* Exception should already have been thrown */
129+
return;
79130
}
80131

81-
phongo_manager_init(intern, uri_string ? uri_string : PHONGO_MANAGER_URI_DEFAULT, &bson_options, driverOptions TSRMLS_CC);
82-
bson_destroy(&bson_options);
132+
phongo_manager_init(intern, uri_string ? uri_string : PHONGO_MANAGER_URI_DEFAULT, options, driverOptions TSRMLS_CC);
83133
}
84134
/* }}} */
85135

@@ -359,7 +409,8 @@ static void php_phongo_manager_free_object(phongo_free_object_arg *object TSRMLS
359409
zend_object_std_dtor(&intern->std TSRMLS_CC);
360410

361411
if (intern->client) {
362-
mongoc_client_destroy(intern->client);
412+
MONGOC_DEBUG("Not destroying persistent client for Manager");
413+
intern->client = NULL;
363414
}
364415

365416
#if PHP_VERSION_ID < 70000

tests/connect/standalone-x509-auth-001.phpt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ $driverOptions = [
1414
'weak_cert_validation' => false,
1515
'ca_file' => $SSL_DIR . '/ca.pem',
1616
'pem_file' => $SSL_DIR . '/client.pem',
17-
// TODO: this doesn't appear to have any effect. Does the PEM file not have a password?
18-
'pem_pwd' => 'qwerty',
1917
];
2018

2119
$manager = new MongoDB\Driver\Manager(STANDALONE_X509, ['ssl' => true], $driverOptions);

tests/connect/standalone-x509-auth-002.phpt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ $driverOptions = [
1616
'allow_self_signed' => false, // "weak_cert_validation" alias
1717
'cafile' => $SSL_DIR . '/ca.pem', // "ca_file" alias
1818
'local_cert' => $SSL_DIR . '/client.pem', // "pem_file" alias
19-
// TODO: this doesn't appear to have any effect. Does the PEM file not have a password?
20-
'passphrase' => 'qwerty', // "pem_pwd" alias
2119
],
2220
]),
2321
];

tests/manager/manager-debug-001.phpt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@ require_once __DIR__ . "/../utils/basic.inc";
1010
$name = tempnam(sys_get_temp_dir(), "PHONGO");
1111
unlink($name);
1212
mkdir($name);
13-
$manager = new MongoDB\Driver\Manager(STANDALONE, array(), array("debug" => $name));
13+
ini_set('mongodb.debug', $name);
14+
$manager = new MongoDB\Driver\Manager(STANDALONE);
1415
$bulk = new MongoDB\Driver\BulkWrite();
1516
$bulk->insert(array('_id' => 1, 'x' => 1));
1617
$result = $manager->executeBulkWrite(NS, $bulk);
18+
ini_set('mongodb.debug', 'off');
1719
foreach(glob($name."/*") as $file);
1820
$content = file($file);
1921
unlink($file);
2022
rmdir($name);
2123

22-
echo $content[0];
24+
echo $content[0], $content[1];
2325
foreach($content as $line) {
2426
if (strpos($line, "mongoc_bulk_operation_execute")) {
2527
echo $line;
@@ -30,6 +32,7 @@ foreach($content as $line) {
3032
===DONE===
3133
<?php exit(0); ?>
3234
--EXPECTF--
35+
[%s] PHONGO: DEBUG > Connection string: '%s'
3336
[%s] PHONGO: DEBUG > Creating Manager, phongo-1.%d.%d%S[%s] - mongoc-1.%s(%s), libbson-1.%s(%s), php-%s
3437
[%s] mongoc: TRACE > ENTRY: mongoc_bulk_operation_execute():%d
3538
[%s] mongoc: TRACE > EXIT: mongoc_bulk_operation_execute():%d

tests/manager/manager-debug-002.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ $bulk = new MongoDB\Driver\BulkWrite();
1414
$bulk->insert(array('_id' => 1, 'x' => 1));
1515
$result = $manager->executeBulkWrite(NS, $bulk);
1616

17-
ini_set("mongodb.debug", "off");
1817
?>
1918
===DONE===
2019
<?php exit(0); ?>
@@ -23,3 +22,4 @@ ini_set("mongodb.debug", "off");
2322
[%s] PHONGO: DEBUG > Creating Manager, phongo-1.%d.%d%S[%s] - mongoc-1.%s(%s), libbson-1.%s(%s), php-%s
2423
%a
2524
===DONE===
25+
%a

0 commit comments

Comments
 (0)