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