* fixing PHP5 snap build
[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 "snprintf.h"
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"
28
29 #include "php_http.h"
30 #include "php_http_api.h"
31
32 #if defined(HAVE_CURL) && HAVE_CURL
33 # ifdef PHP_WIN32
34 # include <winsock2.h>
35 # include <sys/types.h>
36 # endif
37 #include <curl/curl.h>
38 #endif
39
40 ZEND_DECLARE_MODULE_GLOBALS(http)
41
42 #ifdef COMPILE_DL_HTTP
43 ZEND_GET_MODULE(http)
44 #endif
45
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)
71 #endif
72 PHP_FE(http_auth_basic, NULL)
73 PHP_FE(http_auth_basic_cb, NULL)
74 {NULL, NULL, NULL}
75 };
76 /* }}} */
77
78 /* {{{ http_module_entry */
79 zend_module_entry http_module_entry = {
80 #if ZEND_MODULE_API_NO >= 20010901
81 STANDARD_MODULE_HEADER,
82 #endif
83 "http",
84 http_functions,
85 PHP_MINIT(http),
86 NULL,
87 NULL,
88 PHP_RSHUTDOWN(http),
89 PHP_MINFO(http),
90 #if ZEND_MODULE_API_NO >= 20010901
91 PHP_EXT_HTTP_VERSION,
92 #endif
93 STANDARD_MODULE_PROPERTIES
94 };
95 /* }}} */
96
97 #define RETURN_SUCCESS(v) RETURN_BOOL(SUCCESS == (v))
98 #define HASH_ORNULL(z) ((z) ? Z_ARRVAL_P(z) : NULL)
99
100 /* {{{ proto string http_date([int timestamp])
101 *
102 * This function returns a valid HTTP date regarding RFC 822/1123
103 * looking like: "Wed, 22 Dec 2004 11:34:47 GMT"
104 *
105 */
106 PHP_FUNCTION(http_date)
107 {
108 long t = -1;
109
110 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
111 RETURN_FALSE;
112 }
113
114 if (t == -1) {
115 t = (long) time(NULL);
116 }
117
118 RETURN_STRING(http_date(t), 0);
119 }
120 /* }}} */
121
122 /* {{{ proto string http_absolute_uri(string url[, string proto])
123 *
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.
130 *
131 * Some examples:
132 * <pre>
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
136 * </pre>
137 *
138 */
139 PHP_FUNCTION(http_absolute_uri)
140 {
141 char *url = NULL, *proto = NULL;
142 int url_len = 0, proto_len = 0;
143
144 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &url, &url_len, &proto, &proto_len) != SUCCESS) {
145 RETURN_FALSE;
146 }
147
148 RETURN_STRING(http_absolute_uri(url, proto), 0);
149 }
150 /* }}} */
151
152 /* {{{ proto string http_negotiate_language(array supported[, string default = 'en-US'])
153 *
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.
157 *
158 * The qualifier is recognized and languages without qualifier are rated highest.
159 *
160 * The supported parameter is expected to be an array having
161 * the supported languages as array values.
162 *
163 * Example:
164 * <pre>
165 * <?php
166 * $langs = array(
167 * 'en-US',// default
168 * 'fr',
169 * 'fr-FR',
170 * 'de',
171 * 'de-DE',
172 * 'de-AT',
173 * 'de-CH',
174 * );
175 * include './langs/'. http_negotiate_language($langs) .'.php';
176 * ?>
177 * </pre>
178 *
179 */
180 PHP_FUNCTION(http_negotiate_language)
181 {
182 zval *supported;
183 char *def = NULL;
184 int def_len = 0;
185
186 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|s", &supported, &def, &def_len) != SUCCESS) {
187 RETURN_FALSE;
188 }
189
190 if (!def) {
191 def = "en-US";
192 }
193
194 RETURN_STRING(http_negotiate_language(supported, def), 0);
195 }
196 /* }}} */
197
198 /* {{{ proto string http_negotiate_charset(array supported[, string default = 'iso-8859-1'])
199 *
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.
203 *
204 * The qualifier is recognized and charset without qualifier are rated highest.
205 *
206 * The supported parameter is expected to be an array having
207 * the supported charsets as array values.
208 *
209 * Example:
210 * <pre>
211 * <?php
212 * $charsets = array(
213 * 'iso-8859-1', // default
214 * 'iso-8859-2',
215 * 'iso-8859-15',
216 * 'utf-8'
217 * );
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');
223 * }
224 * ?>
225 * </pre>
226 */
227 PHP_FUNCTION(http_negotiate_charset)
228 {
229 zval *supported;
230 char *def = NULL;
231 int def_len = 0;
232
233 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|s", &supported, &def, &def_len) != SUCCESS) {
234 RETURN_FALSE;
235 }
236
237 if (!def) {
238 def = "iso-8859-1";
239 }
240
241 RETURN_STRING(http_negotiate_charset(supported, def), 0);
242 }
243 /* }}} */
244
245 /* {{{ proto bool http_send_status(int status)
246 *
247 * Send HTTP status code.
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 sent.
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" resp.
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" resp. "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 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.
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? (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 */
358 } else {
359 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 php_end_ob_buffers(0 TSRMLS_CC);
405 http_send_header("Cache-Control: private, must-revalidate, max-age=0");
406
407 /* if no etag is given and we didn't already
408 * start ob_etaghandler -- start it
409 */
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));
414 }
415
416 if (http_etag_match("HTTP_IF_NONE_MATCH", etag)) {
417 if (SUCCESS == http_send_status(304)) {
418 zend_bailout();
419 } else {
420 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not send 304 Not Modified");
421 RETURN_FALSE;
422 }
423 }
424
425 RETURN_SUCCESS(http_send_etag(etag, etag_len));
426 }
427 /* }}} */
428
429 /* {{{ proto void http_redirect([string url[, array params[, bool session,[ bool permanent]]]])
430 *
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.
435 *
436 * Depending on permanent the redirection will be issued with a permanent
437 * ("301 Moved Permanently") or a temporary ("302 Found") redirection
438 * status code.
439 *
440 * To be RFC compliant, "Redirecting to <a>URI</a>." will be displayed,
441 * if the client doesn't redirect immediatly.
442 */
443 PHP_FUNCTION(http_redirect)
444 {
445 int url_len;
446 zend_bool session = 0, permanent = 0;
447 zval *params = NULL;
448 smart_str qstr = {0};
449 char *url, *URI, LOC[HTTP_URI_MAXLEN + 9], RED[HTTP_URI_MAXLEN * 2 + 34];
450
451 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sa!/bb", &url, &url_len, &params, &session, &permanent) != SUCCESS) {
452 RETURN_FALSE;
453 }
454
455 /* append session info */
456 if (session && (PS(session_status) == php_session_active)) {
457 if (!params) {
458 MAKE_STD_ZVAL(params);
459 array_init(params);
460 }
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");
463 }
464 }
465
466 /* treat params array with http_build_query() */
467 if (params) {
468 if (php_url_encode_hash_ex(Z_ARRVAL_P(params), &qstr, NULL,0,NULL,0,NULL,0,NULL TSRMLS_CC) != SUCCESS) {
469 if (qstr.c) {
470 efree(qstr.c);
471 }
472 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not encode query parameters");
473 RETURN_FALSE;
474 }
475 smart_str_0(&qstr);
476 }
477
478 URI = http_absolute_uri(url, NULL);
479 if (qstr.c) {
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);
482 efree(qstr.c);
483 } else {
484 snprintf(LOC, HTTP_URI_MAXLEN + strlen("Location: "), "Location: %s", URI);
485 sprintf(RED, "Redirecting to <a href=\"%s\">%s</a>.\n", URI, URI);
486 }
487 efree(URI);
488
489 if ((SUCCESS == http_send_header(LOC)) && (SUCCESS == http_send_status((permanent ? 301 : 302)))) {
490 php_body_write(RED, strlen(RED) TSRMLS_CC);
491 RETURN_TRUE;
492 }
493 RETURN_FALSE;
494 }
495 /* }}} */
496
497 /* {{{ proto bool http_send_data(string data)
498 *
499 * Sends raw data with support for (multiple) range requests.
500 *
501 */
502 PHP_FUNCTION(http_send_data)
503 {
504 zval *zdata;
505
506 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zdata) != SUCCESS) {
507 RETURN_FALSE;
508 }
509
510 convert_to_string_ex(&zdata);
511 http_send_header("Accept-Ranges: bytes");
512 RETURN_SUCCESS(http_send_data(zdata));
513 }
514 /* }}} */
515
516 /* {{{ proto bool http_send_file(string file)
517 *
518 * Sends a file with support for (multiple) range requests.
519 *
520 */
521 PHP_FUNCTION(http_send_file)
522 {
523 zval *zfile;
524
525 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zfile) != SUCCESS) {
526 RETURN_FALSE;
527 }
528
529 convert_to_string_ex(&zfile);
530 http_send_header("Accept-Ranges: bytes");
531 RETURN_SUCCESS(http_send_file(zfile));
532 }
533 /* }}} */
534
535 /* {{{ proto bool http_send_stream(resource stream)
536 *
537 * Sends an already opened stream with support for (multiple) range requests.
538 *
539 */
540 PHP_FUNCTION(http_send_stream)
541 {
542 zval *zstream;
543 php_stream *file;
544
545 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream) != SUCCESS) {
546 RETURN_FALSE;
547 }
548
549 php_stream_from_zval(file, &zstream);
550 http_send_header("Accept-Ranges: bytes");
551 RETURN_SUCCESS(http_send_stream(file));
552 }
553 /* }}} */
554
555 /* {{{ proto bool http_content_type([string content_type = 'application/x-octetstream'])
556 *
557 * Sets the content type.
558 *
559 */
560 PHP_FUNCTION(http_content_type)
561 {
562 char *ct, *content_type;
563 int ct_len = 0;
564
565 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &ct, &ct_len) != SUCCESS) {
566 RETURN_FALSE;
567 }
568
569 if (!ct_len) {
570 RETURN_SUCCESS(http_send_header("Content-Type: application/x-octetstream"));
571 }
572
573 /* remember for multiple ranges */
574 if (HTTP_G(ctype)) {
575 efree(HTTP_G(ctype));
576 }
577 HTTP_G(ctype) = estrndup(ct, ct_len);
578
579 content_type = (char *) emalloc(strlen("Content-Type: ") + ct_len + 1);
580 sprintf(content_type, "Content-Type: %s", ct);
581
582 RETVAL_BOOL(SUCCESS == http_send_header(content_type));
583 efree(content_type);
584 }
585 /* }}} */
586
587 /* {{{ proto bool http_content_disposition(string filename[, bool inline = false])
588 *
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).
592 *
593 */
594 PHP_FUNCTION(http_content_disposition)
595 {
596 char *filename, *header;
597 int f_len;
598 zend_bool send_inline = 0;
599
600 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &filename, &f_len, &send_inline) != SUCCESS) {
601 RETURN_FALSE;
602 }
603
604 if (send_inline) {
605 header = (char *) emalloc(strlen("Content-Disposition: inline; filename=\"\"") + f_len + 1);
606 sprintf(header, "Content-Disposition: inline; filename=\"%s\"", filename);
607 } else {
608 header = (char *) emalloc(strlen("Content-Disposition: attachment; filename=\"\"") + f_len + 1);
609 sprintf(header, "Content-Disposition: attachment; filename=\"%s\"", filename);
610 }
611
612 RETVAL_BOOL(SUCCESS == http_send_header(header));
613 efree(header);
614 }
615 /* }}} */
616
617 /* {{{ proto string http_chunked_decode(string encoded)
618 *
619 * This function decodes a string that was HTTP-chunked encoded.
620 * Returns false on failure.
621 */
622 PHP_FUNCTION(http_chunked_decode)
623 {
624 char *encoded = NULL, *decoded = NULL;
625 int encoded_len = 0, decoded_len = 0;
626
627 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &encoded, &encoded_len) != SUCCESS) {
628 RETURN_FALSE;
629 }
630
631 if (SUCCESS == http_chunked_decode(encoded, encoded_len, &decoded, &decoded_len)) {
632 RETURN_STRINGL(decoded, decoded_len, 0);
633 } else {
634 RETURN_FALSE;
635 }
636 }
637 /* }}} */
638
639 /* {{{ proto array http_split_response(string http_response)
640 *
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:
643 *
644 * <pre>
645 * array(
646 * 0 => array(
647 * 'Status' => '200 Ok',
648 * 'Content-Type' => 'text/plain',
649 * 'Content-Language' => 'en-US'
650 * ),
651 * 1 => "Hello World!"
652 * );
653 * </pre>
654 */
655 PHP_FUNCTION(http_split_response)
656 {
657 zval *zresponse, *zbody, *zheaders;
658
659 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zresponse) != SUCCESS) {
660 RETURN_FALSE;
661 }
662
663 convert_to_string_ex(&zresponse);
664
665 MAKE_STD_ZVAL(zbody);
666 MAKE_STD_ZVAL(zheaders);
667 array_init(zheaders);
668
669 http_split_response(zresponse, zheaders, zbody);
670
671 array_init(return_value);
672 add_index_zval(return_value, 0, zheaders);
673 add_index_zval(return_value, 1, zbody);
674 }
675 /* }}} */
676
677 /* {{{ HAVE_CURL */
678 #if defined(HAVE_CURL) && HAVE_CURL
679
680 /* {{{ proto string http_get(string url[, array options[, array &info]])
681 *
682 * Performs an HTTP GET request on the supplied url.
683 *
684 * The second parameter is expected to be an associative
685 * array where the following keys will be recognized:
686 * <pre>
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
697 * (defaults to true)
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
707 * </pre>
708 *
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()).
712 */
713 PHP_FUNCTION(http_get)
714 {
715 char *URL, *data = NULL;
716 size_t data_len = 0;
717 int URL_len;
718 zval *options = NULL, *info = NULL;
719
720 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
721 RETURN_FALSE;
722 }
723
724 if (info) {
725 zval_dtor(info);
726 array_init(info);
727 }
728
729 if (SUCCESS == http_get(URL, HASH_ORNULL(options), HASH_ORNULL(info), &data, &data_len)) {
730 RETURN_STRINGL(data, data_len, 0);
731 } else {
732 RETURN_FALSE;
733 }
734 }
735 /* }}} */
736
737 /* {{{ proto string http_head(string url[, array options[, array &info]])
738 *
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.
742 */
743 PHP_FUNCTION(http_head)
744 {
745 char *URL, *data = NULL;
746 size_t data_len = 0;
747 int URL_len;
748 zval *options = NULL, *info = NULL;
749
750 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
751 RETURN_FALSE;
752 }
753
754 if (info) {
755 zval_dtor(info);
756 array_init(info);
757 }
758
759 if (SUCCESS == http_head(URL, HASH_ORNULL(options), HASH_ORNULL(info), &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[, &info]])
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, *info = NULL;
779
780 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &postdata, &postdata_len, &options, &info) != SUCCESS) {
781 RETURN_FALSE;
782 }
783
784 if (info) {
785 zval_dtor(info);
786 array_init(info);
787 }
788
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);
791 } else {
792 RETURN_FALSE;
793 }
794 }
795 /* }}} */
796
797 /* {{{ proto string http_post_array(string url, array data[, array options[, array &info]])
798 *
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.
802 */
803 PHP_FUNCTION(http_post_array)
804 {
805 char *URL, *data = NULL;
806 size_t data_len = 0;
807 int URL_len;
808 zval *options = NULL, *info = NULL, *postdata;
809
810 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa|a/!z", &URL, &URL_len, &postdata, &options, &info) != SUCCESS) {
811 RETURN_FALSE;
812 }
813
814 if (info) {
815 zval_dtor(info);
816 array_init(info);
817 }
818
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);
821 } else {
822 RETURN_FALSE;
823 }
824 }
825 /* }}} */
826
827 #endif
828 /* }}} HAVE_CURL */
829
830
831 /* {{{ proto bool http_auth_basic(string user, string pass[, string realm = "Restricted"])
832 *
833 * Example:
834 * <pre>
835 * <?php
836 * if (!http_auth_basic('mike', 's3c|r3t')) {
837 * die('<h1>Authorization failed!</h1>');
838 * }
839 * ?>
840 * </pre>
841 */
842 PHP_FUNCTION(http_auth_basic)
843 {
844 char *realm = NULL, *user, *pass, *suser, *spass;
845 int r_len, u_len, p_len;
846
847 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &user, &u_len, &pass, &p_len, &realm, &r_len) != SUCCESS) {
848 RETURN_FALSE;
849 }
850
851 if (!realm) {
852 realm = "Restricted";
853 }
854
855 if (SUCCESS != http_auth_credentials(&suser, &spass)) {
856 http_auth_header("Basic", realm);
857 RETURN_FALSE;
858 }
859
860 if (strcasecmp(suser, user)) {
861 http_auth_header("Basic", realm);
862 RETURN_FALSE;
863 }
864
865 if (strcmp(spass, pass)) {
866 http_auth_header("Basic", realm);
867 RETURN_FALSE;
868 }
869
870 RETURN_TRUE;
871 }
872 /* }}} */
873
874 /* {{{ proto bool http_auth_basic_cb(mixed callback[, string realm = "Restricted"])
875 *
876 * Example:
877 * <pre>
878 * <?php
879 * function auth_cb($user, $pass)
880 * {
881 * global $db;
882 * $query = 'SELECT pass FROM users WHERE user='. $db->quoteSmart($user);
883 * if (strlen($realpass = $db->getOne($query)) {
884 * return $pass === $realpass;
885 * }
886 * return false;
887 * }
888 *
889 * if (!http_auth_basic_cb('auth_cb')) {
890 * die('<h1>Authorization failed</h1>');
891 * }
892 * ?>
893 * </pre>
894 */
895 PHP_FUNCTION(http_auth_basic_cb)
896 {
897 zval *cb;
898 char *realm = NULL, *user, *pass;
899 int r_len;
900
901 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|s", &cb, &realm, &r_len) != SUCCESS) {
902 RETURN_FALSE;
903 }
904
905 if (!realm) {
906 realm = "Restricted";
907 }
908
909 if (SUCCESS != http_auth_credentials(&user, &pass)) {
910 http_auth_header("Basic", realm);
911 RETURN_FALSE;
912 }
913 {
914 zval *zparams[2] = {NULL, NULL}, retval;
915 int result = 0;
916
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);
921
922 if (SUCCESS == call_user_function(EG(function_table), NULL, cb,
923 &retval, 2, zparams TSRMLS_CC)) {
924 result = Z_LVAL(retval);
925 }
926
927 efree(user);
928 efree(pass);
929 efree(zparams[0]);
930 efree(zparams[1]);
931
932 if (!result) {
933 http_auth_header("Basic", realm);
934 }
935
936 RETURN_BOOL(result);
937 }
938 }
939 /* }}}*/
940
941
942 /* {{{ php_http_init_globals(zend_http_globals *) */
943 static void php_http_init_globals(zend_http_globals *http_globals)
944 {
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;
956 #endif
957 }
958 /* }}} */
959
960 /* {{{ PHP_MINIT_FUNCTION */
961 PHP_MINIT_FUNCTION(http)
962 {
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);
968 #endif
969 return SUCCESS;
970 }
971 /* }}} */
972
973 /* {{{ PHP_RSHUTDOWN_FUNCTION */
974 PHP_RSHUTDOWN_FUNCTION(http)
975 {
976 if (HTTP_G(ctype)) {
977 efree(HTTP_G(ctype));
978 }
979 if (HTTP_G(etag)) {
980 efree(HTTP_G(etag));
981 }
982 #if defined(HAVE_CURL) && HAVE_CURL
983 if (HTTP_G(curlbuf).body.data) {
984 efree(HTTP_G(curlbuf).body.data);
985 }
986 if (HTTP_G(curlbuf).hdrs.data) {
987 efree(HTTP_G(curlbuf).hdrs.data);
988 }
989 #endif
990 return SUCCESS;
991 }
992 /* }}} */
993
994 /* {{{ PHP_MINFO_FUNCTION */
995 PHP_MINFO_FUNCTION(http)
996 {
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
1002 "enabled"
1003 #else
1004 "disabled"
1005 #endif
1006 );
1007 php_info_print_table_end();
1008 }
1009 /* }}} */
1010
1011 /*
1012 * Local variables:
1013 * tab-width: 4
1014 * c-basic-offset: 4
1015 * End:
1016 * vim600: noet sw=4 ts=4 fdm=marker
1017 * vim<600: noet sw=4 ts=4
1018 */
1019