| 1 | <?php |
| 2 | /************************************************************* |
| 3 | # 076 License |
| 4 | |
| 5 | Copyright (c) テクニカル諏訪子 |
| 6 | |
| 7 | Permission is hereby granted to any person obtaining a copy of the software |
| 8 | Little Beast (the "Software") to use, modify, merge, copy, publish, distribute, |
| 9 | sublicense, and/or sell copies of the Software, subject to the following conditions: |
| 10 | |
| 11 | 1. **Origin Attribution**: |
| 12 | - You must not misrepresent the origin of the Software; you must not claim |
| 13 | you created the original Software. |
| 14 | - If the Software is used in a product, you must either: |
| 15 | a. Provide clear attribution in the product's documentation, user interface, |
| 16 | or other visible areas, **OR** |
| 17 | b. Pay the original developers a fee they specify in writing. |
| 18 | 2. **Usage Restriction**: |
| 19 | - The Software, or any derivative works, dependencies, or libraries |
| 20 | incorporating it, must not be used for censorship or to suppress freedom of |
| 21 | speech, expression, or creativity. Prohibited uses include, but are not |
| 22 | limited to: |
| 23 | - Censorship of so-called "hate speech", visuals, non-mainstream opinions, |
| 24 | ideas, or objective reality. |
| 25 | - Tools or systems designed to restrict access to information or |
| 26 | artistic works. |
| 27 | 3. **Notice Preservation**: |
| 28 | - This license and the above copyright notice must remain intact in all copies |
| 29 | of the source code. |
| 30 | |
| 31 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 32 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 33 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 34 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 35 | CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE |
| 36 | OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 37 | */ |
| 38 | namespace Site\Controller; |
| 39 | |
| 40 | use Std\Lib\Activitypub; |
| 41 | use Std\Lib\Comments; |
| 42 | use Std\Lib\Fedi\Fedi; |
| 43 | use Std\Lib\Markdown; |
| 44 | use Std\Lib\Template; |
| 45 | use Roles; |
| 46 | |
| 47 | class BlogPost { |
| 48 | /** |
| 49 | * ブログ投稿を取得する |
| 50 | * |
| 51 | * @return array 投稿の配列 |
| 52 | */ |
| 53 | public function getPosts(string $section, ?\stdClass $user = null): array { |
| 54 | $path = ROOT.$section; |
| 55 | $posts = []; |
| 56 | $isMember = $user !== NULL && $user->role !== Roles::BANNED; |
| 57 | $isPaywall = $user !== NULL && ($user->role & Roles::SUBSCRIBER); |
| 58 | $isStaff = $user !== NULL && ($user->role & (Roles::ADMIN | Roles::STAFF)); |
| 59 | |
| 60 | if (!is_dir($path)) return $posts; |
| 61 | $files = glob($path.'/*.md'); |
| 62 | |
| 63 | foreach ($files as $file) { |
| 64 | $content = file_get_contents($file); |
| 65 | $parts = explode('----', $content, 2); |
| 66 | if (count($parts) != 2) continue; |
| 67 | |
| 68 | $metadata = []; |
| 69 | $meta = explode("\n", trim($parts[0])); |
| 70 | |
| 71 | foreach ($meta as $line) { |
| 72 | $line = trim($line); |
| 73 | if (empty($line)) continue; |
| 74 | |
| 75 | $colonPos = strpos($line, ':'); |
| 76 | if ($colonPos === false) continue; |
| 77 | |
| 78 | $key = trim(substr($line, 0, $colonPos)); |
| 79 | $value = trim(substr($line, $colonPos + 1)); |
| 80 | $value = trim($value, '"\''); |
| 81 | |
| 82 | if ($key == 'category') { |
| 83 | $metadata[$key] = array_map('trim', explode(',', $value)); |
| 84 | } else { |
| 85 | $metadata[$key] = $value; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // コメントはプレビューに出さない |
| 90 | $articleBody = trim(Markdown::stripComments($parts[1])); |
| 91 | $preview = mb_substr(strip_tags($articleBody), 0, 50).'...'; |
| 92 | $slug = basename($file, '.md'); |
| 93 | if (isset($metadata['draft']) && !$isStaff) continue; |
| 94 | $cannotPreview = (!$isMember && in_array('memberonly', $metadata['category'])) |
| 95 | || ((!$isPaywall && !$isStaff) && in_array('subscriberonly', $metadata['category'])) |
| 96 | || (!$isStaff && in_array('staffonly', $metadata['category'])); |
| 97 | |
| 98 | $posts[] = [ |
| 99 | 'title' => $metadata['title'] ?? '', |
| 100 | 'date' => $metadata['date'] ?? '', |
| 101 | 'thumbnail' => $metadata['thumbnail'] ?? '', |
| 102 | 'thumborient' => $metadata['thumborient'] ?? '', |
| 103 | 'category' => $metadata['category'] ?? [], |
| 104 | 'uuid' => $metadata['uuid'] ?? '', |
| 105 | 'preview' => ($cannotPreview ? '未許可' : $preview), |
| 106 | 'slug' => $slug, |
| 107 | ]; |
| 108 | } |
| 109 | |
| 110 | // 日付でソート(新しい順) |
| 111 | usort($posts, function($a, $b) { |
| 112 | return strtotime($b['date']) - strtotime($a['date']); |
| 113 | }); |
| 114 | |
| 115 | return $posts; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * ActivityPubで配信するブログ(config.phpのBLOGSでfederateがtrue)の投稿 |
| 120 | * |
| 121 | * @return array 投稿の配列(「section」はブログのディレクトリ) |
| 122 | */ |
| 123 | public function federatedPosts(): array { |
| 124 | $posts = []; |
| 125 | foreach (blogs() as $name => $b) { |
| 126 | if (!$b['federate']) continue; |
| 127 | foreach ($this->getPosts("/{$name}/", null) as $p) $posts[] = $p + ['section' => $name]; |
| 128 | } |
| 129 | return $posts; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * 記事のコメント欄をテンプレートに割り当てる(COMMENTS_ENABLEDが無効なら何もしない) |
| 134 | * |
| 135 | * @param Template $tmpl テンプレート |
| 136 | * @param string $section 記事のディレクトリ(「blog」「tutorial/linalg」等) |
| 137 | * @param string $slug 記事 |
| 138 | * @param \stdClass $meta 記事のメタデータ |
| 139 | * @param \stdClass|null $user ログイン中のユーザー |
| 140 | * @return void |
| 141 | */ |
| 142 | protected function assignComments(Template $tmpl, string $section, string $slug, \stdClass $meta, ?\stdClass $user): void { |
| 143 | if (!Comments::enabled()) return; |
| 144 | $thread = "{$section}/{$slug}"; |
| 145 | |
| 146 | // 連合するブログの記事には、Fediverseからも返信出来る |
| 147 | $fedi = null; |
| 148 | if (ACTIVITYPUB_ENABLED && (blogs()[$section]['federate'] ?? false)) { |
| 149 | $post = ['uuid' => (string)($meta->uuid ?? ''), 'date' => (string)($meta->date ?? ''), 'category' => (array)($meta->category ?? []), |
| 150 | 'slug' => $slug, 'section' => $section, 'title' => (string)$meta->title]; |
| 151 | $fedi = (new Activitypub([$post]))->objectId($post); |
| 152 | } |
| 153 | |
| 154 | $tmpl->assign('comments', [ |
| 155 | 'thread' => $thread, |
| 156 | 'list' => Comments::view($thread, $user), |
| 157 | 'count' => Comments::count($thread), |
| 158 | 'canPost' => Comments::canPost($user), |
| 159 | 'maxLength' => Comments::MAX_LENGTH, |
| 160 | 'fedi' => $fedi, |
| 161 | 'actor' => $fedi !== null ? '@'.FEDIINFO['actor'].'@'.Fedi::host() : '', |
| 162 | ]); |
| 163 | // クッキーはテンプレートの出力の前に設定する |
| 164 | if ($user !== null) $tmpl->assign('csrf', make_csrf_token()); |
| 165 | $tmpl->addCss('comments'); |
| 166 | } |
| 167 | } |