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