Line data Source code
1 : /*
2 : +--------------------------------------------------------------------+
3 : | PECL :: http |
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-2014, Michael Wallner <mike@php.net> |
10 : +--------------------------------------------------------------------+
11 : */
12 :
13 : #include "php_http_api.h"
14 :
15 3 : static int grab_file(void *zpp TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
16 : {
17 3 : zval *zfiles, **name, **zname, **error, **zerror, **type, **ztype, **size, **zsize, **tmp_name = zpp;
18 : zend_hash_key *file_key;
19 :
20 3 : zfiles = (zval *) va_arg(argv, zval *);
21 3 : file_key = (zend_hash_key *) va_arg(argv, zend_hash_key *);
22 3 : name = (zval **) va_arg(argv, zval **);
23 3 : size = (zval **) va_arg(argv, zval **);
24 3 : type = (zval **) va_arg(argv, zval **);
25 3 : error = (zval **) va_arg(argv, zval **);
26 :
27 3 : if (SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(name), key->h, (void *) &zname)
28 3 : && SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(size), key->h, (void *) &zsize)
29 3 : && SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(type), key->h, (void *) &ztype)
30 3 : && SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(error), key->h, (void *) &zerror)
31 : ) {
32 : zval *entry, **array;
33 :
34 3 : MAKE_STD_ZVAL(entry);
35 3 : array_init(entry);
36 :
37 3 : Z_ADDREF_PP(tmp_name);
38 3 : add_assoc_zval_ex(entry, ZEND_STRS("file"), *tmp_name);
39 3 : Z_ADDREF_PP(zname);
40 3 : add_assoc_zval_ex(entry, ZEND_STRS("name"), *zname);
41 3 : Z_ADDREF_PP(zsize);
42 3 : add_assoc_zval_ex(entry, ZEND_STRS("size"), *zsize);
43 3 : Z_ADDREF_PP(ztype);
44 3 : add_assoc_zval_ex(entry, ZEND_STRS("type"), *ztype);
45 3 : Z_ADDREF_PP(zerror);
46 3 : add_assoc_zval_ex(entry, ZEND_STRS("error"), *zerror);
47 :
48 3 : if (SUCCESS == zend_hash_quick_find(Z_ARRVAL_P(zfiles), file_key->arKey, file_key->nKeyLength, file_key->h, (void *) &array)) {
49 2 : add_next_index_zval(*array, entry);
50 : } else {
51 : zval *tmp;
52 :
53 1 : MAKE_STD_ZVAL(tmp);
54 1 : array_init(tmp);
55 1 : add_next_index_zval(tmp, entry);
56 1 : zend_hash_quick_update(Z_ARRVAL_P(zfiles), file_key->arKey, file_key->nKeyLength, file_key->h, (void *) &tmp, sizeof(zval *), NULL);
57 : }
58 : }
59 :
60 3 : return ZEND_HASH_APPLY_KEEP;
61 : }
62 :
63 3 : static int grab_files(void *zpp TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
64 : {
65 3 : zval *zfiles, **name, **tmp_name, **error, **type, **size, **val = zpp;
66 :
67 3 : zfiles = (zval *) va_arg(argv, zval *);
68 :
69 3 : if (Z_TYPE_PP(val) == IS_ARRAY
70 3 : && SUCCESS == zend_hash_find(Z_ARRVAL_PP(val), ZEND_STRS("tmp_name"), (void *) &tmp_name)
71 3 : && SUCCESS == zend_hash_find(Z_ARRVAL_PP(val), ZEND_STRS("name"), (void *) &name)
72 3 : && SUCCESS == zend_hash_find(Z_ARRVAL_PP(val), ZEND_STRS("size"), (void *) &size)
73 3 : && SUCCESS == zend_hash_find(Z_ARRVAL_PP(val), ZEND_STRS("type"), (void *) &type)
74 3 : && SUCCESS == zend_hash_find(Z_ARRVAL_PP(val), ZEND_STRS("error"), (void *) &error)
75 : ) {
76 : int count;
77 :
78 3 : if (Z_TYPE_PP(tmp_name) == IS_ARRAY && (count = zend_hash_num_elements(Z_ARRVAL_PP(tmp_name))) > 1) {
79 2 : if (count == zend_hash_num_elements(Z_ARRVAL_PP(name))
80 1 : && count == zend_hash_num_elements(Z_ARRVAL_PP(size))
81 1 : && count == zend_hash_num_elements(Z_ARRVAL_PP(type))
82 1 : && count == zend_hash_num_elements(Z_ARRVAL_PP(error))
83 : ) {
84 1 : zend_hash_apply_with_arguments(Z_ARRVAL_PP(tmp_name) TSRMLS_CC, grab_file, 6, zfiles, key, name, size, type, error);
85 : } else {
86 : /* wat?! */
87 0 : return ZEND_HASH_APPLY_STOP;
88 : }
89 : } else {
90 : zval *cpy, **tmp;
91 :
92 2 : MAKE_STD_ZVAL(cpy);
93 2 : MAKE_COPY_ZVAL(val, cpy);
94 2 : if (SUCCESS == zend_hash_find(Z_ARRVAL_P(cpy), ZEND_STRS("tmp_name"), (void *) &tmp)) {
95 2 : Z_ADDREF_PP(tmp);
96 2 : add_assoc_zval_ex(cpy, ZEND_STRS("file"), *tmp);
97 2 : zend_hash_del_key_or_index(Z_ARRVAL_P(cpy), ZEND_STRS("tmp_name"), 0, HASH_DEL_KEY);
98 : }
99 2 : if (key->nKeyLength > 0) {
100 0 : zend_hash_quick_update(Z_ARRVAL_P(zfiles), key->arKey, key->nKeyLength, key->h, (void *) &cpy, sizeof(zval *), NULL);
101 : } else {
102 2 : zend_hash_index_update(Z_ARRVAL_P(zfiles), key->h, (void *) &cpy, sizeof(zval *), NULL);
103 : }
104 : }
105 : }
106 :
107 3 : return ZEND_HASH_APPLY_KEEP;
108 : }
109 :
110 : #define PHP_HTTP_ENV_REQUEST_OBJECT_INIT(obj) \
111 : do { \
112 : if (!obj->message) { \
113 : obj->message = php_http_message_init_env(NULL, PHP_HTTP_REQUEST TSRMLS_CC); \
114 : } \
115 : } while(0)
116 :
117 :
118 : ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvRequest___construct, 0, 0, 0)
119 : ZEND_END_ARG_INFO();
120 12 : static PHP_METHOD(HttpEnvRequest, __construct)
121 : {
122 : php_http_message_object_t *obj;
123 : zval *zsg, *zqs;
124 :
125 12 : php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
126 :
127 12 : obj = zend_object_store_get_object(getThis() TSRMLS_CC);
128 12 : obj->body = NULL;
129 :
130 12 : php_http_expect(obj->message = php_http_message_init_env(obj->message, PHP_HTTP_REQUEST TSRMLS_CC), unexpected_val, return);
131 :
132 12 : zsg = php_http_env_get_superglobal(ZEND_STRL("_GET") TSRMLS_CC);
133 12 : MAKE_STD_ZVAL(zqs);
134 12 : object_init_ex(zqs, php_http_querystring_class_entry);
135 12 : php_http_expect(SUCCESS == php_http_querystring_ctor(zqs, zsg TSRMLS_CC), unexpected_val,
136 : zval_ptr_dtor(&zqs);
137 : return;
138 : );
139 12 : zend_update_property(php_http_env_request_class_entry, getThis(), ZEND_STRL("query"), zqs TSRMLS_CC);
140 12 : zval_ptr_dtor(&zqs);
141 :
142 12 : zsg = php_http_env_get_superglobal(ZEND_STRL("_POST") TSRMLS_CC);
143 12 : MAKE_STD_ZVAL(zqs);
144 12 : object_init_ex(zqs, php_http_querystring_class_entry);
145 12 : php_http_expect(SUCCESS == php_http_querystring_ctor(zqs, zsg TSRMLS_CC), unexpected_val,
146 : zval_ptr_dtor(&zqs);
147 : return;
148 : );
149 12 : zend_update_property(php_http_env_request_class_entry, getThis(), ZEND_STRL("form"), zqs TSRMLS_CC);
150 12 : zval_ptr_dtor(&zqs);
151 :
152 12 : zsg = php_http_env_get_superglobal(ZEND_STRL("_COOKIE") TSRMLS_CC);
153 12 : MAKE_STD_ZVAL(zqs);
154 12 : object_init_ex(zqs, php_http_querystring_class_entry);
155 12 : php_http_expect(SUCCESS == php_http_querystring_ctor(zqs, zsg TSRMLS_CC), unexpected_val,
156 : zval_ptr_dtor(&zqs);
157 : return;
158 : );
159 12 : zend_update_property(php_http_env_request_class_entry, getThis(), ZEND_STRL("cookie"), zqs TSRMLS_CC);
160 12 : zval_ptr_dtor(&zqs);
161 :
162 12 : MAKE_STD_ZVAL(zqs);
163 12 : array_init(zqs);
164 12 : if ((zsg = php_http_env_get_superglobal(ZEND_STRL("_FILES") TSRMLS_CC))) {
165 12 : zend_hash_apply_with_arguments(Z_ARRVAL_P(zsg) TSRMLS_CC, grab_files, 1, zqs);
166 : }
167 12 : zend_update_property(php_http_env_request_class_entry, getThis(), ZEND_STRL("files"), zqs TSRMLS_CC);
168 12 : zval_ptr_dtor(&zqs);
169 : }
170 :
171 : #define call_querystring_get(prop) \
172 : do {\
173 : zend_fcall_info fci; \
174 : zend_fcall_info_cache fcc; \
175 : zval *rv, mn, ***args = ecalloc(sizeof(zval **), ZEND_NUM_ARGS()); \
176 : zval *qs = zend_read_property(Z_OBJCE_P(getThis()), getThis(), ZEND_STRL(prop), 0 TSRMLS_CC); \
177 : \
178 : INIT_PZVAL(&mn); \
179 : array_init(&mn); \
180 : Z_ADDREF_P(qs); \
181 : add_next_index_zval(&mn, qs); \
182 : add_next_index_stringl(&mn, ZEND_STRL("get"), 1); \
183 : zend_fcall_info_init(&mn, 0, &fci, &fcc, NULL, NULL TSRMLS_CC); \
184 : zend_get_parameters_array_ex(ZEND_NUM_ARGS(), args); \
185 : zend_fcall_info_argp(&fci TSRMLS_CC, ZEND_NUM_ARGS(), args); \
186 : zend_fcall_info_call(&fci, &fcc, &rv, NULL TSRMLS_CC); \
187 : zend_fcall_info_args_clear(&fci, 1); \
188 : efree(args); \
189 : zval_dtor(&mn); \
190 : RETVAL_ZVAL(rv, 0, 1); \
191 : } while(0);
192 :
193 : ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvRequest_getForm, 0, 0, 0)
194 : ZEND_ARG_INFO(0, name)
195 : ZEND_ARG_INFO(0, type)
196 : ZEND_ARG_INFO(0, defval)
197 : ZEND_ARG_INFO(0, delete)
198 : ZEND_END_ARG_INFO();
199 4 : static PHP_METHOD(HttpEnvRequest, getForm)
200 : {
201 4 : if (ZEND_NUM_ARGS()) {
202 2 : call_querystring_get("form");
203 : } else {
204 2 : zval *zform = zend_read_property(php_http_env_request_class_entry, getThis(), ZEND_STRL("form"), 0 TSRMLS_CC);
205 2 : RETURN_ZVAL(zform, 1, 0);
206 : }
207 : }
208 :
209 : ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvRequest_getQuery, 0, 0, 0)
210 : ZEND_ARG_INFO(0, name)
211 : ZEND_ARG_INFO(0, type)
212 : ZEND_ARG_INFO(0, defval)
213 : ZEND_ARG_INFO(0, delete)
214 : ZEND_END_ARG_INFO();
215 4 : static PHP_METHOD(HttpEnvRequest, getQuery)
216 : {
217 4 : if (ZEND_NUM_ARGS()) {
218 2 : call_querystring_get("query");
219 : } else {
220 2 : zval *zquery = zend_read_property(php_http_env_request_class_entry, getThis(), ZEND_STRL("query"), 0 TSRMLS_CC);
221 2 : RETURN_ZVAL(zquery, 1, 0);
222 : }
223 : }
224 :
225 : ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvRequest_getCookie, 0, 0, 0)
226 : ZEND_ARG_INFO(0, name)
227 : ZEND_ARG_INFO(0, type)
228 : ZEND_ARG_INFO(0, defval)
229 : ZEND_ARG_INFO(0, delete)
230 : ZEND_END_ARG_INFO();
231 4 : static PHP_METHOD(HttpEnvRequest, getCookie)
232 : {
233 4 : if (ZEND_NUM_ARGS()) {
234 3 : call_querystring_get("cookie");
235 : } else {
236 1 : zval *zcookie = zend_read_property(php_http_env_request_class_entry, getThis(), ZEND_STRL("cookie"), 0 TSRMLS_CC);
237 1 : RETURN_ZVAL(zcookie, 1, 0);
238 : }
239 : }
240 :
241 : ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvRequest_getFiles, 0, 0, 0)
242 : ZEND_END_ARG_INFO();
243 2 : static PHP_METHOD(HttpEnvRequest, getFiles)
244 : {
245 2 : if (SUCCESS == zend_parse_parameters_none()) {
246 2 : zval *zfiles = zend_read_property(php_http_env_request_class_entry, getThis(), ZEND_STRL("files"), 0 TSRMLS_CC);
247 2 : RETURN_ZVAL(zfiles, 1, 0);
248 : }
249 : }
250 :
251 : static zend_function_entry php_http_env_request_methods[] = {
252 : PHP_ME(HttpEnvRequest, __construct, ai_HttpEnvRequest___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
253 : PHP_ME(HttpEnvRequest, getForm, ai_HttpEnvRequest_getForm, ZEND_ACC_PUBLIC)
254 : PHP_ME(HttpEnvRequest, getQuery, ai_HttpEnvRequest_getQuery, ZEND_ACC_PUBLIC)
255 : PHP_ME(HttpEnvRequest, getCookie, ai_HttpEnvRequest_getCookie, ZEND_ACC_PUBLIC)
256 : PHP_ME(HttpEnvRequest, getFiles, ai_HttpEnvRequest_getFiles, ZEND_ACC_PUBLIC)
257 : EMPTY_FUNCTION_ENTRY
258 : };
259 :
260 : zend_class_entry *php_http_env_request_class_entry;
261 :
262 374 : PHP_MINIT_FUNCTION(http_env_request)
263 : {
264 374 : zend_class_entry ce = {0};
265 :
266 374 : INIT_NS_CLASS_ENTRY(ce, "http\\Env", "Request", php_http_env_request_methods);
267 374 : php_http_env_request_class_entry = zend_register_internal_class_ex(&ce, php_http_message_class_entry, NULL TSRMLS_CC);
268 :
269 374 : zend_declare_property_null(php_http_env_request_class_entry, ZEND_STRL("query"), ZEND_ACC_PROTECTED TSRMLS_CC);
270 374 : zend_declare_property_null(php_http_env_request_class_entry, ZEND_STRL("form"), ZEND_ACC_PROTECTED TSRMLS_CC);
271 374 : zend_declare_property_null(php_http_env_request_class_entry, ZEND_STRL("cookie"), ZEND_ACC_PROTECTED TSRMLS_CC);
272 374 : zend_declare_property_null(php_http_env_request_class_entry, ZEND_STRL("files"), ZEND_ACC_PROTECTED TSRMLS_CC);
273 :
274 374 : return SUCCESS;
275 : }
276 :
277 : /*
278 : * Local variables:
279 : * tab-width: 4
280 : * c-basic-offset: 4
281 : * End:
282 : * vim600: noet sw=4 ts=4 fdm=marker
283 : * vim<600: noet sw=4 ts=4
284 : */
|