From 7dfbe54c4e2d77bf390b130c5c3868e64517daf8 Mon Sep 17 00:00:00 2001 From: ozh Date: Wed, 1 Jan 2014 12:46:21 +0100 Subject: [PATCH] Filterable charset in content-type headers. Fixes #1533 --- admin/admin-ajax.php | 2 +- admin/index.php | 2 +- includes/functions-api.php | 7 ++++--- includes/functions-html.php | 19 ++++++++++++++++++- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/admin/admin-ajax.php b/admin/admin-ajax.php index c186196..ba31536 100644 --- a/admin/admin-ajax.php +++ b/admin/admin-ajax.php @@ -5,7 +5,7 @@ yourls_maybe_require_auth(); // This file will output a JSON string -header( 'Content-type: application/json' ); +yourls_content_type_header( 'application/json' ); if( !isset( $_REQUEST['action'] ) ) die(); diff --git a/admin/index.php b/admin/index.php index 3ad4090..5441ea0 100644 --- a/admin/index.php +++ b/admin/index.php @@ -157,7 +157,7 @@ if( isset( $_GET['jsonp'] ) && $_GET['jsonp'] == 'yourls' ) { $short = $return['shorturl'] ? $return['shorturl'] : ''; $message = $return['message']; - header( 'Content-type: application/json' ); + yourls_content_type_header( 'application/javascript' ); echo yourls_apply_filter( 'bookmarklet_jsonp', "yourls_callback({'short_url':'$short','message':'$message'});" ); die(); diff --git a/includes/functions-api.php b/includes/functions-api.php index 3d83d42..a918377 100644 --- a/includes/functions-api.php +++ b/includes/functions-api.php @@ -105,22 +105,23 @@ function yourls_api_output( $mode, $return ) { switch ( $mode ) { case 'jsonp': - header( 'Content-type: application/javascript' ); + yourls_content_type_header( 'application/javascript' ); echo $return['callback'] . '(' . json_encode( $return ) . ')'; break; case 'json': - header( 'Content-type: application/json' ); + yourls_content_type_header( 'application/json' ); echo json_encode( $return ); break; case 'xml': - header( 'Content-type: application/xml' ); + yourls_content_type_header( 'application/xml' ); echo yourls_xml_encode( $return ); break; case 'simple': default: + yourls_content_type_header( 'text/plain' ); if( isset( $simple ) ) echo $simple; break; diff --git a/includes/functions-html.php b/includes/functions-html.php index dff4c8d..329a96a 100644 --- a/includes/functions-html.php +++ b/includes/functions-html.php @@ -60,6 +60,7 @@ function yourls_html_head( $context = 'index', $title = '' ) { header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' ); header( 'Cache-Control: no-cache, must-revalidate, max-age=0' ); header( 'Pragma: no-cache' ); + yourls_content_type_header( yourls_apply_filters( 'html_head_content-type', 'text/html' ) ); yourls_do_action( 'admin_headers', $context, $title ); } @@ -82,7 +83,7 @@ function yourls_html_head( $context = 'index', $title = '' ) { <?php echo $title ?> - + @@ -890,3 +891,19 @@ function yourls_new_core_version_notice() { } } +/** + * Send a filerable content type header + * + * @since 1.7 + * @param string $type content type ('text/html', 'application/json', ...) + * @return bool whether header was sent + */ +function yourls_content_type_header( $type ) { + if( !headers_sent() ) { + $charset = yourls_apply_filters( 'content_type_header_charset', 'utf-8' ); + header( "Content-Type: $type; charset=$charset" ); + return true; + } + return false; +} + -- 2.45.0