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