-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
71 lines (60 loc) · 2.33 KB
/
index.php
File metadata and controls
71 lines (60 loc) · 2.33 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
<?php
use Helpers\ValidationHelper;
spl_autoload_extensions('.php');
spl_autoload_register(function ($class) {
$namespace = explode('\\', $class);
$file = __DIR__ . '/' . implode('/', $namespace) . '.php';
if (file_exists($file)) {
require_once $file;
}
});
$DEBUG = true;
// ルーティング
$routes = include (__DIR__ . '/Routing/routes.php');
// リクエストURLからパスの部分を取得
// 例: /new -> new
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$path = ltrim($path, '/');
// パスがsnippetから始まる場合、/{hash}の部分は除く
$validPath = ValidationHelper::path($path, 'snippet');
// デフォルトでnewページを表示
if ($validPath === '') {
header('Location: /new');
exit();
}
// ルートパスの一致を確認
if (isset($routes[$validPath])) {
// ルートパスをキーとして、コールバック関数を取得
$renderer = $routes[$validPath]();
try {
// ヘッダーフィールドを設定
foreach ($renderer->getFields() as $name => $value) {
// ヘッダーに設定する値を無害なものにサニタイズ
// FILTER_SANITIZE_STRING ... 文字列に変換
$sanitized_value = filter_var($value, FILTER_SANITIZE_STRING);
// サニタイズされた値がもとの値と一致する場合、ヘッダーに設定
if ($sanitized_value === $value) {
header("{$name}: {$value}");
} else {
// 一致しない場合、ログに記録するか処理する
// エラー処理によっては例外をスローするか、デフォルトのまま続行
http_response_code(500);
if ($DEBUG) {
print ("Failed setting header - original value: '{$value}', sanitized value: '{$sanitized_value}'");
}
exit();
}
}
print ($renderer->getContent());
} catch (Exception $e) {
http_response_code(500);
print ("Internal error, please contact the admin. <br>");
if ($DEBUG) {
print ($e->getMessage());
}
}
} else {
// 一致するルートがない場合、404 Not Found
http_response_code(404);
print ("404 Not Found: The requested URL was not found on this server.");
}