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;
|
|
|
|
|
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;
|
2022-01-13 16:02:14 +00:00
|
|
|
use App\Models\MediaModel;
|
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;
|
2022-01-13 16:02:14 +00:00
|
|
|
use CodeIgniter\Files\File;
|
2021-10-26 15:54:56 +00:00
|
|
|
use CodeIgniter\HTTP\RedirectResponse;
|
|
|
|
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
|
2022-01-13 16:45:08 +00:00
|
|
|
delete_files(media_path('/site'));
|
2021-10-26 15:54:56 +00:00
|
|
|
|
|
|
|
// save original in disk
|
|
|
|
$originalFilename = save_media($siteIconFile, 'site', 'icon');
|
|
|
|
|
|
|
|
// convert jpeg image to png if not
|
|
|
|
if ($siteIconFile->getClientMimeType() !== 'image/png') {
|
2022-01-13 16:45:08 +00:00
|
|
|
service('image')->withFile(media_path($originalFilename))
|
2021-10-26 15:54:56 +00:00
|
|
|
->convert(IMAGETYPE_JPEG)
|
2022-01-13 16:45:08 +00:00
|
|
|
->save(media_path('/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();
|
2022-01-13 16:45:08 +00:00
|
|
|
$ico_lib->add_image(media_path('/site/icon.png'), [[32, 32], [64, 64]]);
|
|
|
|
$ico_lib->save_ico(media_path("/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')
|
2022-01-13 16:45:08 +00:00
|
|
|
->withFile(media_path('/site/icon.png'))
|
2021-10-26 15:54:56 +00:00
|
|
|
->resize($size, $size)
|
2021-11-23 11:54:34 +00:00
|
|
|
->save(media_path("/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
|
2022-01-13 16:45:08 +00:00
|
|
|
delete_files(media_path('/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
|
|
|
|
{
|
2022-01-13 16:45:08 +00:00
|
|
|
helper('media');
|
|
|
|
|
2021-11-23 11:54:34 +00:00
|
|
|
$allPodcasts = (new PodcastModel())->findAll();
|
2022-01-29 15:32:38 +00:00
|
|
|
$imageExt = ['jpg', 'png', 'webp'];
|
2021-11-23 11:54:34 +00:00
|
|
|
|
|
|
|
foreach ($allPodcasts as $podcast) {
|
2022-01-29 15:32:38 +00:00
|
|
|
foreach ($imageExt as $ext) {
|
|
|
|
$podcastImages = glob(media_path("/podcasts/{$podcast->handle}/*_*{$ext}"));
|
2021-11-23 11:54:34 +00:00
|
|
|
|
2022-01-29 15:32:38 +00:00
|
|
|
if ($podcastImages) {
|
|
|
|
foreach ($podcastImages as $podcastImage) {
|
|
|
|
if (is_file($podcastImage)) {
|
|
|
|
unlink($podcastImage);
|
|
|
|
}
|
2021-11-23 11:54:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-29 15:32:38 +00:00
|
|
|
foreach ($imageExt as $ext) {
|
|
|
|
$personsImages = glob(media_path("/persons/*_*{$ext}"));
|
|
|
|
if ($personsImages) {
|
|
|
|
foreach ($personsImages as $personsImage) {
|
|
|
|
if (is_file($personsImage)) {
|
|
|
|
unlink($personsImage);
|
|
|
|
}
|
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-01-13 16:02:14 +00:00
|
|
|
helper('media');
|
|
|
|
|
2022-01-14 17:42:55 +00:00
|
|
|
if ($this->request->getPost('rewrite_media') === 'yes') {
|
2022-01-29 15:32:38 +00:00
|
|
|
$imageExt = ['jpg', 'png', 'webp'];
|
2022-01-14 17:42:55 +00:00
|
|
|
// Delete all podcast image sizes to recreate them
|
|
|
|
$allPodcasts = (new PodcastModel())->findAll();
|
|
|
|
foreach ($allPodcasts as $podcast) {
|
2022-01-29 15:32:38 +00:00
|
|
|
foreach ($imageExt as $ext) {
|
|
|
|
$podcastImages = glob(media_path("/podcasts/{$podcast->handle}/*_*{$ext}"));
|
|
|
|
|
|
|
|
if ($podcastImages) {
|
|
|
|
foreach ($podcastImages as $podcastImage) {
|
|
|
|
if (is_file($podcastImage)) {
|
|
|
|
unlink($podcastImage);
|
|
|
|
}
|
2022-01-14 17:42:55 +00:00
|
|
|
}
|
2022-01-13 16:02:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-14 17:42:55 +00:00
|
|
|
// Delete all person image sizes to recreate them
|
2022-01-29 15:32:38 +00:00
|
|
|
foreach ($imageExt as $ext) {
|
|
|
|
$personsImages = glob(media_path("/persons/*_*{$ext}"));
|
|
|
|
if ($personsImages) {
|
|
|
|
foreach ($personsImages as $personsImage) {
|
|
|
|
if (is_file($personsImage)) {
|
|
|
|
unlink($personsImage);
|
|
|
|
}
|
2022-01-14 17:42:55 +00:00
|
|
|
}
|
2022-01-13 16:02:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-14 17:42:55 +00:00
|
|
|
$allImages = (new MediaModel('image'))->getAllOfType();
|
|
|
|
foreach ($allImages as $image) {
|
|
|
|
if (str_starts_with($image->file_path, 'podcasts')) {
|
|
|
|
if (str_ends_with($image->file_path, 'banner.jpg') || str_ends_with(
|
|
|
|
$image->file_path,
|
|
|
|
'banner.png'
|
2022-01-29 15:32:38 +00:00
|
|
|
) || str_ends_with($image->file_path, 'banner.jpeg')) {
|
2022-01-14 17:42:55 +00:00
|
|
|
$image->sizes = config('Images')
|
|
|
|
->podcastBannerSizes;
|
|
|
|
} else {
|
|
|
|
$image->sizes = config('Images')
|
|
|
|
->podcastCoverSizes;
|
|
|
|
}
|
|
|
|
} elseif (str_starts_with($image->file_path, 'persons')) {
|
2022-01-13 16:02:14 +00:00
|
|
|
$image->sizes = config('Images')
|
2022-01-14 17:42:55 +00:00
|
|
|
->personAvatarSizes;
|
2022-01-29 15:21:46 +00:00
|
|
|
} else {
|
|
|
|
$image->sizes = [];
|
2022-01-13 16:02:14 +00:00
|
|
|
}
|
2022-01-29 15:21:46 +00:00
|
|
|
|
2022-01-14 17:42:55 +00:00
|
|
|
$image->setFile(new File(media_path($image->file_path)));
|
|
|
|
|
|
|
|
(new MediaModel('image'))->updateMedia($image);
|
2022-01-13 16:02:14 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 17:42:55 +00:00
|
|
|
$allAudio = (new MediaModel('audio'))->getAllOfType();
|
|
|
|
foreach ($allAudio as $audio) {
|
|
|
|
$audio->setFile(new File(media_path($audio->file_path)));
|
|
|
|
|
|
|
|
(new MediaModel('audio'))->updateMedia($audio);
|
|
|
|
}
|
2022-01-13 16:02:14 +00:00
|
|
|
|
2022-01-14 17:42:55 +00:00
|
|
|
$allTranscripts = (new MediaModel('transcript'))->getAllOfType();
|
|
|
|
foreach ($allTranscripts as $transcript) {
|
|
|
|
$transcript->setFile(new File(media_path($transcript->file_path)));
|
2022-01-13 16:02:14 +00:00
|
|
|
|
2022-01-14 17:42:55 +00:00
|
|
|
(new MediaModel('transcript'))->updateMedia($transcript);
|
|
|
|
}
|
2022-01-13 16:02:14 +00:00
|
|
|
|
2022-01-14 17:42:55 +00:00
|
|
|
$allChapters = (new MediaModel('chapters'))->getAllOfType();
|
|
|
|
foreach ($allChapters as $chapters) {
|
|
|
|
$chapters->setFile(new File(media_path($chapters->file_path)));
|
2022-01-13 16:02:14 +00:00
|
|
|
|
2022-01-14 17:42:55 +00:00
|
|
|
(new MediaModel('chapters'))->updateMedia($chapters);
|
|
|
|
}
|
2022-01-13 16:02:14 +00:00
|
|
|
|
2022-01-14 17:42:55 +00:00
|
|
|
$allVideos = (new MediaModel('video'))->getAllOfType();
|
|
|
|
foreach ($allVideos as $video) {
|
|
|
|
$video->setFile(new File(media_path($video->file_path)));
|
2022-01-13 16:02:14 +00:00
|
|
|
|
2022-01-14 17:42:55 +00:00
|
|
|
(new MediaModel('video'))->updateMedia($video);
|
|
|
|
}
|
2022-01-13 16:02:14 +00:00
|
|
|
|
2022-01-14 17:42:55 +00:00
|
|
|
// reset avatar and banner image urls for each podcast actor
|
|
|
|
foreach ($allPodcasts as $podcast) {
|
|
|
|
$actorModel = new ActorModel();
|
|
|
|
$actor = $actorModel->getActorById($podcast->actor_id);
|
2022-01-13 16:02:14 +00:00
|
|
|
|
2022-01-14 17:42:55 +00:00
|
|
|
if ($actor !== null) {
|
|
|
|
// update values
|
|
|
|
$actor->avatar_image_url = $podcast->cover->federation_url;
|
|
|
|
$actor->avatar_image_mimetype = $podcast->cover->file_mimetype;
|
|
|
|
$actor->cover_image_url = $podcast->banner->federation_url;
|
|
|
|
$actor->cover_image_mimetype = $podcast->banner->file_mimetype;
|
2022-01-13 16:02:14 +00:00
|
|
|
|
2022-01-14 17:42:55 +00:00
|
|
|
if ($actor->hasChanged()) {
|
|
|
|
$actorModel->update($actor->id, $actor);
|
|
|
|
}
|
2022-01-13 16:02:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-29 16:42:34 +00:00
|
|
|
if ($this->request->getPost('clear_cache') === 'yes') {
|
|
|
|
cache()->clean();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|