-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathHeaderUtils.class.php
More file actions
191 lines (158 loc) · 4.09 KB
/
HeaderUtils.class.php
File metadata and controls
191 lines (158 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
/***************************************************************************
* Copyright (C) 2004-2008 by Konstantin V. Arkhipov *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of the *
* License, or (at your option) any later version. *
* *
***************************************************************************/
/**
* Collection of static header functions.
*
* @ingroup Http
**/
final class HeaderUtils extends StaticFactory
{
private static $headerSent = false;
private static $redirectSent = false;
private static $cacheLifeTime = 3600;
private static $headers = array();
public static function redirectRaw($url)
{
header("Location: {$url}");
self::$headerSent = true;
self::$redirectSent = true;
}
public static function redirectBack()
{
if (isset($_SERVER['HTTP_REFERER'])) {
header("Location: {$_SERVER['HTTP_REFERER']}");
self::$headerSent = true;
self::$redirectSent = true;
return $_SERVER['HTTP_REFERER'];
}
return false;
}
public static function getRequestHeaderList()
{
if (!empty(self::$headers))
return self::$headers;
if (function_exists('apache_request_headers')) {
self::$headers = apache_request_headers();
} else {
foreach($_SERVER as $key => $value) {
if (substr($key, 0, 5) == "HTTP_") {
$name = self::extractHeader($key, "_", 5);
self::$headers[$name] = $value;
}
}
}
return self::$headers;
}
public static function getRequestHeader($name)
{
$name = self::extractHeader($name, "-", 0);
$list = self::getRequestHeaderList();
if (isset($list[$name]))
return $list[$name];
return null;
}
public static function getParsedURI(/* ... */)
{
if ($num = func_num_args()) {
$out = self::getURI();
$uri = null;
$arr = func_get_args();
for ($i = 0; $i < $num; ++$i)
unset($out[$arr[$i]]);
foreach ($out as $key => $val) {
if (is_array($val)) {
foreach ($val as $k => $v)
$uri .= "&{$key}[{$k}]={$v}";
} else
$uri .= "&{$key}={$val}";
}
return $uri;
}
return null;
}
public static function sendCachedHeader()
{
header('Cache-control: private, max-age=3600');
header(
'Expires: '
.date('D, d M Y H:i:s', date('U') + self::$cacheLifeTime)
.' GMT'
);
self::$headerSent = true;
}
public static function sendNotCachedHeader()
{
header('Cache-control: no-cache');
header(
'Expires: '
.date('D, d M Y H:i:s', date('U') - self::$cacheLifeTime)
.' GMT'
);
self::$headerSent = true;
}
public static function sendContentLength($length)
{
Assert::isInteger($length);
header("Content-Length: {$length}");
self::$headerSent = true;
}
public static function sendHttpStatus(HttpStatus $status)
{
header($status->toString());
self::$headerSent = true;
}
public static function isHeaderSent()
{
return self::$headerSent;
}
public static function forceHeaderSent()
{
self::$headerSent = true;
}
public static function isRedirectSent()
{
return self::$redirectSent;
}
public static function setCacheLifeTime($cacheLifeTime)
{
self::$cacheLifeTime = $cacheLifeTime;
}
public static function getCacheLifeTime()
{
return self::$cacheLifeTime;
}
private static function getURI()
{
$out = null;
parse_str($_SERVER['QUERY_STRING'], $out);
return $out;
}
private static function extractHeader($name, $delimiter, $length)
{
return
str_replace(
" ",
"-",
ucwords(
strtolower(
str_replace(
$delimiter,
" ",
$length
? substr($name, $length)
: $name
)
)
)
);
}
}
?>