forked from akissa/php-couchdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcouchdb.c
2500 lines (1977 loc) · 70.4 KB
/
couchdb.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 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2009 |
+----------------------------------------------------------------------+
| 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. |
+----------------------------------------------------------------------+
| Author: Andrew Colin Kissa <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "zend_exceptions.h"
#include "ext/standard/info.h"
#include "ext/standard/php_string.h"
#include "ext/standard/php_smart_str.h"
#include "ext/standard/url.h"
#include "Zend/zend_extensions.h"
#include "php_couchdb.h"
#include "ext/json/php_json.h"
#include <curl/curl.h>
#define CAAL(s, v) add_assoc_long_ex(returned_info, s, sizeof(s), (long) v);
#define CAAD(s, v) add_assoc_double_ex(returned_info, s, sizeof(s), (double) v);
#define CAAS(s, v) add_assoc_string_ex(returned_info, s, sizeof(s), (char *) (v ? v : ""), 1);
#define COUCHDB_ERROR(errcode, msg, ... ) \
zend_throw_exception_ex(couchdb_exception_ce_ptr, errcode TSRMLS_CC, msg, ##__VA_ARGS__);
#define FREE_ARGS_HASH(a) \
if (a) { \
zend_hash_destroy(a); \
FREE_HASHTABLE(a); \
}
#define PROCESS_JSON_RESULT(code, object, assoc) \
if (code != -1 && object->lastresponse.c) { \
MAKE_STD_ZVAL(zret); \
ZVAL_STRINGL(zret, object->lastresponse.c, object->lastresponse.len, 1); \
couchdb_set_response_args(object->properties, zret TSRMLS_CC); \
php_json_decode(return_value, object->lastresponse.c, object->lastresponse.len, assoc TSRMLS_CC); \
return; \
}else{ \
RETURN_NULL(); \
} \
#define PROCESS_JSON_RESULT_EX(code, object, assoc) \
if (code != -1 && object->lastresponse.c) { \
MAKE_STD_ZVAL(zret); \
ZVAL_STRINGL(zret, object->lastresponse.c, object->lastresponse.len, 1); \
couchdb_set_response_args(object->properties, zret TSRMLS_CC); \
php_json_decode(return_value, object->lastresponse.c, object->lastresponse.len, assoc TSRMLS_CC); \
return; \
}else{ \
COUCHDB_ERROR(0, object->lastresponse.c); \
} \
#define PROCESS_JSON_RESULT_COMPART(code, object, assoc) \
long depth = JSON_PARSER_DEFAULT_DEPTH; \
if (code != -1 && object->lastresponse.c) { \
MAKE_STD_ZVAL(zret); \
ZVAL_STRINGL(zret, object->lastresponse.c, object->lastresponse.len, 1); \
couchdb_set_response_args(object->properties, zret TSRMLS_CC); \
php_json_decode(return_value, object->lastresponse.c, object->lastresponse.len, assoc, depth TSRMLS_CC); \
return; \
}else{ \
RETURN_NULL(); \
} \
#define PROCESS_JSON_RESULT_COMPART_EX(code, object, assoc) \
long depth = JSON_PARSER_DEFAULT_DEPTH; \
if (code != -1 && object->lastresponse.c) { \
MAKE_STD_ZVAL(zret); \
ZVAL_STRINGL(zret, object->lastresponse.c, object->lastresponse.len, 1); \
couchdb_set_response_args(object->properties, zret TSRMLS_CC); \
php_json_decode(return_value, object->lastresponse.c, object->lastresponse.len, assoc, depth TSRMLS_CC); \
return; \
}else{ \
if (object->lastresponse.len) { \
COUCHDB_ERROR(0, object->lastresponse.c); \
}else{ \
COUCHDB_ERROR(0, "Unknown error"); \
} \
} \
#define PROCESS_BOOL_RESULT(code, object, rcode) \
if (code == rcode && object->lastresponse.c) { \
MAKE_STD_ZVAL(zret); \
ZVAL_STRINGL(zret, object->lastresponse.c, object->lastresponse.len, 1); \
couchdb_set_response_args(object->properties, zret TSRMLS_CC); \
RETURN_TRUE; \
}else{ \
RETURN_FALSE; \
} \
#define PROCESS_BOOL_RESULT_EX(code, object, rcode) \
if (code == rcode && object->lastresponse.c) { \
MAKE_STD_ZVAL(zret); \
ZVAL_STRINGL(zret, object->lastresponse.c, object->lastresponse.len, 1); \
couchdb_set_response_args(object->properties, zret TSRMLS_CC); \
RETURN_TRUE; \
}else{ \
if (object->lastresponse.len) { \
COUCHDB_ERROR(0, object->lastresponse.c); \
}else { \
COUCHDB_ERROR(0, "Unknown error"); \
}\
} \
#define CHECK_DB_NAME(object) \
db_name_len = Z_STRLEN_PP(couchdb_get_property(object, COUCHDB_DB TSRMLS_CC)); \
if (!db_name_len) { \
COUCHDB_ERROR(0, COUCHDB_DB_NOT_SET); \
return; \
} \
db_name = Z_STRVAL_PP(couchdb_get_property(object, COUCHDB_DB TSRMLS_CC)); \
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
#ifndef MIN
# define MIN(a,b) (a<b?a:b)
#endif
#ifndef JSON_PARSER_DEFAULT_DEPTH
#define JSON_PARSER_DEFAULT_DEPTH 512
#endif
static zend_object_handlers couchdb_client_handlers;
static zend_class_entry *couchdb_client_ce_ptr = NULL;
static zend_class_entry *couchdb_exception_ce_ptr = NULL;
typedef struct _php_couchdb_object {
zend_object std;
HashTable *properties;
void ***thread_ctx;
smart_str lastresponse;
smart_str lastrequest;
smart_str cookie;
zend_bool use_cookie_auth;
uint previous:28;
zval *this_ptr;
} php_couchdb_object;
static int couchdb_add_req_arg(HashTable *ht, const char *arg, const char *val TSRMLS_DC) /* {{{ */
{
zval *varg;
ulong h;
MAKE_STD_ZVAL(varg);
ZVAL_STRING(varg, (char *)val, 1);
h = zend_hash_func((char *)arg, strlen(arg)+1);
zend_hash_quick_update(ht, (char *)arg, strlen(arg)+1, h, &varg, sizeof(zval *), NULL);
return SUCCESS;
}
/* }}} */
static char *couchdb_encode_url(char *url) /* {{{ */
{
char *encoded_url = NULL, *rets;
int encoded_url_len, rets_len;
if (url) {
encoded_url = php_raw_url_encode(url, strlen(url), &encoded_url_len);
}
if (encoded_url) {
rets = php_str_to_str_ex(encoded_url, encoded_url_len, "%7E", sizeof("%7E")-1, "~", sizeof("~")-1, &rets_len, 0, NULL);
efree(encoded_url);
return rets;
}
return NULL;
}
/* }}} */
int couchdb_build_query(smart_str *s, HashTable *args, zend_bool prepend_amp) /* {{{ */
{
HashPosition pos;
void *current_val;
char *arg_key = NULL, *current_key = NULL, *param_value;
int numargs = 0;
ulong idx = 0;
if (args) {
for (zend_hash_internal_pointer_reset_ex(args, &pos); zend_hash_get_current_key_ex(args, ¤t_key, 0, &idx, 0, &pos) != HASH_KEY_NON_EXISTANT;
zend_hash_move_forward_ex(args, &pos)) {
if (current_key) {
if (prepend_amp) {
smart_str_appendc(s, '&');
}
zend_hash_get_current_data_ex(args, (void **)¤t_val, &pos);
arg_key = couchdb_encode_url(current_key);
if (Z_TYPE_PP((zval **)current_val) == IS_STRING) {
param_value = couchdb_encode_url(Z_STRVAL_PP((zval **)current_val));
}else {
SEPARATE_ZVAL((zval **)current_val);
convert_to_string_ex((zval **)current_val);
param_value = couchdb_encode_url(Z_STRVAL_PP((zval **)current_val));
}
if (arg_key && param_value) {
smart_str_appends(s, arg_key);
efree(arg_key);
}else {
efree(arg_key);
}
if (param_value) {
smart_str_appendc(s, '=');
smart_str_appends(s, param_value);
efree(param_value);
}
prepend_amp = TRUE;
++numargs;
}
}
}
return numargs;
}
/* }}} */
static smart_str *http_url_concat(smart_str *surl) /* {{{ */
{
smart_str_0(surl);
if (!strchr(surl->c, '?')) {
smart_str_appendc(surl, '?');
}else {
smart_str_appendc(surl, '&');
}
return surl;
}
/* }}} */
static int couchdb_set_response_args(HashTable *hasht, zval *data TSRMLS_DC) /* {{{ */
{
if (data && Z_TYPE_P(data) == IS_STRING) {
ulong h = zend_hash_func(COUCHDB_LAST_RAW_RES, sizeof(COUCHDB_LAST_RAW_RES));
return zend_hash_quick_update(hasht, COUCHDB_LAST_RAW_RES, sizeof(COUCHDB_LAST_RAW_RES), h, &data, sizeof(zval *), NULL);
}
return FAILURE;
}
/* }}} */
static inline zval **couchdb_get_property(php_couchdb_object *local_client, char *prop_name TSRMLS_DC) /* {{{ */
{
size_t prop_len = 0;
void *data_ptr;
ulong h;
prop_len = strlen(prop_name);
h = zend_hash_func(prop_name, prop_len+1);
if (zend_hash_quick_find(local_client->properties, prop_name, prop_len+1, h, (void **)&data_ptr) == SUCCESS) {
return (zval **)data_ptr;
}
return NULL;
}
/* }}} */
static inline int couchdb_set_property(php_couchdb_object *local_client, zval *prop, char *prop_name TSRMLS_DC) /* {{{ */
{
size_t prop_len = 0;
ulong h;
prop_len = strlen(prop_name);
h = zend_hash_func(prop_name, prop_len+1);
return zend_hash_quick_update(local_client->properties, prop_name, prop_len+1, h, (void *)&prop, sizeof(zval *), NULL);
}
/* }}} */
static void couchdb_hash_dtor(php_couchdb_object *local_client TSRMLS_DC) /* {{{ */
{
HashTable *ht;
ht = local_client->properties;
FREE_ARGS_HASH(ht);
}
/* }}} */
static size_t couchdb_read_response(char *ptr, size_t size, size_t nmemb, void *ctx) /* {{{ */
{
uint relsize;
php_couchdb_object *local_client = (php_couchdb_object *)ctx;
relsize = size * nmemb;
smart_str_appendl(&local_client->lastresponse, ptr, relsize);
return relsize;
}
/* }}} */
static size_t couchdb_write_response(void *ptr, size_t size, size_t nmemb, void *ctx) /* {{{ */
{
php_couchdb_object *local_client = (php_couchdb_object *)ctx;
if (local_client->lastrequest.len) {
size_t out = MIN(size * nmemb, local_client->lastrequest.len - local_client->previous);
if (out) {
memcpy(ptr, ((char *) local_client->lastrequest.c) + local_client->previous, out);
local_client->previous += out;
return out;
}
}
return 0;
}
/* }}} */
static CURLcode couchdb_make_request(php_couchdb_object *local_client, const char *url, const smart_str *payload, const char *http_method, HashTable *request_headers TSRMLS_DC) /* {{{ */
{
CURLcode cres, crres, cookie_result;
CURL *curl;
zval **zca_info, **zca_path;
void *p_cur;
struct curl_slist *curl_headers = NULL, *cookies, *cookie_itr;
long response_code = -1;
char *current_key;
smart_str rheader = {0};
uint current_key_len;
ulong num_key;
zca_info = couchdb_get_property(local_client, COUCHDB_CA_INFO TSRMLS_CC);
zca_path = couchdb_get_property(local_client, COUCHDB_CA_PATH TSRMLS_CC);
curl = curl_easy_init();
curl_headers = curl_slist_append(curl_headers, COUCHDB_ACCEPT_HEADERS);
if (request_headers) {
for (zend_hash_internal_pointer_reset(request_headers); zend_hash_get_current_data(request_headers, (void **)&p_cur) == SUCCESS;
zend_hash_move_forward(request_headers)) {
if (HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(request_headers, ¤t_key, ¤t_key_len, &num_key, 0, NULL)) {
smart_str_appends(&rheader, current_key);
smart_str_appends(&rheader, ": ");
smart_str_appends(&rheader, Z_STRVAL_PP((zval **)p_cur));
smart_str_0(&rheader);
curl_headers = curl_slist_append(curl_headers, rheader.c);
smart_str_free(&rheader);
}
}
}
if (payload->len) {
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, payload->c);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, payload->len);
}
if (local_client->lastrequest.len && strcmp(http_method, COUCHDB_PUT) == 0) {
curl_easy_setopt(curl, CURLOPT_PUT, TRUE);
curl_easy_setopt(curl, CURLOPT_UPLOAD, TRUE);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, couchdb_write_response);
curl_easy_setopt(curl, CURLOPT_READDATA, local_client);
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, local_client->lastrequest.len);
#if !defined(ZTS)
curl_headers = curl_slist_append(curl_headers, "Transfer-Encoding: chunked");
#endif
}
curl_easy_setopt(curl, CURLOPT_URL, url);
if(zca_path && Z_STRLEN_PP(zca_path)) {
curl_easy_setopt(curl, CURLOPT_CAPATH, Z_STRVAL_PP(zca_path));
}
if(zca_info && Z_STRLEN_PP(zca_info)) {
curl_easy_setopt(curl, CURLOPT_CAINFO, Z_STRVAL_PP(zca_info));
}
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "");
if (local_client->use_cookie_auth) {
curl_easy_setopt(curl, CURLOPT_COOKIE, local_client->cookie.c);
curl_headers = curl_slist_append(curl_headers, "X-CouchDB-WWW-Authenticate: Cookie");
curl_headers = curl_slist_append(curl_headers, "Authorization:");
}
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, http_method);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 4L);
curl_headers = curl_slist_append(curl_headers, "Expect:");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, curl_headers);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, couchdb_read_response);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, local_client);
#if defined(ZTS)
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
#endif
smart_str_free(&local_client->lastresponse);
cres = curl_easy_perform(curl);
smart_str_0(&local_client->lastresponse);
smart_str_free(&local_client->lastrequest);
curl_slist_free_all(curl_headers);
if (CURLE_OK == cres) {
crres = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
cookie_result = curl_easy_getinfo(curl, CURLINFO_COOKIELIST, &cookies);
if (CURLE_OK == cookie_result) {
char *token, *p;
int x = 1;
cookie_itr = cookies;
while (cookie_itr) {
p = php_strtok_r(cookie_itr->data, "\t", &token);
while (p) {
if (x == 6) {
smart_str_free(&local_client->cookie);
smart_str_appends(&local_client->cookie, p);
smart_str_appendc(&local_client->cookie, '=');
}else if (x == 7) {
smart_str_appends(&local_client->cookie, p);
}
x++;
p = php_strtok_r(NULL, "\t", &token);
}
cookie_itr = cookie_itr->next;
}
smart_str_0(&local_client->cookie);
curl_slist_free_all(cookies);
}else {
php_error(E_WARNING, COUCHDB_COOKIE_NOT_GOT);
}
}else {
crres = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
php_error(E_WARNING, COUCHDB_SERVER_FAIL, local_client->lastresponse.c);
}
curl_easy_cleanup(curl);
return response_code;
}
/* }}} */
static long couchdb_prepare_request(php_couchdb_object *local_client, const char *url, const char *method, zval *request_params, HashTable *request_headers, int get_flags TSRMLS_DC) /* {{{ */
{
long http_response_code;
smart_str surl = {0}, payload = {0}, postdata = {0};
HashTable *rargs = NULL;
if (request_params) {
switch (Z_TYPE_P(request_params)) {
case IS_ARRAY:
rargs = HASH_OF(request_params);
couchdb_build_query(&postdata, rargs, 0);
break;
case IS_STRING:
smart_str_appendl(&postdata, Z_STRVAL_P(request_params), Z_STRLEN_P(request_params));
break;
}
}
smart_str_0(&postdata);
smart_str_0(&surl);
smart_str_appends(&surl, url);
http_response_code = -1;
if (strcmp(method, COUCHDB_GET) == 0 || strcmp(method, COUCHDB_DELETE) == 0) {
if (postdata.len) {
smart_str_append(http_url_concat(&surl), &postdata);
}
}else {
if (get_flags && postdata.len) {
smart_str_append(http_url_concat(&surl), &postdata);
}else {
smart_str_append(&payload, &postdata);
smart_str_0(&payload);
}
}
smart_str_0(&surl);
http_response_code = couchdb_make_request(local_client, surl.c, &payload, method, request_headers TSRMLS_CC);
smart_str_free(&payload);
if (http_response_code < COUCHDB_STATUS_OK || http_response_code > COUCHDB_STATUS_END) {
http_response_code = -1;
}
smart_str_free(&surl);
smart_str_free(&postdata);
return http_response_code;
}
/* }}} */
static int couchdb_cookie_login(php_couchdb_object *local_client, char *url, char *user_name, char *password TSRMLS_DC) /* {{{ */
{
zval *auth_string;
char *buff, *euser_name, *epassword;
int tlen = 0;
long http_response_code;
euser_name = couchdb_encode_url(user_name);
epassword = couchdb_encode_url(password);
tlen = spprintf(&buff, 0, "username=%s&password=%s", euser_name, epassword);
MAKE_STD_ZVAL(auth_string);
ZVAL_STRINGL(auth_string, buff, tlen, 1);
efree(euser_name);
efree(epassword);
efree(buff);
http_response_code = couchdb_prepare_request(local_client, url, COUCHDB_POST, auth_string, NULL, 0 TSRMLS_CC);
zval_ptr_dtor(&auth_string);
if (http_response_code == COUCHDB_STATUS_OK) {
return TRUE;
}else {
return FALSE;
}
}
/* }}} */
static int couchdb_cookie_logout(php_couchdb_object *local_client, char *url TSRMLS_DC) /* {{{ */
{
long http_response_code;
http_response_code = couchdb_prepare_request(local_client, url, COUCHDB_DELETE, NULL, NULL, 0 TSRMLS_CC);
if (http_response_code == COUCHDB_STATUS_OK) {
return TRUE;
}else {
return FALSE;
}
}
/* }}} */
static inline php_couchdb_object *fetch_couchdb_object(zval *obj TSRMLS_DC) /* {{{ */
{
php_couchdb_object *local_client = (php_couchdb_object *)zend_object_store_get_object(obj TSRMLS_CC);
local_client->this_ptr = obj;
return local_client;
}
/* }}} */
static zval *couchdb_read_member(zval *obj, zval *mem, int type TSRMLS_DC) /* {{{ */
{
zval *return_value = NULL;
php_couchdb_object *local_client;
local_client = fetch_couchdb_object(obj TSRMLS_CC);
return_value = zend_get_std_object_handlers()->read_property(obj, mem, type TSRMLS_CC);
return return_value;
}
/* }}} */
static void couchdb_write_member(zval *obj, zval *mem, zval *value TSRMLS_DC) /* {{{ */
{
char *property;
php_couchdb_object *local_client;
property = Z_STRVAL_P(mem);
local_client = fetch_couchdb_object(obj TSRMLS_CC);
zend_get_std_object_handlers()->write_property(obj, mem, value TSRMLS_CC);
}
/* }}} */
static void couchdb_object_free_storage(void *obj TSRMLS_DC) /* {{{ */
{
php_couchdb_object *local_client;
local_client = (php_couchdb_object *) obj;
zend_object_std_dtor(&local_client->std TSRMLS_CC);
if (local_client->lastresponse.c) {
smart_str_free(&local_client->lastresponse);
}
if (local_client->lastrequest.c) {
smart_str_free(&local_client->lastrequest);
}
if (local_client->cookie.c) {
smart_str_free(&local_client->cookie);
}
efree(obj);
}
/* }}} */
static zend_object_value couchdb_client_new(zend_class_entry *ce TSRMLS_DC) /* {{{ */
{
zend_object_value retval;
php_couchdb_object *local_client;
local_client = ecalloc(1, sizeof(php_couchdb_object));
zend_object_std_init(&(local_client->std), ce TSRMLS_CC);
retval.handle = zend_objects_store_put(local_client, (zend_objects_store_dtor_t)zend_objects_destroy_object, couchdb_object_free_storage, NULL TSRMLS_CC);
retval.handlers = (zend_object_handlers *) &couchdb_client_handlers;
return retval;
}
/* }}} */
static void _couchdb_getdbinfo(INTERNAL_FUNCTION_PARAMETERS, int type) /* {{{ */
{
php_couchdb_object *local_client;
char *url = NULL, *db_name, *edb_name;
int db_name_len = 0;
smart_str surl = {0};
long http_response_code;
zend_bool assoc = 1;
zval * zret = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &db_name, &db_name_len) == FAILURE) {
return;
}
local_client = fetch_couchdb_object(getThis() TSRMLS_CC);
if (!db_name_len) {
CHECK_DB_NAME(local_client);
}
edb_name = couchdb_encode_url(db_name);
url = Z_STRVAL_PP(couchdb_get_property(local_client, COUCHDB_URL TSRMLS_CC));
smart_str_appends(&surl, url);
smart_str_appendc(&surl, '/');
smart_str_appends(&surl, edb_name);
smart_str_0(&surl);
efree(edb_name);
http_response_code = couchdb_prepare_request(local_client, surl.c, COUCHDB_GET, NULL, NULL, 0 TSRMLS_CC);
smart_str_free(&surl);
#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 3) || (PHP_MAJOR_VERSION > 5)
PROCESS_JSON_RESULT_COMPART(http_response_code, local_client, assoc);
#else
PROCESS_JSON_RESULT(http_response_code, local_client, assoc);
#endif
}
/* }}} */
static void _couchdb_sendrequest(INTERNAL_FUNCTION_PARAMETERS, int type, zend_bool throw_exception) /* {{{ */
{
php_couchdb_object *local_client;
char *url = NULL, *db_name, *edb_name, *ldb_name=NULL;
int db_name_len = 0;
smart_str surl = {0};
long http_response_code, comp_response;
zval * zret = NULL, *dbp = NULL;
HashTable *rheaders = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &db_name, &db_name_len) == FAILURE) {
return;
}
local_client = fetch_couchdb_object(getThis() TSRMLS_CC);
if (!db_name_len) {
CHECK_DB_NAME(local_client);
}
edb_name = couchdb_encode_url(db_name);
url = Z_STRVAL_PP(couchdb_get_property(local_client, COUCHDB_URL TSRMLS_CC));
smart_str_appends(&surl, url);
smart_str_appendc(&surl, '/');
smart_str_appends(&surl, edb_name);
if (type == COUCHDB_DB_COMPACT) {
smart_str_appends(&surl, COUCHDB_COMPACT);
}
smart_str_0(&surl);
efree(edb_name);
if (type == COUCHDB_DB_COMPACT) {
ALLOC_HASHTABLE(rheaders);
zend_hash_init(rheaders, 0, NULL, ZVAL_PTR_DTOR, 0);
couchdb_add_req_arg(rheaders, "Content-Type", "application/json" TSRMLS_CC);
comp_response = COUCHDB_STATUS_ACCEPTED;
http_response_code = couchdb_prepare_request(local_client, surl.c, COUCHDB_POST, NULL, rheaders, 0 TSRMLS_CC);
} else if (type == COUCHDB_DB_DELETE) {
comp_response = COUCHDB_STATUS_OK;
http_response_code = couchdb_prepare_request(local_client, surl.c, COUCHDB_DELETE, NULL, NULL, 0 TSRMLS_CC);
ldb_name = "";
MAKE_STD_ZVAL(dbp);
ZVAL_STRING(dbp, ldb_name, 1);
if (couchdb_set_property(local_client, dbp, COUCHDB_DB TSRMLS_CC) != SUCCESS) {
COUCHDB_ERROR(0, COUCHDB_DB_DESELECT_FAIL);
return;
}
}else{
comp_response = COUCHDB_STATUS_CREATED;
http_response_code = couchdb_prepare_request(local_client, surl.c, COUCHDB_PUT, NULL, NULL, 0 TSRMLS_CC);
}
smart_str_free(&surl);
if (throw_exception) {
PROCESS_BOOL_RESULT_EX(http_response_code, local_client, comp_response);
}else {
PROCESS_BOOL_RESULT(http_response_code, local_client, comp_response);
}
}
/* }}} */
/* ---- METHODS ---- */
/* {{{ proto void CouchdbClient::__construct(string url [,bool use_cookie_auth [, string db_name]])
Constructor of the CouchdbClient class */
TC_METHOD(__construct)
{
HashTable *hasht;
php_couchdb_object *local_client;
zval *urp, *dbp, *obj = NULL, *zuser_name, *zpassword;
char *uri, *db_name = NULL, *striped_url;
smart_str surl = {0};
int uri_len, db_name_len = 0;
php_url *urlparts;
zend_bool use_cookie_auth = 0;
obj = getThis();
local_client = fetch_couchdb_object(obj TSRMLS_CC);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|bs", &uri, &uri_len, &use_cookie_auth, &db_name, &db_name_len) == FAILURE) {
return;
}
if (!uri_len) {
COUCHDB_ERROR(0, COUCHDB_INVALID_URI);
return;
}
urlparts = php_url_parse_ex(uri, strlen(uri));
if (!urlparts || !urlparts->host || !urlparts->scheme) {
COUCHDB_ERROR(0, COUCHDB_INVALID_URI);
php_url_free(urlparts);
return;
}
if ((strcmp(urlparts->scheme, "http") != 0) && (strcmp(urlparts->scheme, "https") != 0)) {
COUCHDB_ERROR(0, COUCHDB_INVALID_SCHEME, urlparts->scheme);
php_url_free(urlparts);
return;
}
/* check if user auth details were supplied */
if ((!urlparts->user || !urlparts->pass) && use_cookie_auth) {
COUCHDB_ERROR(0, COUCHDB_COOKIE_PARAMS);
php_url_free(urlparts);
return;
}
local_client->cookie.c = NULL;
local_client->lastresponse.c = NULL;
local_client->lastrequest.c = NULL;
local_client->previous = 0;
local_client->use_cookie_auth = 0;
if (!db_name_len) {
db_name = "";
}
TSRMLS_SET_CTX(local_client->thread_ctx);
if (local_client->properties) {
zend_hash_clean(local_client->properties);
hasht = local_client->properties;
}else {
ALLOC_HASHTABLE(hasht);
zend_hash_init(hasht, 0, NULL, ZVAL_PTR_DTOR, 0);
local_client->properties = hasht;
}
MAKE_STD_ZVAL(urp);
ZVAL_STRINGL(urp, uri, uri_len, 1);
if (couchdb_set_property(local_client, urp, COUCHDB_URL TSRMLS_CC) != SUCCESS) {
return;
}
MAKE_STD_ZVAL(dbp);
ZVAL_STRINGL(dbp, db_name, db_name_len, 1);
if (couchdb_set_property(local_client, dbp, COUCHDB_DB TSRMLS_CC) != SUCCESS) {
return;
}
if (use_cookie_auth) {
/* do cookie login */
if (urlparts->port) {
spprintf(&striped_url, 0, "%s://%s:%d", urlparts->scheme, urlparts->host, urlparts->port);
smart_str_appends(&surl, striped_url);
smart_str_appends(&surl, COUCHDB_SESSION);
smart_str_0(&surl);
}else {
spprintf(&striped_url, 0, "%s://%s", urlparts->scheme, urlparts->host);
smart_str_appends(&surl, striped_url);
smart_str_appends(&surl, COUCHDB_SESSION);
smart_str_0(&surl);
}
if (couchdb_cookie_login(local_client, surl.c, urlparts->user, urlparts->pass TSRMLS_CC) == 0) {
COUCHDB_ERROR(0, COUCHDB_COOKIE_AUTH_FAILURE);
FREE_ARGS_HASH(local_client->properties);
efree(striped_url);
php_url_free(urlparts);
smart_str_free(&surl);
return;
}
smart_str_free(&surl);
uri = striped_url;
efree(striped_url);
local_client->use_cookie_auth = 1;
}
MAKE_STD_ZVAL(zuser_name);
MAKE_STD_ZVAL(zpassword);
if (use_cookie_auth) {
ZVAL_STRING(zuser_name, urlparts->user, 1);
ZVAL_STRING(zpassword, urlparts->pass, 1);
}else {
ZVAL_STRING(zuser_name, "", 1);
ZVAL_STRING(zpassword, "", 1);
}
php_url_free(urlparts);
if (couchdb_set_property(local_client, zuser_name, COUCHDB_USER TSRMLS_CC) != SUCCESS) {
return;
}
if (couchdb_set_property(local_client, zpassword, COUCHDB_PASSWORD TSRMLS_CC) != SUCCESS) {
return;
}
}
/* }}} */
/* {{{ CouchdbClient::__destruct
Destructor for CouchdbClient */
TC_METHOD(__destruct)
{
php_couchdb_object *local_client;
char *url;
smart_str surl = {0};
local_client = fetch_couchdb_object(getThis() TSRMLS_CC);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE) {
return;
}
if (local_client->cookie.c) {
url = Z_STRVAL_PP(couchdb_get_property(local_client, COUCHDB_URL TSRMLS_CC));
smart_str_appends(&surl, url);
smart_str_appends(&surl, COUCHDB_SESSION);
smart_str_0(&surl);
couchdb_cookie_logout(local_client, surl.c TSRMLS_CC);
smart_str_free(&local_client->cookie);
smart_str_free(&surl);
}
couchdb_hash_dtor(local_client TSRMLS_CC);
}
/* }}} */
/* {{{ proto bool CouchdbClient::compactDatabase([, string db_name ])
Compacts a CouchDB database
*/
TC_METHOD(compactDatabase)
{
_couchdb_sendrequest(INTERNAL_FUNCTION_PARAM_PASSTHRU, COUCHDB_DB_COMPACT, FALSE);
}
/* }}} */
/* {{{ proto bool CouchdbClient::createDatabase([, string db_name ])
Creates a CouchDB database
Returns true on success and false for failure.
*/
TC_METHOD(createDatabase)
{
_couchdb_sendrequest(INTERNAL_FUNCTION_PARAM_PASSTHRU, COUCHDB_DB_NONE, TRUE);
}
/* }}} */
/* {{{ proto bool CouchdbClient::deleteDatabase([, string db_name])
Drops (deletes) a CouchDB database
*/
TC_METHOD(deleteDatabase)
{
_couchdb_sendrequest(INTERNAL_FUNCTION_PARAM_PASSTHRU, COUCHDB_DB_DELETE, TRUE);
}
/* }}} */
/* {{{ proto array CouchdbClient::getDatabaseInfo([, string db_name ])
Returns CouchDB database information
*/
TC_METHOD(getDatabaseInfo)
{
_couchdb_getdbinfo(INTERNAL_FUNCTION_PARAM_PASSTHRU, COUCHDB_DB_INFO);
}
/* }}} */
/* {{{ proto array CouchdbClient::getDatabaseChanges([, array query_options ])
Returns CouchDB database change history
*/
TC_METHOD(getDatabaseChanges)
{
php_couchdb_object *local_client;
char *url = NULL, *db_name, *edb_name;
int db_name_len = 0;
smart_str surl = {0};
long http_response_code;
zend_bool assoc = 1;
zval * zret = NULL, *query_options;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a", &query_options) == FAILURE) {
return;
}
local_client = fetch_couchdb_object(getThis() TSRMLS_CC);
CHECK_DB_NAME(local_client);
edb_name = couchdb_encode_url(db_name);
url = Z_STRVAL_PP(couchdb_get_property(local_client, COUCHDB_URL TSRMLS_CC));
smart_str_appends(&surl, url);
smart_str_appendc(&surl, '/');
smart_str_appends(&surl, COUCHDB_CHANGES);
smart_str_0(&surl);
efree(edb_name);
http_response_code = couchdb_prepare_request(local_client, surl.c, COUCHDB_GET, query_options, NULL, 0 TSRMLS_CC);
smart_str_free(&surl);
#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 3) || (PHP_MAJOR_VERSION > 5)
PROCESS_JSON_RESULT_COMPART(http_response_code, local_client, assoc);
#else