-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.php
286 lines (246 loc) · 9.12 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
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
<?php
require_once(dirname(__FILE__) . '/ContentsPlanet.php');
require_once(MODULE_DIR . '/Debug.php');
require_once(MODULE_DIR . '/Utils.php');
require_once(MODULE_DIR . '/ContentDatabase.php');
require_once(MODULE_DIR . '/Authenticator.php');
require_once(MODULE_DIR . '/Localization.php');
require_once(MODULE_DIR . '/ErrorHandling.php');
require_once(MODULE_DIR . '/PathUtils.php');
set_error_handler('ErrorHandling\StyledErrorHandler');
// --- Setup htaccess file ---
$htaccessDesc =
"\n<IfModule mod_rewrite.c>\n" .
"RewriteEngine On\n" .
(REDIRECT_HTTPS_ENABLED ?
"\nRewriteCond %{HTTPS} off\n" .
"RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]\n"
: '') .
"\nRewriteCond %{REQUEST_URI} !(^" . CLIENT_URI . "/)\n" .
"RewriteCond %{REQUEST_URI} !(^" . SERVICE_URI . "/)\n" .
"RewriteRule ^(.*)$ index.php\n" .
"\nRewriteCond %{HTTP:Authorization} ^(.*)\n" .
"RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]\n" .
"</IfModule>\n";
// NOTE: fopen オプション w ではなく c にする理由
// wの時は, ファイルポインタをファイルの先頭に置き, ファイルサイズをゼロにします.
// つまり, openしたときにファイルが切り詰められる. ファイルの中身が消される.
// cオプションは, 切り詰められない.
$fp = fopen(ROOT_DIR . '/.htaccess', 'c+');
if (flock($fp, LOCK_SH)) {
$htaccess = stream_get_contents($fp);
flock($fp, LOCK_UN);
fclose($fp);
$makeSegment = function ($desc, $hash) {
return
"\n# BEGIN ContentsPlanet\n" .
"# hash: {$hash}\n" .
$desc .
"\n# END ContentsPlanet\n";
};
$hash = hash('fnv132', $htaccessDesc);
if (preg_match(
"/(^|\n)# BEGIN ContentsPlanet\n# hash: (.*?)\n(.*)\n# END ContentsPlanet/s",
$htaccess,
$matches,
PREG_OFFSET_CAPTURE
)) {
if ($matches[2][0] != $hash) {
// Need to update.
$htaccess = substr_replace(
$htaccess,
$makeSegment($htaccessDesc, $hash),
$matches[0][1],
strlen($matches[0][0])
);
file_put_contents(ROOT_DIR . '/.htaccess', $htaccess, LOCK_EX);
}
} else {
file_put_contents(
ROOT_DIR . '/.htaccess',
$htaccess . $makeSegment($htaccessDesc, $hash),
LOCK_EX
);
}
}
// End setup htaccess file ---
$vars = [];
// layer, language
// 言語パックになくても, layerがある場合
// 例えば, test_zh.content (中国語)があって, 言語パックがない.
// その時, 言語パックをenにするならば, layer名とlocale名が一致しなくなる.
//
// layer(language)の確認, localization の設定
$vars['layerName'] = DEFAULT_LAYER_NAME;
if (isset($_COOKIE['layer'])) {
$vars['layerName'] = $_COOKIE['layer'];
}
$vars['language'] = 'en';
if (isset($_GET['hl'])) {
$vars['language'] = $_GET['hl'];
} else if (isset($_COOKIE['language'])) {
$vars['language'] = $_COOKIE['language'];
}
if (!Localization\SetLocale($vars['language'])) {
$vars['language'] = 'en';
Localization\SetLocale($vars['language']);
}
// 有効時間 6カ月
setcookieSecure('language', $vars['language'], time() + (60 * 60 * 24 * 30 * 6), '/');
// $_SERVER['REQUEST_URI'] = '/ContentsPlanet/Master/../../Debugger/Contents/Root';
// $_SERVER['REQUEST_URI'] = '/ContentsPlanet/Master/../../../Test';
try {
$normalizedURI = PathUtils\canonicalize($_SERVER['REQUEST_URI']);
} catch (Exception $error) {
$vars['errorMessage'] = Localization\Localize('invalidURL', 'Invalid URL.');
require(FRONTEND_DIR . '/400.php');
exit();
}
if (ROOT_URI !== '' && strpos($normalizedURI, ROOT_URI) !== 0) {
$vars['errorMessage'] = Localization\Localize('invalidURL', 'Invalid URL.');
require(FRONTEND_DIR . '/400.php');
exit();
}
$_SERVER['REQUEST_URI'] = $normalizedURI;
// サブURIの取得
// ex) /Master/Root
$vars['subURI'] = substr($_SERVER['REQUEST_URI'], strlen(ROOT_URI));
$length = strpos($vars['subURI'], '?');
if ($length === false) $vars['subURI'] = substr($vars['subURI'], 0);
else $vars['subURI'] = substr($vars['subURI'], 0, $length);
$vars['subURI'] = urldecode($vars['subURI']);
// 特定のパス確認
if ($vars['subURI'] == '/admin') {
require(FRONTEND_DIR . '/admin.php');
exit();
} else if ($vars['subURI'] == '/login') {
require(FRONTEND_DIR . '/login.php');
exit();
} else if ($vars['subURI'] == '/logout') {
require(FRONTEND_DIR . '/logout.php');
exit();
} else if ($vars['subURI'] == '/setup') {
require(FRONTEND_DIR . '/setup.php');
exit();
} else if ($vars['subURI'] == '/feedbacks') {
require(FRONTEND_DIR . '/feedback-viewer.php');
exit();
} else if ($vars['subURI'] == '/logs') {
require(FRONTEND_DIR . '/log-viewer.php');
exit();
} else if ($vars['subURI'] == '/' || $vars['subURI'] == '') {
$vars['subURI'] = DEFAULT_SUB_URI;
header('Location: ' . ROOT_URI . DEFAULT_SUB_URI, true, 301);
exit();
}
// 権限情報の確認
$vars['owner'] = Authenticator::GetFileOwnerName('.' . URI2Path($vars['subURI']));
if ($vars['owner'] !== false) {
$vars['isPublic'] = false;
$vars['isAuthorized'] = true;
Authenticator::GetUserInfo($vars['owner'], 'isPublic', $vars['isPublic']);
if (!$vars['isPublic']) {
// セッション開始
@session_start();
$loginedUser = Authenticator::GetLoginedUsername();
if ($loginedUser !== $vars['owner']) {
$vars['isAuthorized'] = false;
}
}
}
if ($vars['owner'] === false) {
// ownerを持たないパスは存在しない
require(FRONTEND_DIR . '/404.php');
exit();
}
$vars['contentsFolder'] = DEFAULT_CONTENTS_FOLDER;
Authenticator::GetUserInfo($vars['owner'], 'contentsFolder', $vars['contentsFolder']);
// ここまでで設定されている変数
// subURI
// owner
// isPublic
// isAuthorized
// contentsFolder
if (!$vars['isPublic'] && !$vars['isAuthorized']) {
// 非公開かつ認証されていないとき
// 403(Forbidden)は, 認証を受けていないクライアントに存在を知られるので使用しない方がいいかも.
// 多くのWebアプリケーション(PukiWiki, GitLab, Wordpressなど)は,
// 302(Found)からログインページへリダイレクトしている.
require(FRONTEND_DIR . '/403.php');
// header('Location: ' . ROOT_URI . "/Logout?token=" . H(Authenticator::GenerateCsrfToken()) . "&returnTo=" . urlencode($_SERVER["REQUEST_URI"]));
exit();
}
// NOTE: Can we use the colon in URLs.
// Colons are allowed in the URI path.
// But you need to be careful when writing relative
// URI paths with a colon since it is not allowed when used like this:
//
// * https://stackoverflow.com/questions/1737575/are-colons-allowed-in-urls
// Split path into each segments.
// ex)
// '/Master/:tagmap/A'
// => ['', 'Master', ':tagmap', 'A']
$segments = explode('/', $vars['subURI']);
// URLs except resources in content folders begin with `:`.
// Redirect old tagmap url.
// TODO: Someday this process will be removed.
if (isset($segments[2]) && $segments[2] === 'TagMap') {
$segments[2] = ':tagmap';
header('Location: ' . ROOT_URI . implode('/', $segments) . '?' . $_SERVER['QUERY_STRING'], true, 301);
exit();
}
if (isset($segments[2]) && $segments[2] === ':tagmap') {
require(FRONTEND_DIR . '/tag-viewer.php');
exit();
}
if (isset($segments[2]) && $segments[2] === ':scripts') {
require(FRONTEND_DIR . '/script-server.php');
exit();
}
// ファイルかどうか
if (is_file(CONTENTS_HOME_DIR . URI2Path($vars['subURI']))) {
$vars['filePath'] = CONTENTS_HOME_DIR . URI2Path($vars['subURI']);
require(FRONTEND_DIR . '/file-server.php');
exit();
}
// ディレクトリかどうか
if (is_dir(CONTENTS_HOME_DIR . URI2Path($vars['subURI']))) {
$directoryPath = URI2Path($vars['subURI']);
if (strrpos($directoryPath, '/') === strlen($directoryPath) - 1) {
$directoryPath = substr($directoryPath, 0, -1);
}
$vars['directoryPath'] = '.' . $directoryPath;
require(FRONTEND_DIR . '/directory-viewer.php');
exit();
}
// contentPathの取得
$vars['contentPath'] = '.' . URI2Path($vars['subURI']);
// コマンドの確認
if (isset($_GET['cmd'])) {
if ($_GET['cmd'] == 'edit') {
require(FRONTEND_DIR . '/content-editor.php');
exit();
} else if ($_GET['cmd'] == 'preview') {
require(FRONTEND_DIR . '/preview.php');
exit();
} else if ($_GET['cmd'] == 'history') {
require(FRONTEND_DIR . '/history-viewer.php');
exit();
}
}
// plainText モードの確認
if (isset($_GET['plainText'])) {
$vars['filePath'] = ContentPathUtils::RealPath($vars['contentPath'] . Content::EXTENSION);
if ($vars['filePath'] === false) {
require(FRONTEND_DIR . '/404.php');
exit();
}
require(FRONTEND_DIR . '/plaintext.php');
exit();
}
// ノートページのとき
if (pathinfo($vars['subURI'], PATHINFO_EXTENSION) == 'note') {
require(FRONTEND_DIR . '/note-viewer.php');
exit();
}
require(FRONTEND_DIR . '/contents-viewer.php');