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