プロフィール編集
守矢諏訪子 <suwako@076.moe>、2026-11-10 22:15
コミット 2680b67c29e1da106240102f7b04563c62489be7
・親 2e4f443
・ファイルを見る
8個のファイル、+348 -26
変更
route.php
| @@ -88,6 +88,8 @@ | ||
| 88 | 88 | $routes[] = Route::add('GET', 'logout', User::class.'@logout'); |
| 89 | 89 | $routes[] = Route::add('GET', 'verify/{token}', User::class.'@verify'); |
| 90 | 90 | $routes[] = Route::add('GET', 'profile/{name}', User::class.'@profile'); |
| 91 | + $routes[] = Route::add('GET', 'profile/{name}/edit', User::class.'@editProfile'); | |
| 92 | + $routes[] = Route::add('POST', 'profile/{name}/edit', User::class.'@editProfile'); | |
| 91 | 93 | |
| 92 | 94 | $routes[] = Route::add('GET', 'memberonly', Page::class.'@memberonly'); |
| 93 | 95 | $routes[] = Route::add('GET', 'staffonly', Page::class.'@staffonly'); |
| @@ -50,8 +50,8 @@ | ||
| 50 | 50 | $path = ROOT.$section; |
| 51 | 51 | $posts = []; |
| 52 | 52 | $isMember = $user !== NULL && $user->role !== Roles::BANNED; |
| 53 | - $isPaywall = $user !== NULL && $user->role >= Roles::SUBSCRIBER; | |
| 54 | - $isStaff = $user !== NULL && $user->role & (Roles::ADMIN | Roles::STAFF); | |
| 53 | + $isPaywall = $user !== NULL && ($user->role & Roles::SUBSCRIBER); | |
| 54 | + $isStaff = $user !== NULL && ($user->role & (Roles::ADMIN | Roles::STAFF)); | |
| 55 | 55 | |
| 56 | 56 | if (!is_dir($path)) return $posts; |
| 57 | 57 | $files = glob($path.'/*.md'); |
| @@ -151,8 +151,8 @@ | ||
| 151 | 151 | $auth = new Auth(); |
| 152 | 152 | $user = $auth->getLoggedInUser(); |
| 153 | 153 | $isMember = $user !== NULL && $user->role !== Roles::BANNED; |
| 154 | - $isPaywall = $user !== NULL && $user->role >= Roles::SUBSCRIBER; | |
| 155 | - $isStaff = $user !== NULL && $user->role & (Roles::ADMIN | Roles::STAFF); | |
| 154 | + $isPaywall = $user !== NULL && ($user->role & Roles::SUBSCRIBER); | |
| 155 | + $isStaff = $user !== NULL && ($user->role & (Roles::ADMIN | Roles::STAFF)); | |
| 156 | 156 | $tmpl->assign('user', $user); |
| 157 | 157 | |
| 158 | 158 | $tmpl->assign('pagetit', $pagetit); |
| @@ -77,7 +77,7 @@ | ||
| 77 | 77 | $this->pagetit .= 'ホーム'; |
| 78 | 78 | $this->tmpl->assign('pagetit', $this->pagetit); |
| 79 | 79 | |
| 80 | - if (!$this->user || $this->user->role !== Roles::ADMIN) goto noaccess; | |
| 80 | + if (!$this->user || !($this->user->role & Roles::ADMIN)) goto noaccess; | |
| 81 | 81 | $data = $this->op->getReseller(['with_statistics' => true]); |
| 82 | 82 | $domain = $this->op->listDomains(['order_by.renewal_date' => 'asc', 'status' => 'ACT']); |
| 83 | 83 | $this->tmpl->assign('data', $data->data); |
| @@ -96,7 +96,7 @@ | ||
| 96 | 96 | try { |
| 97 | 97 | $this->pagetit .= '請求書一覧'; |
| 98 | 98 | $this->tmpl->assign('pagetit', $this->pagetit); |
| 99 | - if (!$this->user || $this->user->role !== Roles::ADMIN) goto noaccess; | |
| 99 | + if (!$this->user || !($this->user->role & Roles::ADMIN)) goto noaccess; | |
| 100 | 100 | $data = $this->op->listInvoices(); |
| 101 | 101 | $this->tmpl->assign('data', $data->data); |
| 102 | 102 | $this->tmpl->render('listinvoices'); |
| @@ -114,7 +114,7 @@ | ||
| 114 | 114 | $this->pagetit .= '請求書一覧'; |
| 115 | 115 | $this->tmpl->assign('pagetit', $this->pagetit); |
| 116 | 116 | |
| 117 | - if (!$this->user || $this->user->role !== Roles::ADMIN) goto noaccess; | |
| 117 | + if (!$this->user || !($this->user->role & Roles::ADMIN)) goto noaccess; | |
| 118 | 118 | $data = $this->op->listPayments(); |
| 119 | 119 | $this->tmpl->assign('data', $data->data); |
| 120 | 120 | $this->tmpl->render('listpayments'); |
| @@ -132,7 +132,7 @@ | ||
| 132 | 132 | $this->pagetit .= '請求書一覧'; |
| 133 | 133 | $this->tmpl->assign('pagetit', $this->pagetit); |
| 134 | 134 | |
| 135 | - if (!$this->user || $this->user->role !== Roles::ADMIN) goto noaccess; | |
| 135 | + if (!$this->user || !($this->user->role & Roles::ADMIN)) goto noaccess; | |
| 136 | 136 | $data = $this->op->listTransactions(); |
| 137 | 137 | $this->tmpl->assign('data', $data->data); |
| 138 | 138 | $this->tmpl->render('listtransactions'); |
| @@ -150,7 +150,7 @@ | ||
| 150 | 150 | $this->pagetit .= '顧客様検索'; |
| 151 | 151 | $this->tmpl->assign('pagetit', $this->pagetit); |
| 152 | 152 | |
| 153 | - if (!$this->user || $this->user->role !== Roles::ADMIN) goto noaccess; | |
| 153 | + if (!$this->user || !($this->user->role & Roles::ADMIN)) goto noaccess; | |
| 154 | 154 | $data = $this->op->listCustomers(); |
| 155 | 155 | $this->tmpl->assign('data', $data->data); |
| 156 | 156 | $this->tmpl->render('listcustomers'); |
| @@ -217,7 +217,7 @@ | ||
| 217 | 217 | |
| 218 | 218 | $err = []; |
| 219 | 219 | |
| 220 | - if (!$this->user || $this->user->role !== Roles::ADMIN) goto noaccess; | |
| 220 | + if (!$this->user || !($this->user->role & Roles::ADMIN)) goto noaccess; | |
| 221 | 221 | |
| 222 | 222 | if (!empty($_POST)) { |
| 223 | 223 | if (NULL === $_POST['last_name'] || $_POST['last_name'] === '' |
| @@ -270,7 +270,7 @@ | ||
| 270 | 270 | $this->pagetit .= "顧客様「{$handle}」の表示"; |
| 271 | 271 | $this->tmpl->assign('pagetit', $this->pagetit); |
| 272 | 272 | |
| 273 | - if (!$this->user || $this->user->role !== Roles::ADMIN) goto noaccess; | |
| 273 | + if (!$this->user || !($this->user->role & Roles::ADMIN)) goto noaccess; | |
| 274 | 274 | $data = $this->op->getCustomer($handle, true); |
| 275 | 275 | $this->tmpl->assign('data', $data->data); |
| 276 | 276 | $this->tmpl->render('getcustomer'); |
| @@ -295,7 +295,7 @@ | ||
| 295 | 295 | $this->pagetit .= "顧客様「{$handle}」の表示"; |
| 296 | 296 | $this->tmpl->assign('pagetit', $this->pagetit); |
| 297 | 297 | |
| 298 | - if (!$this->user || $this->user->role !== Roles::ADMIN) goto noaccess; | |
| 298 | + if (!$this->user || !($this->user->role & Roles::ADMIN)) goto noaccess; | |
| 299 | 299 | if (empty($_POST)) goto render_confirm; |
| 300 | 300 | |
| 301 | 301 | if (isset($_POST['delete_reject'])) { |
| @@ -325,7 +325,7 @@ | ||
| 325 | 325 | $this->pagetit .= 'TLD一覧'; |
| 326 | 326 | $this->tmpl->assign('pagetit', $this->pagetit); |
| 327 | 327 | |
| 328 | - if (!$this->user || $this->user->role !== Roles::ADMIN) goto noaccess; | |
| 328 | + if (!$this->user || !($this->user->role & Roles::ADMIN)) goto noaccess; | |
| 329 | 329 | $data = $this->op->listTlds(); |
| 330 | 330 | $this->tmpl->assign('data', $data->data); |
| 331 | 331 | $this->tmpl->render('listtlds'); |
| @@ -349,7 +349,7 @@ | ||
| 349 | 349 | $this->pagetit .= "{$tld}の表示"; |
| 350 | 350 | $this->tmpl->assign('pagetit', $this->pagetit); |
| 351 | 351 | |
| 352 | - if (!$this->user || $this->user->role !== Roles::ADMIN) goto noaccess; | |
| 352 | + if (!$this->user || !($this->user->role & Roles::ADMIN)) goto noaccess; | |
| 353 | 353 | $data = $this->op->getTld($tld); |
| 354 | 354 | $this->tmpl->assign('data', $data->data); |
| 355 | 355 | $this->tmpl->render('gettld'); |
| @@ -367,7 +367,7 @@ | ||
| 367 | 367 | $this->pagetit .= 'ドメイン確認'; |
| 368 | 368 | $this->tmpl->assign('pagetit', $this->pagetit); |
| 369 | 369 | |
| 370 | - if (!$this->user || $this->user->role !== Roles::ADMIN) goto noaccess; | |
| 370 | + if (!$this->user || !($this->user->role & Roles::ADMIN)) goto noaccess; | |
| 371 | 371 | $data = $this->op->listDomains($domains, true); |
| 372 | 372 | $this->tmpl->assign('data', $data->data); |
| 373 | 373 | $this->tmpl->render('checkdomain'); |
| @@ -387,7 +387,7 @@ | ||
| 387 | 387 | $this->pagetit .= 'ドメイン確認'; |
| 388 | 388 | $this->tmpl->assign('pagetit', $this->pagetit); |
| 389 | 389 | |
| 390 | - if (!$this->user || $this->user->role !== Roles::ADMIN) goto noaccess; | |
| 390 | + if (!$this->user || !($this->user->role & Roles::ADMIN)) goto noaccess; | |
| 391 | 391 | if (!empty($domains)) { |
| 392 | 392 | $data = $this->op->checkDomainAvailable($domains, true); |
| 393 | 393 | $this->tmpl->assign('data', $data->data); |
| @@ -412,7 +412,7 @@ | ||
| 412 | 412 | $this->pagetit .= 'ドメイン値段一覧'; |
| 413 | 413 | $this->tmpl->assign('pagetit', $this->pagetit); |
| 414 | 414 | |
| 415 | - if (!$this->user || $this->user->role !== Roles::ADMIN) goto noaccess; | |
| 415 | + if (!$this->user || !($this->user->role & Roles::ADMIN)) goto noaccess; | |
| 416 | 416 | $data = $this->op->getDomainPrices(['domain.name' => $domainname, 'domain.extension' => $domainext]); |
| 417 | 417 | $this->tmpl->assign('data', $data->data); |
| 418 | 418 | $this->tmpl->render('getdomainprices'); |
| @@ -430,7 +430,7 @@ | ||
| 430 | 430 | $this->pagetit .= 'DNS一覧'; |
| 431 | 431 | $this->tmpl->assign('pagetit', $this->pagetit); |
| 432 | 432 | |
| 433 | - if (!$this->user || $this->user->role !== Roles::ADMIN) goto noaccess; | |
| 433 | + if (!$this->user || !($this->user->role & Roles::ADMIN)) goto noaccess; | |
| 434 | 434 | $data = $this->op->listDnsZones(); |
| 435 | 435 | $this->tmpl->assign('data', $data->data); |
| 436 | 436 | $this->tmpl->render('listdnszones'); |
| @@ -454,7 +454,7 @@ | ||
| 454 | 454 | $this->pagetit .= "{$domain}のDNS管理"; |
| 455 | 455 | $this->tmpl->assign('pagetit', $this->pagetit); |
| 456 | 456 | |
| 457 | - if (!$this->user || $this->user->role !== Roles::ADMIN) goto noaccess; | |
| 457 | + if (!$this->user || !($this->user->role & Roles::ADMIN)) goto noaccess; | |
| 458 | 458 | $data = ['rec' => $this->op->listZoneRecords($domain), 'zone' => $this->op->getDnsZone($domain)]; |
| 459 | 459 | $this->tmpl->assign('data', $data->data); |
| 460 | 460 | $this->tmpl->render('getdns'); |
| @@ -55,6 +55,8 @@ | ||
| 55 | 55 | protected int $tokenDuration = 31536000; // 1年間 |
| 56 | 56 | protected int $verifyDuration = 86400; // 確認メールのリンクの有効期限(24時間) |
| 57 | 57 | protected int $verifyResendInterval = 600; // 確認メールの再送の間隔(10分) |
| 58 | + /** アップロードをis_uploaded_fileで確認する(テストではfalse) */ | |
| 59 | + protected bool $checkUploads = true; | |
| 58 | 60 | protected string|int|null $algo = PASSWORD_ARGON2ID; |
| 59 | 61 | |
| 60 | 62 | public function __construct(?string $username = null) { |
| @@ -71,6 +73,7 @@ | ||
| 71 | 73 | unset($this->pubUser->password); |
| 72 | 74 | unset($this->pubUser->tokens); |
| 73 | 75 | unset($this->pubUser->verifyToken); |
| 76 | + $this->pubUser->avatarUrl = avatar_url($this->pubUser); | |
| 74 | 77 | return $this->pubUser; |
| 75 | 78 | } |
| 76 | 79 | |
| @@ -81,12 +84,21 @@ | ||
| 81 | 84 | unset($user->tokens); |
| 82 | 85 | unset($user->verifyToken, $user->verifyExpire, $user->verifySent); |
| 83 | 86 | $myself = $this->getUserData(); |
| 84 | - if (!$myself || ($myself->id != $user->id && $myself->role < Roles::STAFF)) $user->email = '(秘密)'; | |
| 87 | + // 以前は「role < STAFF」で比べていたので、ビットを足すと(Fediverse等)全員にメールアドレスが見えた | |
| 88 | + $isStaff = $myself && ($myself->role & (Roles::STAFF | Roles::ADMIN)); | |
| 89 | + if (!$myself || ($myself->id != $user->id && !$isStaff)) $user->email = '(秘密)'; | |
| 90 | + $user->canEdit = $myself && ($myself->id == $user->id || ($myself->role & Roles::ADMIN)); | |
| 91 | + $user->bioHtml = nl2br(htmlspecialchars((string)($user->bio ?? ''), ENT_QUOTES, 'UTF-8'), false); | |
| 92 | + $user->avatar = avatar_url($user); | |
| 85 | 93 | $user->name = namecolor($user); |
| 86 | 94 | $user->regDate = date('Y年m月d日', $user->regDate); |
| 87 | 95 | $user->gender = $user->gender === Gender::MALE ? '男' : ($user->gender === Gender::FEMALE ? '女' : '不明'); |
| 88 | 96 | |
| 89 | 97 | switch (true) { |
| 98 | + // 以前は「role & BANNED」(BANNEDは0なので常に偽)だった | |
| 99 | + case ($user->role === Roles::BANNED): | |
| 100 | + $user->role = 'BANされた'; | |
| 101 | + break; | |
| 90 | 102 | case ($user->role & Roles::ADMIN): |
| 91 | 103 | $user->role = '管理者'; |
| 92 | 104 | break; |
| @@ -96,15 +108,11 @@ | ||
| 96 | 108 | case ($user->role & (Roles::FSUBSCRIBER | Roles::PSUBSCRIBER | Roles::NSUBSCRIBER | Roles::SUBSCRIBER)): |
| 97 | 109 | $user->role = '支援者♡'; |
| 98 | 110 | break; |
| 99 | - case ($user->role & Roles::BANNED): | |
| 100 | - $user->role = 'BANされた'; | |
| 101 | - break; | |
| 102 | 111 | default: |
| 103 | 112 | $user->role = 'ユーザー'; |
| 104 | 113 | break; |
| 105 | 114 | } |
| 106 | 115 | |
| 107 | - $user->avatar = '/static/user/'.($user->avatar === '' ? 'noicon.png' : $user->username.'/'.$user->avatar); | |
| 108 | 116 | $user->altName = $user->displayname ?: $user->username; |
| 109 | 117 | unset($user->username); |
| 110 | 118 | unset($user->displayname); |
| @@ -370,6 +378,190 @@ | ||
| 370 | 378 | } |
| 371 | 379 | |
| 372 | 380 | /** |
| 381 | + * プロフィールを編集する | |
| 382 | + * | |
| 383 | + * 本人:表示名、性別、名前の色、自己紹介、アイコン、パスワード(現在のパスワードが必要)、メールアドレス(同じ) | |
| 384 | + * 管理者:他のユーザーの表示名等とロール(パスワードとメールアドレスは本人だけ) | |
| 385 | + * | |
| 386 | + * @param string $username 編集するユーザー | |
| 387 | + * @param array $in displayname, gender, namecolor(#rrggbb又は空), bio, removeAvatar, | |
| 388 | + * currentPassword, newPassword, newPasswordVerify, email, roles(管理者:ビットの配列) | |
| 389 | + * @param array|null $avatar $_FILES['avatar'] | |
| 390 | + * @return Result | |
| 391 | + */ | |
| 392 | + public function updateProfile(string $username, array $in, ?array $avatar = null): Result { | |
| 393 | + if (!AUTH_ENABLED) return Result::Error('エラー:認証システムは無効です。'); | |
| 394 | + $me = $this->id > 0 ? $this->getUserData() : null; | |
| 395 | + $path = $this->userPath($username); | |
| 396 | + if (!$me || $path === null) return Result::Error('エラー:ユーザーが見つかりません。'); | |
| 397 | + $user = json_decode((string)file_get_contents($path)); | |
| 398 | + if (!$user instanceof \stdClass) return Result::Error('エラー:ユーザーが見つかりません。'); | |
| 399 | + $self = $me->id == $user->id; | |
| 400 | + $admin = (bool)($me->role & Roles::ADMIN); | |
| 401 | + if (!$self && !$admin) return Result::Error('エラー:権限がありません。'); | |
| 402 | + | |
| 403 | + $errors = []; | |
| 404 | + $notices = []; | |
| 405 | + | |
| 406 | + // 表示名 | |
| 407 | + if (array_key_exists('displayname', $in)) { | |
| 408 | + $d = trim(preg_replace('/[\x00-\x1f\x7f]/u', '', (string)$in['displayname'])); | |
| 409 | + if (mb_strlen($d) > 50) $errors[] = 'エラー:表示名は50文字までです。'; | |
| 410 | + else $user->displayname = $d; | |
| 411 | + } | |
| 412 | + // 性別 | |
| 413 | + if (array_key_exists('gender', $in)) { | |
| 414 | + $g = (int)$in['gender']; | |
| 415 | + $user->gender = in_array($g, [\Gender::UNKNOWN, \Gender::MALE, \Gender::FEMALE], true) ? $g : \Gender::UNKNOWN; | |
| 416 | + } | |
| 417 | + // 名前の色(styleに入るので、色だけ許可する) | |
| 418 | + if (array_key_exists('namecolor', $in)) { | |
| 419 | + $c = trim((string)$in['namecolor']); | |
| 420 | + if ($c === '') $user->namecolor = ''; | |
| 421 | + else if (preg_match('/^#[0-9a-fA-F]{6}$/', $c)) $user->namecolor = "color: {$c};"; | |
| 422 | + else $errors[] = 'エラー:名前の色は「#rrggbb」の形式で入力して下さい。'; | |
| 423 | + } | |
| 424 | + // 自己紹介 | |
| 425 | + if (array_key_exists('bio', $in)) { | |
| 426 | + $b = trim(str_replace(["\r\n", "\r"], "\n", (string)$in['bio'])); | |
| 427 | + if (mb_strlen($b) > 500) $errors[] = 'エラー:自己紹介は500文字までです。'; | |
| 428 | + else $user->bio = $b; | |
| 429 | + } | |
| 430 | + | |
| 431 | + // アイコン | |
| 432 | + $oldAvatar = (string)($user->avatar ?? ''); | |
| 433 | + $newAvatar = null; | |
| 434 | + if ($avatar && ($avatar['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_NO_FILE) { | |
| 435 | + if (!$res->isSuccess) $errors[] = 'エラー:'.$res->message; | |
| 436 | + else $newAvatar = $res->data['name']; | |
| 437 | + } else if (!empty($in['removeAvatar'])) { | |
| 438 | + $user->avatar = ''; | |
| 439 | + } | |
| 440 | + | |
| 441 | + // パスワード(本人だけ) | |
| 442 | + if ($self && (string)($in['newPassword'] ?? '') !== '') { | |
| 443 | + if (!password_verify((string)($in['currentPassword'] ?? ''), $user->password)) { | |
| 444 | + $errors[] = 'エラー:現在のパスワードが一致していません。'; | |
| 445 | + } else { | |
| 446 | + $res = $this->verifyPassword((string)$in['newPassword'], (string)($in['newPasswordVerify'] ?? '')); | |
| 447 | + if (!$res->isSuccess) $errors[] = $res->message; | |
| 448 | + else { | |
| 449 | + $user->password = password_hash((string)$in['newPassword'], $this->algo); | |
| 450 | + // 他の端末のログインを取り消す(このログインは残す) | |
| 451 | + $current = $this->token ? hash('sha256', $this->token) : ''; | |
| 452 | + $user->tokens = array_values(array_filter($user->tokens ?? [], fn($t) => hash_equals((string)($t->token ?? ''), $current))); | |
| 453 | + $notices[] = 'パスワードを変更しました。'; | |
| 454 | + } | |
| 455 | + } | |
| 456 | + } | |
| 457 | + | |
| 458 | + // メールアドレス(本人だけ) | |
| 459 | + $sendVerify = null; | |
| 460 | + $email = trim((string)($in['email'] ?? '')); | |
| 461 | + if ($self && $email !== '' && $email !== $user->email) { | |
| 462 | + if (!password_verify((string)($in['currentPassword'] ?? ''), $user->password)) { | |
| 463 | + $errors[] = 'エラー:メールアドレスを変更するには現在のパスワードを入力して下さい。'; | |
| 464 | + } else { | |
| 465 | + $res = $this->verifyEmail($email); | |
| 466 | + if (!$res->isSuccess) $errors[] = $res->message; | |
| 467 | + else if ($this->requiresVerification()) { | |
| 468 | + // 確認するまでは前のメールアドレスのまま | |
| 469 | + $sendVerify = bin2hex(random_bytes(32)); | |
| 470 | + $user->pendingEmail = $email; | |
| 471 | + $user->verifyToken = hash('sha256', $sendVerify); | |
| 472 | + $user->verifyExpire = time() + $this->verifyDuration; | |
| 473 | + $user->verifySent = time(); | |
| 474 | + } else { | |
| 475 | + $user->email = $email; | |
| 476 | + $notices[] = 'メールアドレスを変更しました。'; | |
| 477 | + } | |
| 478 | + } | |
| 479 | + } | |
| 480 | + | |
| 481 | + // ロール(管理者だけ) | |
| 482 | + if ($admin && isset($in['roles']) && is_array($in['roles'])) { | |
| 483 | + $role = Roles::BANNED; | |
| 484 | + foreach ($in['roles'] as $bit) { | |
| 485 | + $bit = (int)$bit; | |
| 486 | + if (isset(Roles::LABELS[$bit])) $role |= $bit; | |
| 487 | + } | |
| 488 | + // 最後の管理者は管理者を外せない | |
| 489 | + if (($user->role & Roles::ADMIN) && !($role & Roles::ADMIN) && $this->countAdmins() <= 1) { | |
| 490 | + $errors[] = 'エラー:最後の管理者のロールは外せません。'; | |
| 491 | + } else { | |
| 492 | + $user->role = $role; | |
| 493 | + // Fediverseのロールの既定(全員に付与)を使わず、以後はビットで判断する | |
| 494 | + $user->fediDefault = true; | |
| 495 | + } | |
| 496 | + } | |
| 497 | + | |
| 498 | + if (!empty($errors)) { | |
| 499 | + return Result::Error(implode('<br />', array_map(fn($e) => htmlspecialchars($e, ENT_QUOTES, 'UTF-8'), $errors))); | |
| 500 | + } | |
| 501 | + | |
| 502 | + if ($newAvatar !== null) $user->avatar = $newAvatar; | |
| 503 | + $json = json_encode($user, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); | |
| 504 | + if (file_put_contents($path, $json, LOCK_EX) === false) { | |
| 505 | + logger(LogType::Auth, '【updateProfile】JSONファイルを変更出来なかった。パス:'.$path); | |
| 506 | + return Result::Error('エラー:ユーザーデータの保存に失敗。'); | |
| 507 | + } | |
| 508 | + if ($oldAvatar !== '' && $oldAvatar !== ($user->avatar ?? '')) { | |
| 509 | + \Std\Lib\Fedi\Media::remove('/public/static/user/'.$user->username.'/'.$oldAvatar); | |
| 510 | + } | |
| 511 | + | |
| 512 | + if ($sendVerify !== null) { | |
| 513 | + $mailUser = clone $user; | |
| 514 | + $mailUser->email = $email; | |
| 515 | + if ($this->sendVerificationMail($mailUser, $sendVerify)->isSuccess) { | |
| 516 | + $notices[] = "確認メールを「{$email}」に送信しました。リンクを開くとメールアドレスが変わります。"; | |
| 517 | + } else { | |
| 518 | + $notices[] = '確認メールの送信に失敗しました。メールアドレスは変わっていません。'; | |
| 519 | + } | |
| 520 | + } | |
| 521 | + | |
| 522 | + return Result::Success(implode(' ', $notices) ?: 'プロフィールを保存しました。', ['user' => $user]); | |
| 523 | + } | |
| 524 | + | |
| 525 | + /** | |
| 526 | + * プロフィールの編集画面用のデータ(パスワード等を除く) | |
| 527 | + * | |
| 528 | + * @param string $username ユーザー | |
| 529 | + * @return \stdClass|null 編集出来ない場合はnull | |
| 530 | + */ | |
| 531 | + public function getEditableUser(string $username): ?\stdClass { | |
| 532 | + $me = $this->id > 0 ? $this->getUserData() : null; | |
| 533 | + $path = $this->userPath($username); | |
| 534 | + if (!$me || $path === null) return null; | |
| 535 | + $user = json_decode((string)file_get_contents($path)); | |
| 536 | + if (!$user instanceof \stdClass) return null; | |
| 537 | + if ($me->id != $user->id && !($me->role & Roles::ADMIN)) return null; | |
| 538 | + unset($user->password, $user->tokens, $user->verifyToken); | |
| 539 | + $user->isSelf = $me->id == $user->id; | |
| 540 | + $user->isAdmin = (bool)($me->role & Roles::ADMIN); | |
| 541 | + $user->colorHex = preg_match('/#[0-9a-fA-F]{6}/', (string)($user->namecolor ?? ''), $m) ? $m[0] : ''; | |
| 542 | + $user->avatarUrl = avatar_url($user); | |
| 543 | + return $user; | |
| 544 | + } | |
| 545 | + | |
| 546 | + /** ユーザー名からJSONファイルのパス */ | |
| 547 | + private function userPath(string $username): ?string { | |
| 548 | + if (!preg_match('/^[A-Za-z0-9]+$/', $username)) return null; | |
| 549 | + foreach ($this->userFiles() as $path) { | |
| 550 | + if (preg_match('/^\d+\.'.$username.'\.json$/', basename($path))) return $path; | |
| 551 | + } | |
| 552 | + return null; | |
| 553 | + } | |
| 554 | + | |
| 555 | + private function countAdmins(): int { | |
| 556 | + $n = 0; | |
| 557 | + foreach ($this->userFiles() as $path) { | |
| 558 | + $d = json_decode((string)file_get_contents($path)); | |
| 559 | + if ($d instanceof \stdClass && (($d->role ?? 0) & Roles::ADMIN)) $n++; | |
| 560 | + } | |
| 561 | + return $n; | |
| 562 | + } | |
| 563 | + | |
| 564 | + /** | |
| 373 | 565 | * 登録時にメールアドレスの確認が必要か |
| 374 | 566 | * |
| 375 | 567 | * @return bool |
| @@ -397,6 +589,11 @@ | ||
| 397 | 589 | if (($data->verifyExpire ?? 0) < time()) return Result::Error($invalid); |
| 398 | 590 | |
| 399 | 591 | $data->verified = true; |
| 592 | + // メールアドレスの変更の確認 | |
| 593 | + if (isset($data->pendingEmail)) { | |
| 594 | + $data->email = $data->pendingEmail; | |
| 595 | + unset($data->pendingEmail); | |
| 596 | + } | |
| 400 | 597 | unset($data->verifyToken, $data->verifyExpire, $data->verifySent); |
| 401 | 598 | $json = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); |
| 402 | 599 | if (file_put_contents($path, $json, LOCK_EX) === false) { |
変更
util.php
| @@ -373,6 +373,16 @@ | ||
| 373 | 373 | return $color.$suffix; |
| 374 | 374 | } |
| 375 | 375 | |
| 376 | +/** | |
| 377 | + * アイコンのURL(無ければnoicon.png) | |
| 378 | + */ | |
| 379 | +function avatar_url(\stdClass $user): string { | |
| 380 | + $avatar = (string)($user->avatar ?? ''); | |
| 381 | + $name = (string)($user->username ?? ''); | |
| 382 | + if ($avatar === '' || $name === '' || !is_file(ROOT."/public/static/user/{$name}/{$avatar}")) return '/static/user/noicon.png'; | |
| 383 | + return "/static/user/{$name}/".rawurlencode($avatar); | |
| 384 | +} | |
| 385 | + | |
| 376 | 386 | function make_csrf_token(?bool $force = false): string { |
| 377 | 387 | if (null !== getcookie('csrf_token') && !$force) return getcookie('csrf_token'); |
| 378 | 388 | $token = bin2hex(random_bytes(32)); |
| @@ -0,0 +1,100 @@ | ||
| 1 | +{@ include(common/header) @} | |
| 2 | + <h1 class="paragraph">プロフィールの編集({{ $e->username }})</h1> | |
| 3 | +{@ if ($error) @} | |
| 4 | + <p> | |
| 5 | + <div class="errormes"> | |
| 6 | + {{{ $error }}} | |
| 7 | + </div> | |
| 8 | + </p> | |
| 9 | +{@ endif @} | |
| 10 | +{@ if ($message) @} | |
| 11 | + <p class="paragraph">{{ $message }}</p> | |
| 12 | +{@ endif @} | |
| 13 | + | |
| 14 | + <form action="/profile/{{ $e->username }}/edit" method="POST" enctype="multipart/form-data"> | |
| 15 | + <input type="hidden" name="csrf_token" value="{{ $csrf }}" /> | |
| 16 | + <table> | |
| 17 | + <tbody> | |
| 18 | + <tr> | |
| 19 | + <td>アイコン:</td> | |
| 20 | + <td> | |
| 21 | + <img src="{{ $e->avatarUrl }}" alt="" style="max-width: 100px; max-height: 100px;" /><br /> | |
| 22 | + <input type="file" name="avatar" accept="image/png,image/jpeg,image/gif" /><br /> | |
| 23 | +{@ if ($e->avatar !== '') @} | |
| 24 | + <label><input type="checkbox" name="removeAvatar" value="1" /> アイコンを削除する</label> | |
| 25 | +{@ endif @} | |
| 26 | + </td> | |
| 27 | + </tr> | |
| 28 | + <tr> | |
| 29 | + <td><label for="displayname">表示名:</label></td> | |
| 30 | + <td><input type="text" id="displayname" name="displayname" maxlength="50" value="{{ $e->displayname }}" /></td> | |
| 31 | + </tr> | |
| 32 | + <tr> | |
| 33 | + <td><label for="gender">性別:</label></td> | |
| 34 | + <td> | |
| 35 | + <select id="gender" name="gender"> | |
| 36 | + <option value="-1"{{{ $e->gender === -1 ? ' selected="selected"' : '' }}}>不明</option> | |
| 37 | + <option value="0"{{{ $e->gender === 0 ? ' selected="selected"' : '' }}}>男</option> | |
| 38 | + <option value="1"{{{ $e->gender === 1 ? ' selected="selected"' : '' }}}>女</option> | |
| 39 | + </select> | |
| 40 | + </td> | |
| 41 | + </tr> | |
| 42 | + <tr> | |
| 43 | + <td><label for="namecolor">名前の色:</label></td> | |
| 44 | + <td> | |
| 45 | + <label><input type="checkbox" name="usecolor" value="1"{{{ $e->colorHex !== '' ? ' checked="checked"' : '' }}} /> 色を指定する</label> | |
| 46 | + <input type="color" id="namecolor" name="namecolor" value="{{ $e->colorHex !== '' ? $e->colorHex : '#ea79d8' }}" /> | |
| 47 | + </td> | |
| 48 | + </tr> | |
| 49 | + <tr> | |
| 50 | + <td><label for="bio">自己紹介:</label></td> | |
| 51 | + <td><textarea id="bio" name="bio" rows="5" cols="50" maxlength="500">{{ $e->bio ?? '' }}</textarea></td> | |
| 52 | + </tr> | |
| 53 | +{@ if ($fediEnabled && $e->fedi) @} | |
| 54 | + <tr> | |
| 55 | + <td>Fediverse:</td> | |
| 56 | + <td><label><input type="checkbox" name="locked" value="1"{{{ $locked ? ' checked="checked"' : '' }}} /> フォローを承認制にする</label></td> | |
| 57 | + </tr> | |
| 58 | +{@ endif @} | |
| 59 | +{@ if ($e->isSelf) @} | |
| 60 | + <tr> | |
| 61 | + <td><label for="email">メールアドレス:</label></td> | |
| 62 | + <td> | |
| 63 | + <input type="email" id="email" name="email" value="{{ $e->email }}" /> | |
| 64 | +{@ if (isset($e->pendingEmail)) @} | |
| 65 | + <br /><small>「{{ $e->pendingEmail }}」の確認待ち</small> | |
| 66 | +{@ endif @} | |
| 67 | + </td> | |
| 68 | + </tr> | |
| 69 | + <tr> | |
| 70 | + <td><label for="newPassword">新しいパスワード:</label></td> | |
| 71 | + <td><input type="password" id="newPassword" name="newPassword" autocomplete="new-password" /></td> | |
| 72 | + </tr> | |
| 73 | + <tr> | |
| 74 | + <td><label for="newPasswordVerify">新しいパスワード(確認):</label></td> | |
| 75 | + <td><input type="password" id="newPasswordVerify" name="newPasswordVerify" autocomplete="new-password" /></td> | |
| 76 | + </tr> | |
| 77 | + <tr> | |
| 78 | + <td><label for="currentPassword">現在のパスワード:</label></td> | |
| 79 | + <td><input type="password" id="currentPassword" name="currentPassword" autocomplete="current-password" /><br /><small>メールアドレスかパスワードを変更する場合は必須</small></td> | |
| 80 | + </tr> | |
| 81 | +{@ endif @} | |
| 82 | +{@ if ($e->isAdmin) @} | |
| 83 | + <tr> | |
| 84 | + <td>ロール(管理者):</td> | |
| 85 | + <td> | |
| 86 | +{@ foreach ($roles as $bit => $label) @} | |
| 87 | + <label><input type="checkbox" name="roles[]" value="{{ $bit }}"{{{ ($e->role & $bit) || ($bit === \Roles::FEDIVERSE && $e->fedi) ? ' checked="checked"' : '' }}} /> {{ $label }}</label><br /> | |
| 88 | +{@ endforeach @} | |
| 89 | + <small>全部外すとBANになります。</small> | |
| 90 | + </td> | |
| 91 | + </tr> | |
| 92 | +{@ endif @} | |
| 93 | + <tr> | |
| 94 | + <td></td> | |
| 95 | + <td><button>保存</button> <a href="/profile/{{ $e->username }}">戻る</a></td> | |
| 96 | + </tr> | |
| 97 | + </tbody> | |
| 98 | + </table> | |
| 99 | + </form> | |
| 100 | +{@ include(common/footer) @} |
| @@ -4,7 +4,7 @@ | ||
| 4 | 4 | <table> |
| 5 | 5 | <tbody> |
| 6 | 6 | <tr> |
| 7 | - <td rowspan="4"><img src="{{ $u->avatar }}" alt="{{ $u->altName }}" style="max-width: 100px; max-height: 100px; width: 100%; height: 100%;" /></td> | |
| 7 | + <td rowspan="5"><img src="{{ $u->avatar }}" alt="{{ $u->altName }}" style="max-width: 100px; max-height: 100px; width: 100%; height: 100%;" /></td> | |
| 8 | 8 | <td>登録日:{{ $u->regDate }}</td> |
| 9 | 9 | </tr> |
| 10 | 10 | <tr> |
| @@ -16,7 +16,20 @@ | ||
| 16 | 16 | <tr> |
| 17 | 17 | <td>メールアドレス:{{ $u->email }}</td> |
| 18 | 18 | </tr> |
| 19 | + <tr> | |
| 20 | + <td> | |
| 21 | +{@ if ($u->fediverse !== '') @} | |
| 22 | + Fediverse:<a href="/microblog/user/{{ $name }}">{{ $u->fediverse }}</a> | |
| 23 | +{@ endif @} | |
| 24 | +{@ if ($u->canEdit) @} | |
| 25 | + <a href="/profile/{{ $name }}/edit">プロフィールを編集</a> | |
| 26 | +{@ endif @} | |
| 27 | + </td> | |
| 28 | + </tr> | |
| 19 | 29 | </tbody> |
| 20 | 30 | </table> |
| 21 | 31 | </p> |
| 22 | -{@ include(common/footer) @} | |
| 32 | +{@ if ($u->bioHtml !== '') @} | |
| 33 | + <p class="paragraph">{{{ $u->bioHtml }}}</p> | |
| 34 | +{@ endif @} | |
| 35 | +{@ include(common/footer) @} |