-/* {{{ proto string http_absolute_uri(string url[, string proto[, string host[, int port]]])
- *
- * This function returns an absolute URI constructed from url.
- * If the url is already abolute but a different proto was supplied,
- * only the proto part of the URI will be updated. If url has no
- * path specified, the path of the current REQUEST_URI will be taken.
- * The host will be taken either from the Host HTTP header of the client
- * the SERVER_NAME or just localhost if prior are not available.
- *
- * Some examples:
- * <pre>
- * url = "page.php" => http://www.example.com/current/path/page.php
- * url = "/page.php" => http://www.example.com/page.php
- * url = "/page.php", proto = "https" => https://www.example.com/page.php
- * </pre>
- *
- */
-PHP_FUNCTION(http_absolute_uri)
-{
- char *url = NULL, *proto = NULL, *host = NULL;
- int url_len = 0, proto_len = 0, host_len = 0;
- long port = 0;
-
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ssl", &url, &url_len, &proto, &proto_len, &host, &host_len, &port) != SUCCESS) {
- RETURN_FALSE;
- }
-
- RETURN_STRING(http_absolute_uri_ex(url, url_len, proto, proto_len, host, host_len, port), 0);
-}
-/* }}} */
-
-/* {{{ proto string http_negotiate_language(array supported[, string default = 'en-US'])
- *
- * This function negotiates the clients preferred language based on its
- * Accept-Language HTTP header. It returns the negotiated language or
- * the default language if none match.
- *
- * The qualifier is recognized and languages without qualifier are rated highest.
- *
- * The supported parameter is expected to be an array having
- * the supported languages as array values.
- *
- * Example:
- * <pre>
- * <?php
- * $langs = array(
- * 'en-US',// default
- * 'fr',
- * 'fr-FR',
- * 'de',
- * 'de-DE',
- * 'de-AT',
- * 'de-CH',
- * );
- * include './langs/'. http_negotiate_language($langs) .'.php';
- * ?>
- * </pre>
- *
- */
+/* {{{ proto string http_build_url([mixed url[, mixed parts[, int flags = HTTP_URL_REPLACE|HTTP_URL_FROM_ENV[, array &new_url]]]])
+ Build an URL. */
+PHP_FUNCTION(http_build_url)
+{
+ char *url_str = NULL;
+ size_t url_len = 0;
+ long flags = HTTP_URL_REPLACE|HTTP_URL_FROM_ENV;
+ zval *z_old_url = NULL, *z_new_url = NULL, *z_composed_url = NULL;
+ php_url *old_url = NULL, *new_url = NULL, *composed_url = NULL;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z!/z!/lz", &z_old_url, &z_new_url, &flags, &z_composed_url) != SUCCESS) {
+ RETURN_FALSE;
+ }
+
+ if (z_new_url) {
+ if (Z_TYPE_P(z_new_url) == IS_ARRAY || Z_TYPE_P(z_new_url) == IS_OBJECT) {
+ new_url = http_url_from_struct(NULL, HASH_OF(z_new_url));
+ } else {
+ convert_to_string(z_new_url);
+ if (!(new_url = php_url_parse_ex(Z_STRVAL_P(z_new_url), Z_STRLEN_P(z_new_url)))) {
+ http_error_ex(HE_WARNING, HTTP_E_URL, "Could not parse URL (%s)", Z_STRVAL_P(z_new_url));
+ RETURN_FALSE;
+ }
+ }
+ }
+
+ if (z_old_url) {
+ if (Z_TYPE_P(z_old_url) == IS_ARRAY || Z_TYPE_P(z_old_url) == IS_OBJECT) {
+ old_url = http_url_from_struct(NULL, HASH_OF(z_old_url));
+ } else {
+ convert_to_string(z_old_url);
+ if (!(old_url = php_url_parse_ex(Z_STRVAL_P(z_old_url), Z_STRLEN_P(z_old_url)))) {
+ if (new_url) {
+ php_url_free(new_url);
+ }
+ http_error_ex(HE_WARNING, HTTP_E_URL, "Could not parse URL (%s)", Z_STRVAL_P(z_old_url));
+ RETURN_FALSE;
+ }
+ }
+ }
+
+ if (z_composed_url) {
+ http_build_url(flags, old_url, new_url, &composed_url, &url_str, &url_len);
+ http_url_tostruct(composed_url, z_composed_url);
+ php_url_free(composed_url);
+ } else {
+ http_build_url(flags, old_url, new_url, NULL, &url_str, &url_len);
+ }
+
+ if (new_url) {
+ php_url_free(new_url);
+ }
+ if (old_url) {
+ php_url_free(old_url);
+ }
+
+ RETURN_STRINGL(url_str, url_len, 0);
+}
+/* }}} */
+
+/* {{{ proto string http_build_str(array query [, string prefix[, string arg_separator]])
+ Opponent to parse_str(). */
+PHP_FUNCTION(http_build_str)
+{
+ zval *formdata;
+ char *prefix = NULL, *arg_sep = INI_STR("arg_separator.output");
+ int prefix_len = 0, arg_sep_len = strlen(arg_sep);
+ phpstr formstr;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|ss", &formdata, &prefix, &prefix_len, &arg_sep, &arg_sep_len) != SUCCESS) {
+ RETURN_FALSE;
+ }
+
+ if (!arg_sep_len) {
+ arg_sep = HTTP_URL_ARGSEP;
+ arg_sep_len = lenof(HTTP_URL_ARGSEP);
+ }
+
+ phpstr_init(&formstr);
+ if (SUCCESS != http_urlencode_hash_recursive(HASH_OF(formdata), &formstr, arg_sep, arg_sep_len, prefix, prefix_len)) {
+ RETURN_FALSE;
+ }
+
+ if (!formstr.used) {
+ phpstr_dtor(&formstr);
+ RETURN_NULL();
+ }
+
+ RETURN_PHPSTR_VAL(&formstr);
+}
+/* }}} */
+
+#define HTTP_DO_NEGOTIATE_DEFAULT(supported) \
+ { \
+ zval **value; \
+ \
+ zend_hash_internal_pointer_reset(Z_ARRVAL_P(supported)); \
+ if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(supported), (void *) &value)) { \
+ RETVAL_ZVAL(*value, 1, 0); \
+ } else { \
+ RETVAL_NULL(); \
+ } \
+ }
+
+#define HTTP_DO_NEGOTIATE_HANDLE_DEFAULT(supported, rs_array) \
+ HTTP_DO_NEGOTIATE_DEFAULT(supported); \
+ if (rs_array) { \
+ HashPosition pos; \
+ zval **value_ptr; \
+ \
+ FOREACH_VAL(pos, supported, value_ptr) { \
+ zval *value = http_zsep(IS_STRING, *value_ptr); \
+ add_assoc_double(rs_array, Z_STRVAL_P(value), 1.0); \
+ zval_ptr_dtor(&value); \
+ } \
+ }
+
+#define HTTP_DO_NEGOTIATE_HANDLE_RESULT(result, supported, rs_array) \
+ { \
+ char *key; \
+ uint key_len; \
+ ulong idx; \
+ \
+ if (zend_hash_num_elements(result) && HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(result, &key, &key_len, &idx, 1, NULL)) { \
+ RETVAL_STRINGL(key, key_len-1, 0); \
+ } else { \
+ HTTP_DO_NEGOTIATE_DEFAULT(supported); \
+ } \
+ \
+ if (rs_array) { \
+ zend_hash_copy(Z_ARRVAL_P(rs_array), result, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *)); \
+ } \
+ \
+ zend_hash_destroy(result); \
+ FREE_HASHTABLE(result); \
+ }
+
+#define HTTP_DO_NEGOTIATE(type, supported, rs_array) \
+ { \
+ HashTable *result; \
+ if ((result = http_negotiate_ ##type(supported))) { \
+ HTTP_DO_NEGOTIATE_HANDLE_RESULT(result, supported, rs_array); \
+ } else { \
+ HTTP_DO_NEGOTIATE_HANDLE_DEFAULT(supported, rs_array); \
+ } \
+ }
+
+/* {{{ proto string http_negotiate_language(array supported[, array &result])
+ Negotiate the clients preferred language. */