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