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