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