]> git.mxchange.org Git - friendica.git/blob - view/theme/frio/style.php
Merge pull request #1 from friendica/develop
[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/php/PHPColors/Color.php';
26
27 $scheme = '';
28 $schemecss = '';
29 $schemecssfile = false;
30 $scheme_modified = 0;
31
32 DI::config()->load('frio');
33
34 // Default to hard-coded values for empty settings
35 $scheme           = DI::config()->get('frio', 'scheme', DI::config()->get('frio', 'schema'));
36 $nav_bg           = DI::config()->get('frio', 'nav_bg')           ?: '#708fa0';
37 $nav_icon_color   = DI::config()->get('frio', 'nav_icon_color')   ?: '#ffffff';
38 $link_color       = DI::config()->get('frio', 'link_color')       ?: '#6fdbe8';
39 $background_color = DI::config()->get('frio', 'background_color') ?: '#ededed';
40 $contentbg_transp = DI::config()->get('frio', 'contentbg_transp') ?: '';
41 $background_image = DI::config()->get('frio', 'background_image') ?: 'img/none.png';
42 $bg_image_option  = DI::config()->get('frio', 'bg_image_option')  ?: '';
43 $login_bg_image   = DI::config()->get('frio', 'login_bg_image')   ?: '';
44 $login_bg_color   = DI::config()->get('frio', 'login_bg_color')   ?: '';
45 $modified         = DI::config()->get('frio', 'css_modified')     ?: time();
46
47 if (!$login_bg_image && !$login_bg_color) {
48         $login_bg_image = 'img/login_bg.jpg';
49 }
50 $login_bg_color = $login_bg_color ?: '#ededed';
51
52 // Get the UID of the profile owner.
53 $uid = $_REQUEST['puid'] ?? 0;
54 if ($uid) {
55         DI::pConfig()->load($uid, 'frio');
56
57         // Only override display settings that have actually been set
58         $scheme           = DI::pConfig()->get($uid, 'frio', 'scheme', DI::pConfig()->get($uid, 'frio', 'schema')) ?: $scheme;
59         $nav_bg           = DI::pConfig()->get($uid, 'frio', 'nav_bg')           ?: $nav_bg;
60         $nav_icon_color   = DI::pConfig()->get($uid, 'frio', 'nav_icon_color')   ?: $nav_icon_color;
61         $link_color       = DI::pConfig()->get($uid, 'frio', 'link_color')       ?: $link_color;
62         $background_color = DI::pConfig()->get($uid, 'frio', 'background_color') ?: $background_color;
63         $contentbg_transp = DI::pConfig()->get($uid, 'frio', 'contentbg_transp') ?: $contentbg_transp;
64         $background_image = DI::pConfig()->get($uid, 'frio', 'background_image') ?: $background_image;
65         $bg_image_option  = DI::pConfig()->get($uid, 'frio', 'bg_image_option')  ?: $bg_image_option;
66         $modified         = DI::pConfig()->get($uid, 'frio', 'css_modified')     ?: $modified;
67 }
68
69 // Now load the scheme.  If a value is changed above, we'll keep the settings
70 // If not, we'll keep those defined by the scheme
71 // Setting $scheme to '' wasn't working for some reason, so we'll check it's
72 // not --- like the mobile theme does instead.
73 // Allow layouts to over-ride the scheme.
74 if (!empty($_REQUEST['scheme'])) {
75         $scheme = $_REQUEST['scheme'];
76 }
77
78 $scheme = Strings::sanitizeFilePathItem($scheme);
79
80 if (($scheme) && ($scheme != '---')) {
81         if (file_exists('view/theme/frio/scheme/' . $scheme . '.php')) {
82                 $schemefile = 'view/theme/frio/scheme/' . $scheme . '.php';
83                 require_once $schemefile;
84         }
85         if (file_exists('view/theme/frio/scheme/' . $scheme . '.css')) {
86                 $schemecssfile = 'view/theme/frio/scheme/' . $scheme . '.css';
87         }
88 }
89
90 // If we haven't got a scheme, load the default.  We shouldn't touch this - we
91 // should leave it for admins to define for themselves.
92 // default.php and default.css MUST be symlinks to existing scheme files.
93 if (!$scheme) {
94         if (file_exists('view/theme/frio/scheme/default.php')) {
95                 $schemefile = 'view/theme/frio/scheme/default.php';
96                 require_once $schemefile;
97         }
98         if (file_exists('view/theme/frio/scheme/default.css')) {
99                 $schemecssfile = 'view/theme/frio/scheme/default.css';
100         }
101 }
102
103 $contentbg_transp = ((isset($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(10);
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 ];
189
190 $css_tpl = file_get_contents('view/theme/frio/css/style.css');
191
192 // Get the content of the scheme css file and the time of the last file change.
193 if ($schemecssfile) {
194         $css_tpl .= file_get_contents($schemecssfile);
195         $scheme_modified = filemtime($schemecssfile);
196 }
197
198 // We need to check which is the most recent css data.
199 // We will use this time later to decide if we load the cached or fresh css data.
200 if ($scheme_modified > $modified) {
201         $modified = $scheme_modified;
202 }
203 // Apply the settings to the css template.
204 $css = str_replace(array_keys($options), array_values($options), $css_tpl);
205
206 $modified = gmdate('r', $modified);
207
208 $etag = md5($css);
209
210 // Set a header for caching.
211 header('Cache-Control: public');
212 header('ETag: "' . $etag . '"');
213 header('Last-Modified: ' . $modified);
214
215 // Only send the CSS file if it was changed.
216 /// @todo Check if this works at all (possibly clients are sending only the one or the other header) - compare with mod/photo.php
217 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
218         $cached_modified = gmdate('r', strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']));
219         $cached_etag = str_replace(['"', '-gzip'], ['', ''],
220                                 stripslashes($_SERVER['HTTP_IF_NONE_MATCH']));
221
222         if (($cached_modified == $modified) && ($cached_etag == $etag)) {
223                 header('HTTP/1.1 304 Not Modified');
224                 exit();
225         }
226 }
227
228 echo $css;