-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.php
95 lines (90 loc) · 3.89 KB
/
index.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
<?php
include "config.php";
require_once('lib/namespaces.class.php');
require_once('lib/response.class.php');
require_once('lib/request.class.php');
require_once('lib/site.class.php');
require_once('lib/http_exception.class.php');
// Main objects
$namespaces = new Namespaces($config);
$request = new Request();
$response = new Response($config['site_base'], $request->uri);
$site = new Site($request->uri, $response, $namespaces, $config['block_time']);
set_exception_handler(array($site, 'exception_handler'));
// Helper regexes
$prefix_regex = $namespaces->get_prefix_regex();
$relaxed_prefix_regex = '[a-zA-Z0-9]*';
$extensions_regex = '(?:([a-z0-9]+)(\.plain)?|(file\.)?([a-z0-9]+))';
// Dispatch based on URI and method
if ($q = $request->matches('/^(robots|favicon)/')) {
// The “robots” and “favicon” prefixes do not exist, to avoid clashes with robots.txt and favicon.ico
$response->error(404);
} else if ($q = $request->matches('/^$/', array('q'))) {
$request->enforce_get();
$site->action_query($q['q']);
} else if ($q = $request->matches('/^$/')) {
$request->enforce_get();
$site->action_home();
} else if ($q = $request->matches('/^about$/')) {
$request->enforce_get();
$site->action_about();
} else if ($q = $request->matches('/^about\/formats$/')) {
$request->enforce_get();
$site->action_about_formats();
} else if ($q = $request->matches('/^about\/google$/')) {
$request->enforce_get();
$site->action_about_google();
} else if ($q = $request->matches('/^about\/users$/')) {
$request->enforce_get();
$site->action_about_users();
} else if ($q = $request->matches('/^about\/api$/')) {
$request->enforce_get();
$site->action_about_api();
} else if ($q = $request->matches('/^about\/json-ld$/')) {
$request->enforce_get();
$site->action_about_jsonld();
} else if ($q = $request->matches("/^popular(\/all)?(\.$extensions_regex)?$/")) {
$request->enforce_get();
$site->action_popular(!empty($q[1]), @$q[3] . @$q[6], !empty($q[4]) || !empty($q[5]));
} else if ($q = $request->matches("/^context(\.$extensions_regex)?$/", $q)) {
$request->enforce_get();
$site->action_popular(true, @$q[2] ? $q[2] : 'jsonld', true);
} else if ($q = $request->matches("/^latest(\/([0-9]+))?$/")) {
$request->enforce_get();
$site->action_latest(@$q[2]);
} else if ($q = $request->matches("/^latest\.rss$/")) {
$request->enforce_get();
$site->action_latest_feed();
} else if ($q = $request->matches("/^google-coop.rss$/")) {
$request->enforce_get();
$site->action_google_feed();
} else if ($q = $request->matches("/^reverse$/", array("uri"))) {
$request->enforce_get();
$site->action_reverse($q['uri'], @$_GET['format']);
} else if ($q = $request->matches("/^reverse$/")) {
$request->enforce_get();
$site->action_page_reverse();
} else if ($q = $request->matches("/^($prefix_regex)(:(.*)|\.$extensions_regex|)$/")) {
if ($request->is_get()) {
if (strlen(@$q[3]) > 0) {
$site->action_curie($q[1], $q[3]);
} else {
$site->action_prefix($q[1], @$q[4] . @$q[7], !empty($q[5]) || !empty($q[6]));
}
} else {
$request->enforce_post(array('create'));
$site->action_declare($q[1], @$q[3], $_POST['create']);
}
} else if ($q = $request->matches("!^($prefix_regex)/vote$!")) {
$request->enforce_post(array('uri', 'vote'));
$site->action_vote($q[1], $_POST['uri'], $_POST['vote'] == 'up');
} else if ($q = $request->matches("/^($relaxed_prefix_regex(,$relaxed_prefix_regex)+)(\.$extensions_regex)?$/")) {
// We use a forgiving regex here, and do a check against the full prefix regex for each
// prefix inside the method
$request->enforce_get();
$site->action_prefixes(explode(',', $q[1]), @$q[4] . @$q[7], !empty($q[5]) || !empty($q[6]));
} else if ($q = $request->matches("/^([^\/?]+)$/")) {
$site->failed_lookup($q[1]);
} else {
$response->error(404);
}