Issue #10730: Use crypto_api for generating nonces and improve hashing
[mantis/radio.git] / core / http_api.php
blob5e4514ab31d4eeadce80835358406cd128c51b18
1 <?php
2 # MantisBT - A PHP based bugtracking system
4 # MantisBT is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 2 of the License, or
7 # (at your option) any later version.
9 # MantisBT is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * HTTP API
20 * Provides functions to manage HTTP response headers.
22 * @package CoreAPI
23 * @subpackage HTTPAPI
24 * @copyright Copyright (C) 2002 - 2010 MantisBT Team - mantisbt-dev@lists.sourceforge.net
25 * @link http://www.mantisbt.org
27 * @uses config_api.php
30 require_api( 'config_api.php' );
32 /**
33 * Check to see if the client is using Microsoft Internet Explorer so we can
34 * enable quirks and hacky non-standards-compliant workarounds.
35 * @return boolean True if Internet Explorer is detected as the user agent
37 function is_browser_internet_explorer() {
38 $t_user_agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : 'none';
40 if ( strpos( $t_user_agent, 'MSIE' ) ) {
41 return true;
44 return false;
47 /**
48 * Checks to see if the client is using Google Chrome so we can enable quirks
49 * and hacky non-standards-compliant workarounds.
50 * @return boolean True if Chrome is detected as the user agent
52 function is_browser_chrome() {
53 $t_user_agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : 'none';
55 if ( strpos( $t_user_agent, 'Chrome/' ) ) {
56 return true;
59 return false;
62 /**
63 * Send a Content-Disposition header. This is more complex than it sounds
64 * because only a few browsers properly support RFC2231. For those browsers
65 * which are behind the times or are otherwise broken, we need to use
66 * some hacky workarounds to get them to work 'nicely' with attachments and
67 * inline files. See http://greenbytes.de/tech/tc2231/ for full reasoning.
68 * @param string Filename
69 * @param boolean Display file inline (optional, default = treat as attachment)
71 function http_content_disposition_header( $p_filename, $p_inline = false ) {
72 if ( !headers_sent() ) {
73 $t_encoded_filename = rawurlencode( $p_filename );
74 $t_disposition = '';
75 if ( !$p_inline ) {
76 $t_disposition = 'attachment;';
78 if ( is_browser_internet_explorer() || is_browser_chrome() ) {
79 // Internet Explorer does not support RFC2231 however it does
80 // incorrectly decode URL encoded filenames and we can use this to
81 // get UTF8 filenames to work with the file download dialog. Chrome
82 // behaves in the same was as Internet Explorer in this respect.
83 // See http://greenbytes.de/tech/tc2231/#attwithfnrawpctenclong
84 header( 'Content-Disposition:' . $t_disposition . ' filename="' . $t_encoded_filename . '"' );
85 } else {
86 // For most other browsers, we can use this technique:
87 // http://greenbytes.de/tech/tc2231/#attfnboth2
88 header( 'Content-Disposition:' . $t_disposition . ' filename*=UTF-8\'\'' . $t_encoded_filename . '; filename="' . $t_encoded_filename . '"' );
93 /**
94 * Set caching headers that will allow or prevent browser caching.
95 * @param boolean Allow caching
97 function http_caching_headers( $p_allow_caching=false ) {
98 global $g_allow_browser_cache;
100 // Headers to prevent caching
101 // with option to bypass if running from script
102 if ( !headers_sent() ) {
103 if ( $p_allow_caching || ( isset( $g_allow_browser_cache ) && ON == $g_allow_browser_cache ) ) {
104 if ( is_browser_internet_explorer() ) {
105 header( 'Cache-Control: private, proxy-revalidate' );
106 } else {
107 header( 'Cache-Control: private, must-revalidate' );
109 } else {
110 header( 'Cache-Control: no-store, no-cache, must-revalidate' );
113 header( 'Expires: ' . gmdate( 'D, d M Y H:i:s \G\M\T', time() ) );
114 header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s \G\M\T', time() ) );
119 * Set content-type headers.
121 function http_content_headers() {
122 if ( !headers_sent() ) {
123 header( 'Content-type: text/html;charset=utf-8' );
128 * Load and set any custom headers defined by the site configuration.
130 function http_custom_headers() {
131 if ( !headers_sent() ) {
132 // send user-defined headers
133 foreach( config_get_global( 'custom_headers' ) as $t_header ) {
134 header( $t_header );
140 * Set all headers used by a normal page load.
142 function http_all_headers() {
143 global $g_bypass_headers;
145 if ( !$g_bypass_headers && !headers_sent() ) {
146 http_content_headers();
147 http_caching_headers();
148 http_custom_headers();