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