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"
30 #include "phpstr/phpstr.h"
33 #include "php_http_std_defs.h"
34 #include "php_http_api.h"
35 #include "php_http_auth_api.h"
36 #include "php_http_curl_api.h"
37 #include "php_http_cache_api.h"
38 #include "php_http_curl_api.h"
39 #include "php_http_date_api.h"
40 #include "php_http_headers_api.h"
41 #include "php_http_message_api.h"
42 #include "php_http_send_api.h"
43 #include "php_http_url_api.h"
45 ZEND_EXTERN_MODULE_GLOBALS(http
)
47 /* {{{ proto string http_date([int timestamp])
49 * This function returns a valid HTTP date regarding RFC 822/1123
50 * looking like: "Wed, 22 Dec 2004 11:34:47 GMT"
53 PHP_FUNCTION(http_date
)
57 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
62 t
= (long) time(NULL
);
65 RETURN_STRING(http_date(t
), 0);
69 /* {{{ proto string http_absolute_uri(string url[, string proto[, string host[, int port]]])
71 * This function returns an absolute URI constructed from url.
72 * If the url is already abolute but a different proto was supplied,
73 * only the proto part of the URI will be updated. If url has no
74 * path specified, the path of the current REQUEST_URI will be taken.
75 * The host will be taken either from the Host HTTP header of the client
76 * the SERVER_NAME or just localhost if prior are not available.
80 * url = "page.php" => http://www.example.com/current/path/page.php
81 * url = "/page.php" => http://www.example.com/page.php
82 * url = "/page.php", proto = "https" => https://www.example.com/page.php
86 PHP_FUNCTION(http_absolute_uri
)
88 char *url
= NULL
, *proto
= NULL
, *host
= NULL
;
89 int url_len
= 0, proto_len
= 0, host_len
= 0;
92 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|ssl", &url
, &url_len
, &proto
, &proto_len
, &host
, &host_len
, &port
) != SUCCESS
) {
96 RETURN_STRING(http_absolute_uri_ex(url
, url_len
, proto
, proto_len
, host
, host_len
, port
), 0);
100 /* {{{ proto string http_negotiate_language(array supported[, string default = 'en-US'])
102 * This function negotiates the clients preferred language based on its
103 * Accept-Language HTTP header. It returns the negotiated language or
104 * the default language if none match.
106 * The qualifier is recognized and languages without qualifier are rated highest.
108 * The supported parameter is expected to be an array having
109 * the supported languages as array values.
123 * include './langs/'. http_negotiate_language($langs) .'.php';
128 PHP_FUNCTION(http_negotiate_language
)
134 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|s", &supported
, &def
, &def_len
) != SUCCESS
) {
142 RETURN_STRING(http_negotiate_language(supported
, def
), 0);
146 /* {{{ proto string http_negotiate_charset(array supported[, string default = 'iso-8859-1'])
148 * This function negotiates the clients preferred charset based on its
149 * Accept-Charset HTTP header. It returns the negotiated charset or
150 * the default charset if none match.
152 * The qualifier is recognized and charset without qualifier are rated highest.
154 * The supported parameter is expected to be an array having
155 * the supported charsets as array values.
161 * 'iso-8859-1', // default
166 * $pref = http_negotiate_charset($charsets);
167 * if (!strcmp($pref, 'iso-8859-1')) {
168 * iconv_set_encoding('internal_encoding', 'iso-8859-1');
169 * iconv_set_encoding('output_encoding', $pref);
170 * ob_start('ob_iconv_handler');
175 PHP_FUNCTION(http_negotiate_charset
)
181 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|s", &supported
, &def
, &def_len
) != SUCCESS
) {
189 RETURN_STRING(http_negotiate_charset(supported
, def
), 0);
193 /* {{{ proto bool http_send_status(int status)
195 * Send HTTP status code.
198 PHP_FUNCTION(http_send_status
)
202 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &status
) != SUCCESS
) {
205 if (status
< 100 || status
> 510) {
206 http_error_ex(E_WARNING
, HTTP_E_HEADER
, "Invalid HTTP status code (100-510): %d", status
);
210 RETURN_SUCCESS(http_send_status(status
));
214 /* {{{ proto bool http_send_last_modified([int timestamp])
216 * This converts the given timestamp to a valid HTTP date and
217 * sends it as "Last-Modified" HTTP header. If timestamp is
218 * omitted, current time is sent.
221 PHP_FUNCTION(http_send_last_modified
)
225 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
230 t
= (long) time(NULL
);
233 RETURN_SUCCESS(http_send_last_modified(t
));
237 /* {{{ proto bool http_send_content_type([string content_type = 'application/x-octetstream'])
239 * Sets the content type.
242 PHP_FUNCTION(http_send_content_type
)
247 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &ct
, &ct_len
) != SUCCESS
) {
252 RETURN_SUCCESS(http_send_content_type("application/x-octetstream", sizeof("application/x-octetstream") - 1));
254 RETURN_SUCCESS(http_send_content_type(ct
, ct_len
));
258 /* {{{ proto bool http_send_content_disposition(string filename[, bool inline = false])
260 * Set the Content Disposition. The Content-Disposition header is very useful
261 * if the data actually sent came from a file or something similar, that should
262 * be "saved" by the client/user (i.e. by browsers "Save as..." popup window).
265 PHP_FUNCTION(http_send_content_disposition
)
269 zend_bool send_inline
= 0;
271 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &filename
, &f_len
, &send_inline
) != SUCCESS
) {
274 RETURN_SUCCESS(http_send_content_disposition(filename
, f_len
, send_inline
));
278 /* {{{ proto bool http_match_modified([int timestamp[, for_range = false]])
280 * Matches the given timestamp against the clients "If-Modified-Since" resp.
281 * "If-Unmodified-Since" HTTP headers.
284 PHP_FUNCTION(http_match_modified
)
287 zend_bool for_range
= 0;
289 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|lb", &t
, &for_range
) != SUCCESS
) {
293 // current time if not supplied (senseless though)
295 t
= (long) time(NULL
);
299 RETURN_BOOL(http_modified_match("HTTP_IF_UNMODIFIED_SINCE", t
));
301 RETURN_BOOL(http_modified_match("HTTP_IF_MODIFIED_SINCE", t
));
305 /* {{{ proto bool http_match_etag(string etag[, for_range = false])
307 * This matches the given ETag against the clients
308 * "If-Match" resp. "If-None-Match" HTTP headers.
311 PHP_FUNCTION(http_match_etag
)
315 zend_bool for_range
= 0;
317 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &etag
, &etag_len
, &for_range
) != SUCCESS
) {
322 RETURN_BOOL(http_etag_match("HTTP_IF_MATCH", etag
));
324 RETURN_BOOL(http_etag_match("HTTP_IF_NONE_MATCH", etag
));
328 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
330 * If timestamp_or_exires is greater than 0, it is handled as timestamp
331 * and will be sent as date of last modification. If it is 0 or omitted,
332 * the current time will be sent as Last-Modified date. If it's negative,
333 * it is handled as expiration time in seconds, which means that if the
334 * requested last modification date is not between the calculated timespan,
335 * the Last-Modified header is updated and the actual body will be sent.
338 PHP_FUNCTION(http_cache_last_modified
)
340 long last_modified
= 0, send_modified
= 0, t
;
343 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &last_modified
) != SUCCESS
) {
347 t
= (long) time(NULL
);
350 if (!last_modified
) {
351 /* does the client have? (att: caching "forever") */
352 if (zlm
= http_get_server_var("HTTP_IF_MODIFIED_SINCE")) {
353 last_modified
= send_modified
= http_parse_date(Z_STRVAL_P(zlm
));
354 /* send current time */
358 /* negative value is supposed to be expiration time */
359 } else if (last_modified
< 0) {
362 /* send supplied time explicitly */
364 send_modified
= last_modified
;
367 RETURN_SUCCESS(http_cache_last_modified(last_modified
, send_modified
, HTTP_DEFAULT_CACHECONTROL
, sizeof(HTTP_DEFAULT_CACHECONTROL
) - 1));
371 /* {{{ proto bool http_cache_etag([string etag])
373 * This function attempts to cache the HTTP body based on an ETag,
374 * either supplied or generated through calculation of the MD5
375 * checksum of the output (uses output buffering).
377 * If clients "If-None-Match" header matches the supplied/calculated
378 * ETag, the body is considered cached on the clients side and
379 * a "304 Not Modified" status code is issued.
382 PHP_FUNCTION(http_cache_etag
)
387 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &etag
, &etag_len
) != SUCCESS
) {
391 RETURN_SUCCESS(http_cache_etag(etag
, etag_len
, HTTP_DEFAULT_CACHECONTROL
, sizeof(HTTP_DEFAULT_CACHECONTROL
) - 1));
395 /* {{{ proto string ob_httpetaghandler(string data, int mode)
397 * For use with ob_start().
398 * Note that this has to be started as first output buffer.
399 * WARNING: Don't use with http_send_*().
401 PHP_FUNCTION(ob_httpetaghandler
)
407 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sl", &data
, &data_len
, &mode
)) {
411 if (mode
& PHP_OUTPUT_HANDLER_START
) {
412 if (HTTP_G(etag_started
)) {
413 http_error(E_WARNING
, HTTP_E_OBUFFER
, "ob_httpetaghandler can only be used once");
414 RETURN_STRINGL(data
, data_len
, 1);
416 http_send_header("Cache-Control: " HTTP_DEFAULT_CACHECONTROL
);
417 HTTP_G(etag_started
) = 1;
420 if (OG(ob_nesting_level
) > 1) {
421 http_error(E_WARNING
, HTTP_E_OBUFFER
, "ob_httpetaghandler must be started prior to other output buffers");
422 RETURN_STRINGL(data
, data_len
, 1);
425 Z_TYPE_P(return_value
) = IS_STRING
;
426 http_ob_etaghandler(data
, data_len
, &Z_STRVAL_P(return_value
), &Z_STRLEN_P(return_value
), mode
);
430 /* {{{ proto void http_redirect([string url[, array params[, bool session,[ bool permanent]]]])
432 * Redirect to a given url.
433 * The supplied url will be expanded with http_absolute_uri(), the params array will
434 * be treated with http_build_query() and the session identification will be appended
435 * if session is true.
437 * Depending on permanent the redirection will be issued with a permanent
438 * ("301 Moved Permanently") or a temporary ("302 Found") redirection
441 * To be RFC compliant, "Redirecting to <a>URI</a>." will be displayed,
442 * if the client doesn't redirect immediatly.
444 PHP_FUNCTION(http_redirect
)
447 size_t query_len
= 0;
448 zend_bool session
= 0, permanent
= 0;
450 char *query
, *url
, *URI
,
451 LOC
[HTTP_URI_MAXLEN
+ sizeof("Location: ")],
452 RED
[HTTP_URI_MAXLEN
* 2 + sizeof("Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n")];
454 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sa!/bb", &url
, &url_len
, ¶ms
, &session
, &permanent
) != SUCCESS
) {
458 /* append session info */
459 if (session
&& (PS(session_status
) == php_session_active
)) {
461 MAKE_STD_ZVAL(params
);
464 if (add_assoc_string(params
, PS(session_name
), PS(id
), 1) != SUCCESS
) {
465 http_error(E_WARNING
, HTTP_E_ENCODE
, "Could not append session information");
469 /* treat params array with http_build_query() */
471 if (SUCCESS
!= http_urlencode_hash_ex(Z_ARRVAL_P(params
), 0, NULL
, 0, &query
, &query_len
)) {
476 URI
= http_absolute_uri(url
);
479 snprintf(LOC
, HTTP_URI_MAXLEN
+ sizeof("Location: "), "Location: %s?%s", URI
, query
);
480 sprintf(RED
, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI
, query
, URI
, query
);
483 snprintf(LOC
, HTTP_URI_MAXLEN
+ sizeof("Location: "), "Location: %s", URI
);
484 sprintf(RED
, "Redirecting to <a href=\"%s\">%s</a>.\n", URI
, URI
);
488 if ((SUCCESS
== http_send_header(LOC
)) && (SUCCESS
== http_send_status((permanent
? 301 : 302)))) {
489 php_body_write(RED
, strlen(RED
) TSRMLS_CC
);
496 /* {{{ proto bool http_send_data(string data)
498 * Sends raw data with support for (multiple) range requests.
501 PHP_FUNCTION(http_send_data
)
505 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zdata
) != SUCCESS
) {
509 convert_to_string_ex(&zdata
);
510 http_send_header("Accept-Ranges: bytes");
511 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata
), Z_STRLEN_P(zdata
)));
515 /* {{{ proto bool http_send_file(string file)
517 * Sends a file with support for (multiple) range requests.
520 PHP_FUNCTION(http_send_file
)
525 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &file
, &flen
) != SUCCESS
) {
532 http_send_header("Accept-Ranges: bytes");
533 RETURN_SUCCESS(http_send_file(file
));
537 /* {{{ proto bool http_send_stream(resource stream)
539 * Sends an already opened stream with support for (multiple) range requests.
542 PHP_FUNCTION(http_send_stream
)
547 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &zstream
) != SUCCESS
) {
551 php_stream_from_zval(file
, &zstream
);
552 http_send_header("Accept-Ranges: bytes");
553 RETURN_SUCCESS(http_send_stream(file
));
557 /* {{{ proto string http_chunked_decode(string encoded)
559 * This function decodes a string that was HTTP-chunked encoded.
560 * Returns false on failure.
562 PHP_FUNCTION(http_chunked_decode
)
564 char *encoded
= NULL
, *decoded
= NULL
;
565 int encoded_len
= 0, decoded_len
= 0;
567 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &encoded
, &encoded_len
) != SUCCESS
) {
571 if (SUCCESS
== http_chunked_decode(encoded
, encoded_len
, &decoded
, &decoded_len
)) {
572 RETURN_STRINGL(decoded
, decoded_len
, 0);
579 /* {{{ proto array http_split_response(string http_response)
581 * This function splits an HTTP response into an array with headers and the
582 * content body. The returned array may look simliar to the following example:
588 * 'Status' => '200 Ok',
589 * 'Content-Type' => 'text/plain',
590 * 'Content-Language' => 'en-US'
592 * 1 => "Hello World!"
597 PHP_FUNCTION(http_split_response
)
599 zval
*zresponse
, *zbody
, *zheaders
;
601 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zresponse
) != SUCCESS
) {
605 convert_to_string(zresponse
);
607 MAKE_STD_ZVAL(zbody
);
608 MAKE_STD_ZVAL(zheaders
);
609 array_init(zheaders
);
611 if (SUCCESS
!= http_split_response(zresponse
, zheaders
, zbody
)) {
612 http_error(E_WARNING
, HTTP_E_PARSE
, "Could not parse HTTP response");
616 array_init(return_value
);
617 add_index_zval(return_value
, 0, zheaders
);
618 add_index_zval(return_value
, 1, zbody
);
622 /* {{{ proto array http_parse_headers(string header)
625 PHP_FUNCTION(http_parse_headers
)
630 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &header
, &header_len
)) {
634 array_init(return_value
);
636 if (rnrn
= strstr(header
, HTTP_CRLF HTTP_CRLF
)) {
637 header_len
= rnrn
- header
+ 2;
639 if (SUCCESS
!= http_parse_headers(header
, header_len
, return_value
)) {
640 http_error(E_WARNING
, HTTP_E_PARSE
, "Could not parse HTTP headers");
641 zval_dtor(return_value
);
647 /* {{{ proto array http_get_request_headers(void)
650 PHP_FUNCTION(http_get_request_headers
)
654 array_init(return_value
);
655 http_get_request_headers(return_value
);
660 #ifdef HTTP_HAVE_CURL
662 /* {{{ proto string http_get(string url[, array options[, array &info]])
664 * Performs an HTTP GET request on the supplied url.
666 * The second parameter is expected to be an associative
667 * array where the following keys will be recognized:
669 * - redirect: int, whether and how many redirects to follow
670 * - unrestrictedauth: bool, whether to continue sending credentials on
671 * redirects to a different host
672 * - proxyhost: string, proxy host in "host[:port]" format
673 * - proxyport: int, use another proxy port as specified in proxyhost
674 * - proxyauth: string, proxy credentials in "user:pass" format
675 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
676 * - httpauth: string, http credentials in "user:pass" format
677 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
678 * - compress: bool, whether to allow gzip/deflate content encoding
680 * - port: int, use another port as specified in the url
681 * - referer: string, the referer to sends
682 * - useragent: string, the user agent to send
683 * (defaults to PECL::HTTP/version (PHP/version)))
684 * - headers: array, list of custom headers as associative array
685 * like array("header" => "value")
686 * - cookies: array, list of cookies as associative array
687 * like array("cookie" => "value")
688 * - cookiestore: string, path to a file where cookies are/will be stored
689 * - resume: int, byte offset to start the download from;
690 * if the server supports ranges
691 * - maxfilesize: int, maximum file size that should be downloaded;
692 * has no effect, if the size of the requested entity is not known
693 * - lastmodified: int, timestamp for If-(Un)Modified-Since header
694 * - timeout: int, seconds the request may take
695 * - connecttimeout: int, seconds the connect may take
698 * The optional third parameter will be filled with some additional information
699 * in form af an associative array, if supplied, like the following example:
703 * 'effective_url' => 'http://localhost',
704 * 'response_code' => 403,
705 * 'total_time' => 0.017,
706 * 'namelookup_time' => 0.013,
707 * 'connect_time' => 0.014,
708 * 'pretransfer_time' => 0.014,
709 * 'size_upload' => 0,
710 * 'size_download' => 202,
711 * 'speed_download' => 11882,
712 * 'speed_upload' => 0,
713 * 'header_size' => 145,
714 * 'request_size' => 62,
715 * 'ssl_verifyresult' => 0,
717 * 'content_length_download' => 202,
718 * 'content_length_upload' => 0,
719 * 'starttransfer_time' => 0.017,
720 * 'content_type' => 'text/html; charset=iso-8859-1',
721 * 'redirect_time' => 0,
722 * 'redirect_count' => 0,
724 * 'http_connectcode' => 0,
725 * 'httpauth_avail' => 0,
726 * 'proxyauth_avail' => 0,
731 PHP_FUNCTION(http_get
)
733 zval
*options
= NULL
, *info
= NULL
;
738 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
747 phpstr_init_ex(&response
, HTTP_CURLBUF_SIZE
, 0);
748 if (SUCCESS
== http_get(URL
, options
? Z_ARRVAL_P(options
) : NULL
, info
? Z_ARRVAL_P(info
) : NULL
, &response
)) {
749 RETURN_PHPSTR_VAL(response
);
756 /* {{{ proto string http_head(string url[, array options[, array &info]])
758 * Performs an HTTP HEAD request on the suppied url.
759 * Returns the HTTP response as string.
760 * See http_get() for a full list of available options.
762 PHP_FUNCTION(http_head
)
764 zval
*options
= NULL
, *info
= NULL
;
769 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
778 phpstr_init_ex(&response
, HTTP_CURLBUF_SIZE
, 0);
779 if (SUCCESS
== http_head(URL
, options
? Z_ARRVAL_P(options
) : NULL
, info
? Z_ARRVAL_P(info
) : NULL
, &response
)) {
780 RETURN_PHPSTR_VAL(response
);
787 /* {{{ proto string http_post_data(string url, string data[, array options[, &info]])
789 * Performs an HTTP POST request, posting data.
790 * Returns the HTTP response as string.
791 * See http_get() for a full list of available options.
793 PHP_FUNCTION(http_post_data
)
795 zval
*options
= NULL
, *info
= NULL
;
796 char *URL
, *postdata
;
797 int postdata_len
, URL_len
;
800 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &postdata
, &postdata_len
, &options
, &info
) != SUCCESS
) {
809 phpstr_init_ex(&response
, HTTP_CURLBUF_SIZE
, 0);
810 if (SUCCESS
== http_post_data(URL
, postdata
, (size_t) postdata_len
, options
? Z_ARRVAL_P(options
) : NULL
, info
? Z_ARRVAL_P(info
) : NULL
, &response
)) {
811 RETURN_PHPSTR_VAL(response
);
818 /* {{{ proto string http_post_array(string url, array data[, array options[, array &info]])
820 * Performs an HTTP POST request, posting www-form-urlencoded array data.
821 * Returns the HTTP response as string.
822 * See http_get() for a full list of available options.
824 PHP_FUNCTION(http_post_array
)
826 zval
*options
= NULL
, *info
= NULL
, *postdata
;
831 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sa|a/!z", &URL
, &URL_len
, &postdata
, &options
, &info
) != SUCCESS
) {
840 phpstr_init_ex(&response
, HTTP_CURLBUF_SIZE
, 0);
841 if (SUCCESS
== http_post_array(URL
, Z_ARRVAL_P(postdata
), options
? Z_ARRVAL_P(options
) : NULL
, info
? Z_ARRVAL_P(info
) : NULL
, &response
)) {
842 RETURN_PHPSTR_VAL(response
);
853 /* {{{ proto bool http_auth_basic(string user, string pass[, string realm = "Restricted"])
858 * if (!http_auth_basic('mike', 's3c|r3t')) {
859 * die('<h1>Authorization failed!</h1>');
864 PHP_FUNCTION(http_auth_basic
)
866 char *realm
= NULL
, *user
, *pass
, *suser
, *spass
;
867 int r_len
, u_len
, p_len
;
869 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &user
, &u_len
, &pass
, &p_len
, &realm
, &r_len
) != SUCCESS
) {
874 realm
= "Restricted";
877 if (SUCCESS
!= http_auth_credentials(&suser
, &spass
)) {
878 http_auth_header("Basic", realm
);
882 if (strcasecmp(suser
, user
)) {
883 http_auth_header("Basic", realm
);
887 if (strcmp(spass
, pass
)) {
888 http_auth_header("Basic", realm
);
896 /* {{{ proto bool http_auth_basic_cb(mixed callback[, string realm = "Restricted"])
901 * function auth_cb($user, $pass)
904 * $query = 'SELECT pass FROM users WHERE user='. $db->quoteSmart($user);
905 * if (strlen($realpass = $db->getOne($query)) {
906 * return $pass === $realpass;
910 * if (!http_auth_basic_cb('auth_cb')) {
911 * die('<h1>Authorization failed</h1>');
916 PHP_FUNCTION(http_auth_basic_cb
)
919 char *realm
= NULL
, *user
, *pass
;
922 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z|s", &cb
, &realm
, &r_len
) != SUCCESS
) {
927 realm
= "Restricted";
930 if (SUCCESS
!= http_auth_credentials(&user
, &pass
)) {
931 http_auth_header("Basic", realm
);
935 zval
*zparams
[2] = {NULL
, NULL
}, retval
;
938 MAKE_STD_ZVAL(zparams
[0]);
939 MAKE_STD_ZVAL(zparams
[1]);
940 ZVAL_STRING(zparams
[0], user
, 0);
941 ZVAL_STRING(zparams
[1], pass
, 0);
943 if (SUCCESS
== call_user_function(EG(function_table
), NULL
, cb
,
944 &retval
, 2, zparams TSRMLS_CC
)) {
945 result
= Z_LVAL(retval
);
954 http_auth_header("Basic", realm
);
962 /* {{{ Sara Golemons http_build_query() */
963 #ifndef ZEND_ENGINE_2
965 /* {{{ proto string http_build_query(mixed formdata [, string prefix[, string arg_separator]])
966 Generates a form-encoded query string from an associative array or object. */
967 PHP_FUNCTION(http_build_query
)
970 char *prefix
= NULL
, *arg_sep
= INI_STR("arg_separator.output");
971 int prefix_len
= 0, arg_sep_len
= strlen(arg_sep
);
974 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z|ss", &formdata
, &prefix
, &prefix_len
, &arg_sep
, &arg_sep_len
) != SUCCESS
) {
978 if (Z_TYPE_P(formdata
) != IS_ARRAY
&& Z_TYPE_P(formdata
) != IS_OBJECT
) {
979 http_error(E_WARNING
, HTTP_E_PARAM
, "Parameter 1 expected to be Array or Object. Incorrect value given.");
984 arg_sep
= HTTP_URL_ARGSEP
;
987 formstr
= phpstr_new();
988 if (SUCCESS
!= http_urlencode_hash_implementation_ex(HASH_OF(formdata
), formstr
, arg_sep
, prefix
, prefix_len
, NULL
, 0, NULL
, 0, (Z_TYPE_P(formdata
) == IS_OBJECT
? formdata
: NULL
))) {
989 phpstr_free(formstr
);
993 if (!formstr
->used
) {
994 phpstr_free(formstr
);
998 RETURN_PHPSTR_PTR(formstr
);
1001 #endif /* !ZEND_ENGINE_2 */
1004 PHP_FUNCTION(http_test
)
1013 * vim600: noet sw=4 ts=4 fdm=marker
1014 * vim<600: noet sw=4 ts=4