1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by: Jens |
4
|
|
|
* Date: 27-3-2018 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace CloudControl\Cms\cc; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class ResponseHeaders |
11
|
|
|
{ |
12
|
|
|
const HEADER_ACCESS_CONTROL_ALLOW_ORIGIN = 'Access-Control-Allow-Origin'; |
13
|
|
|
const HEADER_ACCESS_CONTROL_ALLOW_ORIGIN_CONTENT = '*'; |
14
|
|
|
const HEADER_CACHE_CONTROL = 'Cache-Control'; |
15
|
|
|
const HEADER_CACHE_CONTROL_CONTENT_NO_STORE_NO_CACHE_MUST_REVALIDATE_MAX_AGE_0 = 'no-store, no-cache, must-revalidate, max-age=0'; |
16
|
|
|
const HEADER_CONNECTION = 'Connection'; |
17
|
|
|
const HEADER_CONNECTION_CONTENT_KEEP_ALIVE = 'Keep-Alive'; |
18
|
|
|
const HEADER_CONTENT_DESCRIPTION = 'Content-Description'; |
19
|
|
|
const HEADER_CONTENT_DESCRIPTION_CONTENT = 'File Transfer'; |
20
|
|
|
const HEADER_CONTENT_DISPOSITION = 'Content-Disposition'; |
21
|
|
|
const HEADER_CONTENT_LENGTH = 'Content-Length'; |
22
|
|
|
const HEADER_CONTENT_SECURITY_POLICY = 'Content-Security-Policy'; |
23
|
|
|
const HEADER_CONTENT_SECURITY_POLICY_CONTENT_INSECURE = 'default-src \'self\' https: \'unsafe-inline\' \'unsafe-eval\''; |
24
|
|
|
const HEADER_CONTENT_SECURITY_POLICY_CONTENT_LOCALHOST = 'default-src * \'unsafe-inline\' \'unsafe-eval\' data: blob:'; |
25
|
|
|
const HEADER_CONTENT_SECURITY_POLICY_CONTENT_SECURE = 'default-src https: \'unsafe-inline\' \'unsafe-eval\''; |
26
|
|
|
const HEADER_CONTENT_TRANSFER_ENCODING = 'Content-Transfer-Encoding'; |
27
|
|
|
const HEADER_CONTENT_TRANSFER_ENCODING_CONTENT_BINARY = 'binary'; |
28
|
|
|
const HEADER_CONTENT_TYPE = 'Content-Type'; |
29
|
|
|
const HEADER_CONTENT_TYPE_CONTENT_APPLICATION_JSON = 'application/json'; |
30
|
|
|
const HEADER_CONTENT_TYPE_CONTENT_TEXT_HTML = 'text/html'; |
31
|
|
|
const HEADER_EXPIRES = 'Expires'; |
32
|
|
|
const HEADER_PRAGMA = 'Pragma'; |
33
|
|
|
const HEADER_PRAGMA_CONTENT_CACHE = 'cache'; |
34
|
|
|
const HEADER_PRAGMA_CONTENT_NO_CACHE = 'no-cache'; |
35
|
|
|
const HEADER_PRAGMA_CONTENT_PUBLIC = 'public'; |
36
|
|
|
const HEADER_REFERRER_POLICY = 'Referrer-Policy'; |
37
|
|
|
const HEADER_REFERRER_POLICY_CONTENT = 'strict-origin-when-cross-origin'; |
38
|
|
|
const HEADER_SET_COOKIE = 'Set-Cookie'; |
39
|
|
|
const HEADER_STRICT_TRANSPORT_SECURITY = 'Strict-Transport-Security'; |
40
|
|
|
const HEADER_STRICT_TRANSPORT_SECURITY_CONTENT = 'max-age=31536000'; |
41
|
|
|
const HEADER_X_CONTENT_SECURITY_POLICY = 'X-Content-Security-Policy'; // For IE |
42
|
|
|
const HEADER_X_CONTENT_TYPE_OPTIONS = 'X-Content-Type-Options'; |
43
|
|
|
const HEADER_X_CONTENT_TYPE_OPTIONS_CONTENT = 'nosniff;'; |
44
|
|
|
const HEADER_X_FRAME_OPTIONS = 'X-Frame-Options: '; |
45
|
|
|
const HEADER_X_FRAME_OPTIONS_CONTENT = 'SAMEORIGIN'; |
46
|
|
|
const HEADER_X_POWERED_BY = 'X-Powered-By'; |
47
|
|
|
const HEADER_X_POWERED_BY_CONTENT = 'Cloud Control - https://getcloudcontrol.org'; |
48
|
|
|
const HEADER_X_XSS_PROTECTION = 'X-XSS-Protection'; |
49
|
|
|
const HEADER_X_XSS_PROTECTION_CONTENT = '1; mode=block'; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Default headers |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
protected static $headers = array( |
56
|
|
|
self::HEADER_ACCESS_CONTROL_ALLOW_ORIGIN => self::HEADER_ACCESS_CONTROL_ALLOW_ORIGIN_CONTENT, |
57
|
|
|
self::HEADER_CONTENT_TYPE => self::HEADER_CONTENT_TYPE_CONTENT_TEXT_HTML, |
58
|
|
|
self::HEADER_REFERRER_POLICY => self::HEADER_REFERRER_POLICY_CONTENT, |
59
|
|
|
self::HEADER_STRICT_TRANSPORT_SECURITY => self::HEADER_STRICT_TRANSPORT_SECURITY_CONTENT, |
60
|
|
|
self::HEADER_X_CONTENT_TYPE_OPTIONS => self::HEADER_X_CONTENT_TYPE_OPTIONS_CONTENT, |
61
|
|
|
self::HEADER_X_FRAME_OPTIONS => self::HEADER_X_FRAME_OPTIONS_CONTENT, |
62
|
|
|
self::HEADER_X_POWERED_BY => self::HEADER_X_POWERED_BY_CONTENT, |
63
|
|
|
self::HEADER_X_XSS_PROTECTION => self::HEADER_X_XSS_PROTECTION_CONTENT, |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
protected static $initialized = false; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* ResponseHeaders constructor. |
70
|
|
|
*/ |
71
|
|
|
public function __construct() |
72
|
|
|
{ |
73
|
|
|
self::init(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Adds content security policy headers |
78
|
|
|
*/ |
79
|
|
|
public static function init() |
80
|
|
|
{ |
81
|
|
|
self::add(self::HEADER_SET_COOKIE, '__Host-sess=' . session_id() . '; path=' . Request::$subfolders . '; Secure; HttpOnly; SameSite;'); |
82
|
|
|
if (Request::isSecure()) { |
83
|
|
|
self::add(self::HEADER_CONTENT_SECURITY_POLICY, self::HEADER_CONTENT_SECURITY_POLICY_CONTENT_SECURE); |
84
|
|
|
self::add(self::HEADER_STRICT_TRANSPORT_SECURITY, self::HEADER_STRICT_TRANSPORT_SECURITY_CONTENT); |
85
|
|
|
self::add(self::HEADER_X_CONTENT_SECURITY_POLICY, self::HEADER_CONTENT_SECURITY_POLICY_CONTENT_SECURE); |
86
|
|
|
} elseif (Request::isLocalhost()) { |
87
|
|
|
self::add(self::HEADER_CONTENT_SECURITY_POLICY, self::HEADER_CONTENT_SECURITY_POLICY_CONTENT_LOCALHOST); |
88
|
|
|
self::add(self::HEADER_X_CONTENT_SECURITY_POLICY, self::HEADER_CONTENT_SECURITY_POLICY_CONTENT_LOCALHOST); |
89
|
|
|
} else { |
90
|
|
|
self::add(self::HEADER_CONTENT_SECURITY_POLICY, self::HEADER_CONTENT_SECURITY_POLICY_CONTENT_INSECURE); |
91
|
|
|
self::add(self::HEADER_X_CONTENT_SECURITY_POLICY, self::HEADER_CONTENT_SECURITY_POLICY_CONTENT_INSECURE); |
92
|
|
|
} |
93
|
|
|
self::$initialized = true; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param $headerName |
98
|
|
|
* @param $headerContent |
99
|
|
|
*/ |
100
|
|
|
public static function add($headerName, $headerContent) |
101
|
|
|
{ |
102
|
|
|
self::$headers[$headerName] = $headerContent; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param $headerName |
107
|
|
|
*/ |
108
|
|
|
public static function delete($headerName) |
109
|
|
|
{ |
110
|
|
|
if (isset(self::$headers[$headerName])) { |
111
|
|
|
unset(self::$headers[$headerName]); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
|
|
public static function getHeaders() |
119
|
|
|
{ |
120
|
|
|
return self::$headers; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public static function sendAllHeaders() |
124
|
|
|
{ |
125
|
|
|
if (self::$initialized === false) { |
126
|
|
|
self::init(); |
127
|
|
|
} |
128
|
|
|
foreach (self::$headers as $headerName => $headerContent) { |
129
|
|
|
header($headerName . ': ' . $headerContent); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |