39
39
#include <php.h>
40
40
#include <php_ini.h>
41
41
#include <ext/standard/info.h>
42
- #include "Zend/zend_hash.h"
43
- #include "Zend/zend_interfaces.h"
44
- #include "Zend/zend_exceptions.h"
45
- #include "ext/spl/spl_iterators.h"
46
- #include "ext/spl/spl_exceptions.h"
42
+ #include <ext/standard/file.h>
43
+ #include <Zend/zend_hash.h>
44
+ #include <Zend/zend_interfaces.h>
45
+ #include <Zend/zend_exceptions.h>
46
+ #include <ext/spl/spl_iterators.h>
47
+ #include <ext/spl/spl_exceptions.h>
47
48
/* For formating timestamp in the log */
48
49
#include <ext/date/php_date.h>
49
50
/* Stream wrapper */
@@ -243,7 +244,7 @@ php_phongo_stream_logger phongo_stream_logger = {
243
244
/* }}} */
244
245
245
246
/* {{{ Init objects */
246
- void phongo_result_init (zval * return_value , mongoc_cursor_t * cursor , const bson_t * bson , int server_hint , zend_bool is_command_cursor TSRMLS_DC ) /* {{{ */
247
+ void phongo_result_init (zval * return_value , mongoc_cursor_t * cursor , const bson_t * bson , mongoc_client_t * client , int server_id , zend_bool is_command_cursor TSRMLS_DC ) /* {{{ */
247
248
{
248
249
php_phongo_result_t * result ;
249
250
@@ -252,23 +253,24 @@ void phongo_result_init(zval *return_value, mongoc_cursor_t *cursor, const bson_
252
253
result = (php_phongo_result_t * )zend_object_store_get_object (return_value TSRMLS_CC );
253
254
if (cursor ) {
254
255
result -> cursor = cursor ;
255
- result -> hint = mongoc_cursor_get_hint (cursor );
256
+ result -> server_id = mongoc_cursor_get_hint (cursor );
256
257
} else {
257
- result -> hint = server_hint ;
258
+ result -> server_id = server_id ;
258
259
}
260
+ result -> client = client ;
259
261
result -> is_command_cursor = is_command_cursor ;
260
262
result -> firstBatch = bson ? bson_copy (bson ) : NULL ;
261
263
} /* }}} */
262
264
263
- void phongo_server_init (zval * return_value , int hint , mongoc_host_list_t * host TSRMLS_DC ) /* {{{ */
265
+ void phongo_server_init (zval * return_value , mongoc_client_t * client , int server_id TSRMLS_DC ) /* {{{ */
264
266
{
265
267
php_phongo_server_t * server ;
266
268
267
269
object_init_ex (return_value , php_phongo_server_ce );
268
270
269
271
server = (php_phongo_server_t * )zend_object_store_get_object (return_value TSRMLS_CC );
270
- server -> hint = hint ;
271
- server -> host = host ;
272
+ server -> client = client ;
273
+ server -> server_id = server_id ;
272
274
}
273
275
/* }}} */
274
276
@@ -346,14 +348,15 @@ zend_bool phongo_writeerror_init(zval *return_value, bson_t *bson TSRMLS_DC) /*
346
348
return true;
347
349
} /* }}} */
348
350
349
- php_phongo_writeresult_t * phongo_writeresult_init (zval * return_value , mongoc_write_result_t * write_result , int server_hint TSRMLS_DC ) /* {{{ */
351
+ php_phongo_writeresult_t * phongo_writeresult_init (zval * return_value , mongoc_write_result_t * write_result , mongoc_client_t * client , int server_id TSRMLS_DC ) /* {{{ */
350
352
{
351
353
php_phongo_writeresult_t * writeresult ;
352
354
353
355
object_init_ex (return_value , php_phongo_writeresult_ce );
354
356
355
357
writeresult = (php_phongo_writeresult_t * )zend_object_store_get_object (return_value TSRMLS_CC );
356
- writeresult -> hint = server_hint ;
358
+ writeresult -> client = client ;
359
+ writeresult -> server_id = server_id ;
357
360
358
361
/* Copy write_results or else it'll get destroyed with the bulk destruction */
359
362
#define SCP (field ) writeresult->write_result.field = write_result->field
@@ -422,22 +425,22 @@ void phongo_unwrap_exception(bool retval, zval *return_value TSRMLS_DC)
422
425
}
423
426
}
424
427
425
- int phongo_execute_single_insert (mongoc_client_t * client , const char * namespace , const bson_t * doc , const mongoc_write_concern_t * write_concern , zval * return_value , int return_value_used TSRMLS_DC ) /* {{{ */
428
+ int phongo_execute_single_insert (mongoc_client_t * client , const char * namespace , const bson_t * doc , const mongoc_write_concern_t * write_concern , int server_id , zval * return_value , int return_value_used TSRMLS_DC ) /* {{{ */
426
429
{
427
430
bool retval = false;
428
431
mongoc_bulk_operation_t * bulk ;
429
432
430
433
bulk = phongo_bulkwrite_init (true);
431
434
mongoc_bulk_operation_insert (bulk , doc );
432
435
433
- retval = phongo_execute_write (client , namespace , bulk , write_concern , 0 , return_value , return_value_used TSRMLS_CC );
436
+ retval = phongo_execute_write (client , namespace , bulk , write_concern , server_id , return_value , return_value_used TSRMLS_CC );
434
437
mongoc_bulk_operation_destroy (bulk );
435
438
436
439
phongo_unwrap_exception (retval , return_value TSRMLS_CC );
437
440
return retval ;
438
441
} /* }}} */
439
442
440
- int phongo_execute_single_update (mongoc_client_t * client , const char * namespace , const bson_t * query , const bson_t * update , const mongoc_write_concern_t * write_concern , mongoc_update_flags_t flags , zval * return_value , int return_value_used TSRMLS_DC ) /* {{{ */
443
+ int phongo_execute_single_update (mongoc_client_t * client , const char * namespace , const bson_t * query , const bson_t * update , const mongoc_write_concern_t * write_concern , int server_id , mongoc_update_flags_t flags , zval * return_value , int return_value_used TSRMLS_DC ) /* {{{ */
441
444
{
442
445
bool retval = false;
443
446
mongoc_bulk_operation_t * bulk ;
@@ -448,14 +451,14 @@ int phongo_execute_single_update(mongoc_client_t *client, const char *namespace,
448
451
} else {
449
452
mongoc_bulk_operation_update (bulk , query , update , !!(flags & MONGOC_UPDATE_UPSERT ));
450
453
}
451
- retval = phongo_execute_write (client , namespace , bulk , write_concern , 0 , return_value , return_value_used TSRMLS_CC );
454
+ retval = phongo_execute_write (client , namespace , bulk , write_concern , server_id , return_value , return_value_used TSRMLS_CC );
452
455
mongoc_bulk_operation_destroy (bulk );
453
456
454
457
phongo_unwrap_exception (retval , return_value TSRMLS_CC );
455
458
return retval ;
456
459
} /* }}} */
457
460
458
- int phongo_execute_single_delete (mongoc_client_t * client , const char * namespace , const bson_t * query , const mongoc_write_concern_t * write_concern , mongoc_delete_flags_t flags , zval * return_value , int return_value_used TSRMLS_DC ) /* {{{ */
461
+ int phongo_execute_single_delete (mongoc_client_t * client , const char * namespace , const bson_t * query , const mongoc_write_concern_t * write_concern , int server_id , mongoc_delete_flags_t flags , zval * return_value , int return_value_used TSRMLS_DC ) /* {{{ */
459
462
{
460
463
bool retval = false;
461
464
mongoc_bulk_operation_t * bulk ;
@@ -467,19 +470,19 @@ int phongo_execute_single_delete(mongoc_client_t *client, const char *namespace,
467
470
mongoc_bulk_operation_remove (bulk , query );
468
471
}
469
472
470
- retval = phongo_execute_write (client , namespace , bulk , write_concern , 0 , return_value , return_value_used TSRMLS_CC );
473
+ retval = phongo_execute_write (client , namespace , bulk , write_concern , server_id , return_value , return_value_used TSRMLS_CC );
471
474
mongoc_bulk_operation_destroy (bulk );
472
475
473
476
phongo_unwrap_exception (retval , return_value TSRMLS_CC );
474
477
return retval ;
475
478
} /* }}} */
476
479
477
- bool phongo_execute_write (mongoc_client_t * client , const char * namespace , mongoc_bulk_operation_t * bulk , const mongoc_write_concern_t * write_concern , int server_hint , zval * return_value , int return_value_used TSRMLS_DC ) /* {{{ */
480
+ bool phongo_execute_write (mongoc_client_t * client , const char * namespace , mongoc_bulk_operation_t * bulk , const mongoc_write_concern_t * write_concern , int server_id , zval * return_value , int return_value_used TSRMLS_DC ) /* {{{ */
478
481
{
479
482
bson_error_t error ;
480
483
char * dbname ;
481
484
char * collname ;
482
- int hint ;
485
+ int success ;
483
486
php_phongo_writeresult_t * writeresult ;
484
487
485
488
if (!phongo_split_namespace (namespace , & dbname , & collname )) {
@@ -502,14 +505,14 @@ bool phongo_execute_write(mongoc_client_t *client, const char *namespace, mongoc
502
505
efree (dbname );
503
506
efree (collname );
504
507
505
- if (server_hint ) {
506
- mongoc_bulk_operation_set_hint (bulk , server_hint );
508
+ if (server_id > 0 ) {
509
+ mongoc_bulk_operation_set_hint (bulk , server_id );
507
510
}
508
511
509
- hint = mongoc_bulk_operation_execute (bulk , NULL , & error );
512
+ success = mongoc_bulk_operation_execute (bulk , NULL , & error );
510
513
511
514
/* Write succeeded and the user doesn't care for the results */
512
- if (hint && !return_value_used ) {
515
+ if (success && !return_value_used ) {
513
516
return true;
514
517
}
515
518
@@ -518,11 +521,11 @@ bool phongo_execute_write(mongoc_client_t *client, const char *namespace, mongoc
518
521
return false;
519
522
}
520
523
521
- writeresult = phongo_writeresult_init (return_value , & bulk -> result , server_hint TSRMLS_CC );
524
+ writeresult = phongo_writeresult_init (return_value , & bulk -> result , client , bulk -> hint TSRMLS_CC );
522
525
writeresult -> write_concern = mongoc_write_concern_copy (write_concern );
523
526
524
527
/* The Write failed */
525
- if (!hint ) {
528
+ if (!success ) {
526
529
/* The Command itself failed */
527
530
if (
528
531
bson_empty0 (& writeresult -> write_result .writeErrors )
@@ -541,7 +544,7 @@ bool phongo_execute_write(mongoc_client_t *client, const char *namespace, mongoc
541
544
return true;
542
545
} /* }}} */
543
546
544
- int phongo_execute_query (mongoc_client_t * client , const char * namespace , const php_phongo_query_t * query , const mongoc_read_prefs_t * read_preference , zval * return_value , int return_value_used TSRMLS_DC ) /* {{{ */
547
+ int phongo_execute_query (mongoc_client_t * client , const char * namespace , const php_phongo_query_t * query , const mongoc_read_prefs_t * read_preference , int server_id , zval * return_value , int return_value_used TSRMLS_DC ) /* {{{ */
545
548
{
546
549
const bson_t * doc = NULL ;
547
550
mongoc_cursor_t * cursor ;
@@ -566,6 +569,7 @@ int phongo_execute_query(mongoc_client_t *client, const char *namespace, const p
566
569
return false;
567
570
}
568
571
572
+ cursor -> hint = server_id ;
569
573
if (!mongoc_cursor_next (cursor , & doc )) {
570
574
bson_error_t error ;
571
575
@@ -583,11 +587,11 @@ int phongo_execute_query(mongoc_client_t *client, const char *namespace, const p
583
587
return true;
584
588
}
585
589
586
- phongo_result_init (return_value , cursor , doc , 0 , 0 TSRMLS_CC );
590
+ phongo_result_init (return_value , cursor , doc , client , server_id , 0 TSRMLS_CC );
587
591
return true;
588
592
} /* }}} */
589
593
590
- int phongo_execute_command (mongoc_client_t * client , const char * db , const bson_t * command , const mongoc_read_prefs_t * read_preference , zval * return_value , int return_value_used TSRMLS_DC ) /* {{{ */
594
+ int phongo_execute_command (mongoc_client_t * client , const char * db , const bson_t * command , const mongoc_read_prefs_t * read_preference , int server_id , zval * return_value , int return_value_used TSRMLS_DC ) /* {{{ */
591
595
{
592
596
mongoc_cursor_t * cursor ;
593
597
const bson_t * doc ;
@@ -596,6 +600,7 @@ int phongo_execute_command(mongoc_client_t *client, const char *db, const bson_t
596
600
597
601
598
602
cursor = mongoc_client_command (client , db , MONGOC_QUERY_NONE , 0 , 1 , 0 , command , NULL , read_preference );
603
+ cursor -> hint = server_id ;
599
604
600
605
if (!mongoc_cursor_next (cursor , & doc )) {
601
606
bson_error_t error ;
@@ -633,15 +638,15 @@ int phongo_execute_command(mongoc_client_t *client, const char *db, const bson_t
633
638
_mongoc_cursor_cursorid_init (cursor );
634
639
cursor -> limit = 0 ;
635
640
cursor -> is_command = false;
636
- phongo_result_init (return_value , cursor , & first_batch , mongoc_cursor_get_hint (cursor ), 1 TSRMLS_CC );
641
+ phongo_result_init (return_value , cursor , & first_batch , client , mongoc_cursor_get_hint (cursor ), 1 TSRMLS_CC );
637
642
return true;
638
643
}
639
644
}
640
645
}
641
646
}
642
647
}
643
648
644
- phongo_result_init (return_value , cursor , doc , mongoc_cursor_get_hint (cursor ), 0 TSRMLS_CC );
649
+ phongo_result_init (return_value , cursor , doc , client , mongoc_cursor_get_hint (cursor ), 0 TSRMLS_CC );
645
650
return true;
646
651
} /* }}} */
647
652
@@ -762,8 +767,7 @@ mongoc_stream_t* phongo_stream_get_base_stream(mongoc_stream_t *stream) /* {{{ *
762
767
return (mongoc_stream_t * ) stream ;
763
768
} /* }}} */
764
769
765
- ssize_t
766
- phongo_stream_poll (mongoc_stream_poll_t * streams , size_t nstreams , int32_t timeout )
770
+ ssize_t phongo_stream_poll (mongoc_stream_poll_t * streams , size_t nstreams , int32_t timeout ) /* {{{ */
767
771
{
768
772
php_pollfd * fds = NULL ;
769
773
size_t i ;
@@ -791,7 +795,7 @@ phongo_stream_poll (mongoc_stream_poll_t *streams, size_t nstreams, int32_t time
791
795
efree (fds );
792
796
793
797
return rval ;
794
- }
798
+ } /* }}} */
795
799
796
800
mongoc_stream_t * phongo_stream_initiator (const mongoc_uri_t * uri , const mongoc_host_list_t * host , void * user_data , bson_error_t * error ) /* {{{ */
797
801
{
@@ -1188,12 +1192,59 @@ void php_phongo_result_to_zval(zval *retval, php_phongo_result_t *result) /* {{{
1188
1192
} else {
1189
1193
add_assoc_null_ex (retval , ZEND_STRS ("firstBatch" ));
1190
1194
}
1191
- add_assoc_long_ex (retval , ZEND_STRS ("hint " ), result -> hint );
1195
+ add_assoc_long_ex (retval , ZEND_STRS ("server_id " ), result -> server_id );
1192
1196
add_assoc_bool_ex (retval , ZEND_STRS ("is_command_cursor" ), result -> is_command_cursor );
1193
1197
1194
1198
} /* }}} */
1195
1199
1196
1200
1201
+ mongoc_client_t * php_phongo_make_mongo_client (const char * uri , zval * driverOptions ) /* {{{ */
1202
+ {
1203
+ php_stream_context * ctx = NULL ;
1204
+ mongoc_client_t * client = mongoc_client_new (uri );
1205
+
1206
+ if (driverOptions ) {
1207
+ zval * * tmp ;
1208
+
1209
+ if (zend_hash_find (Z_ARRVAL_P (driverOptions ), "context" , strlen ("context" ) + 1 , (void * * )& tmp ) == SUCCESS ) {
1210
+ ctx = php_stream_context_from_zval (* tmp , PHP_FILE_NO_DEFAULT_CONTEXT );
1211
+ } else if (FG (default_context )) {
1212
+ ctx = FG (default_context );
1213
+ }
1214
+
1215
+ if (ctx ) {
1216
+ const mongoc_uri_t * muri = mongoc_client_get_uri (client );
1217
+ const char * mech = mongoc_uri_get_auth_mechanism (muri );
1218
+
1219
+ /* Check if we are doing X509 auth, in which case extract the username (subject) from the cert if no username is provided */
1220
+ if (mech && !strcasecmp (mech , "MONGODB-X509" ) && !mongoc_uri_get_username (muri )) {
1221
+ zval * * pem ;
1222
+
1223
+ if (SUCCESS == php_stream_context_get_option (ctx , "ssl" , "local_cert" , & pem )) {
1224
+ char filename [MAXPATHLEN ];
1225
+
1226
+ convert_to_string_ex (pem );
1227
+ if (VCWD_REALPATH (Z_STRVAL_PP (pem ), filename )) {
1228
+ mongoc_ssl_opt_t ssl_options ;
1229
+
1230
+ ssl_options .pem_file = filename ;
1231
+ mongoc_client_set_ssl_opts (client , & ssl_options );
1232
+ }
1233
+ }
1234
+ }
1235
+ }
1236
+
1237
+ if (zend_hash_find (Z_ARRVAL_P (driverOptions ), "debug" , strlen ("debug" ) + 1 , (void * * )& tmp ) == SUCCESS ) {
1238
+ convert_to_string (* tmp );
1239
+
1240
+ zend_alter_ini_entry_ex ((char * )"phongo.debug_log" , sizeof ("phongo.debug_log" ) , Z_STRVAL_PP (tmp ), Z_STRLEN_PP (tmp ), PHP_INI_USER , PHP_INI_STAGE_RUNTIME , 0 TSRMLS_CC );
1241
+ }
1242
+ }
1243
+
1244
+ mongoc_client_set_stream_initiator (client , phongo_stream_initiator , ctx );
1245
+
1246
+ return client ;
1247
+ } /* }}} */
1197
1248
1198
1249
void php_phongo_new_utcdatetime_from_epoch (zval * object , int64_t msec_since_epoch TSRMLS_DC ) /* {{{ */
1199
1250
{
@@ -1204,6 +1255,7 @@ void php_phongo_new_utcdatetime_from_epoch(zval *object, int64_t msec_since_epoc
1204
1255
intern = (php_phongo_utcdatetime_t * )zend_object_store_get_object (object TSRMLS_CC );
1205
1256
intern -> milliseconds = msec_since_epoch ;
1206
1257
} /* }}} */
1258
+
1207
1259
void php_phongo_new_datetime_from_utcdatetime (zval * object , int64_t milliseconds TSRMLS_DC ) /* {{{ */
1208
1260
{
1209
1261
php_date_obj * datetime_obj ;
0 commit comments