pixelfed/app/Http/Controllers/PageController.php

121 lines
2.9 KiB
PHP
Raw Permalink Normal View History

2019-01-31 19:52:05 +00:00
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
2019-06-20 02:54:48 +00:00
use Auth, Cache;
2019-01-31 19:52:05 +00:00
use App\Page;
2022-08-16 05:39:33 +00:00
use App\Services\ConfigCacheService;
2019-01-31 19:52:05 +00:00
class PageController extends Controller
{
public function __construct()
{
$this->middleware(['auth', 'admin']);
}
2019-06-20 02:54:48 +00:00
protected function cacheKeys() {
return [
'/site/about' => 'site:about',
'/site/privacy' => 'site:privacy',
'/site/terms' => 'site:terms',
2022-08-16 05:39:33 +00:00
'/site/kb/community-guidelines' => 'site:help:community-guidelines',
'/site/legal-notice' => 'site:legal-notice'
2019-06-20 02:54:48 +00:00
];
}
2019-01-31 19:52:05 +00:00
protected function authCheck($admin_only = false)
{
$auth = $admin_only ?
Auth::check() && Auth::user()->is_admin == true :
Auth::check();
if($auth == false) {
abort(403);
}
}
public function edit(Request $request)
{
$this->authCheck(true);
$this->validate($request, [
'page' => 'required|string'
]);
$slug = urldecode($request->page);
2019-06-20 03:06:10 +00:00
if(in_array($slug, array_keys($this->cacheKeys())) == false) {
return redirect(route('admin.settings.pages'));
}
2019-01-31 19:52:05 +00:00
$page = Page::firstOrCreate(['slug' => $slug]);
return view('admin.pages.edit', compact('page'));
}
public function store(Request $request)
{
$this->validate($request, [
'slug' => 'required|string',
'content' => 'required|string',
'title' => 'nullable|string',
'active' => 'required|boolean'
]);
$slug = urldecode($request->input('slug'));
$page = Page::firstOrCreate(['slug' => $slug]);
$page->content = $request->input('content');
$page->title = $request->input('title');
$page->active = (bool) $request->input('active');
$page->save();
2022-08-16 05:39:33 +00:00
$keys = $this->cacheKeys();
$key = $keys[$page->slug];
Cache::forget($key);
if($page->slug === '/site/legal-notice') {
ConfigCacheService::put('instance.has_legal_notice', $page->active);
2019-06-20 02:54:48 +00:00
}
2019-01-31 19:52:05 +00:00
return response()->json(['msg' => 200]);
}
2019-06-19 20:30:33 +00:00
2019-06-20 02:54:48 +00:00
public function delete(Request $request)
{
$this->validate($request, [
'id' => 'required|integer|min:1|exists:pages,id'
]);
$page = Page::findOrFail($request->input('id'));
2022-08-16 05:39:33 +00:00
$keys = $this->cacheKeys();
$key = $keys[$page->slug];
2019-06-20 02:54:48 +00:00
$page->delete();
2022-08-16 05:39:33 +00:00
Cache::forget($key);
2019-06-20 02:54:48 +00:00
return redirect(route('admin.settings.pages'));
}
2019-06-19 20:30:33 +00:00
public function generatePage(Request $request)
{
$this->validate($request, [
2022-08-16 05:39:33 +00:00
'page' => 'required|string|in:about,terms,privacy,community_guidelines,legal_notice',
2019-06-19 20:30:33 +00:00
]);
$page = $request->input('page');
switch ($page) {
case 'about':
Page::firstOrCreate(['slug' => '/site/about']);
break;
case 'privacy':
Page::firstOrCreate(['slug' => '/site/privacy']);
break;
case 'terms':
Page::firstOrCreate(['slug' => '/site/terms']);
break;
2019-06-27 06:49:12 +00:00
case 'community_guidelines':
Page::firstOrCreate(['slug' => '/site/kb/community-guidelines']);
break;
2022-08-16 05:39:33 +00:00
case 'legal_notice':
Page::firstOrCreate(['slug' => '/site/legal-notice']);
break;
2019-06-19 20:30:33 +00:00
}
return redirect(route('admin.settings.pages'));
}
2019-01-31 19:52:05 +00:00
}