forked from tedkulp/silk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.silk_response.php
234 lines (219 loc) · 6.73 KB
/
class.silk_response.php
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php // -*- mode:php; tab-width:4; indent-tabs-mode:t; c-basic-offset:4; -*-
// The MIT License
//
// Copyright (c) 2008 Ted Kulp
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
/**
* Static methods for handling web responses.
*
* @author Ted Kulp
* @since 1.0
**/
class SilkResponse extends SilkObject
{
function __construct()
{
parent::__construct();
}
function get_encoding()
{
return 'UTF-8';
}
/**
* Redirects the browser to the given url.
*
* @param string The url to redirect to
* @return void
* @author Ted Kulp
**/
public static function redirect($to)
{
$_SERVER['PHP_SELF'] = null;
$config = array();
/*
try
{
$config = cms_config();
}
catch (Exception $e)
{
}
*/
$schema = $_SERVER['SERVER_PORT'] == '443' ? 'https' : 'http';
$host = strlen($_SERVER['HTTP_HOST'])?$_SERVER['HTTP_HOST']:$_SERVER['SERVER_NAME'];
$components = parse_url($to);
if(count($components) > 0)
{
$to = (isset($components['scheme']) && starts_with($components['scheme'], 'http') ? $components['scheme'] : $schema) . '://';
$to .= isset($components['host']) ? $components['host'] : $host;
$to .= isset($components['port']) ? ':' . $components['port'] : '';
if(isset($components['path']))
{
if(in_array(substr($components['path'],0,1),array('\\','/')))//Path is absolute, just append.
{
$to .= $components['path'];
}
//Path is relative, append current directory first.
else if (isset($_SERVER['PHP_SELF']) && !is_null($_SERVER['PHP_SELF'])) //Apache
{
$to .= (strlen(dirname($_SERVER['PHP_SELF'])) > 1 ? dirname($_SERVER['PHP_SELF']).'/' : '/') . $components['path'];
}
else if (isset($_SERVER['REQUEST_URI']) && !is_null($_SERVER['REQUEST_URI'])) //Lighttpd
{
if (endswith($_SERVER['REQUEST_URI'], '/'))
$to .= (strlen($_SERVER['REQUEST_URI']) > 1 ? $_SERVER['REQUEST_URI'] : '/') . $components['path'];
else
$to .= (strlen(dirname($_SERVER['REQUEST_URI'])) > 1 ? dirname($_SERVER['REQUEST_URI']).'/' : '/') . $components['path'];
}
}
$to .= isset($components['query']) ? '?' . $components['query'] : '';
$to .= isset($components['fragment']) ? '#' . $components['fragment'] : '';
}
else
{
$to = $schema."://".$host."/".$to;
}
if (headers_sent() && !(isset($config) && $config['debug'] == true))
{
// use javascript instead
echo '<script type="text/javascript">
<!--
location.replace("'.$to.'");
// -->
</script>
<noscript>
<meta http-equiv="Refresh" content="0;URL='.$to.'">
</noscript>';
exit;
}
else
{
if (isset($config) && $config['debug'] == true)
{
echo "Debug is on. Redirecting disabled... Please click this link to continue.<br />";
echo "<a href=\"".$to."\">".$to."</a><br />";
echo '<pre>';
echo CmsProfiler::get_instance()->report();
echo '</pre>';
exit();
}
else
{
header("Location: $to");
exit();
}
}
}
/**
* Given a has of key/value pairs, generates and redirects to a URL
* for this application. Takes the same parameters as
* SilkResponse::create_url.
*
* @param array List of parameters used to create the url
* @return void
* @author Ted Kulp
**/
public static function redirect_to_action($params = array())
{
SilkResponse::redirect(SilkResponse::create_url($params));
}
/**
* Given a hash of key/value pairs, generate a URL for this application.
* It will try and select the best URL for the situation by first going
* through all the routes and seeing which is the best match. Then, any
* remaining parameters are put into the querystring.
*
* Given the following and assuming the default route list:
* @code
* create_url(array('controller' => 'user', 'action' => 'list', 'some_param' => '1'))
* @endcode
*
* Should generate:
* @code
* /user/list?some_param=1
* @endcode
*
* @param array List of parameters used to create the url
* @return string
* @author Ted Kulp
**/
public static function create_url($params = array())
{
$new_url = '';
foreach(SilkRoute::get_routes() as $one_route)
{
$route_params = SilkRoute::get_params_from_route($one_route->route_string);
$diff = array_diff($route_params, array_keys($params));
if (!count($diff))
{
//This is the first route that should work ok for the given parameters
//Even if it's short, we can add the rest on via the query string
$new_url = $one_route->route_string;
$similar = array_intersect($route_params, array_keys($params));
foreach ($similar as $one_param)
{
$new_url = str_replace(":{$one_param}", $params[$one_param], $new_url);
unset($params[$one_param]);
}
break;
}
}
if (count($params))
{
$new_url = $new_url . '?' . http_build_query($params, '', '&');
}
return SilkRequest::get_calculated_url_base(true, true) . $new_url;
}
/**
* Shows a very close approximation of an Apache generated 404 error.
* It also sends the actual header along as well, so that generic
* browser error pages (like what IE does) will be displayed.
*
* @return void
* @author Ted Kulp
**/
function send_error_404()
{
while (@ob_end_clean());
header("HTTP/1.0 404 Not Found");
header("Status: 404 Not Found");
echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
</body></html>';
exit();
}
/**
* Converts a string into a valid DOM id.
*
* @param string The string to be converted
* @return string The converted string
* @author Ted Kulp
**/
public static function make_dom_id($text)
{
return trim(preg_replace("/[^a-z0-9_\-]+/", '_', strtolower($text)), ' _');
}
}
# vim:ts=4 sw=4 noet
?>