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