forked from nozomu-y/kleines-mypage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
initialize.php
342 lines (313 loc) · 8.42 KB
/
initialize.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
if (php_sapi_name() != 'cli') {
throw new Exception('This script must be run on the command line.');
}
require __DIR__ . '/Core/config.php';
if (empty(DB_HOST) && empty(DB_USER) && empty(DB_PASS) && empty(DB_NAME) && empty(MYPAGE_ROOT) && empty(WEB_DOMAIN) && empty(ADMIN_EMAIL)) {
print("Please fill in all the constants in /Core/config.php");
exit();
}
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if ($mysqli->connect_error) {
error_log($mysqli->connect_error);
exit;
}
$query = "
CREATE TABLE IF NOT EXISTS users (
user_id int(5) UNSIGNED ZEROFILL AUTO_INCREMENT,
email varchar(256) UNIQUE,
password varchar(256),
status varchar(32),
PRIMARY KEY (user_id)
);";
$result = $mysqli->query($query);
if (!$result) {
print('Query Failed : ' . $mysqli->error);
$mysqli->close();
exit();
}
$query = "
CREATE TABLE IF NOT EXISTS profiles (
user_id int(5) UNSIGNED ZEROFILL,
last_name varchar(256),
first_name varchar(256),
name_kana varchar(256),
grade int(2) UNSIGNED ZEROFILL,
part varchar(1),
birthday date,
PRIMARY KEY (user_id)
);";
$result = $mysqli->query($query);
if (!$result) {
print('Query Failed : ' . $mysqli->error);
$mysqli->close();
exit();
}
$query = "
CREATE TABLE IF NOT EXISTS admins (
user_id int(5) UNSIGNED ZEROFILL,
role varchar(32),
PRIMARY KEY (user_id)
);";
$result = $mysqli->query($query);
if (!$result) {
print('Query Failed : ' . $mysqli->error);
$mysqli->close();
exit();
}
$query = "
CREATE TABLE IF NOT EXISTS login_histories (
user_id int(5) UNSIGNED ZEROFILL,
datetime datetime,
success int(1),
IP varchar(32),
PRIMARY KEY (user_id, datetime)
);";
$result = $mysqli->query($query);
if (!$result) {
print('Query Failed : ' . $mysqli->error);
$mysqli->close();
exit();
}
$query = "
CREATE TABLE IF NOT EXISTS password_updates (
user_id int(5) UNSIGNED ZEROFILL,
datetime datetime,
IP varchar(32),
PRIMARY KEY (user_id, datetime)
);";
$result = $mysqli->query($query);
if (!$result) {
print('Query Failed : ' . $mysqli->error);
$mysqli->close();
exit();
}
$query = "
CREATE TABLE IF NOT EXISTS identity_verifications (
user_id int(5) UNSIGNED ZEROFILL,
datetime datetime,
token varchar(64),
PRIMARY KEY (user_id)
);";
$result = $mysqli->query($query);
if (!$result) {
print('Query Failed : ' . $mysqli->error);
$mysqli->close();
exit();
}
$query = "
CREATE TABLE IF NOT EXISTS accounting_lists (
accounting_id int(5) UNSIGNED ZEROFILL AUTO_INCREMENT,
name varchar(256),
deadline date,
admin varchar(32),
PRIMARY KEY (accounting_id)
);";
$result = $mysqli->query($query);
if (!$result) {
print('Query Failed : ' . $mysqli->error);
$mysqli->close();
exit();
}
$query = "
CREATE TABLE IF NOT EXISTS accounting_records (
accounting_id int(5) UNSIGNED ZEROFILL,
user_id int(5) UNSIGNED ZEROFILL,
price int(10),
paid_cash int(10),
datetime datetime,
PRIMARY KEY (accounting_id, user_id)
);";
$result = $mysqli->query($query);
if (!$result) {
print('Query Failed : ' . $mysqli->error);
$mysqli->close();
exit();
}
$query = "
CREATE TABLE IF NOT EXISTS individual_accounting_lists (
list_id int(5) UNSIGNED ZEROFILL AUTO_INCREMENT,
name varchar(256),
datetime datetime,
PRIMARY KEY (list_id)
);";
$result = $mysqli->query($query);
if (!$result) {
print('Query Failed : ' . $mysqli->error);
$mysqli->close();
exit();
}
$query = "
CREATE TABLE IF NOT EXISTS individual_accounting_records (
user_id int(5) UNSIGNED ZEROFILL,
datetime datetime,
price int(10),
accounting_id int(5) UNSIGNED ZEROFILL,
list_id int(5) UNSIGNED ZEROFILL
);";
$result = $mysqli->query($query);
if (!$result) {
print('Query Failed : ' . $mysqli->error);
$mysqli->close();
exit();
}
$query = "
CREATE TABLE IF NOT EXISTS bulletin_boards (
bulletin_board_id int UNSIGNED ZEROFILL AUTO_INCREMENT,
user_id int(5) UNSIGNED ZEROFILL,
title varchar(128),
status varchar(32),
PRIMARY KEY (bulletin_board_id)
);";
$result = $mysqli->query($query);
if (!$result) {
print('Query Failed : ' . $mysqli->error);
$mysqli->close();
exit();
}
$query = "
CREATE TABLE IF NOT EXISTS bulletin_board_contents (
bulletin_board_id int UNSIGNED ZEROFILL,
user_id int(5) UNSIGNED ZEROFILL,
datetime datetime,
content varchar(21800),
PRIMARY KEY (bulletin_board_id, datetime)
);";
$result = $mysqli->query($query);
if (!$result) {
print('Query Failed : ' . $mysqli->error);
$mysqli->close();
exit();
}
$query = "
CREATE TABLE IF NOT EXISTS bulletin_board_hashtags (
bulletin_board_id int UNSIGNED ZEROFILL,
hashtag varchar(32),
PRIMARY KEY (bulletin_board_id, hashtag)
);";
$result = $mysqli->query($query);
if (!$result) {
print('Query Failed : ' . $mysqli->error);
$mysqli->close();
exit();
}
$query = "
CREATE TABLE IF NOT EXISTS bulletin_board_views (
bulletin_board_id int UNSIGNED ZEROFILL,
user_id int(5) UNSIGNED ZEROFILL,
datetime datetime,
PRIMARY KEY (bulletin_board_id, datetime)
);";
$result = $mysqli->query($query);
if (!$result) {
print('Query Failed : ' . $mysqli->error);
$mysqli->close();
exit();
}
print("Database initialization finished!\n");
$query = "SELECT user_id FROM users";
$result = $mysqli->query($query);
if (!$result) {
print('Query Failed : ' . $mysqli->error);
$mysqli->close();
exit();
}
$row_cnt = $result->num_rows;
if ($row_cnt == 0) {
while (true) {
echo 'Last Name: ';
$last_name = trim(fgets(STDIN));
if (preg_match('/^\S+$/', $last_name)) {
break;
} else {
echo 'Format not expected.\n';
}
}
while (true) {
echo 'First Name: ';
$first_name = trim(fgets(STDIN));
if (preg_match('/^\S+$/', $first_name)) {
break;
} else {
echo 'Format not expected.\n';
}
}
while (true) {
echo 'Name Kana: ';
$name_kana = trim(fgets(STDIN));
if (preg_match('/^[ァ-ヶー]+$/u', $name_kana)) {
break;
} else {
echo 'Format not expected.\n';
}
}
while (true) {
echo 'Grade (integer): ';
$grade = trim(fgets(STDIN));
if (preg_match('/^\d+$/', $grade)) {
break;
} else {
echo 'Format not expected.\n';
}
}
while (true) {
echo 'Part (S,A,T,B): ';
$part = trim(fgets(STDIN));
if (preg_match('/^[S|A|T|B]$/', $part)) {
break;
} else {
echo 'Format not expected.\n';
}
}
while (true) {
echo 'Email: ';
$email = trim(fgets(STDIN));
if (preg_match('/^[^\s]+@[^\s]+$/', $email)) {
break;
} else {
echo 'Format not expected.\n';
}
}
while (true) {
echo 'Password (at least 8 letters): ';
$password = trim(fgets(STDIN));
if (preg_match('/^([\x21-\x7E]{8,})$/', $password)) {
$password = password_hash($password, PASSWORD_DEFAULT);
break;
} else {
echo 'Format not expected.\n';
}
}
$query = "INSERT INTO users (email, password, status) VALUES ('$email', '$password', 'PRESENT')";
$result = $mysqli->query($query);
if (!$result) {
print('Query Failed : ' . $mysqli->error);
$mysqli->close();
exit();
}
$query = "SELECT user_id FROM users WHERE email='$email'";
$result = $mysqli->query($query);
if (!$result) {
print('Query Failed : ' . $mysqli->error);
$mysqli->close();
exit();
}
while ($row = $result->fetch_assoc()) {
$user_id = $row['user_id'];
}
$query = "INSERT INTO profiles (user_id, last_name, first_name, name_kana, grade, part) VALUES ('$user_id', '$last_name', '$first_name', '$name_kana', '$grade', '$part')";
$result = $mysqli->query($query);
if (!$result) {
print('Query Failed : ' . $mysqli->error);
$mysqli->close();
exit();
}
$query = "INSERT INTO admins (user_id, role) VALUES ('$user_id', 'MASTER')";
$result = $mysqli->query($query);
if (!$result) {
print('Query Failed : ' . $mysqli->error);
$mysqli->close();
exit();
}
print("Account set as admin: " . $grade . $part . " " . $last_name . $first_name . "\n");
}