- unified cache_api function names
[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_curl_api.h"
37 #include "php_http_cache_api.h"
38 #include "php_http_curl_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_exires 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_redirect([string url[, array params[, bool session,[ bool permanent]]]])
415 *
416 * Redirect to a given url.
417 * The supplied url will be expanded with http_absolute_uri(), the params array will
418 * be treated with http_build_query() and the session identification will be appended
419 * if session is true.
420 *
421 * Depending on permanent the redirection will be issued with a permanent
422 * ("301 Moved Permanently") or a temporary ("302 Found") redirection
423 * status code.
424 *
425 * To be RFC compliant, "Redirecting to <a>URI</a>." will be displayed,
426 * if the client doesn't redirect immediatly.
427 */
428 PHP_FUNCTION(http_redirect)
429 {
430 int url_len;
431 size_t query_len = 0;
432 zend_bool session = 0, permanent = 0;
433 zval *params = NULL;
434 char *query, *url, *URI,
435 LOC[HTTP_URI_MAXLEN + sizeof("Location: ")],
436 RED[HTTP_URI_MAXLEN * 2 + sizeof("Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n")];
437
438 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sa!/bb", &url, &url_len, &params, &session, &permanent) != SUCCESS) {
439 RETURN_FALSE;
440 }
441
442 /* append session info */
443 if (session && (PS(session_status) == php_session_active)) {
444 if (!params) {
445 MAKE_STD_ZVAL(params);
446 array_init(params);
447 }
448 if (add_assoc_string(params, PS(session_name), PS(id), 1) != SUCCESS) {
449 http_error(E_WARNING, HTTP_E_ENCODE, "Could not append session information");
450 }
451 }
452
453 /* treat params array with http_build_query() */
454 if (params) {
455 if (SUCCESS != http_urlencode_hash_ex(Z_ARRVAL_P(params), 0, NULL, 0, &query, &query_len)) {
456 RETURN_FALSE;
457 }
458 }
459
460 URI = http_absolute_uri(url);
461
462 if (query_len) {
463 snprintf(LOC, HTTP_URI_MAXLEN + sizeof("Location: "), "Location: %s?%s", URI, query);
464 sprintf(RED, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI, query, URI, query);
465 efree(query);
466 } else {
467 snprintf(LOC, HTTP_URI_MAXLEN + sizeof("Location: "), "Location: %s", URI);
468 sprintf(RED, "Redirecting to <a href=\"%s\">%s</a>.\n", URI, URI);
469 }
470 efree(URI);
471
472 if ((SUCCESS == http_send_header(LOC)) && (SUCCESS == http_send_status((permanent ? 301 : 302)))) {
473 php_body_write(RED, strlen(RED) TSRMLS_CC);
474 RETURN_TRUE;
475 }
476 RETURN_FALSE;
477 }
478 /* }}} */
479
480 /* {{{ proto bool http_send_data(string data)
481 *
482 * Sends raw data with support for (multiple) range requests.
483 *
484 */
485 PHP_FUNCTION(http_send_data)
486 {
487 zval *zdata;
488
489 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zdata) != SUCCESS) {
490 RETURN_FALSE;
491 }
492
493 convert_to_string_ex(&zdata);
494 http_send_header("Accept-Ranges: bytes");
495 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata), Z_STRLEN_P(zdata)));
496 }
497 /* }}} */
498
499 /* {{{ proto bool http_send_file(string file)
500 *
501 * Sends a file with support for (multiple) range requests.
502 *
503 */
504 PHP_FUNCTION(http_send_file)
505 {
506 char *file;
507 int flen = 0;
508
509 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &file, &flen) != SUCCESS) {
510 RETURN_FALSE;
511 }
512 if (!flen) {
513 RETURN_FALSE;
514 }
515
516 http_send_header("Accept-Ranges: bytes");
517 RETURN_SUCCESS(http_send_file(file));
518 }
519 /* }}} */
520
521 /* {{{ proto bool http_send_stream(resource stream)
522 *
523 * Sends an already opened stream with support for (multiple) range requests.
524 *
525 */
526 PHP_FUNCTION(http_send_stream)
527 {
528 zval *zstream;
529 php_stream *file;
530
531 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream) != SUCCESS) {
532 RETURN_FALSE;
533 }
534
535 php_stream_from_zval(file, &zstream);
536 http_send_header("Accept-Ranges: bytes");
537 RETURN_SUCCESS(http_send_stream(file));
538 }
539 /* }}} */
540
541 /* {{{ proto string http_chunked_decode(string encoded)
542 *
543 * This function decodes a string that was HTTP-chunked encoded.
544 * Returns false on failure.
545 */
546 PHP_FUNCTION(http_chunked_decode)
547 {
548 char *encoded = NULL, *decoded = NULL;
549 int encoded_len = 0, decoded_len = 0;
550
551 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &encoded, &encoded_len) != SUCCESS) {
552 RETURN_FALSE;
553 }
554
555 if (SUCCESS == http_chunked_decode(encoded, encoded_len, &decoded, &decoded_len)) {
556 RETURN_STRINGL(decoded, decoded_len, 0);
557 } else {
558 RETURN_FALSE;
559 }
560 }
561 /* }}} */
562
563 /* {{{ proto array http_split_response(string http_response)
564 *
565 * This function splits an HTTP response into an array with headers and the
566 * content body. The returned array may look simliar to the following example:
567 *
568 * <pre>
569 * <?php
570 * array(
571 * 0 => array(
572 * 'Status' => '200 Ok',
573 * 'Content-Type' => 'text/plain',
574 * 'Content-Language' => 'en-US'
575 * ),
576 * 1 => "Hello World!"
577 * );
578 * ?>
579 * </pre>
580 */
581 PHP_FUNCTION(http_split_response)
582 {
583 zval *zresponse, *zbody, *zheaders;
584
585 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zresponse) != SUCCESS) {
586 RETURN_FALSE;
587 }
588
589 convert_to_string(zresponse);
590
591 MAKE_STD_ZVAL(zbody);
592 MAKE_STD_ZVAL(zheaders);
593 array_init(zheaders);
594
595 if (SUCCESS != http_split_response(zresponse, zheaders, zbody)) {
596 http_error(E_WARNING, HTTP_E_PARSE, "Could not parse HTTP response");
597 RETURN_FALSE;
598 }
599
600 array_init(return_value);
601 add_index_zval(return_value, 0, zheaders);
602 add_index_zval(return_value, 1, zbody);
603 }
604 /* }}} */
605
606 /* {{{ proto array http_parse_headers(string header)
607 *
608 */
609 PHP_FUNCTION(http_parse_headers)
610 {
611 char *header, *rnrn;
612 int header_len;
613
614 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &header, &header_len)) {
615 RETURN_FALSE;
616 }
617
618 array_init(return_value);
619
620 if (rnrn = strstr(header, HTTP_CRLF HTTP_CRLF)) {
621 header_len = rnrn - header + 2;
622 }
623 if (SUCCESS != http_parse_headers(header, header_len, return_value)) {
624 http_error(E_WARNING, HTTP_E_PARSE, "Could not parse HTTP headers");
625 zval_dtor(return_value);
626 RETURN_FALSE;
627 }
628 }
629 /* }}}*/
630
631 /* {{{ proto array http_get_request_headers(void)
632 *
633 */
634 PHP_FUNCTION(http_get_request_headers)
635 {
636 NO_ARGS;
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 * - resume: int, byte offset to start the download from;
674 * if the server supports ranges
675 * - maxfilesize: int, maximum file size that should be downloaded;
676 * has no effect, if the size of the requested entity is not known
677 * - lastmodified: int, timestamp for If-(Un)Modified-Since header
678 * - timeout: int, seconds the request may take
679 * - connecttimeout: int, seconds the connect may take
680 * </pre>
681 *
682 * The optional third parameter will be filled with some additional information
683 * in form af an associative array, if supplied, like the following example:
684 * <pre>
685 * <?php
686 * array (
687 * 'effective_url' => 'http://localhost',
688 * 'response_code' => 403,
689 * 'total_time' => 0.017,
690 * 'namelookup_time' => 0.013,
691 * 'connect_time' => 0.014,
692 * 'pretransfer_time' => 0.014,
693 * 'size_upload' => 0,
694 * 'size_download' => 202,
695 * 'speed_download' => 11882,
696 * 'speed_upload' => 0,
697 * 'header_size' => 145,
698 * 'request_size' => 62,
699 * 'ssl_verifyresult' => 0,
700 * 'filetime' => -1,
701 * 'content_length_download' => 202,
702 * 'content_length_upload' => 0,
703 * 'starttransfer_time' => 0.017,
704 * 'content_type' => 'text/html; charset=iso-8859-1',
705 * 'redirect_time' => 0,
706 * 'redirect_count' => 0,
707 * 'private' => '',
708 * 'http_connectcode' => 0,
709 * 'httpauth_avail' => 0,
710 * 'proxyauth_avail' => 0,
711 * )
712 * ?>
713 * </pre>
714 */
715 PHP_FUNCTION(http_get)
716 {
717 zval *options = NULL, *info = NULL;
718 char *URL;
719 int URL_len;
720 phpstr response;
721
722 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
723 RETURN_FALSE;
724 }
725
726 if (info) {
727 zval_dtor(info);
728 array_init(info);
729 }
730
731 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
732 if (SUCCESS == http_get(URL, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
733 RETURN_PHPSTR_VAL(response);
734 } else {
735 RETURN_FALSE;
736 }
737 }
738 /* }}} */
739
740 /* {{{ proto string http_head(string url[, array options[, array &info]])
741 *
742 * Performs an HTTP HEAD request on the suppied url.
743 * Returns the HTTP response as string.
744 * See http_get() for a full list of available options.
745 */
746 PHP_FUNCTION(http_head)
747 {
748 zval *options = NULL, *info = NULL;
749 char *URL;
750 int URL_len;
751 phpstr response;
752
753 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
754 RETURN_FALSE;
755 }
756
757 if (info) {
758 zval_dtor(info);
759 array_init(info);
760 }
761
762 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
763 if (SUCCESS == http_head(URL, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
764 RETURN_PHPSTR_VAL(response);
765 } else {
766 RETURN_FALSE;
767 }
768 }
769 /* }}} */
770
771 /* {{{ proto string http_post_data(string url, string data[, array options[, &info]])
772 *
773 * Performs an HTTP POST request, posting data.
774 * Returns the HTTP response as string.
775 * See http_get() for a full list of available options.
776 */
777 PHP_FUNCTION(http_post_data)
778 {
779 zval *options = NULL, *info = NULL;
780 char *URL, *postdata;
781 int postdata_len, URL_len;
782 phpstr response;
783
784 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &postdata, &postdata_len, &options, &info) != SUCCESS) {
785 RETURN_FALSE;
786 }
787
788 if (info) {
789 zval_dtor(info);
790 array_init(info);
791 }
792
793 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
794 if (SUCCESS == http_post_data(URL, postdata, (size_t) postdata_len, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
795 RETURN_PHPSTR_VAL(response);
796 } else {
797 RETURN_FALSE;
798 }
799 }
800 /* }}} */
801
802 /* {{{ proto string http_post_array(string url, array data[, array options[, array &info]])
803 *
804 * Performs an HTTP POST request, posting www-form-urlencoded array data.
805 * Returns the HTTP response as string.
806 * See http_get() for a full list of available options.
807 */
808 PHP_FUNCTION(http_post_array)
809 {
810 zval *options = NULL, *info = NULL, *postdata;
811 char *URL;
812 int URL_len;
813 phpstr response;
814
815 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa|a/!z", &URL, &URL_len, &postdata, &options, &info) != SUCCESS) {
816 RETURN_FALSE;
817 }
818
819 if (info) {
820 zval_dtor(info);
821 array_init(info);
822 }
823
824 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
825 if (SUCCESS == http_post_array(URL, Z_ARRVAL_P(postdata), options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
826 RETURN_PHPSTR_VAL(response);
827 } else {
828 RETURN_FALSE;
829 }
830 }
831 /* }}} */
832
833 #endif
834 /* }}} HAVE_CURL */
835
836
837 /* {{{ proto bool http_auth_basic(string user, string pass[, string realm = "Restricted"])
838 *
839 * Example:
840 * <pre>
841 * <?php
842 * if (!http_auth_basic('mike', 's3c|r3t')) {
843 * die('<h1>Authorization failed!</h1>');
844 * }
845 * ?>
846 * </pre>
847 */
848 PHP_FUNCTION(http_auth_basic)
849 {
850 char *realm = NULL, *user, *pass, *suser, *spass;
851 int r_len, u_len, p_len;
852
853 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &user, &u_len, &pass, &p_len, &realm, &r_len) != SUCCESS) {
854 RETURN_FALSE;
855 }
856
857 if (!realm) {
858 realm = "Restricted";
859 }
860
861 if (SUCCESS != http_auth_credentials(&suser, &spass)) {
862 http_auth_header("Basic", realm);
863 RETURN_FALSE;
864 }
865
866 if (strcasecmp(suser, user)) {
867 http_auth_header("Basic", realm);
868 RETURN_FALSE;
869 }
870
871 if (strcmp(spass, pass)) {
872 http_auth_header("Basic", realm);
873 RETURN_FALSE;
874 }
875
876 RETURN_TRUE;
877 }
878 /* }}} */
879
880 /* {{{ proto bool http_auth_basic_cb(mixed callback[, string realm = "Restricted"])
881 *
882 * Example:
883 * <pre>
884 * <?php
885 * function auth_cb($user, $pass)
886 * {
887 * global $db;
888 * $query = 'SELECT pass FROM users WHERE user='. $db->quoteSmart($user);
889 * if (strlen($realpass = $db->getOne($query)) {
890 * return $pass === $realpass;
891 * }
892 * return false;
893 * }
894 * if (!http_auth_basic_cb('auth_cb')) {
895 * die('<h1>Authorization failed</h1>');
896 * }
897 * ?>
898 * </pre>
899 */
900 PHP_FUNCTION(http_auth_basic_cb)
901 {
902 zval *cb;
903 char *realm = NULL, *user, *pass;
904 int r_len;
905
906 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|s", &cb, &realm, &r_len) != SUCCESS) {
907 RETURN_FALSE;
908 }
909
910 if (!realm) {
911 realm = "Restricted";
912 }
913
914 if (SUCCESS != http_auth_credentials(&user, &pass)) {
915 http_auth_header("Basic", realm);
916 RETURN_FALSE;
917 }
918 {
919 zval *zparams[2] = {NULL, NULL}, retval;
920 int result = 0;
921
922 MAKE_STD_ZVAL(zparams[0]);
923 MAKE_STD_ZVAL(zparams[1]);
924 ZVAL_STRING(zparams[0], user, 0);
925 ZVAL_STRING(zparams[1], pass, 0);
926
927 if (SUCCESS == call_user_function(EG(function_table), NULL, cb,
928 &retval, 2, zparams TSRMLS_CC)) {
929 result = Z_LVAL(retval);
930 }
931
932 efree(user);
933 efree(pass);
934 efree(zparams[0]);
935 efree(zparams[1]);
936
937 if (!result) {
938 http_auth_header("Basic", realm);
939 }
940
941 RETURN_BOOL(result);
942 }
943 }
944 /* }}}*/
945
946 /* {{{ Sara Golemons http_build_query() */
947 #ifndef ZEND_ENGINE_2
948
949 /* {{{ proto string http_build_query(mixed formdata [, string prefix[, string arg_separator]])
950 Generates a form-encoded query string from an associative array or object. */
951 PHP_FUNCTION(http_build_query)
952 {
953 zval *formdata;
954 char *prefix = NULL, *arg_sep = INI_STR("arg_separator.output");
955 int prefix_len = 0, arg_sep_len = strlen(arg_sep);
956 phpstr *formstr;
957
958 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|ss", &formdata, &prefix, &prefix_len, &arg_sep, &arg_sep_len) != SUCCESS) {
959 RETURN_FALSE;
960 }
961
962 if (Z_TYPE_P(formdata) != IS_ARRAY && Z_TYPE_P(formdata) != IS_OBJECT) {
963 http_error(E_WARNING, HTTP_E_PARAM, "Parameter 1 expected to be Array or Object. Incorrect value given.");
964 RETURN_FALSE;
965 }
966
967 if (!arg_sep_len) {
968 arg_sep = HTTP_URL_ARGSEP;
969 }
970
971 formstr = phpstr_new();
972 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))) {
973 phpstr_free(formstr);
974 RETURN_FALSE;
975 }
976
977 if (!formstr->used) {
978 phpstr_free(formstr);
979 RETURN_NULL();
980 }
981
982 RETURN_PHPSTR_PTR(formstr);
983 }
984 /* }}} */
985 #endif /* !ZEND_ENGINE_2 */
986 /* }}} */
987
988 PHP_FUNCTION(http_test)
989 {
990 }
991
992 /*
993 * Local variables:
994 * tab-width: 4
995 * c-basic-offset: 4
996 * End:
997 * vim600: noet sw=4 ts=4 fdm=marker
998 * vim<600: noet sw=4 ts=4
999 */
1000