]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Design.php
Design::url() will use HTTPS if page is HTTPS
[quix0rs-gnu-social.git] / classes / Design.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2009, StatusNet, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('STATUSNET') && !defined('LACONICA')) {
21     exit(1);
22 }
23
24 define('BACKGROUND_ON', 1);
25 define('BACKGROUND_OFF', 2);
26 define('BACKGROUND_TILE', 4);
27
28 /**
29  * Table Definition for design
30  */
31
32 require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
33 require_once INSTALLDIR . '/lib/webcolor.php';
34
35 class Design extends Memcached_DataObject
36 {
37     ###START_AUTOCODE
38     /* the code below is auto generated do not remove the above tag */
39
40     public $__table = 'design';                          // table name
41     public $id;                              // int(4)  primary_key not_null
42     public $backgroundcolor;                 // int(4)
43     public $contentcolor;                    // int(4)
44     public $sidebarcolor;                    // int(4)
45     public $textcolor;                       // int(4)
46     public $linkcolor;                       // int(4)
47     public $backgroundimage;                 // varchar(255)
48     public $disposition;                     // tinyint(1)   default_1
49
50     /* Static get */
51     function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Design',$k,$v); }
52
53     /* the code above is auto generated do not remove the tag below */
54     ###END_AUTOCODE
55
56     function showCSS($out)
57     {
58         $css = '';
59
60         $bgcolor = Design::toWebColor($this->backgroundcolor);
61
62         if (!empty($bgcolor)) {
63             $css .= 'body { background-color: #' . $bgcolor->hexValue() . ' }' . "\n";
64         }
65
66         $ccolor  = Design::toWebColor($this->contentcolor);
67
68         if (!empty($ccolor)) {
69             $css .= '#content, #site_nav_local_views .current a { background-color: #';
70             $css .= $ccolor->hexValue() . '} '."\n";
71         }
72
73         $sbcolor = Design::toWebColor($this->sidebarcolor);
74
75         if (!empty($sbcolor)) {
76             $css .= '#aside_primary { background-color: #'. $sbcolor->hexValue() . ' }' . "\n";
77         }
78
79         $tcolor  = Design::toWebColor($this->textcolor);
80
81         if (!empty($tcolor)) {
82             $css .= 'html body { color: #'. $tcolor->hexValue() . ' }'. "\n";
83         }
84
85         $lcolor  = Design::toWebColor($this->linkcolor);
86
87         if (!empty($lcolor)) {
88             $css .= 'a { color: #' . $lcolor->hexValue() . ' }' . "\n";
89         }
90
91         if (!empty($this->backgroundimage) &&
92             $this->disposition & BACKGROUND_ON) {
93
94            $repeat = ($this->disposition & BACKGROUND_TILE) ?
95                'background-repeat:repeat;' :
96                'background-repeat:no-repeat;';
97
98             $css .= 'body { background-image:url(' .
99                 Design::url($this->backgroundimage) .
100                 '); ' . $repeat . ' background-attachment:fixed; }' . "\n";
101         }
102
103         if (0 != mb_strlen($css)) {
104             $out->style($css);
105         }
106     }
107
108     static function toWebColor($color)
109     {
110         if ($color == null) {
111             return null;
112         }
113
114         try {
115             return new WebColor($color);
116         } catch (WebColorException $e) {
117             // This shouldn't happen
118             common_log(LOG_ERR, "Unable to create web color for $color",
119                 __FILE__);
120             return null;
121         }
122     }
123
124     static function filename($id, $extension, $extra=null)
125     {
126         return $id . (($extra) ? ('-' . $extra) : '') . $extension;
127     }
128
129     static function path($filename)
130     {
131         $dir = common_config('background', 'dir');
132
133         if ($dir[strlen($dir)-1] != '/') {
134             $dir .= '/';
135         }
136
137         return $dir . $filename;
138     }
139
140     static function url($filename)
141     {
142         if (StatusNet::isHTTPS()) {
143
144             $sslserver = common_config('background', 'sslserver');
145
146             if (empty($sslserver)) {
147                 // XXX: this assumes that background dir == site dir + /background/
148                 // not true if there's another server
149                 $server = common_config('site', 'server');
150                 $path   = common_config('site', 'path') . '/background/';
151             } else {
152                 $server = $sslserver;
153                 $path   = common_config('background', 'sslpath');
154                 if (empty($path)) {
155                     $path = common_config('background', 'path');
156                 }
157             }
158
159             $protocol = 'https';
160
161         } else {
162
163             $path = common_config('background', 'path');
164
165             $server = common_config('background', 'server');
166
167             if (empty($server)) {
168                 $server = common_config('site', 'server');
169             }
170
171             $protocol = 'http';
172         }
173
174         if ($path[strlen($path)-1] != '/') {
175             $path .= '/';
176         }
177
178         if ($path[0] != '/') {
179             $path = '/'.$path;
180         }
181
182         return $protocol.'://'.$server.$path.$filename;
183     }
184
185     function setDisposition($on, $off, $tile)
186     {
187         if ($on) {
188             $this->disposition |= BACKGROUND_ON;
189         } else {
190             $this->disposition &= ~BACKGROUND_ON;
191         }
192
193         if ($off) {
194             $this->disposition |= BACKGROUND_OFF;
195         } else {
196             $this->disposition &= ~BACKGROUND_OFF;
197         }
198
199         if ($tile) {
200             $this->disposition |= BACKGROUND_TILE;
201         } else {
202             $this->disposition &= ~BACKGROUND_TILE;
203         }
204     }
205
206     /**
207      * Return a design object based on the configured site design.
208      *
209      * @return Design a singleton design object for the site.
210      */
211
212     static function siteDesign()
213     {
214         static $siteDesign = null;
215
216         if (empty($siteDesign)) {
217
218             $siteDesign = new Design();
219
220             $attrs = array('backgroundcolor',
221                            'contentcolor',
222                            'sidebarcolor',
223                            'textcolor',
224                            'linkcolor',
225                            'backgroundimage',
226                            'disposition');
227
228             foreach ($attrs as $attr) {
229                 $val = common_config('design', $attr);
230                 if ($val !== false) {
231                     $siteDesign->$attr = $val;
232                 }
233             }
234         }
235
236         return $siteDesign;
237     }
238 }