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