| 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 Site\Controller\BlogPost; |
| 41 | use Site\Controller\Mods; |
| 42 | use Std\Lib\Activitypub; |
| 43 | use Std\Lib\Auth; |
| 44 | use Std\Lib\Markdown; |
| 45 | use Std\Lib\Template; |
| 46 | use Roles; |
| 47 | |
| 48 | class Home extends BlogPost { |
| 49 | use Mods; |
| 50 | |
| 51 | private array $searchKeywords = []; |
| 52 | private string $domain = 'technicalsuwako.moe'; |
| 53 | |
| 54 | //------------------------------------------ |
| 55 | // ページ |
| 56 | //------------------------------------------ |
| 57 | |
| 58 | /** |
| 59 | * ブログ投稿ページ |
| 60 | * |
| 61 | * @param array $params ページ番号等 |
| 62 | * @return void |
| 63 | */ |
| 64 | public function show(array $params): void { |
| 65 | try { |
| 66 | $page = isset($_GET['page']) ? $_GET['page'] : 1; |
| 67 | $postsPerPage = 20; |
| 68 | |
| 69 | $tmpl = new Template('/'); |
| 70 | $pagetit = 'トップページ'; |
| 71 | |
| 72 | $description = 'テクニカル諏訪子ちゃんの個人ブログ'; |
| 73 | |
| 74 | // ユーザー |
| 75 | $auth = new Auth(); |
| 76 | $user = $auth->getLoggedInUser(); |
| 77 | |
| 78 | $posts = $this->getPosts('/blog/', $user); |
| 79 | if (!is_array($posts)) $posts = []; |
| 80 | |
| 81 | // 検索機能が使用されている場合 |
| 82 | if (isset($_GET['q']) && !empty($_GET['q'])) { |
| 83 | $this->searchKeywords = array_map('trim', explode(',', $_GET['q'])); |
| 84 | $posts = $this->searchPosts($this->searchKeywords, $posts, '/blog/'); |
| 85 | $pagetit = '検索結果: '.htmlspecialchars($_GET['q']); |
| 86 | |
| 87 | // 検索結果にキーワードをハイライト |
| 88 | $posts = $this->highlightKeywords($posts); |
| 89 | } |
| 90 | |
| 91 | // ページネーション |
| 92 | $totalPosts = count($posts); |
| 93 | $totalPages = ceil($totalPosts / $postsPerPage); |
| 94 | $page = min($page, $totalPages); |
| 95 | $currentPosts = array_slice( |
| 96 | $posts, |
| 97 | ($page - 1) * $postsPerPage, |
| 98 | $postsPerPage |
| 99 | ); |
| 100 | |
| 101 | $tmpl->assign('user', $user); |
| 102 | $tmpl->assign('currentPage', $page); |
| 103 | $tmpl->assign('totalPages', $totalPages); |
| 104 | $tmpl->assign('posts', $currentPosts); |
| 105 | $tmpl->assign('pagetit', $pagetit); |
| 106 | $tmpl->assign('curblog', 'gd'); |
| 107 | $tmpl->assign('section', 'blog'); |
| 108 | $tmpl->assign('curPage', 'blog'); |
| 109 | $tmpl->assign('custCss', false); |
| 110 | $tmpl->assign('menu', $this->getMenu()); |
| 111 | $tmpl->assign('description', $description); |
| 112 | $tmpl->assign('searchActive', !empty($this->searchKeywords)); |
| 113 | |
| 114 | $tmpl->addCss('news'); |
| 115 | $tmpl->addCss('search'); |
| 116 | $tmpl->addCss('blogtype'); |
| 117 | $tmpl->addCss('pagination'); |
| 118 | $tmpl->render('home'); |
| 119 | } catch (\Exception $e) { |
| 120 | throw new \Exception($e->getMessage()); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * ブログ投稿ページ |
| 126 | * |
| 127 | * @param array $params マークダウンファイル等 |
| 128 | * @return void |
| 129 | */ |
| 130 | public function article(array $params): void { |
| 131 | $page = ''; |
| 132 | if (isset($params['page'])) $page = $params['page']; |
| 133 | |
| 134 | try { |
| 135 | $tmpl = new Template('/'); |
| 136 | $md = new Markdown($page, '/blog/'); |
| 137 | |
| 138 | $meta = $md->getMetadata(); |
| 139 | $pagetit = $meta->title; |
| 140 | $article = $md->parse(); |
| 141 | $description = 'テクニカル諏訪子ちゃんの個人ブログ'; |
| 142 | |
| 143 | // 検索からの遷移の場合、記事内のキーワードをハイライト |
| 144 | if (isset($_GET['q']) && !empty($_GET['q'])) { |
| 145 | $keywords = array_map('trim', explode(',', $_GET['q'])); |
| 146 | $article = $this->highlightTextContent($article, $keywords); |
| 147 | $meta->title = $this->highlightTextContent($meta->title, $keywords); |
| 148 | } |
| 149 | |
| 150 | // ユーザー |
| 151 | $auth = new Auth(); |
| 152 | $user = $auth->getLoggedInUser(); |
| 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)); |
| 156 | $tmpl->assign('user', $user); |
| 157 | |
| 158 | $tmpl->assign('pagetit', $pagetit); |
| 159 | $tmpl->assign('curPage', 'blog'); |
| 160 | $tmpl->assign('custCss', false); |
| 161 | $tmpl->assign('curblog', 'gd'); |
| 162 | $tmpl->assign('section', 'blog'); |
| 163 | $tmpl->assign('menu', $this->getMenu()); |
| 164 | $tmpl->assign('article', $article); |
| 165 | $tmpl->assign('meta', $meta); |
| 166 | $tmpl->assign('description', $description); |
| 167 | |
| 168 | if (isset($meta->css) && !empty($meta->css)) { |
| 169 | foreach ($meta->css as $v) { |
| 170 | $tmpl->addCss($v); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | if (!$isMember && in_array('memberonly', $meta->category)) goto denyaccess; |
| 175 | if ((!$isPaywall && !$isStaff) && in_array('subscriberonly', $meta->category)) goto denyaccess; |
| 176 | if (!$isStaff && in_array('staffonly', $meta->category)) goto denyaccess; |
| 177 | if (isset($meta->draft) && !$isStaff) goto denyaccess; |
| 178 | |
| 179 | showpage: |
| 180 | $this->assignComments($tmpl, 'blog', $page, $meta, $user); |
| 181 | $tmpl->addCss('news-article'); |
| 182 | $tmpl->addCss('search'); |
| 183 | $tmpl->addCss('blogtype'); |
| 184 | $tmpl->addCss('code'); |
| 185 | $tmpl->render('article'); |
| 186 | exit(); |
| 187 | |
| 188 | denyaccess: |
| 189 | unset($md, $meta, $pagetit, $article, $description); |
| 190 | $tmpl->assign('pagetit', 'Not found'); |
| 191 | $tmpl->assign('curPage', '404'); |
| 192 | $tmpl->assign('menu', $this->getMenu()); |
| 193 | $tmpl->assign('description', ''); |
| 194 | $tmpl->render('404'); |
| 195 | } catch (\Exception $e) { |
| 196 | throw new \Exception($e->getMessage()); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | public function robots(array $params): void { |
| 201 | $txt = "User-Agent: *\nDisallow: /login/\nDisallow: /register/"; |
| 202 | $tmpFile = tempnam(sys_get_temp_dir(), 'tmpl_'); |
| 203 | file_put_contents($tmpFile, $txt); |
| 204 | |
| 205 | include $tmpFile; |
| 206 | unlink($tmpFile); |
| 207 | } |
| 208 | |
| 209 | //------------------------------------------ |
| 210 | // 機能性 |
| 211 | //------------------------------------------ |
| 212 | |
| 213 | /** |
| 214 | * キーワードに基づいて投稿を検索する |
| 215 | * |
| 216 | * @param array $keywords 検索キーワードの配列 |
| 217 | * @param array $posts 検索対象の投稿記事の配列 |
| 218 | * @return array 検索条件に一致する投稿記事の配列 |
| 219 | */ |
| 220 | private function searchPosts(array $keywords, array $posts, string $section): array { |
| 221 | if (empty($keywords) || empty($posts)) { |
| 222 | return $posts; |
| 223 | } |
| 224 | |
| 225 | $foundPosts = []; |
| 226 | $path = ROOT.$section; |
| 227 | |
| 228 | foreach ($posts as $post) { |
| 229 | $matched = false; |
| 230 | |
| 231 | // タイトルで検索 |
| 232 | foreach ($keywords as $keyword) { |
| 233 | $keyword = trim($keyword); |
| 234 | if (empty($keyword)) continue; |
| 235 | |
| 236 | // タイトル内でキーワードが見つかった場合 |
| 237 | if (mb_stripos($post['title'], $keyword) !== false) { |
| 238 | $foundPosts[] = $post; |
| 239 | $matched = true; |
| 240 | break; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | // すでにマッチしていれば次の記事へ |
| 245 | if ($matched) continue; |
| 246 | |
| 247 | // 記事の本文をチェック |
| 248 | $slug = $post['slug']; |
| 249 | $filePath = $path.$slug.'.md'; |
| 250 | |
| 251 | if (file_exists($filePath)) { |
| 252 | $content = file_get_contents($filePath); |
| 253 | $parts = explode('----', $content, 2); |
| 254 | if (count($parts) > 1) { |
| 255 | // コメントは検索しない(非公開のメモが検索で当たらない様に) |
| 256 | $articleBody = trim(Markdown::stripComments($parts[1])); |
| 257 | |
| 258 | foreach ($keywords as $keyword) { |
| 259 | $keyword = trim($keyword); |
| 260 | if (empty($keyword)) continue; |
| 261 | |
| 262 | // 本文内でキーワードが見つかった場合 |
| 263 | if (mb_stripos($articleBody, $keyword) !== false) { |
| 264 | $foundPosts[] = $post; |
| 265 | break; |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | return $foundPosts; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * 検索結果の投稿内のキーワードをハイライトする |
| 277 | * |
| 278 | * @param array $posts 検索結果の投稿配列 |
| 279 | * @return array ハイライト処理後の投稿配列 |
| 280 | */ |
| 281 | private function highlightKeywords(array $posts): array { |
| 282 | if (empty($this->searchKeywords) || empty($posts)) { |
| 283 | return $posts; |
| 284 | } |
| 285 | |
| 286 | foreach ($posts as &$post) { |
| 287 | // タイトルのハイライト |
| 288 | if (!empty($post['title'])) { |
| 289 | $post['title'] = |
| 290 | $this->highlightTextContent($post['title'], $this->searchKeywords); |
| 291 | } |
| 292 | |
| 293 | // プレビューのハイライト |
| 294 | if (!empty($post['preview'])) { |
| 295 | $post['preview'] = |
| 296 | $this->highlightTextContent($post['preview'], $this->searchKeywords); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | return $posts; |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * テキスト内のキーワードをハイライトする |
| 305 | * |
| 306 | * @param string $text ハイライト対象のテキスト |
| 307 | * @param array $keywords ハイライトするキーワード配列 |
| 308 | * @return string ハイライト処理後のテキスト |
| 309 | */ |
| 310 | private function highlightTextContent(string $text, array $keywords): string { |
| 311 | if (empty($keywords) || empty($text)) { |
| 312 | return $text; |
| 313 | } |
| 314 | |
| 315 | $alts = []; |
| 316 | |
| 317 | foreach ($keywords as $keyword) { |
| 318 | $keyword = trim($keyword); |
| 319 | $escaped = htmlspecialchars($keyword, ENT_QUOTES, 'UTF-8'); |
| 320 | if ($escaped === '') continue; |
| 321 | $alts[] = preg_quote($escaped, '/'); |
| 322 | } |
| 323 | if (empty($alts)) return $text; |
| 324 | $pattern = '/('.implode('|', $alts).')/iu'; |
| 325 | |
| 326 | $parts = preg_split('/(<[^>]*>|&[#\w]+;)/u', $text, -1, PREG_SPLIT_DELIM_CAPTURE); |
| 327 | if ($parts === false) return $text; |
| 328 | |
| 329 | foreach ($parts as $i => $part) { |
| 330 | if ($part === '' || $part[0] === '<' || $part[0] === '&') continue; |
| 331 | $res = preg_replace($pattern, '<span class="search-highlight">$1</span>', $part); |
| 332 | if ($res !== null) $parts[$i] = $res; |
| 333 | } |
| 334 | |
| 335 | return implode('', $parts); |
| 336 | } |
| 337 | } |