-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.php
More file actions
executable file
·107 lines (87 loc) · 2.54 KB
/
Controller.php
File metadata and controls
executable file
·107 lines (87 loc) · 2.54 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
<?php
// core/Controller.php
namespace Core;
class Controller
{
protected $auth;
public function __construct()
{
$this->auth = Auth::getInstance();
}
// ビューを表示するメソッド
protected function view($view, $data = [])
{
$this->sendNoCacheHeaders();
ob_start();
// データを変数として展開
extract($data);
// ヘッダー表示
require_once __DIR__ . '/../views/layouts/header.php';
// メインコンテンツ表示
$viewPath = __DIR__ . '/../views/' . $view . '.php';
if (file_exists($viewPath)) {
require_once $viewPath;
} else {
throw new \Exception("View {$view} not found");
}
// フッター表示
require_once __DIR__ . '/../views/layouts/footer.php';
$html = (string)ob_get_clean();
echo RuntimeI18n::translateHtml($html);
}
// リダイレクト
protected function redirect($url)
{
header('Location: ' . $url);
exit;
}
// JSONレスポンスを返す
protected function json($data, $statusCode = 200)
{
http_response_code($statusCode);
$this->sendNoCacheHeaders();
header('Content-Type: application/json');
echo json_encode(RuntimeI18n::translateApiPayload($data), JSON_UNESCAPED_UNICODE);
exit;
}
// GETパラメータを取得
protected function getQuery($key = null, $default = null)
{
if ($key === null) {
return $_GET;
}
return $_GET[$key] ?? $default;
}
// POSTデータを取得
protected function getPost($key = null, $default = null)
{
if ($key === null) {
return $_POST;
}
return $_POST[$key] ?? $default;
}
// リクエストボディを取得(JSON)
protected function getRequestBody()
{
$input = file_get_contents('php://input');
return json_decode($input, true);
}
// CSRFトークンを検証
protected function validateCsrfToken($token)
{
return $token === $_SESSION['csrf_token'];
}
// CSRFトークンを生成
protected function generateCsrfToken()
{
$token = bin2hex(random_bytes(32));
$_SESSION['csrf_token'] = $token;
return $token;
}
protected function sendNoCacheHeaders()
{
header('Cache-Control: private, no-store, no-cache, must-revalidate, max-age=0');
header('Pragma: no-cache');
header('Expires: 0');
}
}