2 +--------------------------------------------------------------------+
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2010, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
15 #define HTTP_WANT_CURL
18 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
20 #include "zend_interfaces.h"
22 #include "php_http_api.h"
23 #include "php_http_exception_object.h"
24 #include "php_http_request_api.h"
25 #include "php_http_request_object.h"
26 #include "php_http_request_datashare_api.h"
27 #include "php_http_requestdatashare_object.h"
29 #define HTTP_BEGIN_ARGS(method, req_args) HTTP_BEGIN_ARGS_EX(HttpRequestDataShare, method, 0, req_args)
30 #define HTTP_EMPTY_ARGS(method) HTTP_EMPTY_ARGS_EX(HttpRequestDataShare, method, 0)
31 #define HTTP_RSHARE_ME(method, visibility) PHP_ME(HttpRequestDataShare, method, HTTP_ARGS(HttpRequestDataShare, method), visibility)
33 #if defined(HAVE_SPL) && !defined(WONKY)
34 /* SPL doesn't install its headers */
35 extern PHPAPI zend_class_entry
*spl_ce_Countable
;
38 HTTP_EMPTY_ARGS(__destruct
);
39 HTTP_EMPTY_ARGS(count
);
41 HTTP_BEGIN_ARGS(attach
, 1)
42 HTTP_ARG_OBJ(HttpRequest
, request
, 0)
44 HTTP_BEGIN_ARGS(detach
, 1)
45 HTTP_ARG_OBJ(HttpRequest
, request
, 0)
48 HTTP_EMPTY_ARGS(reset
);
50 HTTP_BEGIN_ARGS(factory
, 0)
51 HTTP_ARG_VAL(global
, 0)
52 HTTP_ARG_VAL(class_name
, 0)
56 HTTP_BEGIN_ARGS(singleton
, 0)
57 HTTP_ARG_VAL(global
, 0)
62 #define http_requestdatashare_object_read_prop _http_requestdatashare_object_read_prop
63 static zval
*_http_requestdatashare_object_read_prop(zval
*object
, zval
*member
, int type ZEND_LITERAL_KEY_DC TSRMLS_DC
);
64 #define http_requestdatashare_object_write_prop _http_requestdatashare_object_write_prop
65 static void _http_requestdatashare_object_write_prop(zval
*object
, zval
*member
, zval
*value ZEND_LITERAL_KEY_DC TSRMLS_DC
);
66 #define http_requestdatashare_instantiate(t, g) _http_requestdatashare_instantiate((t), (g) TSRMLS_CC)
67 static inline zval
*_http_requestdatashare_instantiate(zval
*this_ptr
, zend_bool global TSRMLS_DC
);
69 #define THIS_CE http_requestdatashare_object_ce
70 zend_class_entry
*http_requestdatashare_object_ce
;
71 zend_function_entry http_requestdatashare_object_fe
[] = {
72 HTTP_RSHARE_ME(__destruct
, ZEND_ACC_PUBLIC
|ZEND_ACC_DTOR
)
73 HTTP_RSHARE_ME(count
, ZEND_ACC_PUBLIC
)
74 HTTP_RSHARE_ME(attach
, ZEND_ACC_PUBLIC
)
75 HTTP_RSHARE_ME(detach
, ZEND_ACC_PUBLIC
)
76 HTTP_RSHARE_ME(reset
, ZEND_ACC_PUBLIC
)
77 HTTP_RSHARE_ME(factory
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
79 HTTP_RSHARE_ME(singleton
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
83 static zend_object_handlers http_requestdatashare_object_handlers
;
85 PHP_MINIT_FUNCTION(http_requestdatashare_object
)
87 HTTP_REGISTER_CLASS_EX(HttpRequestDataShare
, http_requestdatashare_object
, NULL
, 0);
88 http_requestdatashare_object_handlers
.clone_obj
= NULL
;
89 http_requestdatashare_object_handlers
.read_property
= http_requestdatashare_object_read_prop
;
90 http_requestdatashare_object_handlers
.write_property
= http_requestdatashare_object_write_prop
;
92 #if defined(HAVE_SPL) && !defined(WONKY)
93 zend_class_implements(http_requestdatashare_object_ce TSRMLS_CC
, 1, spl_ce_Countable
);
96 zend_declare_property_null(THIS_CE
, ZEND_STRS("instance")-1, (ZEND_ACC_STATIC
|ZEND_ACC_PRIVATE
) TSRMLS_CC
);
97 zend_declare_property_bool(THIS_CE
, ZEND_STRS("cookie")-1, 0, ZEND_ACC_PUBLIC TSRMLS_CC
);
98 zend_declare_property_bool(THIS_CE
, ZEND_STRS("dns")-1, 0, ZEND_ACC_PUBLIC TSRMLS_CC
);
99 zend_declare_property_bool(THIS_CE
, ZEND_STRS("ssl")-1, 0, ZEND_ACC_PUBLIC TSRMLS_CC
);
100 zend_declare_property_bool(THIS_CE
, ZEND_STRS("connect")-1, 0, ZEND_ACC_PUBLIC TSRMLS_CC
);
105 zend_object_value
_http_requestdatashare_object_new(zend_class_entry
*ce TSRMLS_DC
)
107 return http_requestdatashare_object_new_ex(ce
, NULL
, NULL
);
110 zend_object_value
_http_requestdatashare_object_new_ex(zend_class_entry
*ce
, http_request_datashare
*share
, http_requestdatashare_object
**ptr TSRMLS_DC
)
112 zend_object_value ov
;
113 http_requestdatashare_object
*o
;
115 o
= ecalloc(1, sizeof(http_requestdatashare_object
));
121 o
->share
= http_request_datashare_new();
128 #ifdef ZEND_ENGINE_2_4
129 zend_object_std_init(o
, ce TSRMLS_CC
);
130 object_properties_init(o
, ce
);
132 ALLOC_HASHTABLE(OBJ_PROP(o
));
133 zend_hash_init(OBJ_PROP(o
), zend_hash_num_elements(&ce
->default_properties
), NULL
, ZVAL_PTR_DTOR
, 0);
134 zend_hash_copy(OBJ_PROP(o
), &ce
->default_properties
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
137 ov
.handle
= putObject(http_requestdatashare_object
, o
);
138 ov
.handlers
= &http_requestdatashare_object_handlers
;
143 void _http_requestdatashare_object_free(zend_object
*object TSRMLS_DC
)
145 http_requestdatashare_object
*o
= (http_requestdatashare_object
*) object
;
147 if (!o
->share
->persistent
) {
148 http_request_datashare_free(&o
->share
);
153 static zval
*_http_requestdatashare_object_read_prop(zval
*object
, zval
*member
, int type ZEND_LITERAL_KEY_DC TSRMLS_DC
)
155 if (type
== BP_VAR_W
&&
156 #ifdef ZEND_ENGINE_2_4
157 zend_hash_exists(&THIS_CE
->properties_info
, Z_STRVAL_P(member
), Z_STRLEN_P(member
)+1)
159 zend_hash_exists(&THIS_CE
->default_properties
, Z_STRVAL_P(member
), Z_STRLEN_P(member
)+1)
162 zend_error(E_ERROR
, "Cannot access HttpRequestDataShare default properties by reference or array key/index");
166 return zend_get_std_object_handlers()->read_property(object
, member
, type ZEND_LITERAL_KEY_CC TSRMLS_CC
);
169 static void _http_requestdatashare_object_write_prop(zval
*object
, zval
*member
, zval
*value ZEND_LITERAL_KEY_DC TSRMLS_DC
)
172 #ifdef ZEND_ENGINE_2_4
173 zend_hash_exists(&THIS_CE
->properties_info
, Z_STRVAL_P(member
), Z_STRLEN_P(member
)+1)
175 zend_hash_exists(&THIS_CE
->default_properties
, Z_STRVAL_P(member
), Z_STRLEN_P(member
)+1)
179 getObjectEx(http_requestdatashare_object
, obj
, object
);
181 status
= http_request_datashare_set(obj
->share
, Z_STRVAL_P(member
), Z_STRLEN_P(member
), (zend_bool
) i_zend_is_true(value
));
182 if (SUCCESS
!= status
) {
187 zend_get_std_object_handlers()->write_property(object
, member
, value ZEND_LITERAL_KEY_CC TSRMLS_CC
);
190 /* {{{ proto void HttpRequestDataShare::__destruct()
191 Clean up HttpRequestDataShare object. */
192 PHP_METHOD(HttpRequestDataShare
, __destruct
)
195 getObject(http_requestdatashare_object
, obj
);
196 http_request_datashare_detach_all(obj
->share
);
201 /* {{{ proto int HttpRequestDataShare::count()
202 Implements Countable::count(). */
203 PHP_METHOD(HttpRequestDataShare
, count
)
205 getObject(http_requestdatashare_object
, obj
);
209 RETURN_LONG(zend_llist_count(HTTP_RSHARE_HANDLES(obj
->share
)));
213 PHP_METHOD(HttpRequestDataShare
, attach
)
216 getObject(http_requestdatashare_object
, obj
);
218 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &request
, http_request_object_ce
)) {
222 RETURN_SUCCESS(http_request_datashare_attach(obj
->share
, request
));
225 PHP_METHOD(HttpRequestDataShare
, detach
)
228 getObject(http_requestdatashare_object
, obj
);
230 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &request
, http_request_object_ce
)) {
234 RETURN_SUCCESS(http_request_datashare_detach(obj
->share
, request
));
237 PHP_METHOD(HttpRequestDataShare
, reset
)
240 getObject(http_requestdatashare_object
, obj
);
241 http_request_datashare_detach_all(obj
->share
);
245 PHP_METHOD(HttpRequestDataShare
, factory
)
247 zend_bool global
= 0;
250 zend_object_value ov
;
253 if ( SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|bs", &global
, &cn
, &cl
) &&
254 SUCCESS
== http_object_new(&ov
, cn
, cl
, _http_requestdatashare_object_new_ex
, http_requestdatashare_object_ce
, NULL
, NULL
)) {
255 RETVAL_OBJVAL(ov
, 0);
256 http_requestdatashare_instantiate(return_value
, global
);
262 /* {{{ proto static HttpRequestDataShare HttpRequestDataShare::singleton([bool global = false])
263 Get a single instance (differentiates between the global setting). */
264 PHP_METHOD(HttpRequestDataShare
, singleton
)
266 zend_bool global
= 0;
267 zval
*instance
= *zend_std_get_static_property(THIS_CE
, ZEND_STRS("instance")-1, 0 ZEND_LITERAL_NIL_CC TSRMLS_CC
);
270 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &global
)) {
271 zval
**zobj_ptr
= NULL
, *zobj
= NULL
;
273 if (Z_TYPE_P(instance
) == IS_ARRAY
) {
274 if (SUCCESS
== zend_hash_index_find(Z_ARRVAL_P(instance
), global
, (void *) &zobj_ptr
)) {
275 RETVAL_ZVAL(*zobj_ptr
, 1, 0);
277 zobj
= http_requestdatashare_instantiate(NULL
, global
);
278 add_index_zval(instance
, global
, zobj
);
279 RETVAL_OBJECT(zobj
, 1);
282 MAKE_STD_ZVAL(instance
);
283 array_init(instance
);
285 zobj
= http_requestdatashare_instantiate(NULL
, global
);
286 add_index_zval(instance
, global
, zobj
);
287 RETVAL_OBJECT(zobj
, 1);
289 zend_update_static_property(THIS_CE
, ZEND_STRS("instance")-1, instance TSRMLS_CC
);
290 zval_ptr_dtor(&instance
);
298 static inline zval
*_http_requestdatashare_instantiate(zval
*this_ptr
, zend_bool global TSRMLS_DC
)
301 MAKE_STD_ZVAL(this_ptr
);
302 Z_TYPE_P(this_ptr
) = IS_OBJECT
;
303 this_ptr
->value
.obj
= http_requestdatashare_object_new_ex(http_requestdatashare_object_ce
, global
? http_request_datashare_global_get() : NULL
, NULL
);
306 if (HTTP_G
->request
.datashare
.cookie
) {
307 zend_update_property_bool(THIS_CE
, getThis(), ZEND_STRS("cookie")-1, HTTP_G
->request
.datashare
.cookie TSRMLS_CC
);
309 if (HTTP_G
->request
.datashare
.dns
) {
310 zend_update_property_bool(THIS_CE
, getThis(), ZEND_STRS("dns")-1, HTTP_G
->request
.datashare
.dns TSRMLS_CC
);
312 if (HTTP_G
->request
.datashare
.ssl
) {
313 zend_update_property_bool(THIS_CE
, getThis(), ZEND_STRS("ssl")-1, HTTP_G
->request
.datashare
.ssl TSRMLS_CC
);
315 if (HTTP_G
->request
.datashare
.connect
) {
316 zend_update_property_bool(THIS_CE
, getThis(), ZEND_STRS("connect")-1, HTTP_G
->request
.datashare
.connect TSRMLS_CC
);
322 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
329 * vim600: noet sw=4 ts=4 fdm=marker
330 * vim<600: noet sw=4 ts=4