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