Newer
Older

Yassine Doghri
committed
<?php
declare(strict_types=1);
/**
* @copyright 2020 Podlibre
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
namespace Modules\Admin\Controllers;
use App\Entities\Media\Image;
use App\Models\ActorModel;
use App\Models\MediaModel;

Yassine Doghri
committed
use App\Models\PersonModel;
use App\Models\PodcastModel;
use CodeIgniter\Files\File;

Yassine Doghri
committed
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' =>

Yassine Doghri
committed
'is_image[site_icon]|ext_in[site_icon,png,jpeg]|is_image_ratio[site_icon,1,1]|min_dims[image,512,512]|permit_empty',

Yassine Doghri
committed
];
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

Yassine Doghri
committed
delete_files(media_path('/site'));

Yassine Doghri
committed
// save original in disk
$originalFilename = save_media($siteIconFile, 'site', 'icon');
// convert jpeg image to png if not
if ($siteIconFile->getClientMimeType() !== 'image/png') {

Yassine Doghri
committed
service('image')->withFile(media_path($originalFilename))

Yassine Doghri
committed
->convert(IMAGETYPE_JPEG)

Yassine Doghri
committed
->save(media_path('/site/icon.png'));

Yassine Doghri
committed
}
// 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();

Yassine Doghri
committed
$ico_lib->add_image(media_path('/site/icon.png'), [[32, 32], [64, 64]]);
$ico_lib->save_ico(media_path("/site/favicon.{$randomHash}.ico"));

Yassine Doghri
committed
// resize original to needed sizes
foreach ([64, 180, 192, 512] as $size) {
service('image')

Yassine Doghri
committed
->withFile(media_path('/site/icon.png'))

Yassine Doghri
committed
->resize($size, $size)

Yassine Doghri
committed
->save(media_path("/site/icon-{$size}.{$randomHash}.png"));

Yassine Doghri
committed
}
service('settings')
->set('App.siteIcon', [

Yassine Doghri
committed
'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"),

Yassine Doghri
committed
]);
}

Yassine Doghri
committed
return redirect('settings-general')->with('message', lang('Settings.instance.editSuccess'));

Yassine Doghri
committed
}
public function deleteIcon(): RedirectResponse
{

Yassine Doghri
committed
helper(['filesystem', 'media']);

Yassine Doghri
committed
// delete site folder in media

Yassine Doghri
committed
delete_files(media_path('/site'));

Yassine Doghri
committed
service('settings')
->forget('App.siteIcon');

Yassine Doghri
committed
return redirect('settings-general')->with('message', lang('Settings.instance.deleteIconSuccess'));
}
public function regenerateImages(): RedirectResponse
{

Yassine Doghri
committed
helper('media');

Yassine Doghri
committed
$allPodcasts = (new PodcastModel())->findAll();
foreach ($allPodcasts as $podcast) {

Yassine Doghri
committed
$podcastImages = glob(media_path("/podcasts/{$podcast->handle}/*_*{jpg,png,webp}"), GLOB_BRACE);

Yassine Doghri
committed
if ($podcastImages) {
foreach ($podcastImages as $podcastImage) {
if (is_file($podcastImage)) {
unlink($podcastImage);
}
}
}

Yassine Doghri
committed
$podcast->cover->saveSizes();
if ($podcast->banner_id !== null) {
$podcast->banner->saveSizes();

Yassine Doghri
committed
}
foreach ($podcast->episodes as $episode) {

Yassine Doghri
committed
if ($episode->cover_id !== null) {
$episode->cover->saveSizes();

Yassine Doghri
committed
}
}
}

Yassine Doghri
committed
$personsImages = glob(media_path('/persons/*_*{jpg,png,webp}'), GLOB_BRACE);

Yassine Doghri
committed
if ($personsImages) {
foreach ($personsImages as $personsImage) {
if (is_file($personsImage)) {
unlink($personsImage);
}
}
}
$persons = (new PersonModel())->findAll();
foreach ($persons as $person) {

Yassine Doghri
committed
if ($person->avatar_id !== null) {
$person->avatar->saveSizes();

Yassine Doghri
committed
}
}
return redirect('settings-general')->with('message', lang('Settings.images.regenerationSuccess'));

Yassine Doghri
committed
}
public function runHousekeeping(): RedirectResponse
{
helper('media');
// Delete all podcast image sizes to recreate them
$allPodcasts = (new PodcastModel())->findAll();
foreach ($allPodcasts as $podcast) {

Yassine Doghri
committed
$podcastImages = glob(media_path("/podcasts/{$podcast->handle}/*_*{jpg,png,webp}"), GLOB_BRACE);
if ($podcastImages) {
foreach ($podcastImages as $podcastImage) {
if (is_file($podcastImage)) {
unlink($podcastImage);
}
}
}
}
// Delete all person image sizes to recreate them

Yassine Doghri
committed
$personsImages = glob(media_path('/persons/*_*{jpg,png,webp}'), GLOB_BRACE);
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
if ($personsImages) {
foreach ($personsImages as $personsImage) {
if (is_file($personsImage)) {
unlink($personsImage);
}
}
}
$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')) {
$image->sizes = config('Images')
->podcastBannerSizes;
} else {
$image->sizes = config('Images')
->podcastCoverSizes;
}
} elseif (str_starts_with($image->file_path, 'persons')) {
$image->sizes = config('Images')
->personAvatarSizes;
}
$image->setFile(new File(media_path($image->file_path)));
(new MediaModel('image'))->updateMedia($image);
}
$allAudio = (new MediaModel('audio'))->getAllOfType();
foreach ($allAudio as $audio) {
$audio->setFile(new File(media_path($audio->file_path)));
(new MediaModel('audio'))->updateMedia($audio);
}
$allTranscripts = (new MediaModel('transcript'))->getAllOfType();
foreach ($allTranscripts as $transcript) {
$transcript->setFile(new File(media_path($transcript->file_path)));
(new MediaModel('transcript'))->updateMedia($transcript);
}
$allChapters = (new MediaModel('chapters'))->getAllOfType();
foreach ($allChapters as $chapters) {
$chapters->setFile(new File(media_path($chapters->file_path)));
(new MediaModel('chapters'))->updateMedia($chapters);
}
$allVideos = (new MediaModel('video'))->getAllOfType();
foreach ($allVideos as $video) {
$video->setFile(new File(media_path($video->file_path)));
(new MediaModel('video'))->updateMedia($video);
}
// reset avatar and banner image urls for each podcast actor
foreach ($allPodcasts as $podcast) {
$actorModel = new ActorModel();
$actor = $actorModel->getActorById($podcast->actor_id);
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;
if ($actor->hasChanged()) {
$actorModel->update($actor->id, $actor);
}
}
}
return redirect('settings-general')->with('message', lang('Settings.housekeeping.runSuccess'));
}

Yassine Doghri
committed
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'));