]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/theme.php
Basic custom CSS and theme uploading features. 'local' subdir can now be customized...
[quix0rs-gnu-social.git] / lib / theme.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Utilities for theme files and paths
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Paths
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @author    Sarven Capadisli <csarven@status.net>
26  * @copyright 2008-2009 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
32     exit(1);
33 }
34
35 /**
36  * Class for querying and manipulating a theme
37  *
38  * Themes are directories with some expected sub-directories and files
39  * in them. They're found in either local/theme (for locally-installed themes)
40  * or theme/ subdir of installation dir.
41  * 
42  * Note that the 'local' directory can be overridden as $config['local']['path']
43  * and $config['local']['dir'] etc.
44  *
45  * This used to be a couple of functions, but for various reasons it's nice
46  * to have a class instead.
47  *
48  * @category Output
49  * @package  StatusNet
50  * @author   Evan Prodromou <evan@status.net>
51  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
52  * @link     http://status.net/
53  */
54
55 class Theme
56 {
57     var $dir  = null;
58     var $path = null;
59
60     /**
61      * Constructor
62      *
63      * Determines the proper directory and path for this theme.
64      *
65      * @param string $name Name of the theme; defaults to config value
66      */
67
68     function __construct($name=null)
69     {
70         if (empty($name)) {
71             $name = common_config('site', 'theme');
72         }
73
74         // Check to see if it's in the local dir
75
76         $localroot = self::localRoot();
77
78         $fulldir = $localroot.'/'.$name;
79
80         if (file_exists($fulldir) && is_dir($fulldir)) {
81             $this->dir  = $fulldir;
82             $this->path = $this->relativeThemePath('local', 'local', 'theme/' . $name);
83             return;
84         }
85
86         // Check to see if it's in the distribution dir
87
88         $instroot = self::installRoot();
89
90         $fulldir = $instroot.'/'.$name;
91
92         if (file_exists($fulldir) && is_dir($fulldir)) {
93
94             $this->dir = $fulldir;
95             $this->path = $this->relativeThemePath('theme', 'theme', $name);
96         }
97     }
98
99     /**
100      * Build a full URL to the given theme's base directory, possibly
101      * using an offsite theme server path.
102      * 
103      * @param string $group configuration section name to pull paths from
104      * @param string $fallbackSubdir default subdirectory under INSTALLDIR
105      * @param string $name theme name
106      * 
107      * @return string URL
108      * 
109      * @todo consolidate code with that for other customizable paths
110      */
111
112     protected function relativeThemePath($group, $fallbackSubdir, $name)
113     {
114         $path = common_config($group, 'path');
115
116         if (empty($path)) {
117             $path = common_config('site', 'path') . '/';
118             if ($fallbackSubdir) {
119                 $path .= $fallbackSubdir . '/';
120             }
121         }
122
123         if ($path[strlen($path)-1] != '/') {
124             $path .= '/';
125         }
126
127         if ($path[0] != '/') {
128             $path = '/'.$path;
129         }
130
131         $server = common_config($group, 'server');
132
133         if (empty($server)) {
134             $server = common_config('site', 'server');
135         }
136
137         $ssl = common_config($group, 'ssl');
138
139         if (is_null($ssl)) { // null -> guess
140             if (common_config('site', 'ssl') == 'always' &&
141                 !common_config($group, 'server')) {
142                 $ssl = true;
143             } else {
144                 $ssl = false;
145             }
146         }
147
148         $protocol = ($ssl) ? 'https' : 'http';
149
150         $path = $protocol . '://'.$server.$path.$name;
151         return $path;
152     }
153
154     /**
155      * Gets the full local filename of a file in this theme.
156      *
157      * @param string $relative relative name, like 'logo.png'
158      *
159      * @return string full pathname, like /var/www/mublog/theme/default/logo.png
160      */
161
162     function getFile($relative)
163     {
164         return $this->dir.'/'.$relative;
165     }
166
167     /**
168      * Gets the full HTTP url of a file in this theme
169      *
170      * @param string $relative relative name, like 'logo.png'
171      *
172      * @return string full URL, like 'http://example.com/theme/default/logo.png'
173      */
174
175     function getPath($relative)
176     {
177         return $this->path.'/'.$relative;
178     }
179
180     /**
181      * Gets the full path of a file in a theme dir based on its relative name
182      *
183      * @param string $relative relative path within the theme directory
184      * @param string $name     name of the theme; defaults to current theme
185      *
186      * @return string File path to the theme file
187      */
188
189     static function file($relative, $name=null)
190     {
191         $theme = new Theme($name);
192         return $theme->getFile($relative);
193     }
194
195     /**
196      * Gets the full URL of a file in a theme dir based on its relative name
197      *
198      * @param string $relative relative path within the theme directory
199      * @param string $name     name of the theme; defaults to current theme
200      *
201      * @return string URL of the file
202      */
203
204     static function path($relative, $name=null)
205     {
206         $theme = new Theme($name);
207         return $theme->getPath($relative);
208     }
209
210     /**
211      * list available theme names
212      *
213      * @return array list of available theme names
214      */
215
216     static function listAvailable()
217     {
218         $local   = self::subdirsOf(self::localRoot());
219         $install = self::subdirsOf(self::installRoot());
220
221         $i = array_search('base', $install);
222
223         unset($install[$i]);
224
225         return array_merge($local, $install);
226     }
227
228     /**
229      * Utility for getting subdirs of a directory
230      *
231      * @param string $dir full path to directory to check
232      *
233      * @return array relative filenames of subdirs, or empty array
234      */
235
236     protected static function subdirsOf($dir)
237     {
238         $subdirs = array();
239
240         if (is_dir($dir)) {
241             if ($dh = opendir($dir)) {
242                 while (($filename = readdir($dh)) !== false) {
243                     if ($filename != '..' && $filename !== '.' &&
244                         is_dir($dir.'/'.$filename)) {
245                         $subdirs[] = $filename;
246                     }
247                 }
248                 closedir($dh);
249             }
250         }
251
252         return $subdirs;
253     }
254
255     /**
256      * Local root dir for themes
257      *
258      * @return string local root dir for themes
259      */
260
261     protected static function localRoot()
262     {
263         $basedir = common_config('local', 'dir');
264
265         if (empty($basedir)) {
266             $basedir = INSTALLDIR . '/local';
267         }
268
269         return $basedir . '/theme';
270     }
271
272     /**
273      * Root dir for themes that are shipped with StatusNet
274      *
275      * @return string root dir for StatusNet themes
276      */
277
278     protected static function installRoot()
279     {
280         $instroot = common_config('theme', 'dir');
281
282         if (empty($instroot)) {
283             $instroot = INSTALLDIR.'/theme';
284         }
285
286         return $instroot;
287     }
288 }