-
Notifications
You must be signed in to change notification settings - Fork 14
/
http.php
206 lines (174 loc) · 5.65 KB
/
http.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
<?php
/**
* @package MTV
* @version 1.0
*/
namespace mtv\http;
use Exception,
BadFunctionCallException,
mtv\shortcuts;
global $url_patterns;
$url_patterns = array();
/**
* Match a url to the associated function, then call it
* Takes:
* $url - url to run on, probably $_REQUEST['path'] or something
* $url_patterns - url regexes and functions to pass them to
**/
function urlresolver( $kwargs ) {
extract( $kwargs );
if ( !isset($url) )
$url = get_default($_REQUEST, 'url', '');
if ( !isset($url_patterns) )
throw new BadFunctionCallException(__('url_patterns keyword argument required', 'mtv'));
try {
// Start buffering so we can toss output in case we get an exception
ob_start();
if ( resolve( $url, $url_patterns ) ) {
// everything worked! Flush the buffer and return
ob_end_flush();
return true;
} else
// We didn't find any matching patterns :( So... 404!
if (defined('DOING_AJAX') && DOING_AJAX)
throw new AjaxHttp404;
else
throw new Http404;
} catch (HttpException $e) {
ob_end_clean();
// Our view threw an HttpException, so display it
$e->display();
} catch (Exception $e) {
ob_end_clean();
// Somebody threw some sort of exception, so display 500
if (defined('DOING_AJAX') && DOING_AJAX)
$http_ex = new AjaxHttp500($e->getMessage(), $e->getCode(), $e);
else
$http_ex = new Http500($e->getMessage(), $e->getCode(), $e);
$http_ex->display();
}
return false; // We had some errors, so return false
}
function resolve($url, $url_patterns) {
// collapse multidimensional $url_patterns
$url_patterns = collapse_urls($url_patterns);
// cycle through our patterns in order to find a view to execute
foreach ($url_patterns as $pattern => $view) {
if ( preg_match($pattern, $url, $matches) > 0 ) {
// we found a match!
// Check to see if the function exists
if ( ! function_exists( $view ) ) throw new BadFunctionCallException(
sprintf(__("Can't find view function: %s", 'mtv'), $view));
// pass the match array to the view function
call_user_func( $view, array_slice($matches, 1) );
return true; // We're all done, so return
}
}
return false;
}
function include_urls_for($app_name) {
global $registered_apps;
if ( isset($registered_apps[$app_name]['urls']) ) {
include $registered_apps[$app_name]['urls'];
return $url_patterns;
} else
throw new Exception(sprintf(__("MTV App %s has no urls.php", 'mtv'), $app_name));
}
function collapse_urls($array) {
$collapse = array();
array_walk_recursive($array, function($value, $key) use (&$collapse) {
if ( !array_key_exists($key, $collapse) )
$collapse[$key] = $value;
});
return $collapse;
}
class HttpException extends Exception {
public $message = 'HTTP Error!';
public $code = null;
public $error_data;
public function __construct( $message=null, $error_data=null, $previous=null ) {
parent::__construct($message, $this->code, $previous);
$this->error_data = $error_data;
}
public function display_header() {
switch ( $this->code ) {
case '404':
header( "HTTP/1.1 404 Not Found" );
break;
default:
header( "HTTP/1.1 500 Internal Server Error" );
}
}
public function display_message() {
print( $this->message );
}
public function display() {
$this->display_header();
$this->display_message();
}
}
class Http404 extends HttpException {
public $code = '404';
public function display_message() {
global $wp_query;
$wp_query->is_404 = true;
if ( $this->getPrevious() )
$ex = $this->getPrevious();
else $ex = $this;
shortcuts\set_query_flags(array('page', '404'));
shortcuts\display_template(
'404.html',
array(
'exception_class' => get_class($ex),
'exception' => $ex,
'globals' => $GLOBALS,
'post' => $_POST,
'get' => $_GET,
'server' => $_SERVER
)
);
exit;
}
}
class Http500 extends HttpException {
public $code = '500';
public function display_message() {
shortcuts\set_query_flags('page');
if ( $this->getPrevious() )
$ex = $this->getPrevious();
else $ex = $this;
shortcuts\display_template(
'500.html',
array(
'exception_class' => get_class($ex),
'exception' => $ex,
'globals' => $GLOBALS,
'post' => $_POST,
'get' => $_GET,
'server' => $_SERVER
)
);
exit;
}
}
class AjaxHttp500 extends HttpException {
public $code = '500';
public function display_message() {
$response = array(
'error' => $this->message,
'trace' => $this->getTrace(),
'data' => $this->error_data
);
shortcuts\display_json($response);
}
}
class AjaxHttp404 extends HttpException {
public $code = '404';
public function display_message() {
$response = array(
'exception' => $this,
'error' => __('Callback not found', 'mtv'),
);
shortcuts\display_json($response);
}
}