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