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