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