Merge branch 'develop' of https://github.com/friendica/friendica into develop
[friendica.git/.git] / view / theme / frio / style.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2024, 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/scheme.php';
28 require_once 'view/theme/frio/php/PHPColors/Color.php';
29
30 $schemecssfile = false;
31 $scheme_modified = 0;
32
33 /*
34  * This script can be included when the maintenance mode is on, which requires us to avoid any config call and
35  * use the following hardcoded defaults
36  */
37 $scheme           = FRIO_DEFAULT_SCHEME;
38 $scheme_accent    = FRIO_SCHEME_ACCENT_BLUE;
39 $nav_bg           = '#708fa0';
40 $nav_icon_color   = '#ffffff';
41 $link_color       = '#6fdbe8';
42 $background_color = '#ededed';
43 $contentbg_transp = 100;
44 $background_image = 'img/none.png';
45 $bg_image_option  = '';
46 $login_bg_image   = '';
47 $login_bg_color   = '';
48 $modified         = time();
49
50 if (DI::mode()->has(\Friendica\App\Mode::MAINTENANCEDISABLED)) {
51         DI::config()->reload();
52
53         // Default to hard-coded values for empty settings
54         $scheme           = frio_scheme_get_current();
55         $scheme_accent    = DI::config()->get('frio', 'scheme_accent')    ?: $scheme_accent;
56         $nav_bg           = DI::config()->get('frio', 'nav_bg')           ?: $nav_bg;
57         $nav_icon_color   = DI::config()->get('frio', 'nav_icon_color')   ?: $nav_icon_color;
58         $link_color       = DI::config()->get('frio', 'link_color')       ?: $link_color;
59         $background_color = DI::config()->get('frio', 'background_color') ?: $background_color;
60         $contentbg_transp = DI::config()->get('frio', 'contentbg_transp') ?? $contentbg_transp;
61         $background_image = DI::config()->get('frio', 'background_image') ?: $background_image;
62         $bg_image_option  = DI::config()->get('frio', 'bg_image_option')  ?: $bg_image_option;
63         $login_bg_image   = DI::config()->get('frio', 'login_bg_image')   ?: $login_bg_image;
64         $login_bg_color   = DI::config()->get('frio', 'login_bg_color')   ?: $login_bg_color;
65         $modified         = DI::config()->get('frio', 'css_modified')     ?: $modified;
66
67         // Get the UID of the profile owner.
68         $uid = $_REQUEST['puid'] ?? 0;
69         if ($uid) {
70                 DI::pConfig()->load($uid, 'frio');
71
72                 // Only override display settings that have actually been set
73                 $scheme           = frio_scheme_get_current_for_user($uid);
74                 $scheme_accent    = DI::pConfig()->get($uid, 'frio', 'scheme_accent')    ?: $scheme_accent;
75                 $nav_bg           = DI::pConfig()->get($uid, 'frio', 'nav_bg')           ?: $nav_bg;
76                 $nav_icon_color   = DI::pConfig()->get($uid, 'frio', 'nav_icon_color')   ?: $nav_icon_color;
77                 $link_color       = DI::pConfig()->get($uid, 'frio', 'link_color')       ?: $link_color;
78                 $background_color = DI::pConfig()->get($uid, 'frio', 'background_color') ?: $background_color;
79                 $contentbg_transp = DI::pConfig()->get($uid, 'frio', 'contentbg_transp') ?? $contentbg_transp;
80                 $background_image = DI::pConfig()->get($uid, 'frio', 'background_image') ?: $background_image;
81                 $bg_image_option  = DI::pConfig()->get($uid, 'frio', 'bg_image_option')  ?: $bg_image_option;
82                 $modified         = DI::pConfig()->get($uid, 'frio', 'css_modified')     ?: $modified;
83         }
84 }
85
86 if (!$login_bg_image && !$login_bg_color) {
87         $login_bg_image = 'img/login_bg.jpg';
88 }
89 $login_bg_color = $login_bg_color ?: '#ededed';
90
91 $scheme = Strings::sanitizeFilePathItem($scheme);
92
93 if ($scheme != FRIO_CUSTOM_SCHEME) {
94         if (file_exists('view/theme/frio/scheme/' . $scheme . '.php')) {
95                 $schemefile = 'view/theme/frio/scheme/' . $scheme . '.php';
96                 require_once $schemefile;
97         }
98         if (file_exists('view/theme/frio/scheme/' . $scheme . '.css')) {
99                 $schemecssfile = 'view/theme/frio/scheme/' . $scheme . '.css';
100         }
101 }
102
103 $contentbg_transp = $contentbg_transp != '' ? $contentbg_transp : 100;
104
105 // Calculate some colors in dependance of existing colors.
106 // Some colors are calculated to don't have too many selection
107 // fields in the theme settings.
108 if (!isset($menu_background_hover_color)) {
109         $mbhc = new Color($nav_bg);
110         $mcolor = $mbhc->getHex();
111
112         if ($mbhc->isLight($mcolor, 75)) {
113                 $menu_is = 'light';
114                 $menu_background_hover_color = '#' . $mbhc->darken(5);
115         } else {
116                 $menu_is = 'dark';
117                 $menu_background_hover_color = '#' . $mbhc->lighten(5);
118         }
119 }
120 if (!isset($nav_icon_hover_color)) {
121         $nihc = new Color($nav_bg);
122
123         if ($nihc->isLight()) {
124                 $nav_icon_hover_color = '#' . $nihc->darken(10);
125         } else {
126                 $nav_icon_hover_color = '#' . $nihc->lighten(20);
127         }
128 }
129 if (!isset($link_hover_color)) {
130         $lhc = new Color($link_color);
131         $lcolor = $lhc->getHex();
132
133         if ($lhc->isLight($lcolor, 75)) {
134                 $link_hover_color = '#' . $lhc->darken(5);
135         } else {
136                 $link_hover_color = '#' . $lhc->lighten(5);
137         }
138 }
139
140 // Convert $bg_image_options into css.
141 if (!isset($bg_image_option)) {
142         $bg_image_option = null;
143 }
144
145 switch ($bg_image_option) {
146         case 'stretch':
147                 $background_size_img = '100%';
148                 $background_repeat = 'no-repeat';
149                 break;
150         case 'cover':
151                 $background_size_img = 'cover';
152                 $background_repeat = 'no-repeat';
153                 break;
154         case 'repeat':
155                 $background_size_img = 'auto';
156                 $background_repeat = 'repeat';
157                 break;
158         case 'contain':
159                 $background_size_img = 'contain';
160                 $background_repeat = 'repeat';
161                 break;
162
163         default:
164                 $background_size_img = 'auto';
165                 $background_repeat = 'no-repeat';
166                 break;
167 }
168
169 // Convert transparency level from percentage to opacity value.
170 $contentbg_transp = $contentbg_transp / 100;
171
172 $options = [
173         '$nav_bg'                      => $nav_bg,
174         '$nav_icon_color'              => $nav_icon_color,
175         '$nav_icon_hover_color'        => $nav_icon_hover_color,
176         '$link_color'                  => $link_color,
177         '$link_hover_color'            => $link_hover_color,
178         '$menu_background_hover_color' => $menu_background_hover_color,
179         '$btn_primary_color'           => $nav_icon_color,
180         '$btn_primary_hover_color'     => $menu_background_hover_color,
181         '$background_color'            => $background_color,
182         '$contentbg_transp'            => $contentbg_transp,
183         '$background_image'            => $background_image,
184         '$background_size_img'         => $background_size_img,
185         '$background_repeat'           => $background_repeat,
186         '$login_bg_image'              => $login_bg_image,
187         '$login_bg_color'              => $login_bg_color,
188         '$font_color_darker'           => $font_color_darker ?? '#222',
189         '$font_color_lighter'          => $font_color_lighter ?? '#aaa',
190         '$font_color'                  => $font_color ?? '#444',
191 ];
192
193 $css_tpl = file_get_contents('view/theme/frio/css/style.css');
194 $css_tpl .= file_get_contents('view/theme/frio/css/dropzone.min.frio.css');
195
196 // Get the content of the scheme css file and the time of the last file change.
197 if ($schemecssfile) {
198         $css_tpl .= file_get_contents($schemecssfile);
199         $scheme_modified = filemtime($schemecssfile);
200 }
201
202 // We need to check which is the most recent css data.
203 // We will use this time later to decide if we load the cached or fresh css data.
204 if ($scheme_modified > $modified) {
205         $modified = $scheme_modified;
206 }
207 // Apply the settings to the css template.
208 $css = str_replace(array_keys($options), array_values($options), $css_tpl);
209
210 $modified = gmdate('r', $modified);
211
212 $etag = md5($css);
213
214 // Set a header for caching.
215 header('Cache-Control: public');
216 header('ETag: "' . $etag . '"');
217 header('Last-Modified: ' . $modified);
218
219 // Only send the CSS file if it was changed.
220 /// @todo Check if this works at all (possibly clients are sending only the one or the other header) - compare with mod/photo.php
221 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
222         $cached_modified = gmdate('r', strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']));
223         $cached_etag = str_replace(['"', '-gzip'], ['', ''],
224                                 stripslashes($_SERVER['HTTP_IF_NONE_MATCH']));
225
226         if (($cached_modified == $modified) && ($cached_etag == $etag)) {
227                 throw new NotModifiedException();
228         }
229 }
230
231 echo $css;