| | @@ -16,6 +16,7 @@ |
| 16 | 16 | private string $desc; |
| 17 | 17 | private string $icon; |
| 18 | 18 | private array $posts = []; |
| 19 | + private string $https; |
| 19 | 20 | |
| 20 | 21 | /** |
| 21 | 22 | * コンストラクタ |
| | @@ -23,11 +24,13 @@ |
| 23 | 24 | * @param array $posts 投稿データの配列 |
| 24 | 25 | */ |
| 25 | 26 | public function __construct(array $posts = []) { |
| 26 | | - $this->domain = $_SERVER['SERVER_NAME']; |
| 27 | + if (!ACTIVITYPUB_ENABLED) return; |
| 28 | + $this->https = 'http'.((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 's' : '').'://'; |
| 29 | + $this->domain = $this->https.($_SERVER['SERVER_NAME'] ?? '127.0.0.1:8000'); |
| 27 | 30 | $this->actor = FEDIINFO['actor']; |
| 28 | 31 | $this->actorNick = FEDIINFO['actorNick']; |
| 29 | 32 | $this->desc = FEDIINFO['desc']; |
| 30 | | - $this->icon = "https://{$this->domain}".FEDIINFO['icon']; |
| 33 | + $this->icon = "{$this->domain}".FEDIINFO['icon']; |
| 31 | 34 | $this->posts = $posts; |
| 32 | 35 | } |
| 33 | 36 | |
| | @@ -38,6 +41,7 @@ |
| 38 | 41 | * @throws \Exception 公開鍵の読み込みに失敗した場合 |
| 39 | 42 | */ |
| 40 | 43 | public function getActor(): string { |
| 44 | + if (!ACTIVITYPUB_ENABLED) return ''; |
| 41 | 45 | $pubkey = file_get_contents(FEDIINFO['pubkey']); |
| 42 | 46 | if ($pubkey === false) { |
| 43 | 47 | throw new \Exception('公開鍵の受取に失敗。パス:'.FEDIINFO['pubkey']); |
| | @@ -48,7 +52,7 @@ |
| 48 | 52 | 'https://www.w3.org/ns/activitystreams', |
| 49 | 53 | 'https://w3id.org/security/v1', |
| 50 | 54 | ], |
| 51 | | - 'id' => "https://{$this->domain}/ap/actor", |
| 55 | + 'id' => "{$this->domain}/ap/actor", |
| 52 | 56 | 'name' => $this->actorNick, |
| 53 | 57 | 'summary' => $this->desc, |
| 54 | 58 | 'manuallyApprovesFollowers' => false, |
| | @@ -59,22 +63,21 @@ |
| 59 | 63 | ], |
| 60 | 64 | 'image' => [ |
| 61 | 65 | 'type' => 'Image', |
| 62 | | - 'url' => |
| 63 | | - "https://{$this->domain}/static/article/o_53803618dc1691.28179609.jpg", |
| 66 | + 'url' => "{$this->domain}/static/article/o_53803618dc1691.28179609.jpg", |
| 64 | 67 | 'mediaType' => 'image/jpeg', |
| 65 | 68 | ], |
| 66 | 69 | 'type' => 'Person', |
| 67 | | - 'url' => "https://{$this->domain}", |
| 70 | + 'url' => "{$this->domain}", |
| 68 | 71 | 'preferredUsername' => $this->actor, |
| 69 | | - 'inbox' => "https://{$this->domain}/ap/inbox", |
| 70 | | - 'outbox' => "https://{$this->domain}/ap/outbox", |
| 71 | | - 'followers' => "https://{$this->domain}/ap/followers", |
| 72 | | - 'following' => "https://{$this->domain}/ap/following", |
| 72 | + 'inbox' => "{$this->domain}/ap/inbox", |
| 73 | + 'outbox' => "{$this->domain}/ap/outbox", |
| 74 | + 'followers' => "{$this->domain}/ap/followers", |
| 75 | + 'following' => "{$this->domain}/ap/following", |
| 73 | 76 | 'published' => '2025-03-28T18:00:00Z', |
| 74 | 77 | 'updated' => gmdate('c'), |
| 75 | 78 | 'publicKey' => [ |
| 76 | | - 'id' => "https://{$this->domain}/ap/actor#main-key", |
| 77 | | - 'owner' => "https://{$this->domain}/ap/actor", |
| 79 | + 'id' => "{$this->domain}/ap/actor#main-key", |
| 80 | + 'owner' => "{$this->domain}/ap/actor", |
| 78 | 81 | 'publicKeyPem' => $pubkey, |
| 79 | 82 | ], |
| 80 | 83 | ]; |
| | @@ -89,6 +92,7 @@ |
| 89 | 92 | * @return string JSONエンコードされたアクティビティデータ |
| 90 | 93 | */ |
| 91 | 94 | public function getActivity(string $uuid): string { |
| 95 | + if (!ACTIVITYPUB_ENABLED) return ''; |
| 92 | 96 | $items = []; |
| 93 | 97 | |
| 94 | 98 | foreach ($this->posts as $post) { |
| | @@ -107,28 +111,27 @@ |
| 107 | 111 | 'toot' => 'http://joinmastodon.org/ns#', |
| 108 | 112 | ], |
| 109 | 113 | ], |
| 110 | | - 'id' => "https://{$this->domain}/ap/activities/create/{$post['uuid']}", |
| 114 | + 'id' => "{$this->domain}/ap/activities/create/{$post['uuid']}", |
| 111 | 115 | 'type' => 'Create', |
| 112 | | - 'actor' => "https://{$this->domain}/ap/actor", |
| 116 | + 'actor' => "{$this->domain}/ap/actor", |
| 113 | 117 | 'cc' => [ |
| 114 | | - "https://{$this->domain}/ap/followers", |
| 118 | + "{$this->domain}/ap/followers", |
| 115 | 119 | ], |
| 116 | 120 | 'published' => date("Y-m-d\TH:i:s.u\Z", strtotime($post['date'])), |
| 117 | 121 | 'to' => ['https://www.w3.org/ns/activitystreams#Public'], |
| 118 | 122 | 'object' => [ |
| 119 | | - 'id' => "https://{$this->domain}/ap/objects/{$post['uuid']}", |
| 123 | + 'id' => "{$this->domain}/ap/objects/{$post['uuid']}", |
| 120 | 124 | 'type' => 'Note', |
| 121 | 125 | 'name' => $post['title'], |
| 122 | | - 'attributedTo' => "https://{$this->domain}/ap/actor", |
| 126 | + 'attributedTo' => "{$this->domain}/ap/actor", |
| 123 | 127 | 'cc' => [ |
| 124 | | - "https://{$this->domain}/ap/followers", |
| 128 | + "{$this->domain}/ap/followers", |
| 125 | 129 | ], |
| 126 | 130 | 'to' => ['https://www.w3.org/ns/activitystreams#Public'], |
| 127 | | - 'content' => |
| 128 | | - $post['preview']."<br /><br /><a href=\"https://{$this->domain}/blog/{$post['slug']}\">読み続き</a>", |
| 129 | | - 'url' => "https://{$this->domain}/blog/{$post['slug']}", |
| 131 | + 'content' => $post['preview']."<br /><br /><a href=\"{$this->domain}/blog/{$post['slug']}\">読み続き</a>", |
| 132 | + 'url' => "{$this->domain}/blog/{$post['slug']}", |
| 130 | 133 | 'published' => date("Y-m-d\TH:i:s.u\Z", strtotime($post['date'])), |
| 131 | | - 'replies' => "https://{$this->domain}/ap/objects/{$uuid}/replies", |
| 134 | + 'replies' => "{$this->domain}/ap/objects/{$uuid}/replies", |
| 132 | 135 | 'sensitive' => false, |
| 133 | 136 | ], |
| 134 | 137 | ]; |
| | @@ -141,7 +144,7 @@ |
| 141 | 144 | } |
| 142 | 145 | |
| 143 | 146 | if (isset($post['thumbnail']) && $post['thumbnail'] != '') { |
| 144 | | - $imgurl = "https://technicalsuwako.moe/static/article/{$post['thumbnail']}"; |
| 147 | + $imgurl = "{$this->domain}/static/article/{$post['thumbnail']}"; |
| 145 | 148 | $imgpath = ROOT."/public/static/article/{$post['thumbnail']}"; |
| 146 | 149 | $imgraw = file_get_contents($imgpath); |
| 147 | 150 | |
| | @@ -167,6 +170,7 @@ |
| 167 | 170 | * @return string JSONエンコードされたアウトボックスデータ |
| 168 | 171 | */ |
| 169 | 172 | public function getOutbox(): string { |
| 173 | + if (!ACTIVITYPUB_ENABLED) return ''; |
| 170 | 174 | $items = []; |
| 171 | 175 | $counter = 0; |
| 172 | 176 | |
| | @@ -186,28 +190,27 @@ |
| 186 | 190 | 'toot' => 'http://joinmastodon.org/ns#', |
| 187 | 191 | ], |
| 188 | 192 | ], |
| 189 | | - 'id' => "https://{$this->domain}/ap/activities/create/{$uid}", |
| 193 | + 'id' => "{$this->domain}/ap/activities/create/{$uid}", |
| 190 | 194 | 'type' => 'Create', |
| 191 | | - 'actor' => "https://{$this->domain}/ap/actor", |
| 195 | + 'actor' => "{$this->domain}/ap/actor", |
| 192 | 196 | 'cc' => [ |
| 193 | | - "https://{$this->domain}/ap/followers", |
| 197 | + "{$this->domain}/ap/followers", |
| 194 | 198 | ], |
| 195 | 199 | 'published' => date("Y-m-d\TH:i:s.u\Z", strtotime($post['date'])), |
| 196 | 200 | 'to' => ['https://www.w3.org/ns/activitystreams#Public'], |
| 197 | 201 | 'object' => [ |
| 198 | | - 'id' => "https://{$this->domain}/ap/objects/{$uid}", |
| 202 | + 'id' => "{$this->domain}/ap/objects/{$uid}", |
| 199 | 203 | 'type' => 'Note', |
| 200 | 204 | 'name' => $post['title'], |
| 201 | | - 'attributedTo' => "https://{$this->domain}/ap/actor", |
| 205 | + 'attributedTo' => "{$this->domain}/ap/actor", |
| 202 | 206 | 'cc' => [ |
| 203 | | - "https://{$this->domain}/ap/followers", |
| 207 | + "{$this->domain}/ap/followers", |
| 204 | 208 | ], |
| 205 | 209 | 'to' => ['https://www.w3.org/ns/activitystreams#Public'], |
| 206 | | - 'content' => |
| 207 | | - $post['preview']."<br /><br /><a href=\"https://{$this->domain}/blog/{$post['slug']}\">読み続き</a>", |
| 208 | | - 'url' => "https://{$this->domain}/blog/{$post['slug']}", |
| 210 | + 'content' => $post['preview']."<br /><br /><a href=\"{$this->domain}/blog/{$post['slug']}\">読み続き</a>", |
| 211 | + 'url' => "{$this->domain}/blog/{$post['slug']}", |
| 209 | 212 | 'published' => date("Y-m-d\TH:i:s.u\Z", strtotime($post['date'])), |
| 210 | | - 'replies' => "https://{$this->domain}/ap/objects/{$uid}/replies", |
| 213 | + 'replies' => "{$this->domain}/ap/objects/{$uid}/replies", |
| 211 | 214 | 'sensitive' => false, |
| 212 | 215 | ], |
| 213 | 216 | ]; |
| | @@ -220,7 +223,7 @@ |
| 220 | 223 | } |
| 221 | 224 | |
| 222 | 225 | if (isset($post['thumbnail']) && $post['thumbnail'] != '') { |
| 223 | | - $imgurl = "https://technicalsuwako.moe/static/article/{$post['thumbnail']}"; |
| 226 | + $imgurl = "{$this->domain}/static/article/{$post['thumbnail']}"; |
| 224 | 227 | $imgpath = ROOT."/public/static/article/{$post['thumbnail']}"; |
| 225 | 228 | $imgraw = file_get_contents($imgpath); |
| 226 | 229 | |
| | @@ -242,7 +245,7 @@ |
| 242 | 245 | 'https://www.w3.org/ns/activitystreams', |
| 243 | 246 | 'https://w3id.org/security/v1', |
| 244 | 247 | ], |
| 245 | | - 'id' => "https://{$this->domain}/ap/outbox", |
| 248 | + 'id' => "{$this->domain}/ap/outbox", |
| 246 | 249 | 'type' => 'OrderedCollection', |
| 247 | 250 | 'totalItems' => count($items), |
| 248 | 251 | 'orderedItems' => $items, |
| | @@ -257,13 +260,14 @@ |
| 257 | 260 | * @return string JSONエンコードされたWebFingerデータ |
| 258 | 261 | */ |
| 259 | 262 | public function getWebfinger(): string { |
| 263 | + if (!ACTIVITYPUB_ENABLED) return ''; |
| 260 | 264 | $webfinger = [ |
| 261 | 265 | 'subject' => "acct:{$this->actor}@{$this->domain}", |
| 262 | 266 | 'links' => [ |
| 263 | 267 | [ |
| 264 | 268 | 'rel' => 'self', |
| 265 | 269 | 'type' => 'application/activity+json', |
| 266 | | - 'href' => "https://{$this->domain}/ap/actor", |
| 270 | + 'href' => "{$this->domain}/ap/actor", |
| 267 | 271 | ], |
| 268 | 272 | ], |
| 269 | 273 | ]; |
| | @@ -277,6 +281,7 @@ |
| 277 | 281 | * @return string JSONエンコードされたフォロワーのリスト |
| 278 | 282 | */ |
| 279 | 283 | public function getFollowers(): string { |
| 284 | + if (!ACTIVITYPUB_ENABLED) return ''; |
| 280 | 285 | $f = array_filter(explode("\n", file_get_contents(ROOT.'/data/followers.txt'))); |
| 281 | 286 | |
| 282 | 287 | $followers = [ |
| | @@ -284,7 +289,7 @@ |
| 284 | 289 | 'https://www.w3.org/ns/activitystreams', |
| 285 | 290 | 'https://w3id.org/security/v1', |
| 286 | 291 | ], |
| 287 | | - 'id' => "https://{$this->domain}/ap/followers", |
| 292 | + 'id' => "{$this->domain}/ap/followers", |
| 288 | 293 | 'type' => 'OrderredCollection', |
| 289 | 294 | 'totalItems' => count($f), |
| 290 | 295 | 'orderedItems' => $f, |
| | @@ -299,6 +304,7 @@ |
| 299 | 304 | * @return string JSONエンコードされたフォローリスト |
| 300 | 305 | */ |
| 301 | 306 | public function getFollowing(): string { |
| 307 | + if (!ACTIVITYPUB_ENABLED) return ''; |
| 302 | 308 | $f = array_filter(explode("\n", file_get_contents(ROOT.'/data/following.txt'))); |
| 303 | 309 | |
| 304 | 310 | $following = [ |
| | @@ -306,7 +312,7 @@ |
| 306 | 312 | 'https://www.w3.org/ns/activitystreams', |
| 307 | 313 | 'https://w3id.org/security/v1', |
| 308 | 314 | ], |
| 309 | | - 'id' => "https://{$this->domain}/ap/following", |
| 315 | + 'id' => "{$this->domain}/ap/following", |
| 310 | 316 | 'type' => 'OrderredCollection', |
| 311 | 317 | 'totalItems' => count($f), |
| 312 | 318 | 'orderedItems' => $f, |
| | @@ -322,6 +328,7 @@ |
| 322 | 328 | * @return void |
| 323 | 329 | */ |
| 324 | 330 | public function postInbox(array $activity): void { |
| 331 | + if (!ACTIVITYPUB_ENABLED) return; |
| 325 | 332 | switch ($activity['type']) { |
| 326 | 333 | case 'Follow': |
| 327 | 334 | $this->acceptFollower($activity); |
| | @@ -329,8 +336,7 @@ |
| 329 | 336 | default: |
| 330 | 337 | header('HTTP/1.1 501 Not Implemented'); |
| 331 | 338 | header('Content-Type: application/activity+json'); |
| 332 | | - echo json_encode(['error' => |
| 333 | | - '未対応なアクティビティタイプ: '.$activity['type']]); |
| 339 | + echo json_encode(['error' => '未対応なアクティビティタイプ: '.$activity['type']]); |
| 334 | 340 | exit; |
| 335 | 341 | } |
| 336 | 342 | |
| | @@ -346,14 +352,15 @@ |
| 346 | 352 | * @return string JSONエンコードされた更新アクティビティ |
| 347 | 353 | */ |
| 348 | 354 | public function update(): string { |
| 355 | + if (!ACTIVITYPUB_ENABLED) return ''; |
| 349 | 356 | $update = [ |
| 350 | 357 | '@context' => [ |
| 351 | 358 | 'https://www.w3.org/ns/activitystreams', |
| 352 | 359 | 'https://w3id.org/security/v1', |
| 353 | 360 | ], |
| 354 | | - 'id' => "https://{$this->domain}/ap/activities/update/".uuid(), |
| 361 | + 'id' => "{$this->domain}/ap/activities/update/".uuid(), |
| 355 | 362 | 'type' => 'Update', |
| 356 | | - 'actor' => "https://{$this->domain}/ap/actor", |
| 363 | + 'actor' => "{$this->domain}/ap/actor", |
| 357 | 364 | 'to' => ['https://www.w3.org/ns/activitystreams#Public'], |
| 358 | 365 | 'object' => json_decode($this->getActor(), true), |
| 359 | 366 | ]; |
| | @@ -368,14 +375,13 @@ |
| 368 | 375 | * @return void |
| 369 | 376 | */ |
| 370 | 377 | public function sendActorUpdate(array $params): void { |
| 378 | + if (!ACTIVITYPUB_ENABLED) return; |
| 371 | 379 | $f = array_filter(explode("\n", file_get_contents(ROOT.'/data/followers.txt'))); |
| 372 | 380 | $ap = new Activitypub(); |
| 373 | 381 | $inboxes = implode("\n", $f); |
| 374 | 382 | $update = json_decode($ap->update(), true); |
| 375 | 383 | |
| 376 | | - foreach ($f as $inbox) { |
| 377 | | - $this->sendActivity($inbox, $update); |
| 378 | | - } |
| 384 | + foreach ($f as $inbox) $this->sendActivity($inbox, $update); |
| 379 | 385 | } |
| 380 | 386 | |
| 381 | 387 | // 機能性メソッド |
| | @@ -410,9 +416,7 @@ |
| 410 | 416 | 'Digest' => "SHA-256=$digest", |
| 411 | 417 | ]; |
| 412 | 418 | |
| 413 | | - $stringToSign = "host: {$headers['Host']}\n" |
| 414 | | - ."date: {$headers['Date']}\n" |
| 415 | | - ."digest: {$headers['Digest']}"; |
| 419 | + $stringToSign = "host: {$headers['Host']}\n"."date: {$headers['Date']}\n"."digest: {$headers['Digest']}"; |
| 416 | 420 | logger(\LogType::ActivityPub, "署名対象: {$stringToSign}"); |
| 417 | 421 | |
| 418 | 422 | if (!openssl_sign($stringToSign, $signature, $priv, OPENSSL_ALGO_SHA256)) { |
| | @@ -425,18 +429,16 @@ |
| 425 | 429 | } |
| 426 | 430 | |
| 427 | 431 | $sigValue = base64_encode($signature); |
| 428 | | - $headers['Signature'] = "keyId=\"https://{$this->domain}/ap/actor#main-key\","; |
| 432 | + $headers['Signature'] = "keyId=\"{$this->domain}/ap/actor#main-key\","; |
| 429 | 433 | $headers['Signature'] .= 'algorithm="rsa-sha256",'; |
| 430 | 434 | $headers['Signature'] .= 'headers="host date digest",'; |
| 431 | 435 | $headers['Signature'] .= 'signature="'.$sigValue.'"'; |
| 432 | | - logger(\LogType::ActivityPub, |
| 433 | | - "署名: {$headers['Signature']}\n送信データ: {$body}"); |
| 436 | + logger(\LogType::ActivityPub, "署名: {$headers['Signature']}\n送信データ: {$body}"); |
| 434 | 437 | |
| 435 | 438 | $curl = new Curl($inboxUrl); |
| 436 | 439 | $curl->setMethod('POST') |
| 437 | 440 | ->setPostRaw($body) |
| 438 | | - ->setHeaders(array_map(fn($k, $v) => "$k: $v", |
| 439 | | - array_keys($headers), $headers)) |
| 441 | + ->setHeaders(array_map(fn($k, $v) => "$k: $v", array_keys($headers), $headers)) |
| 440 | 442 | ->setCaInfo('/etc/ssl/cert.pem') |
| 441 | 443 | ->setVerbose(true) |
| 442 | 444 | ->setStderr(fopen(ROOT.'/log/ap_log.txt', 'a')); |
| | @@ -447,8 +449,7 @@ |
| 447 | 449 | $err = $curl->getError(); |
| 448 | 450 | |
| 449 | 451 | var_dump(print_r($res)); |
| 450 | | - logger(\LogType::ActivityPub, |
| 451 | | - "アクティビティは「{$inboxUrl}」に送信しました: HTTP {$code}"); |
| 452 | + logger(\LogType::ActivityPub, "アクティビティは「{$inboxUrl}」に送信しました: HTTP {$code}"); |
| 452 | 453 | logger(\LogType::ActivityPub, "エラー: {$err}"); |
| 453 | 454 | logger(\LogType::ActivityPub, "レスポンス: {$res}"); |
| 454 | 455 | } |
| | @@ -483,9 +484,9 @@ |
| 483 | 484 | 'https://www.w3.org/ns/activitystreams', |
| 484 | 485 | 'https://w3id.org/security/v1', |
| 485 | 486 | ], |
| 486 | | - 'id' => "https://{$this->domain}/ap/activities/".uniqid(), |
| 487 | + 'id' => "{$this->domain}/ap/activities/".uniqid(), |
| 487 | 488 | 'type' => 'Accept', |
| 488 | | - 'actor' => "https://{$this->domain}/ap/actor", |
| 489 | + 'actor' => "{$this->domain}/ap/actor", |
| 489 | 490 | 'object' => $activity, |
| 490 | 491 | ]; |
| 491 | 492 | |
| | @@ -506,9 +507,7 @@ |
| 506 | 507 | } |
| 507 | 508 | |
| 508 | 509 | $followers = $this->getFollowersList(); |
| 509 | | - if (!in_array($followerActor, $followers)) { |
| 510 | | - file_put_contents($file, "$followerActor\n", FILE_APPEND); |
| 511 | | - } |
| 510 | + if (!in_array($followerActor, $followers)) file_put_contents($file, "$followerActor\n", FILE_APPEND); |
| 512 | 511 | } |
| 513 | 512 | |
| 514 | 513 | /** |
| | @@ -519,8 +518,7 @@ |
| 519 | 518 | private function getFollowersList(): array { |
| 520 | 519 | $file = ROOT.'/data/followers.txt'; |
| 521 | 520 | $f = array_filter(explode("\n", file_get_contents($file))); |
| 522 | | - return file_exists($file) |
| 523 | | - ? array_filter(explode("\n", file_get_contents($file))) : []; |
| 521 | + return file_exists($file) ? array_filter(explode("\n", file_get_contents($file))) : []; |
| 524 | 522 | } |
| 525 | 523 | |
| 526 | 524 | /** |