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