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