* hoppala
[m6w6/ext-http] / http.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18 #define _WINSOCKAPI_
19 #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include "php.h"
26 #include "php_ini.h"
27 #include "snprintf.h"
28 #include "ext/standard/info.h"
29 #include "ext/session/php_session.h"
30 #include "ext/standard/php_string.h"
31 #include "ext/standard/php_smart_str.h"
32
33 #include "SAPI.h"
34
35 #include "php_http.h"
36 #include "php_http_api.h"
37
38 #ifdef ZEND_ENGINE_2
39 # include "ext/standard/php_http.h"
40 #endif
41
42 #ifdef HTTP_HAVE_CURL
43
44 # ifdef PHP_WIN32
45 # include <winsock2.h>
46 # include <sys/types.h>
47 # endif
48
49 # include <curl/curl.h>
50
51 /* {{{ ARG_INFO */
52 # ifdef ZEND_BEGIN_ARG_INFO
53 ZEND_BEGIN_ARG_INFO(http_request_info_ref_3, 0)
54 ZEND_ARG_PASS_INFO(0)
55 ZEND_ARG_PASS_INFO(0)
56 ZEND_ARG_PASS_INFO(1)
57 ZEND_END_ARG_INFO();
58
59 ZEND_BEGIN_ARG_INFO(http_request_info_ref_4, 0)
60 ZEND_ARG_PASS_INFO(0)
61 ZEND_ARG_PASS_INFO(0)
62 ZEND_ARG_PASS_INFO(0)
63 ZEND_ARG_PASS_INFO(1)
64 ZEND_END_ARG_INFO();
65 # else
66 static unsigned char http_request_info_ref_3[] = {3, BYREF_NONE, BYREF_NONE, BYREF_FORCE};
67 static unsigned char http_request_info_ref_4[] = {4, BYREF_NONE, BYREF_NONE, BYREF_NONE, BYREF_FORCE};
68 # endif
69 /* }}} ARG_INFO */
70
71 #endif /* HTTP_HAVE_CURL */
72
73 ZEND_DECLARE_MODULE_GLOBALS(http)
74
75 #ifdef COMPILE_DL_HTTP
76 ZEND_GET_MODULE(http)
77 #endif
78
79 /* {{{ http_functions[] */
80 function_entry http_functions[] = {
81 PHP_FE(http_date, NULL)
82 PHP_FE(http_absolute_uri, NULL)
83 PHP_FE(http_negotiate_language, NULL)
84 PHP_FE(http_negotiate_charset, NULL)
85 PHP_FE(http_redirect, NULL)
86 PHP_FE(http_send_status, NULL)
87 PHP_FE(http_send_last_modified, NULL)
88 PHP_FE(http_match_modified, NULL)
89 PHP_FE(http_match_etag, NULL)
90 PHP_FE(http_cache_last_modified, NULL)
91 PHP_FE(http_cache_etag, NULL)
92 PHP_FE(http_content_type, NULL)
93 PHP_FE(http_content_disposition, NULL)
94 PHP_FE(http_send_data, NULL)
95 PHP_FE(http_send_file, NULL)
96 PHP_FE(http_send_stream, NULL)
97 PHP_FE(http_chunked_decode, NULL)
98 PHP_FE(http_split_response, NULL)
99 PHP_FE(http_parse_headers, NULL)
100 PHP_FE(http_get_request_headers, NULL)
101 #ifdef HTTP_HAVE_CURL
102 PHP_FE(http_get, http_request_info_ref_3)
103 PHP_FE(http_head, http_request_info_ref_3)
104 PHP_FE(http_post_data, http_request_info_ref_4)
105 PHP_FE(http_post_array, http_request_info_ref_4)
106 #endif
107 PHP_FE(http_auth_basic, NULL)
108 PHP_FE(http_auth_basic_cb, NULL)
109 #ifndef ZEND_ENGINE_2
110 PHP_FE(http_build_query, NULL)
111 #endif
112 PHP_FE(ob_httpetaghandler, NULL)
113 {NULL, NULL, NULL}
114 };
115 /* }}} */
116
117 #define RETURN_SUCCESS(v) RETURN_BOOL(SUCCESS == (v))
118 #define HASH_ORNULL(z) ((z) ? Z_ARRVAL_P(z) : NULL)
119
120 #ifdef ZEND_ENGINE_2
121
122 # define HTTP_REGISTER_CLASS_EX(classname, name, parent, flags) \
123 { \
124 zend_class_entry ce; \
125 INIT_CLASS_ENTRY(ce, #classname, name## _class_methods); \
126 ce.create_object = name## _new_object; \
127 name## _ce = zend_register_internal_class_ex(&ce, parent, NULL TSRMLS_CC); \
128 name## _ce->ce_flags |= flags; \
129 memcpy(& name## _object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \
130 name## _object_handlers.clone_obj = NULL; \
131 name## _declare_default_properties(name## _ce); \
132 }
133
134 # define HTTP_REGISTER_CLASS(classname, name, parent, flags) \
135 { \
136 zend_class_entry ce; \
137 INIT_CLASS_ENTRY(ce, #classname, name## _class_methods); \
138 ce.create_object = NULL; \
139 name## _ce = zend_register_internal_class_ex(&ce, parent, NULL TSRMLS_CC); \
140 name## _ce->ce_flags |= flags; \
141 }
142
143 # define getObject(t, o) t * o = ((t *) zend_object_store_get_object(getThis() TSRMLS_CC))
144 # define OBJ_PROP(o) o->zo.properties
145 # define DCL_PROP(a, t, n, v) zend_declare_property_ ##t(ce, (#n), sizeof(#n), (v), (ZEND_ACC_ ##a) TSRMLS_CC)
146 # define UPD_PROP(o, t, n, v) zend_update_property_ ##t(o->zo.ce, getThis(), (#n), sizeof(#n), (v) TSRMLS_CC)
147 # define GET_PROP(o, n) zend_read_property(o->zo.ce, getThis(), (#n), sizeof(#n), 0 TSRMLS_CC)
148
149 /* {{{ HTTPi */
150
151 zend_class_entry *httpi_ce;
152
153 #define HTTPi_ME(me, al, ai) ZEND_FENTRY(me, ZEND_FN(al), ai, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
154
155 zend_function_entry httpi_class_methods[] = {
156 HTTPi_ME(date, http_date, NULL)
157 HTTPi_ME(absoluteURI, http_absolute_uri, NULL)
158 HTTPi_ME(negotiateLanguage, http_negotiate_language, NULL)
159 HTTPi_ME(negotiateCharset, http_negotiate_charset, NULL)
160 HTTPi_ME(redirect, http_redirect, NULL)
161 HTTPi_ME(sendStatus, http_send_status, NULL)
162 HTTPi_ME(sendLastModified, http_send_last_modified, NULL)
163 HTTPi_ME(matchModified, http_match_modified, NULL)
164 HTTPi_ME(matchEtag, http_match_etag, NULL)
165 HTTPi_ME(cacheLastModified, http_cache_last_modified, NULL)
166 HTTPi_ME(cacheEtag, http_cache_etag, NULL)
167 HTTPi_ME(chunkedDecode, http_chunked_decode, NULL)
168 HTTPi_ME(splitResponse, http_split_response, NULL)
169 HTTPi_ME(parseHeaders, http_parse_headers, NULL)
170 HTTPi_ME(getRequestHeaders, http_get_request_headers, NULL)
171 #ifdef HTTP_HAVE_CURL
172 HTTPi_ME(get, http_get, http_request_info_ref_3)
173 HTTPi_ME(head, http_head, http_request_info_ref_3)
174 HTTPi_ME(postData, http_post_data, http_request_info_ref_4)
175 HTTPi_ME(postArray, http_post_array, http_request_info_ref_4)
176 #endif
177 HTTPi_ME(authBasic, http_auth_basic, NULL)
178 HTTPi_ME(authBasicCallback, http_auth_basic_cb, NULL)
179 {NULL, NULL, NULL}
180 };
181 /* }}} HTTPi */
182
183 /* {{{ HTTPi_Response */
184
185 zend_class_entry *httpi_response_ce;
186 static zend_object_handlers httpi_response_object_handlers;
187
188 typedef struct {
189 zend_object zo;
190 } httpi_response_object;
191
192 #define httpi_response_declare_default_properties(ce) _httpi_response_declare_default_properties(ce TSRMLS_CC)
193 static inline void _httpi_response_declare_default_properties(zend_class_entry *ce TSRMLS_DC)
194 {
195 DCL_PROP(PROTECTED, string, contentType, "application/x-octetstream");
196 DCL_PROP(PROTECTED, string, eTag, "");
197 DCL_PROP(PROTECTED, string, dispoFile, "");
198 DCL_PROP(PROTECTED, string, cacheControl, "public");
199 DCL_PROP(PROTECTED, long, lastModified, 0);
200 DCL_PROP(PROTECTED, long, dispoInline, 0);
201 DCL_PROP(PROTECTED, long, cache, 0);
202 DCL_PROP(PROTECTED, long, gzip, 0);
203
204 DCL_PROP(PRIVATE, long, raw_cache_header, 0);
205 }
206
207 #define httpi_response_destroy_object _httpi_response_destroy_object
208 void _httpi_response_destroy_object(void *object, zend_object_handle handle TSRMLS_DC)
209 {
210 httpi_response_object *o = object;
211 if (OBJ_PROP(o)) {
212 zend_hash_destroy(OBJ_PROP(o));
213 FREE_HASHTABLE(OBJ_PROP(o));
214 }
215 }
216
217 #define httpi_response_new_object _httpi_response_new_object
218 zend_object_value _httpi_response_new_object(zend_class_entry *ce TSRMLS_DC)
219 {
220 zend_object_value ov;
221 httpi_response_object *o;
222
223 o = ecalloc(sizeof(httpi_response_object), 1);
224 o->zo.ce = ce;
225
226 ALLOC_HASHTABLE(o->zo.properties);
227 zend_hash_init(o->zo.properties, 0, NULL, ZVAL_PTR_DTOR, 0);
228
229 ov.handle = zend_objects_store_put(o, httpi_response_destroy_object, NULL, NULL TSRMLS_CC);
230 ov.handlers = &httpi_response_object_handlers;
231
232 return ov;
233 }
234
235 zend_function_entry httpi_response_class_methods[] = {
236 PHP_ME(HTTPi_Response, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
237 /* PHP_ME(HTTPi_Response, __destruct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
238 */
239 PHP_ME(HTTPi_Response, setETag, NULL, ZEND_ACC_PUBLIC)
240 PHP_ME(HTTPi_Response, getETag, NULL, ZEND_ACC_PUBLIC)
241
242 PHP_ME(HTTPi_Response, setContentDisposition, NULL, ZEND_ACC_PUBLIC)
243 PHP_ME(HTTPi_Response, getContentDisposition, NULL, ZEND_ACC_PUBLIC)
244
245 PHP_ME(HTTPi_Response, setContentType, NULL, ZEND_ACC_PUBLIC)
246 PHP_ME(HTTPi_Response, getContentType, NULL, ZEND_ACC_PUBLIC)
247
248 PHP_ME(HTTPi_Response, setCache, NULL, ZEND_ACC_PUBLIC)
249 PHP_ME(HTTPi_Response, getCache, NULL, ZEND_ACC_PUBLIC)
250
251 PHP_ME(HTTPi_Response, setCacheControl, NULL, ZEND_ACC_PUBLIC)
252 PHP_ME(HTTPi_Response, getCacheControl, NULL, ZEND_ACC_PUBLIC)
253
254 PHP_ME(HTTPi_Response, setGzip, NULL, ZEND_ACC_PUBLIC)
255 PHP_ME(HTTPi_Response, getGzip, NULL, ZEND_ACC_PUBLIC)
256 /*
257 PHP_ME(HTTPi_Response, setData, NULL, ZEND_ACC_PUBLIC)
258 PHP_ME(HTTPi_Response, getData, NULL, ZEND_ACC_PUBLIC)
259
260 PHP_ME(HTTPi_Response, setFile, NULL, ZEND_ACC_PUBLIC)
261 PHP_ME(HTTPi_Response, getFile, NULL, ZEND_ACC_PUBLIC)
262
263 PHP_ME(HTTPi_Response, setStream, NULL, ZEND_ACC_PUBLIC)
264 PHP_ME(HTTPi_Response, getStream, NULL, ZEND_ACC_PUBLIC)
265
266 PHP_ME(HTTPi_Response, send, NULL, ZEND_ACC_PUBLIC)*/
267
268 {NULL, NULL, NULL}
269 };
270
271 /* {{{ proto void HTTPi_Response::__construct(bool cache, bool gzip)
272 *
273 */
274 PHP_METHOD(HTTPi_Response, __construct)
275 {
276 zend_bool do_cache = 0, do_gzip = 0;
277 getObject(httpi_response_object, obj);
278
279 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|bb", &do_cache, &do_gzip)) {
280 // throw exception
281 return;
282 }
283
284 UPD_PROP(obj, long, cache, do_cache);
285 UPD_PROP(obj, long, gzip, do_gzip);
286 }
287 /* }}} */
288
289 /* {{{ proto bool HTTPi_Response::setCache(bool cache)
290 *
291 */
292 PHP_METHOD(HTTPi_Response, setCache)
293 {
294 zend_bool do_cache = 0;
295 getObject(httpi_response_object, obj);
296
297 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &do_cache)) {
298 RETURN_FALSE;
299 }
300
301 UPD_PROP(obj, long, cache, do_cache);
302 RETURN_TRUE;
303 }
304 /* }}} */
305
306 /* {{{ proto bool HTTPi_Response::getCache()
307 *
308 */
309 PHP_METHOD(HTTPi_Response, getCache)
310 {
311 zval *do_cache = NULL;
312 getObject(httpi_response_object, obj);
313
314 if (ZEND_NUM_ARGS()) {
315 WRONG_PARAM_COUNT;
316 }
317
318 do_cache = GET_PROP(obj, cache);
319 RETURN_BOOL(Z_LVAL_P(do_cache));
320 }
321 /* }}}*/
322
323 /* {{{ proto bool HTTPi_Response::setGzip(bool gzip)
324 *
325 */
326 PHP_METHOD(HTTPi_Response, setGzip)
327 {
328 zend_bool do_gzip = 0;
329 getObject(httpi_response_object, obj);
330
331 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &do_gzip)) {
332 RETURN_FALSE;
333 }
334
335 UPD_PROP(obj, long, gzip, do_gzip);
336 RETURN_TRUE;
337 }
338 /* }}} */
339
340 /* {{{ proto bool HTTPi_Response::getGzip()
341 *
342 */
343 PHP_METHOD(HTTPi_Response, getGzip)
344 {
345 zval *do_gzip = NULL;
346 getObject(httpi_response_object, obj);
347
348 if (ZEND_NUM_ARGS()) {
349 WRONG_PARAM_COUNT;
350 }
351
352 do_gzip = GET_PROP(obj, gzip);
353 RETURN_BOOL(Z_LVAL_P(do_gzip));
354 }
355 /* }}} */
356
357 /* {{{ proto bool HTTPi_Response::setCacheControl(string control[, bool raw = false])
358 *
359 */
360 PHP_METHOD(HTTPi_Response, setCacheControl)
361 {
362 char *ccontrol;
363 int cc_len;
364 zend_bool raw = 0;
365 getObject(httpi_response_object, obj);
366
367 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &ccontrol, &cc_len, &raw)) {
368 RETURN_FALSE;
369 }
370
371 if ((!raw) && (strcmp(ccontrol, "public") && strcmp(ccontrol, "private") && strcmp(ccontrol, "no-cache"))) {
372 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cache-Control '%s' doesn't match public, private or no-cache", ccontrol);
373 RETURN_FALSE;
374 }
375
376 UPD_PROP(obj, long, raw_cache_header, raw);
377 UPD_PROP(obj, string, cacheControl, ccontrol);
378 RETURN_TRUE;
379 }
380 /* }}} */
381
382 /* {{{ proto string HTTPi_Response::getCacheControl()
383 *
384 */
385 PHP_METHOD(HTTPi_Response, getCacheControl)
386 {
387 zval *ccontrol;
388 getObject(httpi_response_object, obj);
389
390 if (ZEND_NUM_ARGS()) {
391 WRONG_PARAM_COUNT;
392 }
393
394 ccontrol = GET_PROP(obj, cacheControl);
395 RETURN_STRINGL(Z_STRVAL_P(ccontrol), Z_STRLEN_P(ccontrol), 1);
396 }
397 /* }}} */
398
399 /* {{{ proto bool HTTPi::setContentType(string content_type)
400 *
401 */
402 PHP_METHOD(HTTPi_Response, setContentType)
403 {
404 char *ctype;
405 int ctype_len;
406 getObject(httpi_response_object, obj);
407
408 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ctype_len)) {
409 RETURN_FALSE;
410 }
411
412 if (!strchr(ctype, '/')) {
413 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Content type '%s' doesn't seem to contain a primary and secondary part", ctype);
414 RETURN_FALSE;
415 }
416
417 UPD_PROP(obj, string, contentType, ctype);
418 RETURN_TRUE;
419 }
420 /* }}} */
421
422 /* {{{ proto string HTTPi_Response::getContentType()
423 *
424 */
425 PHP_METHOD(HTTPi_Response, getContentType)
426 {
427 zval *ctype;
428 getObject(httpi_response_object, obj);
429
430 if (ZEND_NUM_ARGS()) {
431 WRONG_PARAM_COUNT;
432 }
433
434 ctype = GET_PROP(obj, contentType);
435 RETURN_STRINGL(Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
436 }
437 /* }}} */
438
439 /* {{{ proto bool HTTPi_Response::setContentDisposition(string filename[, bool inline = false])
440 *
441 */
442 PHP_METHOD(HTTPi_Response, setContentDisposition)
443 {
444 char *file;
445 int file_len;
446 zend_bool is_inline = 0;
447 getObject(httpi_response_object, obj);
448
449 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &file, &file_len, &is_inline)) {
450 RETURN_FALSE;
451 }
452
453 UPD_PROP(obj, string, dispoFile, file);
454 UPD_PROP(obj, long, dispoInline, is_inline);
455 RETURN_TRUE;
456 }
457 /* }}} */
458
459 /* {{{ proto array HTTPi_Response::getContentDisposition()
460 *
461 */
462 PHP_METHOD(HTTPi_Response, getContentDisposition)
463 {
464 zval *file;
465 zval *is_inline;
466 getObject(httpi_response_object, obj);
467
468 if (ZEND_NUM_ARGS()) {
469 WRONG_PARAM_COUNT;
470 }
471
472 file = GET_PROP(obj, dispoFile);
473 is_inline = GET_PROP(obj, dispoInline);
474
475 array_init(return_value);
476 add_assoc_stringl(return_value, "filename", Z_STRVAL_P(file), Z_STRLEN_P(file), 1);
477 add_assoc_bool(return_value, "inline", Z_LVAL_P(is_inline));
478 }
479 /* }}} */
480
481 /* {{{ proto bool HTTPi_Response::setETag(string etag)
482 *
483 */
484 PHP_METHOD(HTTPi_Response, setETag)
485 {
486 char *etag;
487 int etag_len;
488 getObject(httpi_response_object, obj);
489
490 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &etag, &etag_len)) {
491 RETURN_FALSE;
492 }
493
494 UPD_PROP(obj, string, eTag, etag);
495 RETURN_TRUE;
496 }
497 /* }}} */
498
499 /* {{{ proto string HTTPi_Response::getETag()
500 *
501 */
502 PHP_METHOD(HTTPi_Response, getETag)
503 {
504 zval *etag;
505 getObject(httpi_response_object, obj);
506
507 if (ZEND_NUM_ARGS()) {
508 WRONG_PARAM_COUNT;
509 }
510
511 etag = GET_PROP(obj, eTag);
512 RETURN_STRINGL(Z_STRVAL_P(etag), Z_STRLEN_P(etag), 1);
513 }
514 /* }}} */
515
516 #endif /* ZEND_ENGINE_2 */
517
518 /* {{{ http_module_entry */
519 zend_module_entry http_module_entry = {
520 #if ZEND_MODULE_API_NO >= 20010901
521 STANDARD_MODULE_HEADER,
522 #endif
523 "http",
524 http_functions,
525 PHP_MINIT(http),
526 PHP_MSHUTDOWN(http),
527 PHP_RINIT(http),
528 PHP_RSHUTDOWN(http),
529 PHP_MINFO(http),
530 #if ZEND_MODULE_API_NO >= 20010901
531 PHP_EXT_HTTP_VERSION,
532 #endif
533 STANDARD_MODULE_PROPERTIES
534 };
535 /* }}} */
536
537 /* {{{ proto string http_date([int timestamp])
538 *
539 * This function returns a valid HTTP date regarding RFC 822/1123
540 * looking like: "Wed, 22 Dec 2004 11:34:47 GMT"
541 *
542 */
543 PHP_FUNCTION(http_date)
544 {
545 long t = -1;
546
547 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
548 RETURN_FALSE;
549 }
550
551 if (t == -1) {
552 t = (long) time(NULL);
553 }
554
555 RETURN_STRING(http_date(t), 0);
556 }
557 /* }}} */
558
559 /* {{{ proto string http_absolute_uri(string url[, string proto])
560 *
561 * This function returns an absolute URI constructed from url.
562 * If the url is already abolute but a different proto was supplied,
563 * only the proto part of the URI will be updated. If url has no
564 * path specified, the path of the current REQUEST_URI will be taken.
565 * The host will be taken either from the Host HTTP header of the client
566 * the SERVER_NAME or just localhost if prior are not available.
567 *
568 * Some examples:
569 * <pre>
570 * url = "page.php" => http://www.example.com/current/path/page.php
571 * url = "/page.php" => http://www.example.com/page.php
572 * url = "/page.php", proto = "https" => https://www.example.com/page.php
573 * </pre>
574 *
575 */
576 PHP_FUNCTION(http_absolute_uri)
577 {
578 char *url = NULL, *proto = NULL;
579 int url_len = 0, proto_len = 0;
580
581 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &url, &url_len, &proto, &proto_len) != SUCCESS) {
582 RETURN_FALSE;
583 }
584
585 RETURN_STRING(http_absolute_uri(url, proto), 0);
586 }
587 /* }}} */
588
589 /* {{{ proto string http_negotiate_language(array supported[, string default = 'en-US'])
590 *
591 * This function negotiates the clients preferred language based on its
592 * Accept-Language HTTP header. It returns the negotiated language or
593 * the default language if none match.
594 *
595 * The qualifier is recognized and languages without qualifier are rated highest.
596 *
597 * The supported parameter is expected to be an array having
598 * the supported languages as array values.
599 *
600 * Example:
601 * <pre>
602 * <?php
603 * $langs = array(
604 * 'en-US',// default
605 * 'fr',
606 * 'fr-FR',
607 * 'de',
608 * 'de-DE',
609 * 'de-AT',
610 * 'de-CH',
611 * );
612 * include './langs/'. http_negotiate_language($langs) .'.php';
613 * ?>
614 * </pre>
615 *
616 */
617 PHP_FUNCTION(http_negotiate_language)
618 {
619 zval *supported;
620 char *def = NULL;
621 int def_len = 0;
622
623 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|s", &supported, &def, &def_len) != SUCCESS) {
624 RETURN_FALSE;
625 }
626
627 if (!def) {
628 def = "en-US";
629 }
630
631 RETURN_STRING(http_negotiate_language(supported, def), 0);
632 }
633 /* }}} */
634
635 /* {{{ proto string http_negotiate_charset(array supported[, string default = 'iso-8859-1'])
636 *
637 * This function negotiates the clients preferred charset based on its
638 * Accept-Charset HTTP header. It returns the negotiated charset or
639 * the default charset if none match.
640 *
641 * The qualifier is recognized and charset without qualifier are rated highest.
642 *
643 * The supported parameter is expected to be an array having
644 * the supported charsets as array values.
645 *
646 * Example:
647 * <pre>
648 * <?php
649 * $charsets = array(
650 * 'iso-8859-1', // default
651 * 'iso-8859-2',
652 * 'iso-8859-15',
653 * 'utf-8'
654 * );
655 * $pref = http_negotiate_charset($charsets);
656 * if (!strcmp($pref, 'iso-8859-1')) {
657 * iconv_set_encoding('internal_encoding', 'iso-8859-1');
658 * iconv_set_encoding('output_encoding', $pref);
659 * ob_start('ob_iconv_handler');
660 * }
661 * ?>
662 * </pre>
663 */
664 PHP_FUNCTION(http_negotiate_charset)
665 {
666 zval *supported;
667 char *def = NULL;
668 int def_len = 0;
669
670 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|s", &supported, &def, &def_len) != SUCCESS) {
671 RETURN_FALSE;
672 }
673
674 if (!def) {
675 def = "iso-8859-1";
676 }
677
678 RETURN_STRING(http_negotiate_charset(supported, def), 0);
679 }
680 /* }}} */
681
682 /* {{{ proto bool http_send_status(int status)
683 *
684 * Send HTTP status code.
685 *
686 */
687 PHP_FUNCTION(http_send_status)
688 {
689 int status = 0;
690
691 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &status) != SUCCESS) {
692 RETURN_FALSE;
693 }
694 if (status < 100 || status > 510) {
695 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid HTTP status code (100-510): %d", status);
696 RETURN_FALSE;
697 }
698
699 RETURN_SUCCESS(http_send_status(status));
700 }
701 /* }}} */
702
703 /* {{{ proto bool http_send_last_modified([int timestamp])
704 *
705 * This converts the given timestamp to a valid HTTP date and
706 * sends it as "Last-Modified" HTTP header. If timestamp is
707 * omitted, current time is sent.
708 *
709 */
710 PHP_FUNCTION(http_send_last_modified)
711 {
712 long t = -1;
713
714 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
715 RETURN_FALSE;
716 }
717
718 if (t == -1) {
719 t = (long) time(NULL);
720 }
721
722 RETURN_SUCCESS(http_send_last_modified(t));
723 }
724 /* }}} */
725
726 /* {{{ proto bool http_match_modified([int timestamp])
727 *
728 * Matches the given timestamp against the clients "If-Modified-Since" resp.
729 * "If-Unmodified-Since" HTTP headers.
730 *
731 */
732 PHP_FUNCTION(http_match_modified)
733 {
734 long t = -1;
735
736 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
737 RETURN_FALSE;
738 }
739
740 // current time if not supplied (senseless though)
741 if (t == -1) {
742 t = (long) time(NULL);
743 }
744
745 RETURN_BOOL(http_modified_match("HTTP_IF_MODIFIED_SINCE", t) || http_modified_match("HTTP_IF_UNMODIFIED_SINCE", t));
746 }
747 /* }}} */
748
749 /* {{{ proto bool http_match_etag(string etag)
750 *
751 * This matches the given ETag against the clients
752 * "If-Match" resp. "If-None-Match" HTTP headers.
753 *
754 */
755 PHP_FUNCTION(http_match_etag)
756 {
757 int etag_len;
758 char *etag;
759
760 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &etag, &etag_len) != SUCCESS) {
761 RETURN_FALSE;
762 }
763
764 RETURN_BOOL(http_etag_match("HTTP_IF_NONE_MATCH", etag) || http_etag_match("HTTP_IF_MATCH", etag));
765 }
766 /* }}} */
767
768 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
769 *
770 * If timestamp_or_exires is greater than 0, it is handled as timestamp
771 * and will be sent as date of last modification. If it is 0 or omitted,
772 * the current time will be sent as Last-Modified date. If it's negative,
773 * it is handled as expiration time in seconds, which means that if the
774 * requested last modification date is not between the calculated timespan,
775 * the Last-Modified header is updated and the actual body will be sent.
776 *
777 */
778 PHP_FUNCTION(http_cache_last_modified)
779 {
780 long last_modified = 0, send_modified = 0, t;
781 zval *zlm;
782
783 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &last_modified) != SUCCESS) {
784 RETURN_FALSE;
785 }
786
787 t = (long) time(NULL);
788
789 /* 0 or omitted */
790 if (!last_modified) {
791 /* does the client have? (att: caching "forever") */
792 if (zlm = http_get_server_var("HTTP_IF_MODIFIED_SINCE")) {
793 last_modified = send_modified = http_parse_date(Z_STRVAL_P(zlm));
794 /* send current time */
795 } else {
796 send_modified = t;
797 }
798 /* negative value is supposed to be expiration time */
799 } else if (last_modified < 0) {
800 last_modified += t;
801 send_modified = t;
802 /* send supplied time explicitly */
803 } else {
804 send_modified = last_modified;
805 }
806
807 http_send_header("Cache-Control: private, must-revalidate, max-age=0");
808
809 if (http_modified_match("HTTP_IF_MODIFIED_SINCE", last_modified)) {
810 if (SUCCESS == http_send_status(304)) {
811 zend_bailout();
812 } else {
813 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not send 304 Not Modified");
814 RETURN_FALSE;
815 }
816 }
817 RETURN_SUCCESS(http_send_last_modified(send_modified));
818 }
819 /* }}} */
820
821 /* {{{ proto bool http_cache_etag([string etag])
822 *
823 * This function attempts to cache the HTTP body based on an ETag,
824 * either supplied or generated through calculation of the MD5
825 * checksum of the output (uses output buffering).
826 *
827 * If clients "If-None-Match" header matches the supplied/calculated
828 * ETag, the body is considered cached on the clients side and
829 * a "304 Not Modified" status code is issued.
830 *
831 */
832 PHP_FUNCTION(http_cache_etag)
833 {
834 char *etag;
835 int etag_len = 0;
836
837 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &etag, &etag_len) != SUCCESS) {
838 RETURN_FALSE;
839 }
840
841 http_send_header("Cache-Control: private, must-revalidate, max-age=0");
842
843 if (etag_len) {
844 http_send_etag(etag, etag_len);
845 if (http_etag_match("HTTP_IF_NONE_MATCH", etag)) {
846 if (SUCCESS == http_send_status(304)) {
847 zend_bailout();
848 } else {
849 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not send 304 Not Modified");
850 RETURN_FALSE;
851 }
852 }
853 }
854
855 /* if no etag is given and we didn't already start ob_etaghandler -- start it */
856 if (!HTTP_G(etag_started)) {
857 RETURN_BOOL(HTTP_G(etag_started) = (SUCCESS == http_start_ob_handler(_http_ob_etaghandler, "ob_etaghandler", 4096, 1)));
858 }
859 RETURN_TRUE;
860 }
861 /* }}} */
862
863 /* {{{ proto string ob_httpetaghandler(string data, int mode)
864 *
865 * For use with ob_start().
866 * Note that this has to be started as first output buffer.
867 * WARNING: Don't use with http_send_*().
868 */
869 PHP_FUNCTION(ob_httpetaghandler)
870 {
871 char *data;
872 int data_len;
873 long mode;
874
875 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &data, &data_len, &mode)) {
876 RETURN_FALSE;
877 }
878
879 if (mode & PHP_OUTPUT_HANDLER_START) {
880 if (HTTP_G(etag_started)) {
881 php_error_docref(NULL TSRMLS_CC, E_WARNING, "ob_httpetaghandler can only be used once");
882 RETURN_STRINGL(data, data_len, 1);
883 }
884 http_send_header("Cache-Control: private, must-revalidate, max-age=0");
885 HTTP_G(etag_started) = 1;
886 }
887
888 if (OG(ob_nesting_level) > 1) {
889 php_error_docref(NULL TSRMLS_CC, E_WARNING, "ob_httpetaghandler must be started prior to other output buffers");
890 RETURN_STRINGL(data, data_len, 1);
891 }
892
893 Z_TYPE_P(return_value) = IS_STRING;
894 http_ob_etaghandler(data, data_len, &Z_STRVAL_P(return_value), &Z_STRLEN_P(return_value), mode);
895 }
896 /* }}} */
897
898 /* {{{ proto void http_redirect([string url[, array params[, bool session,[ bool permanent]]]])
899 *
900 * Redirect to a given url.
901 * The supplied url will be expanded with http_absolute_uri(), the params array will
902 * be treated with http_build_query() and the session identification will be appended
903 * if session is true.
904 *
905 * Depending on permanent the redirection will be issued with a permanent
906 * ("301 Moved Permanently") or a temporary ("302 Found") redirection
907 * status code.
908 *
909 * To be RFC compliant, "Redirecting to <a>URI</a>." will be displayed,
910 * if the client doesn't redirect immediatly.
911 */
912 PHP_FUNCTION(http_redirect)
913 {
914 int url_len;
915 zend_bool session = 0, permanent = 0;
916 zval *params = NULL;
917 smart_str qstr = {0};
918 char *url, *URI, LOC[HTTP_URI_MAXLEN + 9], RED[HTTP_URI_MAXLEN * 2 + 34];
919
920 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sa!/bb", &url, &url_len, &params, &session, &permanent) != SUCCESS) {
921 RETURN_FALSE;
922 }
923
924 /* append session info */
925 if (session && (PS(session_status) == php_session_active)) {
926 if (!params) {
927 MAKE_STD_ZVAL(params);
928 array_init(params);
929 }
930 if (add_assoc_string(params, PS(session_name), PS(id), 1) != SUCCESS) {
931 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not append session information");
932 }
933 }
934
935 /* treat params array with http_build_query() */
936 if (params) {
937 if (php_url_encode_hash_ex(Z_ARRVAL_P(params), &qstr, NULL,0,NULL,0,NULL,0,NULL TSRMLS_CC) != SUCCESS) {
938 if (qstr.c) {
939 efree(qstr.c);
940 }
941 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not encode query parameters");
942 RETURN_FALSE;
943 }
944 smart_str_0(&qstr);
945 }
946
947 URI = http_absolute_uri(url, NULL);
948 if (qstr.c) {
949 snprintf(LOC, HTTP_URI_MAXLEN + strlen("Location: "), "Location: %s?%s", URI, qstr.c);
950 sprintf(RED, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI, qstr.c, URI, qstr.c);
951 efree(qstr.c);
952 } else {
953 snprintf(LOC, HTTP_URI_MAXLEN + strlen("Location: "), "Location: %s", URI);
954 sprintf(RED, "Redirecting to <a href=\"%s\">%s</a>.\n", URI, URI);
955 }
956 efree(URI);
957
958 if ((SUCCESS == http_send_header(LOC)) && (SUCCESS == http_send_status((permanent ? 301 : 302)))) {
959 php_body_write(RED, strlen(RED) TSRMLS_CC);
960 RETURN_TRUE;
961 }
962 RETURN_FALSE;
963 }
964 /* }}} */
965
966 /* {{{ proto bool http_send_data(string data)
967 *
968 * Sends raw data with support for (multiple) range requests.
969 *
970 */
971 PHP_FUNCTION(http_send_data)
972 {
973 zval *zdata;
974
975 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zdata) != SUCCESS) {
976 RETURN_FALSE;
977 }
978
979 convert_to_string_ex(&zdata);
980 http_send_header("Accept-Ranges: bytes");
981 RETURN_SUCCESS(http_send_data(zdata));
982 }
983 /* }}} */
984
985 /* {{{ proto bool http_send_file(string file)
986 *
987 * Sends a file with support for (multiple) range requests.
988 *
989 */
990 PHP_FUNCTION(http_send_file)
991 {
992 zval *zfile;
993
994 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zfile) != SUCCESS) {
995 RETURN_FALSE;
996 }
997
998 convert_to_string_ex(&zfile);
999 http_send_header("Accept-Ranges: bytes");
1000 RETURN_SUCCESS(http_send_file(zfile));
1001 }
1002 /* }}} */
1003
1004 /* {{{ proto bool http_send_stream(resource stream)
1005 *
1006 * Sends an already opened stream with support for (multiple) range requests.
1007 *
1008 */
1009 PHP_FUNCTION(http_send_stream)
1010 {
1011 zval *zstream;
1012 php_stream *file;
1013
1014 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream) != SUCCESS) {
1015 RETURN_FALSE;
1016 }
1017
1018 php_stream_from_zval(file, &zstream);
1019 http_send_header("Accept-Ranges: bytes");
1020 RETURN_SUCCESS(http_send_stream(file));
1021 }
1022 /* }}} */
1023
1024 /* {{{ proto bool http_content_type([string content_type = 'application/x-octetstream'])
1025 *
1026 * Sets the content type.
1027 *
1028 */
1029 PHP_FUNCTION(http_content_type)
1030 {
1031 char *ct, *content_type;
1032 int ct_len = 0;
1033
1034 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &ct, &ct_len) != SUCCESS) {
1035 RETURN_FALSE;
1036 }
1037
1038 if (!ct_len) {
1039 RETURN_SUCCESS(http_send_header("Content-Type: application/x-octetstream"));
1040 }
1041
1042 /* remember for multiple ranges */
1043 if (HTTP_G(ctype)) {
1044 efree(HTTP_G(ctype));
1045 }
1046 HTTP_G(ctype) = estrndup(ct, ct_len);
1047
1048 content_type = (char *) emalloc(strlen("Content-Type: ") + ct_len + 1);
1049 sprintf(content_type, "Content-Type: %s", ct);
1050
1051 RETVAL_BOOL(SUCCESS == http_send_header(content_type));
1052 efree(content_type);
1053 }
1054 /* }}} */
1055
1056 /* {{{ proto bool http_content_disposition(string filename[, bool inline = false])
1057 *
1058 * Set the Content Disposition. The Content-Disposition header is very useful
1059 * if the data actually sent came from a file or something similar, that should
1060 * be "saved" by the client/user (i.e. by browsers "Save as..." popup window).
1061 *
1062 */
1063 PHP_FUNCTION(http_content_disposition)
1064 {
1065 char *filename, *header;
1066 int f_len;
1067 zend_bool send_inline = 0;
1068
1069 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &filename, &f_len, &send_inline) != SUCCESS) {
1070 RETURN_FALSE;
1071 }
1072
1073 if (send_inline) {
1074 header = (char *) emalloc(strlen("Content-Disposition: inline; filename=\"\"") + f_len + 1);
1075 sprintf(header, "Content-Disposition: inline; filename=\"%s\"", filename);
1076 } else {
1077 header = (char *) emalloc(strlen("Content-Disposition: attachment; filename=\"\"") + f_len + 1);
1078 sprintf(header, "Content-Disposition: attachment; filename=\"%s\"", filename);
1079 }
1080
1081 RETVAL_BOOL(SUCCESS == http_send_header(header));
1082 efree(header);
1083 }
1084 /* }}} */
1085
1086 /* {{{ proto string http_chunked_decode(string encoded)
1087 *
1088 * This function decodes a string that was HTTP-chunked encoded.
1089 * Returns false on failure.
1090 */
1091 PHP_FUNCTION(http_chunked_decode)
1092 {
1093 char *encoded = NULL, *decoded = NULL;
1094 int encoded_len = 0, decoded_len = 0;
1095
1096 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &encoded, &encoded_len) != SUCCESS) {
1097 RETURN_FALSE;
1098 }
1099
1100 if (SUCCESS == http_chunked_decode(encoded, encoded_len, &decoded, &decoded_len)) {
1101 RETURN_STRINGL(decoded, decoded_len, 0);
1102 } else {
1103 RETURN_FALSE;
1104 }
1105 }
1106 /* }}} */
1107
1108 /* {{{ proto array http_split_response(string http_response)
1109 *
1110 * This function splits an HTTP response into an array with headers and the
1111 * content body. The returned array may look simliar to the following example:
1112 *
1113 * <pre>
1114 * <?php
1115 * array(
1116 * 0 => array(
1117 * 'Status' => '200 Ok',
1118 * 'Content-Type' => 'text/plain',
1119 * 'Content-Language' => 'en-US'
1120 * ),
1121 * 1 => "Hello World!"
1122 * );
1123 * ?>
1124 * </pre>
1125 */
1126 PHP_FUNCTION(http_split_response)
1127 {
1128 zval *zresponse, *zbody, *zheaders;
1129
1130 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zresponse) != SUCCESS) {
1131 RETURN_FALSE;
1132 }
1133
1134 convert_to_string_ex(&zresponse);
1135
1136 MAKE_STD_ZVAL(zbody);
1137 MAKE_STD_ZVAL(zheaders);
1138 array_init(zheaders);
1139
1140 if (SUCCESS != http_split_response(zresponse, zheaders, zbody)) {
1141 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not parse HTTP response");
1142 RETURN_FALSE;
1143 }
1144
1145 array_init(return_value);
1146 add_index_zval(return_value, 0, zheaders);
1147 add_index_zval(return_value, 1, zbody);
1148 }
1149 /* }}} */
1150
1151 /* {{{ proto array http_parse_headers(string header)
1152 *
1153 */
1154 PHP_FUNCTION(http_parse_headers)
1155 {
1156 char *header, *rnrn;
1157 int header_len;
1158
1159 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &header, &header_len)) {
1160 RETURN_FALSE;
1161 }
1162
1163 array_init(return_value);
1164
1165 if (rnrn = strstr(header, HTTP_CRLF HTTP_CRLF)) {
1166 header_len = rnrn - header + 2;
1167 }
1168 if (SUCCESS != http_parse_headers(header, header_len, return_value)) {
1169 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not parse HTTP header");
1170 zval_dtor(return_value);
1171 RETURN_FALSE;
1172 }
1173 }
1174 /* }}}*/
1175
1176 /* {{{ proto array http_get_request_headers(void)
1177 *
1178 */
1179 PHP_FUNCTION(http_get_request_headers)
1180 {
1181 if (ZEND_NUM_ARGS()) {
1182 WRONG_PARAM_COUNT;
1183 }
1184
1185 array_init(return_value);
1186 http_get_request_headers(return_value);
1187 }
1188 /* }}} */
1189
1190 /* {{{ HAVE_CURL */
1191 #ifdef HTTP_HAVE_CURL
1192
1193 /* {{{ proto string http_get(string url[, array options[, array &info]])
1194 *
1195 * Performs an HTTP GET request on the supplied url.
1196 *
1197 * The second parameter is expected to be an associative
1198 * array where the following keys will be recognized:
1199 * <pre>
1200 * - redirect: int, whether and how many redirects to follow
1201 * - unrestrictedauth: bool, whether to continue sending credentials on
1202 * redirects to a different host
1203 * - proxyhost: string, proxy host in "host[:port]" format
1204 * - proxyport: int, use another proxy port as specified in proxyhost
1205 * - proxyauth: string, proxy credentials in "user:pass" format
1206 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
1207 * - httpauth: string, http credentials in "user:pass" format
1208 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
1209 * - compress: bool, whether to allow gzip/deflate content encoding
1210 * (defaults to true)
1211 * - port: int, use another port as specified in the url
1212 * - referer: string, the referer to sends
1213 * - useragent: string, the user agent to send
1214 * (defaults to PECL::HTTP/version (PHP/version)))
1215 * - headers: array, list of custom headers as associative array
1216 * like array("header" => "value")
1217 * - cookies: array, list of cookies as associative array
1218 * like array("cookie" => "value")
1219 * - cookiestore: string, path to a file where cookies are/will be stored
1220 * </pre>
1221 *
1222 * The optional third parameter will be filled with some additional information
1223 * in form af an associative array, if supplied, like the following example:
1224 * <pre>
1225 * <?php
1226 * array (
1227 * 'effective_url' => 'http://localhost',
1228 * 'response_code' => 403,
1229 * 'total_time' => 0.017,
1230 * 'namelookup_time' => 0.013,
1231 * 'connect_time' => 0.014,
1232 * 'pretransfer_time' => 0.014,
1233 * 'size_upload' => 0,
1234 * 'size_download' => 202,
1235 * 'speed_download' => 11882,
1236 * 'speed_upload' => 0,
1237 * 'header_size' => 145,
1238 * 'request_size' => 62,
1239 * 'ssl_verifyresult' => 0,
1240 * 'filetime' => -1,
1241 * 'content_length_download' => 202,
1242 * 'content_length_upload' => 0,
1243 * 'starttransfer_time' => 0.017,
1244 * 'content_type' => 'text/html; charset=iso-8859-1',
1245 * 'redirect_time' => 0,
1246 * 'redirect_count' => 0,
1247 * 'private' => '',
1248 * 'http_connectcode' => 0,
1249 * 'httpauth_avail' => 0,
1250 * 'proxyauth_avail' => 0,
1251 * )
1252 * ?>
1253 * </pre>
1254 */
1255 PHP_FUNCTION(http_get)
1256 {
1257 char *URL, *data = NULL;
1258 size_t data_len = 0;
1259 int URL_len;
1260 zval *options = NULL, *info = NULL;
1261
1262 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
1263 RETURN_FALSE;
1264 }
1265
1266 if (info) {
1267 zval_dtor(info);
1268 array_init(info);
1269 }
1270
1271 if (SUCCESS == http_get(URL, HASH_ORNULL(options), HASH_ORNULL(info), &data, &data_len)) {
1272 RETURN_STRINGL(data, data_len, 0);
1273 } else {
1274 RETURN_FALSE;
1275 }
1276 }
1277 /* }}} */
1278
1279 /* {{{ proto string http_head(string url[, array options[, array &info]])
1280 *
1281 * Performs an HTTP HEAD request on the suppied url.
1282 * Returns the HTTP response as string.
1283 * See http_get() for a full list of available options.
1284 */
1285 PHP_FUNCTION(http_head)
1286 {
1287 char *URL, *data = NULL;
1288 size_t data_len = 0;
1289 int URL_len;
1290 zval *options = NULL, *info = NULL;
1291
1292 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
1293 RETURN_FALSE;
1294 }
1295
1296 if (info) {
1297 zval_dtor(info);
1298 array_init(info);
1299 }
1300
1301 if (SUCCESS == http_head(URL, HASH_ORNULL(options), HASH_ORNULL(info), &data, &data_len)) {
1302 RETURN_STRINGL(data, data_len, 0);
1303 } else {
1304 RETURN_FALSE;
1305 }
1306 }
1307 /* }}} */
1308
1309 /* {{{ proto string http_post_data(string url, string data[, array options[, &info]])
1310 *
1311 * Performs an HTTP POST request, posting data.
1312 * Returns the HTTP response as string.
1313 * See http_get() for a full list of available options.
1314 */
1315 PHP_FUNCTION(http_post_data)
1316 {
1317 char *URL, *postdata, *data = NULL;
1318 size_t data_len = 0;
1319 int postdata_len, URL_len;
1320 zval *options = NULL, *info = NULL;
1321
1322 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &postdata, &postdata_len, &options, &info) != SUCCESS) {
1323 RETURN_FALSE;
1324 }
1325
1326 if (info) {
1327 zval_dtor(info);
1328 array_init(info);
1329 }
1330
1331 if (SUCCESS == http_post_data(URL, postdata, (size_t) postdata_len, HASH_ORNULL(options), HASH_ORNULL(info), &data, &data_len)) {
1332 RETURN_STRINGL(data, data_len, 0);
1333 } else {
1334 RETURN_FALSE;
1335 }
1336 }
1337 /* }}} */
1338
1339 /* {{{ proto string http_post_array(string url, array data[, array options[, array &info]])
1340 *
1341 * Performs an HTTP POST request, posting www-form-urlencoded array data.
1342 * Returns the HTTP response as string.
1343 * See http_get() for a full list of available options.
1344 */
1345 PHP_FUNCTION(http_post_array)
1346 {
1347 char *URL, *data = NULL;
1348 size_t data_len = 0;
1349 int URL_len;
1350 zval *options = NULL, *info = NULL, *postdata;
1351
1352 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa|a/!z", &URL, &URL_len, &postdata, &options, &info) != SUCCESS) {
1353 RETURN_FALSE;
1354 }
1355
1356 if (info) {
1357 zval_dtor(info);
1358 array_init(info);
1359 }
1360
1361 if (SUCCESS == http_post_array(URL, Z_ARRVAL_P(postdata), HASH_ORNULL(options), HASH_ORNULL(info), &data, &data_len)) {
1362 RETURN_STRINGL(data, data_len, 0);
1363 } else {
1364 RETURN_FALSE;
1365 }
1366 }
1367 /* }}} */
1368
1369 #endif
1370 /* }}} HAVE_CURL */
1371
1372
1373 /* {{{ proto bool http_auth_basic(string user, string pass[, string realm = "Restricted"])
1374 *
1375 * Example:
1376 * <pre>
1377 * <?php
1378 * if (!http_auth_basic('mike', 's3c|r3t')) {
1379 * die('<h1>Authorization failed!</h1>');
1380 * }
1381 * ?>
1382 * </pre>
1383 */
1384 PHP_FUNCTION(http_auth_basic)
1385 {
1386 char *realm = NULL, *user, *pass, *suser, *spass;
1387 int r_len, u_len, p_len;
1388
1389 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &user, &u_len, &pass, &p_len, &realm, &r_len) != SUCCESS) {
1390 RETURN_FALSE;
1391 }
1392
1393 if (!realm) {
1394 realm = "Restricted";
1395 }
1396
1397 if (SUCCESS != http_auth_credentials(&suser, &spass)) {
1398 http_auth_header("Basic", realm);
1399 RETURN_FALSE;
1400 }
1401
1402 if (strcasecmp(suser, user)) {
1403 http_auth_header("Basic", realm);
1404 RETURN_FALSE;
1405 }
1406
1407 if (strcmp(spass, pass)) {
1408 http_auth_header("Basic", realm);
1409 RETURN_FALSE;
1410 }
1411
1412 RETURN_TRUE;
1413 }
1414 /* }}} */
1415
1416 /* {{{ proto bool http_auth_basic_cb(mixed callback[, string realm = "Restricted"])
1417 *
1418 * Example:
1419 * <pre>
1420 * <?php
1421 * function auth_cb($user, $pass)
1422 * {
1423 * global $db;
1424 * $query = 'SELECT pass FROM users WHERE user='. $db->quoteSmart($user);
1425 * if (strlen($realpass = $db->getOne($query)) {
1426 * return $pass === $realpass;
1427 * }
1428 * return false;
1429 * }
1430 *
1431 * if (!http_auth_basic_cb('auth_cb')) {
1432 * die('<h1>Authorization failed</h1>');
1433 * }
1434 * ?>
1435 * </pre>
1436 */
1437 PHP_FUNCTION(http_auth_basic_cb)
1438 {
1439 zval *cb;
1440 char *realm = NULL, *user, *pass;
1441 int r_len;
1442
1443 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|s", &cb, &realm, &r_len) != SUCCESS) {
1444 RETURN_FALSE;
1445 }
1446
1447 if (!realm) {
1448 realm = "Restricted";
1449 }
1450
1451 if (SUCCESS != http_auth_credentials(&user, &pass)) {
1452 http_auth_header("Basic", realm);
1453 RETURN_FALSE;
1454 }
1455 {
1456 zval *zparams[2] = {NULL, NULL}, retval;
1457 int result = 0;
1458
1459 MAKE_STD_ZVAL(zparams[0]);
1460 MAKE_STD_ZVAL(zparams[1]);
1461 ZVAL_STRING(zparams[0], user, 0);
1462 ZVAL_STRING(zparams[1], pass, 0);
1463
1464 if (SUCCESS == call_user_function(EG(function_table), NULL, cb,
1465 &retval, 2, zparams TSRMLS_CC)) {
1466 result = Z_LVAL(retval);
1467 }
1468
1469 efree(user);
1470 efree(pass);
1471 efree(zparams[0]);
1472 efree(zparams[1]);
1473
1474 if (!result) {
1475 http_auth_header("Basic", realm);
1476 }
1477
1478 RETURN_BOOL(result);
1479 }
1480 }
1481 /* }}}*/
1482
1483
1484 /* {{{ php_http_init_globals(zend_http_globals *) */
1485 static void php_http_init_globals(zend_http_globals *http_globals)
1486 {
1487 http_globals->etag_started = 0;
1488 http_globals->ctype = NULL;
1489 http_globals->etag = NULL;
1490 http_globals->lmod = 0;
1491 #ifdef HTTP_HAVE_CURL
1492 http_globals->curlbuf.body.data = NULL;
1493 http_globals->curlbuf.body.used = 0;
1494 http_globals->curlbuf.body.free = 0;
1495 http_globals->curlbuf.hdrs.data = NULL;
1496 http_globals->curlbuf.hdrs.used = 0;
1497 http_globals->curlbuf.hdrs.free = 0;
1498 #endif
1499 http_globals->allowed_methods = NULL;
1500 }
1501 /* }}} */
1502
1503 /* {{{ static inline STATUS http_check_allowed_methods(char *, int) */
1504 #define http_check_allowed_methods(m, l) _http_check_allowed_methods((m), (l) TSRMLS_CC)
1505 static inline void _http_check_allowed_methods(char *methods, int length TSRMLS_DC)
1506 {
1507 if (length && SG(request_info).request_method && (!strstr(methods, SG(request_info).request_method))) {
1508 char *allow_header = emalloc(length + sizeof("Allow: "));
1509 sprintf(allow_header, "Allow: %s", methods);
1510 http_send_header(allow_header);
1511 efree(allow_header);
1512 http_send_status(405);
1513 zend_bailout();
1514 }
1515 }
1516 /* }}} */
1517
1518 /* {{{ PHP_INI */
1519 PHP_INI_MH(update_allowed_methods)
1520 {
1521 http_check_allowed_methods(new_value, new_value_length);
1522 return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
1523 }
1524
1525 PHP_INI_BEGIN()
1526 STD_PHP_INI_ENTRY("http.allowed_methods", "OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE,CONNECT", PHP_INI_ALL, update_allowed_methods, allowed_methods, zend_http_globals, http_globals)
1527 PHP_INI_END()
1528 /* }}} */
1529
1530 /* {{{ PHP_MINIT_FUNCTION */
1531 PHP_MINIT_FUNCTION(http)
1532 {
1533 ZEND_INIT_MODULE_GLOBALS(http, php_http_init_globals, NULL);
1534 REGISTER_INI_ENTRIES();
1535 /*
1536 REGISTER_LONG_CONSTANT("HTTP_GET", HTTP_GET, CONST_CS | CONST_PERSISTENT);
1537 REGISTER_LONG_CONSTANT("HTTP_HEAD", HTTP_HEAD, CONST_CS | CONST_PERSISTENT);
1538 REGISTER_LONG_CONSTANT("HTTP_POST", HTTP_POST, CONST_CS | CONST_PERSISTENT);
1539 */
1540 #ifdef HTTP_HAVE_CURL
1541 REGISTER_LONG_CONSTANT("HTTP_AUTH_BASIC", CURLAUTH_BASIC, CONST_CS | CONST_PERSISTENT);
1542 REGISTER_LONG_CONSTANT("HTTP_AUTH_DIGEST", CURLAUTH_DIGEST, CONST_CS | CONST_PERSISTENT);
1543 REGISTER_LONG_CONSTANT("HTTP_AUTH_NTLM", CURLAUTH_NTLM, CONST_CS | CONST_PERSISTENT);
1544 #endif
1545
1546 #ifdef ZEND_ENGINE_2
1547 HTTP_REGISTER_CLASS(HTTPi, httpi, NULL, ZEND_ACC_FINAL_CLASS);
1548 HTTP_REGISTER_CLASS_EX(HTTPi_Response, httpi_response, NULL, 0);
1549 #endif
1550 return SUCCESS;
1551 }
1552 /* }}} */
1553
1554 /* {{{ PHP_MSHUTDOWN_FUNCTION */
1555 PHP_MSHUTDOWN_FUNCTION(http)
1556 {
1557 UNREGISTER_INI_ENTRIES();
1558 return SUCCESS;
1559 }
1560 /* }}} */
1561
1562 /* {{{ PHP_RINIT_FUNCTION */
1563 PHP_RINIT_FUNCTION(http)
1564 {
1565 char *allowed_methods = INI_STR("http.allowed_methods");
1566 http_check_allowed_methods(allowed_methods, strlen(allowed_methods));
1567 return SUCCESS;
1568 }
1569 /* }}} */
1570
1571 /* {{{ PHP_RSHUTDOWN_FUNCTION */
1572 PHP_RSHUTDOWN_FUNCTION(http)
1573 {
1574 HTTP_G(etag_started) = 0;
1575 HTTP_G(lmod) = 0;
1576
1577 if (HTTP_G(etag)) {
1578 efree(HTTP_G(etag));
1579 HTTP_G(etag) = NULL;
1580 }
1581
1582 if (HTTP_G(ctype)) {
1583 efree(HTTP_G(ctype));
1584 HTTP_G(ctype) = NULL;
1585 }
1586 #ifdef HTTP_HAVE_CURL
1587 if (HTTP_G(curlbuf).body.data) {
1588 efree(HTTP_G(curlbuf).body.data);
1589 HTTP_G(curlbuf).body.data = NULL;
1590 }
1591 if (HTTP_G(curlbuf).hdrs.data) {
1592 efree(HTTP_G(curlbuf).hdrs.data);
1593 HTTP_G(curlbuf).hdrs.data = NULL;
1594 }
1595 #endif
1596 return SUCCESS;
1597 }
1598 /* }}} */
1599
1600 /* {{{ PHP_MINFO_FUNCTION */
1601 PHP_MINFO_FUNCTION(http)
1602 {
1603 php_info_print_table_start();
1604 php_info_print_table_header(2, "Extended HTTP support", "enabled");
1605 php_info_print_table_row(2, "Version:", PHP_EXT_HTTP_VERSION);
1606 php_info_print_table_row(2, "cURL convenience functions:",
1607 #ifdef HTTP_HAVE_CURL
1608 "enabled"
1609 #else
1610 "disabled"
1611 #endif
1612 );
1613 php_info_print_table_end();
1614
1615 DISPLAY_INI_ENTRIES();
1616 }
1617 /* }}} */
1618
1619 /*
1620 * Local variables:
1621 * tab-width: 4
1622 * c-basic-offset: 4
1623 * End:
1624 * vim600: noet sw=4 ts=4 fdm=marker
1625 * vim<600: noet sw=4 ts=4
1626 */
1627