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