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