2 +----------------------------------------------------------------------+
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 +----------------------------------------------------------------------+
18 #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
26 #include "ext/standard/info.h"
27 #include "ext/session/php_session.h"
28 #include "ext/standard/php_string.h"
29 #include "ext/standard/php_smart_str.h"
32 #include "php_http_api.h"
35 #include "ext/standard/php_http.h"
42 #include <sys/types.h>
45 #include <curl/curl.h>
48 ZEND_DECLARE_MODULE_GLOBALS(http
)
50 #ifdef COMPILE_DL_HTTP
55 #ifdef ZEND_BEGIN_ARG_INFO
56 ZEND_BEGIN_ARG_INFO(http_request_info_ref_3
, 0)
62 ZEND_BEGIN_ARG_INFO(http_request_info_ref_4
, 0)
69 static unsigned char http_request_info_ref_3
[] = {3, BYREF_NONE
, BYREF_FORCE_REST
};
70 static unsigned char http_request_info_ref_4
[] = {4, BYREF_NONE
, BYREF_FORCE_REST
};
74 /* {{{ http_functions[] */
75 function_entry http_functions
[] = {
76 PHP_FE(http_date
, NULL
)
77 PHP_FE(http_absolute_uri
, NULL
)
78 PHP_FE(http_negotiate_language
, NULL
)
79 PHP_FE(http_negotiate_charset
, NULL
)
80 PHP_FE(http_redirect
, NULL
)
81 PHP_FE(http_send_status
, NULL
)
82 PHP_FE(http_send_last_modified
, NULL
)
83 PHP_FE(http_match_modified
, NULL
)
84 PHP_FE(http_match_etag
, NULL
)
85 PHP_FE(http_cache_last_modified
, NULL
)
86 PHP_FE(http_cache_etag
, NULL
)
87 PHP_FE(http_content_type
, NULL
)
88 PHP_FE(http_content_disposition
, NULL
)
89 PHP_FE(http_send_data
, NULL
)
90 PHP_FE(http_send_file
, NULL
)
91 PHP_FE(http_send_stream
, NULL
)
92 PHP_FE(http_chunked_decode
, NULL
)
93 PHP_FE(http_split_response
, NULL
)
94 PHP_FE(http_parse_headers
, NULL
)
96 PHP_FE(http_get
, http_request_info_ref_3
)
97 PHP_FE(http_head
, http_request_info_ref_3
)
98 PHP_FE(http_post_data
, http_request_info_ref_4
)
99 PHP_FE(http_post_array
, http_request_info_ref_4
)
101 PHP_FE(http_auth_basic
, NULL
)
102 PHP_FE(http_auth_basic_cb
, NULL
)
103 #ifndef ZEND_ENGINE_2
104 PHP_FE(http_build_query
, NULL
)
110 /* {{{ http_module_entry */
111 zend_module_entry http_module_entry
= {
112 #if ZEND_MODULE_API_NO >= 20010901
113 STANDARD_MODULE_HEADER
,
122 #if ZEND_MODULE_API_NO >= 20010901
123 PHP_EXT_HTTP_VERSION
,
125 STANDARD_MODULE_PROPERTIES
129 #define RETURN_SUCCESS(v) RETURN_BOOL(SUCCESS == (v))
130 #define HASH_ORNULL(z) ((z) ? Z_ARRVAL_P(z) : NULL)
132 /* {{{ proto string http_date([int timestamp])
134 * This function returns a valid HTTP date regarding RFC 822/1123
135 * looking like: "Wed, 22 Dec 2004 11:34:47 GMT"
138 PHP_FUNCTION(http_date
)
142 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
147 t
= (long) time(NULL
);
150 RETURN_STRING(http_date(t
), 0);
154 /* {{{ proto string http_absolute_uri(string url[, string proto])
156 * This function returns an absolute URI constructed from url.
157 * If the url is already abolute but a different proto was supplied,
158 * only the proto part of the URI will be updated. If url has no
159 * path specified, the path of the current REQUEST_URI will be taken.
160 * The host will be taken either from the Host HTTP header of the client
161 * the SERVER_NAME or just localhost if prior are not available.
165 * url = "page.php" => http://www.example.com/current/path/page.php
166 * url = "/page.php" => http://www.example.com/page.php
167 * url = "/page.php", proto = "https" => https://www.example.com/page.php
171 PHP_FUNCTION(http_absolute_uri
)
173 char *url
= NULL
, *proto
= NULL
;
174 int url_len
= 0, proto_len
= 0;
176 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|s", &url
, &url_len
, &proto
, &proto_len
) != SUCCESS
) {
180 RETURN_STRING(http_absolute_uri(url
, proto
), 0);
184 /* {{{ proto string http_negotiate_language(array supported[, string default = 'en-US'])
186 * This function negotiates the clients preferred language based on its
187 * Accept-Language HTTP header. It returns the negotiated language or
188 * the default language if none match.
190 * The qualifier is recognized and languages without qualifier are rated highest.
192 * The supported parameter is expected to be an array having
193 * the supported languages as array values.
207 * include './langs/'. http_negotiate_language($langs) .'.php';
212 PHP_FUNCTION(http_negotiate_language
)
218 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|s", &supported
, &def
, &def_len
) != SUCCESS
) {
226 RETURN_STRING(http_negotiate_language(supported
, def
), 0);
230 /* {{{ proto string http_negotiate_charset(array supported[, string default = 'iso-8859-1'])
232 * This function negotiates the clients preferred charset based on its
233 * Accept-Charset HTTP header. It returns the negotiated charset or
234 * the default charset if none match.
236 * The qualifier is recognized and charset without qualifier are rated highest.
238 * The supported parameter is expected to be an array having
239 * the supported charsets as array values.
245 * 'iso-8859-1', // default
250 * $pref = http_negotiate_charset($charsets);
251 * if (!strcmp($pref, 'iso-8859-1')) {
252 * iconv_set_encoding('internal_encoding', 'iso-8859-1');
253 * iconv_set_encoding('output_encoding', $pref);
254 * ob_start('ob_iconv_handler');
259 PHP_FUNCTION(http_negotiate_charset
)
265 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|s", &supported
, &def
, &def_len
) != SUCCESS
) {
273 RETURN_STRING(http_negotiate_charset(supported
, def
), 0);
277 /* {{{ proto bool http_send_status(int status)
279 * Send HTTP status code.
282 PHP_FUNCTION(http_send_status
)
286 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &status
) != SUCCESS
) {
289 if (status
< 100 || status
> 510) {
290 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Invalid HTTP status code (100-510): %d", status
);
294 RETURN_SUCCESS(http_send_status(status
));
298 /* {{{ proto bool http_send_last_modified([int timestamp])
300 * This converts the given timestamp to a valid HTTP date and
301 * sends it as "Last-Modified" HTTP header. If timestamp is
302 * omitted, current time is sent.
305 PHP_FUNCTION(http_send_last_modified
)
309 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
314 t
= (long) time(NULL
);
317 RETURN_SUCCESS(http_send_last_modified(t
));
321 /* {{{ proto bool http_match_modified([int timestamp])
323 * Matches the given timestamp against the clients "If-Modified-Since" resp.
324 * "If-Unmodified-Since" HTTP headers.
327 PHP_FUNCTION(http_match_modified
)
331 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
335 // current time if not supplied (senseless though)
337 t
= (long) time(NULL
);
340 RETURN_BOOL(http_modified_match("HTTP_IF_MODIFIED_SINCE", t
) || http_modified_match("HTTP_IF_UNMODIFIED_SINCE", t
));
344 /* {{{ proto bool http_match_etag(string etag)
346 * This matches the given ETag against the clients
347 * "If-Match" resp. "If-None-Match" HTTP headers.
350 PHP_FUNCTION(http_match_etag
)
355 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &etag
, &etag_len
) != SUCCESS
) {
359 RETURN_BOOL(http_etag_match("HTTP_IF_NONE_MATCH", etag
) || http_etag_match("HTTP_IF_MATCH", etag
));
363 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
365 * If timestamp_or_exires is greater than 0, it is handled as timestamp
366 * and will be sent as date of last modification. If it is 0 or omitted,
367 * the current time will be sent as Last-Modified date. If it's negative,
368 * it is handled as expiration time in seconds, which means that if the
369 * requested last modification date is not between the calculated timespan,
370 * the Last-Modified header is updated and the actual body will be sent.
373 PHP_FUNCTION(http_cache_last_modified
)
375 long last_modified
= 0, send_modified
= 0, t
;
378 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &last_modified
) != SUCCESS
) {
382 t
= (long) time(NULL
);
385 if (!last_modified
) {
386 /* does the client have? (att: caching "forever") */
387 if (zlm
= http_get_server_var("HTTP_IF_MODIFIED_SINCE")) {
388 last_modified
= send_modified
= http_parse_date(Z_STRVAL_P(zlm
));
389 /* send current time */
393 /* negative value is supposed to be expiration time */
394 } else if (last_modified
< 0) {
397 /* send supplied time explicitly */
399 send_modified
= last_modified
;
402 http_send_header("Cache-Control: private, must-revalidate, max-age=0");
404 if (http_modified_match("HTTP_IF_MODIFIED_SINCE", last_modified
)) {
405 if (SUCCESS
== http_send_status(304)) {
408 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not send 304 Not Modified");
412 RETURN_SUCCESS(http_send_last_modified(send_modified
));
416 /* {{{ proto bool http_cache_etag([string etag])
418 * This function attempts to cache the HTTP body based on an ETag,
419 * either supplied or generated through calculation of the MD5
420 * checksum of the output (uses output buffering).
422 * If clients "If-None-Match" header matches the supplied/calculated
423 * ETag, the body is considered cached on the clients side and
424 * a "304 Not Modified" status code is issued.
427 PHP_FUNCTION(http_cache_etag
)
432 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &etag
, &etag_len
) != SUCCESS
) {
436 php_end_ob_buffers(0 TSRMLS_CC
);
437 http_send_header("Cache-Control: private, must-revalidate, max-age=0");
439 /* if no etag is given and we didn't already
440 * start ob_etaghandler -- start it
442 if (!HTTP_G(etag_started
) && !etag_len
) {
443 php_ob_set_internal_handler(_http_ob_etaghandler
, (uint
) 4096, "etag output handler", 0 TSRMLS_CC
);
444 HTTP_G(etag_started
) = 1;
445 RETURN_BOOL(php_start_ob_buffer_named("etag output handler", (uint
) 4096, 0 TSRMLS_CC
));
448 if (http_etag_match("HTTP_IF_NONE_MATCH", etag
)) {
449 if (SUCCESS
== http_send_status(304)) {
452 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not send 304 Not Modified");
457 RETURN_SUCCESS(http_send_etag(etag
, etag_len
));
461 /* {{{ proto void http_redirect([string url[, array params[, bool session,[ bool permanent]]]])
463 * Redirect to a given url.
464 * The supplied url will be expanded with http_absolute_uri(), the params array will
465 * be treated with http_build_query() and the session identification will be appended
466 * if session is true.
468 * Depending on permanent the redirection will be issued with a permanent
469 * ("301 Moved Permanently") or a temporary ("302 Found") redirection
472 * To be RFC compliant, "Redirecting to <a>URI</a>." will be displayed,
473 * if the client doesn't redirect immediatly.
475 PHP_FUNCTION(http_redirect
)
478 zend_bool session
= 0, permanent
= 0;
480 smart_str qstr
= {0};
481 char *url
, *URI
, LOC
[HTTP_URI_MAXLEN
+ 9], RED
[HTTP_URI_MAXLEN
* 2 + 34];
483 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sa!/bb", &url
, &url_len
, ¶ms
, &session
, &permanent
) != SUCCESS
) {
487 /* append session info */
488 if (session
&& (PS(session_status
) == php_session_active
)) {
490 MAKE_STD_ZVAL(params
);
493 if (add_assoc_string(params
, PS(session_name
), PS(id
), 1) != SUCCESS
) {
494 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not append session information");
498 /* treat params array with http_build_query() */
500 if (php_url_encode_hash_ex(Z_ARRVAL_P(params
), &qstr
, NULL
,0,NULL
,0,NULL
,0,NULL TSRMLS_CC
) != SUCCESS
) {
504 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not encode query parameters");
510 URI
= http_absolute_uri(url
, NULL
);
512 snprintf(LOC
, HTTP_URI_MAXLEN
+ strlen("Location: "), "Location: %s?%s", URI
, qstr
.c
);
513 sprintf(RED
, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI
, qstr
.c
, URI
, qstr
.c
);
516 snprintf(LOC
, HTTP_URI_MAXLEN
+ strlen("Location: "), "Location: %s", URI
);
517 sprintf(RED
, "Redirecting to <a href=\"%s\">%s</a>.\n", URI
, URI
);
521 if ((SUCCESS
== http_send_header(LOC
)) && (SUCCESS
== http_send_status((permanent
? 301 : 302)))) {
522 php_body_write(RED
, strlen(RED
) TSRMLS_CC
);
529 /* {{{ proto bool http_send_data(string data)
531 * Sends raw data with support for (multiple) range requests.
534 PHP_FUNCTION(http_send_data
)
538 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zdata
) != SUCCESS
) {
542 convert_to_string_ex(&zdata
);
543 http_send_header("Accept-Ranges: bytes");
544 RETURN_SUCCESS(http_send_data(zdata
));
548 /* {{{ proto bool http_send_file(string file)
550 * Sends a file with support for (multiple) range requests.
553 PHP_FUNCTION(http_send_file
)
557 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zfile
) != SUCCESS
) {
561 convert_to_string_ex(&zfile
);
562 http_send_header("Accept-Ranges: bytes");
563 RETURN_SUCCESS(http_send_file(zfile
));
567 /* {{{ proto bool http_send_stream(resource stream)
569 * Sends an already opened stream with support for (multiple) range requests.
572 PHP_FUNCTION(http_send_stream
)
577 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &zstream
) != SUCCESS
) {
581 php_stream_from_zval(file
, &zstream
);
582 http_send_header("Accept-Ranges: bytes");
583 RETURN_SUCCESS(http_send_stream(file
));
587 /* {{{ proto bool http_content_type([string content_type = 'application/x-octetstream'])
589 * Sets the content type.
592 PHP_FUNCTION(http_content_type
)
594 char *ct
, *content_type
;
597 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &ct
, &ct_len
) != SUCCESS
) {
602 RETURN_SUCCESS(http_send_header("Content-Type: application/x-octetstream"));
605 /* remember for multiple ranges */
607 efree(HTTP_G(ctype
));
609 HTTP_G(ctype
) = estrndup(ct
, ct_len
);
611 content_type
= (char *) emalloc(strlen("Content-Type: ") + ct_len
+ 1);
612 sprintf(content_type
, "Content-Type: %s", ct
);
614 RETVAL_BOOL(SUCCESS
== http_send_header(content_type
));
619 /* {{{ proto bool http_content_disposition(string filename[, bool inline = false])
621 * Set the Content Disposition. The Content-Disposition header is very useful
622 * if the data actually sent came from a file or something similar, that should
623 * be "saved" by the client/user (i.e. by browsers "Save as..." popup window).
626 PHP_FUNCTION(http_content_disposition
)
628 char *filename
, *header
;
630 zend_bool send_inline
= 0;
632 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &filename
, &f_len
, &send_inline
) != SUCCESS
) {
637 header
= (char *) emalloc(strlen("Content-Disposition: inline; filename=\"\"") + f_len
+ 1);
638 sprintf(header
, "Content-Disposition: inline; filename=\"%s\"", filename
);
640 header
= (char *) emalloc(strlen("Content-Disposition: attachment; filename=\"\"") + f_len
+ 1);
641 sprintf(header
, "Content-Disposition: attachment; filename=\"%s\"", filename
);
644 RETVAL_BOOL(SUCCESS
== http_send_header(header
));
649 /* {{{ proto string http_chunked_decode(string encoded)
651 * This function decodes a string that was HTTP-chunked encoded.
652 * Returns false on failure.
654 PHP_FUNCTION(http_chunked_decode
)
656 char *encoded
= NULL
, *decoded
= NULL
;
657 int encoded_len
= 0, decoded_len
= 0;
659 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &encoded
, &encoded_len
) != SUCCESS
) {
663 if (SUCCESS
== http_chunked_decode(encoded
, encoded_len
, &decoded
, &decoded_len
)) {
664 RETURN_STRINGL(decoded
, decoded_len
, 0);
671 /* {{{ proto array http_split_response(string http_response)
673 * This function splits an HTTP response into an array with headers and the
674 * content body. The returned array may look simliar to the following example:
679 * 'Status' => '200 Ok',
680 * 'Content-Type' => 'text/plain',
681 * 'Content-Language' => 'en-US'
683 * 1 => "Hello World!"
687 PHP_FUNCTION(http_split_response
)
689 zval
*zresponse
, *zbody
, *zheaders
;
691 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zresponse
) != SUCCESS
) {
695 convert_to_string_ex(&zresponse
);
697 MAKE_STD_ZVAL(zbody
);
698 MAKE_STD_ZVAL(zheaders
);
699 array_init(zheaders
);
701 if (SUCCESS
!= http_split_response(zresponse
, zheaders
, zbody
)) {
702 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not parse HTTP response");
706 array_init(return_value
);
707 add_index_zval(return_value
, 0, zheaders
);
708 add_index_zval(return_value
, 1, zbody
);
712 /* {{{ proto array http_parse_headers(string header) */
713 PHP_FUNCTION(http_parse_headers
)
718 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &header
, &header_len
)) {
722 array_init(return_value
);
724 if (rnrn
= strstr(header
, HTTP_CRLF HTTP_CRLF
)) {
725 header_len
= rnrn
- header
+ 2;
727 if (SUCCESS
!= http_parse_headers(header
, header_len
, return_value
)) {
728 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not parse HTTP header");
729 zval_dtor(return_value
);
736 #ifdef HTTP_HAVE_CURL
738 /* {{{ proto string http_get(string url[, array options[, array &info]])
740 * Performs an HTTP GET request on the supplied url.
742 * The second parameter is expected to be an associative
743 * array where the following keys will be recognized:
745 * - redirect: int, whether and how many redirects to follow
746 * - unrestrictedauth: bool, whether to continue sending credentials on
747 * redirects to a different host
748 * - proxyhost: string, proxy host in "host[:port]" format
749 * - proxyport: int, use another proxy port as specified in proxyhost
750 * - proxyauth: string, proxy credentials in "user:pass" format
751 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
752 * - httpauth: string, http credentials in "user:pass" format
753 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
754 * - compress: bool, whether to allow gzip/deflate content encoding
756 * - port: int, use another port as specified in the url
757 * - referer: string, the referer to sends
758 * - useragent: string, the user agent to send
759 * (defaults to PECL::HTTP/version (PHP/version)))
760 * - headers: array, list of custom headers as associative array
761 * like array("header" => "value")
762 * - cookies: array, list of cookies as associative array
763 * like array("cookie" => "value")
764 * - cookiestore: string, path to a file where cookies are/will be stored
767 * The optional third parameter will be filled with some additional information
768 * in form af an associative array, if supplied (don't forget to initialize it
769 * with NULL or array()).
771 PHP_FUNCTION(http_get
)
773 char *URL
, *data
= NULL
;
776 zval
*options
= NULL
, *info
= NULL
;
778 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
787 if (SUCCESS
== http_get(URL
, HASH_ORNULL(options
), HASH_ORNULL(info
), &data
, &data_len
)) {
788 RETURN_STRINGL(data
, data_len
, 0);
795 /* {{{ proto string http_head(string url[, array options[, array &info]])
797 * Performs an HTTP HEAD request on the suppied url.
798 * Returns the HTTP response as string.
799 * See http_get() for a full list of available options.
801 PHP_FUNCTION(http_head
)
803 char *URL
, *data
= NULL
;
806 zval
*options
= NULL
, *info
= NULL
;
808 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
817 if (SUCCESS
== http_head(URL
, HASH_ORNULL(options
), HASH_ORNULL(info
), &data
, &data_len
)) {
818 RETURN_STRINGL(data
, data_len
, 0);
825 /* {{{ proto string http_post_data(string url, string data[, array options[, &info]])
827 * Performs an HTTP POST request, posting data.
828 * Returns the HTTP response as string.
829 * See http_get() for a full list of available options.
831 PHP_FUNCTION(http_post_data
)
833 char *URL
, *postdata
, *data
= NULL
;
835 int postdata_len
, URL_len
;
836 zval
*options
= NULL
, *info
= NULL
;
838 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &postdata
, &postdata_len
, &options
, &info
) != SUCCESS
) {
847 if (SUCCESS
== http_post_data(URL
, postdata
, (size_t) postdata_len
, HASH_ORNULL(options
), HASH_ORNULL(info
), &data
, &data_len
)) {
848 RETURN_STRINGL(data
, data_len
, 0);
855 /* {{{ proto string http_post_array(string url, array data[, array options[, array &info]])
857 * Performs an HTTP POST request, posting www-form-urlencoded array data.
858 * Returns the HTTP response as string.
859 * See http_get() for a full list of available options.
861 PHP_FUNCTION(http_post_array
)
863 char *URL
, *data
= NULL
;
866 zval
*options
= NULL
, *info
= NULL
, *postdata
;
868 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sa|a/!z", &URL
, &URL_len
, &postdata
, &options
, &info
) != SUCCESS
) {
877 if (SUCCESS
== http_post_array(URL
, Z_ARRVAL_P(postdata
), HASH_ORNULL(options
), HASH_ORNULL(info
), &data
, &data_len
)) {
878 RETURN_STRINGL(data
, data_len
, 0);
889 /* {{{ proto bool http_auth_basic(string user, string pass[, string realm = "Restricted"])
894 * if (!http_auth_basic('mike', 's3c|r3t')) {
895 * die('<h1>Authorization failed!</h1>');
900 PHP_FUNCTION(http_auth_basic
)
902 char *realm
= NULL
, *user
, *pass
, *suser
, *spass
;
903 int r_len
, u_len
, p_len
;
905 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &user
, &u_len
, &pass
, &p_len
, &realm
, &r_len
) != SUCCESS
) {
910 realm
= "Restricted";
913 if (SUCCESS
!= http_auth_credentials(&suser
, &spass
)) {
914 http_auth_header("Basic", realm
);
918 if (strcasecmp(suser
, user
)) {
919 http_auth_header("Basic", realm
);
923 if (strcmp(spass
, pass
)) {
924 http_auth_header("Basic", realm
);
932 /* {{{ proto bool http_auth_basic_cb(mixed callback[, string realm = "Restricted"])
937 * function auth_cb($user, $pass)
940 * $query = 'SELECT pass FROM users WHERE user='. $db->quoteSmart($user);
941 * if (strlen($realpass = $db->getOne($query)) {
942 * return $pass === $realpass;
947 * if (!http_auth_basic_cb('auth_cb')) {
948 * die('<h1>Authorization failed</h1>');
953 PHP_FUNCTION(http_auth_basic_cb
)
956 char *realm
= NULL
, *user
, *pass
;
959 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z|s", &cb
, &realm
, &r_len
) != SUCCESS
) {
964 realm
= "Restricted";
967 if (SUCCESS
!= http_auth_credentials(&user
, &pass
)) {
968 http_auth_header("Basic", realm
);
972 zval
*zparams
[2] = {NULL
, NULL
}, retval
;
975 MAKE_STD_ZVAL(zparams
[0]);
976 MAKE_STD_ZVAL(zparams
[1]);
977 ZVAL_STRING(zparams
[0], user
, 0);
978 ZVAL_STRING(zparams
[1], pass
, 0);
980 if (SUCCESS
== call_user_function(EG(function_table
), NULL
, cb
,
981 &retval
, 2, zparams TSRMLS_CC
)) {
982 result
= Z_LVAL(retval
);
991 http_auth_header("Basic", realm
);
1000 /* {{{ php_http_init_globals(zend_http_globals *) */
1001 static void php_http_init_globals(zend_http_globals
*http_globals
)
1003 http_globals
->etag_started
= 0;
1004 http_globals
->ctype
= NULL
;
1005 http_globals
->etag
= NULL
;
1006 http_globals
->lmod
= 0;
1007 #ifdef HTTP_HAVE_CURL
1008 http_globals
->curlbuf
.body
.data
= NULL
;
1009 http_globals
->curlbuf
.body
.used
= 0;
1010 http_globals
->curlbuf
.body
.free
= 0;
1011 http_globals
->curlbuf
.hdrs
.data
= NULL
;
1012 http_globals
->curlbuf
.hdrs
.used
= 0;
1013 http_globals
->curlbuf
.hdrs
.free
= 0;
1018 /* {{{ PHP_MINIT_FUNCTION */
1019 PHP_MINIT_FUNCTION(http
)
1021 ZEND_INIT_MODULE_GLOBALS(http
, php_http_init_globals
, NULL
);
1022 #ifdef HTTP_HAVE_CURL
1023 REGISTER_LONG_CONSTANT("HTTP_AUTH_BASIC", CURLAUTH_BASIC
, CONST_CS
| CONST_PERSISTENT
);
1024 REGISTER_LONG_CONSTANT("HTTP_AUTH_DIGEST", CURLAUTH_DIGEST
, CONST_CS
| CONST_PERSISTENT
);
1025 REGISTER_LONG_CONSTANT("HTTP_AUTH_NTLM", CURLAUTH_NTLM
, CONST_CS
| CONST_PERSISTENT
);
1031 /* {{{ PHP_RSHUTDOWN_FUNCTION */
1032 PHP_RSHUTDOWN_FUNCTION(http
)
1034 if (HTTP_G(ctype
)) {
1035 efree(HTTP_G(ctype
));
1038 efree(HTTP_G(etag
));
1040 #ifdef HTTP_HAVE_CURL
1041 if (HTTP_G(curlbuf
).body
.data
) {
1042 efree(HTTP_G(curlbuf
).body
.data
);
1044 if (HTTP_G(curlbuf
).hdrs
.data
) {
1045 efree(HTTP_G(curlbuf
).hdrs
.data
);
1052 /* {{{ PHP_MINFO_FUNCTION */
1053 PHP_MINFO_FUNCTION(http
)
1055 php_info_print_table_start();
1056 php_info_print_table_header(2, "Extended HTTP support", "enabled");
1057 php_info_print_table_row(2, "Version:", PHP_EXT_HTTP_VERSION
);
1058 php_info_print_table_row(2, "cURL convenience functions:",
1059 #ifdef HTTP_HAVE_CURL
1065 php_info_print_table_end();
1074 * vim600: noet sw=4 ts=4 fdm=marker
1075 * vim<600: noet sw=4 ts=4