- zval *supported;
- char *def = NULL;
- int def_len = 0;
-
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|s", &supported, &def, &def_len) != SUCCESS) {
- RETURN_FALSE;
- }
-
- if (!def) {
- def = "en-US";
- }
-
- RETURN_STRING(http_negotiate_language(supported, def), 0);
-}
-/* }}} */
-
-/* {{{ proto string http_negotiate_charset(array supported[, string default = 'iso-8859-1'])
- *
- * This function negotiates the clients preferred charset based on its
- * Accept-Charset HTTP header. It returns the negotiated charset or
- * the default charset if none match.
- *
- * The qualifier is recognized and charset without qualifier are rated highest.
- *
- * The supported parameter is expected to be an array having
- * the supported charsets as array values.
- *
- * Example:
- * <pre>
- * <?php
- * $charsets = array(
- * 'iso-8859-1', // default
- * 'iso-8859-2',
- * 'iso-8859-15',
- * 'utf-8'
- * );
- * $pref = http_negotiate_charset($charsets);
- * if (!strcmp($pref, 'iso-8859-1')) {
- * iconv_set_encoding('internal_encoding', 'iso-8859-1');
- * iconv_set_encoding('output_encoding', $pref);
- * ob_start('ob_iconv_handler');
- * }
- * ?>
- * </pre>
- */
-PHP_FUNCTION(http_negotiate_charset)
-{
- zval *supported;
- char *def = NULL;
- int def_len = 0;
-
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|s", &supported, &def, &def_len) != SUCCESS) {
- RETURN_FALSE;
- }
-
- if (!def) {
- def = "iso-8859-1";
- }
-
- RETURN_STRING(http_negotiate_charset(supported, def), 0);
-}
-/* }}} */
-
-/* {{{ proto bool http_send_status(int status)
- *
- * Send HTTP status code.
- *
- */
-PHP_FUNCTION(http_send_status)
-{
- int status = 0;
-
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &status) != SUCCESS) {
- RETURN_FALSE;
- }
- if (status < 100 || status > 510) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid HTTP status code (100-510): %d", status);
- RETURN_FALSE;
- }
-
- RETURN_SUCCESS(http_send_status(status));
-}
-/* }}} */
-
-/* {{{ proto bool http_send_last_modified([int timestamp])
- *
- * This converts the given timestamp to a valid HTTP date and
- * sends it as "Last-Modified" HTTP header. If timestamp is
- * omitted, current time is sent.
- *
- */
-PHP_FUNCTION(http_send_last_modified)
-{
- long t = -1;
-
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
- RETURN_FALSE;
- }
-
- if (t == -1) {
- t = (long) time(NULL);
- }
-
- RETURN_SUCCESS(http_send_last_modified(t));
-}
-/* }}} */
-
-/* {{{ proto bool http_match_modified([int timestamp])
- *
- * Matches the given timestamp against the clients "If-Modified-Since" resp.
- * "If-Unmodified-Since" HTTP headers.
- *
- */
-PHP_FUNCTION(http_match_modified)
-{
- long t = -1;
-
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
- RETURN_FALSE;
- }
-
- // current time if not supplied (senseless though)
- if (t == -1) {
- t = (long) time(NULL);
- }
-
- RETURN_BOOL(http_modified_match("HTTP_IF_MODIFIED_SINCE", t) || http_modified_match("HTTP_IF_UNMODIFIED_SINCE", t));
-}
-/* }}} */
-
-/* {{{ proto bool http_match_etag(string etag)
- *
- * This matches the given ETag against the clients
- * "If-Match" resp. "If-None-Match" HTTP headers.
- *
- */
-PHP_FUNCTION(http_match_etag)
-{
- int etag_len;
- char *etag;
-
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &etag, &etag_len) != SUCCESS) {
- RETURN_FALSE;
- }
-
- RETURN_BOOL(http_etag_match("HTTP_IF_NONE_MATCH", etag) || http_etag_match("HTTP_IF_MATCH", etag));
-}
-/* }}} */
-
-/* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
- *
- * If timestamp_or_exires is greater than 0, it is handled as timestamp
- * and will be sent as date of last modification. If it is 0 or omitted,
- * the current time will be sent as Last-Modified date. If it's negative,
- * it is handled as expiration time in seconds, which means that if the
- * requested last modification date is not between the calculated timespan,
- * the Last-Modified header is updated and the actual body will be sent.
- *
- */
-PHP_FUNCTION(http_cache_last_modified)
-{
- long last_modified = 0, send_modified = 0, t;
- zval *zlm;
-
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &last_modified) != SUCCESS) {
- RETURN_FALSE;
- }
-
- t = (long) time(NULL);
-
- /* 0 or omitted */
- if (!last_modified) {
- /* does the client have? (att: caching "forever") */
- if (zlm = http_get_server_var("HTTP_IF_MODIFIED_SINCE")) {
- last_modified = send_modified = http_parse_date(Z_STRVAL_P(zlm));
- /* send current time */
- } else {
- send_modified = t;
- }
- /* negative value is supposed to be expiration time */
- } else if (last_modified < 0) {
- last_modified += t;
- send_modified = t;
- /* send supplied time explicitly */
- } else {
- send_modified = last_modified;
- }
-
- http_send_header("Cache-Control: private, must-revalidate, max-age=0");
-
- if (http_modified_match("HTTP_IF_MODIFIED_SINCE", last_modified)) {
- if (SUCCESS == http_send_status(304)) {
- zend_bailout();
- } else {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not send 304 Not Modified");
- RETURN_FALSE;
- }
- }
- RETURN_SUCCESS(http_send_last_modified(send_modified));