-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
240 lines (201 loc) · 7.77 KB
/
api.php
File metadata and controls
240 lines (201 loc) · 7.77 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
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
<?php
/**
* Doçentlik Yolu - API
* Kişisel ilerleme takibi için basit JSON tabanlı API
* Tek kullanıcı + Ziyaretçi modu destekli
*/
require_once __DIR__ . '/config.php';
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Authorization');
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
exit(0);
}
session_start();
// Yardımcı fonksiyonlar
function isLoggedIn() {
return isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true;
}
function getSelectedArea() {
return $_SESSION['selected_area'] ?? 'muhendislik';
}
function getDataFile() {
$area = getSelectedArea();
return DATA_DIR . '/user_progress_' . $area . '.json';
}
function getCriteriaFile($area = null) {
if ($area === null) {
$area = getSelectedArea();
}
return KRITERLER_DIR . '/' . $area . '.json';
}
function initializeDataFile($file) {
if (!file_exists(DATA_DIR)) {
mkdir(DATA_DIR, 0755, true);
}
if (!file_exists($file)) {
$initialData = [
'user' => [
'name' => '',
'email' => '',
'created_at' => date('Y-m-d H:i:s'),
'last_updated' => date('Y-m-d H:i:s')
],
'tasks' => [],
'achievements' => [],
'total_points' => 0,
'post_doc_points' => 0
];
file_put_contents($file, json_encode($initialData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
}
}
$action = $_GET['action'] ?? $_POST['action'] ?? '';
switch ($action) {
case 'login':
$input = json_decode(file_get_contents('php://input'), true);
$username = $input['username'] ?? '';
$password = $input['password'] ?? '';
if ($username === ADMIN_USERNAME && $password === ADMIN_PASSWORD) {
$_SESSION['logged_in'] = true;
$_SESSION['login_time'] = time();
echo json_encode(['success' => true, 'message' => 'Giriş başarılı']);
} else {
echo json_encode(['success' => false, 'error' => 'Kullanıcı adı veya şifre hatalı']);
}
break;
case 'logout':
// 1. Session verilerini temizle
$_SESSION = array();
// 2. Session cookie'sini sil
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]
);
}
// 3. Session'ı yok et
session_destroy();
echo json_encode(['success' => true, 'message' => 'Çıkış yapıldı']);
break;
case 'check_auth':
echo json_encode([
'success' => true,
'logged_in' => isLoggedIn(),
'selected_area' => getSelectedArea()
]);
break;
case 'get_areas':
// Mevcut alanları listele
$areasFile = KRITERLER_DIR . '/alanlar.json';
if (file_exists($areasFile)) {
$areas = json_decode(file_get_contents($areasFile), true);
echo json_encode(['success' => true, 'data' => $areas]);
} else {
echo json_encode(['success' => false, 'error' => 'Alan listesi bulunamadı']);
}
break;
case 'select_area':
$input = json_decode(file_get_contents('php://input'), true);
$area = $input['area'] ?? 'muhendislik';
// Geçerli alan mı kontrol et
$criteriaFile = getCriteriaFile($area);
if (file_exists($criteriaFile)) {
$_SESSION['selected_area'] = $area;
echo json_encode(['success' => true, 'message' => 'Alan seçildi', 'area' => $area]);
} else {
echo json_encode(['success' => false, 'error' => 'Geçersiz alan']);
}
break;
case 'get_criteria':
$area = $_GET['area'] ?? getSelectedArea();
$criteriaFile = getCriteriaFile($area);
if (file_exists($criteriaFile)) {
$criteria = json_decode(file_get_contents($criteriaFile), true);
echo json_encode(['success' => true, 'data' => $criteria, 'area' => $area], JSON_UNESCAPED_UNICODE);
} else {
echo json_encode(['success' => false, 'error' => 'Kriter dosyası bulunamadı: ' . $area]);
}
break;
case 'get_progress':
// view_only parametresi varsa admin verisini döndür
$viewOnly = isset($_GET['view_only']) && $_GET['view_only'] === 'true';
if (!isLoggedIn() && !$viewOnly) {
echo json_encode([
'success' => true,
'data' => [
'tasks' => [],
'achievements' => [],
'total_points' => 0,
'post_doc_points' => 0
],
'visitor_mode' => true
]);
break;
}
$dataFile = getDataFile();
initializeDataFile($dataFile);
$data = json_decode(file_get_contents($dataFile), true);
$data['visitor_mode'] = false;
$data['view_only'] = $viewOnly;
echo json_encode(['success' => true, 'data' => $data], JSON_UNESCAPED_UNICODE);
break;
case 'save_progress':
// Ziyaretçi modunda kaydetme yok
if (!isLoggedIn()) {
echo json_encode(['success' => false, 'error' => 'Kaydetmek için giriş yapmalısınız', 'visitor_mode' => true]);
break;
}
$input = json_decode(file_get_contents('php://input'), true);
if (!$input) {
echo json_encode(['success' => false, 'error' => 'Geçersiz veri']);
break;
}
$dataFile = getDataFile();
initializeDataFile($dataFile);
$data = json_decode(file_get_contents($dataFile), true);
if (isset($input['tasks'])) {
$data['tasks'] = $input['tasks'];
}
if (isset($input['achievements'])) {
$data['achievements'] = $input['achievements'];
}
if (isset($input['total_points'])) {
$data['total_points'] = $input['total_points'];
}
if (isset($input['post_doc_points'])) {
$data['post_doc_points'] = $input['post_doc_points'];
}
$data['user']['last_updated'] = date('Y-m-d H:i:s');
file_put_contents($dataFile, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
echo json_encode(['success' => true, 'message' => 'Kaydedildi']);
break;
case 'reset':
if (!isLoggedIn()) {
echo json_encode(['success' => false, 'error' => 'Sıfırlamak için giriş yapmalısınız']);
break;
}
$dataFile = getDataFile();
$initialData = [
'user' => [
'name' => '',
'email' => '',
'created_at' => date('Y-m-d H:i:s'),
'last_updated' => date('Y-m-d H:i:s')
],
'tasks' => [],
'achievements' => [],
'total_points' => 0,
'post_doc_points' => 0
];
file_put_contents($dataFile, json_encode($initialData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
echo json_encode(['success' => true, 'message' => 'İlerleme sıfırlandı']);
break;
default:
echo json_encode([
'success' => false,
'error' => 'Geçersiz eylem',
'available_actions' => ['login', 'logout', 'check_auth', 'get_areas', 'select_area', 'get_criteria', 'get_progress', 'save_progress', 'reset']
]);
}