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
54 /* {{{ http_functions[] */
55 function_entry http_functions
[] = {
56 PHP_FE(http_date
, NULL
)
57 PHP_FE(http_absolute_uri
, NULL
)
58 PHP_FE(http_negotiate_language
, NULL
)
59 PHP_FE(http_negotiate_charset
, NULL
)
60 PHP_FE(http_redirect
, NULL
)
61 PHP_FE(http_send_status
, NULL
)
62 PHP_FE(http_send_last_modified
, NULL
)
63 PHP_FE(http_match_modified
, NULL
)
64 PHP_FE(http_match_etag
, NULL
)
65 PHP_FE(http_cache_last_modified
, NULL
)
66 PHP_FE(http_cache_etag
, NULL
)
67 PHP_FE(http_content_type
, NULL
)
68 PHP_FE(http_content_disposition
, NULL
)
69 PHP_FE(http_send_data
, NULL
)
70 PHP_FE(http_send_file
, NULL
)
71 PHP_FE(http_send_stream
, NULL
)
72 PHP_FE(http_chunked_decode
, NULL
)
73 PHP_FE(http_split_response
, NULL
)
74 PHP_FE(http_parse_header
, NULL
)
76 PHP_FE(http_get
, NULL
)
77 PHP_FE(http_head
, NULL
)
78 PHP_FE(http_post_data
, NULL
)
79 PHP_FE(http_post_array
, NULL
)
81 PHP_FE(http_auth_basic
, NULL
)
82 PHP_FE(http_auth_basic_cb
, NULL
)
84 PHP_FE(http_build_query
, NULL
)
90 /* {{{ http_module_entry */
91 zend_module_entry http_module_entry
= {
92 #if ZEND_MODULE_API_NO >= 20010901
93 STANDARD_MODULE_HEADER
,
102 #if ZEND_MODULE_API_NO >= 20010901
103 PHP_EXT_HTTP_VERSION
,
105 STANDARD_MODULE_PROPERTIES
109 #define RETURN_SUCCESS(v) RETURN_BOOL(SUCCESS == (v))
110 #define HASH_ORNULL(z) ((z) ? Z_ARRVAL_P(z) : NULL)
112 /* {{{ proto string http_date([int timestamp])
114 * This function returns a valid HTTP date regarding RFC 822/1123
115 * looking like: "Wed, 22 Dec 2004 11:34:47 GMT"
118 PHP_FUNCTION(http_date
)
122 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
127 t
= (long) time(NULL
);
130 RETURN_STRING(http_date(t
), 0);
134 /* {{{ proto string http_absolute_uri(string url[, string proto])
136 * This function returns an absolute URI constructed from url.
137 * If the url is already abolute but a different proto was supplied,
138 * only the proto part of the URI will be updated. If url has no
139 * path specified, the path of the current REQUEST_URI will be taken.
140 * The host will be taken either from the Host HTTP header of the client
141 * the SERVER_NAME or just localhost if prior are not available.
145 * url = "page.php" => http://www.example.com/current/path/page.php
146 * url = "/page.php" => http://www.example.com/page.php
147 * url = "/page.php", proto = "https" => https://www.example.com/page.php
151 PHP_FUNCTION(http_absolute_uri
)
153 char *url
= NULL
, *proto
= NULL
;
154 int url_len
= 0, proto_len
= 0;
156 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|s", &url
, &url_len
, &proto
, &proto_len
) != SUCCESS
) {
160 RETURN_STRING(http_absolute_uri(url
, proto
), 0);
164 /* {{{ proto string http_negotiate_language(array supported[, string default = 'en-US'])
166 * This function negotiates the clients preferred language based on its
167 * Accept-Language HTTP header. It returns the negotiated language or
168 * the default language if none match.
170 * The qualifier is recognized and languages without qualifier are rated highest.
172 * The supported parameter is expected to be an array having
173 * the supported languages as array values.
187 * include './langs/'. http_negotiate_language($langs) .'.php';
192 PHP_FUNCTION(http_negotiate_language
)
198 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|s", &supported
, &def
, &def_len
) != SUCCESS
) {
206 RETURN_STRING(http_negotiate_language(supported
, def
), 0);
210 /* {{{ proto string http_negotiate_charset(array supported[, string default = 'iso-8859-1'])
212 * This function negotiates the clients preferred charset based on its
213 * Accept-Charset HTTP header. It returns the negotiated charset or
214 * the default charset if none match.
216 * The qualifier is recognized and charset without qualifier are rated highest.
218 * The supported parameter is expected to be an array having
219 * the supported charsets as array values.
225 * 'iso-8859-1', // default
230 * $pref = http_negotiate_charset($charsets);
231 * if (!strcmp($pref, 'iso-8859-1')) {
232 * iconv_set_encoding('internal_encoding', 'iso-8859-1');
233 * iconv_set_encoding('output_encoding', $pref);
234 * ob_start('ob_iconv_handler');
239 PHP_FUNCTION(http_negotiate_charset
)
245 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|s", &supported
, &def
, &def_len
) != SUCCESS
) {
253 RETURN_STRING(http_negotiate_charset(supported
, def
), 0);
257 /* {{{ proto bool http_send_status(int status)
259 * Send HTTP status code.
262 PHP_FUNCTION(http_send_status
)
266 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &status
) != SUCCESS
) {
269 if (status
< 100 || status
> 510) {
270 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Invalid HTTP status code (100-510): %d", status
);
274 RETURN_SUCCESS(http_send_status(status
));
278 /* {{{ proto bool http_send_last_modified([int timestamp])
280 * This converts the given timestamp to a valid HTTP date and
281 * sends it as "Last-Modified" HTTP header. If timestamp is
282 * omitted, current time is sent.
285 PHP_FUNCTION(http_send_last_modified
)
289 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
294 t
= (long) time(NULL
);
297 RETURN_SUCCESS(http_send_last_modified(t
));
301 /* {{{ proto bool http_match_modified([int timestamp])
303 * Matches the given timestamp against the clients "If-Modified-Since" resp.
304 * "If-Unmodified-Since" HTTP headers.
307 PHP_FUNCTION(http_match_modified
)
311 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
315 // current time if not supplied (senseless though)
317 t
= (long) time(NULL
);
320 RETURN_BOOL(http_modified_match("HTTP_IF_MODIFIED_SINCE", t
) || http_modified_match("HTTP_IF_UNMODIFIED_SINCE", t
));
324 /* {{{ proto bool http_match_etag(string etag)
326 * This matches the given ETag against the clients
327 * "If-Match" resp. "If-None-Match" HTTP headers.
330 PHP_FUNCTION(http_match_etag
)
335 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &etag
, &etag_len
) != SUCCESS
) {
339 RETURN_BOOL(http_etag_match("HTTP_IF_NONE_MATCH", etag
) || http_etag_match("HTTP_IF_MATCH", etag
));
343 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
345 * If timestamp_or_exires is greater than 0, it is handled as timestamp
346 * and will be sent as date of last modification. If it is 0 or omitted,
347 * the current time will be sent as Last-Modified date. If it's negative,
348 * it is handled as expiration time in seconds, which means that if the
349 * requested last modification date is not between the calculated timespan,
350 * the Last-Modified header is updated and the actual body will be sent.
353 PHP_FUNCTION(http_cache_last_modified
)
355 long last_modified
= 0, send_modified
= 0, t
;
358 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &last_modified
) != SUCCESS
) {
362 t
= (long) time(NULL
);
365 if (!last_modified
) {
366 /* does the client have? (att: caching "forever") */
367 if (zlm
= http_get_server_var("HTTP_IF_MODIFIED_SINCE")) {
368 last_modified
= send_modified
= http_parse_date(Z_STRVAL_P(zlm
));
369 /* send current time */
373 /* negative value is supposed to be expiration time */
374 } else if (last_modified
< 0) {
377 /* send supplied time explicitly */
379 send_modified
= last_modified
;
382 http_send_header("Cache-Control: private, must-revalidate, max-age=0");
384 if (http_modified_match("HTTP_IF_MODIFIED_SINCE", last_modified
)) {
385 if (SUCCESS
== http_send_status(304)) {
388 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not send 304 Not Modified");
392 RETURN_SUCCESS(http_send_last_modified(send_modified
));
396 /* {{{ proto bool http_cache_etag([string etag])
398 * This function attempts to cache the HTTP body based on an ETag,
399 * either supplied or generated through calculation of the MD5
400 * checksum of the output (uses output buffering).
402 * If clients "If-None-Match" header matches the supplied/calculated
403 * ETag, the body is considered cached on the clients side and
404 * a "304 Not Modified" status code is issued.
407 PHP_FUNCTION(http_cache_etag
)
412 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &etag
, &etag_len
) != SUCCESS
) {
416 php_end_ob_buffers(0 TSRMLS_CC
);
417 http_send_header("Cache-Control: private, must-revalidate, max-age=0");
419 /* if no etag is given and we didn't already
420 * start ob_etaghandler -- start it
422 if (!HTTP_G(etag_started
) && !etag_len
) {
423 php_ob_set_internal_handler(_http_ob_etaghandler
, (uint
) 4096, "etag output handler", 0 TSRMLS_CC
);
424 HTTP_G(etag_started
) = 1;
425 RETURN_BOOL(php_start_ob_buffer_named("etag output handler", (uint
) 4096, 0 TSRMLS_CC
));
428 if (http_etag_match("HTTP_IF_NONE_MATCH", etag
)) {
429 if (SUCCESS
== http_send_status(304)) {
432 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not send 304 Not Modified");
437 RETURN_SUCCESS(http_send_etag(etag
, etag_len
));
441 /* {{{ proto void http_redirect([string url[, array params[, bool session,[ bool permanent]]]])
443 * Redirect to a given url.
444 * The supplied url will be expanded with http_absolute_uri(), the params array will
445 * be treated with http_build_query() and the session identification will be appended
446 * if session is true.
448 * Depending on permanent the redirection will be issued with a permanent
449 * ("301 Moved Permanently") or a temporary ("302 Found") redirection
452 * To be RFC compliant, "Redirecting to <a>URI</a>." will be displayed,
453 * if the client doesn't redirect immediatly.
455 PHP_FUNCTION(http_redirect
)
458 zend_bool session
= 0, permanent
= 0;
460 smart_str qstr
= {0};
461 char *url
, *URI
, LOC
[HTTP_URI_MAXLEN
+ 9], RED
[HTTP_URI_MAXLEN
* 2 + 34];
463 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sa!/bb", &url
, &url_len
, ¶ms
, &session
, &permanent
) != SUCCESS
) {
467 /* append session info */
468 if (session
&& (PS(session_status
) == php_session_active
)) {
470 MAKE_STD_ZVAL(params
);
473 if (add_assoc_string(params
, PS(session_name
), PS(id
), 1) != SUCCESS
) {
474 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not append session information");
478 /* treat params array with http_build_query() */
480 if (php_url_encode_hash_ex(Z_ARRVAL_P(params
), &qstr
, NULL
,0,NULL
,0,NULL
,0,NULL TSRMLS_CC
) != SUCCESS
) {
484 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not encode query parameters");
490 URI
= http_absolute_uri(url
, NULL
);
492 snprintf(LOC
, HTTP_URI_MAXLEN
+ strlen("Location: "), "Location: %s?%s", URI
, qstr
.c
);
493 sprintf(RED
, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI
, qstr
.c
, URI
, qstr
.c
);
496 snprintf(LOC
, HTTP_URI_MAXLEN
+ strlen("Location: "), "Location: %s", URI
);
497 sprintf(RED
, "Redirecting to <a href=\"%s\">%s</a>.\n", URI
, URI
);
501 if ((SUCCESS
== http_send_header(LOC
)) && (SUCCESS
== http_send_status((permanent
? 301 : 302)))) {
502 php_body_write(RED
, strlen(RED
) TSRMLS_CC
);
509 /* {{{ proto bool http_send_data(string data)
511 * Sends raw data with support for (multiple) range requests.
514 PHP_FUNCTION(http_send_data
)
518 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zdata
) != SUCCESS
) {
522 convert_to_string_ex(&zdata
);
523 http_send_header("Accept-Ranges: bytes");
524 RETURN_SUCCESS(http_send_data(zdata
));
528 /* {{{ proto bool http_send_file(string file)
530 * Sends a file with support for (multiple) range requests.
533 PHP_FUNCTION(http_send_file
)
537 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zfile
) != SUCCESS
) {
541 convert_to_string_ex(&zfile
);
542 http_send_header("Accept-Ranges: bytes");
543 RETURN_SUCCESS(http_send_file(zfile
));
547 /* {{{ proto bool http_send_stream(resource stream)
549 * Sends an already opened stream with support for (multiple) range requests.
552 PHP_FUNCTION(http_send_stream
)
557 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &zstream
) != SUCCESS
) {
561 php_stream_from_zval(file
, &zstream
);
562 http_send_header("Accept-Ranges: bytes");
563 RETURN_SUCCESS(http_send_stream(file
));
567 /* {{{ proto bool http_content_type([string content_type = 'application/x-octetstream'])
569 * Sets the content type.
572 PHP_FUNCTION(http_content_type
)
574 char *ct
, *content_type
;
577 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &ct
, &ct_len
) != SUCCESS
) {
582 RETURN_SUCCESS(http_send_header("Content-Type: application/x-octetstream"));
585 /* remember for multiple ranges */
587 efree(HTTP_G(ctype
));
589 HTTP_G(ctype
) = estrndup(ct
, ct_len
);
591 content_type
= (char *) emalloc(strlen("Content-Type: ") + ct_len
+ 1);
592 sprintf(content_type
, "Content-Type: %s", ct
);
594 RETVAL_BOOL(SUCCESS
== http_send_header(content_type
));
599 /* {{{ proto bool http_content_disposition(string filename[, bool inline = false])
601 * Set the Content Disposition. The Content-Disposition header is very useful
602 * if the data actually sent came from a file or something similar, that should
603 * be "saved" by the client/user (i.e. by browsers "Save as..." popup window).
606 PHP_FUNCTION(http_content_disposition
)
608 char *filename
, *header
;
610 zend_bool send_inline
= 0;
612 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &filename
, &f_len
, &send_inline
) != SUCCESS
) {
617 header
= (char *) emalloc(strlen("Content-Disposition: inline; filename=\"\"") + f_len
+ 1);
618 sprintf(header
, "Content-Disposition: inline; filename=\"%s\"", filename
);
620 header
= (char *) emalloc(strlen("Content-Disposition: attachment; filename=\"\"") + f_len
+ 1);
621 sprintf(header
, "Content-Disposition: attachment; filename=\"%s\"", filename
);
624 RETVAL_BOOL(SUCCESS
== http_send_header(header
));
629 /* {{{ proto string http_chunked_decode(string encoded)
631 * This function decodes a string that was HTTP-chunked encoded.
632 * Returns false on failure.
634 PHP_FUNCTION(http_chunked_decode
)
636 char *encoded
= NULL
, *decoded
= NULL
;
637 int encoded_len
= 0, decoded_len
= 0;
639 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &encoded
, &encoded_len
) != SUCCESS
) {
643 if (SUCCESS
== http_chunked_decode(encoded
, encoded_len
, &decoded
, &decoded_len
)) {
644 RETURN_STRINGL(decoded
, decoded_len
, 0);
651 /* {{{ proto array http_split_response(string http_response)
653 * This function splits an HTTP response into an array with headers and the
654 * content body. The returned array may look simliar to the following example:
659 * 'Status' => '200 Ok',
660 * 'Content-Type' => 'text/plain',
661 * 'Content-Language' => 'en-US'
663 * 1 => "Hello World!"
667 PHP_FUNCTION(http_split_response
)
669 zval
*zresponse
, *zbody
, *zheaders
;
671 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zresponse
) != SUCCESS
) {
675 convert_to_string_ex(&zresponse
);
677 MAKE_STD_ZVAL(zbody
);
678 MAKE_STD_ZVAL(zheaders
);
679 array_init(zheaders
);
681 if (SUCCESS
!= http_split_response(zresponse
, zheaders
, zbody
)) {
682 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not parse HTTP response");
686 array_init(return_value
);
687 add_index_zval(return_value
, 0, zheaders
);
688 add_index_zval(return_value
, 1, zbody
);
692 /* {{{ proto array http_parse_header(string header) */
693 PHP_FUNCTION(http_parse_header
)
698 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &header
, &header_len
)) {
702 array_init(return_value
);
704 if (rnrn
= strstr(header
, "\r\n\r\n")) {
705 header_len
= rnrn
- header
+ 2;
707 if (SUCCESS
!= http_parse_header(header
, header_len
, return_value
)) {
708 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not parse HTTP header");
709 zval_dtor(return_value
);
716 #ifdef HTTP_HAVE_CURL
718 /* {{{ proto string http_get(string url[, array options[, array &info]])
720 * Performs an HTTP GET request on the supplied url.
722 * The second parameter is expected to be an associative
723 * array where the following keys will be recognized:
725 * - redirect: int, whether and how many redirects to follow
726 * - unrestrictedauth: bool, whether to continue sending credentials on
727 * redirects to a different host
728 * - proxyhost: string, proxy host in "host[:port]" format
729 * - proxyport: int, use another proxy port as specified in proxyhost
730 * - proxyauth: string, proxy credentials in "user:pass" format
731 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
732 * - httpauth: string, http credentials in "user:pass" format
733 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
734 * - compress: bool, whether to allow gzip/deflate content encoding
736 * - port: int, use another port as specified in the url
737 * - referer: string, the referer to sends
738 * - useragent: string, the user agent to send
739 * (defaults to PECL::HTTP/version (PHP/version)))
740 * - headers: array, list of custom headers as associative array
741 * like array("header" => "value")
742 * - cookies: array, list of cookies as associative array
743 * like array("cookie" => "value")
744 * - cookiestore: string, path to a file where cookies are/will be stored
747 * The optional third parameter will be filled with some additional information
748 * in form af an associative array, if supplied (don't forget to initialize it
749 * with NULL or array()).
751 PHP_FUNCTION(http_get
)
753 char *URL
, *data
= NULL
;
756 zval
*options
= NULL
, *info
= NULL
;
758 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
767 if (SUCCESS
== http_get(URL
, HASH_ORNULL(options
), HASH_ORNULL(info
), &data
, &data_len
)) {
768 RETURN_STRINGL(data
, data_len
, 0);
775 /* {{{ proto string http_head(string url[, array options[, array &info]])
777 * Performs an HTTP HEAD request on the suppied url.
778 * Returns the HTTP response as string.
779 * See http_get() for a full list of available options.
781 PHP_FUNCTION(http_head
)
783 char *URL
, *data
= NULL
;
786 zval
*options
= NULL
, *info
= NULL
;
788 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
797 if (SUCCESS
== http_head(URL
, HASH_ORNULL(options
), HASH_ORNULL(info
), &data
, &data_len
)) {
798 RETURN_STRINGL(data
, data_len
, 0);
805 /* {{{ proto string http_post_data(string url, string data[, array options[, &info]])
807 * Performs an HTTP POST request, posting data.
808 * Returns the HTTP response as string.
809 * See http_get() for a full list of available options.
811 PHP_FUNCTION(http_post_data
)
813 char *URL
, *postdata
, *data
= NULL
;
815 int postdata_len
, URL_len
;
816 zval
*options
= NULL
, *info
= NULL
;
818 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &postdata
, &postdata_len
, &options
, &info
) != SUCCESS
) {
827 if (SUCCESS
== http_post_data(URL
, postdata
, (size_t) postdata_len
, HASH_ORNULL(options
), HASH_ORNULL(info
), &data
, &data_len
)) {
828 RETURN_STRINGL(data
, data_len
, 0);
835 /* {{{ proto string http_post_array(string url, array data[, array options[, array &info]])
837 * Performs an HTTP POST request, posting www-form-urlencoded array data.
838 * Returns the HTTP response as string.
839 * See http_get() for a full list of available options.
841 PHP_FUNCTION(http_post_array
)
843 char *URL
, *data
= NULL
;
846 zval
*options
= NULL
, *info
= NULL
, *postdata
;
848 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sa|a/!z", &URL
, &URL_len
, &postdata
, &options
, &info
) != SUCCESS
) {
857 if (SUCCESS
== http_post_array(URL
, Z_ARRVAL_P(postdata
), HASH_ORNULL(options
), HASH_ORNULL(info
), &data
, &data_len
)) {
858 RETURN_STRINGL(data
, data_len
, 0);
869 /* {{{ proto bool http_auth_basic(string user, string pass[, string realm = "Restricted"])
874 * if (!http_auth_basic('mike', 's3c|r3t')) {
875 * die('<h1>Authorization failed!</h1>');
880 PHP_FUNCTION(http_auth_basic
)
882 char *realm
= NULL
, *user
, *pass
, *suser
, *spass
;
883 int r_len
, u_len
, p_len
;
885 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &user
, &u_len
, &pass
, &p_len
, &realm
, &r_len
) != SUCCESS
) {
890 realm
= "Restricted";
893 if (SUCCESS
!= http_auth_credentials(&suser
, &spass
)) {
894 http_auth_header("Basic", realm
);
898 if (strcasecmp(suser
, user
)) {
899 http_auth_header("Basic", realm
);
903 if (strcmp(spass
, pass
)) {
904 http_auth_header("Basic", realm
);
912 /* {{{ proto bool http_auth_basic_cb(mixed callback[, string realm = "Restricted"])
917 * function auth_cb($user, $pass)
920 * $query = 'SELECT pass FROM users WHERE user='. $db->quoteSmart($user);
921 * if (strlen($realpass = $db->getOne($query)) {
922 * return $pass === $realpass;
927 * if (!http_auth_basic_cb('auth_cb')) {
928 * die('<h1>Authorization failed</h1>');
933 PHP_FUNCTION(http_auth_basic_cb
)
936 char *realm
= NULL
, *user
, *pass
;
939 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z|s", &cb
, &realm
, &r_len
) != SUCCESS
) {
944 realm
= "Restricted";
947 if (SUCCESS
!= http_auth_credentials(&user
, &pass
)) {
948 http_auth_header("Basic", realm
);
952 zval
*zparams
[2] = {NULL
, NULL
}, retval
;
955 MAKE_STD_ZVAL(zparams
[0]);
956 MAKE_STD_ZVAL(zparams
[1]);
957 ZVAL_STRING(zparams
[0], user
, 0);
958 ZVAL_STRING(zparams
[1], pass
, 0);
960 if (SUCCESS
== call_user_function(EG(function_table
), NULL
, cb
,
961 &retval
, 2, zparams TSRMLS_CC
)) {
962 result
= Z_LVAL(retval
);
971 http_auth_header("Basic", realm
);
980 /* {{{ php_http_init_globals(zend_http_globals *) */
981 static void php_http_init_globals(zend_http_globals
*http_globals
)
983 http_globals
->etag_started
= 0;
984 http_globals
->ctype
= NULL
;
985 http_globals
->etag
= NULL
;
986 http_globals
->lmod
= 0;
987 #ifdef HTTP_HAVE_CURL
988 http_globals
->curlbuf
.body
.data
= NULL
;
989 http_globals
->curlbuf
.body
.used
= 0;
990 http_globals
->curlbuf
.body
.free
= 0;
991 http_globals
->curlbuf
.hdrs
.data
= NULL
;
992 http_globals
->curlbuf
.hdrs
.used
= 0;
993 http_globals
->curlbuf
.hdrs
.free
= 0;
998 /* {{{ PHP_MINIT_FUNCTION */
999 PHP_MINIT_FUNCTION(http
)
1001 ZEND_INIT_MODULE_GLOBALS(http
, php_http_init_globals
, NULL
);
1002 #ifdef HTTP_HAVE_CURL
1003 REGISTER_LONG_CONSTANT("HTTP_AUTH_BASIC", CURLAUTH_BASIC
, CONST_CS
| CONST_PERSISTENT
);
1004 REGISTER_LONG_CONSTANT("HTTP_AUTH_DIGEST", CURLAUTH_DIGEST
, CONST_CS
| CONST_PERSISTENT
);
1005 REGISTER_LONG_CONSTANT("HTTP_AUTH_NTLM", CURLAUTH_NTLM
, CONST_CS
| CONST_PERSISTENT
);
1011 /* {{{ PHP_RSHUTDOWN_FUNCTION */
1012 PHP_RSHUTDOWN_FUNCTION(http
)
1014 if (HTTP_G(ctype
)) {
1015 efree(HTTP_G(ctype
));
1018 efree(HTTP_G(etag
));
1020 #ifdef HTTP_HAVE_CURL
1021 if (HTTP_G(curlbuf
).body
.data
) {
1022 efree(HTTP_G(curlbuf
).body
.data
);
1024 if (HTTP_G(curlbuf
).hdrs
.data
) {
1025 efree(HTTP_G(curlbuf
).hdrs
.data
);
1032 /* {{{ PHP_MINFO_FUNCTION */
1033 PHP_MINFO_FUNCTION(http
)
1035 php_info_print_table_start();
1036 php_info_print_table_header(2, "Extended HTTP support", "enabled");
1037 php_info_print_table_row(2, "Version:", PHP_EXT_HTTP_VERSION
);
1038 php_info_print_table_row(2, "cURL convenience functions:",
1039 #ifdef HTTP_HAVE_CURL
1045 php_info_print_table_end();
1054 * vim600: noet sw=4 ts=4 fdm=marker
1055 * vim<600: noet sw=4 ts=4