110 lines
4.3 KiB
PHP
110 lines
4.3 KiB
PHP
<?php
|
|
/*************************************************************
|
|
# 076 License
|
|
|
|
Copyright (c) テクニカル諏訪子
|
|
|
|
Permission is hereby granted to any person obtaining a copy of the software
|
|
Little Beast (the "Software") to use, modify, merge, copy, publish, distribute,
|
|
sublicense, and/or sell copies of the Software, subject to the following conditions:
|
|
|
|
1. **Origin Attribution**:
|
|
- You must not misrepresent the origin of the Software; you must not claim
|
|
you created the original Software.
|
|
- If the Software is used in a product, you must either:
|
|
a. Provide clear attribution in the product's documentation, user interface,
|
|
or other visible areas, **OR**
|
|
b. Pay the original developers a fee they specify in writing.
|
|
2. **Usage Restriction**:
|
|
- The Software, or any derivative works, dependencies, or libraries
|
|
incorporating it, must not be used for censorship or to suppress freedom of
|
|
speech, expression, or creativity. Prohibited uses include, but are not
|
|
limited to:
|
|
- Censorship of so-called "hate speech", visuals, non-mainstream opinions,
|
|
ideas, or objective reality.
|
|
- Tools or systems designed to restrict access to information or
|
|
artistic works.
|
|
3. **Notice Preservation**:
|
|
- This license and the above copyright notice must remain intact in all copies
|
|
of the source code.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
|
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
|
|
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
*/
|
|
namespace Site\Controller;
|
|
|
|
class BlogPost {
|
|
/**
|
|
* ブログ投稿を取得する
|
|
*
|
|
* @return array 投稿の配列
|
|
*/
|
|
public function getPosts(string $section, ?\stdClass $user = null): array {
|
|
$path = ROOT.$section;
|
|
$posts = [];
|
|
$isMember = $user !== NULL && $user->role !== \Roles::BANNED;
|
|
$isPaywall = $user !== NULL && $user->role >= \Roles::SUBSCRIBER;
|
|
$isStaff = $user !== NULL && $user->role & (\Roles::ADMIN | \Roles::STAFF);
|
|
|
|
if (!is_dir($path)) return $posts;
|
|
$files = glob($path.'/*.md');
|
|
|
|
foreach ($files as $file) {
|
|
$content = file_get_contents($file);
|
|
$parts = explode('----', $content, 2);
|
|
if (count($parts) != 2) continue;
|
|
|
|
$metadata = [];
|
|
$meta = explode("\n", trim($parts[0]));
|
|
|
|
foreach ($meta as $line) {
|
|
$line = trim($line);
|
|
if (empty($line)) continue;
|
|
|
|
$colonPos = strpos($line, ':');
|
|
if ($colonPos === false) continue;
|
|
|
|
$key = trim(substr($line, 0, $colonPos));
|
|
$value = trim(substr($line, $colonPos + 1));
|
|
$value = trim($value, '"\'');
|
|
|
|
if ($key == 'category') {
|
|
$metadata[$key] = array_map('trim', explode(',', $value));
|
|
} else {
|
|
$metadata[$key] = $value;
|
|
}
|
|
}
|
|
|
|
$articleBody = trim($parts[1]);
|
|
$preview = mb_substr(strip_tags($articleBody), 0, 50).'...';
|
|
$slug = basename($file, '.md');
|
|
if (isset($metadata['draft']) && !$isStaff) continue;
|
|
$cannotPreview = (!$isMember && in_array('memberonly', $metadata['category']))
|
|
|| ((!$isPaywall && !$isStaff) && in_array('subscriberonly', $metadata['category']))
|
|
|| (!$isStaff && in_array('staffonly', $metadata['category']));
|
|
|
|
$posts[] = [
|
|
'title' => $metadata['title'] ?? '',
|
|
'date' => $metadata['date'] ?? '',
|
|
'thumbnail' => $metadata['thumbnail'] ?? '',
|
|
'thumborient' => $metadata['thumborient'] ?? '',
|
|
'category' => $metadata['category'] ?? [],
|
|
'uuid' => $metadata['uuid'] ?? '',
|
|
'preview' => ($cannotPreview ? '未許可' : $preview),
|
|
'slug' => $slug,
|
|
];
|
|
}
|
|
|
|
// 日付でソート(新しい順)
|
|
usort($posts, function($a, $b) {
|
|
return strtotime($b['date']) - strtotime($a['date']);
|
|
});
|
|
|
|
return $posts;
|
|
}
|
|
}
|