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