]> git.mxchange.org Git - friendica.git/blob - view/theme/frio/style.php
Move PConfig::set() to DI::pConfig()->set()
[friendica.git] / view / theme / frio / style.php
1 <?php
2 /**
3  * @file view/theme/frio/style.php
4  */
5
6 use Friendica\Core\Config;
7 use Friendica\Core\PConfig;
8 use Friendica\DI;
9 use Friendica\Util\Strings;
10
11 require_once 'view/theme/frio/php/PHPColors/Color.php';
12
13 $scheme = '';
14 $schemecss = '';
15 $schemecssfile = false;
16 $scheme_modified = 0;
17
18 Config::load('frio');
19
20 // Default to hard-coded values for empty settings
21 $scheme           = Config::get('frio', 'scheme', Config::get('frio', 'schema'));
22 $nav_bg           = Config::get('frio', 'nav_bg')           ?: '#708fa0';
23 $nav_icon_color   = Config::get('frio', 'nav_icon_color')   ?: '#ffffff';
24 $link_color       = Config::get('frio', 'link_color')       ?: '#6fdbe8';
25 $background_color = Config::get('frio', 'background_color') ?: '#ededed';
26 $contentbg_transp = Config::get('frio', 'contentbg_transp') ?: '';
27 $background_image = Config::get('frio', 'background_image') ?: 'img/none.png';
28 $bg_image_option  = Config::get('frio', 'bg_image_option')  ?: '';
29 $login_bg_image   = Config::get('frio', 'login_bg_image')   ?: '';
30 $login_bg_color   = Config::get('frio', 'login_bg_color')   ?: '';
31 $modified         = Config::get('frio', 'css_modified')     ?: time();
32
33 if (!$login_bg_image && !$login_bg_color) {
34         $login_bg_image = 'img/login_bg.jpg';
35 }
36 $login_bg_color = $login_bg_color ?: '#ededed';
37
38 // Get the UID of the profile owner.
39 $uid = $_REQUEST['puid'] ?? 0;
40 if ($uid) {
41         DI::pConfig()->load($uid, 'frio');
42
43         // Only override display settings that have actually been set
44         $scheme           = DI::pConfig()->get($uid, 'frio', 'scheme', DI::pConfig()->get($uid, 'frio', 'schema')) ?: $scheme;
45         $nav_bg           = DI::pConfig()->get($uid, 'frio', 'nav_bg')           ?: $nav_bg;
46         $nav_icon_color   = DI::pConfig()->get($uid, 'frio', 'nav_icon_color')   ?: $nav_icon_color;
47         $link_color       = DI::pConfig()->get($uid, 'frio', 'link_color')       ?: $link_color;
48         $background_color = DI::pConfig()->get($uid, 'frio', 'background_color') ?: $background_color;
49         $contentbg_transp = DI::pConfig()->get($uid, 'frio', 'contentbg_transp') ?: $contentbg_transp;
50         $background_image = DI::pConfig()->get($uid, 'frio', 'background_image') ?: $background_image;
51         $bg_image_option  = DI::pConfig()->get($uid, 'frio', 'bg_image_option')  ?: $bg_image_option;
52         $modified         = DI::pConfig()->get($uid, 'frio', 'css_modified')     ?: $modified;
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 scheme
57 // Setting $scheme 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 scheme.
60 if (!empty($_REQUEST['scheme'])) {
61         $scheme = $_REQUEST['scheme'];
62 }
63
64 $scheme = Strings::sanitizeFilePathItem($scheme);
65
66 if (($scheme) && ($scheme != '---')) {
67         if (file_exists('view/theme/frio/scheme/' . $scheme . '.php')) {
68                 $schemefile = 'view/theme/frio/scheme/' . $scheme . '.php';
69                 require_once $schemefile;
70         }
71         if (file_exists('view/theme/frio/scheme/' . $scheme . '.css')) {
72                 $schemecssfile = 'view/theme/frio/scheme/' . $scheme . '.css';
73         }
74 }
75
76 // If we haven't got a scheme, 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 scheme files.
79 if (!$scheme) {
80         if (file_exists('view/theme/frio/scheme/default.php')) {
81                 $schemefile = 'view/theme/frio/scheme/default.php';
82                 require_once $schemefile;
83         }
84         if (file_exists('view/theme/frio/scheme/default.css')) {
85                 $schemecssfile = 'view/theme/frio/scheme/default.css';
86         }
87 }
88
89 $contentbg_transp = ((isset($contentbg_transp) && $contentbg_transp != '') ? $contentbg_transp : 100);
90
91 // Calculate some colors in dependance of existing colors.
92 // Some colors are calculated to don't have too many selection
93 // fields in the theme settings.
94 if (!isset($menu_background_hover_color)) {
95         $mbhc = new Color($nav_bg);
96         $mcolor = $mbhc->getHex();
97
98         if ($mbhc->isLight($mcolor, 75)) {
99                 $menu_is = 'light';
100                 $menu_background_hover_color = '#' . $mbhc->darken(5);
101         } else {
102                 $menu_is = 'dark';
103                 $menu_background_hover_color = '#' . $mbhc->lighten(5);
104         }
105 }
106 if (!isset($nav_icon_hover_color)) {
107         $nihc = new Color($nav_bg);
108
109         if ($nihc->isLight()) {
110                 $nav_icon_hover_color = '#' . $nihc->darken(10);
111         } else {
112                 $nav_icon_hover_color = '#' . $nihc->lighten(10);
113         }
114 }
115 if (!isset($link_hover_color)) {
116         $lhc = new Color($link_color);
117         $lcolor = $lhc->getHex();
118
119         if ($lhc->isLight($lcolor, 75)) {
120                 $link_hover_color = '#' . $lhc->darken(5);
121         } else {
122                 $link_hover_color = '#' . $lhc->lighten(5);
123         }
124 }
125
126 // Convert $bg_image_options into css.
127 if (!isset($bg_image_option)) {
128         $bg_image_option = null;
129 }
130
131 switch ($bg_image_option) {
132         case 'stretch':
133                 $background_size_img = '100%';
134                 $background_repeat = 'no-repeat';
135                 break;
136         case 'cover':
137                 $background_size_img = 'cover';
138                 $background_repeat = 'no-repeat';
139                 break;
140         case 'repeat':
141                 $background_size_img = 'auto';
142                 $background_repeat = 'repeat';
143                 break;
144         case 'contain':
145                 $background_size_img = 'contain';
146                 $background_repeat = 'repeat';
147                 break;
148
149         default:
150                 $background_size_img = 'auto';
151                 $background_repeat = 'no-repeat';
152                 break;
153 }
154
155 // Convert transparency level from percentage to opacity value.
156 $contentbg_transp = $contentbg_transp / 100;
157
158 $options = [
159         '$nav_bg'                      => $nav_bg,
160         '$nav_icon_color'              => $nav_icon_color,
161         '$nav_icon_hover_color'        => $nav_icon_hover_color,
162         '$link_color'                  => $link_color,
163         '$link_hover_color'            => $link_hover_color,
164         '$menu_background_hover_color' => $menu_background_hover_color,
165         '$btn_primary_color'           => $nav_icon_color,
166         '$btn_primary_hover_color'     => $menu_background_hover_color,
167         '$background_color'            => $background_color,
168         '$contentbg_transp'            => $contentbg_transp,
169         '$background_image'            => $background_image,
170         '$background_size_img'         => $background_size_img,
171         '$background_repeat'           => $background_repeat,
172         '$login_bg_image'              => $login_bg_image,
173         '$login_bg_color'              => $login_bg_color
174 ];
175
176 $css_tpl = file_get_contents('view/theme/frio/css/style.css');
177
178 // Get the content of the scheme css file and the time of the last file change.
179 if ($schemecssfile) {
180         $css_tpl .= file_get_contents($schemecssfile);
181         $scheme_modified = filemtime($schemecssfile);
182 }
183
184 // We need to check which is the most recent css data.
185 // We will use this time later to decide if we load the cached or fresh css data.
186 if ($scheme_modified > $modified) {
187         $modified = $scheme_modified;
188 }
189 // Apply the settings to the css template.
190 $css = str_replace(array_keys($options), array_values($options), $css_tpl);
191
192 $modified = gmdate('r', $modified);
193
194 $etag = md5($css);
195
196 // Set a header for caching.
197 header('Cache-Control: public');
198 header('ETag: "' . $etag . '"');
199 header('Last-Modified: ' . $modified);
200
201 // Only send the CSS file if it was changed.
202 /// @todo Check if this works at all (possibly clients are sending only the one or the other header) - compare with mod/photo.php
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(['"', '-gzip'], ['', ''],
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;