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