]> git.mxchange.org Git - friendica.git/blob - view/theme/frio/style.php
c227c354f8a4ebf5c12e9fd10ed32c0534e72615
[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 $schema = basename($schema);
65
66 if (($schema) && ($schema != '---')) {
67         if (file_exists('view/theme/frio/schema/' . $schema . '.php')) {
68                 $schemefile = 'view/theme/frio/schema/' . $schema . '.php';
69                 require_once $schemefile;
70         }
71         if (file_exists('view/theme/frio/schema/' . $schema . '.css')) {
72                 $schemecssfile = 'view/theme/frio/schema/' . $schema . '.css';
73         }
74 }
75
76 // If we haven't got a schema, load the default.  We shouldn't touch this - we
77 // should leave it for admins to define for themselves.
78 // default.php and default.css MUST be symlinks to existing schema files.
79 if (! $schema) {
80         if(file_exists('view/theme/frio/schema/default.php')) {
81                 $schemefile = 'view/theme/frio/schema/default.php';
82                 require_once $schemefile;
83         }
84         if(file_exists('view/theme/frio/schema/default.css')) {
85                 $schemecssfile = 'view/theme/frio/schema/default.css';
86         }
87 }
88
89 //Set some defaults - we have to do this after pulling owner settings, and we have to check for each setting
90 //individually.  If we don't, we'll have problems if a user has set one, but not all options.
91 $nav_bg           = (empty($nav_bg)           ? "#708fa0"      : $nav_bg);
92 $nav_icon_color   = (empty($nav_icon_color)   ? "#fff"         : $nav_icon_color);
93 $link_color       = (empty($link_color)       ? "#6fdbe8"      : $link_color);
94 $bgcolor          = (empty($bgcolor)          ? "#ededed"      : $bgcolor);
95 // The background image can not be empty. So we use a dummy jpg if no image was set.
96 $background_image = (empty($background_image) ? 'img/none.jpg' : $background_image);
97 $modified         = (empty($modified)         ? time()         :$modified);
98
99 $contentbg_transp = ((isset($contentbg_transp) && $contentbg_transp != "") ?  $contentbg_transp : 100);
100
101 // Calculate some colors in dependance of existing colors.
102 // Some colors are calculated to don't have too many selection
103 // fields in the theme settings.
104 if (! $menu_background_hover_color) {
105         $mbhc = new Color($nav_bg);
106         $mcolor = $mbhc->getHex();
107
108         if ($mbhc->isLight($mcolor, 75)) {
109                 $menu_is = 'light';
110                 $menu_background_hover_color = '#' . $mbhc->darken(5);
111         } else {
112                 $menu_is = 'dark';
113                 $menu_background_hover_color = '#' . $mbhc->lighten(5);
114         }
115 }
116 if (! $nav_icon_hover_color) {
117         $nihc = new Color($nav_bg);
118
119         if ($nihc->isLight()) {
120                 $nav_icon_hover_color = '#' . $nihc->darken(10);
121         } else {
122                 $nav_icon_hover_color = '#' . $nihc->lighten(10);
123         }
124 }
125 if (! $link_hover_color) {
126         $lhc = new Color($link_color);
127         $lcolor = $lhc->getHex();
128
129         if ($lhc->isLight($lcolor, 75)) {
130                 $link_hover_color = '#' . $lhc->darken(5);
131         } else {
132                 $link_hover_color = '#' . $lhc->lighten(5);
133         }
134
135 }
136
137 // Convert $bg_image_options into css.
138 switch ($bg_image_option) {
139         case "stretch":
140                 $background_size_img = "100%";
141                 break;
142         case "cover":
143                 $background_size_img ="cover";
144                 break;
145         case "repeat":
146                 $background_size_img = "auto";
147                 break;
148         case "contain":
149                 $background_size_img = "contain";
150                 break;
151
152         default:
153                 $background_size_img = "auto";
154                 break;
155 }
156
157 // Convert transparency level from percentage to opacity value.
158 $contentbg_transp = $contentbg_transp / 100;
159
160 $options = array (
161         '$nav_bg'                      => $nav_bg,
162         '$nav_icon_color'              => $nav_icon_color,
163         '$nav_icon_hover_color'        => $nav_icon_hover_color,
164         '$link_color'                  => $link_color,
165         '$link_hover_color'            => $link_hover_color,
166         '$menu_background_hover_color' => $menu_background_hover_color,
167         '$btn_primary_color'           => $nav_icon_color,
168         '$btn_primary_hover_color'     => $menu_background_hover_color,
169         '$bgcolor'                     => $bgcolor,
170         '$contentbg_transp'            => $contentbg_transp,
171         '$background_image'            => $background_image,
172         '$background_size_img'         => $background_size_img,
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 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) || isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
202         $cached_modified = gmdate('r', strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']));
203         $cached_etag = str_replace(array('"', "-gzip"), array('', ''),
204                                 stripslashes($_SERVER['HTTP_IF_NONE_MATCH']));
205
206         if (($cached_modified == $modified) && ($cached_etag == $etag)) {
207                 header('HTTP/1.1 304 Not Modified');
208                 exit();
209         }
210 }
211
212 echo $css;