+/* {{{ proto object http_parse_cookie(string cookie[, int flags[, array allowed_extras]])
+ *
+ * Parses HTTP cookies like sent in a response into a struct.
+ *
+ * Expects a string as parameter containing the value of a Set-Cookie response header.
+ *
+ * Returns an stdClass olike shown in the example on success or FALSE on failure.
+ *
+ * Example:
+ * <pre>
+ * <?php
+ * print_r(http_parse_cookie("foo=bar; bar=baz; path=/; domain=example.com; comment=; secure", 0, array("comment")));
+ *
+ * stdClass Object
+ * (
+ * [cookies] => Array
+ * (
+ * [foo] => bar
+ * [bar] => baz
+ * )
+ *
+ * [extras] => Array
+ * (
+ * [comment] =>
+ * )
+ *
+ * [flags] => 16
+ * [expires] => 0
+ * [path] => /
+ * [domain] => example.com
+ * )
+ * ?>
+ * </pre>
+ */
+PHP_FUNCTION(http_parse_cookie)
+{
+ char *cookie, **allowed_extras = NULL;
+ int i = 0, cookie_len;
+ long flags = 0;
+ zval *allowed_extras_array = NULL, **entry = NULL;
+ HashPosition pos;
+ http_cookie_list list;
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|la!", &cookie, &cookie_len, &flags, &allowed_extras_array)) {
+ RETURN_FALSE;
+ }
+
+ if (allowed_extras_array) {
+ allowed_extras = ecalloc(zend_hash_num_elements(Z_ARRVAL_P(allowed_extras_array)) + 1, sizeof(char *));
+ FOREACH_VAL(pos, allowed_extras_array, entry) {
+ ZVAL_ADDREF(*entry);
+ convert_to_string_ex(entry);
+ allowed_extras[i++] = estrndup(Z_STRVAL_PP(entry), Z_STRLEN_PP(entry));
+ zval_ptr_dtor(entry);
+ }
+ }
+
+ if (http_parse_cookie_ex(&list, cookie, flags, allowed_extras)) {
+ object_init(return_value);
+ http_cookie_list_tostruct(&list, return_value);
+ http_cookie_list_dtor(&list);
+ } else {
+ RETVAL_FALSE;
+ }
+
+ if (allowed_extras) {
+ for (i = 0; allowed_extras[i]; ++i) {
+ efree(allowed_extras[i]);
+ }
+ efree(allowed_extras);
+ }
+}
+/* }}} */
+
+/* {{{ proto string http_build_cookie(array cookie)
+ *
+ * Build a cookie string from an array/object like returned by http_parse_cookie().
+ */
+PHP_FUNCTION(http_build_cookie)
+{
+ char *str = NULL;
+ size_t len = 0;
+ zval *strct;
+ http_cookie_list list;
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &strct)) {
+ RETURN_FALSE;
+ }
+
+ http_cookie_list_fromstruct(&list, strct);
+ http_cookie_list_tostring(&list, &str, &len);
+ http_cookie_list_dtor(&list);
+
+ RETURN_STRINGL(str, len, 0);
+}
+/* }}} */
+
+/* {{{ proto object http_parse_params(string param[, int flags = HTTP_PARAMS_DEFAULT])
+ *
+ * Parse parameter list.
+ */
+PHP_FUNCTION(http_parse_params)
+{
+ char *param;
+ int param_len;
+ zval *params;
+ long flags = HTTP_PARAMS_DEFAULT;
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", ¶m, ¶m_len, &flags)) {
+ RETURN_FALSE;
+ }
+
+ params = ecalloc(1, sizeof(zval));
+ array_init(params);
+ if (SUCCESS != http_parse_params(param, flags, Z_ARRVAL_P(params))) {
+ zval_dtor(params);
+ FREE_ZVAL(params);
+ RETURN_FALSE;
+ }
+ object_init(return_value);
+ add_property_zval(return_value, "params", params);
+}
+/* }}} */
+