]> git.mxchange.org Git - friendica.git/blob - view/theme/frio/style.php
Refactor IManageConfigValues interface
[friendica.git] / view / theme / frio / style.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 use Friendica\DI;
23 use Friendica\Network\HTTPException\NotModifiedException;
24 use Friendica\Util\Strings;
25
26 require_once 'view/theme/frio/theme.php';
27 require_once 'view/theme/frio/php/PHPColors/Color.php';
28
29 $scheme = '';
30 $schemecss = '';
31 $schemecssfile = false;
32 $scheme_modified = 0;
33
34 /*
35  * This script can be included when the maintenance mode is on, which requires us to avoid any config call and
36  * use the following hardcoded defaults
37  */
38 $scheme           = null;
39 $scheme_accent    = FRIO_SCHEME_ACCENT_BLUE;
40 $nav_bg           = '#708fa0';
41 $nav_icon_color   = '#ffffff';
42 $link_color       = '#6fdbe8';
43 $background_color = '#ededed';
44 $contentbg_transp = 100;
45 $background_image = 'img/none.png';
46 $bg_image_option  = '';
47 $login_bg_image   = '';
48 $login_bg_color   = '';
49 $modified         = time();
50
51 if (DI::mode()->has(\Friendica\App\Mode::MAINTENANCEDISABLED)) {
52         DI::config()->reload('frio');
53
54         // Default to hard-coded values for empty settings
55         $scheme           = DI::config()->get('frio', 'scheme', DI::config()->get('frio', 'schema'));
56         $scheme_accent    = DI::config()->get('frio', 'scheme_accent')    ?: $scheme_accent;
57         $nav_bg           = DI::config()->get('frio', 'nav_bg')           ?: $nav_bg;
58         $nav_icon_color   = DI::config()->get('frio', 'nav_icon_color')   ?: $nav_icon_color;
59         $link_color       = DI::config()->get('frio', 'link_color')       ?: $link_color;
60         $background_color = DI::config()->get('frio', 'background_color') ?: $background_color;
61         $contentbg_transp = DI::config()->get('frio', 'contentbg_transp') ?? $contentbg_transp;
62         $background_image = DI::config()->get('frio', 'background_image') ?: $background_image;
63         $bg_image_option  = DI::config()->get('frio', 'bg_image_option')  ?: $bg_image_option;
64         $login_bg_image   = DI::config()->get('frio', 'login_bg_image')   ?: $login_bg_image;
65         $login_bg_color   = DI::config()->get('frio', 'login_bg_color')   ?: $login_bg_color;
66         $modified         = DI::config()->get('frio', 'css_modified')     ?: $modified;
67
68         // Get the UID of the profile owner.
69         $uid = $_REQUEST['puid'] ?? 0;
70         if ($uid) {
71                 DI::pConfig()->load($uid, 'frio');
72
73                 // Only override display settings that have actually been set
74                 $scheme           = DI::pConfig()->get($uid, 'frio', 'scheme', DI::pConfig()->get($uid, 'frio', 'schema')) ?: $scheme;
75                 $scheme_accent    = DI::pConfig()->get($uid, 'frio', 'scheme_accent')    ?: $scheme_accent;
76                 $nav_bg           = DI::pConfig()->get($uid, 'frio', 'nav_bg')           ?: $nav_bg;
77                 $nav_icon_color   = DI::pConfig()->get($uid, 'frio', 'nav_icon_color')   ?: $nav_icon_color;
78                 $link_color       = DI::pConfig()->get($uid, 'frio', 'link_color')       ?: $link_color;
79                 $background_color = DI::pConfig()->get($uid, 'frio', 'background_color') ?: $background_color;
80                 $contentbg_transp = DI::pConfig()->get($uid, 'frio', 'contentbg_transp') ?? $contentbg_transp;
81                 $background_image = DI::pConfig()->get($uid, 'frio', 'background_image') ?: $background_image;
82                 $bg_image_option  = DI::pConfig()->get($uid, 'frio', 'bg_image_option')  ?: $bg_image_option;
83                 $modified         = DI::pConfig()->get($uid, 'frio', 'css_modified')     ?: $modified;
84         }
85 }
86
87 if (!$login_bg_image && !$login_bg_color) {
88         $login_bg_image = 'img/login_bg.jpg';
89 }
90 $login_bg_color = $login_bg_color ?: '#ededed';
91
92 // Now load the scheme.  If a value is changed above, we'll keep the settings
93 // If not, we'll keep those defined by the scheme
94 // Setting $scheme to '' wasn't working for some reason, so we'll check it's
95 // not --- like the mobile theme does instead.
96 // Allow layouts to over-ride the scheme.
97 if (!empty($_REQUEST['scheme'])) {
98         $scheme = $_REQUEST['scheme'];
99 }
100
101 $scheme = Strings::sanitizeFilePathItem($scheme ?? '');
102
103 if ($scheme && ($scheme != '---')) {
104         if (file_exists('view/theme/frio/scheme/' . $scheme . '.php')) {
105                 $schemefile = 'view/theme/frio/scheme/' . $scheme . '.php';
106                 require_once $schemefile;
107         }
108         if (file_exists('view/theme/frio/scheme/' . $scheme . '.css')) {
109                 $schemecssfile = 'view/theme/frio/scheme/' . $scheme . '.css';
110         }
111 }
112
113 // If we haven't got a scheme, load the default.  We shouldn't touch this - we
114 // should leave it for admins to define for themselves.
115 // default.php and default.css MUST be symlinks to existing scheme files.
116 if (!$scheme) {
117         if (file_exists('view/theme/frio/scheme/default.php')) {
118                 $schemefile = 'view/theme/frio/scheme/default.php';
119                 require_once $schemefile;
120         }
121         if (file_exists('view/theme/frio/scheme/default.css')) {
122                 $schemecssfile = 'view/theme/frio/scheme/default.css';
123         }
124 }
125
126 $contentbg_transp = $contentbg_transp != '' ? $contentbg_transp : 100;
127
128 // Calculate some colors in dependance of existing colors.
129 // Some colors are calculated to don't have too many selection
130 // fields in the theme settings.
131 if (!isset($menu_background_hover_color)) {
132         $mbhc = new Color($nav_bg);
133         $mcolor = $mbhc->getHex();
134
135         if ($mbhc->isLight($mcolor, 75)) {
136                 $menu_is = 'light';
137                 $menu_background_hover_color = '#' . $mbhc->darken(5);
138         } else {
139                 $menu_is = 'dark';
140                 $menu_background_hover_color = '#' . $mbhc->lighten(5);
141         }
142 }
143 if (!isset($nav_icon_hover_color)) {
144         $nihc = new Color($nav_bg);
145
146         if ($nihc->isLight()) {
147                 $nav_icon_hover_color = '#' . $nihc->darken(10);
148         } else {
149                 $nav_icon_hover_color = '#' . $nihc->lighten(20);
150         }
151 }
152 if (!isset($link_hover_color)) {
153         $lhc = new Color($link_color);
154         $lcolor = $lhc->getHex();
155
156         if ($lhc->isLight($lcolor, 75)) {
157                 $link_hover_color = '#' . $lhc->darken(5);
158         } else {
159                 $link_hover_color = '#' . $lhc->lighten(5);
160         }
161 }
162
163 // Convert $bg_image_options into css.
164 if (!isset($bg_image_option)) {
165         $bg_image_option = null;
166 }
167
168 switch ($bg_image_option) {
169         case 'stretch':
170                 $background_size_img = '100%';
171                 $background_repeat = 'no-repeat';
172                 break;
173         case 'cover':
174                 $background_size_img = 'cover';
175                 $background_repeat = 'no-repeat';
176                 break;
177         case 'repeat':
178                 $background_size_img = 'auto';
179                 $background_repeat = 'repeat';
180                 break;
181         case 'contain':
182                 $background_size_img = 'contain';
183                 $background_repeat = 'repeat';
184                 break;
185
186         default:
187                 $background_size_img = 'auto';
188                 $background_repeat = 'no-repeat';
189                 break;
190 }
191
192 // Convert transparency level from percentage to opacity value.
193 $contentbg_transp = $contentbg_transp / 100;
194
195 $options = [
196         '$nav_bg'                      => $nav_bg,
197         '$nav_icon_color'              => $nav_icon_color,
198         '$nav_icon_hover_color'        => $nav_icon_hover_color,
199         '$link_color'                  => $link_color,
200         '$link_hover_color'            => $link_hover_color,
201         '$menu_background_hover_color' => $menu_background_hover_color,
202         '$btn_primary_color'           => $nav_icon_color,
203         '$btn_primary_hover_color'     => $menu_background_hover_color,
204         '$background_color'            => $background_color,
205         '$contentbg_transp'            => $contentbg_transp,
206         '$background_image'            => $background_image,
207         '$background_size_img'         => $background_size_img,
208         '$background_repeat'           => $background_repeat,
209         '$login_bg_image'              => $login_bg_image,
210         '$login_bg_color'              => $login_bg_color,
211         '$font_color_darker'           => $font_color_darker ?? '#222',
212         '$font_color'                  => $font_color ?? '#444',
213 ];
214
215 $css_tpl = file_get_contents('view/theme/frio/css/style.css');
216
217 // Get the content of the scheme css file and the time of the last file change.
218 if ($schemecssfile) {
219         $css_tpl .= file_get_contents($schemecssfile);
220         $scheme_modified = filemtime($schemecssfile);
221 }
222
223 // We need to check which is the most recent css data.
224 // We will use this time later to decide if we load the cached or fresh css data.
225 if ($scheme_modified > $modified) {
226         $modified = $scheme_modified;
227 }
228 // Apply the settings to the css template.
229 $css = str_replace(array_keys($options), array_values($options), $css_tpl);
230
231 $modified = gmdate('r', $modified);
232
233 $etag = md5($css);
234
235 // Set a header for caching.
236 header('Cache-Control: public');
237 header('ETag: "' . $etag . '"');
238 header('Last-Modified: ' . $modified);
239
240 // Only send the CSS file if it was changed.
241 /// @todo Check if this works at all (possibly clients are sending only the one or the other header) - compare with mod/photo.php
242 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
243         $cached_modified = gmdate('r', strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']));
244         $cached_etag = str_replace(['"', '-gzip'], ['', ''],
245                                 stripslashes($_SERVER['HTTP_IF_NONE_MATCH']));
246
247         if (($cached_modified == $modified) && ($cached_etag == $etag)) {
248                 throw new NotModifiedException();
249         }
250 }
251
252 echo $css;