]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/theme.php
Merge branch '0.9.x' of gitorious.org:statusnet/mainline into 1.0.x
[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 $name = null;
58     var $dir  = null;
59     var $path = null;
60
61     /**
62      * Constructor
63      *
64      * Determines the proper directory and path for this theme.
65      *
66      * @param string $name Name of the theme; defaults to config value
67      */
68
69     function __construct($name=null)
70     {
71         if (empty($name)) {
72             $name = common_config('site', 'theme');
73         }
74         if (!self::validName($name)) {
75             throw new ServerException("Invalid theme name.");
76         }
77         $this->name = $name;
78
79         // Check to see if it's in the local dir
80
81         $localroot = self::localRoot();
82
83         $fulldir = $localroot.'/'.$name;
84
85         if (file_exists($fulldir) && is_dir($fulldir)) {
86             $this->dir  = $fulldir;
87             $this->path = $this->relativeThemePath('local', 'local', 'theme/' . $name);
88             return;
89         }
90
91         // Check to see if it's in the distribution dir
92
93         $instroot = self::installRoot();
94
95         $fulldir = $instroot.'/'.$name;
96
97         if (file_exists($fulldir) && is_dir($fulldir)) {
98
99             $this->dir = $fulldir;
100             $this->path = $this->relativeThemePath('theme', 'theme', $name);
101         }
102     }
103
104     /**
105      * Build a full URL to the given theme's base directory, possibly
106      * using an offsite theme server path.
107      * 
108      * @param string $group configuration section name to pull paths from
109      * @param string $fallbackSubdir default subdirectory under INSTALLDIR
110      * @param string $name theme name
111      * 
112      * @return string URL
113      * 
114      * @todo consolidate code with that for other customizable paths
115      */
116
117     protected function relativeThemePath($group, $fallbackSubdir, $name)
118     {
119         $path = common_config($group, 'path');
120
121         if (empty($path)) {
122             $path = common_config('site', 'path') . '/';
123             if ($fallbackSubdir) {
124                 $path .= $fallbackSubdir . '/';
125             }
126         }
127
128         if ($path[strlen($path)-1] != '/') {
129             $path .= '/';
130         }
131
132         if ($path[0] != '/') {
133             $path = '/'.$path;
134         }
135
136         $server = common_config($group, 'server');
137
138         if (empty($server)) {
139             $server = common_config('site', 'server');
140         }
141
142         $ssl = common_config($group, 'ssl');
143
144         if (is_null($ssl)) { // null -> guess
145             if (common_config('site', 'ssl') == 'always' &&
146                 !common_config($group, 'server')) {
147                 $ssl = true;
148             } else {
149                 $ssl = false;
150             }
151         }
152
153         $protocol = ($ssl) ? 'https' : 'http';
154
155         $path = $protocol . '://'.$server.$path.$name;
156         return $path;
157     }
158
159     /**
160      * Gets the full local filename of a file in this theme.
161      *
162      * @param string $relative relative name, like 'logo.png'
163      *
164      * @return string full pathname, like /var/www/mublog/theme/default/logo.png
165      */
166
167     function getFile($relative)
168     {
169         return $this->dir.'/'.$relative;
170     }
171
172     /**
173      * Gets the full HTTP url of a file in this theme
174      *
175      * @param string $relative relative name, like 'logo.png'
176      *
177      * @return string full URL, like 'http://example.com/theme/default/logo.png'
178      */
179
180     function getPath($relative)
181     {
182         return $this->path.'/'.$relative;
183     }
184
185     /**
186      * Fetch a list of other themes whose CSS needs to be pulled in before
187      * this theme's, based on following the theme.ini 'include' settings.
188      * (May be empty if this theme has no include dependencies.)
189      *
190      * @return array of strings with theme names
191      */
192     function getDeps()
193     {
194         $chain = $this->doGetDeps(array($this->name));
195         array_pop($chain); // Drop us back off
196         return $chain;
197     }
198
199     protected function doGetDeps($chain)
200     {
201         $data = $this->getMetadata();
202         if (!empty($data['include'])) {
203             $include = $data['include'];
204
205             // Protect against cycles!
206             if (!in_array($include, $chain)) {
207                 try {
208                     $theme = new Theme($include);
209                     array_unshift($chain, $include);
210                     return $theme->doGetDeps($chain);
211                 } catch (Exception $e) {
212                     common_log(LOG_ERR,
213                             "Exception while fetching theme dependencies " .
214                             "for $this->name: " . $e->getMessage());
215                 }
216             }
217         }
218         return $chain;
219     }
220
221     /**
222      * Pull data from the theme's theme.ini file.
223      * @fixme calling getFile will fall back to default theme, this may be unsafe.
224      * 
225      * @return associative array of strings
226      */
227     function getMetadata()
228     {
229         $iniFile = $this->getFile('theme.ini');
230         if (file_exists($iniFile)) {
231             return parse_ini_file($iniFile);
232         } else {
233             return array();
234         }
235     }
236
237     /**
238      * Gets the full path of a file in a theme dir based on its relative name
239      *
240      * @param string $relative relative path within the theme directory
241      * @param string $name     name of the theme; defaults to current theme
242      *
243      * @return string File path to the theme file
244      */
245
246     static function file($relative, $name=null)
247     {
248         $theme = new Theme($name);
249         return $theme->getFile($relative);
250     }
251
252     /**
253      * Gets the full URL of a file in a theme dir based on its relative name
254      *
255      * @param string $relative relative path within the theme directory
256      * @param string $name     name of the theme; defaults to current theme
257      *
258      * @return string URL of the file
259      */
260
261     static function path($relative, $name=null)
262     {
263         $theme = new Theme($name);
264         return $theme->getPath($relative);
265     }
266
267     /**
268      * list available theme names
269      *
270      * @return array list of available theme names
271      */
272
273     static function listAvailable()
274     {
275         $local   = self::subdirsOf(self::localRoot());
276         $install = self::subdirsOf(self::installRoot());
277
278         $i = array_search('base', $install);
279
280         unset($install[$i]);
281
282         return array_merge($local, $install);
283     }
284
285     /**
286      * Utility for getting subdirs of a directory
287      *
288      * @param string $dir full path to directory to check
289      *
290      * @return array relative filenames of subdirs, or empty array
291      */
292
293     protected static function subdirsOf($dir)
294     {
295         $subdirs = array();
296
297         if (is_dir($dir)) {
298             if ($dh = opendir($dir)) {
299                 while (($filename = readdir($dh)) !== false) {
300                     if ($filename != '..' && $filename !== '.' &&
301                         is_dir($dir.'/'.$filename)) {
302                         $subdirs[] = $filename;
303                     }
304                 }
305                 closedir($dh);
306             }
307         }
308
309         return $subdirs;
310     }
311
312     /**
313      * Local root dir for themes
314      *
315      * @return string local root dir for themes
316      */
317
318     protected static function localRoot()
319     {
320         $basedir = common_config('local', 'dir');
321
322         if (empty($basedir)) {
323             $basedir = INSTALLDIR . '/local';
324         }
325
326         return $basedir . '/theme';
327     }
328
329     /**
330      * Root dir for themes that are shipped with StatusNet
331      *
332      * @return string root dir for StatusNet themes
333      */
334
335     protected static function installRoot()
336     {
337         $instroot = common_config('theme', 'dir');
338
339         if (empty($instroot)) {
340             $instroot = INSTALLDIR.'/theme';
341         }
342
343         return $instroot;
344     }
345
346     static function validName($name)
347     {
348         return preg_match('/^[a-z0-9][a-z0-9_-]*$/i', $name);
349     }
350 }