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 +----------------------------------------------------------------------+
24 #include "ext/standard/info.h"
25 #include "ext/session/php_session.h"
26 #include "ext/standard/php_string.h"
27 #include "ext/standard/php_smart_str.h"
30 #include "php_http_api.h"
32 #if defined(HAVE_CURL) && HAVE_CURL
34 # include <winsock2.h>
35 # include <sys/types.h>
37 #include <curl/curl.h>
40 ZEND_DECLARE_MODULE_GLOBALS(http
)
42 #ifdef COMPILE_DL_HTTP
46 /* {{{ http_functions[] */
47 function_entry http_functions
[] = {
48 PHP_FE(http_date
, NULL
)
49 PHP_FE(http_absolute_uri
, NULL
)
50 PHP_FE(http_negotiate_language
, NULL
)
51 PHP_FE(http_negotiate_charset
, NULL
)
52 PHP_FE(http_redirect
, NULL
)
53 PHP_FE(http_send_status
, NULL
)
54 PHP_FE(http_send_last_modified
, NULL
)
55 PHP_FE(http_match_modified
, NULL
)
56 PHP_FE(http_match_etag
, NULL
)
57 PHP_FE(http_cache_last_modified
, NULL
)
58 PHP_FE(http_cache_etag
, NULL
)
59 PHP_FE(http_content_type
, NULL
)
60 PHP_FE(http_content_disposition
, NULL
)
61 PHP_FE(http_send_data
, NULL
)
62 PHP_FE(http_send_file
, NULL
)
63 PHP_FE(http_send_stream
, NULL
)
64 PHP_FE(http_chunked_decode
, NULL
)
65 PHP_FE(http_split_response
, NULL
)
66 #if defined(HAVE_CURL) && HAVE_CURL
67 PHP_FE(http_get
, NULL
)
68 PHP_FE(http_head
, NULL
)
69 PHP_FE(http_post_data
, NULL
)
70 PHP_FE(http_post_array
, NULL
)
72 PHP_FE(http_auth_basic
, NULL
)
73 PHP_FE(http_auth_basic_cb
, NULL
)
78 /* {{{ http_module_entry */
79 zend_module_entry http_module_entry
= {
80 #if ZEND_MODULE_API_NO >= 20010901
81 STANDARD_MODULE_HEADER
,
90 #if ZEND_MODULE_API_NO >= 20010901
93 STANDARD_MODULE_PROPERTIES
97 #define RETURN_SUCCESS(v) RETURN_BOOL(SUCCESS == (v))
98 #define HASH_ORNULL(z) ((z) ? Z_ARRVAL_P(z) : NULL)
100 /* {{{ proto string http_date([int timestamp])
102 * This function returns a valid HTTP date regarding RFC 822/1123
103 * looking like: "Wed, 22 Dec 2004 11:34:47 GMT"
106 PHP_FUNCTION(http_date
)
110 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
115 t
= (long) time(NULL
);
118 RETURN_STRING(http_date(t
), 0);
122 /* {{{ proto string http_absolute_uri(string url[, string proto])
124 * This function returns an absolute URI constructed from url.
125 * If the url is already abolute but a different proto was supplied,
126 * only the proto part of the URI will be updated. If url has no
127 * path specified, the path of the current REQUEST_URI will be taken.
128 * The host will be taken either from the Host HTTP header of the client
129 * the SERVER_NAME or just localhost if prior are not available.
133 * url = "page.php" => http://www.example.com/current/path/page.php
134 * url = "/page.php" => http://www.example.com/page.php
135 * url = "/page.php", proto = "https" => https://www.example.com/page.php
139 PHP_FUNCTION(http_absolute_uri
)
141 char *url
= NULL
, *proto
= NULL
;
142 int url_len
= 0, proto_len
= 0;
144 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|s", &url
, &url_len
, &proto
, &proto_len
) != SUCCESS
) {
148 RETURN_STRING(http_absolute_uri(url
, proto
), 0);
152 /* {{{ proto string http_negotiate_language(array supported[, string default = 'en-US'])
154 * This function negotiates the clients preferred language based on its
155 * Accept-Language HTTP header. It returns the negotiated language or
156 * the default language if none match.
158 * The qualifier is recognized and languages without qualifier are rated highest.
160 * The supported parameter is expected to be an array having
161 * the supported languages as array values.
175 * include './langs/'. http_negotiate_language($langs) .'.php';
180 PHP_FUNCTION(http_negotiate_language
)
186 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|s", &supported
, &def
, &def_len
) != SUCCESS
) {
194 RETURN_STRING(http_negotiate_language(supported
, def
), 0);
198 /* {{{ proto string http_negotiate_charset(array supported[, string default = 'iso-8859-1'])
200 * This function negotiates the clients preferred charset based on its
201 * Accept-Charset HTTP header. It returns the negotiated charset or
202 * the default charset if none match.
204 * The qualifier is recognized and charset without qualifier are rated highest.
206 * The supported parameter is expected to be an array having
207 * the supported charsets as array values.
213 * 'iso-8859-1', // default
218 * $pref = http_negotiate_charset($charsets);
219 * if (!strcmp($pref, 'iso-8859-1')) {
220 * iconv_set_encoding('internal_encoding', 'iso-8859-1');
221 * iconv_set_encoding('output_encoding', $pref);
222 * ob_start('ob_iconv_handler');
227 PHP_FUNCTION(http_negotiate_charset
)
233 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|s", &supported
, &def
, &def_len
) != SUCCESS
) {
241 RETURN_STRING(http_negotiate_charset(supported
, def
), 0);
245 /* {{{ proto bool http_send_status(int status)
247 * Send HTTP status code.
250 PHP_FUNCTION(http_send_status
)
254 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &status
) != SUCCESS
) {
257 if (status
< 100 || status
> 510) {
258 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Invalid HTTP status code (100-510): %d", status
);
262 RETURN_SUCCESS(http_send_status(status
));
266 /* {{{ proto bool http_send_last_modified([int timestamp])
268 * This converts the given timestamp to a valid HTTP date and
269 * sends it as "Last-Modified" HTTP header. If timestamp is
270 * omitted, current time is sent.
273 PHP_FUNCTION(http_send_last_modified
)
277 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
282 t
= (long) time(NULL
);
285 RETURN_SUCCESS(http_send_last_modified(t
));
289 /* {{{ proto bool http_match_modified([int timestamp])
291 * Matches the given timestamp against the clients "If-Modified-Since" resp.
292 * "If-Unmodified-Since" HTTP headers.
295 PHP_FUNCTION(http_match_modified
)
299 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
303 // current time if not supplied (senseless though)
305 t
= (long) time(NULL
);
308 RETURN_BOOL(http_modified_match("HTTP_IF_MODIFIED_SINCE", t
) || http_modified_match("HTTP_IF_UNMODIFIED_SINCE", t
));
312 /* {{{ proto bool http_match_etag(string etag)
314 * This matches the given ETag against the clients
315 * "If-Match" resp. "If-None-Match" HTTP headers.
318 PHP_FUNCTION(http_match_etag
)
323 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &etag
, &etag_len
) != SUCCESS
) {
327 RETURN_BOOL(http_etag_match("HTTP_IF_NONE_MATCH", etag
) || http_etag_match("HTTP_IF_MATCH", etag
));
331 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
333 * If timestamp_or_exires is greater than 0, it is handled as timestamp
334 * and will be sent as date of last modification. If it is 0 or omitted,
335 * the current time will be sent as Last-Modified date. If it's negative,
336 * it is handled as expiration time in seconds, which means that if the
337 * requested last modification date is not between the calculated timespan,
338 * the Last-Modified header is updated and the actual body will be sent.
341 PHP_FUNCTION(http_cache_last_modified
)
343 long last_modified
= 0, send_modified
= 0, t
;
346 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &last_modified
) != SUCCESS
) {
350 t
= (long) time(NULL
);
353 if (!last_modified
) {
354 /* does the client have? (att: caching "forever") */
355 if (zlm
= http_get_server_var("HTTP_IF_MODIFIED_SINCE")) {
356 last_modified
= send_modified
= http_parse_date(Z_STRVAL_P(zlm
));
357 /* send current time */
361 /* negative value is supposed to be expiration time */
362 } else if (last_modified
< 0) {
365 /* send supplied time explicitly */
367 send_modified
= last_modified
;
370 http_send_header("Cache-Control: private, must-revalidate, max-age=0");
372 if (http_modified_match("HTTP_IF_MODIFIED_SINCE", last_modified
)) {
373 if (SUCCESS
== http_send_status(304)) {
376 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not send 304 Not Modified");
380 RETURN_SUCCESS(http_send_last_modified(send_modified
));
384 /* {{{ proto bool http_cache_etag([string etag])
386 * This function attempts to cache the HTTP body based on an ETag,
387 * either supplied or generated through calculation of the MD5
388 * checksum of the output (uses output buffering).
390 * If clients "If-None-Match" header matches the supplied/calculated
391 * ETag, the body is considered cached on the clients side and
392 * a "304 Not Modified" status code is issued.
395 PHP_FUNCTION(http_cache_etag
)
400 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &etag
, &etag_len
) != SUCCESS
) {
404 php_end_ob_buffers(0 TSRMLS_CC
);
405 http_send_header("Cache-Control: private, must-revalidate, max-age=0");
407 /* if no etag is given and we didn't already
408 * start ob_etaghandler -- start it
410 if (!HTTP_G(etag_started
) && !etag_len
) {
411 php_ob_set_internal_handler(_http_ob_etaghandler
, (uint
) 4096, "etag output handler", 0 TSRMLS_CC
);
412 HTTP_G(etag_started
) = 1;
413 RETURN_BOOL(php_start_ob_buffer_named("etag output handler", (uint
) 4096, 0 TSRMLS_CC
));
416 if (http_etag_match("HTTP_IF_NONE_MATCH", etag
)) {
417 if (SUCCESS
== http_send_status(304)) {
420 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not send 304 Not Modified");
425 RETURN_SUCCESS(http_send_etag(etag
, etag_len
));
429 /* {{{ proto void http_redirect([string url[, array params[, bool session,[ bool permanent]]]])
431 * Redirect to a given url.
432 * The supplied url will be expanded with http_absolute_uri(), the params array will
433 * be treated with http_build_query() and the session identification will be appended
434 * if session is true.
436 * Depending on permanent the redirection will be issued with a permanent
437 * ("301 Moved Permanently") or a temporary ("302 Found") redirection
440 * To be RFC compliant, "Redirecting to <a>URI</a>." will be displayed,
441 * if the client doesn't redirect immediatly.
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], RED
[HTTP_URI_MAXLEN
* 2 + 34];
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
);
481 sprintf(RED
, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI
, qstr
.c
, URI
, qstr
.c
);
484 snprintf(LOC
, HTTP_URI_MAXLEN
+ strlen("Location: "), "Location: %s", URI
);
485 sprintf(RED
, "Redirecting to <a href=\"%s\">%s</a>.\n", URI
, URI
);
489 if ((SUCCESS
== http_send_header(LOC
)) && (SUCCESS
== http_send_status((permanent
? 301 : 302)))) {
490 php_body_write(RED
, strlen(RED
) TSRMLS_CC
);
497 /* {{{ proto bool http_send_data(string data)
499 * Sends raw data with support for (multiple) range requests.
502 PHP_FUNCTION(http_send_data
)
506 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zdata
) != SUCCESS
) {
510 convert_to_string_ex(&zdata
);
511 http_send_header("Accept-Ranges: bytes");
512 RETURN_SUCCESS(http_send_data(zdata
));
516 /* {{{ proto bool http_send_file(string file)
518 * Sends a file with support for (multiple) range requests.
521 PHP_FUNCTION(http_send_file
)
525 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zfile
) != SUCCESS
) {
529 convert_to_string_ex(&zfile
);
530 http_send_header("Accept-Ranges: bytes");
531 RETURN_SUCCESS(http_send_file(zfile
));
535 /* {{{ proto bool http_send_stream(resource stream)
537 * Sends an already opened stream with support for (multiple) range requests.
540 PHP_FUNCTION(http_send_stream
)
545 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &zstream
) != SUCCESS
) {
549 php_stream_from_zval(file
, &zstream
);
550 http_send_header("Accept-Ranges: bytes");
551 RETURN_SUCCESS(http_send_stream(file
));
555 /* {{{ proto bool http_content_type([string content_type = 'application/x-octetstream'])
557 * Sets the content type.
560 PHP_FUNCTION(http_content_type
)
562 char *ct
, *content_type
;
565 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &ct
, &ct_len
) != SUCCESS
) {
570 RETURN_SUCCESS(http_send_header("Content-Type: application/x-octetstream"));
573 /* remember for multiple ranges */
575 efree(HTTP_G(ctype
));
577 HTTP_G(ctype
) = estrndup(ct
, ct_len
);
579 content_type
= (char *) emalloc(strlen("Content-Type: ") + ct_len
+ 1);
580 sprintf(content_type
, "Content-Type: %s", ct
);
582 RETVAL_BOOL(SUCCESS
== http_send_header(content_type
));
587 /* {{{ proto bool http_content_disposition(string filename[, bool inline = false])
589 * Set the Content Disposition. The Content-Disposition header is very useful
590 * if the data actually sent came from a file or something similar, that should
591 * be "saved" by the client/user (i.e. by browsers "Save as..." popup window).
594 PHP_FUNCTION(http_content_disposition
)
596 char *filename
, *header
;
598 zend_bool send_inline
= 0;
600 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &filename
, &f_len
, &send_inline
) != SUCCESS
) {
605 header
= (char *) emalloc(strlen("Content-Disposition: inline; filename=\"\"") + f_len
+ 1);
606 sprintf(header
, "Content-Disposition: inline; filename=\"%s\"", filename
);
608 header
= (char *) emalloc(strlen("Content-Disposition: attachment; filename=\"\"") + f_len
+ 1);
609 sprintf(header
, "Content-Disposition: attachment; filename=\"%s\"", filename
);
612 RETVAL_BOOL(SUCCESS
== http_send_header(header
));
617 /* {{{ proto string http_chunked_decode(string encoded)
619 * This function decodes a string that was HTTP-chunked encoded.
620 * Returns false on failure.
622 PHP_FUNCTION(http_chunked_decode
)
624 char *encoded
= NULL
, *decoded
= NULL
;
625 int encoded_len
= 0, decoded_len
= 0;
627 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &encoded
, &encoded_len
) != SUCCESS
) {
631 if (SUCCESS
== http_chunked_decode(encoded
, encoded_len
, &decoded
, &decoded_len
)) {
632 RETURN_STRINGL(decoded
, decoded_len
, 0);
639 /* {{{ proto array http_split_response(string http_response)
641 * This function splits an HTTP response into an array with headers and the
642 * content body. The returned array may look simliar to the following example:
647 * 'Status' => '200 Ok',
648 * 'Content-Type' => 'text/plain',
649 * 'Content-Language' => 'en-US'
651 * 1 => "Hello World!"
655 PHP_FUNCTION(http_split_response
)
657 zval
*zresponse
, *zbody
, *zheaders
;
659 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zresponse
) != SUCCESS
) {
663 convert_to_string_ex(&zresponse
);
665 MAKE_STD_ZVAL(zbody
);
666 MAKE_STD_ZVAL(zheaders
);
667 array_init(zheaders
);
669 http_split_response(zresponse
, zheaders
, zbody
);
671 array_init(return_value
);
672 add_index_zval(return_value
, 0, zheaders
);
673 add_index_zval(return_value
, 1, zbody
);
678 #if defined(HAVE_CURL) && HAVE_CURL
680 /* {{{ proto string http_get(string url[, array options[, array &info]])
682 * Performs an HTTP GET request on the supplied url.
684 * The second parameter is expected to be an associative
685 * array where the following keys will be recognized:
687 * - redirect: int, whether and how many redirects to follow
688 * - unrestrictedauth: bool, whether to continue sending credentials on
689 * redirects to a different host
690 * - proxyhost: string, proxy host in "host[:port]" format
691 * - proxyport: int, use another proxy port as specified in proxyhost
692 * - proxyauth: string, proxy credentials in "user:pass" format
693 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
694 * - httpauth: string, http credentials in "user:pass" format
695 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
696 * - compress: bool, whether to allow gzip/deflate content encoding
698 * - port: int, use another port as specified in the url
699 * - referer: string, the referer to sends
700 * - useragent: string, the user agent to send
701 * (defaults to PECL::HTTP/version (PHP/version)))
702 * - headers: array, list of custom headers as associative array
703 * like array("header" => "value")
704 * - cookies: array, list of cookies as associative array
705 * like array("cookie" => "value")
706 * - cookiestore: string, path to a file where cookies are/will be stored
709 * The optional third parameter will be filled with some additional information
710 * in form af an associative array, if supplied (don't forget to initialize it
711 * with NULL or array()).
713 PHP_FUNCTION(http_get
)
715 char *URL
, *data
= NULL
;
718 zval
*options
= NULL
, *info
= NULL
;
720 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
729 if (SUCCESS
== http_get(URL
, HASH_ORNULL(options
), HASH_ORNULL(info
), &data
, &data_len
)) {
730 RETURN_STRINGL(data
, data_len
, 0);
737 /* {{{ proto string http_head(string url[, array options[, array &info]])
739 * Performs an HTTP HEAD request on the suppied url.
740 * Returns the HTTP response as string.
741 * See http_get() for a full list of available options.
743 PHP_FUNCTION(http_head
)
745 char *URL
, *data
= NULL
;
748 zval
*options
= NULL
, *info
= NULL
;
750 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
759 if (SUCCESS
== http_head(URL
, HASH_ORNULL(options
), HASH_ORNULL(info
), &data
, &data_len
)) {
760 RETURN_STRINGL(data
, data_len
, 0);
767 /* {{{ proto string http_post_data(string url, string data[, array options[, &info]])
769 * Performs an HTTP POST request, posting data.
770 * Returns the HTTP response as string.
771 * See http_get() for a full list of available options.
773 PHP_FUNCTION(http_post_data
)
775 char *URL
, *postdata
, *data
= NULL
;
777 int postdata_len
, URL_len
;
778 zval
*options
= NULL
, *info
= NULL
;
780 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &postdata
, &postdata_len
, &options
, &info
) != SUCCESS
) {
789 if (SUCCESS
== http_post_data(URL
, postdata
, (size_t) postdata_len
, HASH_ORNULL(options
), HASH_ORNULL(info
), &data
, &data_len
)) {
790 RETURN_STRINGL(data
, data_len
, 0);
797 /* {{{ proto string http_post_array(string url, array data[, array options[, array &info]])
799 * Performs an HTTP POST request, posting www-form-urlencoded array data.
800 * Returns the HTTP response as string.
801 * See http_get() for a full list of available options.
803 PHP_FUNCTION(http_post_array
)
805 char *URL
, *data
= NULL
;
808 zval
*options
= NULL
, *info
= NULL
, *postdata
;
810 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sa|a/!z", &URL
, &URL_len
, &postdata
, &options
, &info
) != SUCCESS
) {
819 if (SUCCESS
== http_post_array(URL
, Z_ARRVAL_P(postdata
), HASH_ORNULL(options
), HASH_ORNULL(info
), &data
, &data_len
)) {
820 RETURN_STRINGL(data
, data_len
, 0);
831 /* {{{ proto bool http_auth_basic(string user, string pass[, string realm = "Restricted"])
836 * if (!http_auth_basic('mike', 's3c|r3t')) {
837 * die('<h1>Authorization failed!</h1>');
842 PHP_FUNCTION(http_auth_basic
)
844 char *realm
= NULL
, *user
, *pass
, *suser
, *spass
;
845 int r_len
, u_len
, p_len
;
847 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &user
, &u_len
, &pass
, &p_len
, &realm
, &r_len
) != SUCCESS
) {
852 realm
= "Restricted";
855 if (SUCCESS
!= http_auth_credentials(&suser
, &spass
)) {
856 http_auth_header("Basic", realm
);
860 if (strcasecmp(suser
, user
)) {
861 http_auth_header("Basic", realm
);
865 if (strcmp(spass
, pass
)) {
866 http_auth_header("Basic", realm
);
874 /* {{{ proto bool http_auth_basic_cb(mixed callback[, string realm = "Restricted"])
879 * function auth_cb($user, $pass)
882 * $query = 'SELECT pass FROM users WHERE user='. $db->quoteSmart($user);
883 * if (strlen($realpass = $db->getOne($query)) {
884 * return $pass === $realpass;
889 * if (!http_auth_basic_cb('auth_cb')) {
890 * die('<h1>Authorization failed</h1>');
895 PHP_FUNCTION(http_auth_basic_cb
)
898 char *realm
= NULL
, *user
, *pass
;
901 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z|s", &cb
, &realm
, &r_len
) != SUCCESS
) {
906 realm
= "Restricted";
909 if (SUCCESS
!= http_auth_credentials(&user
, &pass
)) {
910 http_auth_header("Basic", realm
);
914 zval
*zparams
[2] = {NULL
, NULL
}, retval
;
917 MAKE_STD_ZVAL(zparams
[0]);
918 MAKE_STD_ZVAL(zparams
[1]);
919 ZVAL_STRING(zparams
[0], user
, 0);
920 ZVAL_STRING(zparams
[1], pass
, 0);
922 if (SUCCESS
== call_user_function(EG(function_table
), NULL
, cb
,
923 &retval
, 2, zparams TSRMLS_CC
)) {
924 result
= Z_LVAL(retval
);
933 http_auth_header("Basic", realm
);
942 /* {{{ php_http_init_globals(zend_http_globals *) */
943 static void php_http_init_globals(zend_http_globals
*http_globals
)
945 http_globals
->etag_started
= 0;
946 http_globals
->ctype
= NULL
;
947 http_globals
->etag
= NULL
;
948 http_globals
->lmod
= 0;
949 #if defined(HAVE_CURL) && HAVE_CURL
950 http_globals
->curlbuf
.body
.data
= NULL
;
951 http_globals
->curlbuf
.body
.used
= 0;
952 http_globals
->curlbuf
.body
.free
= 0;
953 http_globals
->curlbuf
.hdrs
.data
= NULL
;
954 http_globals
->curlbuf
.hdrs
.used
= 0;
955 http_globals
->curlbuf
.hdrs
.free
= 0;
960 /* {{{ PHP_MINIT_FUNCTION */
961 PHP_MINIT_FUNCTION(http
)
963 ZEND_INIT_MODULE_GLOBALS(http
, php_http_init_globals
, NULL
);
964 #if defined(HAVE_CURL) && HAVE_CURL
965 REGISTER_LONG_CONSTANT("HTTP_AUTH_BASIC", CURLAUTH_BASIC
, CONST_CS
| CONST_PERSISTENT
);
966 REGISTER_LONG_CONSTANT("HTTP_AUTH_DIGEST", CURLAUTH_DIGEST
, CONST_CS
| CONST_PERSISTENT
);
967 REGISTER_LONG_CONSTANT("HTTP_AUTH_NTLM", CURLAUTH_NTLM
, CONST_CS
| CONST_PERSISTENT
);
973 /* {{{ PHP_RSHUTDOWN_FUNCTION */
974 PHP_RSHUTDOWN_FUNCTION(http
)
977 efree(HTTP_G(ctype
));
982 #if defined(HAVE_CURL) && HAVE_CURL
983 if (HTTP_G(curlbuf
).body
.data
) {
984 efree(HTTP_G(curlbuf
).body
.data
);
986 if (HTTP_G(curlbuf
).hdrs
.data
) {
987 efree(HTTP_G(curlbuf
).hdrs
.data
);
994 /* {{{ PHP_MINFO_FUNCTION */
995 PHP_MINFO_FUNCTION(http
)
997 php_info_print_table_start();
998 php_info_print_table_header(2, "Extended HTTP support", "enabled");
999 php_info_print_table_row(2, "Version:", PHP_EXT_HTTP_VERSION
);
1000 php_info_print_table_row(2, "cURL convenience functions:",
1001 #if defined(HAVE_CURL) && HAVE_CURL
1007 php_info_print_table_end();
1016 * vim600: noet sw=4 ts=4 fdm=marker
1017 * vim<600: noet sw=4 ts=4