]> git.mxchange.org Git - friendica.git/blob - view/theme/frio/style.php
041f890e29085117551e45f9aee78156bbf8b53b
[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 // The background image can not be empty. So we use a dummy jpg if no image was set.
73 $background_image = (empty($background_image) ? 'img/none.jpg' : $background_image);
74 $modified         = (empty($modified)         ? time()         :$modified);
75
76 $contentbg_transp = ((isset($contentbg_transp) && $contentbg_transp != "") ?  $contentbg_transp : 100);
77
78 // Calculate some colors in dependance of existing colors.
79 // Some colors are calculated to don't have too many selection
80 // fields in the theme settings.
81 if (! $menu_background_hover_color) {
82         $mbhc = new Color($nav_bg);
83         $mcolor = $mbhc->getHex();
84
85         if ($mbhc->isLight($mcolor, 75)) {
86                 $menu_is = 'light';
87                 $menu_background_hover_color = '#' . $mbhc->darken(5);
88         } else {
89                 $menu_is = 'dark';
90                 $menu_background_hover_color = '#' . $mbhc->lighten(5);
91         }
92 }
93 if (! $nav_icon_hover_color) {
94         $nihc = new Color($nav_bg);
95
96         if ($nihc->isLight()) {
97                 $nav_icon_hover_color = '#' . $nihc->darken(10);
98         } else {
99                 $nav_icon_hover_color = '#' . $nihc->lighten(10);
100         }
101 }
102 if (! $link_hover_color) {
103         $lhc = new Color($link_color);
104         $lcolor = $lhc->getHex();
105
106         if ($lhc->isLight($lcolor, 75)) {
107                 $link_hover_color = '#' . $lhc->darken(5);
108         } else {
109                 $link_hover_color = '#' . $lhc->lighten(5);
110         }
111
112 }
113
114 // Convert $bg_image_options into css.
115 switch ($bg_image_option) {
116         case "stretch":
117                 $background_size_img = "100%";
118                 break;
119         case "cover":
120                 $background_size_img ="cover";
121                 break;
122         case "repeat":
123                 $background_size_img = "auto";
124                 break;
125         case "contain":
126                 $background_size_img = "contain";
127                 break;
128
129         default:
130                 $background_size_img = "auto";
131                 break;
132 }
133
134 // Convert transparency level from percentage to opacity value.
135 $contentbg_transp = $contentbg_transp / 100;
136
137 $options = array (
138         '$nav_bg'                      => $nav_bg,
139         '$nav_icon_color'              => $nav_icon_color,
140         '$nav_icon_hover_color'        => $nav_icon_hover_color,
141         '$link_color'                  => $link_color,
142         '$link_hover_color'            => $link_hover_color,
143         '$menu_background_hover_color' => $menu_background_hover_color,
144         '$btn_primary_color'           => $nav_icon_color,
145         '$btn_primary_hover_color'     => $menu_background_hover_color,
146         '$bgcolor'                     => $bgcolor,
147         '$contentbg_transp'            => $contentbg_transp,
148         '$background_image'            => $background_image,
149         '$background_size_img'         => $background_size_img,
150 );
151
152 $css_tpl = file_get_contents('view/theme/frio/css/style.css');
153
154 // Get the content of the scheme css file and the time of the last file change.
155 if ($schemecssfile) {
156         $css_tpl .= file_get_contents($schemecssfile);
157         $scheme_modified = filemtime($schemecssfile);
158 }
159
160 // We need to check which is the most recent css data.
161 // We will use this time later to decide if we load the cached or fresh css data.
162 if ($scheme_modified > $modified) {
163         $modified = $scheme_modified;
164 }
165 // Apply the settings to the css template.
166 $css = str_replace(array_keys($options), array_values($options), $css_tpl);
167
168 $modified = gmdate('r', $modified);
169
170 $etag = md5($css);
171
172 // Set a header for caching.
173 header('Cache-Control: public');
174 header('ETag: "'.$etag.'"');
175 header('Last-Modified: '.$modified);
176
177 // Only send the CSS file if it was changed.
178 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) || isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
179         $cached_modified = gmdate('r', strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']));
180         $cached_etag = str_replace(array('"', "-gzip"), array('', ''),
181                                 stripslashes($_SERVER['HTTP_IF_NONE_MATCH']));
182
183         if (($cached_modified == $modified) && ($cached_etag == $etag)) {
184                 header('HTTP/1.1 304 Not Modified');
185                 exit();
186         }
187 }
188
189 echo $css;