Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 198 lines (165 sloc) 5.493 kb
62ea783 @ryanmark initial commit of pared down MTV
ryanmark authored
1 <?php
2 /**
3 * @package MTV
4 * @version 1.0
5 */
6
7 namespace mtv\http;
8 use Exception,
9 BadFunctionCallException,
10 mtv\shortcuts;
11
12 global $url_patterns;
13 $url_patterns = array();
14
15 /**
16 * Match a url to the associated function, then call it
17 * Takes:
18 * $url - url to run on, probably $_REQUEST['path'] or something
19 * $url_patterns - url regexes and functions to pass them to
20 **/
21 function urlresolver( $kwargs ) {
22 extract( $kwargs );
23
24 if ( !isset($url) )
25 $url = get_default($_REQUEST, 'url', '');
26
27 if ( !isset($url_patterns) )
c4998f1 @rnagle make stuff internationalizationable
rnagle authored
28 throw new BadFunctionCallException(__('url_patterns keyword argument required', 'mtv'));
62ea783 @ryanmark initial commit of pared down MTV
ryanmark authored
29
30 try {
31
603b2ce @ryanmark fixes for exception error handling, removed more chicagonow specific …
ryanmark authored
32 // Start buffering so we can toss output in case we get an exception
33 ob_start();
34
35 if ( resolve( $url, $url_patterns ) ) {
36 // everything worked! Flush the buffer and return
37 ob_end_flush();
38 return true;
39 } else
62ea783 @ryanmark initial commit of pared down MTV
ryanmark authored
40 // We didn't find any matching patterns :( So... 404!
44d3179 @rnagle fix urlresolver to throw ajax exceptions when DOING_AJAX
rnagle authored
41 if (defined('DOING_AJAX') && DOING_AJAX)
42 throw new AjaxHttp404;
43 else
44 throw new Http404;
62ea783 @ryanmark initial commit of pared down MTV
ryanmark authored
45
46 } catch (HttpException $e) {
603b2ce @ryanmark fixes for exception error handling, removed more chicagonow specific …
ryanmark authored
47 ob_end_clean();
62ea783 @ryanmark initial commit of pared down MTV
ryanmark authored
48 // Our view threw an HttpException, so display it
49 $e->display();
50 } catch (Exception $e) {
603b2ce @ryanmark fixes for exception error handling, removed more chicagonow specific …
ryanmark authored
51 ob_end_clean();
62ea783 @ryanmark initial commit of pared down MTV
ryanmark authored
52 // Somebody threw some sort of exception, so display 500
44d3179 @rnagle fix urlresolver to throw ajax exceptions when DOING_AJAX
rnagle authored
53 if (defined('DOING_AJAX') && DOING_AJAX)
c8bb766 @ryanmark better server error messages
ryanmark authored
54 $http_ex = new AjaxHttp500($e->getMessage(), $e->getCode(), $e);
44d3179 @rnagle fix urlresolver to throw ajax exceptions when DOING_AJAX
rnagle authored
55 else
c8bb766 @ryanmark better server error messages
ryanmark authored
56 $http_ex = new Http500($e->getMessage(), $e->getCode(), $e);
44d3179 @rnagle fix urlresolver to throw ajax exceptions when DOING_AJAX
rnagle authored
57
62ea783 @ryanmark initial commit of pared down MTV
ryanmark authored
58 $http_ex->display();
59 }
60
61 return false; // We had some errors, so return false
62 }
63
64 function resolve($url, $url_patterns) {
65 // cycle through our patterns in order to find a view to execute
66 foreach ($url_patterns as $pattern => $view) {
2620b0f @rnagle don't return early from resolve function
rnagle authored
67 if ( is_array( $view ) ) resolve($url, $view);
62ea783 @ryanmark initial commit of pared down MTV
ryanmark authored
68 else if ( preg_match($pattern, $url, $matches) > 0 ) {
603b2ce @ryanmark fixes for exception error handling, removed more chicagonow specific …
ryanmark authored
69 // we found a match!
70
71 // Check to see if the function exists
c4998f1 @rnagle make stuff internationalizationable
rnagle authored
72 if ( ! function_exists( $view ) ) throw new BadFunctionCallException(
73 sprintf(__("Can't find view function: %s", 'mtv'), $view));
603b2ce @ryanmark fixes for exception error handling, removed more chicagonow specific …
ryanmark authored
74
75 // pass the match array to the view function
62ea783 @ryanmark initial commit of pared down MTV
ryanmark authored
76 call_user_func( $view, array_slice($matches, 1) );
77 return true; // We're all done, so return
78 }
79 }
80 return false;
81
82 }
83
84 function include_urls_for($app_name) {
85 global $registered_apps;
86
87 if ( isset($registered_apps[$app_name]['urls']) ) {
88 include $registered_apps[$app_name]['urls'];
89 return $url_patterns;
90 } else
c4998f1 @rnagle make stuff internationalizationable
rnagle authored
91 throw new Exception(sprintf(__("MTV App %s has no urls.php", 'mtv'), $app_name));
62ea783 @ryanmark initial commit of pared down MTV
ryanmark authored
92 }
93
94 class HttpException extends Exception {
95 public $message = 'HTTP Error!';
c8bb766 @ryanmark better server error messages
ryanmark authored
96 public $code = null;
62ea783 @ryanmark initial commit of pared down MTV
ryanmark authored
97 public $error_data;
98
c8bb766 @ryanmark better server error messages
ryanmark authored
99 public function __construct( $message=null, $error_data=null, $previous=null ) {
100 parent::__construct($message, $this->code, $previous);
62ea783 @ryanmark initial commit of pared down MTV
ryanmark authored
101 $this->error_data = $error_data;
102 }
103
104 public function display_header() {
105 switch ( $this->code ) {
106 case '404':
107 header( "HTTP/1.1 404 Not Found" );
108 break;
109 default:
110 header( "HTTP/1.1 500 Internal Server Error" );
111 }
112 }
113
114 public function display_message() {
115 print( $this->message );
116 }
117
118 public function display() {
119 $this->display_header();
120 $this->display_message();
121 }
122 }
123
124 class Http404 extends HttpException {
125 public $code = '404';
126
127 public function display_message() {
128 global $wp_query;
129 $wp_query->is_404 = true;
44d3179 @rnagle fix urlresolver to throw ajax exceptions when DOING_AJAX
rnagle authored
130
c8bb766 @ryanmark better server error messages
ryanmark authored
131 if ( $this->getPrevious() )
132 $ex = $this->getPrevious();
133 else $ex = $this;
134
950f2e6 @rnagle set query flag for 404 pages
rnagle authored
135 shortcuts\set_query_flags(array('page', '404'));
603b2ce @ryanmark fixes for exception error handling, removed more chicagonow specific …
ryanmark authored
136 shortcuts\display_template(
137 '404.html',
138 array(
c8bb766 @ryanmark better server error messages
ryanmark authored
139 'exception_class' => get_class($ex),
140 'exception' => $ex,
141 'globals' => $GLOBALS,
142 'post' => $_POST,
143 'get' => $_GET,
144 'server' => $_SERVER
603b2ce @ryanmark fixes for exception error handling, removed more chicagonow specific …
ryanmark authored
145 )
146 );
62ea783 @ryanmark initial commit of pared down MTV
ryanmark authored
147 exit;
148 }
149 }
150
151 class Http500 extends HttpException {
152 public $code = '500';
153
154 public function display_message() {
155 shortcuts\set_query_flags('page');
c8bb766 @ryanmark better server error messages
ryanmark authored
156
157 if ( $this->getPrevious() )
158 $ex = $this->getPrevious();
159 else $ex = $this;
160
603b2ce @ryanmark fixes for exception error handling, removed more chicagonow specific …
ryanmark authored
161 shortcuts\display_template(
162 '500.html',
163 array(
c8bb766 @ryanmark better server error messages
ryanmark authored
164 'exception_class' => get_class($ex),
165 'exception' => $ex,
166 'globals' => $GLOBALS,
167 'post' => $_POST,
168 'get' => $_GET,
169 'server' => $_SERVER
603b2ce @ryanmark fixes for exception error handling, removed more chicagonow specific …
ryanmark authored
170 )
171 );
62ea783 @ryanmark initial commit of pared down MTV
ryanmark authored
172 exit;
173 }
174 }
175
176 class AjaxHttp500 extends HttpException {
177 public $code = '500';
178 public function display_message() {
179 $response = array(
c8bb766 @ryanmark better server error messages
ryanmark authored
180 'error' => $this->message,
181 'trace' => $this->getTrace(),
182 'data' => $this->error_data
62ea783 @ryanmark initial commit of pared down MTV
ryanmark authored
183 );
184 shortcuts\display_json($response);
185 }
186 }
187
188 class AjaxHttp404 extends HttpException {
189 public $code = '404';
190 public function display_message() {
191 $response = array(
c8bb766 @ryanmark better server error messages
ryanmark authored
192 'exception' => $this,
c4998f1 @rnagle make stuff internationalizationable
rnagle authored
193 'error' => __('Callback not found', 'mtv'),
62ea783 @ryanmark initial commit of pared down MTV
ryanmark authored
194 );
195 shortcuts\display_json($response);
196 }
197 }
Something went wrong with that request. Please try again.