public
Description: The ultra-lightweight ultra-flexible blogging engine with a fetish for birds and misspellings.
Homepage: http://chyrp.net/
Clone URL: git://github.com/vito/chyrp.git
Click here to lend your support to: chyrp and make a donation at www.pledgie.com !
chyrp / includes / class / Route.php
100755 358 lines (289 sloc) 11.69 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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<?php
/**
* Class: Route
* Holds information for URLs, redirecting, etc.
*/
class Route {
# Array: $code
# The translation array of the post URL setting to regular expressions.
# Passed through the route_code filter.
public $code = array('(year)' => '([0-9]{4})',
'(month)' => '([0-9]{1,2})',
'(day)' => '([0-9]{1,2})',
'(hour)' => '([0-9]{1,2})',
'(minute)' => '([0-9]{1,2})',
'(second)' => '([0-9]{1,2})',
'(id)' => '([0-9]+)',
'(author)' => '([^\/]+)',
'(clean)' => '([^\/]+)',
'(url)' => '([^\/]+)',
'(feather)' => '([^\/]+)',
'(feathers)' => '([^\/]+)');
 
# Function: $urls
# An array of clean URL => dirty URL translations.
public $urls = array('/\/id\/([0-9]+)\//' => '/?action=view&amp;id=$1',
'/\/page\/(([^\/]+)\/)+/' => '/?action=page&amp;url=$2',
'/\/search\//' => '/?action=search',
'/\/search\/([^\/]+)\//' => '/?action=search&amp;query=$1',
'/\/archive\/([0-9]{4})\/([0-9]{2})\//'
=> '/?action=archive&amp;year=$1&amp;month=$2',
'/\/archive\/([0-9]{4})\/([0-9]{2})\/([0-9]{2})\//'
=> '/?action=archive&amp;year=$1&amp;month=$2&amp;day=$3',
'/\/theme_preview\/([^\/]+)\//' => '/?action=theme_preview&amp;theme=$1',
'/\/([^\/]+)\/feed\/([^\/]+)\//' => '/?action=$1&amp;feed&amp;title=$2',
'/\/([^\/]+)\/feed\//' => '/?action=$1&amp;feed');
 
# String: $action
# The current action.
public $action = "";
 
# Boolean: $ajax
# Shortcut to the AJAX constant (useful for Twig).
public $ajax = AJAX;
 
# Array: $post_url_attrs
# Contains an associative array of URL key to value arguments if we're viewing a post.
public $post_url_attrs = array();
 
/**
* Function: __construct
* Filters the key => val code so that modules may extend it.
*/
private function __construct() {}
 
/**
* Function: url
* Attempts to change the specified clean URL to a dirty URL if clean URLs is disabled.
* Use this for linking to things. The applicable URL conversions are passed through the
* parse_urls trigger.
*
* Parameters:
* $url - The clean URL.
*
* Returns:
* Clean URL - if $config->clean_urls is set to *true*.
* Dirty URL - if $config->clean_urls is set to *false*.
*/
public function url($url, $use_chyrp_url = false) {
$config = Config::current();
 
if ($url[0] == "/")
return (ADMIN or $use_chyrp_url) ?
Config::current()->chyrp_url.$url :
Config::current()->url.$url ;
 
if ($config->clean_urls) { # If their post URL doesn't have a trailing slash, remove it from these as well.
if (substr($url, 0, 5) == "page/") # Different URL for viewing a page
$url = substr($url, 5);
 
return (substr($config->post_url, -1) == "/" or $url == "search/") ?
$config->url."/".$url :
$config->url."/".rtrim($url, "/") ;
}
 
$urls = $this->urls;
Trigger::current()->filter($urls, "parse_urls");
 
foreach (array_diff_assoc($urls, $this->urls) as $key => $value)
$urls[substr($key, 0, -1)."feed\//"] = "/".$value."&amp;feed";
 
$urls["/\/(.*?)\/$/"] = "/?action=$1";
 
return $config->url.preg_replace(
array_keys($urls),
array_values($urls),
"/".$url, 1);
}
 
/**
* Function: key_regexp
* Converts the values in $config->post_url to regular expressions.
*
* Parameters:
* $key - Input URL with the keys from <Routes->$code>.
*
* Returns:
* $key values replaced with their regular expressions from <Routes->$code>.
*/
private function key_regexp($key) {
Trigger::current()->filter($this->code, "url_code");
return str_replace(array_keys($this->code), array_values($this->code), str_replace("/", "\\/", $key));
}
 
/**
* Function: determine_action
* This meaty function determines what exactly to do with the URL.
*/
public function determine_action() {
global $page;
$config = Config::current();
 
$this->action =& $_GET['action'];
 
# Parse the current URL and extract information.
$parse = parse_url($config->url);
fallback($parse["path"]);
 
$this->safe_path = str_replace("/", "\\/", $parse["path"]);
$this->request = preg_replace("/".$this->safe_path."/", "", $_SERVER['REQUEST_URI'], 1);
$this->arg = explode("/", trim($this->request, "/"));
 
if (empty($this->arg[0])) # If they're just at /, don't bother with all this.
return $this->action = "index";
 
# Feed
if (preg_match("/\/feed\/?$/", $this->request)) {
$_GET['feed'] = "true";
 
if ($this->arg[0] == "feed") # Don't set $this->action to "feed" (bottom of this function).
return $this->action = "index";
}
 
# Feed with a title parameter
if (preg_match("/\/feed\/([^\/]+)\/?$/", $this->request, $title)) {
$_GET['feed'] = "true";
$_GET['title'] = $title[1];
 
if ($this->arg[0] == "feed") # Don't set $this->action to "feed" (bottom of this function).
return $this->action = "index";
}
 
# Viewing a post by its ID
if ($this->arg[0] == "id") {
$_GET['id'] = $this->arg[1];
return $this->action = "id";
}
 
# Paginator
if (preg_match_all("/\/((([^_\/]+)_)?page)\/([0-9]+)/", $this->request, $page_matches)) {
foreach ($page_matches[1] as $key => $page_var) {
$index = array_search($page_var, $this->arg);
$_GET[$page_var] = $this->arg[$index + 1];
}
 
if ($index == 0) # Don't set $this->action to "page" (bottom of this function).
return $this->action = (isset($config->routes["/"])) ? $config->routes["/"] : "index" ;
}
 
# Archive
if ($this->arg[0] == "archive") {
# Make sure they're numeric; there might be a /page/ in there.
if (isset($this->arg[1]) and is_numeric($this->arg[1]))
$_GET['year'] = $this->arg[1];
if (isset($this->arg[2]) and is_numeric($this->arg[2]))
$_GET['month'] = $this->arg[2];
if (isset($this->arg[3]) and is_numeric($this->arg[3]))
$_GET['day'] = $this->arg[3];
 
return $this->action = "archive";
}
 
# Searching
if ($this->arg[0] == "search") {
if (isset($this->arg[1]) and strpos($this->request, "?action=search&query="))
redirect(str_replace("?action=search&query=", "", $this->request));
 
if (isset($this->arg[1]))
$_GET['query'] = $this->arg[1];
 
return $this->action = "search";
}
 
# Theme Previewing
if ($this->arg[0] == "theme_preview" and !empty($this->arg[1])) {
$_GET['theme'] = $this->arg[1];
return $this->action = "theme_preview";
}
 
# Bookmarklet
if ($this->arg[0] == "bookmarklet") {
$_GET['status'] = $this->arg[1];
return $this->action = "bookmarklet";
}
}
 
public function check_viewing_page($i_have_the_power = false) {
global $page;
 
$config = Config::current();
if (ADMIN or JAVASCRIPT or AJAX or XML_RPC or !$config->clean_urls or !empty($this->action))
return;
 
if (count($this->arg) == 1 and method_exists(MainController::current(), $this->arg[0]))
return $this->action = $this->arg[0];
 
$valids = Page::find(array("where" => "url IN ('".implode("', '", $this->arg)."')"));
 
if (count($valids) == count($this->arg)) {
foreach ($valids as $page)
if ($page->url == end($this->arg))
return list($_GET['url'], $this->action) = array($page->url, "page");
} elseif ($i_have_the_power)
return $this->action = fallback($this->arg[0], "index");
}
 
public function check_viewing_post($i_have_the_power = false) {
$config = Config::current();
 
if (ADMIN or JAVASCRIPT or AJAX or XML_RPC or !$config->clean_urls or !empty($this->action))
return;
 
if (count($this->arg) == 1 and method_exists(MainController::current(), $this->arg[0]))
return $this->action = $this->arg[0];
 
$post_url = $config->post_url;
 
$request = $this->request;
foreach (explode("/", $post_url) as $path)
foreach (preg_split("/\(([^\)]+)\)/", $path) as $leftover) {
$request = preg_replace("/".preg_quote($leftover)."/", "", $request, 1);
$post_url = preg_replace("/".preg_quote($leftover)."/", "", $post_url, 1);
}
 
$args = explode("/", trim($request, "/"));
 
$post_url = $this->key_regexp(rtrim($post_url, "/"));
preg_match_all("/\(([^\/]+)\)/", $config->post_url, $parameters);
if (preg_match("/".$post_url."/", rtrim($request, "/"), $matches)) {
array_shift($matches);
 
foreach ($parameters[0] as $index => $parameter)
if ($parameter[0] == "(")
$this->post_url_attrs[rtrim(ltrim($parameter, "("), ")")] = urldecode($args[$index]);
 
$check = Post::from_url($this->post_url_attrs);
 
if (!$check->no_results)
return $this->action = "view";
}
 
if ($i_have_the_power)
return $this->action = fallback($this->arg[0], "index");
}
 
public function check_custom_routes() {
$config = Config::current();
 
# Custom pages added by Modules, Feathers, Themes, etc.
foreach ($config->routes as $route => $action) {
if (is_numeric($action))
$action = $this->arg[0];
 
preg_match_all("/\(([^\)]+)\)/", $route, $matches);
 
if ($route != "/")
$route = trim($route, "/");
 
$escape = preg_quote($route, "/");
$to_regexp = preg_replace("/\\\\\(([^\)]+)\\\\\)/", "([^\/]+)", $escape);
 
if ($route == "/")
$to_regexp = "\$";
 
if (preg_match("/^\/{$to_regexp}/", $this->request, $url_matches)) {
array_shift($url_matches);
 
if (isset($matches[1]))
foreach ($matches[1] as $index => $parameter)
$_GET[$parameter] = $url_matches[$index];
 
$params = explode(";", $action);
$action = $params[0];
 
array_shift($params);
foreach ($params as $param) {
$split = explode("=", $param);
$_GET[$split[0]] = $split[1];
}
 
$its_fine = true;
Trigger::current()->filter($its_fine, "check_route_".$action, $action);
 
if ($its_fine)
return $this->action = $action;
}
}
}
 
/**
* Function: add
* Adds a route to Chyrp. Only needed for actions that have more than one parameter.
* For example, for /tags/ you won't need to do this, but you will for /tag/tag-name/.
*
* Parameters:
* $path - The path to add. Wrap variables with parentheses, e.g. "tag/(name)/".
* $action - The action the path points to.
*
* See Also:
* <remove_route>
*/
public function add($path, $action) {
$config = Config::current();
 
$new_routes = $config->routes;
$new_routes[$path] = $action;
 
$config->set("routes", $new_routes);
}
 
/**
* Function: remove_route
* Removes a route from the install's .htaccess file.
*
* Parameters:
* $path - The path to remove. Same as <add>.
*
* See Also:
* <add_route>
*/
public function remove($path) {
$config = Config::current();
 
unset($config->routes[$path]);
 
$config->set("routes", $config->routes);
}
 
/**
* Function: current
* Returns a singleton reference to the current class.
*/
public static function & current() {
static $instance = null;
return $instance = (empty($instance)) ? new self() : $instance ;
}
}
$route = Route::current();