| | @@ -7,15 +7,13 @@ |
| 7 | 7 | /**
|
| 8 | 8 | * 性別: -1 = 不明, 0 = 男性, 1 = 女性
|
| 9 | 9 | * ロール: -1 = BAN, 0 = ユーザー, 1 = スタッフ
|
| 10 | | - * トークン: string token, int expDate
|
| 11 | | - *
|
| 12 | | - * パスワードとメールアドレスはArgon2IDでハッシュする
|
| 13 | 10 | */
|
| 14 | 11 | private \stdClass $user;
|
| 15 | 12 | private \stdClass $pubUser;
|
| 16 | 13 | private ?string $token;
|
| 17 | 14 |
|
| 18 | 15 | protected string $dataDir = ROOT."/data/user/";
|
| 16 | + protected string $assDir = ROOT."/public/static/user/";
|
| 19 | 17 | protected int $minPassLen = 20;
|
| 20 | 18 | protected int $tokenDuration = 31536000; // 1年間
|
| 21 | 19 | protected string|int|null $algo = PASSWORD_ARGON2ID;
|
| | @@ -36,25 +34,6 @@ |
| 36 | 34 | return $this->pubUser;
|
| 37 | 35 | }
|
| 38 | 36 |
|
| 39 | | - private function verifyLogin(string $username, string $password): \Result {
|
| 40 | | - $userData = $this->getUserData();
|
| 41 | | - if ($username !== $userData->username || !password_verify($password, $userData->password)) {
|
| 42 | | - return \Result::error('エラー:ユーザー名又はパスワードが一致していません。');
|
| 43 | | - }
|
| 44 | | -
|
| 45 | | - if (password_needs_rehash($userData->password, $algo)) {
|
| 46 | | - $userData->password = password_hash($password, $algo);
|
| 47 | | - $path = $this->dataDir.$this->id.'.'.$userData->username.'.json';
|
| 48 | | - $json = json_encode($userData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
| 49 | | -
|
| 50 | | - if (file_put_contents($path, $json) === false) {
|
| 51 | | - return \Result::error('エラー:ユーザーデータの保存に失敗。');
|
| 52 | | - }
|
| 53 | | - }
|
| 54 | | -
|
| 55 | | - return \Result::success();
|
| 56 | | - }
|
| 57 | | -
|
| 58 | 37 | public function setToken(string $username, string $password): \Result {
|
| 59 | 38 | $userData = $this->getUserData();
|
| 60 | 39 | if (!isset($userData->tokens) || !is_array($userData->tokens)) $userData->tokens = [];
|
| | @@ -156,7 +135,7 @@ |
| 156 | 135 | $userList = scandir($this->dataDir);
|
| 157 | 136 |
|
| 158 | 137 | foreach ($userList as $list) {
|
| 159 | | - if ($list === '.' || $list === '..') continue;
|
| 138 | + if ($list === '.kara' || $list === '.' || $list === '..') continue;
|
| 160 | 139 | $file = str_replace('.json', '', $list);
|
| 161 | 140 | $user = explode('.', $file)[1];
|
| 162 | 141 | if ($username === $user) return \Result::Success("ユーザー「{$username}」は既に存在します。");
|
| | @@ -165,8 +144,86 @@ |
| 165 | 144 | return \Result::Error("エラー:ユーザー「{$username}」は存在しません。");
|
| 166 | 145 | }
|
| 167 | 146 |
|
| 147 | + public function mkUser(?string $username = null, ?string $password = null, ?string $passwordVerify = null, ?string $email = null): \Result {
|
| 148 | + if (!AUTH_REGISTER_ENABLED) return \Result::Error('ユーザー登録は無効です。');
|
| 149 | + $resUsr = $this->verifyUsername($username);
|
| 150 | + $resPwd = $this->verifyPassword($password, $passwordVerify);
|
| 151 | + $resEml = $this->verifyEmail($email);
|
| 152 | +
|
| 153 | + $err = '';
|
| 154 | +
|
| 155 | + if (!$resUsr->isSuccess) $err .= $resUsr->message."<br />";
|
| 156 | + if (!$resPwd->isSuccess) $err .= $resPwd->message."<br />";
|
| 157 | + if (!$resEml->isSuccess) $err .= $resEml->message."<br />";
|
| 158 | +
|
| 159 | + if ($err != '') return \Result::Error($err);
|
| 160 | + $err = '';
|
| 161 | +
|
| 162 | + if (!mkdir($this->assDir.$username, 0755)) {
|
| 163 | + return \Result::Error('エラー:ユーザーのアイコンディレクトリの作成に失敗。');
|
| 164 | + }
|
| 165 | +
|
| 166 | + $file = scandir($this->dataDir);
|
| 167 | + $lastId = 0;
|
| 168 | + if ($file) {
|
| 169 | + foreach ($file as $f) {
|
| 170 | + if ($f === '.kara' || $f === '.' || $f === '..') continue;
|
| 171 | + $base = substr($f, 0, -5); // .jsonの削除
|
| 172 | + $dot = strpos($base, '.');
|
| 173 | + if (!$dot) continue;
|
| 174 | + $id = substr($base, 0, $dot);
|
| 175 | + if (ctype_digit($id)) {
|
| 176 | + if ((int)$id > $lastId) $lastId = $id;
|
| 177 | + }
|
| 178 | + }
|
| 179 | + $lastId++;
|
| 180 | + }
|
| 181 | +
|
| 182 | + $user = new \stdClass;
|
| 183 | + $user->id = $lastId;
|
| 184 | + $user->username = $username;
|
| 185 | + $user->password = password_hash($password, $algo);
|
| 186 | + $user->avatar = '';
|
| 187 | + $user->email = $email;
|
| 188 | + $user->regDate = time();
|
| 189 | + $user->namecolor = '';
|
| 190 | + $user->displayname = '';
|
| 191 | + $user->gender = -1;
|
| 192 | + $user->role = 0;
|
| 193 | + $user->tokens = [];
|
| 194 | +
|
| 195 | + $path = "{$this->dataDir}{$lastId}.{$username}.json";
|
| 196 | + $json = json_encode($user, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
| 197 | +
|
| 198 | + if (file_put_contents($path, $json) === false) {
|
| 199 | + rmdir($this->assDir.$username);
|
| 200 | + return \Result::Error('エラー:ユーザーデータの保存に失敗。');
|
| 201 | + }
|
| 202 | +
|
| 203 | + return \Result::Success();
|
| 204 | + }
|
| 205 | +
|
| 168 | 206 | ////////////////////
|
| 169 | 207 |
|
| 208 | + private function verifyLogin(string $username, string $password): \Result {
|
| 209 | + $userData = $this->getUserData();
|
| 210 | + if ($username !== $userData->username || !password_verify($password, $userData->password)) {
|
| 211 | + return \Result::Error('エラー:ユーザー名又はパスワードが一致していません。');
|
| 212 | + }
|
| 213 | +
|
| 214 | + if (password_needs_rehash($userData->password, $algo)) {
|
| 215 | + $userData->password = password_hash($password, $algo);
|
| 216 | + $path = $this->dataDir.$this->id.'.'.$userData->username.'.json';
|
| 217 | + $json = json_encode($userData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
| 218 | +
|
| 219 | + if (file_put_contents($path, $json) === false) {
|
| 220 | + return \Result::Error('エラー:ユーザーデータの保存に失敗。');
|
| 221 | + }
|
| 222 | + }
|
| 223 | +
|
| 224 | + return \Result::Success();
|
| 225 | + }
|
| 226 | +
|
| 170 | 227 | private function purgeOldTokens(\stdClass $userData): \stdClass|\Result {
|
| 171 | 228 | // 有効期限切れたトークンの削除
|
| 172 | 229 | $out = $userData;
|
| | @@ -178,7 +235,7 @@ |
| 178 | 235 | $json = json_encode($userData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
| 179 | 236 |
|
| 180 | 237 | if (file_put_contents($path, $json) === false) {
|
| 181 | | - return \Result::error('エラー:ユーザーデータの保存に失敗。');
|
| 238 | + return \Result::Error('エラー:ユーザーデータの保存に失敗。');
|
| 182 | 239 | }
|
| 183 | 240 |
|
| 184 | 241 | return $out;
|
| | @@ -191,7 +248,7 @@ |
| 191 | 248 | $id = 0;
|
| 192 | 249 |
|
| 193 | 250 | foreach ($file as $f) {
|
| 194 | | - if ($f === '.' || $f === '..') continue;
|
| 251 | + if ($f === '.kara' || $f === '.' || $f === '..') continue;
|
| 195 | 252 |
|
| 196 | 253 | $path = "{$this->dataDir}{$f}";
|
| 197 | 254 | if (!is_file($path)) continue;
|
| | @@ -215,7 +272,7 @@ |
| 215 | 272 | $matches = [];
|
| 216 | 273 |
|
| 217 | 274 | foreach ($file as $f) {
|
| 218 | | - if ($f === '.' || $f === '..') continue;
|
| 275 | + if ($f === '.kara' || $f === '.' || $f === '..') continue;
|
| 219 | 276 | if (preg_match('/^'.$this->id.'\.(.*?)\.json$/', $f, $matches)) {
|
| 220 | 277 | $userFile = $matches[0];
|
| 221 | 278 | break;
|
| | @@ -239,41 +296,82 @@ |
| 239 | 296 | return json_decode($lines);
|
| 240 | 297 | }
|
| 241 | 298 |
|
| 242 | | - private function isEmailExist(?string $email): \Result {
|
| 243 | | - if (null === $password) return \Result::Error('エラー:パスワードをご入力下さい。');
|
| 299 | + private function isEmailExist(?string $email = null): \Result {
|
| 300 | + $userList = scandir($this->dataDir);
|
| 301 | + $matches = [];
|
| 302 | +
|
| 303 | + foreach ($userList as $f) {
|
| 304 | + if ($f === '.kara' || $f === '.' || $f === '..') continue;
|
| 305 | + $path = "{$this->dataDir}{$f}";
|
| 306 | + if (!is_file($path)) continue;
|
| 307 | + $content = file_get_contents($path);
|
| 308 | + $file = str_replace('.json', '', $f);
|
| 309 | + if (str_contains($content, '"email": "'.$email.'",')) {
|
| 310 | + $matches[] = $email;
|
| 311 | + }
|
| 312 | + if (count($matches) > 0) return \Result::Error("ユーザー「{$email}」は既に存在します。");
|
| 313 | + }
|
| 314 | +
|
| 315 | + return \Result::Success('エラー:メールアドレスが存在しません。');
|
| 244 | 316 | }
|
| 245 | 317 |
|
| 318 | + private function verifyEmail(?string $email = null): \Result {
|
| 319 | + if (null === $email || '' === $email) return \Result::Error('エラー:メールアドレスをご入力下さい。');
|
| 320 | + if (strpos($email, '@') === false) return \Result::Error('エラー:メールアドレスは不正です。');
|
| 321 | +
|
| 322 | + $domain = explode('@', $email)[1];
|
| 323 | + if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
| 324 | + return \Result::Error('エラー:メールアドレスは不正です。');
|
| 325 | + }
|
| 326 | +
|
| 327 | + if (!checkdnsrr($domain, 'MX')) {
|
| 328 | + return \Result::Error('エラー:メールアドレスは不正です。');
|
| 329 | + }
|
| 330 | +
|
| 331 | + $res = $this->isEmailExist($email);
|
| 332 | + if (!$res->isSuccess) return \Result::Error($res->message);
|
| 333 | +
|
| 334 | + return \Result::Success();
|
| 335 | + }
|
| 336 | +
|
| 246 | 337 | private function verifyUsername(?string $username): \Result {
|
| 247 | | - if (null === $username) return \Result::Error('エラー:ユーザー名をご入力下さい。');
|
| 338 | + if (null === $username || '' === $username) return \Result::Error('エラー:ユーザー名をご入力下さい。');
|
| 248 | 339 | if (strlen($username) < 6) return \Result::Error('エラー:ユーザー名は6文字以上をご入力下さい。');
|
| 249 | 340 |
|
| 250 | | - $accepted = 'A-Za-z0-9';
|
| 341 | + $res = $this->isUserExist($username);
|
| 342 | + if ($res->isSuccess) return \Result::Error($res->message);
|
| 251 | 343 |
|
| 252 | | - if (preg_match('/[^'.preg_quote($accepted, '/').']/', $password)) {
|
| 344 | + $accepted = 'A-Za-z0-9';
|
| 345 | + if (preg_match("/[^{$accepted}]/", $username)) {
|
| 253 | 346 | return \Result::Error('エラー:ユーザー名に不正な文字が含みます。');
|
| 254 | 347 | }
|
| 348 | +
|
| 255 | 349 | return \Result::Success();
|
| 256 | 350 | }
|
| 257 | 351 |
|
| 258 | | - private function verifyPassword(?string $password): \Result {
|
| 352 | + private function verifyPassword(?string $password = null, ?string $passwordVerify = null): \Result {
|
| 353 | + if (null === $password || '' === $password) return \Result::Error('エラー:パスワードをご入力下さい。');
|
| 354 | + if ($password !== $passwordVerify) return \Result::Error('エラー:パスワードは一致していません。');
|
| 355 | +
|
| 356 | + $res = $this->checkPasswordStandards($password);
|
| 357 | + if (!$res->isSuccess) {
|
| 358 | + return \Result::Error($res->message);
|
| 359 | + }
|
| 360 | +
|
| 259 | 361 | return \Result::Success();
|
| 260 | 362 | }
|
| 261 | 363 |
|
| 262 | | - private function checkPasswordStandards(?string $password): \Result {
|
| 263 | | - if (null === $password) return \Result::Error('エラー:パスワードをご入力下さい。');
|
| 264 | | - if (strlen($password) < $this->minPassLen) return \Result::Error("エラー:パスワード数は{$this->minPassLen}以上をご入力下さい。");
|
| 364 | + private function checkPasswordStandards(?string $password = null): \Result {
|
| 365 | + if (strlen($password) < $this->minPassLen) return \Result::Error("エラー:パスワードは{$this->minPassLen}以上をご入力下さい。");
|
| 265 | 366 |
|
| 266 | | - $specChar = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~]/';
|
| 267 | | - $accepted = 'A-Za-z0-9'.$specChar;
|
| 268 | | -
|
| 269 | | - if (preg_match('/[^'.preg_quote($accepted, '/').']/', $password)) {
|
| 367 | + if (!\countmatch($password)) {
|
| 270 | 368 | return \Result::Error('エラー:パスワードに不正な文字が含みます。');
|
| 271 | 369 | }
|
| 272 | 370 |
|
| 273 | 371 | $countUpper = preg_match_all('/[A-Z]/', $password) < 2;
|
| 274 | 372 | $countLower = preg_match_all('/[a-z]/', $password) < 2;
|
| 275 | 373 | $countDigit = preg_match_all('/[0-9]/', $password) < 2;
|
| 276 | | - $countSymbol = preg_match_all('/['.$specChar.']/', $password) < 2;
|
| 374 | + $countSymbol = \count_special_chars($password) < 2;
|
| 277 | 375 |
|
| 278 | 376 | if ($countUpper || $countLower || $countDigit || $countSymbol) {
|
| 279 | 377 | return \Result::Error('エラー:パスワードは2つ以上の大文字、2つ以上の小文字、2つ以上の数字、及び2つ以上の特別文字を含む事が必須です。');
|