-
-
Notifications
You must be signed in to change notification settings - Fork 171
/
routes.php
195 lines (183 loc) · 4.51 KB
/
routes.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
<?php
use Kirby\Cms\App;
use Kirby\Cms\LanguageRoutes;
use Kirby\Cms\Media;
use Kirby\Cms\PluginAssets;
use Kirby\Panel\Panel;
use Kirby\Panel\Plugins;
use Kirby\Toolkit\Str;
use Kirby\Uuid\Uuid;
return function (App $kirby) {
$api = $kirby->option('api.slug', 'api');
$panel = $kirby->option('panel.slug', 'panel');
$index = $kirby->url('index');
$media = $kirby->url('media');
if (Str::startsWith($media, $index) === true) {
$media = Str::after($media, $index);
} else {
// media URL is outside of the site, we can't make routing work;
// fall back to the standard media route
$media = 'media';
}
/**
* Before routes are running before the
* plugin routes and cannot be overwritten by
* plugins.
*/
$before = [
[
'pattern' => $api . '/(:all)',
'method' => 'ALL',
'env' => 'api',
'action' => function (string $path = null) use ($kirby) {
if ($kirby->option('api') === false) {
return null;
}
$request = $kirby->request();
return $kirby->api()->render($path, $this->method(), [
'body' => $request->body()->toArray(),
'files' => $request->files()->toArray(),
'headers' => $request->headers(),
'query' => $request->query()->toArray(),
]);
}
],
[
'pattern' => $media . '/plugins/index.(css|js)',
'env' => 'media',
'action' => function (string $type) use ($kirby) {
$plugins = new Plugins();
return $kirby
->response()
->type($type)
->body($plugins->read($type));
}
],
[
// TODO: change to '/plugins/(:any)/(:any)/(:any)/(:all)' once
// the hash is made mandatory
'pattern' => $media . '/plugins/(:any)/(:any)/(?:(:any)/)?(:all)',
'env' => 'media',
'action' => function (
string $provider,
string $pluginName,
string $hash,
string $path
) {
return PluginAssets::resolve(
$provider . '/' . $pluginName,
$hash,
$path
);
}
],
[
'pattern' => $media . '/pages/(:all)/(:any)/(:any)',
'env' => 'media',
'action' => function (
string $path,
string $hash,
string $filename
) use ($kirby) {
return Media::link($kirby->page($path), $hash, $filename);
}
],
[
'pattern' => $media . '/site/(:any)/(:any)',
'env' => 'media',
'action' => function (
string $hash,
string $filename
) use ($kirby) {
return Media::link($kirby->site(), $hash, $filename);
}
],
[
'pattern' => $media . '/users/(:any)/(:any)/(:any)',
'env' => 'media',
'action' => function (
string $id,
string $hash,
string $filename
) use ($kirby) {
return Media::link($kirby->user($id), $hash, $filename);
}
],
[
'pattern' => $media . '/assets/(:all)/(:any)/(:any)',
'env' => 'media',
'action' => function (
string $path,
string $hash,
string $filename
) {
return Media::thumb($path, $hash, $filename);
}
],
[
'pattern' => $panel . '/(:all?)',
'method' => 'ALL',
'env' => 'panel',
'action' => function (string $path = null) {
return Panel::router($path);
}
],
// permalinks for page/file UUIDs
[
'pattern' => '@/(page|file)/(:all)',
'method' => 'ALL',
'env' => 'site',
'action' => function (string $type, string $id) use ($kirby) {
// try to resolve to model, but only from UUID cache;
// this ensures that only existing UUIDs can be queried
// and attackers can't force Kirby to go through the whole
// site index with a non-existing UUID
if ($model = Uuid::for($type . '://' . $id)?->model(true)) {
return $kirby
->response()
->redirect($model->url());
}
// render the error page
return false;
}
],
];
// Multi-language setup
if ($kirby->multilang() === true) {
$after = LanguageRoutes::create($kirby);
} else {
// Single-language home
$after[] = [
'pattern' => '',
'method' => 'ALL',
'env' => 'site',
'action' => function () use ($kirby) {
return $kirby->resolve();
}
];
// redirect the home page folder to the real homepage
$after[] = [
'pattern' => $kirby->option('home', 'home'),
'method' => 'ALL',
'env' => 'site',
'action' => function () use ($kirby) {
return $kirby
->response()
->redirect($kirby->site()->url());
}
];
// Single-language subpages
$after[] = [
'pattern' => '(:all)',
'method' => 'ALL',
'env' => 'site',
'action' => function (string $path) use ($kirby) {
return $kirby->resolve($path);
}
];
}
return [
'before' => $before,
'after' => $after
];
};