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