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;

Yassine Doghri
committed
use App\Models\PersonModel;
use App\Models\PodcastModel;

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
];
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
delete_files(ROOTPATH . 'public/media/site/');
// save original in disk
$originalFilename = save_media($siteIconFile, 'site', 'icon');
// convert jpeg image to png if not
if ($siteIconFile->getClientMimeType() !== 'image/png') {
service('image')->withFile(ROOTPATH . 'public/media/' . $originalFilename)
->convert(IMAGETYPE_JPEG)
->save(ROOTPATH . 'public/media/site/icon.png');
}
// 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();
$ico_lib->add_image(ROOTPATH . 'public/media/site/icon.png', [[32, 32], [64, 64]]);
$ico_lib->save_ico(ROOTPATH . "public/media/site/favicon.{$randomHash}.ico");
// resize original to needed sizes
foreach ([64, 180, 192, 512] as $size) {
service('image')
->withFile(ROOTPATH . 'public/media/site/icon.png')
->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
{
helper('filesystem');
// delete site folder in media
delete_files(ROOTPATH . 'public/media/site/');
service('settings')
->forget('App.siteIcon');

Yassine Doghri
committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
return redirect('settings-general')->with('message', lang('Settings.instance.deleteIconSuccess'));
}
public function regenerateImages(): RedirectResponse
{
$allPodcasts = (new PodcastModel())->findAll();
foreach ($allPodcasts as $podcast) {
$podcastImages = glob(ROOTPATH . "public/media/podcasts/{$podcast->handle}/*_*");
if ($podcastImages) {
foreach ($podcastImages as $podcastImage) {
if (is_file($podcastImage)) {
unlink($podcastImage);
}
}
}
$podcast->setCover($podcast->cover);
if ($podcast->banner_path !== null) {
$podcast->setBanner($podcast->banner);
}
foreach ($podcast->episodes as $episode) {
if ($episode->cover_path !== null) {
$episode->setCover($episode->cover);
}
}
}
$personsImages = glob(ROOTPATH . 'public/media/persons/*_*');
if ($personsImages) {
foreach ($personsImages as $personsImage) {
if (is_file($personsImage)) {
unlink($personsImage);
}
}
}
$persons = (new PersonModel())->findAll();
foreach ($persons as $person) {
if ($person->avatar_path !== null) {
$person->setAvatar($person->avatar);
}
}
return redirect('settings-general')->with('message', lang('Settings.images.regenerationSuccess'));

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'));