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-2007, 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 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 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 ALLOC_HASHTABLE(OBJ_PROP(o
));
129 zend_hash_init(OBJ_PROP(o
), zend_hash_num_elements(&ce
->default_properties
), NULL
, ZVAL_PTR_DTOR
, 0);
130 zend_hash_copy(OBJ_PROP(o
), &ce
->default_properties
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
132 ov
.handle
= putObject(http_requestdatashare_object
, o
);
133 ov
.handlers
= &http_requestdatashare_object_handlers
;
138 void _http_requestdatashare_object_free(zend_object
*object TSRMLS_DC
)
140 http_requestdatashare_object
*o
= (http_requestdatashare_object
*) object
;
142 if (!o
->share
->persistent
) {
143 http_request_datashare_free(&o
->share
);
148 static zval
*_http_requestdatashare_object_read_prop(zval
*object
, zval
*member
, int type TSRMLS_DC
)
150 if (type
== BP_VAR_W
&& zend_hash_exists(&THIS_CE
->default_properties
, Z_STRVAL_P(member
), Z_STRLEN_P(member
)+1)) {
151 zend_error(E_ERROR
, "Cannot access HttpRequestDataShare default properties by reference or array key/index");
155 return zend_get_std_object_handlers()->read_property(object
, member
, type TSRMLS_CC
);
158 static void _http_requestdatashare_object_write_prop(zval
*object
, zval
*member
, zval
*value TSRMLS_DC
)
160 if (zend_hash_exists(&THIS_CE
->default_properties
, Z_STRVAL_P(member
), Z_STRLEN_P(member
)+1)) {
163 getObjectEx(http_requestdatashare_object
, obj
, object
);
165 SEPARATE_ZVAL_IF_NOT_REF(&value
);
166 status
= http_request_datashare_set(obj
->share
, Z_STRVAL_P(member
), Z_STRLEN_P(member
), (zend_bool
) zval_is_true(value
));
168 zval_ptr_dtor(&value
);
171 if (SUCCESS
!= status
) {
176 zend_get_std_object_handlers()->write_property(object
, member
, value TSRMLS_CC
);
179 /* {{{ proto void HttpRequestDataShare::__destruct()
180 Clean up HttpRequestDataShare object. */
181 PHP_METHOD(HttpRequestDataShare
, __destruct
)
184 getObject(http_requestdatashare_object
, obj
);
185 http_request_datashare_detach_all(obj
->share
);
190 /* {{{ proto int HttpRequestDataShare::count()
191 Implements Countable::count(). */
192 PHP_METHOD(HttpRequestDataShare
, count
)
194 getObject(http_requestdatashare_object
, obj
);
198 RETURN_LONG(zend_llist_count(HTTP_RSHARE_HANDLES(obj
->share
)));
202 PHP_METHOD(HttpRequestDataShare
, attach
)
205 getObject(http_requestdatashare_object
, obj
);
207 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &request
, http_request_object_ce
)) {
211 RETURN_SUCCESS(http_request_datashare_attach(obj
->share
, request
));
214 PHP_METHOD(HttpRequestDataShare
, detach
)
217 getObject(http_requestdatashare_object
, obj
);
219 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &request
, http_request_object_ce
)) {
223 RETURN_SUCCESS(http_request_datashare_detach(obj
->share
, request
));
226 PHP_METHOD(HttpRequestDataShare
, reset
)
229 getObject(http_requestdatashare_object
, obj
);
230 http_request_datashare_detach_all(obj
->share
);
234 PHP_METHOD(HttpRequestDataShare
, factory
)
236 zend_bool global
= 0;
239 zend_object_value ov
;
242 if ( SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|bs", &global
, &cn
, &cl
) &&
243 SUCCESS
== http_object_new(&ov
, cn
, cl
, _http_requestdatashare_object_new_ex
, http_requestdatashare_object_ce
, NULL
, NULL
)) {
244 RETVAL_OBJVAL(ov
, 0);
245 http_requestdatashare_instantiate(return_value
, global
);
251 /* {{{ proto static HttpRequestDataShare HttpRequestDataShare::singleton([bool global = false])
252 Get a single instance (differentiates between the global setting). */
253 PHP_METHOD(HttpRequestDataShare
, singleton
)
255 zend_bool global
= 0;
256 zval
*instance
= *zend_std_get_static_property(THIS_CE
, ZEND_STRS("instance")-1, 0 TSRMLS_CC
);
259 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &global
)) {
260 zval
**zobj_ptr
= NULL
, *zobj
= NULL
;
262 if (Z_TYPE_P(instance
) == IS_ARRAY
) {
263 if (SUCCESS
== zend_hash_index_find(Z_ARRVAL_P(instance
), global
, (void *) &zobj_ptr
)) {
264 RETVAL_ZVAL(*zobj_ptr
, 1, 0);
266 zobj
= http_requestdatashare_instantiate(NULL
, global
);
267 add_index_zval(instance
, global
, zobj
);
268 RETVAL_OBJECT(zobj
, 1);
271 MAKE_STD_ZVAL(instance
);
272 array_init(instance
);
274 zobj
= http_requestdatashare_instantiate(NULL
, global
);
275 add_index_zval(instance
, global
, zobj
);
276 RETVAL_OBJECT(zobj
, 1);
278 zend_update_static_property(THIS_CE
, ZEND_STRS("instance")-1, instance TSRMLS_CC
);
279 zval_ptr_dtor(&instance
);
287 static inline zval
*_http_requestdatashare_instantiate(zval
*this_ptr
, zend_bool global TSRMLS_DC
)
290 MAKE_STD_ZVAL(this_ptr
);
291 Z_TYPE_P(this_ptr
) = IS_OBJECT
;
292 this_ptr
->value
.obj
= http_requestdatashare_object_new_ex(http_requestdatashare_object_ce
, global
? http_request_datashare_global_get() : NULL
, NULL
);
295 if (HTTP_G
->request
.datashare
.cookie
) {
296 zend_update_property_bool(THIS_CE
, getThis(), ZEND_STRS("cookie")-1, HTTP_G
->request
.datashare
.cookie TSRMLS_CC
);
298 if (HTTP_G
->request
.datashare
.dns
) {
299 zend_update_property_bool(THIS_CE
, getThis(), ZEND_STRS("dns")-1, HTTP_G
->request
.datashare
.dns TSRMLS_CC
);
301 if (HTTP_G
->request
.datashare
.ssl
) {
302 zend_update_property_bool(THIS_CE
, getThis(), ZEND_STRS("ssl")-1, HTTP_G
->request
.datashare
.ssl TSRMLS_CC
);
304 if (HTTP_G
->request
.datashare
.connect
) {
305 zend_update_property_bool(THIS_CE
, getThis(), ZEND_STRS("connect")-1, HTTP_G
->request
.datashare
.connect TSRMLS_CC
);
311 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
318 * vim600: noet sw=4 ts=4 fdm=marker
319 * vim<600: noet sw=4 ts=4