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 +----------------------------------------------------------------------+
23 #include "main/snprintf.h"
24 #include "ext/standard/info.h"
25 #include "ext/standard/datetime.h"
26 #include "ext/session/php_session.h"
27 #include "ext/standard/php_string.h"
28 #include "ext/standard/php_smart_str.h"
31 #include "php_http_api.h"
33 #if defined(HAVE_CURL) && HAVE_CURL
34 #include <curl/curl.h>
37 ZEND_DECLARE_MODULE_GLOBALS(http
)
39 #ifdef COMPILE_DL_HTTP
43 /* {{{ http_functions[] */
44 function_entry http_functions
[] = {
45 PHP_FE(http_date
, NULL
)
46 PHP_FE(http_absolute_uri
, NULL
)
47 PHP_FE(http_negotiate_language
, NULL
)
48 PHP_FE(http_negotiate_charset
, NULL
)
49 PHP_FE(http_redirect
, NULL
)
50 PHP_FE(http_send_status
, NULL
)
51 PHP_FE(http_send_last_modified
, NULL
)
52 PHP_FE(http_match_modified
, NULL
)
53 PHP_FE(http_match_etag
, NULL
)
54 PHP_FE(http_cache_last_modified
, NULL
)
55 PHP_FE(http_cache_etag
, NULL
)
56 PHP_FE(http_accept_ranges
, NULL
)
57 PHP_FE(http_content_type
, NULL
)
58 PHP_FE(http_content_disposition
, NULL
)
59 PHP_FE(http_send_data
, NULL
)
60 PHP_FE(http_send_file
, NULL
)
61 PHP_FE(http_send_stream
, NULL
)
62 PHP_FE(http_chunked_decode
, NULL
)
63 PHP_FE(http_split_response
, NULL
)
64 #if defined(HAVE_CURL) && HAVE_CURL
65 PHP_FE(http_get
, NULL
)
66 PHP_FE(http_head
, NULL
)
67 PHP_FE(http_post_data
, NULL
)
68 PHP_FE(http_post_array
, NULL
)
70 PHP_FE(http_auth_basic
, NULL
)
71 PHP_FE(http_auth_basic_cb
, NULL
)
76 /* {{{ http_module_entry */
77 zend_module_entry http_module_entry
= {
78 #if ZEND_MODULE_API_NO >= 20010901
79 STANDARD_MODULE_HEADER
,
88 #if ZEND_MODULE_API_NO >= 20010901
91 STANDARD_MODULE_PROPERTIES
95 #define RETURN_SUCCESS(v) RETURN_BOOL(SUCCESS == (v))
97 /* {{{ proto string http_date([int timestamp])
99 * This function returns a valid HTTP date regarding RFC 822/1123
100 * looking like: "Wed, 22 Dec 2004 11:34:47 GMT"
103 PHP_FUNCTION(http_date
)
107 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
112 t
= (long) time(NULL
);
115 RETURN_STRING(http_date(t
), 0);
119 /* {{{ proto string http_absolute_uri(string url[, string proto])
121 * This function returns an absolute URI constructed from url.
122 * If the url is already abolute but a different proto was supplied,
123 * only the proto part of the URI will be updated. If url has no
124 * path specified, the path of the current REQUEST_URI will be taken.
125 * The host will be taken either from the Host HTTP header of the client
126 * the SERVER_NAME or just localhost if prior are not available.
130 * url = "page.php" => http://www.example.com/current/path/page.php
131 * url = "/page.php" => http://www.example.com/page.php
132 * url = "/page.php", proto = "https" => https://www.example.com/page.php
136 PHP_FUNCTION(http_absolute_uri
)
138 char *url
= NULL
, *proto
= NULL
;
139 int url_len
= 0, proto_len
= 0;
141 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|s", &url
, &url_len
, &proto
, &proto_len
) != SUCCESS
) {
145 RETURN_STRING(http_absolute_uri(url
, proto
), 0);
149 /* {{{ proto string http_negotiate_language(array supported[, string default = 'en-US'])
151 * This function negotiates the clients preferred language based on its
152 * Accept-Language HTTP header. It returns the negotiated language or
153 * the default language if none match.
155 * The qualifier is recognized and languages without qualifier are rated highest.
157 * The supported parameter is expected to be an array having
158 * the supported languages as array values.
172 * include './langs/'. http_negotiate_language($langs) .'.php';
177 PHP_FUNCTION(http_negotiate_language
)
183 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|s", &supported
, &def
, &def_len
) != SUCCESS
) {
191 RETURN_STRING(http_negotiate_language(supported
, def
), 0);
195 /* {{{ proto string http_negotiate_charset(array supported[, string default = 'iso-8859-1'])
197 * This function negotiates the clients preferred charset based on its
198 * Accept-Charset HTTP header. It returns the negotiated charset or
199 * the default charset if none match.
201 * The qualifier is recognized and charset without qualifier are rated highest.
203 * The supported parameter is expected to be an array having
204 * the supported charsets as array values.
210 * 'iso-8859-1', // default
215 * $pref = http_negotiate_charset($charsets);
216 * if (!strcmp($pref, 'iso-8859-1')) {
217 * iconv_set_encoding('internal_encoding', 'iso-8859-1');
218 * iconv_set_encoding('output_encoding', $pref);
219 * ob_start('ob_iconv_handler');
224 PHP_FUNCTION(http_negotiate_charset
)
230 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|s", &supported
, &def
, &def_len
) != SUCCESS
) {
238 RETURN_STRING(http_negotiate_charset(supported
, def
), 0);
242 /* {{{ proto bool http_send_status(int status)
244 * Send HTTP status code.
245 * This function sends the desired HTTP status code along with its corresponding
246 * status message, e.g. calling http_send_status(304) will issue a
247 * "Status: 304 Not Modified" with CGI-SAPIs and "HTTP/1.1 304 Not Modified"
248 * when PHP is run as web server module.
251 PHP_FUNCTION(http_send_status
)
255 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &status
) != SUCCESS
) {
258 if (status
< 100 || status
> 510) {
259 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Invalid HTTP status code (100-510): %d", status
);
263 RETURN_SUCCESS(http_send_status(status
));
267 /* {{{ proto bool http_send_last_modified([int timestamp])
269 * This converts the given timestamp to a valid HTTP date and
270 * sends it as "Last-Modified" HTTP header. If timestamp is
271 * omitted, current time is taken.
274 PHP_FUNCTION(http_send_last_modified
)
278 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
283 t
= (long) time(NULL
);
286 RETURN_SUCCESS(http_send_last_modified(t
));
290 /* {{{ proto bool http_match_modified([int timestamp])
292 * Matches the given timestamp against the clients "If-Modified-Since" and
293 * "If-Unmodified-Since" HTTP headers.
296 PHP_FUNCTION(http_match_modified
)
300 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
304 // current time if not supplied (senseless though)
306 t
= (long) time(NULL
);
309 RETURN_BOOL(http_modified_match("HTTP_IF_MODIFIED_SINCE", t
) || http_modified_match("HTTP_IF_UNMODIFIED_SINCE", t
));
313 /* {{{ proto bool http_match_etag(string etag)
315 * This matches the given ETag against the clients
316 * "If-Match" and "If-None-Match" HTTP headers.
319 PHP_FUNCTION(http_match_etag
)
324 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &etag
, &etag_len
) != SUCCESS
) {
328 RETURN_BOOL(http_etag_match("HTTP_IF_NONE_MATCH", etag
) || http_etag_match("HTTP_IF_MATCH", etag
));
332 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
334 * If timestamp_or_exires is greater than 0, it is handled as timestamp
335 * and will be sent as date of last modification. If it is 0 or omitted,
336 * the current time will be sent as Last-Modified date. If it's negative,
337 * it is handled as expiration tim in seconds, which means that if the
338 * requested last modification date is not between the calculated timespan,
339 * the Last-Modified header is updated and the actual body will be sent.
342 PHP_FUNCTION(http_cache_last_modified
)
344 long last_modified
= 0, send_modified
= 0, t
;
347 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &last_modified
) != SUCCESS
) {
351 t
= (long) time(NULL
);
354 if (!last_modified
) {
355 /* does the client have? */
356 if (zlm
= http_get_server_var("HTTP_IF_MODIFIED_SINCE")) {
357 last_modified
= send_modified
= http_parse_date(Z_STRVAL_P(zlm
));
358 /* use current time */
360 last_modified
= send_modified
= t
;
362 /* negative value is supposed to be expiration time */
363 } else if (last_modified
< 0) {
366 /* send supplied time explicitly */
368 send_modified
= last_modified
;
371 http_send_header("Cache-Control: private, must-revalidate, max-age=0");
373 if (http_modified_match("HTTP_IF_MODIFIED_SINCE", last_modified
)) {
374 if (SUCCESS
== http_send_status(304)) {
377 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not send 304 Not Modified");
381 RETURN_SUCCESS(http_send_last_modified(send_modified
));
385 /* {{{ proto bool http_cache_etag([string etag])
387 * This function attempts to cache the HTTP body based on an ETag,
388 * either supplied or generated through calculation of the MD5
389 * checksum of the output (uses output buffering).
391 * If clients "If-None-Match" header matches the supplied/calculated
392 * ETag, the body is considered cached on the clients side and
393 * a "304 Not Modified" status code is issued.
396 PHP_FUNCTION(http_cache_etag
)
401 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &etag
, &etag_len
) != SUCCESS
) {
405 /* send remaining data to nirvana */
406 http_send_header("Connection: close");
407 http_send_header("Cache-Control: private, must-revalidate, max-age=0");
409 /* if no etag is given and we didn't already
410 * start ob_etaghandler -- start it
412 if (!HTTP_G(etag_started
) && !etag_len
) {
413 php_ob_set_internal_handler(_http_ob_etaghandler
, (uint
) 4096, "etag output handler", 0 TSRMLS_CC
);
414 HTTP_G(etag_started
) = 1;
415 RETURN_BOOL(php_start_ob_buffer_named("etag output handler", (uint
) 4096, 0 TSRMLS_CC
));
418 if (http_etag_match("HTTP_IF_NONE_MATCH", etag
)) {
419 if (SUCCESS
== http_send_status(304)) {
422 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not send 304 Not Modified");
427 RETURN_SUCCESS(http_send_etag(etag
, etag_len
));
431 /* {{{ proto void http_redirect([string url[, array params[, bool session,[ bool permanent]]]])
433 * Redirect to a given url.
434 * The supplied url will be expanded with http_absolute_uri(), the params array will
435 * be treated with http_build_query() and the session identification will be appended
436 * if session is true.
438 * Depending on permanent the redirection will be issued with a permanent
439 * ("301 Moved Permanently") or a temporary ("302 Found") redirection
443 PHP_FUNCTION(http_redirect
)
446 zend_bool session
= 0, permanent
= 0;
448 smart_str qstr
= {0};
449 char *url
, *URI
, LOC
[HTTP_URI_MAXLEN
+ 9];
451 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sa!bb", &url
, &url_len
, ¶ms
, &session
, &permanent
) != SUCCESS
) {
455 /* append session info */
456 if (session
&& (PS(session_status
) == php_session_active
)) {
458 MAKE_STD_ZVAL(params
);
461 if (add_assoc_string(params
, PS(session_name
), PS(id
), 1) != SUCCESS
) {
462 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not append session information");
466 /* treat params array with http_build_query() */
468 if (php_url_encode_hash_ex(Z_ARRVAL_P(params
), &qstr
, NULL
,0,NULL
,0,NULL
,0,NULL TSRMLS_CC
) != SUCCESS
) {
472 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not encode query parameters");
478 URI
= http_absolute_uri(url
, NULL
);
480 snprintf(LOC
, HTTP_URI_MAXLEN
+ strlen("Location: "), "Location: %s?%s", URI
, qstr
.c
);
483 snprintf(LOC
, HTTP_URI_MAXLEN
+ strlen("Location: "), "Location: %s", URI
);
487 RETVAL_BOOL((SUCCESS
== http_send_header(LOC
)) && (SUCCESS
== http_send_status((permanent
? 301 : 302))));
491 /* {{{ proto bool http_send_data(string data)
493 * Sends raw data with support for (multiple) range requests.
496 PHP_FUNCTION(http_send_data
)
500 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zdata
) != SUCCESS
) {
504 convert_to_string_ex(&zdata
);
506 RETURN_SUCCESS(http_send_data(zdata
));
510 /* {{{ proto bool http_send_file(string file)
512 * Sends a file with support for (multiple) range requests.
515 PHP_FUNCTION(http_send_file
)
519 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zfile
) != SUCCESS
) {
523 convert_to_string_ex(&zfile
);
525 RETURN_SUCCESS(http_send_file(zfile
));
529 /* {{{ proto bool http_send_stream(resource stream)
531 * Sends an already opened stream with support for (multiple) range requests.
534 PHP_FUNCTION(http_send_stream
)
539 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &zstream
) != SUCCESS
) {
543 php_stream_from_zval(file
, &zstream
);
545 RETURN_SUCCESS(http_send_stream(file
));
549 /* {{{ proto bool http_accept_ranges(void)
551 * Issues a "Accept-Ranges: bytes" header.
556 * http_content_type('application/x-zip');
557 * http_content_disposition('latest.zip');
558 * http_accept_ranges();
560 * http_send_file('../some/download.zip');
565 PHP_FUNCTION(http_accept_ranges
)
567 if (ZEND_NUM_ARGS()) {
570 RETURN_SUCCESS(http_send_header("Accept-Ranges: bytes"));
574 /* {{{ proto bool http_content_type([string content_type = 'application/x-octetstream'])
576 * Sets the content type.
579 PHP_FUNCTION(http_content_type
)
581 char *ct
, *content_type
;
584 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &ct
, &ct_len
) != SUCCESS
) {
589 RETURN_SUCCESS(http_send_header("Content-Type: application/x-octetstream"));
592 /* remember for multiple ranges */
594 efree(HTTP_G(ctype
));
596 HTTP_G(ctype
) = estrndup(ct
, ct_len
);
598 content_type
= (char *) emalloc(strlen("Content-Type: ") + ct_len
+ 1);
599 sprintf(content_type
, "Content-Type: %s", ct
);
601 RETVAL_BOOL(SUCCESS
== http_send_header(content_type
));
606 /* {{{ proto bool http_content_disposition(string filename[, bool inline = false])
608 * Set the Content Disposition. The Content-Disposition header is very useful
609 * if the data actually sent came from a file or something similar, that should
610 * be "saved" by the client/user (i.e. by browsers "Save as..." popup window).
613 PHP_FUNCTION(http_content_disposition
)
615 char *filename
, *header
;
617 zend_bool send_inline
= 0;
619 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &filename
, &f_len
, &send_inline
) != SUCCESS
) {
624 header
= (char *) emalloc(strlen("Content-Disposition: inline; filename=\"\"") + f_len
+ 1);
625 sprintf(header
, "Content-Disposition: inline; filename=\"%s\"", filename
);
627 header
= (char *) emalloc(strlen("Content-Disposition: attachment; filename=\"\"") + f_len
+ 1);
628 sprintf(header
, "Content-Disposition: attachment; filename=\"%s\"", filename
);
631 RETVAL_BOOL(SUCCESS
== http_send_header(header
));
636 /* {{{ proto string http_chunked_decode(string encoded)
638 * This function decodes a string that was HTTP-chunked encoded.
639 * Returns false on failure.
641 PHP_FUNCTION(http_chunked_decode
)
643 char *encoded
= NULL
, *decoded
= NULL
;
644 int encoded_len
= 0, decoded_len
= 0;
646 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &encoded
, &encoded_len
) != SUCCESS
) {
650 if (SUCCESS
== http_chunked_decode(encoded
, encoded_len
, &decoded
, &decoded_len
)) {
651 RETURN_STRINGL(decoded
, decoded_len
, 0);
658 /* {{{ proto array http_split_response(string http_response)
660 * This function splits an HTTP response into an array with headers and the
661 * content body. The returned array may look simliar to the following example:
666 * 'Status' => '200 Ok',
667 * 'Content-Type' => 'text/plain',
668 * 'Content-Language' => 'en-US'
670 * 1 => "Hello World!"
674 PHP_FUNCTION(http_split_response
)
676 zval
*zresponse
, *zbody
, *zheaders
;
678 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zresponse
) != SUCCESS
) {
682 convert_to_string_ex(&zresponse
);
684 MAKE_STD_ZVAL(zbody
);
685 MAKE_STD_ZVAL(zheaders
);
686 array_init(zheaders
);
688 http_split_response(zresponse
, zheaders
, zbody
);
690 array_init(return_value
);
691 add_index_zval(return_value
, 0, zheaders
);
692 add_index_zval(return_value
, 1, zbody
);
697 #if defined(HAVE_CURL) && HAVE_CURL
699 /* {{{ proto string http_get(string url[, array options])
701 * Performs an HTTP GET request on the supplied url.
703 * The second parameter is expected to be an associative
704 * array where the following keys will be recognized:
706 * - redirect: int, whether and how many redirects to follow
707 * - unrestrictedauth: bool, whether to continue sending credentials on
708 * redirects to a different host
709 * - proxyhost: string, proxy host in "host[:port]" format
710 * - proxyport: int, use another proxy port as specified in proxyhost
711 * - proxyauth: string, proxy credentials in "user:pass" format
712 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
713 * - httpauth: string, http credentials in "user:pass" format
714 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
715 * - compress: bool, whether to allow gzip/deflate content encoding
716 * - port: int, use another port as specified in the url
717 * - referer: string, the referer to sends
718 * - useragent: string, the user agent to send
719 * - headers: array, list of custom headers in "header: value" format
720 * - cookies: array, list of cookies in "name=value" format
721 * - cookiestore: string, path to a file where cookies are/will be stored
724 PHP_FUNCTION(http_get
)
726 char *URL
, *data
= NULL
;
729 zval
*options
= NULL
;
731 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a", &URL
, &URL_len
, &options
) != SUCCESS
) {
735 if (SUCCESS
== http_get(URL
, options
? Z_ARRVAL_P(options
) : NULL
, &data
, &data_len
)) {
736 RETURN_STRINGL(data
, data_len
, 0);
743 /* {{{ proto string http_head(string url[, array options])
745 * Performs an HTTP HEAD request on the suppied url.
746 * Returns the HTTP response as string.
747 * See http_get() for a full list of available options.
749 PHP_FUNCTION(http_head
)
751 char *URL
, *data
= NULL
;
754 zval
*options
= NULL
;
756 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a", &URL
, &URL_len
, &options
) != SUCCESS
) {
760 if (SUCCESS
== http_head(URL
, options
? Z_ARRVAL_P(options
) : NULL
, &data
, &data_len
)) {
761 RETURN_STRINGL(data
, data_len
, 0);
768 /* {{{ proto string http_post_data(string url, string data[, array options])
770 * Performs an HTTP POST request, posting data.
771 * Returns the HTTP response as string.
772 * See http_get() for a full list of available options.
774 PHP_FUNCTION(http_post_data
)
776 char *URL
, *postdata
, *data
= NULL
;
778 int postdata_len
, URL_len
;
779 zval
*options
= NULL
;
781 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a", &URL
, &URL_len
, &postdata
, &postdata_len
, &options
) != SUCCESS
) {
785 if (SUCCESS
== http_post_data(URL
, postdata
, (size_t) postdata_len
, options
? Z_ARRVAL_P(options
) : NULL
, &data
, &data_len
)) {
786 RETURN_STRINGL(data
, data_len
, 0);
793 /* {{{ proto string http_post_data(string url, array data[, array options])
795 * Performs an HTTP POST request, posting www-form-urlencoded array data.
796 * Returns the HTTP response as string.
797 * See http_get() for a full list of available options.
799 PHP_FUNCTION(http_post_array
)
801 char *URL
, *data
= NULL
;
804 zval
*options
= NULL
, *postdata
;
806 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sa|a", &URL
, &URL_len
, &postdata
, &options
) != SUCCESS
) {
810 if (SUCCESS
== http_post_array(URL
, Z_ARRVAL_P(postdata
), options
? Z_ARRVAL_P(options
) : NULL
, &data
, &data_len
)) {
811 RETURN_STRINGL(data
, data_len
, 0);
822 /* {{{ proto bool http_auth_basic(string user, string pass[, string realm = "Restricted"])
827 * if (!http_auth_basic('mike', 's3c|r3t')) {
828 * die('<h1>Authorization failed!</h1>');
833 PHP_FUNCTION(http_auth_basic
)
835 char *realm
= NULL
, *user
, *pass
, *suser
, *spass
;
836 int r_len
, u_len
, p_len
;
838 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &user
, &u_len
, &pass
, &p_len
, &realm
, &r_len
) != SUCCESS
) {
843 realm
= "Restricted";
846 if (SUCCESS
!= http_auth_credentials(&suser
, &spass
)) {
847 http_auth_header("Basic", realm
);
851 if (strcasecmp(suser
, user
)) {
852 http_auth_header("Basic", realm
);
856 if (strcmp(spass
, pass
)) {
857 http_auth_header("Basic", realm
);
865 /* {{{ proto bool http_auth_basic_cb(mixed callback[, string realm = "Restricted"])
870 * function auth_cb($user, $pass)
873 * $query = 'SELECT pass FROM users WHERE user='. $db->quoteSmart($user);
874 * if (strlen($realpass = $db->getOne($query)) {
875 * return $pass === $realpass;
880 * if (!http_auth_basic_cb('auth_cb')) {
881 * die('<h1>Authorization failed</h1>');
886 PHP_FUNCTION(http_auth_basic_cb
)
889 char *realm
= NULL
, *user
, *pass
;
892 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z|s", &cb
, &realm
, &r_len
) != SUCCESS
) {
897 realm
= "Restricted";
900 if (SUCCESS
!= http_auth_credentials(&user
, &pass
)) {
901 http_auth_header("Basic", realm
);
905 zval
*zparams
[2] = {NULL
, NULL
}, retval
;
908 MAKE_STD_ZVAL(zparams
[0]);
909 MAKE_STD_ZVAL(zparams
[1]);
910 ZVAL_STRING(zparams
[0], user
, 0);
911 ZVAL_STRING(zparams
[1], pass
, 0);
913 if (SUCCESS
== call_user_function(EG(function_table
), NULL
, cb
,
914 &retval
, 2, zparams TSRMLS_CC
)) {
915 result
= Z_LVAL(retval
);
924 http_auth_header("Basic", realm
);
933 /* {{{ php_http_init_globals(zend_http_globals *) */
934 static void php_http_init_globals(zend_http_globals
*http_globals
)
936 http_globals
->etag_started
= 0;
937 http_globals
->ctype
= NULL
;
938 #if defined(HAVE_CURL) && HAVE_CURL
939 http_globals
->curlbuf
.body
.data
= NULL
;
940 http_globals
->curlbuf
.body
.used
= 0;
941 http_globals
->curlbuf
.body
.free
= 0;
942 http_globals
->curlbuf
.hdrs
.data
= NULL
;
943 http_globals
->curlbuf
.hdrs
.used
= 0;
944 http_globals
->curlbuf
.hdrs
.free
= 0;
949 /* {{{ PHP_MINIT_FUNCTION */
950 PHP_MINIT_FUNCTION(http
)
952 ZEND_INIT_MODULE_GLOBALS(http
, php_http_init_globals
, NULL
);
953 #if defined(HAVE_CURL) && HAVE_CURL
954 REGISTER_LONG_CONSTANT("HTTP_AUTH_BASIC", CURLAUTH_BASIC
, CONST_CS
| CONST_PERSISTENT
);
955 REGISTER_LONG_CONSTANT("HTTP_AUTH_DIGEST", CURLAUTH_DIGEST
, CONST_CS
| CONST_PERSISTENT
);
956 REGISTER_LONG_CONSTANT("HTTP_AUTH_NTLM", CURLAUTH_NTLM
, CONST_CS
| CONST_PERSISTENT
);
962 /* {{{ PHP_RSHUTDOWN_FUNCTION */
963 PHP_RSHUTDOWN_FUNCTION(http
)
966 efree(HTTP_G(ctype
));
968 #if defined(HAVE_CURL) && HAVE_CURL
969 if (HTTP_G(curlbuf
).body
.data
) {
970 efree(HTTP_G(curlbuf
).body
.data
);
972 if (HTTP_G(curlbuf
).hdrs
.data
) {
973 efree(HTTP_G(curlbuf
).hdrs
.data
);
980 /* {{{ PHP_MINFO_FUNCTION */
981 PHP_MINFO_FUNCTION(http
)
983 php_info_print_table_start();
984 php_info_print_table_header(2, "Extended HTTP support", "enabled");
985 php_info_print_table_row(2, "Version:", PHP_EXT_HTTP_VERSION
);
986 php_info_print_table_row(2, "cURL convenience functions:",
987 #if defined(HAVE_CURL) && HAVE_CURL
993 php_info_print_table_end();
1002 * vim600: noet sw=4 ts=4 fdm=marker
1003 * vim<600: noet sw=4 ts=4