2021-10-26 15:54:56 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
/**
|
2022-02-19 16:06:11 +00:00
|
|
|
* @copyright 2020 Ad Aures
|
2021-10-26 15:54:56 +00:00
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Modules\Admin\Controllers;
|
|
|
|
|
2023-03-16 13:00:05 +00:00
|
|
|
use App\Entities\Podcast;
|
2022-01-13 16:02:14 +00:00
|
|
|
use App\Models\ActorModel;
|
2022-01-14 17:42:55 +00:00
|
|
|
use App\Models\EpisodeCommentModel;
|
|
|
|
use App\Models\EpisodeModel;
|
2021-11-23 11:54:34 +00:00
|
|
|
use App\Models\PersonModel;
|
|
|
|
use App\Models\PodcastModel;
|
2022-01-14 17:42:55 +00:00
|
|
|
use App\Models\PostModel;
|
2021-10-26 15:54:56 +00:00
|
|
|
use CodeIgniter\HTTP\RedirectResponse;
|
2023-03-16 13:00:05 +00:00
|
|
|
use Modules\Media\Entities\Audio;
|
|
|
|
use Modules\Media\FileManagers\FileManagerInterface;
|
|
|
|
use Modules\Media\FileManagers\FS;
|
|
|
|
use Modules\Media\Models\MediaModel;
|
2021-10-26 15:54:56 +00:00
|
|
|
use PHP_ICO;
|
|
|
|
|
|
|
|
class SettingsController extends BaseController
|
|
|
|
{
|
|
|
|
public function index(): string
|
|
|
|
{
|
|
|
|
helper('form');
|
|
|
|
return view('settings/general');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function attemptInstanceEdit(): RedirectResponse
|
|
|
|
{
|
|
|
|
$rules = [
|
|
|
|
'site_icon' =>
|
2021-11-01 17:12:03 +00:00
|
|
|
'is_image[site_icon]|ext_in[site_icon,png,jpeg]|is_image_ratio[site_icon,1,1]|min_dims[image,512,512]|permit_empty',
|
2021-10-26 15:54:56 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
if (! $this->validate($rules)) {
|
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->withInput()
|
|
|
|
->with('errors', $this->validator->getErrors());
|
|
|
|
}
|
|
|
|
|
|
|
|
$siteName = $this->request->getPost('site_name');
|
|
|
|
if ($siteName !== service('settings')->get('App.siteName')) {
|
|
|
|
service('settings')->set('App.siteName', $siteName);
|
|
|
|
}
|
|
|
|
|
|
|
|
$siteDescription = $this->request->getPost('site_description');
|
|
|
|
if ($siteDescription !== service('settings')->get('App.siteDescription')) {
|
|
|
|
service('settings')->set('App.siteDescription', $siteDescription);
|
|
|
|
}
|
|
|
|
|
|
|
|
$siteIconFile = $this->request->getFile('site_icon');
|
|
|
|
if ($siteIconFile !== null && $siteIconFile->isValid()) {
|
|
|
|
helper(['filesystem', 'media']);
|
|
|
|
|
|
|
|
// delete site folder in media before repopulating it
|
2023-03-28 16:13:04 +00:00
|
|
|
delete_files(media_path_absolute('/site'));
|
2021-10-26 15:54:56 +00:00
|
|
|
|
|
|
|
// save original in disk
|
2023-03-16 13:00:05 +00:00
|
|
|
$originalFilename = (new FS(config('Media')))->save(
|
|
|
|
$siteIconFile,
|
|
|
|
'site/icon.' . $siteIconFile->getExtension()
|
|
|
|
);
|
2021-10-26 15:54:56 +00:00
|
|
|
|
|
|
|
// convert jpeg image to png if not
|
|
|
|
if ($siteIconFile->getClientMimeType() !== 'image/png') {
|
2023-03-28 16:13:04 +00:00
|
|
|
service('image')->withFile(media_path_absolute($originalFilename))
|
2021-10-26 15:54:56 +00:00
|
|
|
->convert(IMAGETYPE_JPEG)
|
2023-03-28 16:13:04 +00:00
|
|
|
->save(media_path_absolute('/site/icon.png'));
|
2021-10-26 15:54:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// generate random hash to use as a suffix to renew browser cache
|
|
|
|
$randomHash = substr(bin2hex(random_bytes(18)), 0, 8);
|
|
|
|
|
|
|
|
// generate ico
|
|
|
|
$ico_lib = new PHP_ICO();
|
2023-03-28 16:13:04 +00:00
|
|
|
$ico_lib->add_image(media_path_absolute('/site/icon.png'), [[32, 32], [64, 64]]);
|
|
|
|
$ico_lib->save_ico(media_path_absolute("/site/favicon.{$randomHash}.ico"));
|
2021-10-26 15:54:56 +00:00
|
|
|
|
|
|
|
// resize original to needed sizes
|
|
|
|
foreach ([64, 180, 192, 512] as $size) {
|
|
|
|
service('image')
|
2023-03-28 16:13:04 +00:00
|
|
|
->withFile(media_path_absolute('/site/icon.png'))
|
2021-10-26 15:54:56 +00:00
|
|
|
->resize($size, $size)
|
2023-03-28 16:13:04 +00:00
|
|
|
->save(media_path_absolute("/site/icon-{$size}.{$randomHash}.png"));
|
2021-10-26 15:54:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
service('settings')
|
|
|
|
->set('App.siteIcon', [
|
2022-01-13 16:45:08 +00:00
|
|
|
'ico' => '/' . media_path("/site/favicon.{$randomHash}.ico"),
|
|
|
|
'64' => '/' . media_path("/site/icon-64.{$randomHash}.png"),
|
|
|
|
'180' => '/' . media_path("/site/icon-180.{$randomHash}.png"),
|
|
|
|
'192' => '/' . media_path("/site/icon-192.{$randomHash}.png"),
|
|
|
|
'512' => '/' . media_path("/site/icon-512.{$randomHash}.png"),
|
2021-10-26 15:54:56 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2021-11-23 11:54:34 +00:00
|
|
|
return redirect('settings-general')->with('message', lang('Settings.instance.editSuccess'));
|
2021-10-26 15:54:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function deleteIcon(): RedirectResponse
|
|
|
|
{
|
2022-01-13 16:45:08 +00:00
|
|
|
helper(['filesystem', 'media']);
|
2021-10-26 15:54:56 +00:00
|
|
|
// delete site folder in media
|
2023-03-28 16:13:04 +00:00
|
|
|
delete_files(media_path_absolute('/site'));
|
2021-10-26 15:54:56 +00:00
|
|
|
|
|
|
|
service('settings')
|
|
|
|
->forget('App.siteIcon');
|
|
|
|
|
2021-11-23 11:54:34 +00:00
|
|
|
return redirect('settings-general')->with('message', lang('Settings.instance.deleteIconSuccess'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function regenerateImages(): RedirectResponse
|
|
|
|
{
|
2023-03-16 13:00:05 +00:00
|
|
|
/** @var Podcast[] $allPodcasts */
|
2021-11-23 11:54:34 +00:00
|
|
|
$allPodcasts = (new PodcastModel())->findAll();
|
2023-03-16 13:00:05 +00:00
|
|
|
|
|
|
|
/** @var FileManagerInterface $fileManager */
|
|
|
|
$fileManager = service('file_manager');
|
2021-11-23 11:54:34 +00:00
|
|
|
|
|
|
|
foreach ($allPodcasts as $podcast) {
|
2023-03-16 13:00:05 +00:00
|
|
|
$fileManager->deletePodcastImageSizes($podcast->handle);
|
2022-01-29 15:32:38 +00:00
|
|
|
|
2022-01-11 10:51:10 +00:00
|
|
|
$podcast->cover->saveSizes();
|
|
|
|
if ($podcast->banner_id !== null) {
|
|
|
|
$podcast->banner->saveSizes();
|
2021-11-23 11:54:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($podcast->episodes as $episode) {
|
2022-01-11 10:51:10 +00:00
|
|
|
if ($episode->cover_id !== null) {
|
|
|
|
$episode->cover->saveSizes();
|
2021-11-23 11:54:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-16 13:00:05 +00:00
|
|
|
$fileManager->deletePersonImagesSizes();
|
2021-11-23 11:54:34 +00:00
|
|
|
|
|
|
|
$persons = (new PersonModel())->findAll();
|
|
|
|
foreach ($persons as $person) {
|
2022-01-11 10:51:10 +00:00
|
|
|
if ($person->avatar_id !== null) {
|
|
|
|
$person->avatar->saveSizes();
|
2021-11-23 11:54:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect('settings-general')->with('message', lang('Settings.images.regenerationSuccess'));
|
2021-11-08 16:52:20 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 16:02:14 +00:00
|
|
|
public function runHousekeeping(): RedirectResponse
|
|
|
|
{
|
2022-01-14 17:42:55 +00:00
|
|
|
if ($this->request->getPost('reset_counts') === 'yes') {
|
|
|
|
// recalculate fediverse counts
|
|
|
|
(new ActorModel())->resetFollowersCount();
|
|
|
|
(new ActorModel())->resetPostsCount();
|
|
|
|
(new PostModel())->setEpisodeIdForRepliesOfEpisodePosts();
|
|
|
|
(new PostModel())->resetFavouritesCount();
|
|
|
|
(new PostModel())->resetReblogsCount();
|
|
|
|
(new PostModel())->resetRepliesCount();
|
|
|
|
(new EpisodeModel())->resetCommentsCount();
|
|
|
|
(new EpisodeModel())->resetPostsCount();
|
|
|
|
(new EpisodeCommentModel())->resetLikesCount();
|
|
|
|
(new EpisodeCommentModel())->resetRepliesCount();
|
|
|
|
}
|
2022-03-04 14:33:48 +00:00
|
|
|
|
2022-01-29 16:42:34 +00:00
|
|
|
if ($this->request->getPost('clear_cache') === 'yes') {
|
|
|
|
cache()->clean();
|
|
|
|
}
|
|
|
|
|
2022-09-28 15:02:09 +00:00
|
|
|
if ($this->request->getPost('rename_episodes_files') === 'yes') {
|
2023-03-16 13:00:05 +00:00
|
|
|
/** @var Audio[] $allAudio */
|
2022-09-28 15:02:09 +00:00
|
|
|
$allAudio = (new MediaModel('audio'))->getAllOfType();
|
|
|
|
|
|
|
|
foreach ($allAudio as $audio) {
|
|
|
|
$audio->rename();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-13 16:02:14 +00:00
|
|
|
return redirect('settings-general')->with('message', lang('Settings.housekeeping.runSuccess'));
|
|
|
|
}
|
|
|
|
|
2021-11-08 16:52:20 +00:00
|
|
|
public function theme(): string
|
|
|
|
{
|
|
|
|
helper('form');
|
|
|
|
return view('settings/theme');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function attemptSetInstanceTheme(): RedirectResponse
|
|
|
|
{
|
|
|
|
$theme = $this->request->getPost('theme');
|
|
|
|
service('settings')
|
|
|
|
->set('App.theme', $theme);
|
|
|
|
|
|
|
|
// delete all pages cache
|
|
|
|
cache()
|
|
|
|
->deleteMatching('page*');
|
|
|
|
|
|
|
|
return redirect('settings-theme')->with('message', lang('Settings.theme.setInstanceThemeSuccess'));
|
2021-10-26 15:54:56 +00:00
|
|
|
}
|
|
|
|
}
|