010c0a249b2fcdd65042f544825be2e44f774f00
[m6w6/ext-http] / http_functions.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
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 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18 #define _WINSOCKAPI_
19 #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include "php.h"
26 #include "php_ini.h"
27 #include "snprintf.h"
28 #include "ext/standard/info.h"
29 #include "ext/session/php_session.h"
30 #include "ext/standard/php_string.h"
31 #include "ext/standard/php_smart_str.h"
32
33 #include "SAPI.h"
34
35 #include "php_http.h"
36 #include "php_http_api.h"
37 #include "php_http_curl_api.h"
38 #include "php_http_std_defs.h"
39
40 ZEND_DECLARE_MODULE_GLOBALS(http)
41
42 /* {{{ proto string http_date([int timestamp])
43 *
44 * This function returns a valid HTTP date regarding RFC 822/1123
45 * looking like: "Wed, 22 Dec 2004 11:34:47 GMT"
46 *
47 */
48 PHP_FUNCTION(http_date)
49 {
50 long t = -1;
51
52 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
53 RETURN_FALSE;
54 }
55
56 if (t == -1) {
57 t = (long) time(NULL);
58 }
59
60 RETURN_STRING(http_date(t), 0);
61 }
62 /* }}} */
63
64 /* {{{ proto string http_absolute_uri(string url[, string proto])
65 *
66 * This function returns an absolute URI constructed from url.
67 * If the url is already abolute but a different proto was supplied,
68 * only the proto part of the URI will be updated. If url has no
69 * path specified, the path of the current REQUEST_URI will be taken.
70 * The host will be taken either from the Host HTTP header of the client
71 * the SERVER_NAME or just localhost if prior are not available.
72 *
73 * Some examples:
74 * <pre>
75 * url = "page.php" => http://www.example.com/current/path/page.php
76 * url = "/page.php" => http://www.example.com/page.php
77 * url = "/page.php", proto = "https" => https://www.example.com/page.php
78 * </pre>
79 *
80 */
81 PHP_FUNCTION(http_absolute_uri)
82 {
83 char *url = NULL, *proto = NULL;
84 int url_len = 0, proto_len = 0;
85
86 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &url, &url_len, &proto, &proto_len) != SUCCESS) {
87 RETURN_FALSE;
88 }
89
90 RETURN_STRING(http_absolute_uri(url, proto), 0);
91 }
92 /* }}} */
93
94 /* {{{ proto string http_negotiate_language(array supported[, string default = 'en-US'])
95 *
96 * This function negotiates the clients preferred language based on its
97 * Accept-Language HTTP header. It returns the negotiated language or
98 * the default language if none match.
99 *
100 * The qualifier is recognized and languages without qualifier are rated highest.
101 *
102 * The supported parameter is expected to be an array having
103 * the supported languages as array values.
104 *
105 * Example:
106 * <pre>
107 * <?php
108 * $langs = array(
109 * 'en-US',// default
110 * 'fr',
111 * 'fr-FR',
112 * 'de',
113 * 'de-DE',
114 * 'de-AT',
115 * 'de-CH',
116 * );
117 * include './langs/'. http_negotiate_language($langs) .'.php';
118 * ?>
119 * </pre>
120 *
121 */
122 PHP_FUNCTION(http_negotiate_language)
123 {
124 zval *supported;
125 char *def = NULL;
126 int def_len = 0;
127
128 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|s", &supported, &def, &def_len) != SUCCESS) {
129 RETURN_FALSE;
130 }
131
132 if (!def) {
133 def = "en-US";
134 }
135
136 RETURN_STRING(http_negotiate_language(supported, def), 0);
137 }
138 /* }}} */
139
140 /* {{{ proto string http_negotiate_charset(array supported[, string default = 'iso-8859-1'])
141 *
142 * This function negotiates the clients preferred charset based on its
143 * Accept-Charset HTTP header. It returns the negotiated charset or
144 * the default charset if none match.
145 *
146 * The qualifier is recognized and charset without qualifier are rated highest.
147 *
148 * The supported parameter is expected to be an array having
149 * the supported charsets as array values.
150 *
151 * Example:
152 * <pre>
153 * <?php
154 * $charsets = array(
155 * 'iso-8859-1', // default
156 * 'iso-8859-2',
157 * 'iso-8859-15',
158 * 'utf-8'
159 * );
160 * $pref = http_negotiate_charset($charsets);
161 * if (!strcmp($pref, 'iso-8859-1')) {
162 * iconv_set_encoding('internal_encoding', 'iso-8859-1');
163 * iconv_set_encoding('output_encoding', $pref);
164 * ob_start('ob_iconv_handler');
165 * }
166 * ?>
167 * </pre>
168 */
169 PHP_FUNCTION(http_negotiate_charset)
170 {
171 zval *supported;
172 char *def = NULL;
173 int def_len = 0;
174
175 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|s", &supported, &def, &def_len) != SUCCESS) {
176 RETURN_FALSE;
177 }
178
179 if (!def) {
180 def = "iso-8859-1";
181 }
182
183 RETURN_STRING(http_negotiate_charset(supported, def), 0);
184 }
185 /* }}} */
186
187 /* {{{ proto bool http_send_status(int status)
188 *
189 * Send HTTP status code.
190 *
191 */
192 PHP_FUNCTION(http_send_status)
193 {
194 int status = 0;
195
196 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &status) != SUCCESS) {
197 RETURN_FALSE;
198 }
199 if (status < 100 || status > 510) {
200 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid HTTP status code (100-510): %d", status);
201 RETURN_FALSE;
202 }
203
204 RETURN_SUCCESS(http_send_status(status));
205 }
206 /* }}} */
207
208 /* {{{ proto bool http_send_last_modified([int timestamp])
209 *
210 * This converts the given timestamp to a valid HTTP date and
211 * sends it as "Last-Modified" HTTP header. If timestamp is
212 * omitted, current time is sent.
213 *
214 */
215 PHP_FUNCTION(http_send_last_modified)
216 {
217 long t = -1;
218
219 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
220 RETURN_FALSE;
221 }
222
223 if (t == -1) {
224 t = (long) time(NULL);
225 }
226
227 RETURN_SUCCESS(http_send_last_modified(t));
228 }
229 /* }}} */
230
231 /* {{{ proto bool http_send_content_type([string content_type = 'application/x-octetstream'])
232 *
233 * Sets the content type.
234 *
235 */
236 PHP_FUNCTION(http_send_content_type)
237 {
238 char *ct;
239 int ct_len = 0;
240
241 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &ct, &ct_len) != SUCCESS) {
242 RETURN_FALSE;
243 }
244
245 if (!ct_len) {
246 RETURN_SUCCESS(http_send_content_type("application/x-octetstream", sizeof("application/x-octetstream") - 1));
247 }
248 RETURN_SUCCESS(http_send_content_type(ct, ct_len));
249 }
250 /* }}} */
251
252 /* {{{ proto bool http_send_content_disposition(string filename[, bool inline = false])
253 *
254 * Set the Content Disposition. The Content-Disposition header is very useful
255 * if the data actually sent came from a file or something similar, that should
256 * be "saved" by the client/user (i.e. by browsers "Save as..." popup window).
257 *
258 */
259 PHP_FUNCTION(http_send_content_disposition)
260 {
261 char *filename;
262 int f_len;
263 zend_bool send_inline = 0;
264
265 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &filename, &f_len, &send_inline) != SUCCESS) {
266 RETURN_FALSE;
267 }
268 RETURN_SUCCESS(http_send_content_disposition(filename, f_len, send_inline));
269 }
270 /* }}} */
271
272 /* {{{ proto bool http_match_modified([int timestamp[, for_range = false]])
273 *
274 * Matches the given timestamp against the clients "If-Modified-Since" resp.
275 * "If-Unmodified-Since" HTTP headers.
276 *
277 */
278 PHP_FUNCTION(http_match_modified)
279 {
280 long t = -1;
281 zend_bool for_range = 0;
282
283 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lb", &t, &for_range) != SUCCESS) {
284 RETURN_FALSE;
285 }
286
287 // current time if not supplied (senseless though)
288 if (t == -1) {
289 t = (long) time(NULL);
290 }
291
292 if (for_range) {
293 RETURN_BOOL(http_modified_match("HTTP_IF_UNMODIFIED_SINCE", t));
294 }
295 RETURN_BOOL(http_modified_match("HTTP_IF_MODIFIED_SINCE", t));
296 }
297 /* }}} */
298
299 /* {{{ proto bool http_match_etag(string etag[, for_range = false])
300 *
301 * This matches the given ETag against the clients
302 * "If-Match" resp. "If-None-Match" HTTP headers.
303 *
304 */
305 PHP_FUNCTION(http_match_etag)
306 {
307 int etag_len;
308 char *etag;
309 zend_bool for_range = 0;
310
311 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &etag, &etag_len, &for_range) != SUCCESS) {
312 RETURN_FALSE;
313 }
314
315 if (for_range) {
316 RETURN_BOOL(http_etag_match("HTTP_IF_MATCH", etag));
317 }
318 RETURN_BOOL(http_etag_match("HTTP_IF_NONE_MATCH", etag));
319 }
320 /* }}} */
321
322 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
323 *
324 * If timestamp_or_exires is greater than 0, it is handled as timestamp
325 * and will be sent as date of last modification. If it is 0 or omitted,
326 * the current time will be sent as Last-Modified date. If it's negative,
327 * it is handled as expiration time in seconds, which means that if the
328 * requested last modification date is not between the calculated timespan,
329 * the Last-Modified header is updated and the actual body will be sent.
330 *
331 */
332 PHP_FUNCTION(http_cache_last_modified)
333 {
334 long last_modified = 0, send_modified = 0, t;
335 zval *zlm;
336
337 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &last_modified) != SUCCESS) {
338 RETURN_FALSE;
339 }
340
341 t = (long) time(NULL);
342
343 /* 0 or omitted */
344 if (!last_modified) {
345 /* does the client have? (att: caching "forever") */
346 if (zlm = http_get_server_var("HTTP_IF_MODIFIED_SINCE")) {
347 last_modified = send_modified = http_parse_date(Z_STRVAL_P(zlm));
348 /* send current time */
349 } else {
350 send_modified = t;
351 }
352 /* negative value is supposed to be expiration time */
353 } else if (last_modified < 0) {
354 last_modified += t;
355 send_modified = t;
356 /* send supplied time explicitly */
357 } else {
358 send_modified = last_modified;
359 }
360
361 RETURN_SUCCESS(http_cache_last_modified(last_modified, send_modified, HTTP_DEFAULT_CACHECONTROL, sizeof(HTTP_DEFAULT_CACHECONTROL) - 1));
362 }
363 /* }}} */
364
365 /* {{{ proto bool http_cache_etag([string etag])
366 *
367 * This function attempts to cache the HTTP body based on an ETag,
368 * either supplied or generated through calculation of the MD5
369 * checksum of the output (uses output buffering).
370 *
371 * If clients "If-None-Match" header matches the supplied/calculated
372 * ETag, the body is considered cached on the clients side and
373 * a "304 Not Modified" status code is issued.
374 *
375 */
376 PHP_FUNCTION(http_cache_etag)
377 {
378 char *etag;
379 int etag_len = 0;
380
381 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &etag, &etag_len) != SUCCESS) {
382 RETURN_FALSE;
383 }
384
385 RETURN_SUCCESS(http_cache_etag(etag, etag_len, HTTP_DEFAULT_CACHECONTROL, sizeof(HTTP_DEFAULT_CACHECONTROL) - 1));
386 }
387 /* }}} */
388
389 /* {{{ proto string ob_httpetaghandler(string data, int mode)
390 *
391 * For use with ob_start().
392 * Note that this has to be started as first output buffer.
393 * WARNING: Don't use with http_send_*().
394 */
395 PHP_FUNCTION(ob_httpetaghandler)
396 {
397 char *data;
398 int data_len;
399 long mode;
400
401 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &data, &data_len, &mode)) {
402 RETURN_FALSE;
403 }
404
405 if (mode & PHP_OUTPUT_HANDLER_START) {
406 if (HTTP_G(etag_started)) {
407 php_error_docref(NULL TSRMLS_CC, E_WARNING, "ob_httpetaghandler can only be used once");
408 RETURN_STRINGL(data, data_len, 1);
409 }
410 http_send_header("Cache-Control: " HTTP_DEFAULT_CACHECONTROL);
411 HTTP_G(etag_started) = 1;
412 }
413
414 if (OG(ob_nesting_level) > 1) {
415 php_error_docref(NULL TSRMLS_CC, E_WARNING, "ob_httpetaghandler must be started prior to other output buffers");
416 RETURN_STRINGL(data, data_len, 1);
417 }
418
419 Z_TYPE_P(return_value) = IS_STRING;
420 http_ob_etaghandler(data, data_len, &Z_STRVAL_P(return_value), &Z_STRLEN_P(return_value), mode);
421 }
422 /* }}} */
423
424 /* {{{ proto void http_redirect([string url[, array params[, bool session,[ bool permanent]]]])
425 *
426 * Redirect to a given url.
427 * The supplied url will be expanded with http_absolute_uri(), the params array will
428 * be treated with http_build_query() and the session identification will be appended
429 * if session is true.
430 *
431 * Depending on permanent the redirection will be issued with a permanent
432 * ("301 Moved Permanently") or a temporary ("302 Found") redirection
433 * status code.
434 *
435 * To be RFC compliant, "Redirecting to <a>URI</a>." will be displayed,
436 * if the client doesn't redirect immediatly.
437 */
438 PHP_FUNCTION(http_redirect)
439 {
440 int url_len;
441 size_t query_len = 0;
442 zend_bool session = 0, permanent = 0;
443 zval *params = NULL;
444 char *query, *url, *URI,
445 LOC[HTTP_URI_MAXLEN + sizeof("Location: ")],
446 RED[HTTP_URI_MAXLEN * 2 + sizeof("Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n")];
447
448 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sa!/bb", &url, &url_len, &params, &session, &permanent) != SUCCESS) {
449 RETURN_FALSE;
450 }
451
452 /* append session info */
453 if (session && (PS(session_status) == php_session_active)) {
454 if (!params) {
455 MAKE_STD_ZVAL(params);
456 array_init(params);
457 }
458 if (add_assoc_string(params, PS(session_name), PS(id), 1) != SUCCESS) {
459 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not append session information");
460 }
461 }
462
463 /* treat params array with http_build_query() */
464 if (params) {
465 if (SUCCESS != http_urlencode_hash_ex(Z_ARRVAL_P(params), 0, NULL, 0, &query, &query_len)) {
466 RETURN_FALSE;
467 }
468 }
469
470 URI = http_absolute_uri(url, NULL);
471 if (query_len) {
472 snprintf(LOC, HTTP_URI_MAXLEN + sizeof("Location: "), "Location: %s?%s", URI, query);
473 sprintf(RED, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI, query, URI, query);
474 efree(query);
475 } else {
476 snprintf(LOC, HTTP_URI_MAXLEN + sizeof("Location: "), "Location: %s", URI);
477 sprintf(RED, "Redirecting to <a href=\"%s\">%s</a>.\n", URI, URI);
478 }
479 efree(URI);
480
481 if ((SUCCESS == http_send_header(LOC)) && (SUCCESS == http_send_status((permanent ? 301 : 302)))) {
482 php_body_write(RED, strlen(RED) TSRMLS_CC);
483 RETURN_TRUE;
484 }
485 RETURN_FALSE;
486 }
487 /* }}} */
488
489 /* {{{ proto bool http_send_data(string data)
490 *
491 * Sends raw data with support for (multiple) range requests.
492 *
493 */
494 PHP_FUNCTION(http_send_data)
495 {
496 zval *zdata;
497
498 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zdata) != SUCCESS) {
499 RETURN_FALSE;
500 }
501
502 convert_to_string_ex(&zdata);
503 http_send_header("Accept-Ranges: bytes");
504 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata), Z_STRLEN_P(zdata)));
505 }
506 /* }}} */
507
508 /* {{{ proto bool http_send_file(string file)
509 *
510 * Sends a file with support for (multiple) range requests.
511 *
512 */
513 PHP_FUNCTION(http_send_file)
514 {
515 char *file;
516 int flen = 0;
517
518 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &file, &flen) != SUCCESS) {
519 RETURN_FALSE;
520 }
521 if (!flen) {
522 RETURN_FALSE;
523 }
524
525 http_send_header("Accept-Ranges: bytes");
526 RETURN_SUCCESS(http_send_file(file));
527 }
528 /* }}} */
529
530 /* {{{ proto bool http_send_stream(resource stream)
531 *
532 * Sends an already opened stream with support for (multiple) range requests.
533 *
534 */
535 PHP_FUNCTION(http_send_stream)
536 {
537 zval *zstream;
538 php_stream *file;
539
540 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream) != SUCCESS) {
541 RETURN_FALSE;
542 }
543
544 php_stream_from_zval(file, &zstream);
545 http_send_header("Accept-Ranges: bytes");
546 RETURN_SUCCESS(http_send_stream(file));
547 }
548 /* }}} */
549
550 /* {{{ proto string http_chunked_decode(string encoded)
551 *
552 * This function decodes a string that was HTTP-chunked encoded.
553 * Returns false on failure.
554 */
555 PHP_FUNCTION(http_chunked_decode)
556 {
557 char *encoded = NULL, *decoded = NULL;
558 int encoded_len = 0, decoded_len = 0;
559
560 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &encoded, &encoded_len) != SUCCESS) {
561 RETURN_FALSE;
562 }
563
564 if (SUCCESS == http_chunked_decode(encoded, encoded_len, &decoded, &decoded_len)) {
565 RETURN_STRINGL(decoded, decoded_len, 0);
566 } else {
567 RETURN_FALSE;
568 }
569 }
570 /* }}} */
571
572 /* {{{ proto array http_split_response(string http_response)
573 *
574 * This function splits an HTTP response into an array with headers and the
575 * content body. The returned array may look simliar to the following example:
576 *
577 * <pre>
578 * <?php
579 * array(
580 * 0 => array(
581 * 'Status' => '200 Ok',
582 * 'Content-Type' => 'text/plain',
583
584 * 'Content-Language' => 'en-US'
585 * ),
586 * 1 => "Hello World!"
587 * );
588 * ?>
589 * </pre>
590 */
591 PHP_FUNCTION(http_split_response)
592 {
593 zval *zresponse, *zbody, *zheaders;
594
595 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zresponse) != SUCCESS) {
596 RETURN_FALSE;
597 }
598
599 convert_to_string_ex(&zresponse);
600
601 MAKE_STD_ZVAL(zbody);
602 MAKE_STD_ZVAL(zheaders);
603 array_init(zheaders);
604
605 if (SUCCESS != http_split_response(zresponse, zheaders, zbody)) {
606 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not parse HTTP response");
607 RETURN_FALSE;
608 }
609
610 array_init(return_value);
611 add_index_zval(return_value, 0, zheaders);
612 add_index_zval(return_value, 1, zbody);
613 }
614 /* }}} */
615
616 /* {{{ proto array http_parse_headers(string header)
617 *
618 */
619 PHP_FUNCTION(http_parse_headers)
620 {
621 char *header, *rnrn;
622 int header_len;
623
624 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &header, &header_len)) {
625 RETURN_FALSE;
626 }
627
628 array_init(return_value);
629
630 if (rnrn = strstr(header, HTTP_CRLF HTTP_CRLF)) {
631 header_len = rnrn - header + 2;
632 }
633 if (SUCCESS != http_parse_headers(header, header_len, return_value)) {
634 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not parse HTTP header");
635 zval_dtor(return_value);
636 RETURN_FALSE;
637 }
638 }
639 /* }}}*/
640
641 /* {{{ proto array http_get_request_headers(void)
642 *
643 */
644 PHP_FUNCTION(http_get_request_headers)
645 {
646 if (ZEND_NUM_ARGS()) {
647 WRONG_PARAM_COUNT;
648 }
649
650 array_init(return_value);
651 http_get_request_headers(return_value);
652 }
653 /* }}} */
654
655 /* {{{ HAVE_CURL */
656 #ifdef HTTP_HAVE_CURL
657
658 /* {{{ proto string http_get(string url[, array options[, array &info]])
659 *
660 * Performs an HTTP GET request on the supplied url.
661 *
662 * The second parameter is expected to be an associative
663 * array where the following keys will be recognized:
664 * <pre>
665 * - redirect: int, whether and how many redirects to follow
666 * - unrestrictedauth: bool, whether to continue sending credentials on
667 * redirects to a different host
668 * - proxyhost: string, proxy host in "host[:port]" format
669 * - proxyport: int, use another proxy port as specified in proxyhost
670 * - proxyauth: string, proxy credentials in "user:pass" format
671 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
672 * - httpauth: string, http credentials in "user:pass" format
673 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
674 * - compress: bool, whether to allow gzip/deflate content encoding
675 * (defaults to true)
676 * - port: int, use another port as specified in the url
677 * - referer: string, the referer to sends
678 * - useragent: string, the user agent to send
679 * (defaults to PECL::HTTP/version (PHP/version)))
680 * - headers: array, list of custom headers as associative array
681 * like array("header" => "value")
682 * - cookies: array, list of cookies as associative array
683 * like array("cookie" => "value")
684 * - cookiestore: string, path to a file where cookies are/will be stored
685 * </pre>
686 *
687 * The optional third parameter will be filled with some additional information
688 * in form af an associative array, if supplied, like the following example:
689 * <pre>
690 * <?php
691 * array (
692 * 'effective_url' => 'http://localhost',
693 * 'response_code' => 403,
694 * 'total_time' => 0.017,
695 * 'namelookup_time' => 0.013,
696 * 'connect_time' => 0.014,
697 * 'pretransfer_time' => 0.014,
698 * 'size_upload' => 0,
699 * 'size_download' => 202,
700 * 'speed_download' => 11882,
701 * 'speed_upload' => 0,
702 * 'header_size' => 145,
703 * 'request_size' => 62,
704 * 'ssl_verifyresult' => 0,
705 * 'filetime' => -1,
706 * 'content_length_download' => 202,
707 * 'content_length_upload' => 0,
708 * 'starttransfer_time' => 0.017,
709 * 'content_type' => 'text/html; charset=iso-8859-1',
710 * 'redirect_time' => 0,
711 * 'redirect_count' => 0,
712 * 'private' => '',
713 * 'http_connectcode' => 0,
714 * 'httpauth_avail' => 0,
715 * 'proxyauth_avail' => 0,
716 * )
717 * ?>
718 * </pre>
719 */
720 PHP_FUNCTION(http_get)
721 {
722 char *URL, *data = NULL;
723 size_t data_len = 0;
724 int URL_len;
725 zval *options = NULL, *info = NULL;
726
727 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
728 RETURN_FALSE;
729 }
730
731 if (info) {
732 zval_dtor(info);
733 array_init(info);
734 }
735
736 if (SUCCESS == http_get(URL, HASH_ORNULL(options), HASH_ORNULL(info), &data, &data_len)) {
737 RETURN_STRINGL(data, data_len, 0);
738 } else {
739 RETURN_FALSE;
740 }
741 }
742 /* }}} */
743
744 /* {{{ proto string http_head(string url[, array options[, array &info]])
745 *
746 * Performs an HTTP HEAD request on the suppied url.
747 * Returns the HTTP response as string.
748 * See http_get() for a full list of available options.
749 */
750 PHP_FUNCTION(http_head)
751 {
752 char *URL, *data = NULL;
753 size_t data_len = 0;
754 int URL_len;
755 zval *options = NULL, *info = NULL;
756
757 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
758 RETURN_FALSE;
759 }
760
761 if (info) {
762 zval_dtor(info);
763 array_init(info);
764 }
765
766 if (SUCCESS == http_head(URL, HASH_ORNULL(options), HASH_ORNULL(info), &data, &data_len)) {
767 RETURN_STRINGL(data, data_len, 0);
768 } else {
769 RETURN_FALSE;
770 }
771 }
772 /* }}} */
773
774 /* {{{ proto string http_post_data(string url, string data[, array options[, &info]])
775 *
776 * Performs an HTTP POST request, posting data.
777 * Returns the HTTP response as string.
778 * See http_get() for a full list of available options.
779 */
780 PHP_FUNCTION(http_post_data)
781 {
782 char *URL, *postdata, *data = NULL;
783 size_t data_len = 0;
784 int postdata_len, URL_len;
785 zval *options = NULL, *info = NULL;
786
787 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &postdata, &postdata_len, &options, &info) != SUCCESS) {
788 RETURN_FALSE;
789 }
790
791 if (info) {
792 zval_dtor(info);
793 array_init(info);
794 }
795
796 if (SUCCESS == http_post_data(URL, postdata, (size_t) postdata_len, HASH_ORNULL(options), HASH_ORNULL(info), &data, &data_len)) {
797 RETURN_STRINGL(data, data_len, 0);
798 } else {
799 RETURN_FALSE;
800 }
801 }
802 /* }}} */
803
804 /* {{{ proto string http_post_array(string url, array data[, array options[, array &info]])
805 *
806 * Performs an HTTP POST request, posting www-form-urlencoded array data.
807 * Returns the HTTP response as string.
808 * See http_get() for a full list of available options.
809 */
810 PHP_FUNCTION(http_post_array)
811 {
812 char *URL, *data = NULL;
813 size_t data_len = 0;
814 int URL_len;
815 zval *options = NULL, *info = NULL, *postdata;
816
817 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa|a/!z", &URL, &URL_len, &postdata, &options, &info) != SUCCESS) {
818 RETURN_FALSE;
819 }
820
821 if (info) {
822 zval_dtor(info);
823 array_init(info);
824 }
825
826 if (SUCCESS == http_post_array(URL, Z_ARRVAL_P(postdata), HASH_ORNULL(options), HASH_ORNULL(info), &data, &data_len)) {
827 RETURN_STRINGL(data, data_len, 0);
828 } else {
829 RETURN_FALSE;
830 }
831 }
832 /* }}} */
833
834 #endif
835 /* }}} HAVE_CURL */
836
837
838 /* {{{ proto bool http_auth_basic(string user, string pass[, string realm = "Restricted"])
839 *
840 * Example:
841 * <pre>
842 * <?php
843 * if (!http_auth_basic('mike', 's3c|r3t')) {
844 * die('<h1>Authorization failed!</h1>');
845 * }
846 * ?>
847 * </pre>
848 */
849 PHP_FUNCTION(http_auth_basic)
850 {
851 char *realm = NULL, *user, *pass, *suser, *spass;
852 int r_len, u_len, p_len;
853
854 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &user, &u_len, &pass, &p_len, &realm, &r_len) != SUCCESS) {
855 RETURN_FALSE;
856 }
857
858 if (!realm) {
859 realm = "Restricted";
860 }
861
862 if (SUCCESS != http_auth_credentials(&suser, &spass)) {
863 http_auth_header("Basic", realm);
864 RETURN_FALSE;
865 }
866
867 if (strcasecmp(suser, user)) {
868 http_auth_header("Basic", realm);
869 RETURN_FALSE;
870 }
871
872 if (strcmp(spass, pass)) {
873 http_auth_header("Basic", realm);
874 RETURN_FALSE;
875 }
876
877 RETURN_TRUE;
878 }
879 /* }}} */
880
881 /* {{{ proto bool http_auth_basic_cb(mixed callback[, string realm = "Restricted"])
882 *
883 * Example:
884 * <pre>
885 * <?php
886 * function auth_cb($user, $pass)
887 * {
888 * global $db;
889 * $query = 'SELECT pass FROM users WHERE user='. $db->quoteSmart($user);
890 * if (strlen($realpass = $db->getOne($query)) {
891 * return $pass === $realpass;
892 * }
893 * return false;
894 * }
895 *
896 * if (!http_auth_basic_cb('auth_cb')) {
897 * die('<h1>Authorization failed</h1>');
898 * }
899 * ?>
900 * </pre>
901 */
902 PHP_FUNCTION(http_auth_basic_cb)
903 {
904 zval *cb;
905 char *realm = NULL, *user, *pass;
906 int r_len;
907
908 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|s", &cb, &realm, &r_len) != SUCCESS) {
909 RETURN_FALSE;
910 }
911
912 if (!realm) {
913 realm = "Restricted";
914 }
915
916 if (SUCCESS != http_auth_credentials(&user, &pass)) {
917 http_auth_header("Basic", realm);
918 RETURN_FALSE;
919 }
920 {
921 zval *zparams[2] = {NULL, NULL}, retval;
922 int result = 0;
923
924 MAKE_STD_ZVAL(zparams[0]);
925 MAKE_STD_ZVAL(zparams[1]);
926 ZVAL_STRING(zparams[0], user, 0);
927 ZVAL_STRING(zparams[1], pass, 0);
928
929 if (SUCCESS == call_user_function(EG(function_table), NULL, cb,
930 &retval, 2, zparams TSRMLS_CC)) {
931 result = Z_LVAL(retval);
932 }
933
934 efree(user);
935 efree(pass);
936 efree(zparams[0]);
937 efree(zparams[1]);
938
939 if (!result) {
940 http_auth_header("Basic", realm);
941 }
942
943 RETURN_BOOL(result);
944 }
945 }
946 /* }}}*/
947
948 /* {{{ Sara Golemons http_build_query() */
949 #ifndef ZEND_ENGINE_2
950
951 /* {{{ proto string http_build_query(mixed formdata [, string prefix])
952 Generates a form-encoded query string from an associative array or object. */
953 PHP_FUNCTION(http_build_query)
954 {
955 zval *formdata;
956 char *prefix = NULL;
957 int prefix_len = 0;
958 smart_str formstr = {0};
959
960 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|s", &formdata, &prefix, &prefix_len) != SUCCESS) {
961 RETURN_FALSE;
962 }
963
964 if (Z_TYPE_P(formdata) != IS_ARRAY && Z_TYPE_P(formdata) != IS_OBJECT) {
965 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Parameter 1 expected to be Array or Object. Incorrect value given.");
966 RETURN_FALSE;
967 }
968
969 if (php_url_encode_hash_ex(HASH_OF(formdata), &formstr, prefix, prefix_len, NULL, 0, NULL, 0, (Z_TYPE_P(formdata) == IS_OBJECT ? formdata : NULL) TSRMLS_CC) == FAILURE) {
970 if (formstr.c) {
971 efree(formstr.c);
972 }
973 RETURN_FALSE;
974 }
975
976 if (!formstr.c) {
977 RETURN_NULL();
978 }
979
980 smart_str_0(&formstr);
981
982 RETURN_STRINGL(formstr.c, formstr.len, 0);
983 }
984 /* }}} */
985 #endif /* !ZEND_ENGINE_2 */
986 /* }}} */
987
988 /*
989 * Local variables:
990 * tab-width: 4
991 * c-basic-offset: 4
992 * End:
993 * vim600: noet sw=4 ts=4 fdm=marker
994 * vim<600: noet sw=4 ts=4
995 */
996