152 lines
7.6 KiB
PHP
152 lines
7.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Editorial;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Articles;
|
|
use App\Models\Categories;
|
|
use App\Services\Editorial\ContentRenderer;
|
|
use App\Services\Editorial\PublicationEffects;
|
|
use App\Services\Editorial\Publisher;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\URL;
|
|
|
|
final class EditorialController extends Controller
|
|
{
|
|
public function catalog()
|
|
{
|
|
return response()->json(['categories' => DB::table('categories')->where('iStatus', 1)->whereNotIn('sSlug', ['freenode', 'shadowrocketids'])->get(['id', 'sName', 'sSlug', 'iParentId']),
|
|
'articles' => DB::table('articles')->leftJoin('editorial_documents', 'articles.id', '=', 'editorial_documents.article_id')
|
|
->whereNotIn('iCategoryId', DB::table('categories')->whereIn('sSlug', ['freenode', 'shadowrocketids'])->pluck('id'))
|
|
->get(['articles.id', 'sTitle', 'sSlug', 'iCategoryId', 'iStatus', 'content_key', 'revision', 'published_revision'])]);
|
|
}
|
|
|
|
public function source(int $article)
|
|
{
|
|
$a = DB::table('articles')->where('id', $article)->first();
|
|
abort_unless($a, 404);
|
|
$category = DB::table('categories')->where('id', $a->iCategoryId)->first();
|
|
abort_unless($category && ! in_array($category->sSlug, ['freenode', 'shadowrocketids']), 422, 'Not an editorial article');
|
|
|
|
return response()->json(['article_id' => $a->id, 'title' => $a->sTitle, 'summary' => $a->sTitleSub,
|
|
'slug' => $a->sSlug, 'body_html' => $a->sContent, 'category_slug' => $category->sSlug,
|
|
'tag_ids' => DB::table('article_tag')->where('iArticleId', $a->id)->pluck('iTagId'),
|
|
'cover' => $a->sThumb, 'show_cover' => (bool) $a->bPageThumb,
|
|
'like_growth' => ['seven_day_remaining' => (int) $a->i7like, 'yearly' => (int) $a->i365like,
|
|
'one_year_target' => (int) $a->iYearLikeTarget, 'one_year_added' => (int) $a->iYearLikeAdded,
|
|
'one_year_started_at' => $a->sYearLikeStartedAt],
|
|
'management' => DB::table('editorial_documents')->where('article_id', $a->id)->first()]);
|
|
}
|
|
|
|
public function store(Request $r, Publisher $p)
|
|
{
|
|
return response()->json($p->draft($r->all()), 201);
|
|
}
|
|
|
|
public function draft(string $draft, Publisher $p)
|
|
{
|
|
return response()->json($p->getDraft($draft));
|
|
}
|
|
|
|
public function publish(Request $r, string $draft, Publisher $p)
|
|
{
|
|
return response()->json($p->publish($draft, $r->all()));
|
|
}
|
|
|
|
public function article(string $key, Publisher $p)
|
|
{
|
|
return response()->json($p->article($key));
|
|
}
|
|
|
|
public function rollback(Request $r, string $key, Publisher $p)
|
|
{
|
|
return response()->json($p->rollback($key, $r->all()), 201);
|
|
}
|
|
|
|
public function receipt(string $publication, Publisher $p)
|
|
{
|
|
return response()->json($p->receipt($publication));
|
|
}
|
|
|
|
public function synchronize(string $publication, PublicationEffects $e)
|
|
{
|
|
return response()->json($e->synchronize($publication));
|
|
}
|
|
|
|
public function version(string $key, int $revision)
|
|
{
|
|
$v = DB::table('editorial_versions')->where('content_key', $key)->where('revision', $revision)->first();
|
|
abort_unless($v, 404);
|
|
|
|
return response()->json(['content_key' => $key, 'revision' => $revision, 'content' => json_decode($v->snapshot, true)]);
|
|
}
|
|
|
|
public function upload(Request $r, ContentRenderer $renderer)
|
|
{
|
|
$r->validate(['image' => 'required|file|max:8192|mimetypes:image/png,image/jpeg,image/webp,image/gif']);
|
|
$file = $r->file('image');
|
|
$info = @getimagesize($file->getRealPath());
|
|
abort_unless($info && $info[0] <= 12000 && $info[1] <= 12000 && $info[0] * $info[1] <= 40000000, 422, 'Image dimensions invalid');
|
|
$mime = $info['mime'];
|
|
$ext = ['image/png' => 'png', 'image/jpeg' => 'jpg', 'image/webp' => 'webp', 'image/gif' => 'gif'][$mime] ?? null;
|
|
abort_unless($ext && $file->getMimeType() === $mime, 422, 'Invalid image encoding');
|
|
$sha = hash_file('sha256', $file->getRealPath());
|
|
$dest = $renderer->privateImage($sha);
|
|
if (! is_dir(dirname($dest))) {
|
|
mkdir(dirname($dest), 0700, true);
|
|
}
|
|
if (! is_file($dest)) {
|
|
$temp = $dest.'.'.bin2hex(random_bytes(8));
|
|
if (! copy($file->getRealPath(), $temp) || ! rename($temp, $dest)) {
|
|
throw new \RuntimeException('Image storage failed');
|
|
}
|
|
chmod($dest, 0600);
|
|
}
|
|
DB::table('editorial_assets')->insertOrIgnore(['sha256' => $sha, 'extension' => $ext, 'mime' => $mime, 'bytes' => filesize($dest), 'created_at' => now()]);
|
|
|
|
return response()->json(['sha256' => $sha, 'reference' => 'asset:'.$sha, 'mime' => $mime, 'width' => $info[0], 'height' => $info[1]], 201);
|
|
}
|
|
|
|
public function asset(string $sha, ContentRenderer $renderer)
|
|
{
|
|
abort_unless(config('editorial.enabled'), 503);
|
|
$a = DB::table('editorial_assets')->where('sha256', $sha)->first();
|
|
abort_unless($a && is_file($renderer->privateImage($sha)), 404);
|
|
|
|
return response()->file($renderer->privateImage($sha), ['Content-Type' => $a->mime, 'Cache-Control' => 'private, no-store', 'X-Robots-Tag' => 'noindex, nofollow', 'X-Content-Type-Options' => 'nosniff']);
|
|
}
|
|
|
|
public function preview(string $draft, string $key, Publisher $p)
|
|
{
|
|
abort_unless(config('editorial.enabled'), 503);
|
|
$record = DB::table('editorial_drafts')->where('id', $draft)->first();
|
|
abort_unless($record, 404);
|
|
$items = json_decode($record->items, true);
|
|
$item = collect($items)->firstWhere('content_key', $key);
|
|
abort_unless($item, 404);
|
|
$expires = now()->addMinutes(config('editorial.preview_minutes'));
|
|
$base = rtrim(config('editorial.base_url'), '/');
|
|
$html = preg_replace_callback('/asset:([a-f0-9]{64})/', fn ($m) => $base.URL::temporarySignedRoute('editorial.preview-asset', $expires, ['sha' => $m[1]], absolute: false), $item['html']);
|
|
// Forward links between articles in this batch stay within the private preview.
|
|
foreach ($items as $target) {
|
|
$preview = $base.URL::temporarySignedRoute('editorial.preview', $expires, ['draft' => $draft, 'key' => $target['content_key']], absolute: false);
|
|
$pattern = '~href="/a/'.preg_quote($target['slug'], '~').'\.html(#[^"]*)?"~';
|
|
$html = preg_replace_callback($pattern, fn ($m) => 'href="'.htmlspecialchars($preview, ENT_QUOTES).($m[1] ?? '').'"', $html);
|
|
}
|
|
$cover = $item['cover'];
|
|
if ($cover && preg_match('/\Aasset:([a-f0-9]{64})\z/', $cover, $m)) {
|
|
$cover = $base.URL::temporarySignedRoute('editorial.preview-asset', $expires, ['sha' => $m[1]], absolute: false);
|
|
}
|
|
$a = new Articles;
|
|
$a->forceFill(['id' => 0, 'sTitle' => $item['title'], 'sTitleSub' => $item['summary'], 'sSlug' => $item['slug'], 'sContent' => $html,
|
|
'created_at' => now(), 'iLikeCount' => 0, 'bPageThumb' => $item['show_cover'] ?? false, 'sThumb' => $cover]);
|
|
$category = Categories::where('sSlug', $item['category_slug'])->with('oParent')->firstOrFail();
|
|
$a->setRelation('oCategory', $category);
|
|
$a->setRelation('oTags', collect());
|
|
|
|
return response()->view('pages.articles.default.show', ['oArticle' => $a, 'bEditorialPreview' => true, 'editorialRevision' => $item['revision'], 'editorialDraftStatus' => $record->status])
|
|
->withHeaders(['Cache-Control' => 'private, no-store', 'X-Robots-Tag' => 'noindex, nofollow', 'Referrer-Policy' => 'no-referrer']);
|
|
}
|
|
}
|