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